Skip to content

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

Open
sailist wants to merge 3 commits into
mainfrom
bug-448-436-11-24-web-completion-notify
Open

sailist wants to merge 3 commits into
mainfrom
bug-448-436-11-24-web-completion-notify

Conversation

@sailist

@sailist sailist commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #5359

Requirement or Bug

Web 与 Desktop 用户开启系统完成通知并切到后台时,连续回合(排队消息、steer、hook 通知)的第一个回合结束就会弹出「回合完成」通知,等后续回合结束时再弹一次。本 PR 基于 #5359 的 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

关键决策

决策 选择 没选 理由
完成通知触发事件 保留主 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)、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 与自定义界面 无(向后兼容的可选响应字段新增,不改既有字段与默认参数)
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: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:conversation.test.ts 在真实 KAP 单回合('none')、排队连续回合('turn' → 'none')、后台子代理('background' → 'none')场景中同时断言 WebSocket 实时帧与 REST 结果。
验证记录
  • 目标测试: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 全部通过(新增与调整断言在修复前均失败)。
  • packages/app-client/test 全量:126 个文件 2409/2409 通过。
  • 类型检查:tsc -p packages/kap-server/tsconfig.json --noEmit 与 pnpm --filter kimi-code-app run typecheck(含 renderer vue-tsc,m0 Linux)通过。
  • 本地回归:tests/scenarios/conversation.test.ts + packages/kap-server/test/services + packages/app-client/test + packages/app-core/test 共 3874 通过、1 失败。唯一失败为 conversation.test.ts 的 waits for approval in manual mode...(item.ended 计数为 2),在父分支 refactor(core): replace turn.ended loopEnd with continuation #5359(38c10d16)干净基线上可复现同样失败,与本 PR 无关。
  • 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.

- expose turn.ended continuation on KAP conversation turn headers over WebSocket and REST
- notify completion in web and desktop only when continuation is none, falling back for older servers without the field
- cover fold, live frames, client notification gating, and KAP scenarios
@changeset-bot

changeset-bot Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9c3e168

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

This PR includes changesets to release 7 packages
Name Type
@moonshot-ai/kap-server Minor
@moonshot-ai/core Minor
@moonshot-ai/kimi-code Patch
kimi-code-app Patch
@moonshot-ai/acp-server Patch
@moonshot-ai/klient Patch
@moonshot-ai/agent-core-v2 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-25T04:31:21.950838Z 9c3e168 New commits
ℹ️ 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@9c3e168
npx https://pkg.pr.new/@moonshot-ai/kimi-code@9c3e168

commit: 9c3e168

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

ℹ️ 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 Gate notifications on the client-side prompt queue

When a web or desktop user queues another prompt while the current turn is running, that prompt remains in promptStore().queuedBySession and is not yet known to the server, so the ending turn legitimately arrives with continuation: "none". This condition consequently sends a completion notification before the idle agent frame lets resumeQueueIfIdle submit the queued prompt, despite work still being pending; include the existing local-work/queue state in this notification gate.

AGENTS.md reference: AGENTS.md:L112-L115

Useful? React with 👍 / 👎.

Base automatically changed from bug-436-11-23-loop-lifecycle-notify to main November 25, 2026 04:19
- suppress completion notifications only when continuation is 'turn'
- notify on 'background' and 'none', keeping the legacy undefined fallback
- update notification tests and changeset to match

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

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

hasPendingQuestion: boolean,
continuation?: ConversationContinuation,
): boolean {
if (continuation === 'turn') return false;

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 Suppress completion for background continuations

When a detached Bash or sub-agent task remains active, the server ends the foreground turn with continuation: 'background' and later runs a notification turn ending with 'none'. Because this condition rejects only 'turn', Web/Desktop display the generic completion notification for both turns, preserving the premature and duplicate notification this change is intended to remove. Allow notifications only for 'none', while retaining the undefined fallback for legacy servers.

AGENTS.md reference: AGENTS.md:L112-L117

Useful? React with 👍 / 👎.

This branch has not been deployed

No deployments
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