Skip to content

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

Merged
sailist merged 1 commit into
mainfrom
bug-436-11-23-loop-lifecycle-notify
Nov 25, 2026
Merged

sailist merged 1 commit into
mainfrom
bug-436-11-23-loop-lifecycle-notify

Conversation

@sailist

@sailist sailist commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

#5337 已将独立的 loop.drained / loop.idle 事件收敛为 turn.ended 上的 loopEnd?: { outcome: 'drained' | 'idle' },但该字段尚未随正式版本发布。loopEnd 容易让使用方误以为存在成对的 loopStart 生命周期事件,嵌套的 .outcome 也与 core turn.ended 自带的 outcome: TurnOutcome 重名,且「后面紧接下一回合」只能通过字段缺失来隐式推断。本 PR 在发版前将其替换为扁平三态字段 continuation?: 'turn' | 'background' | 'none',让客户端直接读取回合结束后的后续去向。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

核心思路

由 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 的下一问预测触发),'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' 正常去重、派发与区分文案,压缩挂起恢复及会话切换不回归。
  • packages/klient、packages/node-sdk、apps/vis/web:契约类型断言、facade 事件校验与渲染器类型检查。
验证记录
  • 类型检查:core、agent-core-v2、klient、node-sdk、apps/kimi-code、apps/vis/web、apps/vis/server 共 7 个包 tsc --noEmit 全部通过。
  • 远程 Linux 全量套件:packages/core 36/36 文件通过;packages/agent-core-v2 373 通过 + 1 跳过、0 失败。
  • Rebase 到最新 main(含 feat(prediction): refine next-prompt interactions #5180)后:TUI 类型检查通过,kimi-tui-message-flow.test.ts + prompt-prediction.test.ts 546/546 通过,toolAdapter.test.ts 本地与远程均 8/8 通过。
  • pnpm lint 与 check:duplication 全部通过。
  • 未验证项:Windows 平台(本改动不涉及路径、子进程或终端输入差异)。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 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-24T12:37:26.751807Z 38c10d1 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.

@changeset-bot

changeset-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 38c10d1

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 24, 2026

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

commit: 38c10d1

@sailist
sailist merged commit bdae103 into main Nov 25, 2026
24 checks passed
@sailist
sailist deleted the bug-436-11-23-loop-lifecycle-notify branch November 25, 2026 04:19
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
MoonshotAI#5359 and MoonshotAI#5404 need the fork's loop/ACP shapes (LiveTurn state machine,
session extensions), MoonshotAI#5434 depends on testkit signal imports, and MoonshotAI#5332
needs a merged SessionAction table. Each is in_progress with the precise
remaining work rather than a forced apply.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 26, 2026
Mark MoonshotAI#5394 ported with its cancel-during-start regression; record MoonshotAI#5359 and MoonshotAI#5351 as blocked on the TUI continuation capability and the tool activation model, and keep MoonshotAI#5432 partial until those rows close.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 26, 2026
The core loop now reports TurnContinuation on turn.ended: 'turn' when a
queued trigger or pending task completion runs next, 'background' when
only background tasks remain, and 'none' when the loop is settled. The
agent turn contract, journal, projection, klient schema and wire manifest
carry the field.

The TUI finalizeTurn consumes it: 'turn' keeps the session running and
leaves queued dispatch to the next turn, 'background' notifies that
background tasks are still running, and 'none' idles as before. The old
loop-ended changeset is replaced by the continuation changeset.

Verified: loop record/journal/wait/coreLoopService 32 pass; TUI
message-flow 489 pass; klient contract 13 pass; node-sdk event types 10
pass; core, agent-core-v2, klient, node-sdk and apps/kimi-code typecheck
clean.
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