Skip to content

refactor(core): replace turn.ended loopEnd with continuation - #5381

Merged
sailist merged 1 commit into
release/2026-11-25from
refact-452-11-25-turn-continuation-release-11-25
Nov 25, 2026
Merged

sailist merged 1 commit into
release/2026-11-25from
refact-452-11-25-turn-continuation-release-11-25

Conversation

@sailist

@sailist sailist commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

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 也与 core turn.ended 自带的 outcome: TurnOutcome 重名,且「后面紧接下一回合」只能通过字段缺失来隐式推断。本 PR 在发版前将其替换为扁平三态字段 continuation?: 'turn' | 'background' | 'none',让客户端直接读取回合结束后的后续去向。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

核心思路

以 release/2026-11-25 tip(9fed8ebe,已包含 #5379 下一问预测)为基线移植 38c10d16,无冲突。所触及文件与 main 上 bdae1030 完全一致(仅保留 release 已有的 #5352 default_swarm_mode 差异)。

由 core loop 在发射 turn.ended 时同步计算本回合结束后的后续去向,以扁平可选字段 continuation 承载,经 agent-core-v2 原样透传到 wire、node-sdk 与 klient。TUI 与 vis 改为消费三态枚举,不再依赖「字段存在与否」区分是否还有连续回合。

取值 回答什么问题 判定条件(core)
'turn' 本回合结束后是否紧接下一回合? 调度未挂起且 mailbox 有待触发工作,或仍有将入队通知的后台完成消息在生成
'background' 前台回合是否已排空、只剩后台任务? 无后续前台回合,且仍有活跃的 detached 后台任务
'none' 是否全部结束? 无后续前台回合,且无活跃后台任务
undefined 无法判定或来自旧记录 loop 正在关闭(!open()),或旧版本写入的会话记录
flowchart LR
  A[core loop-actor<br/>resolveContinuation] -->|turn.ended.continuation| B[agent-core-v2<br/>turnProjection / journal]
  B -->|TurnEnded 透传| C[node-sdk / TUI]
  B -->|agent.events| D[klient schema]
  B -->|wire record| E[vis renderers]
  C -->|continuation 为 background / none| F[派发客户端队列、发终端通知或触发下一问预测]
  C -->|continuation 为 turn| G[保持 waiting 等待下一回合]
Loading

因为后续去向的判定输入在 turn.ended 发射点同步可读,所以在 core 单点计算并随事件透传,客户端不需要再维护 generation 配对或猜测队列状态。

关键决策

决策 选择 没选 理由
字段结构 扁平枚举 continuation?: 'turn' | 'background' | 'none' 保留 loopEnd?: { outcome } 避免与 core 自带的 outcome: TurnOutcome 撞词,也少一层嵌套
连续回合的表示 显式携带 'turn' 省略字段表示还有后续回合 把「旧服务端无字段」与「确定有后续回合」区分开
待交接完成消息被抑制时 检查 shouldQueueTaskNotification,被抑制则不标 'turn' completionPending 一律标 'turn' 子 agent 收尾抑制通知时不会再开启新回合,标 'turn' 会让客户端永远等不到终态
Changeset 原地改写未发版的 turn-ended-loop-end.md 新增第二份 changeset #5337 尚未发版,避免同一变更重复累计版本条目

阅读指南

  1. packages/core/src/loop/contract/emitted.ts 与 packages/core/src/loop/loop-actor.ts:TurnContinuation 类型与三态判定逻辑(resolveContinuation、hasFollowUpTurn、closeUnfinished)。
  2. packages/agent-core-v2/src/agent/loop/turnOps.ts、turnProjection.ts、journal.ts:事件 schema、序列化与透传。
  3. 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' 保持等待。
  4. 其余为 klient / node-sdk 类型镜像、vis 标签渲染、测试断言与快照更新,可略读。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
turn.ended 事件载荷 携带 loopEnd?: { outcome: 'drained' | 'idle' } 改为 continuation?: 'turn' | 'background' | 'none' 基于未发版 main(#5337 之后)对接 loopEnd 的客户端或脚本 无(未发版契约直接替换,正式发版用户从未见过 loopEnd)
连续回合中途的 turn.ended 不携带 loopEnd 显式携带 continuation: 'turn' 检查 turn.ended 字段的消费者(TUI、vis、klient) 无(信息更明确)
回合结束时存在被抑制通知的待交接后台完成消息 不携带 loopEnd 按剩余后台任务携带 'background' 或 'none' 监听子 agent agent.events 的 klient 消费者 无(避免误报永远不来的后续回合)
TUI 完成通知、排队消息派发与下一问预测触发 drained 提示后台任务仍在运行,idle 提示完成;派发队列时不通知 行为不变,改由 'background' / 'none' 触发,'turn' 不触发 TUI 用户 无
vis turn.ended 标签 显示 loop idle / loop drained 显示 continuation none / continuation background / continuation turn 使用 vis 排查会话的开发者 无

受影响模块与测试覆盖:

  • 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):通过,无新增重复片段。
  • 本机(macOS)kimi-tui-message-flow.test.ts + prompt-prediction.test.ts:546/546 通过。
  • m0-dev:core、agent-core-v2、klient、node-sdk、kimi-code(CLI)、vis-web、vis-server 共 7 个包 TypeScript 类型检查全部通过。
  • m0-dev packages/core 全量:36/36 文件、398/398 通过。
  • m0-dev 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.ts 72/72 通过、本机 macOS 运行 72/72 通过。
  • coding-win SSH 连接被重置,本分支未跑 Windows 专项(本改动不涉及路径、子进程或终端输入差异)。

Checklist

@changeset-bot

changeset-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 52600c7

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

This PR includes changesets to release 5 packages
Name Type
@moonshot-ai/core Minor
@moonshot-ai/acp-server Patch
@moonshot-ai/agent-core-v2 Patch
@moonshot-ai/kap-server Patch
@moonshot-ai/klient 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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T05:05:37.026917Z 52600c7 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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

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 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 👍 / 👎.

Comment on lines +490 to +492
if (!held && this.mailbox.hasTrigger) return true;
for (const slot of this.tools.values()) {
if (slot.completionPending === true && (this.shouldQueueTaskNotification?.(slot.handler.taskId) ?? true)) {

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 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 👍 / 👎.

@pkg-pr-new

pkg-pr-new Bot commented Nov 25, 2026

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

commit: 52600c7

@sailist
sailist merged commit ba2491c into release/2026-11-25 Nov 25, 2026
24 checks passed
@sailist
sailist deleted the refact-452-11-25-turn-continuation-release-11-25 branch November 25, 2026 05:35
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