MoonshotAI / MoonshotAI/kimi-code

Corrupted session metadata prevents resume without recovery

Open
#2,159 0 comments 0 reactions 0 assignees View on GitHub

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?
  1. Create a session.
  2. Close it.
  3. Replace the session's state.json contents with incomplete JSON such as {bad json.
  4. Attempt to resume the session.
  5. 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
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:

  1. Write and close the complete temporary file.
  2. Replace the final path atomically where the backend supports it.
  3. Clean up the temporary file if replacement does not happen.
  4. Keep the existing live-session write queue so later metadata still wins.
  5. 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.json readable.
  • Live and offline session metadata mutations use the crash-safe replacement path.
  • Malformed JSON returns the existing actionable session.state_invalid error.
  • 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.