Repository navigation
fix(recovery): 已有终态的轮次不再记成限额失败快照 - #3631
Merged
deepcoldy merged 1 commit intoNov 20, 2026
Merged
Conversation
nengqi
requested review from
47seek,
LucasIcarus,
deepcoldy,
xiaoxueSunn and
xiongz-c
as code owners
November 19, 2026 16:18
deepcoldy
reviewed
Nov 20, 2026
deepcoldy
left a comment
Owner
There was a problem hiding this comment.
复审结论:可合
复审 head:aae797069e3af26d517992e09405b1de8b08d585。
阻断:无。非阻断:无。
正确性核对
- 故障链成立。恢复轮次的 caller 在终态后仍由
session.failedTurnRecovery保留;残留的ds.usageLimit又会把后续 idle 帧投影为limited,因此旧逻辑会在src/core/failed-turn-recovery.ts:72把已完成轮次重新写成可领取的limitedSnapshot。 src/core/failed-turn-recovery.ts:80的守卫与限额快照职责一致:真实失败由recordFailedTurnTerminal记录,completed 终态清除未认领快照;终态后的残留 limited 帧不应再产生新的恢复授权。终态集合在src/core/worker-pool.ts:25246同步写入,早于src/core/worker-pool.ts:25409的异步终态回调,两者之间没有消息交错窗口。- 进行中的真实限额不受影响:此时集合尚无该 turnId,仍走原有记录路径。新增集成测试同时钉住了“idle 确实被投影成 limited”和“终态后不记快照”,并保留了终态前记录限额的对照用例(
test/failed-turn-limited-after-terminal.test.ts:116、:128)。 - 影响面为所有 CLI/后端共用的 daemon 状态回调;不改 IPC、持久化格式、卡片或命令。受控恢复与普通失败重试都使用同一
failedTurnRecoverycaller 结构,均覆盖在此次防护内。
本地验证
评审树 rebase 到当前 origin/master 后,与作者 head 的三文件补丁逐字等价,未改写 PR 分支。
bunx vitest run test/failed-turn-recovery.test.ts test/failed-turn-recovery-sqlite.test.ts test/failed-turn-limited-after-terminal.test.ts test/rate-limit-notify.test.ts test/worker-pool-statusline-quota.test.ts:5 个文件、100 条测试全部通过。bun run build:通过。- 变异复验:临时删除新守卫后,新增单元与集成用例各失败 1 条;恢复后相关 68 条测试全部通过,工作树干净。
- GitHub CI:该 head 的 15 项 check 现已全部成功,包括重跑后的
bun-binary-darwin。
Owner
|
感谢贡献,已合入 master(squash 落地 这个修复的定位很准确:恢复轮次的 caller 在终态后仍保留在 合码前验证:
无后续待办,欢迎继续贡献。 |
|
🚀 Released in v3.59.23 |
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.
问题
一个会话遇到 429 后,
ds.usageLimit会一直留着,直到出现一帧 working/analyzing 画面才清(resolveUsageAwareScreenStatus)。如果这时用 failure-recovery 接口发起一次恢复轮次,恢复轮次正常答完、turn_terminal为 completed,之后来的第一帧 idle 画面仍会被投影成limited,并且带着恢复轮次的 turnId。onScreenStatus收到后调用recordLimitedFailedTurn,把这个已经答完的恢复轮次写成lastFailedTurn {errorCode: 'rate_limited', limitedSnapshot: true}。普通轮次不会这样:轮次结束时
scheduledTurnCallers和activeInteractiveTurn都已释放,failedTurnCaller拿不到 caller 就直接返回。恢复轮次的 caller 存在session.failedTurnRecovery里,终态之后还在,所以只有恢复轮次会中招。线上看到的顺序(同一会话):
turn_terminalcompleted,回复卡片status=done,期间没有 StopFailurelastFailedTurn被写成该恢复轮次,rate_limited,limitedSnapshot: true这条记录又能被 failure-recovery 接口领取。线上确实发生过一次:已经答完的轮次被再次重放,同一个任务跑了两遍。
completed 终态里的
clearLimitedFailedTurn解决不了这个问题:它先执行,快照是终态之后才写进来的。改动
recordLimitedFailedTurn开头加一条判断:ds.terminalIdentityTurnIds里已经有这个 turnId,就不记。recordFailedTurnTerminal负责:completed 时清快照,failed 时记真实失败。终态之后再来的limited帧,只是残留的ds.usageLimit。terminalIdentityTurnIds而不用lastTurnTerminalTurnId:后者只在currentTurnId为空或等于本轮时才写(worker-poolturn_terminal分支)。恢复轮次不经过beginNewTurn,currentTurnId还是原来的旧轮次,所以后者永远写不进恢复轮次的 id。前者在turn_terminal分支里无条件写入、有上限,identityDecisionTurnIsLive也拿它作为「轮次已结束」的依据。影响面:只改
src/core/failed-turn-recovery.ts一个函数,所有 CLI 和后端走同一路径;不改限额卡片和ds.usageLimit的清除时机。验证
test/failed-turn-limited-after-terminal.test.ts,驱动真实的 worker-pool IPC handler(fake worker):currentTurnId设为旧轮次、ds.usageLimit留着,先发turn_terminal(completed),再发 idlescreen_update,onScreenStatus接法与 daemon 相同。断言 idle 帧确实被投影成limited并带着恢复轮次的 turnId,且lastFailedTurn为空;另一条断言轮次进行中遇到限额照常记录。test/failed-turn-recovery.test.ts增加一条单元用例。lastTurnTerminalTurnId === turnId && lastTurnTerminalStatus === 'completed',集成用例变红。这证明那个字段对恢复轮次不起作用。npx vitest run test/failed-turn-recovery.test.ts test/failed-turn-recovery-sqlite.test.ts test/failed-turn-limited-after-terminal.test.ts:72 passednpx tsc --noEmit -p .:通过bun run build:通过bun run test:2858 个文件通过,8 个文件 44 条失败;同样这 8 个文件在未改动的 master(dcbf8a3)上也是 44 条失败(sandbox relay、sqlite poisoned recovery、bun 导入等,都依赖测试机环境),和本改动无关对抗复核
攻过的场景:
beginNewTurn,currentTurnId停在旧轮次。第一版用lastTurnTerminalTurnId做判断,在这里失效,已改用无条件写入的集合,并用上面的集成用例钉住。limited帧后到,以及limited帧先到、终态后到:后一种原本就由 completed 终态清掉快照,行为不变。recordFailedTurnTerminal已经记下真实失败。这里不另做处理。terminalIdentityTurnIds上限 64、只在内存:超出上限或 daemon 重启后退回原行为,方向是安全的。