Skip to content

fix(session): recover concatenated session index records#2074

Draft
shizhigu wants to merge 2 commits into
MoonshotAI:mainfrom
shizhigu:agent/fix-corrupt-session-index
Draft

fix(session): recover concatenated session index records#2074
shizhigu wants to merge 2 commits into
MoonshotAI:mainfrom
shizhigu:agent/fix-corrupt-session-index

Conversation

@shizhigu

@shizhigu shizhigu commented Jul 22, 2026

Copy link
Copy Markdown

Related Issue

Resolve #1925

Problem

When two records in session_index.jsonl lose the newline between them, the reader sends the combined line to JSON.parse and drops both records. SessionStore.get then returns session.not_found even when the session directory is intact.

All current writers append a newline, so the write-side source is still unknown. This change limits recovery to the read path.

What changed

  • Keep the single-record JSON.parse fast path. On failure, scan adjacent JSON containers while accounting for nested values, strings, and escapes.
  • Apply valid records in file order through the existing path checks. Preserve the complete prefix before damage, then stop at the first untrusted byte.
  • Use the same recovery boundary in the v2 reader of the shared legacy index.
  • Add core and public SDK regressions plus a patch changeset.
flowchart LR
  A["physical line"] --> B{"single-record parse"}
  B -->|valid| C["validate and apply"]
  B -->|invalid| D["scan adjacent containers"]
  D --> E["apply complete records in order"]
  D -->|damage| F["stop at first untrusted byte"]
Loading

Here A and B are complete JSON records:

Input Result
A\nB A, B
AB A, B
A + truncated B A
A + garbage + B A

Recovery is read-only and does not rewrite or reindex the file.

Validation

  • Focused recovery tests: 31 passed in v1, 6 passed in v2, including a 150,000-record single-line boundary.
  • Full core suites: 3,983 passed in v1 and 3,958 passed in v2.
  • SDK resume regression: 8 passed, 1 todo. Full node-sdk suite: 231 passed, 1 todo.
  • Typechecks, oxlint, v2 domain lint, changeset status, and git diff --check: passed.

The original Windows write-side trigger was not reproduced. Tests use the missing-separator file shape from the issue.

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 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 490fb8d

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

@shizhigu
shizhigu force-pushed the agent/fix-corrupt-session-index branch from 18fcf1a to 490fb8d Compare July 22, 2026 17:57
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.

[Bug] session_index.jsonl loses newline between entries, causing session.not_found on resume

1 participant