Skip to content

fix(agent-core): write session metadata atomically#2162

Open
morluto wants to merge 1 commit into
MoonshotAI:mainfrom
morluto:codex/fix-session-state-atomic
Open

fix(agent-core): write session metadata atomically#2162
morluto wants to merge 1 commit into
MoonshotAI:mainfrom
morluto:codex/fix-session-state-atomic

Conversation

@morluto

@morluto morluto commented Jul 24, 2026

Copy link
Copy Markdown

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

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

@changeset-bot

changeset-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3e2948a

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Corrupted session metadata prevents resume without recovery

1 participant