Repository navigation
refactor(core): replace turn.ended loopEnd with continuation - #5381
Conversation
🦋 Changeset detectedLatest commit: 52600c7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
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 |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 52600c785d
ℹ️ 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".
| @@ -0,0 +1,5 @@ | |||
| --- | |||
| "@moonshot-ai/core": minor | |||
There was a problem hiding this comment.
Add changesets for the other changed libraries
The changeset lists only @moonshot-ai/core, but this commit also changes library source in packages/agent-core-v2 and packages/klient, including their public turn.ended contract. The repository requires a technical changeset for every library whose source changes, so without entries for these packages their changelogs will omit this contract change; add changeset entries for both libraries.
AGENTS.md reference: AGENTS.md:L170-L171
Useful? React with 👍 / 👎.
| if (!held && this.mailbox.hasTrigger) return true; | ||
| for (const slot of this.tools.values()) { | ||
| if (slot.completionPending === true && (this.shouldQueueTaskNotification?.(slot.handler.taskId) ?? true)) { |
There was a problem hiding this comment.
Account for held mailbox work when completions drain
When a turn ends with held === true, a parked mailbox trigger, and a detached completion whose task notification is suppressed, this predicate reports no follow-up: it ignores the mailbox because it is held and ignores the pending completion because shouldQueueTaskNotification is false. Once completion mapping finishes, however, drainDetachedCompletions unconditionally clears held even for suppressed notifications, so the parked trigger schedules another turn after the event already emitted continuation: 'none' or 'background'; TUI and SDK consumers can therefore finalize or dispatch queued work immediately before an unexpected turn starts. Treat a pending completion plus an existing mailbox trigger as 'turn', or defer the continuation until completion draining determines the final state.
AGENTS.md reference: AGENTS.md:L112-L116
Useful? React with 👍 / 👎.
commit: |
Requirement or Bug
将已合入
main的 #5359(squash 提交bdae1030)移植到release/2026-11-25,随 11-25 发版一起发布。#5337 已将独立的
loop.drained/loop.idle事件收敛为turn.ended上的loopEnd?: { outcome: 'drained' | 'idle' }(已由 #5369 带入release/2026-11-25),但该字段尚未随正式版本发布。loopEnd容易让使用方误以为存在成对的loopStart生命周期事件,嵌套的.outcome也与 coreturn.ended自带的outcome: TurnOutcome重名,且「后面紧接下一回合」只能通过字段缺失来隐式推断。本 PR 在发版前将其替换为扁平三态字段continuation?: 'turn' | 'background' | 'none',让客户端直接读取回合结束后的后续去向。Bug Reproduction Steps
N/A
Root Cause
N/A
Code Changes
核心思路
以
release/2026-11-25tip(9fed8ebe,已包含 #5379 下一问预测)为基线移植38c10d16,无冲突。所触及文件与main上bdae1030完全一致(仅保留 release 已有的 #5352default_swarm_mode差异)。由 core loop 在发射
turn.ended时同步计算本回合结束后的后续去向,以扁平可选字段continuation承载,经 agent-core-v2 原样透传到 wire、node-sdk 与 klient。TUI 与 vis 改为消费三态枚举,不再依赖「字段存在与否」区分是否还有连续回合。'turn''background''none'undefined!open()),或旧版本写入的会话记录因为后续去向的判定输入在
turn.ended发射点同步可读,所以在 core 单点计算并随事件透传,客户端不需要再维护 generation 配对或猜测队列状态。关键决策
continuation?: 'turn' | 'background' | 'none'loopEnd?: { outcome }outcome: TurnOutcome撞词,也少一层嵌套'turn'shouldQueueTaskNotification,被抑制则不标'turn'completionPending一律标'turn''turn'会让客户端永远等不到终态turn-ended-loop-end.md阅读指南
packages/core/src/loop/contract/emitted.ts与packages/core/src/loop/loop-actor.ts:TurnContinuation类型与三态判定逻辑(resolveContinuation、hasFollowUpTurn、closeUnfinished)。packages/agent-core-v2/src/agent/loop/turnOps.ts、turnProjection.ts、journal.ts:事件 schema、序列化与透传。apps/kimi-code/src/tui/controllers/session-event-handler.ts与streaming-ui.ts:TUI 在'background' | 'none'时执行终态处理(含压缩恢复与 feat(prediction): refine next-prompt interactions #5180/feat(prediction): refine next-prompt interactions #5379 的下一问预测触发),'turn'保持等待。Behavior Changes and Affected Users
turn.ended事件载荷loopEnd?: { outcome: 'drained' | 'idle' }continuation?: 'turn' | 'background' | 'none'main(#5337 之后)对接loopEnd的客户端或脚本loopEnd)turn.endedloopEndcontinuation: 'turn'turn.ended字段的消费者(TUI、vis、klient)loopEnd'background'或'none'agent.events的 klient 消费者drained提示后台任务仍在运行,idle提示完成;派发队列时不通知'background'/'none'触发,'turn'不触发turn.ended标签loop idle/loop drainedcontinuation none/continuation background/continuation turn受影响模块与测试覆盖:
packages/core、packages/agent-core-v2:wait.test.ts、record.test.ts、journal.test.ts、loop.test.ts、task-tools.test.ts覆盖单回合'none'、连续/notify/取消后续'turn' → 'none'、后台任务'background' → 'none'、崩溃恢复结算;toolAdapter.test.ts覆盖取消时仍有待交接完成消息(抑制/不抑制通知)的参数化用例。apps/kimi-code(TUI):kimi-tui-message-flow.test.ts显式验证'turn'不派发客户端队列也不通知,'background'/'none'正常去重、派发与区分文案,压缩挂起恢复及会话切换不回归;prompt-prediction.test.ts验证下一问预测触发不回归。packages/klient、packages/node-sdk、apps/vis/web:契约类型断言、facade 事件校验与渲染器类型检查。Verification
pnpm lint(独立 git worktree,0 警告 0 错误)与check:duplication(基线9fed8ebe):通过,无新增重复片段。kimi-tui-message-flow.test.ts+prompt-prediction.test.ts:546/546 通过。core、agent-core-v2、klient、node-sdk、kimi-code(CLI)、vis-web、vis-server共 7 个包 TypeScript 类型检查全部通过。packages/core全量:36/36 文件、398/398 通过。packages/agent-core-v2全量:372 通过 + 1 跳过文件,7929 通过、1 失败。唯一失败为loop.test.ts > does not run loop error handlers for aborted turns(submitTurn后同步立即cancel时expectResumeMatches的history回放竞态);本 PR 的core与agent-core-v2/loop代码与main上bdae1030完全一致,且在 m0-dev 单独运行loop.test.ts72/72 通过、本机 macOS 运行 72/72 通过。Checklist
gen-changesetsskill(沿用 refactor(core): replace turn.ended loopEnd with continuation #5359 已评审的 changeset:将未发版的turn-ended-loop-end.md改写为turn-ended-continuation.md).gen-docsskill, or this PR needs no doc update.