Skip to content

fix(sdk): count cron turns as replay turn boundaries - #3605

Merged
RealKai42 merged 1 commit into
MoonshotAI:mainfrom
huangzheng2016:fix/replay-cron-turn-boundary
Sep 7, 2026
Merged

fix(sdk): count cron turns as replay turn boundaries#3605
RealKai42 merged 1 commit into
MoonshotAI:mainfrom
huangzheng2016:fix/replay-cron-turn-boundary

Conversation

@huangzheng2016

Copy link
Copy Markdown
Contributor

Related Issue

No linked issue — reported via internal user feedback and root-caused from the affected session data.

Problem

After resuming a session whose history is dominated by cron-fired turns, response streaming collapses to 30–50 tok/s in the TUI even though the server streams at ~200 tok/s, and it reproduces on every resume of that session (a fresh session on the same machine streams at full speed).

Root cause: limitAgentReplayByTurns only counted human-typed prompts as turn boundaries — cron_job / cron_missed records were not turns. On the affected session (4,280 cron turns vs 63 real user turns) the "last 11 turns" replay fetch therefore returned 10,478 records (~9 MB, ~45% of the whole history), all of which the TUI mounts and keeps mounted for the process lifetime. Every render frame then walks ~100k mounted transcript lines; during streaming (20 fps flush) that saturates the event loop on modest hardware, so the client cannot drain the socket and the measured server-decode window stretches (the debug split attributes this starvation to "server", which is misleading).

Verified against the real session data: print mode (no TUI) streams at full speed; the mock-server drain of 4,000 tokens takes 10 ms once the transcript is bounded.

What changed

  • packages/node-sdk/src/replay.ts: treat cron_job and cron_missed replay records as turn boundaries. This mirrors the TUI transcript window's own boundary rule (CronMessageComponent is already a fold-segment boundary there). The resumed replay on the affected session drops from 10,478 to 137 records; end-to-end resume output shrinks from 31 MB to 351 KB.
  • packages/node-sdk/test/replay.test.ts (new): bounds replay volume when cron turns dominate (failed before the fix), treats cron_missed as a boundary, verifies injection bursts do not shrink the window, and pins the existing user-turn semantics.

Validation: node-sdk 138 tests, apps/kimi-code 2452 TUI tests, typecheck, oxlint — all green.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: the issue must have a maintainer's /approve).
  • 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 Sep 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b3d610c

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

@pkg-pr-new

pkg-pr-new Bot commented Sep 7, 2026

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

commit: b3d610c

Resuming a session dominated by cron-fired turns mounted an unbounded
transcript: limitAgentReplayByTurns only counted human-typed prompts as
turn boundaries, so the replay limit never engaged (10,478 records for
11 'turns' on an affected session). The mounted transcript made every
render frame O(total lines), starving the event loop during streaming
and collapsing client-side drain throughput.

Treat cron_job and cron_missed replay records as turn boundaries,
matching the TUI transcript window's own boundary rule.
@huangzheng2016
huangzheng2016 force-pushed the fix/replay-cron-turn-boundary branch from 7ed3350 to b3d610c Compare September 7, 2026 07:26
@RealKai42
RealKai42 merged commit f1e9152 into MoonshotAI:main Sep 7, 2026
15 checks passed
@github-actions github-actions Bot mentioned this pull request Sep 7, 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.

2 participants