Skip to content

feat(server): add fast disk-based snapshot reader - #975

Merged
sailist merged 2 commits into
MoonshotAI:mainfrom
sailist:feat/fast-snapshot-reader
Jun 22, 2026
Merged

feat(server): add fast disk-based snapshot reader#975
sailist merged 2 commits into
MoonshotAI:mainfrom
sailist:feat/fast-snapshot-reader

Conversation

@sailist

@sailist sailist commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue — see Problem below.

Problem

GET /sessions/{session_id}/snapshot builds the initial-sync view by walking the heavy core RPC chain (resumeSession / getContext). Under load this exceeded 5s at p99, which trips the gateway's 5s cut-off and surfaces as a 499 to the client. There was no per-request ceiling, so a slow assembly could hang the route past the proxy timeout.

What changed

Fast disk-based snapshot reader

  • Added a snapshot reader that reads state.json and wire.jsonl straight from disk, bypassing the core RPC chain for the common case.
  • Gated behind KIMI_SNAPSHOT_READER (auto by default, legacy to fall back) with a hard KIMI_SNAPSHOT_TIMEOUT_MS ceiling (default 4000ms, under the proxy cut-off). A timeout returns a structured snapshot.timeout error instead of letting the gateway 499.
  • Kept the previous watermark-stability assembly as the legacy fallback, so this is a recoverable cut-over rather than a hard switch.
  • Eagerly instantiate the reader at boot so its event-bus subscription is in place before any session publishes turn lifecycle events.

Changesets config

  • Stopped ignoring @moonshot-ai/server and the other internal packages in .changeset/config.json so they are versioned, and added a changeset so this release covers both the server change and the CLI bundle that ships it.

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.

- add ISnapshotService that reads session state and wire log directly from disk for fast initial sync
- gate the reader behind KIMI_SNAPSHOT_READER (auto/legacy) with a KIMI_SNAPSHOT_TIMEOUT_MS ceiling, keeping the legacy assembly as a fallback
- version @moonshot-ai/server and other internal packages in changesets instead of ignoring them
@changeset-bot

changeset-bot Bot commented Jun 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 11d2014

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

This PR includes changesets to release 2 packages
Name Type
@moonshot-ai/server Minor
@moonshot-ai/kimi-code Minor

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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a1aa8cc607

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +122 to +125
const [snapState, transcriptResult] = await Promise.all([
this.broadcast.getSnapshotState(sid),
this._readTranscriptCached(sid, locator.sessionDir),
]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Don't advance the snapshot cursor past the disk transcript

When a client reconnects while a prompt is active, agent records are flushed to wire.jsonl asynchronously, but WSBroadcastService advances the durable seq as soon as events are published. This Promise.all can therefore return a snapState.seq that includes a durable event while _readTranscriptCached still sees the old wire file; the response then contains stale/missing messages but tells the client to subscribe from the newer as_of_seq. Since getBufferedSince replays only events with seq > cursor.seq, that skipped event/message is not recovered by the normal snapshot→subscribe flow.

Useful? React with 👍 / 👎.

this._transcriptCache.delete(sid);
}

const transcript = await readWireTranscript(sessionDir, MAIN_AGENT_ID);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Apply wire migrations before reducing old transcripts

For sessions created with an older wire protocol and not already resumed in this daemon, this default path feeds raw wire.jsonl records directly into readWireTranscript. That helper is documented to be called after resumeSession because replay migrates outdated wire versions in place; bypassing that precondition means migration-dependent records such as v1.0 tool-call messages can be reduced with the wrong shape and produce incorrect snapshot messages, whereas the legacy MessageService resumed/migrated first.

Useful? React with 👍 / 👎.

@pkg-pr-new

pkg-pr-new Bot commented Jun 22, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@11d2014
npx https://pkg.pr.new/@moonshot-ai/kimi-code@11d2014

commit: 11d2014

Use bracket access for process.env['KIMI_SNAPSHOT_READER'] to satisfy
noPropertyAccessFromIndexSignature.
@sailist
sailist force-pushed the feat/fast-snapshot-reader branch from 8aac974 to 11d2014 Compare June 22, 2026 11:42
@sailist
sailist merged commit c5c1834 into MoonshotAI:main Jun 22, 2026
8 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 22, 2026
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.

1 participant