Skip to content

feat(agent-core-v2): persist the terminal turn.ended wire record - #2457

Merged
sailist merged 3 commits into
MoonshotAI:mainfrom
sailist:feat/turn-ended-wire-record
Jul 31, 2026
Merged

feat(agent-core-v2): persist the terminal turn.ended wire record#2457
sailist merged 3 commits into
MoonshotAI:mainfrom
sailist:feat/turn-ended-wire-record

Conversation

@sailist

@sailist sailist commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue — the problem is explained below.

Problem

Turn observability gaps in the v2 engine and the transcript history rebuild:

  1. The engine publishes turn.ended (with a reason) on the event bus but never persists it. After a restart, the transcript cold rebuild (wire.jsonl → snapshot) hardcodes every historical turn's state to completed — cancelled and failed turns are indistinguishable in history, and the turn's durationMs / terminal error are lost.
  2. The generated state-manifest.d.ts renders a unique symbol property key with the checker's compilation-global id (__@mediaStripSnapshotBrand@2376). That id shifts with any unrelated type addition, so the manifest churns and produces noisy diffs.

What changed

1. Persist turn.ended and rebuild it on the cold path

What was done:

  • New persisted wire op turn.ended ({turnId, reason, error?, durationMs?}), dispatched from runTurn's finally block right before the event is published — the same pattern as turn.prompt + turn.started. Older readers tolerate the unknown record type (skip + warn), so no wire protocol bump is needed, and journals without the record keep the grouping default (completed).
  • The transcript cold rebuild (foldWireRecordFacts) folds the record back into the matching turn item: terminal state (blocked folded into failed, mirroring the live wire edge), durationMs, error message, and endedAt (record time). Engine turn ids are mapped to grouping ordinals through a replay of the loop's turn-clock records (turn.prompt / turn.cancel), so hidden turns — retry turns (RetryStepRequest: a real newTurn with no context messages) and queued-then-cancelled reservations — cannot shift a later turn's terminal state onto the wrong item.
  • Test harness fix: snapshot waiters (until/take) now resolve on emit entries only, so the same-named wire record no longer shadows the turn.ended event in event-stream snapshots.
  • Tests: loop snapshots now pin the persisted record (completed and failed-with-error), a new persistence assertion covers durationMs/time, and the transcript fold gains last-wins, blockedfailed, malformed/unknown-id tolerance, and hidden-turn drift (retry / cancelled-queued) cases.

2. Stabilize unique-symbol keys in the state manifest

What was done:

  • gen-state-manifest renders __@name@NNNN unique-symbol property keys as the stable __@name form, so the generated manifest no longer churns on unrelated type additions.

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.

sailist added 2 commits July 31, 2026 15:41
- add a persisted turn.ended op (turnId, reason, error, durationMs)
  dispatched from the loop's runTurn finally block, alongside the event
- fold the record back in the transcript cold rebuild: terminal state
  (blocked folded into failed, mirroring the live wire edge), durationMs,
  error message and endedAt; journals without the record keep the
  grouping default
- restrict the test harness's snapshot waiters to emit entries so the
  same-named wire record no longer shadows the turn.ended event
The checker names a unique symbol key __@name@NNNN, where NNNN is a
compilation-global counter that shifts with unrelated type additions and
churns the generated manifest. Render the stable __@name form instead.
@changeset-bot

changeset-bot Bot commented Jul 31, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 5d5861c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@pkg-pr-new

pkg-pr-new Bot commented Jul 31, 2026

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

commit: 5d5861c

@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: d1d7bd9cf5

ℹ️ 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".

endedTurns.size > 0
? base.items.map((item) => {
if (item.kind !== 'turn') return item;
const record = endedTurns.get(item.ordinal);

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 Key turn endings by engine turn ids

When a hidden promptless turn exists, this ordinal lookup can apply the wrong terminal state to a later visible turn. I checked RetryStepRequest: it opens a real newTurn with origin retry but contributes no context messages, and groupTurns skips retry-origin messages, so after a retry turn followed by the next normal user turn the base item with ordinal 1 is the later user turn while endedTurns.get(1) is the retry's turn.ended; after restart the user's turn can show the retry's failed/cancelled state, duration, or error. The cold fold needs a mapping from persisted engine turn ids (or must skip hidden ids) rather than assuming display ordinals always match.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good catch — confirmed: RetryStepRequest opens a real newTurn with origin 'retry' and contributes no context messages, so engine turn ids drift from the grouping ordinals. Fixed in 5d5861c: the fold now replays the loop's turn-clock records (turn.prompt / turn.cancel) to find hidden turns (retry turns and queued-then-cancelled reservations), maps engine ids to ordinals past the hidden ids, and drops the hidden turns' own end records. Added regression tests for both drift shapes.

RetryStepRequest opens a real engine turn with origin 'retry' but
contributes no context messages, and a queued-then-cancelled
reservation consumes an engine id without starting. Both make engine
turn ids drift from the grouping ordinals, so matching turn.ended by
ordinal could stamp a later visible turn with the wrong terminal state.
Replay the loop's turn-clock records (turn.prompt / turn.cancel) and
map engine ids to ordinals past the hidden ids; the hidden turns' own
end records map nowhere and are dropped.
@sailist
sailist merged commit 4c4df1b into MoonshotAI:main Jul 31, 2026
15 checks passed
7723qqq added a commit to 7723qqq/kimi-code that referenced this pull request Jul 31, 2026
…shot, bash auth hardening, fs_search tool, user tool RPC

- SessionManager: ensure_loaded with is_valid_shape cold-miss rebuild (TS
  read-model bug parity); turn.ended terminal records (agent-core-v2 MoonshotAI#2457)
- TaskService: notification bookkeeping snapshot/restore for /undo (MoonshotAI#2055)
- callbacks: bash_native_authorize — dangerous commands need explicit
  user allow rule; session/auto/yolo approval no longer blanket-approves
- NativeToolset: fs_search tool (root validation, missing-root error)
- main: register/unregister tool RPCs (SDK registerTool parity)
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