Skip to content

fix(app): gate completion notifications on turn continuation - #5383

Merged
sailist merged 2 commits into
release/2026-11-25from
bug-453-452-11-25-web-completion-notify-release
Nov 25, 2026
Merged

sailist merged 2 commits into
release/2026-11-25from
bug-453-452-11-25-web-completion-notify-release

Conversation

@sailist

@sailist sailist commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #5381

Requirement or Bug

将 #5376(b17c3ad + 9c3e168)移植到 release/2026-11-25(叠在 #5381 之上),随 11-25 发版一起发布。

Web 与 Desktop 用户开启系统完成通知并切到后台时,连续回合(排队消息、steer、hook 通知)的第一个回合结束就会弹出「回合完成」通知,等后续回合结束时再弹一次。本 PR 基于 #5359/#5381 的 turn.ended.continuation 三态契约,让 KAP 透传该字段:Web 与 Desktop 仅在 continuation === 'turn'(后面紧接连续回合)时压住不发;'background'(前台回合已结束、后台任务仍在运行)与 'none'(队列与后台任务全部结束)均正常发送完成通知。

Bug Reproduction Steps

  1. 在 Web(kimi web)或 Desktop 开启系统通知,切到其他窗口使页面不可见;
  2. 发送第一条消息,在模型回复期间再排队发送第二条消息(或触发 steer / hook 连续回合);
  3. 改动前:第一个回合结束时立即收到「回合完成」通知,后续回合结束时又收到新的完成通知。

Root Cause

根本修复(由服务端在 turn.ended 发射点给出权威后续去向,客户端不再自行猜测):

  1. KAP 未透传 continuation:ConversationFeed 将实时事件与历史 wire 记录统一折叠为 ConversationTurnHeader,供 WebSocket turn.ended / turns 与 REST /turns 共用,但 applyTurnEnded 未读取 continuation,协议 schema 与 app-core 类型也没有该字段。
  2. 客户端把每个主 turn.ended 都当作完成:settleMainTurnEnd 在每个主 agent 回合结束时调用 shouldNotifyCompletion,只要 completed、无活跃 goal、无待处理审批/提问就通知。agent 的 idle 帧和 session 的 activity: 'idle' 在连续回合间隙会出现瞬时空闲,客户端无法从这两个状态帧区分「中间回合结束」与「前台回合队列已排空」。
  3. 通知 tag 无法跨连续回合去重:maybeNotifyCompletion 使用 promptId ?? Date.now() 作为 tag,连续回合的 promptId 不同(或退化为时间戳),每个中间回合都会生成独立通知。

Code Changes

核心思路

KAP 在折叠 turn.ended 时把 continuation 写入 ConversationTurnHeader,让实时 WebSocket 帧、REST 分页与重启回放都能读到同一个字段;客户端 settleMainTurnEnd 将 turn.continuation 传给 shouldNotifyCompletion,仅当 'turn' 时拦截完成通知,'background'、'none'(或旧服务端未提供该字段)均允许弹完成通知,其余每回合副作用保持原样。

flowchart LR
  A[core / v2<br/>turn.ended.continuation] -->|serialize| B[KAP conversationFold<br/>applyTurnEnded]
  B --> C[ConversationTurnHeader<br/>continuation?]
  C -->|WS turn.ended / turns| D[app-client<br/>settleMainTurnEnd]
  C -->|REST /turns 首屏与重连兜底| D
  D --> E{shouldNotifyCompletion}
  E -->|background / none<br/>或旧服务端 undefined| F[无 goal / 无待处理交互时<br/>发完成通知]
  E -->|turn| G[不通知,保留其他回合结束副作用]
Loading

因为 WS 实时帧与 REST 分页共用 ConversationTurnHeader 和同一个 settleMainTurnEnd 入口,所以在协议头部统一透传,而不是只在 WS 帧上补字段,否则首屏/重连竞态下走分页结算时会丢失该字段并误触发通知。

 shouldNotifyCompletion(status, hasPendingApproval, hasPendingQuestion, continuation?)
+  if continuation === 'turn'
+    return false
   return status === 'idle' && !hasPendingApproval && !hasPendingQuestion

移植到 release 分支的适配

  1. KAP 契约与 feat: add default_swarm_mode config and remove the swarm mode enter reminder #5352 合并:release 分支已有 feat: add default_swarm_mode config and remove the swarm mode enter reminder #5352 default_swarm_mode(改变了配置接口 schema 与 HOST_CONTRACT 摘要)。本 PR 在保留 default_swarm_mode 的基础上运行 pnpm qa:refresh schema,contract.txt 相对 refactor(core): replace turn.ended loopEnd with continuation #5381 仅新增 3 处可选 continuation(REST /turns、WS turn、WS turns),HOST_CONTRACT 摘要更新为 1b9cf493...,HOST_API_LEVEL 保持 1。
  2. L0 首轮录制 golden 更新:fix(app): gate completion notifications on turn continuation #5376 在 main 上 CI test (1) 失败是因为新增响应字段后未刷新 L0 录制快照。本 PR 已运行 pnpm qa:refresh replay(经维护者确认),tests/scenarios/recordings/first-turn/trace.json 的 diff 仅在 4 处新增 "continuation": "none"(primary / late 两路流各自的回合结束 WS turn 帧与最终 REST turns[0]),其余帧、时序与字段均无变化。

关键决策

决策 选择 没选 理由
完成通知触发事件 保留主 agent turn.ended(含 onMainPage 竞态兜底),新增 continuation === 'turn' 过滤;'background' 与 'none' 均通知 改用 agent 的 idle 帧或 sessionWorkChanged(activity: 'idle');或仅在 'none' 通知 idle 帧在连续回合间隙会瞬时空闲,无法表达「还有排队/通知回合」;主回复结束但后台任务仍在运行('background')时也需要即时提醒用户前台回复已完成
REST /turns 同步新增字段 与 WS 共用 ConversationTurnHeader.continuation 只改 WS 帧 本地发起的请求在首个分页返回前已结束时,onMainPage 会走同一 settleMainTurnEnd 补结算,需要同一份 continuation
旧服务端缺少 continuation undefined 时回退原有判定,HOST_API_LEVEL 保持 1 无字段一律不通知或提升 HOST_API_LEVEL 旧版远程主机从未写过该字段,一律拦截会让 Desktop 连旧远程主机时静默丢失完成通知
隐式结束回合重开 turnForNumber 重开时清空 continuation 保留旧值 迟到的 step/part 重开回合后,旧的结束态字段(含 continuation)都不应残留
其他回合结束副作用 标题生成、评分、git/警告/技能刷新、未读标记、队列恢复、下一问预测仍每回合执行 随通知一起改为仅在非 'turn' 执行 避免改变既有 UI 刷新、未读与预测时序

阅读指南

  1. packages/kap-server/src/protocol/conversation.ts 与 services/conversation/conversationFold.ts:conversationContinuationSchema、协议头部字段及 applyTurnEnded / turnForNumber 折叠逻辑。
  2. packages/app-core/src/conversation/types.ts:客户端 ConversationContinuation 类型镜像。
  3. packages/app-client/src/composables/useNotification.ts 与 client/useKimiWebClient.ts:continuation 的判定与参数透传。
  4. 其余为契约快照(schema/、apiSurface.test.ts)、L0 首轮录制(recordings/first-turn/trace.json)、docs/kap.md、changeset 与三层测试,可略读。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
KAP ConversationTurnHeader(WS turn / turns 与 REST GET /api/v1/sessions/{session_id}/agents/{agent_id}/turns) 不含 continuation 已结束回合新增可选 continuation?: 'turn' | 'background' | 'none';运行中、loop 关闭及旧记录省略 通过 KAP 读取回合的 desktop / web 客户端、外部 AI Agent 与自定义界面 无(向后兼容的可选响应字段新增,不改既有字段与默认参数)
L0 首轮录制快照(tests/scenarios/recordings/first-turn/trace.json) 结束回合无 continuation 4 处结束回合新增 "continuation": "none" tests/scenarios/replay.test.ts 与 packages/app-core/test/replay 无(与协议新增字段一致,离线回放 reducer 校验通过)
Web / Desktop 完成系统通知(新服务端) 每个主 agent turn.ended 只要 completed 且无 goal / 待处理交互即通知 仅 continuation === 'turn'(后续紧接排队/steer/hook 回合)时不通知;'background'(后台任务仍在运行)与 'none' 时均正常通知 开启系统通知并使用排队、steer 或 hook 通知的 desktop 与 web 用户 无(有意取消连续回合中间的重复通知,由最终非 'turn' 回合通知)
Web / Desktop 完成系统通知(旧版远程服务端,无 continuation) 按原规则通知 保持原规则通知(undefined 回退) Desktop 连接旧版 SSH 远程主机的用户 自动回退,无需配置
回合结束时的其他副作用(标题、评分、git/警告/技能刷新、未读标记、队列恢复、下一问预测) 每个主回合结束执行 保持不变 desktop 与 web 用户 无

受影响模块与测试覆盖:

  • packages/kap-server:services/conversation.test.ts 覆盖折叠三态与旧记录缺失、实时 turn.ended 帧、REST 分页与回放透传;apiSurface.test.ts 校验 OpenAPI / AsyncAPI 契约快照与 HOST_CONTRACT 摘要。
  • packages/app-client:notification-logic.test.ts 覆盖 shouldNotifyCompletion 对 'none' / 'background' / 'turn' / undefined 及取消、待处理交互的判定矩阵;conversation-events.test.ts 覆盖真实客户端事件流中 'turn' 不通知、'background' 与 'none' 正常通知、旧帧回退通知及 'none' + interrupted 不通知。
  • tests/scenarios:replay.test.ts(L0)与 packages/app-core/test/replay 校验录制与离线回放一致;conversation.test.ts 在真实 KAP 单回合('none')、排队连续回合('turn' → 'none')、后台子代理('background' → 'none')场景中同时断言 WebSocket 实时帧与 REST 结果。

Verification

  • 本地完整 pnpm lint(独立 git worktree,0 警告 0 错误)与 check:duplication(基线 refactor(core): replace turn.ended loopEnd with continuation #5381 提交):通过,无新增重复片段。
  • 本机(macOS)目标测试 7 个文件:kap-server/test/services/conversation.test.ts、kap-server/test/apiSurface.test.ts、app-client/test/notification-logic.test.ts、app-client/test/conversation-events.test.ts、tests/scenarios/replay.test.ts、tests/scenarios/conversation.test.ts、packages/app-core/test/replay 共 136 通过、1 失败。唯一失败为 tests/scenarios/conversation.test.ts 的 waits for approval in manual mode...(item.ended 计数为 2),已在未移植本 PR 与 refactor(core): replace turn.ended loopEnd with continuation #5381 的 release 基线(feat(prediction): refine next-prompt interactions #5379 提交)上复现同样失败,属基线既有问题,与本改动无关(Linux CI 上该用例通过)。
  • m0-dev:kap-server 与 kimi-code-app(含 renderer vue-tsc)TypeScript 类型检查通过。
  • coding-win SSH 连接被重置,本分支未跑 Windows 专项(本改动不涉及路径、子进程或终端输入差异)。

Checklist

@changeset-bot

changeset-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 706bae9

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

This PR includes changesets to release 3 packages
Name Type
@moonshot-ai/kap-server Minor
@moonshot-ai/kimi-code Patch
kimi-code-app 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:24.236969Z 706bae9 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.

@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@706bae9
npx https://pkg.pr.new/@moonshot-ai/kimi-code@706bae9

commit: 706bae9

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

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

const hasPendingApproval = (rawState.approvalsBySession[sid] ?? []).length > 0;
const hasPendingQuestion = (rawState.questionsBySession[sid] ?? []).length > 0;
if (!goalActive && shouldNotifyCompletion(status, hasPendingApproval, hasPendingQuestion)) {
if (!goalActive && shouldNotifyCompletion(status, hasPendingApproval, hasPendingQuestion, opts.continuation)) {

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 client-side queued prompts before notifying

When a Web or Desktop user queues a message through the composer while a turn is running, sendPrompt stores it only in queuedBySession and does not submit it to KAP until the current turn becomes idle (usePromptPipeline.ts:479-501,588-590). The server therefore reports continuation: 'none' for that current turn, and this condition still sends the intermediate completion notification before the client flushes the queued message, followed by another notification after the next turn—the primary reproduction described by this change. Include the client-side queued/in-flight state in the suppression decision, or submit the queued prompt early enough for the server continuation to reflect it.

AGENTS.md reference: AGENTS.md:L192-L194

Useful? React with 👍 / 👎.

ratingTurnId: turn.turn_id,
predictionSourceId: turn.turn_id,
predictionTurnId: turn.ordinal,
continuation: turn.continuation,

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 Settle the final continuation from the initial page

When a newly activated conversation finishes a fast automatic continuation before its initial turns page arrives, onMainPage settles the first turn because its opener matches the optimistic prompt, but it marks the following hook/notification continuation as settled without calling settleMainTurnEnd because that turn has no optimistic opener (onMainPage lines 1889-1899). Passing 'turn' here suppresses the only notification that is evaluated, while the final turn is never evaluated, so a hidden Web/Desktop window receives no completion notification at all. The initial-page fallback needs to settle the terminal continuation in the same chain, not only the user-owned first turn.

AGENTS.md reference: AGENTS.md:L192-L194

Useful? React with 👍 / 👎.

Base automatically changed from refact-452-11-25-turn-continuation-release-11-25 to release/2026-11-25 November 25, 2026 05:35
@sailist
sailist merged commit ecc92e3 into release/2026-11-25 Nov 25, 2026
22 of 23 checks passed
@sailist
sailist deleted the bug-453-452-11-25-web-completion-notify-release branch November 25, 2026 05:44
kimi-agent-bot pushed a commit that referenced this pull request Nov 25, 2026
* refactor(core): replace turn.ended loopEnd with continuation

* fix(app): gate completion notifications on turn continuation
kimi-agent-bot pushed a commit that referenced this pull request Nov 25, 2026
* refactor(core): replace turn.ended loopEnd with continuation

* fix(app): gate completion notifications on turn continuation
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