Skip to content

fix(claude-code): 内部 Agent 回报不再提前结束飞书回合 - #3977

Merged
deepcoldy merged 2 commits into
deepcoldy:masterfrom
xinna2001:fix/claude-peer-message-turn-boundary-20261128
Nov 28, 2026
Merged

deepcoldy merged 2 commits into
deepcoldy:masterfrom
xinna2001:fix/claude-peer-message-turn-boundary-20261128

Conversation

@xinna2001

@xinna2001 xinna2001 commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

问题

Claude Code 主请求仍在执行工具或等待后续模型输出时,收到内部 Agent 回报后,飞书状态卡却显示「本轮已结束 · 等待输入」。后续执行继续发生,但答复归属被切到合成的本地回合。

这不是单纯的卡片刷新延迟。已有 #3774 门控后台任务的完成回执,#3928 保护 PTY 尚未确认提交的窗口;本次发生在已启动请求中,触发源是内部 peer 消息。

根因

CLI 将内部回报写成以下主链记录:

{
  "type": "attachment",
  "attachment": {
    "type": "queued_command",
    "commandMode": "prompt",
    "isMeta": true,
    "origin": {
      "kind": "peer",
      "from": "worker",
      "senderTaskId": "agent-1"
    },
    "prompt": "<agent-message from=\"worker\">Partial result</agent-message>"
  }
}

isSubmittedQueuedCommand() 原来没有排除 origin.kind === "peer",使这条内部回报同时影响两条路径:

  1. BridgeTurnQueue.handleTurnStart() 把它当作真人新输入,将当前请求的 terminalObserved 置为 true,然后创建 local-* 回合。
  2. BackgroundTaskTracker.observe() 推进 promptSeq,令原请求的 pendingTurnAgents() 归零。

于是原请求可以提前结算,状态卡显示结束,后续模型输出却不再归属原请求。

修复

  • 在 TranscriptEvent.attachment 上声明已有的可选 origin.kind。
  • 在共同的排队输入准入点排除 peer 来源,使其按已有的同轮内部进展路径处理;不丢弃模型收到的回报内容。
  • 不使用 isMeta 或 <agent-message> 文本判断来源。真正的人类排队输入也可能带 isMeta,或引用同样的 XML 示例。
  • 保持普通输入、无来源的旧记录、真人中途吸收、明确中断、task notification 和 sidechain 的既有行为。

运行态语义示意(不是线上截图):

场景 修复前 修复后
主请求执行中收到 peer 回报 原请求提前结束,后续转入 local 回合 原请求继续保持,回报属于内部进展
真正的最终输出及 duration 到达 最终输出可能已失去原请求归属 最终输出和终态仍归属原请求
真人另发追问或明确中断 按原有策略处理 保持原有策略

验证

在 macOS arm64、Node 24.16.0、Bun 1.4.4、Vitest 4.1.11 下完成,测试及临时探针均使用项目的 HOME/tmux/hc 围栏。

  • 新增 22 个分类、队列及计数场景(另补 1 个真实 worker 场景):未修代码上 13 失败、9 通过;修复后对应三个完整文件 441/441 通过。
  • bun test test/claude-transcript.test.ts test/bridge-turn-queue.test.ts test/background-task-tracker.test.ts:441/441 通过。
  • 新增真实 source worker + node-pty + IPC 场景:peer 回报后不提前发送该请求的 turn_terminal 或撤销 origin;最终输出先于唯一的完成终态,通过。
  • 已保存的真实记录重放:38 条 peer 回报不再切断原请求,最终仅结算原请求,2 条 assistant 记录正确归属,没有合成 local 回合。
  • bun run build:通过,包含 TypeScript 检查、域名审计、Dashboard 构建及产物/内嵌资产审计。
  • bun run typecheck:通过。
  • 四个相关完整测试文件:451/451 通过。
  • 将相同新增用例放回未加 peer guard 的官方基线:14 失败、9 通过,包含真实 worker 的提前完成复现;不是靠放宽断言取得通过。
  • 验证期间同步到了最新 master 6cd5470d4;重新构建通过,额外复跑本次真实 worker 场景及最新上游 Codex deferred 测试,1/1、21/21 通过。
  • 独立对抗复核覆盖真人插话/中断、半条 JSONL 与重放、通知/后台结束交叠;生产修改未发现新问题。发现并修复新增测试的 Bun asymmetric matcher 引用改写问题,修复后真实 worker 场景在 Node/Vitest 和原生 Bun 均通过;身份/撤销反例仍被拒绝。
  • 扩大回归的 23 个文件:1375 通过、3 失败、8 跳过。3 个失败全部来自未改动的 ipc-lark-tool-identity.test.ts;另建同一官方 HEAD 的未修改基线,对该文件得到 119 通过、相同 3 失败、7 跳过,失败名称和结果完全一致。没有修改或跳过这些授权用例来制造全绿。

扩大回归包含 Claude 生命周期与后台任务、回合权限、终态回执、模型请求时钟、契约网关,以及 Codex/Grok 转写相关测试。

影响范围与边界

  • 修复是纯转写分类逻辑,无新增定时器、I/O、配置项或提示词。
  • 可选字段向后兼容;没有 peer 来源的旧记录和其他 CLI 记录行为不变。
  • 同一分类入口覆盖新建、恢复及重放的 Claude 转写路径,不依赖 PTY/tmux 的屏幕形态或操作系统分支。
  • 未修改 daemon/卡片投递算法、身份授权策略或其他用户数据。
  • 本次未升级、重启或部署生产 Botmux;未进行真实飞书界面手动验收。

复审回归修复(db757cebcc)

问题清单与探针证据

  1. folded 真人追问可能错收 peer 的回答:真人 fold 尚未回答时折入 renderedRole:"system" 的 peer,再出现 peer answer。未修复 head 的 absorbedInputReplyText(events, humanFoldUuid) 返回 peer answer;父提交在 peer fold 处截止。根因是真人回合准入 guard 同时关闭了 AbsorbedReplyCollector 的折入边界。
  2. keepalive 中 peer 的真实工作不推进活动时间:固定时间探针中,初始活动为 00:00:02,peer 后请求开始于 00:00:13、响应完成于 00:00:14。未修复 head 的 lastServedAt 推进到 13 秒而 lastActivityAt 停在 2 秒;父提交推进到 14 秒。下游 idle auto-compact 使用该活动时间计算空闲起点。

修复将三种语义分开:真人回合准入仍排除 peer;folded-reply collector 独立识别 peer reminder 截止边界;ClaudeModelRequestClock / BackgroundTaskTracker 独立把 peer 识别为主线程真实工作。peer 仍不推进真人 promptSeq,不进入 handleAbsorbedInput,也不开启新的 attributed Lark turn。

新增回归用例覆盖批量与 live collector、plain/reminder 两种 peer 的 upkeep 时钟、后台 peer wake 的开关边沿及真人 prompt 序号保持。

交叠对抗探针

  • 人工中途干预:human → 后台 Agent → 重复 peer → interruption;实时与完整重放均保持 backgroundWait() === 1、pendingTurnAgents() === 1,peer 未推进真人 prompt 序号,中断后无假打开的主回合。
  • 重复、重放与部分观察:重复 peer 与完整事件重放结果一致。三版本 A/B 还确认:plain-peer folded 回复错挂、部分历史缺少新 api-request 时活动时间不推进,均为父提交已存在的行为,本次未引入或加重,不记为本提交回归。
  • 周期 keepalive × 长回合/后台等待:真实 ClaudeModelRequestClock 与 idle sweep 配合可控时间;peer 长回合期间 compact 0 次,响应后 30 秒仍不 compact,超过配置 idle 阈值后 compact 1 次;peer 回合中再次插入 keepalive 时 activity 仍正确推进。
  • 当前修复 diff 未发现新引入或加重的交叠问题。既有 Scenario 3 late-recovery 缺口仍保持独立,不混入本次修复。

实际验证

  • 相关 5 文件 Vitest:466/466 通过;逐文件 fenced Bun:466/466 通过。
  • 邻接 worker/daemon、folded follow-up、background authority、idle compact 7 文件:238/238 通过。
  • 变异复验:撤掉 collector 守卫 2 个用例变红;撤掉 clock 守卫 2 个变红;撤掉 tracker 守卫 1 个变红;重新允许 peer 成为真人 prompt 15 个变红。恢复守卫后全部回绿。
  • bun run typecheck、bun run build:通过;test/test-hygiene.test.ts:28/28 通过。

完整本地 unit 补充(未通过)

额外执行的完整 bun run test 退出码 1:56 个文件、131 个用例失败,66796 通过、206 跳过,另有 3 个未处理错误及一个 fork 终止超时;不得解读为全量通过。

对其中 5 个代表文件(含 Claude worker、live identity、cli-adapters、workspace recycle)做未修复基线 A/B:基线同样为 18 失败、605 通过、2 跳过,失败形状一致,主要是 worker 启动前 tmux socket 路径超长,以及 /tmp/bm0/... 与真实路径的字符串比较差异;该样本未显示本修复引入的差异。其余全量失败未逐项做基线归因,仍以隔离重跑与新 head CI 结果为准。

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复审结论:当前 head 有 2 个 P2 行为回归,暂不建议合并。 远端 head 复核时仍为 ba2b3cb4;本地 7574fbcf34 是同一补丁 rebase 到更新主干后的提交。

1. [P2] peer guard 同时删除了 folded-input 的回复截止边界,真人追问可能收到内部 Agent 的回答

位置:src/services/claude-transcript.ts:2274,受影响消费者为 src/services/claude-transcript.ts:1848(AbsorbedReplyCollector.feed)以及 src/worker.ts:14532、src/worker.ts:18545 的 folded-reply 投递路径。

isSubmittedQueuedCommand() 不只决定“是否开启新回合”,还经 isMidTurnAbsorbedQueuedCommand() 决定 folded-reply collector 何时截止。新增 peer → false 后,带 renderedRole:"system" 的 peer 折入记录不再截止前一条真人 folded 输入。

复现边界:adoptMode + followUpReply 开启;真人 reminder 尚未得到回答时又折入一条 peer reminder,随后 assistant 回答 peer。真实 absorbedInputReplyText 探针结果:

  • 当前 head:以真人 fold uuid 查询,返回 { text: "Acknowledged", lastUuid: "peer-answer" },因此可把 peer 的回答作为该真人追问的 absorbed_reply 发出。
  • 父提交同形状探针:返回 undefined,不会错发这段回答。

建议把“真人回合准入”和“任意 queued-command 折入所形成的回复边界”拆开:collector 应在 peer reminder 处截止,但 peer 不应因此进入 handleAbsorbedInput 或开启真人回合。

2. [P2] peer 引起的真实主线程工作仍被当作 idle upkeep,idle auto-compact 的活动时间停在旧值

位置:src/services/claude-transcript.ts:2274,受影响消费者为 src/services/claude-transcript.ts:1454-1482(ClaudeModelRequestClock);BackgroundTaskTracker 的 mainTurnOpen 也有同根因分类问题。

该 guard 同时令 isClaudeMainTurnStart(peer) 与 isMidTurnAbsorbedQueuedCommand(peer) 都为 false。若 peer 在 prompt-cache keepalive 回合中到达,后续真实 API 请求和已服务 assistant 响应不会清除 upkeepTurn。

真实 ClaudeModelRequestClock 固定时间探针(plain 与 renderedRole:"system" 两种 peer 形状均复现):

  • 初始真实活动:00:00:02;keepalive 后 peer 工作的请求开始于 00:00:13,响应完成于 00:00:14。
  • 当前 head:lastServedAt = 00:00:13,但 lastActivityAt 仍为 00:00:02。
  • 父提交:lastActivityAt = 00:00:14。

idle-auto-compact 正是用 lastActivityRequestAt 计算真实空闲起点;这里会把 peer 工作期间继续计为空闲,并可能在工作结束后的下一次 idle edge 立即按旧 idle 时长触发 compact。建议为这些 observer 单独提供“peer 是主线程真实工作”的分类,但不要增加真人 promptSeq,也不要开启新的 attributed Lark turn。

相关但非本 PR 新增:Scenario 3 late recovery 仍会被 peer 打断

src/services/bridge-turn-queue.ts:1507-1514 对非 meaningful queued command 会直接清掉 recentUnobservedTurn,因此走不到 src/services/bridge-turn-queue.ts:1519 的 lineage-preserving intra-turn 分支。真实 BridgeTurnQueue 探针中,om_A 先按 unobserved close 弹出,随后同 parent 链上的 tool result → peer → final → duration 最终产出 local-headless-a_final,而非重新挂回 om_A。父提交同样不能在该 peer 场景恢复,所以这项不计入上述两个新增阻断;若本次目标是完整保证 peer 轮内链,建议一并覆盖。

已执行验证

  • bun run test -- test/claude-transcript.test.ts test/background-task-tracker.test.ts test/bridge-turn-queue.test.ts test/claude-open-turn-hold.integration.test.ts:4 文件、451/451 通过。
  • 上述 4 文件分别经 fenced-run 在独立 Bun 进程执行:451/451 通过。
  • bun run build:通过(含 typecheck、domain/dist/embedded-asset audits)。
  • 远端 16 项 checks 均为 SUCCESS;现有测试未覆盖上述 folded-reply 与 upkeep-clock 交叠场景。

folded 回复截止与主线程活动观察不再复用真人回合准入结论。peer 仍不推进真人 promptSeq、不开启新的 Lark 回合;reminder 后的回答不错挂前一真人输入,keepalive 中的真实 peer 工作正确推进活动时间。

Co-Authored-By: Kimi <[email protected]>

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

两项 P2 修复已作为追加提交 db757cebcc 推到作者分支,原提交未改写。

  • folded-reply 截止边界与真人回合准入分离:peer reminder 会截止前一真人 fold 的回复窗口,但不会变成 absorbed human turn。
  • observer 活动语义分离:peer 在 keepalive / 后台 wake 中计为主线程真实工作,但不推进真人 promptSeq、不开启新的 attributed Lark turn。
  • 相关 5 文件 Vitest 466/466、逐文件 Bun 466/466;邻接 7 文件 238/238;typecheck、完整 build 与 test-hygiene 均通过。
  • 四组变异均咬红:collector 2、clock 2、tracker 1、重新接纳 peer 为真人 prompt 15;恢复后回绿。
  • 三版本交叠探针与 idle sweep 验证未发现本修复新增或加重的问题。plain-peer folded 回复、缺少新 api-request 的部分历史、Scenario 3 late recovery 均为独立既有边界,未混入本次修复。

详细探针证据与修法已追加到 PR 描述。额外启动的完整本地 unit 尚在运行,不作为上述已通过项宣称;新 head CI 可并行执行。

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

补充完整本地 unit 结果:未通过,退出码 1。总计 56 文件、131 用例失败,66796 通过、206 跳过,另有 3 个未处理错误及一个 fork 终止超时。

已对其中 5 个代表文件做未修复基线 A/B:基线同样为 18 失败、605 通过、2 跳过,失败形状一致,主要是 worker 启动前 tmux socket 路径超长,以及 /tmp/bm0/... 与真实路径的字符串比较差异;该样本未显示本修复引入的差异。

其余全量失败尚未逐项归因,不用此次抽样替代全量结论,也不把完整 unit 标成通过。上述结果已追加到 PR 描述;新 head CI 与定向复核继续作为门禁。

@deepcoldy
deepcoldy merged commit 5b6a2f6 into deepcoldy:master Nov 28, 2026
15 checks passed
@github-actions

Copy link
Copy Markdown

@xinna2001 @deepcoldy 合入 master 后的 Midscene E2E 失败:Feishu live Midscene 失败。

这次运行覆盖 b0300b5…cbeb67d12(4 个提交),同批合入的还有 #3978 #3982 #3981,失败不一定由本 PR 引起。

用例 跑道 状态 失败信息
Antigravity basic bot flow feishu-core-clis failed openThreadForMessage: couldn't find topic entry containing "e2e-antigravity-1795902111520" within 30000ms
Claude DM thread mode gives each message its own topic and session feishu-core-dm-modes-claude failed Node "feishu.runScenario" timed out after 1800000ms.
Codex DM chat mode replies inline and keeps one session feishu-core-dm-modes-codex failed Node "feishu.runScenario" timed out after 1800000ms.
Claude group reply mode new-topic starts a session per topic feishu-core-group-modes failed Node "feishu.runScenario" timed out after 1800000ms.
Claude group @ mode always ignores messages without an @ feishu-core-group-modes failed Reply "ACK-e2e-gm-always-claude-2-1795910134297 RECALL ACK-" appeared 0 time(s) within 240000ms, expected 1 (clicked 直接开启会话 0×; re-opened 1×; repo card buttons 0 visible / 0 total, anchor matches 3, …
Claude group @ mode never answers without an @ feishu-core-group-modes failed Reply "ACK-e2e-gm-never-claude-1-1795910977602 RECALL ACK-" appeared 0 time(s) within 240000ms, expected 1 (clicked 直接开启会话 0×; re-opened 1×; repo card buttons 0 visible / 0 total, anchor matches 3, n…
Claude group @ mode ambient answers without an @ but yields to others feishu-core-group-modes failed Reply "ACK-e2e-gm-ambient-claude-1-1795911803177 RECALL ACK-" appeared 0 time(s) within 240000ms, expected 1 (clicked 直接开启会话 0×; re-opened 1×; repo card buttons 0 visible / 0 total, anchor matches 3,…
Consecutive message queue feishu-core-queue failed Node "feishu.runScenario" timed out after 1800000ms.
Sandbox hides home and harness secrets feishu-core-sandbox failed openThreadForMessage: couldn't find topic entry containing "e2e-sbx-1795907974699" within 30000ms

@github-actions

Copy link
Copy Markdown

🚀 Released in v3.59.38

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