Skip to content

fix(kap-server): report the pending interaction when a session frame is sent - #5347

Merged
wbxl2000 merged 2 commits into
mainfrom
fix/approval-dropped-by-stale-session-frame
Nov 24, 2026
Merged

wbxl2000 merged 2 commits into
mainfrom
fix/approval-dropped-by-stale-session-frame

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Desktop 和 Web 上,审批请求(提问也一样)偶尔不显示:侧栏里这个会话有 Approve 标记,工具卡显示运行中,但对话区没有审批卡片,或者卡片出现一下就消失。服务端一直在等回答,用户没有地方点,会话卡住。

这个 PR 之后,服务端推送的会话状态帧不会再和已经发出的交互帧矛盾,审批卡片不会被旧状态清掉。

Bug Reproduction Steps

问题和时机有关,机器越空闲越容易出现。两种复现方式:

必现(用来看机制)

  1. 在 packages/kap-server/src/transport/ws/v1/sessionEventBroadcaster.ts 的 sessionEvent() 里,toWireSession(...) 之前临时加一行 await new Promise((resolve) => setTimeout(resolve, 100));。
  2. kimi web,打开页面,发一条会让模型立刻调用需要审批的工具的消息(新 E2E 里用假模型:model.script(bash('sleep 1'), say('Done.')),然后 send 再 approve)。
  3. 审批卡片不出现,或者出现后立刻消失;侧栏有 Approve 标记。

不改代码

用同一个四步用例在 Web 上单 worker 重复跑(--workers=1 --repeat-each=15)。在 main 13b09d994 上 30 次里失败 3 次,报 Timed out after 30000 ms waiting for an approval request。比例不稳定,另一轮 30 次全过。

Root Cause

服务端有两条互不排队的发送路径:

  • 会话状态帧(type: "session")走一个串行的异步队列。会话的工作状态一变,就把当时的状态拷成快照、排进队列;真正发出去之前还要等几次异步读取(会话元数据、工作区)。
  • 交互帧(type: "interaction",审批和提问的出现与结束)不排队,交互一产生就直接发。

快照里有一个字段 pending_interaction,说的正是交互帧在报的那件事。快照在队列里等的时候,后产生的审批请求先发了出去;等快照发出来,它里面的 pending_interaction 已经过时:

sequenceDiagram
  participant E as 引擎
  participant B as 广播器
  participant C as 客户端
  E->>B: 回合开始(状态:无待审批)
  Note over B: 拍快照,排进队列,等异步读取
  E->>B: 审批请求
  B->>C: interaction 帧(待审批),不排队,直接发
  Note over C: 审批列表里加上这一条
  B->>C: session 帧,pending_interaction 是 none(旧快照)
  Note over C: 看到 none,清空这个会话的审批列表
  B->>C: session 帧,pending_interaction 是 approval
  Note over C: 只把侧栏标记改回 Approve,卡片回不来
Loading

客户端在 packages/app-core/src/api/daemon/eventReducer.ts 里把 pending_interaction: none 的会话帧当成「这个会话没有待处理的交互了」,清空审批列表。

抓到的帧顺序(加了 100 毫秒延迟的那次,时间是相对第一帧的毫秒数)和上图一致:166 毫秒收到 interaction,307 毫秒收到 pending_interaction: none 的 session 帧,409 毫秒收到 approval。

快照加异步队列是 #4963 引入的。#5290 在生成会话帧的路径上多加了一次异步等待,窗口变大了;这一点是推断,依据是把服务端代码回退到 #5290 之前后 30 次全过,但次数对比本身还不足以排除偶然。

这是根本修复:改的是服务端发出的帧的内容,所有 WebSocket 客户端都受益。客户端没有动。

Code Changes

核心思路

推送「会话有更新」的 session 帧时,pending_interaction 这一个字段不再用生成帧时拿到的值,改成在帧发出的那一刻读取当前值。其他字段不动:工作状态变化的帧里,activity 和 last_turn_outcome 仍然是快照。

交互帧是在交互变化的那一刻同步发出的,会话帧的这个字段是在发出的那一刻同步读取的,所以在同一条连接上,两者不会再矛盾:

情形 改动前客户端收到 改动后客户端收到
回合开始后紧跟着审批请求 待审批 → none → approval 待审批 → approval → approval
上一个审批刚答完,下一个马上到 已结束 → 待审批 → none → approval 已结束 → 待审批 → approval → approval
审批在会话帧发出之前就答完了 待审批 → 已结束 → approval → none 待审批 → 已结束 → none → none

关键决策

决策 选择 没选 理由
在哪修 服务端 客户端不再因为 none 清审批列表 旧状态帧晚到,对任何 WebSocket 客户端都是不一致的。客户端那段清理是兜底(没收到交互结束的帧时靠它清掉卡片),去掉它有别的风险
改内容还是改顺序 改内容:发送时读 pending_interaction 改顺序:让交互帧排到已排队的会话帧后面 第一版写的就是改顺序。独立评审用探针证实了它的代价:交互帧的发送被绑在全局队列上,其他会话或工作区的帧卡住时(例如挂死的网络盘),所有会话的审批都出不来,而改动前只是侧栏状态不更新。改内容不碰交互帧,没有这个耦合,也不增加延迟
哪些字段读当前值 只有 pending_interaction 整个状态都在发送时读 快照是 #4963 有意加的,为的是每一次活动状态的变化都按当时的样子报出去(先 turn 后 idle);整个改成发送时读,短暂的状态会被合并掉。pending_interaction 不一样:它的每一次变化已经由交互帧报了
在哪一步读 发送的那个同步步骤里 生成帧的最后一步 生成完到发出之间还隔着微任务,审批请求可能正好落在里面。新增的测试把请求放在生成过程的每一个位置上各试一次;放在生成的最后一步读,会在其中一个位置失败
哪些帧 所有「会话有更新」的帧:工作状态变化,以及标题和最近一条提示词的更新 只处理带快照的工作状态帧 标题和提示词的更新帧本来就在生成的最后一步读当前状态,离发送只隔一个微任务,但它正好出现在回合开始的时候,评审用探针撞出了同样的现象。客户端对这两种帧走同一段清理逻辑
读不到当前状态时 帧照原样发出 让错误冒出去(帧被丢弃,只记一条日志) 会话正在关闭或归档时,它的服务已经释放、但还登记在会话管理器里,这时读当前状态会抛错。丢掉的可能正是「回合已结束」那一帧,客户端会一直停在运行中

创建、归档、恢复、删除这几种帧没有动:客户端对它们不走那段清理逻辑;归档帧里 pending_interaction 是有意留空的。

阅读指南

  1. packages/kap-server/src/transport/ws/v1/sessionEventBroadcaster.ts:新增的 withCurrentPendingInteraction(),以及 sendGlobal() 里调用它的那一行。一共十行。
  2. packages/kap-server/test/sessionEventBroadcaster.test.ts:新增的 the pending interaction a session frame reports 一组。

Behavior Changes and Affected Users

行为 改动前 改动后 谁依赖旧行为 退路
「会话有更新」的 session 帧里的 pending_interaction 工作状态变化的帧:状态变化那一刻的值。标题和提示词更新的帧:生成帧最后一步的值 帧发出那一刻的值 Desktop 和 Web:受益,审批卡片不再被旧值清掉,侧栏标记也不会在审批答完后再闪一下。外部 KAP WebSocket 客户端:字段和取值范围不变;一个出现后在帧发出之前就结束的交互,不会再让 session 帧报出 approval 或 question,它的出现和结束仍然由 interaction 帧报告 无
同一帧里字段之间的关系 都是同一时刻的值 pending_interaction 可能比同一帧里的 activity 新,例如 activity: "idle" 配 pending_interaction: "approval"(上一回合结束的状态还没发出,新回合已经在等审批),下一帧就会是 turn 加 approval 没有找到依赖两者同一时刻的使用方:第一方客户端的侧栏状态和审批列表分别只看其中一个 无
帧发出时会话已经不在运行 报状态变化那一刻的值 报 none — 无
工作状态变化的帧里的其他字段(activity、last_turn_outcome) 状态变化那一刻的值 不变 klient 的端到端用例靠它等回合结束 —
interaction 帧;创建、归档、恢复、删除的 session 帧 — 不变 — —

REST 接口、帧的结构、docs/kap.md 和契约文件都没有变。

对应的测试都在 packages/kap-server/test/sessionEventBroadcaster.test.ts 的新分组里:

用例 守的是什么
reports an approval requested right after a turn started … 回合开始后紧跟着审批请求
reports the next approval when the previous one was answered just before it 上一个审批刚答完,下一个马上到
reports a question asked right after a turn started … 提问走同一条路径
reports an approval requested right after a turn ended … 延后入队的回合结束状态
reports the pending approval in the session frames sent after an accepted subscription got it again 订阅确认后的重发
no longer reports an approval that was answered before the session frame was sent 反方向的过时:答完之后不再报 approval
reports an approval in the frames of a started turn, at whichever point of preparing them the request arrives 请求落在生成工作状态帧的任何一个位置
reports an approval in the frame of a title change, at whichever point of preparing it the request arrives 请求落在生成标题更新帧的任何一个位置
still reports every other part of the session state as it was when it changed 其他字段仍然是快照
still sends the session frames of a session whose current state can no longer be read 读不到当前状态时不丢帧
leaves the frame of an archived session without a pending interaction 其他种类的帧不受影响

断言写的是契约:某一帧交互帧之后发出的会话帧各自报了什么。没有固定帧的条数和先后。两条「任何一个位置」的用例会一直试到会话帧先于请求发出为止,以后生成帧的步骤变多也不会失去覆盖。

验证

  • 修复前是红的:前八条在未修复的代码上都失败。例如第一条,请求之后的会话帧期望只有 session turn approval,实际还有一条 session turn none。后三条修复前后都通过,它们固定的是没变的行为。
  • 改坏会红,六种改法各自至少让一条用例失败:
    • 去掉修复:前八条。
    • 改成在生成帧的最后一步读:两条「任何一个位置」的用例。
    • 读不到时让错误冒出去:「不丢帧」那条。
    • 用到所有种类的 session 帧上:归档那条。
    • 整个状态都在发送时读:「其他字段仍然是快照」那条和三条已有用例。
    • 只升不降(当前值是 none 时不覆盖):「答完之后不再报」那条。
  • 单元和集成测试:packages/kap-server/test、tests/scenarios、packages/klient 全部通过,合并最新 main 之后一共 1702 条。
  • 端到端,加 100 毫秒延迟(验证完已去掉):
    • 修复前,Web:5 次全部失败。
    • 修复后,Web:6 次全过;Desktop:6 次全过。修复前的对照只在 Web 上做了。
  • 端到端,不加延迟:
    • 原来的复现用例,Web 单 worker:修复前 30 次里失败 3 次;修复后 30 次全过。
    • 现有整套用例:Desktop 54 条(单 worker)、Web 54 条、TUI 和 ACP 共 108 条,全部通过。合并最新 main 之后,在干净的临时 worktree 里重新构建并再跑一遍,216 条全过。
  • 平台:只在 macOS(arm64)上验证过。Linux 由 CI 验证。
  • 合并最新 main 之后,在干净的临时 worktree 里:pnpm lint、node --test 'scripts/checks/*.test.mjs'、sherif、nix 工作区检查、pnpm exec tsc -p packages/kap-server/tsconfig.json --noEmit、新增重复代码检查,都通过。

已知的限制

  • 客户端没有改。连到还没有这个修复的远程主机时,问题仍然存在。客户端要不要另外加防护,单独讨论。
  • 「为什么现在才露出来」是推断,见 Root Cause。
  • 真实模型下,回合开始到第一次工具调用隔得久,回合开始这个触发点不容易撞上。「上一个审批刚答完、下一个马上到」这一种在真实使用里更可能出现,它有单元测试,但没有在真实模型下实测过。
  • 读不到当前状态时帧照原样发出,不记日志:这发生在会话关闭的过程中,是预期内的情况。

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.

…is sent

Session update frames are queued and sent after several async reads, while
interaction frames are sent the moment an approval or a question appears. A
queued frame could therefore report pending_interaction "none" after the
client had already received the pending interaction. The desktop and web
clients then dropped the approval prompt and the session kept waiting.

Read pending_interaction at the moment the frame is sent. The rest of a
frame pushed for an activity change still describes that change, and a frame
is sent as built when the current state of its session can no longer be read.
@changeset-bot

changeset-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3a911eb

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

This PR includes changesets to release 3 packages
Name Type
kimi-code-app Patch
@moonshot-ai/kimi-code Patch
@moonshot-ai/kap-server 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 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-24T08:06:47.148011Z 3a911eb PR opened
🔒 Security Review ✅ Completed 2026-11-24T08:09:42.263862Z 3a911eb 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 24, 2026

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

commit: 3a911eb

@wbxl2000
wbxl2000 merged commit cafb392 into main Nov 24, 2026
24 checks passed
@wbxl2000
wbxl2000 deleted the fix/approval-dropped-by-stale-session-frame branch November 24, 2026 09:13
@github-actions github-actions Bot mentioned this pull request Nov 24, 2026
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