Repository navigation
fix(bridge): Claude 回到提示符时等本轮收尾记录落盘再结算,API 报错不再按正常完成收尾 - #2227
Merged
Merged
Conversation
Claude 先重绘提示符(OSC 7501 同时报 done),本轮的收尾记录(最终消息的块、 截断流的 API 报错、turn_duration)稍后才写进 transcript。回到提示符时的排空 (terminalBoundary)会把还没读到任何终态的普通回合按 completed 弹出,稍后读到 的 server_error 已经没有回合可判失败:CoT 收成完成、卡片停在等待输入、不发失败 提示也不触发自动续跑。会话第一轮还有一处同类放行:#2120 的挂起要求之前见过 turn_duration,否则每次读 transcript 后的排空一读到 end_turn 就结算。 - PTY 回到提示符时复用 Herdr 已有的有界等待(2s,100ms 轮询),直到本轮被 turn_duration 关闭;新输入写入或出现忙碌标记时取消 - 挂起改为默认开启,不再依赖之前见过 turn_duration;等满一次仍没等到才认定 CLI 不写该记录,之后恢复按 stop_reason 结算 Co-Authored-By: Claude Opus 5.5 <[email protected]>
deepcoldy
requested review from
47seek,
LucasIcarus,
xiaoxueSunn and
xiongz-c
as code owners
October 23, 2026 14:16
只喂 end_turn 的本地回合现在会挂起等 turn_duration;按真实形态补上该记录。 「空 end_turn 后晚到正文沿用回复地址」改为在已判定不写 turn_duration 的 CLI 上 验证,并新增默认挂起下晚到正文留在同一回合的断言。 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Owner
Author
|
已合并 → 8e41294(squash)。未发版、未部署。 后续可以跟进的点(不阻断本 PR):
|
|
🚀 Released in v3.51.0 |
LeonYuanYao
pushed a commit
to LeonYuanYao/botmux
that referenced
this pull request
Oct 26, 2026
deepcoldy#2227 让 Claude 回合在 idle 边沿通过 bounded settle 等待 turn_duration 等收尾记录,但该等待只能由 idle settle 触发:后台任务可让 program-status 持续 `working`、陈旧状态保持等待,没有 idle 边沿时, 全新转录的首轮会无限挂住。同时崩溃快照/轮转学到的负能力会跨 CLI 世代污染下一份转录的首轮保护。 无 idle 边沿兜底: - 新增 provisional-exit 看门狗(updateProvisionalExitWatch):transcript ingest 后若全新回合队头仍在等收尾记录且没有 idle settle 在途,按 idle settle 同一时限兜底,到期施加相同 wait-expired 判定。与 idle 路径严格 互斥:settle request 立即撤防;看门狗到期时若已确认 idle、settle 在途, 或提示符可见且无权威 held working(PTY 静默确认窗口)则让位重臂; held working 覆盖可见提示符(后台任务让 composer 与 OSC busy 并存), 兜底照常触发。 - deadline 闭包绑定所属回合(对象+turnId+attempt)与 transcript generation;fire 先 bridgeIngest 读新增字节,再做读前+读后两次世代复核, 换头/换世代立即作废并为当前队头重臂完整窗口。 - drainEmittable 新增 onlyTurn 身份闸,看门狗只释放绑定回合,绝不通过 无界 terminal-boundary drain 弹出在工具执行中的跟随 type-ahead 回合。 负能力世代隔离: - transcript 世代边界(watcher teardown/start 含 fresh-empty,/clear、 /resume、pid-state、fingerprint 换文件)统一走 beginNewBridgeTranscriptGeneration:撤防旧 deadline、bump 世代、重置 marker 判定。 - 切换时仅给已在旧 writer 启动的 pending 回合打 carriedAcrossGeneration; 未启动 mark 等在新文件绑定 live user 后取得新世代身份。carried 回合与 journal 恢复回合(restoredFromJournal)一样可在 terminal 边界放行,但 共享 timeout helper(PTY/Herdr idle onTimeout 与看门狗)不会据此向当前 live CLI 学 waitExpired。 - IdleDetector 新增 screenShowsPrompt 只读谓词(当前屏幕是否渲染输入 提示符,独立于静默窗口确认的 isIdleNow)。 影响面仅限 Claude Code 非 adopt bridge;本地终端回合、adopt、旧 marker-less CLI(首轮最多一个等待窗口)与其他 CLI adapter 行为不变。 Co-Authored-By: Claude Code <[email protected]>
LeonYuanYao
pushed a commit
to LeonYuanYao/botmux
that referenced
this pull request
Oct 26, 2026
deepcoldy#2227 让 Claude 回合在 idle 边沿通过 bounded settle 等待 turn_duration 等收尾记录,但该等待只能由 idle settle 触发:后台任务可让 program-status 持续 `working`、陈旧状态保持等待,没有 idle 边沿时, 全新转录的首轮会无限挂住。同时崩溃快照/轮转学到的负能力会跨 CLI 世代污染下一份转录的首轮保护。 无 idle 边沿兜底: - 新增 provisional-exit 看门狗(updateProvisionalExitWatch):transcript ingest 后若全新回合队头仍在等收尾记录且没有 idle settle 在途,按 idle settle 同一时限兜底,到期施加相同 wait-expired 判定。与 idle 路径严格 互斥:settle request 立即撤防;看门狗到期时若已确认 idle、settle 在途, 或提示符可见且无权威 held working(PTY 静默确认窗口)则让位重臂; held working 覆盖可见提示符(后台任务让 composer 与 OSC busy 并存), 兜底照常触发。 - deadline 闭包绑定所属回合(对象+turnId+attempt)与 transcript generation;fire 先 bridgeIngest 读新增字节,再做读前+读后两次世代复核, 换头/换世代立即作废并为当前队头重臂完整窗口。 - drainEmittable 新增 onlyTurn 身份闸,看门狗只释放绑定回合,绝不通过 无界 terminal-boundary drain 弹出在工具执行中的跟随 type-ahead 回合。 负能力世代隔离: - transcript 世代边界(watcher teardown/start 含 fresh-empty,/clear、 /resume、pid-state、fingerprint 换文件)统一走 beginNewBridgeTranscriptGeneration:撤防旧 deadline、bump 世代、重置 marker 判定。 - 切换时仅给已在旧 writer 启动的 pending 回合打 carriedAcrossGeneration; 未启动 mark 等在新文件绑定 live user 后取得新世代身份。carried 回合与 journal 恢复回合(restoredFromJournal)一样可在 terminal 边界放行,但 共享 timeout helper(PTY/Herdr idle onTimeout 与看门狗)不会据此向当前 live CLI 学 waitExpired。 - IdleDetector 新增 screenShowsPrompt 只读谓词(当前屏幕是否渲染输入 提示符,独立于静默窗口确认的 isIdleNow)。 影响面仅限 Claude Code 非 adopt bridge;本地终端回合、adopt、旧 marker-less CLI(首轮最多一个等待窗口)与其他 CLI adapter 行为不变。 Co-Authored-By: Claude Code <[email protected]>
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.
现象
线上一个 Claude 会话:某一轮的最终消息被 API 报错截断(
isApiErrorMessage: true、error: "server_error"、用量全 0),transcript 里依次是只有思考块的end_turn、API Error、turn_duration,这一轮没有触发 Stop hook。botmux 在回到提示符后 15ms 就把这一轮按完成收尾:CoT 收成「完成」,状态卡显示「等待输入」,既没有「执行失败」提示,也没有触发自动续跑。用同一份 transcript 离线整份重放,回合队列判的是失败(provider_server_error,可重试),所以问题出在实时读取时的结算时机。是哪道判断放行的
1. 生产这一次:回到提示符时,这一轮的收尾记录一条都还没读到,普通回合被屏幕 idle 直接结算
drainBridgesThenMarkReady→bridgeDrainAndMaybeEmit→emitReadyTurns→drainEmittable({ terminalBoundary: true, requireExplicitTerminalForDurable: true })。dispatchAttempt,requireExplicitTerminalForDurable拦不住它;fix(bridge): Claude 最终消息跨读切分时不再提前结算,本地回合实时失败也在群里提示 #2120 加的挂起isAwaitingDurationMarker只在已经读到 stop_reason 记录(awaitingDurationMarker)之后才会挡。这一轮一条终态记录都还没读到,所以terminalBoundary直接把它弹出,terminalOutcome为空,emitTurnTerminal(..., outcome?.status ?? 'completed')发出 completed。ingest的if (this.collecting && …)分支,但这时collecting已经随弹出被清空,失败信息被直接丢掉;turn_duration也没有回合可关。证据:
Prompt detected (idle)在 11:03:05.122Z,同一个 worker 15ms 后Released active turn authority (turn_terminal),早于prompt_ready,说明 completed 来自回到提示符时的排空。turn_duration,durationMarkerSeen为真。拿真实 transcript 离线回放、在不同读取位置模拟回到提示符:只读到思考块之前时,排空会把这一轮按 completed 弹出(与线上一致);读到思考块end_turn时会挂起、等报错和turn_duration。所以这次放行的不是「之前没见过turn_duration」,而是收尾记录还没落盘。message_delta时,会把stop_reason和用量就地写进本条消息已经产出的所有块(for (const m of blocks) { m.message.usage = …; m.message.stop_reason = … })。这条记录带end_turn、用量全 0,说明它是在流收尾时才序列化落盘的,和 API Error、turn_duration是同一批写入,而 OSC 7501 的 idle 先到了。2. 会话第一轮:同一处挂起依赖「之前见过 turn_duration」
isAwaitingDurationMarker要求durationMarkerSeen。第一轮读到思考块的end_turn时,每次读 transcript 后的emitReadyTurns({ explicitTerminalOnly: true })当场就按 completed 结算,根本等不到回到提示符。线上这次没走到这条,但新会话的第一轮会碰到。修法:选 (a),不做 (b)
(a) 回到提示符时有界等待本轮收尾
createTranscriptTerminalSettle(上限 2s,每 100ms 重读一次 transcript),直到本轮被turn_duration关闭(BridgeTurnQueue.isTurnOpenAtIdle:已开始、仍是当前回合、还没读到任何终态,或读到了 stop_reason 但还没读到turn_duration),之后照原来的顺序排空并标记就绪。不是 Claude transcript 桥接的 CLI,以及已经关闭的回合,判定当场为假,和原来走同一条同步路径。beginCliWriteCycle),或出现明确的忙碌标记,就取消这次等待:这个 idle 边沿属于已经过去的那一轮,旧回合仍由每次读取后的排空按它的 transcript 终态结算。turn_duration。只有某次回到提示符时等满上限、读到了 stop_reason 却仍没等到turn_duration,并且这个 transcript 从没出现过它,才认定这个 CLI 不写这条记录(noteDurationMarkerWaitExpired),之后恢复按 stop_reason 结算;之后一旦读到turn_duration,又恢复挂起。为什么不做 (b)(稍后读到报错时把已发的「完成」改判为「失败」):completed 一发出,下游已经按它处理完了:CoT 的
finishStatus只设一次,HTTP Wait 调用方已经拿到结果,异步结果和 durable 回执已经落盘,自动续跑也是按 completed 决定不接手。worker 侧TurnTerminalDeduper每个回合只放一次终态,IPC 也没有「更正终态」的协议。做 (b) 要新增协议,还要让每个消费方都支持撤回,HTTP 等待的响应根本撤不回来。(a) 在源头等到确定信号(turn_duration)再结算,只改 worker 一侧。影响面
bridgeJsonlPath,等待判定当场为假,同步走原来的drainBridgesThenMarkReady;adopt 模式的 idle 处理器没动;PTY 与 tmux 后端共用同一个 idle 处理器;Herdr 改为和 PTY 共用新的判定(第一轮也等);riff/mojo 等远端后端没有 idle 检测器,不受影响。final_output+ completed 的turn_terminal+prompt_ready),卡片和 CoT 的最终表现不变。只有时序变化:turn_duration还没读到的话,prompt_ready(卡片切到「等待输入」)会等它落盘后再发,排在final_output/turn_terminal之后(原来排在前面)。通常是一个刷盘间隔加一次轮询,最多 2s。turn_duration(按 fix(bridge): Claude 最终消息跨读切分时不再提前结算,本地回合实时失败也在群里提示 #2120 的测量,它落在最终块后约 1s),不再一读到 stop_reason 就结算。turn_duration的老版本 Claude:只有第一次回到提示符多等一次上限(2s),之后和现在完全一样。[Request interrupted by user]用户记录,回合队列把它当作新一轮开始——原回合有过输出就被这条记录关闭,没有输出就直接出队、队首换成本地回合(本地回合不参与等待)——所以回到提示符时的等待在这条记录读到后就结束,不会等满 2s(已用回合队列逐条验证两种情况)。验证
本机负载高,只跑了相关文件,全量交给 CI。
新增
test/claude-api-error-after-prompt.integration.test.ts:真实 worker、假 Claude Code、pty 后端。假 Claude 回答后画出❯,测试等 worker 记下这次回到提示符之后,才把剩下的收尾记录追加进 transcript;然后把 worker 实际发出的 IPC 回放进真实的 daemon worker 处理器(CoT 走真实的 message_cot 推送,只替换网络)。turn_terminalfailed /provider_server_error/ retryable;CoT 走 error 收尾(中断)、没有 RUN_FINISHED;daemon 发出「本轮执行失败」卡片final_output正文一次、CoT done、无失败卡final_output)变异(改完就跑上面的测试文件,再还原):
onTimeout在没超时时也调用startedOnly已有测试的调整:几条只喂 stop_reason、不喂
turn_duration就期望当场结算的用例,按现在 Claude 的真实形态补上turn_duration(它们测的是终态分类,不是挂起)。「从没写过 marker 就按 stop_reason 结算」那条,改成断言新语义:等满一次后才恢复按 stop_reason 结算。finalFirst竞态用例按 worker 的真实退出顺序,先做 pre-exit drain 再发cli_exit。零注入模式的本地回合用例同样补上turn_duration;「空 end_turn 后晚到正文沿用回复地址」改在已判定不写turn_duration的 CLI 上验证(默认挂起时晚到正文直接留在同一回合,另加一条断言并做了变异:挂起退回旧语义即红)。🤖 Generated with Claude Code