kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

fix(agent-core): write session metadata atomically

Open morluto opened this issue 4 months ago • 1 comments

Related Issue

Resolve #2159

Problem

Session metadata was overwritten in place. An interrupted write could leave a partial state.json, and resume parsed that file before reconstructing any agents. The resulting raw JSON error made the session inaccessible even though its conversation records remained separate on disk.

What changed

  • Route live session metadata and offline session-store mutations through same-directory temporary files followed by atomic replacement.
  • Preserve existing permission bits, create temporary files privately, sync local file contents, and sync the parent directory after local replacement.
  • Use the OpenSSH POSIX rename extension for remote replacement, with remote mv -f as the atomic fallback when the extension is unavailable. A failed replacement keeps the previous complete target.
  • Validate persisted metadata before reconstruction and report malformed JSON or an invalid minimum shape as session.state_invalid.
  • Keep recovery policy out of scope: invalid metadata is never silently replaced with defaults.
metadata update
      |
      v
private same-dir temp -> write -> chmod -> fsync
      |                                  |
      | failure                          v
      +--------------------------> old state remains
                                         |
                                         v
                                  atomic replacement
                                         |
                                         v
                                    state.json

resume: state.json -> parse + shape check -> reconstruct agents
                         |
                         +-> session.state_invalid

Validation:

  • pnpm --filter @moonshot-ai/kaos exec vitest run test/local.test.ts test/ssh.test.ts --reporter=dot — 120 passed, 17 skipped.
  • pnpm --filter @moonshot-ai/agent-core exec vitest run test/session/init.test.ts test/session/store.test.ts test/harness/model-alias-session.test.ts --reporter=dot — 57 passed.
  • pnpm --filter @moonshot-ai/kaos typecheck
  • pnpm --filter @moonshot-ai/agent-core typecheck
  • pnpm lint — zero errors.
  • Full agent-core suite: 4,000 passed; two unrelated image-processing tests timed out under concurrent load, then their complete test file passed 57/57 in isolation.

Checklist

  • [x] I have read the CONTRIBUTING document.
  • [x] I have linked a related issue, or explained the problem above.
  • [x] I have added tests that prove my feature works.
  • [x] Ran gen-changesets skill, or this PR needs no changeset.
  • [x] Ran gen-docs skill, or this PR needs no doc update.

morluto avatar Jul 24 '26 18:07 morluto

🦋 Changeset detected

Latest commit: 3e2948a0b9cd7c37a7b88c25ac1209a7fb6709fa

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Jul 24 '26 18:07 changeset-bot[bot]