Skip to content

fix(worker): 修正启动卡看门狗时序与降级恢复 - #3607

Merged
deepcoldy merged 2 commits into
masterfrom
wt/worker-starting-health-followup
Nov 19, 2026
Merged

deepcoldy merged 2 commits into
masterfrom
wt/worker-starting-health-followup

Conversation

@deepcoldy

Copy link
Copy Markdown
Owner

改动说明

修复 #3593 后续确认的启动卡看门狗时序与显示状态问题:

  • queued turn 不再误降级:前序 turn 仍占用 capture identity 时只登记 pending entry,等当前 turn 的 capture_identity 激活后才开始 45 秒宽限; queued 期间异 turn、untagged 与 prompt_ready 都不能代为撤防。
  • POST 在途健康帧不再丢失:用单调健康帧序号代替 POST 完成时刻的毫秒比较,starting POST 在途收到 working 后不会再被定时器覆盖为 stalled。
  • display-only degraded 闭环:stalled 身份保存在模块内 WeakMap,不改 lastScreenStatus、usage 结算或 turn reaction。usage tick 停止覆写,普通 PATCH / 卡片按钮重绘保留 stalled,首个合格健康帧即使同值 working 也强制恢复。
  • 身份与发布边界补齐:dispatchAttempt 纳入 entry 身份,旧 attempt terminal 不能关闭新 retry 看门狗;普通 starting POST 与 worker-ready fresh/restored 发布共用 arm helper;prompt_ready 不再冒充屏幕健康帧。晚于 terminal 完成的发布不会重新 arm,completed terminal 会补做终态显示结算;restored 长 PATCH 后按 status revision 重 assert 最新 working。
  • 显式终态不被 stalled 覆盖:adopt 退出与 crash-loop 停止重启前清理健康状态,保留既有 idle 冻结语义。
  • capture timeout 分类:识别 Node/Bun 异步 execFile 的 code:null + killed:true + SIGKILL 形状,保持原 reject 行为。
  • 删除只写不读的 inFlight 字段。

说明:screen_update.status 的生产类型不含 starting,因此发布路径补测使用真实可达的 worker-ready fresh/restored 边界,不保留合成 starting screen_update 的无效探针。

影响面

  • 影响真实飞书 streaming card 的 starting/stalled 展示及恢复,覆盖不同 CLI 的 ready、queued、legacy untagged frame 与 screenshot 路径。
  • 不改执行状态、worker 生命周期、输入投递、tmux 恢复策略、命令路由、持久化 schema 或配置。
  • apiOnly、无 transport、卡片禁用、session close 仍由既有入口和 PATCH 身份围栏拦截。
  • capture 改动仅影响 TmuxPipeBackend 错误日志分类,同步空串和异步 reject 语义不变。

测试验证

  • 新增/更新看门狗回归:queued 与 activation、POST 在途 working、usage/routine render、同值恢复、prompt_ready、旧 attempt terminal、ready fresh/restored、screenshot、异 turn。
  • 相关完整套件:post-terminal-status-settle + tmux-pipe-backend + worker-ready-display-mode 404/404。
  • 卡片与 usage 相邻五文件 166/166。
  • 18/18 定向变异咬红:异 turn、screenshot、queued、POST 序号、usage gate、PATCH projection、同值恢复、prompt_ready、旧 attempt、ready fresh/restored、晚到 terminal、restored 完成顺序、adopt/crash-loop exit reset、missing high-water、terminal 后 idle reconcile、async timeout。
  • bun run build(含 typecheck)与 git diff --check 通过。

独立对抗复核发现与修复

  • P1:restored-ready 长 PATCH × terminal:真实 handler + 假时钟曾复现 starting → stalled。已在共享 arm 入口按 turn/attempt 拒绝 terminal 后重建;completed terminal 补做 idle 结算,completed/failed/cancelled 均有回归。
  • P1:CLI 退出 idle 被 stalled 覆盖:adopt 退出探针曾复现最后写入 stalled。现 adopt 与 crash-loop 停止重启均在冻结前 reset,两个退出用例及删除 reset 的变异均验证。
  • P2:restored working/starting 逆序:按实际 Promise 完成顺序曾复现 working → starting。现 restored 成功后按 status revision reconcile;测试记录 delivered 顺序,删除 reconcile 的变异咬红。
  • P1:无 high-water 的 attempted terminal 被误判为非当前:undefined !== 1 曾导致 terminal 后重新 arm。现无 high-water 时信任已被 handler 接受的 terminal,仅同 turn high-water 用于旧 attempt 否决;missing/matching/newer 三组用例覆盖。
  • P1:restored 旧 starting 覆盖已交付 idle:terminal → idle frame → 旧 starting 完成的逆序曾复现最终 starting。现 restored 不因 terminal 跳过 reconcile,实际 delivered 顺序回归最终 idle。
  • 原 usage 测试在 tick 前切到 off 的证据不足已修正:现保持 streaming 通过 12 秒,usage gate 删除变异咬红。
  • 全量 unit 已实际执行(同一分支早期实现):55,889 通过、133 失败;本次相关三个文件在全量中零失败。失败主要为长 worktree 路径造成的 tmux socket File name too long 与 /tmp/bm0 alias 断言(workspace-recycle 单跑仍复现同样 2 个路径失败)及并发集成错误,本地全量结果不记为通过,需由 CI 继续核验。
  • 独立对抗复核的 5 个交叠发现均已修复并复验关闭;另攻过异 turn high-water、terminal 后 direct-submit live work × 迟到 starting PATCH,未再发现 confirmed 问题。

卡片状态示意沿用 #3593,无新增视觉样式:

Starting 与 stalled 卡片状态对比

🤖 Generated with Claude Code

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

审查结论(head be1962f64e)

未发现 P1/P2 级执行、投递或账本缺陷,但确认 4 组 P3 显示时序缺口,以及 3 项基线已有的健康归因问题、5 处测试缺口。4 组 P3 都落在本 PR 声明要完成的 starting/stalled 闭环内,建议修复并补回归后再合并;3 项既有问题可单独跟进。

以下生产问题均用真实 worker IPC handler / 发布路径 + 假时钟复现,不是仅读代码推断。

本 PR 范围内的生产问题

P3-1:首次数值 attempt 激活会让看门狗失效(含 restored 同类路径)

  • src/core/starting-card-health.ts:169:queued POST 建 entry 时 dispatchAttempt 快照为 undefined;commitCaptureIdentity 先写入当前 turn 的数值 high-water,activateStartingCardHealth 再比较时判定身份不同并关闭 entry。
  • 探针:A(attempt=4) 持有 capture 时完成 queued B 的 POST,随后 capture_identity(B, attempt=0);45 秒后 PATCH 为 [],预期应有 stalled。
  • src/core/worker-pool.ts:21776 有同类缺口:restored ready 的 starting PATCH 在途收到首个 capture_identity(T, attempt=0),high-water 对象身份变化使 ownsRestoredWrite() 失败,PATCH 成功后仍跳过 reconcile/arm;45 秒后同样无 stalled。
  • 修法:激活时允许 entry 采纳该 turn 首次获知的 attempt,restored fence 也需区分“首次获知 attempt”与“已知 attempt 的后续切换”,不能给所有身份比较加 undefined 豁免。queued 处按此回填后,相关文件含探针 79/79。

P3-2:缺 attempt 的兼容 terminal 后会误报 stalled

  • src/core/worker-pool.ts:24673-24677:closeStartingCardHealth 被放进严格 high-water 等值闸;high-water 为 attempt=1、terminal 省略 attempt 时,闸不成立,兼容 terminal 不再关闭看门狗。
  • 探针:noteTurnDispatchAttempt(T, 1) → arm → turn_terminal(T, completed)(无 attempt),45 秒后实际 PATCH stalled。
  • 基线在 high-water 闸外按 turn 关闭健康 entry,因此这是本 PR 引入的回归;worker-pool.ts:24723-24735 也明确保留了缺 attempt terminal 的兼容处理。
  • 修法:把 close 移回 high-water 闸外,依靠 entryMatchesTerminal 的“缺 attempt 兼容、显式旧 attempt 拒绝”规则。临时验证此修法后,缺 attempt terminal 与旧 attempt retry 两个探针同时通过。

P3-3:晚到 starting 发布的终态 reconcile 不完整

  • src/core/worker-pool.ts:6780-6781,7155-7164。
  • completed + cached idle:慢 starting POST 期间 terminal 到达,lastScreenStatus 已是 idle;lastScreenStatus ?? starting 取 idle,busy 闸拒绝修正。POST 完成后及 2 秒 settle 窗口均无 idle PATCH,刚交付的 starting 成为最终显示。
  • failed/cancelled:restored starting PATCH 在途收到终态后,reconcilePublishedStartingCardTerminal 返回 true 并禁止 arm,却不安排非 starting 结算;两种探针在 45 秒时最终 delivered 均仍为 starting。
  • 修法:把刚交付的 starting 渲染本身视为忙碌显示证据;对其它已接受终态重断言合适的非 starting 展示,或在完成修正前不要把 publication 当作已 reconciled。

P3-4:截图健康恢复未完成重绘与 usage 恢复

  • src/core/worker-pool.ts:23243 丢弃 noteStartingCardHealthFrame 的恢复返回值。
  • stalled 后,working screenshot 在 displayMode 已切回 hidden 时到达:degraded 被清掉,但 src/core/worker-pool.ts:23297 直接 break,PATCH 为 [],可见卡片保持 stalled。
  • screenshot 模式下同类帧能 PATCH working,但 degradation 停止的 usageRefreshTimer 仍为 undefined,后续若只有截图帧,usage 不再刷新。
  • 修法:在 managed/substitute 等授权闸之后使用 recovery 标记强制恢复 PATCH,并仅对原本符合 usageRefreshShouldRun 的会话恢复 timer;保留“普通 screenshot 不随意 arm usage”的现有限制。

基线已有问题(不作为本 PR 新回归计数)

  1. 旧 attempt 帧可撤销新 retry 看门狗 — src/core/starting-card-health.ts:72 不接收 attempt。attempt=2 已 arm 时送入延迟 screen_update(T, attempt=1, working),45 秒后无 stalled。基线实现同样缺少 attempt 归因。
  2. untagged POST 在途健康帧仍丢失 — src/core/starting-card-health.ts:92 在 pending/degraded 尚未建立时把帧记到 turnId: ''。legacy untagged working 在 POST 在途到达后,45 秒仍误报 stalled;基线行为相同。
  3. 异 turn 帧会覆盖当前 POST 的健康序号证据 — src/core/starting-card-health.ts:28 每 session 只有一个 latestHealthy。A POST 在途依次收到 A/working、尚未 beginNewTurn 的 B/working 后,A 的健康证据被覆盖,45 秒误报 stalled;基线行为相同。

测试缺口复核

  • identityStillCurrent 的 fire 路径 attempt 比较(starting-card-health.ts:41):新增 retry 探针在当前代码下正确不误降级;删除比较后立即误发 stalled,原用例未钉住。
  • entryMatchesTerminal 的 attempt 匹配(starting-card-health.ts:104):单删内层条件,原文件 77/77 仍绿;再移除外层整个 high-water 条件才咬红。若采用 P3-2 的修法,内层匹配将直接承担保护,必须补测。
  • buildStreamingCardJson 默认 stalled fallback(worker-pool.ts:12845)、显式异 turn 隔离(starting-card-health.ts:59)、cachedStreamingCardStatus stalled 投影(worker-pool.ts:4740):当前行为均正确;分别删除守卫后,对应探针分别退回 working 或让异 turn 继承 stalled,现有用例未覆盖。

验证

  • 原始 PR 代码:bun scripts/fenced-run.ts -- bun x vitest run test/post-terminal-status-settle.test.ts test/tmux-pipe-backend.test.ts test/worker-ready-display-mode.test.ts → 404/404。
  • CI run 44497224138 已完成,14/14 jobs success。
  • 所有临时探针、变异与修法实验均已还原;工作区干净,未改动 PR 内容。

- 允许 queued/restored 发布采纳首次 attempt,保留 retry 身份围栏\n- 兼容缺 attempt terminal,并补齐晚到发布的终态重绘\n- 截图恢复健康时同步恢复卡片状态与 usage 刷新

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

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

G7 合并树复核背书(非相邻豁免)

确认该合并树可合。

独立复核结果:

  • live master:ce0a03a95d2527031dd0c90dd6f92f7af818ed97
  • PR head:43048e1d7e96f413ba6ac140b2bf882272a120c1
  • 以共同基线三点 diff 求交集,确为 src/core/worker-pool.ts、src/types.ts、test/tmux-pipe-backend.test.ts 三个文件。
  • 本地重新执行 git merge-tree --write-tree --messages ce0a03a95d 43048e1d7e,退出码 0;得到同一预测 tree:ef97f2314cc2ed1a63d86dc4916fb2cdc9a1644e。输出仅有上述三个文件的 Auto-merging,无 CONFLICT。
  • G4 run 44583485797 经 API 确认为 run_attempt=1、status=completed、conclusion=success,主 CI 14/14 jobs success;PR 当前可见 checks 均为 pass。
  • 本地仍停在 PR head,工作区干净;本次只重算合并树,未改 PR head,也未保留本地 merge 状态。

本背书绑定上述 master、PR head 与预测 tree。squash 落地后应核验落地提交的 tree 仍为 ef97f2314cc2ed1a63d86dc4916fb2cdc9a1644e;若合入前任一输入继续前移,或落地 tree 不一致,则此背书不再适用,需重新复核。

@github-actions

Copy link
Copy Markdown

🚀 Released in v3.59.23

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.

1 participant