MoonshotAI / MoonshotAI/kimi-code
Corrupted session metadata prevents resume without recovery
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 7.5k
- Forks
- 1.2k
- Avg merge
- 11h 53m
- Merged PRs (30d)
- 350
Description
What version of Kimi Code is running?
main at f06eb5c60e0a4e51162d1854dda1db41892b457c
Which open platform/subscription were you using?
Not provider-specific; this occurs while loading locally persisted session metadata.
Which model were you using?
Not model-specific.
What platform is your computer?
Source-level reproduction; the affected writers include local and SSH persistence paths.
What issue are you seeing?
If a session's state.json contains incomplete or otherwise invalid JSON, resuming that session rejects before any agent is reconstructed. The conversation records under the session's agent directories are stored separately, but the normal resume path cannot reach them without manually repairing the metadata.
The repository already has a test that reproduces this by replacing state.json with {bad json; resumeSession() rejects and records session_load_failed with a SyntaxError.
What steps can reproduce the bug?
- Create a session.
- Close it.
- Replace the session's
state.jsoncontents with incomplete JSON such as{bad json. - Attempt to resume the session.
- Observe that resume rejects before the main agent is reconstructed.
This is a deterministic malformed-file reproduction. An interruption during an in-place overwrite is a plausible way to produce such a file, but this report does not claim an observed production interruption.
What is the expected behavior?
Updating session metadata should not expose partially written JSON at the final path. If metadata is already invalid, resume should report an actionable session.state_invalid error while preserving the damaged file; it should not silently construct a session from empty or default metadata.
Automatic backup recovery is a separate policy decision. If added later, it should only accept a validated, known-good copy and should make recovery visible to the user.
Additional information
Confirmed source behavior
Session.writeMetadata()serializes writes but callspersistenceKaos.writeText()on the finalstate.jsonpath.- Local
Kaos.writeText()useswriteFile()and SSHKaos.writeText()uses an SFTP write on the final path. - Offline rename and archive, fork, and fork-at-turn operations also call
writeFile()on the finalstate.jsonpath. Session.resume()callsreadMetadata()before reconstructing agents.readMetadata()currently passes rawJSON.parse()failures through rather than translating them to the existing publicsession.state_invaliderror.
metadata change
|
v
write final state.json in place
|
+-- write completes --> valid new metadata
|
`-- write interrupted (hypothesis) --> partial JSON
|
v
next resume rejects
before agent replay
Proposed direction
Use same-directory temporary-file replacement for every production state.json writer:
- Write and close the complete temporary file.
- Replace the final path atomically where the backend supports it.
- Clean up the temporary file if replacement does not happen.
- Keep the existing live-session write queue so later metadata still wins.
- Translate malformed persisted metadata to
session.state_invalid, preserving the cause and the on-disk file.
The implementation needs an explicit persistence-abstraction contract because live metadata can use local or SSH-backed Kaos. Backup creation or automatic fallback is intentionally not required by this issue.
Acceptance criteria
- A failed/interrupted write before replacement leaves the previous valid
state.jsonreadable. - Live and offline session metadata mutations use the crash-safe replacement path.
- Malformed JSON returns the existing actionable
session.state_invaliderror. - Invalid metadata is never silently replaced with
{}or default session metadata. - The serialized live metadata queue retains latest-write-wins behavior.
No directly related issue or pull request was found in a narrow search for state.json corruption, session resume, and atomic session metadata writes.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with packages/agent-core/test/harness/model-alias-session.test.ts and Session.resume()/readMetadata() in packages/agent-core/src/session/index.ts, then inspect state.json writers in packages/kaos/src/local.ts, packages/kaos/src/ssh.ts, and session/store/session-store.ts. Check the existing session.state_invalid definition in errors/codes.ts. Done means all listed writers preserve the last valid metadata on interrupted writes, malformed metadata reports the actionable error, and the live queue keeps latest-write-wins behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- backend, cli
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 56/100