Repository navigation
fix(kap-server): report the pending interaction when a session frame is sent - #5347
Merged
Merged
Conversation
…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.
…val-dropped-by-stale-session-frame
🦋 Changeset detectedLatest commit: 3a911eb The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
wbxl2000
approved these changes
Nov 24, 2026
Merged
This was referenced Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requirement or Bug
Desktop 和 Web 上,审批请求(提问也一样)偶尔不显示:侧栏里这个会话有
Approve标记,工具卡显示运行中,但对话区没有审批卡片,或者卡片出现一下就消失。服务端一直在等回答,用户没有地方点,会话卡住。这个 PR 之后,服务端推送的会话状态帧不会再和已经发出的交互帧矛盾,审批卡片不会被旧状态清掉。
Bug Reproduction Steps
问题和时机有关,机器越空闲越容易出现。两种复现方式:
必现(用来看机制)
packages/kap-server/src/transport/ws/v1/sessionEventBroadcaster.ts的sessionEvent()里,toWireSession(...)之前临时加一行await new Promise((resolve) => setTimeout(resolve, 100));。kimi web,打开页面,发一条会让模型立刻调用需要审批的工具的消息(新 E2E 里用假模型:model.script(bash('sleep 1'), say('Done.')),然后send再approve)。Approve标记。不改代码
用同一个四步用例在 Web 上单 worker 重复跑(
--workers=1 --repeat-each=15)。在 main13b09d994上 30 次里失败 3 次,报Timed out after 30000 ms waiting for an approval request。比例不稳定,另一轮 30 次全过。Root Cause
服务端有两条互不排队的发送路径:
type: "session")走一个串行的异步队列。会话的工作状态一变,就把当时的状态拷成快照、排进队列;真正发出去之前还要等几次异步读取(会话元数据、工作区)。type: "interaction",审批和提问的出现与结束)不排队,交互一产生就直接发。快照里有一个字段
pending_interaction,说的正是交互帧在报的那件事。快照在队列里等的时候,后产生的审批请求先发了出去;等快照发出来,它里面的pending_interaction已经过时:客户端在
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→approvalapproval→approvalnone→approvalapproval→approvalapproval→nonenone→none关键决策
none清审批列表pending_interactionpending_interactionturn后idle);整个改成发送时读,短暂的状态会被合并掉。pending_interaction不一样:它的每一次变化已经由交互帧报了创建、归档、恢复、删除这几种帧没有动:客户端对它们不走那段清理逻辑;归档帧里
pending_interaction是有意留空的。阅读指南
packages/kap-server/src/transport/ws/v1/sessionEventBroadcaster.ts:新增的withCurrentPendingInteraction(),以及sendGlobal()里调用它的那一行。一共十行。packages/kap-server/test/sessionEventBroadcaster.test.ts:新增的the pending interaction a session frame reports一组。Behavior Changes and Affected Users
session帧里的pending_interactionsession帧报出approval或question,它的出现和结束仍然由interaction帧报告pending_interaction可能比同一帧里的activity新,例如activity: "idle"配pending_interaction: "approval"(上一回合结束的状态还没发出,新回合已经在等审批),下一帧就会是turn加approvalnoneactivity、last_turn_outcome)interaction帧;创建、归档、恢复、删除的session帧REST 接口、帧的结构、
docs/kap.md和契约文件都没有变。对应的测试都在
packages/kap-server/test/sessionEventBroadcaster.test.ts的新分组里:approval断言写的是契约:某一帧交互帧之后发出的会话帧各自报了什么。没有固定帧的条数和先后。两条「任何一个位置」的用例会一直试到会话帧先于请求发出为止,以后生成帧的步骤变多也不会失去覆盖。
验证
session turn approval,实际还有一条session turn none。后三条修复前后都通过,它们固定的是没变的行为。session帧上:归档那条。none时不覆盖):「答完之后不再报」那条。packages/kap-server/test、tests/scenarios、packages/klient全部通过,合并最新 main 之后一共 1702 条。pnpm lint、node --test 'scripts/checks/*.test.mjs'、sherif、nix 工作区检查、pnpm exec tsc -p packages/kap-server/tsconfig.json --noEmit、新增重复代码检查,都通过。已知的限制
Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.