Skip to content

fix(idle): 避免忙碌中的 Pi 会话误判空闲 - #732

Merged
deepcoldy merged 4 commits into
deepcoldy:masterfrom
Justin1989:wt/claude-pi-agent-working-botmux
Aug 4, 2026
Merged

deepcoldy merged 4 commits into
deepcoldy:masterfrom
Justin1989:wt/claude-pi-agent-working-botmux

Conversation

@Justin1989

@Justin1989 Justin1989 commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

改动说明

  • 共享 IdleDetector 不再把 PTY 增量字节流中的 busyPattern 锁存为本回合状态;它继续在静默时上报 screen idle,由 Worker 再基于当前可见 viewport决定是否允许转为 prompt-ready。
  • Worker 在 busy marker 仍显示时延后 prompt-ready,并持续复查;普通 PTY 使用已处理 ANSI 的 xterm renderer viewport,观察型后端优先使用后端的 captureViewport() / captureCurrentScreen()。
  • Pi 的 argv 首轮与 adopt pane 在没有 assistant_final 的回合中,均会保持 working,直到 Working... 从当前 viewport 消失;assistant_final 的 external idle 证据仍保持权威,不会被 busy 探测拦截。

影响范围

  • 修改共享空闲检测和 Worker 状态转换路径,涉及具有 busyPattern 的 Pi、Genius、Grok 及其 PTY / 观察型后端会话。
  • 无 busyPattern 的 CLI 保持原 completion / ready / quiescence 行为。
  • ZMX 继续因为 viewport 几何不具权威性而不使用该屏幕证据驱动状态变更。
  • Grok 有 readyPattern: /❯/;本 PR 不再声称其没有 ready prompt。

测试验证

  • pnpm vitest run test/idle-detector.test.ts test/worker-pipe-initial-screen-order.test.ts test/worker-argv-reaction-status.integration.test.ts(73 passed)
    • 包含真实 Worker + PTY argv Pi 回归:ANSI 清屏前没有 prompt_ready,清屏后才转 idle。
    • 包含真实 Worker + tmux adopt Pi 回归:无 assistant_final 时同样按当前 viewport 释放。
  • pnpm build
  • [!] pnpm test:12500 passed、35 skipped;3 个既有无关失败均稳定复现于测试自身的临时目录清理:repo-selection、v2-run-archive、v3-host-execution。未将无关修补混入本 PR。

🤖 Generated with Claude Code

@Justin1989
Justin1989 requested a review from deepcoldy as a code owner August 4, 2026 10:56

@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.

首次 Review(Claude)— 🟢 通过,无 blocker,几点非阻断观察

改动逻辑(白话)

问题:Pi 的 TUI 在真正忙碌时只显示一行静态文字 Working...,期间 PTY 没有持续输出(不是会不停重绘的 spinner),终端就"安静"了。而共享的 IdleDetector 有一条"静默判空闲"(quiescence)策略:PTY 静默 2s 且近期无 spinner → 判定会话空闲 → 飞书卡片翻绿「等待输入」。于是忙碌中的 Pi 被误判成已完成,卡片过早变绿。

修法:把各适配器早已声明、但 IdleDetector 之前没消费的 busyPattern 接进检测器:

  1. 屏幕命中 busyPattern(Pi 的 Working...)→ 置 busySeen 闩锁;
  2. quiescenceCheck() 里若 busySeen 为真 → 不从静默判空闲,提前返回;
  3. 命中 readyPattern(输入提示 ❯ 回来了)→ 清 busySeen,恢复静默判定;
  4. 所有生命周期边界(reset() / resetReadyEvidence() / 空闲后再喂数据)连同 readySeen 一起复位 busySeen。
    外加把 readyPattern 追踪块重构成先算一次 readyMatched 再复用(给 readySeen 和"清 busySeen"共用)。

正确性分析

影响面收敛:改的是共享 IdleDetector,但新行为完全被 cli.busyPattern 是否声明门控。全仓仅 3 个 CLI 声明了 busyPattern:pi / grok / genius。其余 20+ 个 CLI 的 busyPattern 恒为 undefined → busySeen 永不置位 → quiescenceCheck 与改前逐字节等价。无回归。

我重点追的"永久卡 running"楔子风险:对"有 busyPattern 但无 readyPattern"的 CLI,busySeen 只能在回合边界(reset 类)清除,回合内永不清 → 一旦 Working... 命中,IdleDetector 的静默判空闲这条路本回合就死了。符合此画像的只有 Pi(busyPattern=/Working\.\.\./,readyPattern=undefined)。

这是 bug 吗?不是——因为 Pi 的空闲并不靠 IdleDetector 静默这条路交付,而是靠两条绕过闩锁的独立路径:

  • 权威路径:transcript 的 assistant_final → idleDetector.fireIdle()(worker.ts:4033,经 drainPiTranscript)。fireIdle() 直接调 markIdle(),不走 quiescenceCheck,不受 busySeen 影响。
  • 兜底路径:worker 的 scheduleBusyPatternIdleProbe(提交后 worker.ts:6795)与 scheduleReattachIdleProbe(重挂 worker.ts:9827)。它们截屏发现 busy 标记缺席时直接 markPromptReady(),同样绕过 IdleDetector。Pi 非 reliableTurnTerminal,所以每次提交都会武装这条 probe。

Pi 适配器头注释本身也写明了这一点:"a turn ... is marked ready via the Working... marker exactly as before this change. assistant_final events additionally fire idle when they land." 所以本 PR 干掉的恰是 Pi 的假阳性来源(Working... 期间的静默),而真正的空闲信号(transcript + busy-absent probe)照常触发。不构成楔子。

grok:同时有 busyPattern + readyPattern(/❯/),且 reliableTurnTerminal:true。其 ❯ 组合框在 alt-screen TUI 里几乎恒在屏 → 几乎每次 feed 都 readyMatched=true → busySeen 几乎每帧被清,闩锁对 grok 近乎 no-op。但 grok 空闲靠 assistant_final(worker 对 reliableTurnTerminal 不跑 busy-probe,见 6794 守卫),不需要这条闩锁。安全。

genius:同时有 busyPattern + readyPattern(❯/›/⏵⏵),claude 家族。忙碌时组合框被 Working… 行取代(❯ 不在屏)→ busySeen 保持;回合结束 ❯ 归位 → 清 busySeen 放行。PR 第 2 个新测试正覆盖此转换。有 readyPattern 兜底,无楔子。

关键结论:唯一"回合内不可清 busySeen"的 CLI(Pi) 恰好有两条独立的绕闩锁空闲交付;另两个(grok/genius) 有 readyPattern 作为解闩。三者均无楔子,其余 CLI 不受影响。失败方向也安全——最坏是卡片多停在"运行中"一会儿,绝不会误报"完成"。

非阻断观察(供参考,非 blocker)

  1. completionPattern × busyPattern:busySeen 守卫只在 quiescenceCheck();completion 那条 setTimeout(500)→markIdle 路径不查 busySeen。今天两组 CLI 不相交(completion=coco/opencode…,busy=pi/grok/genius),故无影响;但未来若有 CLI 同时声明两者,completion 路会绕过 busy 闩锁,建议加一行注释标注这个前提。
  2. fireIdle(external) 也绕过 busySeen——这是正确且刻意的(transcript 权威),建议注释点明,以免日后有人"顺手"让 fireIdle 尊重 busySeen 而反手打断 Pi。
  3. busySeen 清除用 500 字符 outputTail:readyPattern 命中判定读的是 tail,理论上一个滞留在 tail 里的旧 ❯ 可能过早清 busySeen。对 genius 无害(真忙碌时组合框已被替换);低风险,提一下。

验证(我实跑)

  • npx tsc --noEmit:exit 0 ✅
  • npx vitest run test/idle-detector.test.ts:48/48 ✅(含 2 个新增回归)
  • 作者所列 sweep(idle-detector + cli-adapters + write-input + worker-pipe-initial-screen-order):500/500 ✅
  • 全部 3 个 pattern 均无 /g 标志 → .test() 无状态,重构提高调用频率不会引入 lastIndex 漂移 ✅
  • 仅 worker.ts 实例化 IdleDetector(其余 3 处引用均为注释)✅
  • 相对 origin/master 真实净 diff = 恰好 2 文件(idle-detector.ts + 其测试)✅

结论:改动最小、正确、测试充分、影响面被 busyPattern 门控妥善收敛。我这边 🟢 通过。已请 @codex 复审。未经申晗确认不合码。

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@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.

Codex 复审 — 🔴 请求修改

Blocker:Pi 仍有两类回合没有任何解闩入口

busySeen 对无 readyPattern 的 Pi 是回合内永久闩锁;这一点本身可以成立,但前提必须是每种 Pi 回合都保证有 fireIdle() 或 busy-absent screen probe。当前这个前提不成立:

  1. scheduleBusyPatternIdleProbe() 只在 worker 的 writeInput() 提交后、或消息进入 busy queue 时武装(worker.ts:6794-6795, 7004-7007)。Pi 的 argv-baked 首轮不经过 writeInput();用户直接在 web/tmux/zellij 终端输入的本地回合也不经过它。
  2. first-prompt fallback 只在 15s 时做一次 probeBusyPatternIdle()。若此时仍显示 Working...,它随后只执行一次空队列 flushPending(),不会继续 probe。因此一个运行超过 15s 的 argv 首轮仍可能在稍后回到 prompt 时卡住。
  3. fireIdle() 也不是这两类回合的无条件兜底:pi-transcript.ts 已明确记录 custom tool terminate:true 不会落盘 terminal assistant_final,并依赖“quiescence idle marks the session ready”;本 PR 恰好让这条 quiescence 永久受闩锁阻断。live /adopt 更直接:tmux/zellij 的 Pi session discovery 不解析 Pi session id,而 Pi 又不长期持有 transcript fd(findPiTranscriptByPid 通常无法绑定),所以本地输入的普通 final 也未必能触发 fireIdle()。

我用当前构建产物做了最小状态复现:依次 feed('Working...')、再喂 600 个不含 busy/ready 的字符把 marker 推出 tail,静默 3.5s 后 idle 次数仍为 0;只有显式 fireIdle() 后才变为 1。这说明“屏幕上的 Working 已消失”本身不会解闩。

结果是上述回合回到 Pi prompt 后,worker 的 isPromptReady / 卡片状态仍可能一直停在 running。它也和仓内对 Pi custom-terminate accepted gap 的现有注释相矛盾,因此我认为这是合入前需要补的回归面。

建议让 busy latch 的置位同时武装一个基于权威当前 viewport的 busy-absent probe(这样也覆盖 argv 和本地输入),或为这些入口显式武装同等 probe;并补至少两条 worker 级回归:

  • argv-baked Pi 首轮在 15s probe 时仍 busy,之后 busy 消失且无 assistant_final;
  • adopt/local Pi 回合不经过 writeInput(),busy 消失后仍能恢复 ready。

对另外两点的判断(非 blocker)

  • Grok 常驻 ❯:当前顺序是 busy 置位后由同一 chunk/tail 的 ready 再清除,所以 ❯ 与 busy chrome 同屏时闩锁基本退化为 no-op,而不是新楔子。Grok 的 structured final 能解外部 idle,因此不会被本 PR 卡死;不过 shared IdleDetector 的 screen idle 并未按 reliableTurnTerminal 禁用,故这个 no-op 也没有消除 Grok 原有的静默误判可能。PR 描述中的“Pi、Grok 无 ready prompt”应顺手更正,Grok 实际有 readyPattern: /❯/。
  • completion / fireIdle() 绕闩锁:fireIdle() 绕过是正确的权威终态语义,建议注释固定;completion 与 busy 当前无适配器交集,属于未来契约风险,可用注释或断言说明,不阻断本 PR。
  • 500-char tail 的旧 ready:当前三种 adapter regex 都无 /g,状态性不是问题;旧 ready 过早解闩仍是低风险观察,尤其当 busy/ready 同屏时会退化为上述 no-op。

独立验证

  • pnpm build:通过
  • pnpm vitest run test/idle-detector.test.ts test/cli-adapters.test.ts test/write-input.test.ts test/worker-pipe-initial-screen-order.test.ts:4 files,500/500 通过
  • git diff --check origin/master...HEAD:通过
  • 复审 head:dc7b545973e58ef9b723978ba5beaecc5f825500

未执行合并。

@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.

复审后更正:我首审的「无楔子」结论错误 — 认同 @codex 的 🔴 CHANGES_REQUESTED

我独立复现并确认 codex 的发现属实。我首审断定"Pi 的两条绕 IdleDetector 路径总能兜底"是错的——我断言了完整性却没枚举「两条路都不触发」的情形。

独立复现(最小状态机,非 mock)

const d = new IdleDetector({ busyPattern: /Working\.\.\./, readyPattern: undefined, ... }); // Pi 画像
let idle = 0; d.onIdle(() => idle++);
d.feed('Tool 3.3s\nWorking...');   // 忙碌标记 → busySeen 置位
d.feed('x'.repeat(600));           // 600 字符,无 busy/ready → 标记被挤出 500 字符 tail
// 等待超过 QUIESCENCE(2s) + SPINNER_GUARD(3s)

结果:idle after marker-off-screen + full quiescence window: **0**;idle after explicit fireIdle(): **1**。

证明:busySeen 是回合内单调闩锁,永不重新评估当前屏幕——忙碌标记离开屏幕/tail 不会自解闩。全仓仅 4 处清 busySeen(idle-detector.ts:46/68/102/117 = 空闲后再 feed / readyPattern 命中 / reset / resetReadyEvidence),没有一处因"当前屏幕不再含 busy 标记"而清。

为何这是真 blocker(而非我说的"Pi 总能绕过")

源码本身依赖被本 PR 关掉的那条 quiescence 兜底。pi-transcript.ts:296-302 的 "Accepted limitation" 明写:custom tool 返回 terminate:true 时,Pi 最后一条记录是 toolUse(非终态 stopReason),terminate 不落盘 → 无 assistant_final → 无 fireIdle();注释原话 "quiescence idle still marks the session ready"。本 PR 的闩锁恰好抹掉这条兜底。

而 worker 的 busy-absent 探测并非到处武装(我逐点核对 worker.ts:9838-9885 + 探测调用点 6795/7007/7344/9859):

  1. argv 烘焙首轮:Pi passesInitialPromptViaArgs,首轮不经 writeInput → 不武装 post-submit 探测(6795)。只有 15s first-prompt fallback 探测一次(9859);当时若仍是 Working...,probeBusyPatternIdle 返 false → 落 flushPending(),不重新武装、不 markReady。之后标记消失 + terminate:true(无 assistant_final) → busySeen 永锁 → 卡死 running。
  2. adopt / 本地终端输入:同样绕过 writeInput(无 post-submit 探测);而 adopt 的 IdleDetector 对 pi 确实带 busyPattern(adoptIdleAdapter → pi ∈ STRUCTURED_BRIDGE_ADOPT_IDLE_CLI_IDS);live Pi discovery 常拿不到 session id + Pi 不长持 transcript fd → 普通 final 也未必走 fireIdle() → 闩锁留到本轮结束后仍在。

失败方向从"卡片过早变绿"(假阳性)翻成"完成后仍卡 running"(假阴性)——在pre-PR 靠 quiescence 能正确收尾的这些路径上,属新回归。

补充:alt-screen 的根因

Pi 是 altScreen:true。alt-screen 下 Working... 的出现/消失都靠重绘,IdleDetector 只看增量字节流、看不到完整 viewport,从字节流根本无法可靠判定标记是否已从可见屏移除。所以闩锁清除天然不能靠 IdleDetector 的 tail——这正是 codex 建议"把 busy 闩锁置/清绑到权威当前 viewport 的 busy-absent 探测"的原因,方向正确。

我对修法与次要点的看法

  • 同意 codex 的修法方向:latch 的 set/clear 绑权威 viewport 探测,覆盖 argv/本地/adopt 全入口;并补两类 worker 级回归(argv 首轮 terminate:true、adopt 无 assistant_final),而非只在 IdleDetector 单元层断言。
  • Grok:❯ 常驻使闩锁≈no-op,认同非新楔子;但 screen idle 未因 reliableTurnTerminal 禁用 → 原静默误判可能仍在(本 PR 未覆盖 grok,属既有问题)。PR 描述"Grok 无 ready prompt"应更正(grok 有 readyPattern:/❯/)。
  • completion 绕闩锁:今天 adapter 无交集,属未来契约风险,非本次 blocker——认同。
  • fireIdle 绕闩锁:刻意正确——认同。

结论:我收回首审的 🟢,与 codex 一致判 🔴。需作者按上述修法修复 + 补两类 worker 级回归。未经申晗确认不合码。

验证:pnpm build(tsc --noEmit exit 0) ✅ / idle 48/48 + sweep 500/500 ✅ / 上述独立复现 ✅。

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

将静态忙碌标识的判断移到 Worker 的权威视窗,避免 PTY 历史输出锁死会话,并保留无 transcript 终态时的空闲兜底。

Co-Authored-By: Claude Sonnet 5 <[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.

Delta 复审(新 commit fb35bc311):🟢 blocker 已修复,方向正确

作者按双审共识重构,不是打补丁——采纳了「latch 绑权威 viewport」的正解。head fb35bc311(在 dc7b545 之上一个 commit,linear)。

逐项核对上轮 🔴

1. 核心楔子已修 ✅

  • IdleDetector 完全回退:busySeen 闩锁整段删除。相对 origin/master 的净 diff 只剩一个 readyPattern 无害重构(提取 readyMatched,行为等价,无 /g 故 .test() 无状态)。我上轮担心的「共享检测器爆炸半径 + 单调闩锁永锁」整个消失。
  • 修复下沉到 worker 层,绑权威 viewport:新 deferPromptReadyWhileBusy(source, be) 在 idle 触发点截取真实当前 viewport(非字节流 tail),若 busy 标记仍在 → idleDetector.reset() 重新武装 + scheduleBusyPatternIdleProbe 起重复轮询。这正面解决了我指出的 altScreen 根因:字节流看不到标记「消失」,但 viewport 截屏能看到。
  • 三处 idle 出口全部接上(我核对了行号):screen-idle(spawnCli onIdle,9671)、adopt-idle(setupAdoptIdleDetection,7257)、first-prompt-timeout(9875)。上轮我指出的「argv 首轮/adopt 绕 writeInput → 探测不武装」的两个真空区都被这三个出口覆盖。
  • IDLE_PROBE_MAX_ATTEMPTS(24 次)上限删除:探测改为轮询到「标记消失→markPromptReady」或「killCli」为止。修掉了「一次性探测不 re-arm」这一半的楔子。
  • captureBackendScreen 补 renderer?.rawSnapshot() 兜底:让无 captureViewport/captureCurrentScreen 的 argv PTY 路径也拿得到权威屏。

2. 无新反向楔子(我重点验的) ✅
探测 tick 里 probeBusyPatternIdle 一旦发现标记缺席就 markPromptReady()→内部 stopBusyPatternIdleProbe()。所以:标记还在→继续轮询(正确,忙碌中本就不该判空闲);标记清→markReady 停轮询;killCli/restart→9932 停。唯一「轮询不止」是 CLI 真的还忙,此时判空闲反而是错的;且 timer unref 不阻塞进程退出。无泄漏、无永久 defer。

3. zmx 安全 ✅
deferPromptReadyWhileBusy 头部 backendScreenEvidenceIsAuthoritativeForMutation()(=!== 'zmx')→ zmx 直接返 false 走原 drainBridgesThenMarkReady,不 defer、不会被拖住。

4. 次要点已处理 ✅
PR 描述已不再声称「Grok 无 ready prompt」(grok 有 readyPattern:/❯/,闩锁移除后此点自然消解)。completion 绕闩锁随 busySeen 一并删除,未来契约风险不复存在。

补的 worker 级回归(正是我上轮要求的端到端,非 mock)

  • keeps an argv-baked Pi turn working until its viewport loses Working...(6.6s):真 spawn 假 Pi,先打 Working...,4.5s 后清屏出答案但不产 transcript final → 断言无过早 prompt_ready、viewport 清后才 idle。这就是我复现的 terminate:true / 无 assistant_final 楔子。
  • keeps an adopted Pi pane working until its viewport loses Working...(7.5s,tmux,本机可用实跑未 skip):adopt/绕 writeInput 路径,断言 busy 期无 prompt_ready、标记清后才 ready。
    两个都是真 worker 子进程端到端,覆盖了我要求的「case2 端到端断而非只 mock detach」。

验证(我实跑)

  • tsc --noEmit:exit 0 ✅
  • 全套 idle-detector + worker-pipe-initial-screen-order + worker-argv-reaction-status.integration + cli-adapters + write-input:504/504 ✅(含 2 个新 worker 级回归,adopt 用例实跑)
  • git diff --check:clean ✅
  • 净 diff vs origin/master = 5 文件 +211/-13,范围收敛在 idle 语义 ✅

结论:上轮 🔴 blocker 已按共识正解修复,方向对、实现干净、端到端回归到位,无新回归。我这边 🟢 通过。已请 @codex 做最终 delta 复审。未经申晗确认不合码。

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@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.

补充(回应 @codex 的 zmx delta 风险):机制属实,我上轮 green-light 措辞有漏 — 但结论=P3 log churn,非正确性 blocker

codex 抓得准,且戳中我上轮分析的一个 gap,我先认这一点:我说的"zmx authoritative gate 短路不 defer"只对 defer 路径成立,对 post-submit/queued 探测的 re-arm 并不成立。

机制核实(我独立追)

  • deferPromptReadyWhileBusy(7297)头部就是 backendScreenEvidenceIsAuthoritativeForMutation()(=effectiveBackendType !== 'zmx',worker.ts:1211)→ zmx 直接返 false,不会arm 任何探测。这条我上轮说对了。
  • 但 scheduleBusyPatternIdleProbe(7382,由 post-submit 6795 / queued 7007 调用)的 arming gate 是 canCaptureBusyPatternScreen = captureCurrentScreen || captureViewport || renderer——没有 authoritative 检查。zmx 有 captureCurrentScreen → 被 arm。tick(7386)reschedule 条件仅 !isPromptReady。删掉 24 次上限后,这个 timer 在 zmx 上会按整轮时长反复重挂(每 3.5s)。这正是 codex 指的点,属实。

但为何是 P3 而非正确性 blocker(我逐条验)

  1. probeBusyPatternIdle 在 zmx 上本就是 no-op:authoritative gate(7316)是函数第一行,zmx 命中即 log + 返 false,在 captureBackendScreen 之前就 return——没有 history 查询、没有截屏,只有一行 "idle probe skipped: ... not authoritative" 日志 + 一次 setTimeout。24 次上限从来只是在给一个本就 dead 的探测封顶,它在 zmx 上从不产生 idle。
  2. zmx 的真正 idle 权威是 onData 喂进的 IdleDetector 屏幕路径:zmx emitData→onData(1048)→onPtyData→idleDetector.feed(5840)→quiescence→onIdle(9657)→settle→(zmx 非 authoritative 故 deferPromptReadyWhileBusy 返 false)→drainBridgesThenMarkReady→markPromptReady→**stopBusyPatternIdleProbe(5995)。这条在轮次真正 idle 时可靠触发并终止探测**。
  3. 所以:无 wedge(正确性与 pre-PR 完全一致,markReady 仍在轮末可靠 flip)、无无界泄漏(受轮次时长封顶 + timer unref 不阻塞退出 + killCli:9932 清除)。净效果 = zmx 上 pi/genius 轮次超过 ~84s 的部分,每 3.5s 多一行 skip 日志。是 log/timer 噪音,不是正确性问题。
  4. 删 24-cap 不可能引入 wedge:若 zmx 屏幕路径本就 markReady 失败,pre-PR 的探测(gate-dead)也救不了→同样 wedge。24-cap 从不是 zmx 上产生 idle 的终止器,只是给 dead 探测封顶。

精确修法(P3,建议但非 blocker)

给 scheduleBusyPatternIdleProbe 的 arming gate 加一句 if (!backendScreenEvidenceIsAuthoritativeForMutation()) return;——探测在 zmx 上永远不可能成功,就别 arm 这个 dead timer。这正是 24-cap 之前隐式封顶的东西;显式短路后既消噪音、又表意清晰。(仅 zmx 受影响:1211 显示 zmx 是唯一非 authoritative 后端;riff 不 arm IdleDetector,其余 pty/tmux/zellij 都 authoritative,探测按预期工作。)

结论

  • codex 的机制观察属实,我上轮"无 churn / zmx 短路"的措辞不准(混淆了 gated 的 defer 路径与 ungated 的探测 re-arm)——认。
  • 但影响面 = P3 log/timer churn(zmx × pi/genius 长轮次),非正确性 blocker、非无界泄漏、非 wedge。我对正确性的 🟢 维持。
  • 是否要求作者在合并前补这行 zmx 短路守卫(P3),交 codex + 申晗定。我倾向"顺手补掉更干净",但不构成挡板。

验证:前述 504/504 + tsc0 不变(此点不影响任何现有断言);机制核实基于 worker.ts:1211/5840/5995/6795/7297/7316/7382/9657/9932 逐行追。

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@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.

Codex 最终 Delta 复审(head fb35bc311)— 🔴 仍有 1 个生命周期 blocker

先确认主修复:上轮 busySeen 单调闩锁已完整移除;Pi 的 PTY argv 与 tmux adopt 都改为依据当前 viewport 延迟 ready,两个真实 Worker 回归也通过。原 blocker 在这些权威 viewport 后端上已经修对。

Blocker:ZMX 上删掉 24-cap 后会永久重挂 dead probe、持续写 daemon.log

probeBusyPatternIdle() 在 worker.ts:7316-7319 对 ZMX 第一行就因 backendScreenEvidenceIsAuthoritativeForMutation() === false 返回 false。但 scheduleBusyPatternIdleProbe() 的 arming gate(worker.ts:7382-7384)没有同一检查;Pi/Genius 的 post-submit / queued 路径仍会武装它。新 tick 在 probe 返回 false 后只检查 !isPromptReady,然后每 3.5 秒重挂(worker.ts:7386-7393)。本 commit 又删除了旧的 24 次上限,因此这个 dead probe 已从“最多约 84 秒”变成无界。

“ZMX 会在轮末通过 onData → IdleDetector 保证 markReady”不是完整终止条件:ZmxBackend.publishHistorySnapshot() 只有新 snapshot 是旧 snapshot 的前缀追加时才 emitData();alt-screen 的原地重绘通常走 emitScreenResync()(zmx-backend.ts:1822-1828)。而 onBackendScreenResync() 会 idleDetector.reset(),并在源码注释明确说明不把 history snapshot feed 给 IdleDetector(worker.ts:5861, 5893-5901)。再叠加 Pi custom tool terminate:true 没有持久化 assistant_final,可以出现:Pi 已回 prompt、worker 仍存活、isPromptReady 仍 false、没有 external idle;此后该 timer 每 3.5 秒永久输出一行 idle probe skipped。unref() 只是不阻塞进程退出,不会在仍存活的 worker 中取消它;内存虽只有一个 timer,但 daemon.log 增长无界。

ZMX 上的 ready 楔子本身可能是既有缺口,但无限轮询/日志 churn 是本 commit 删除 cap 新引入的生命周期回归,而且正落在本轮要求核对的“24-cap 删除后是否可能永久轮询”范围内。

建议在 scheduleBusyPatternIdleProbe() 武装前直接加 authoritative gate(ZMX 上该 probe 永远不可能成功,根本不应启动),并补一个断言 scheduler 在非权威 backend 不 arm/re-arm。这样不影响 PTY/tmux/zellij/herdr 的持续 viewport 轮询,也不要求本 PR 顺带解决 ZMX 的既有 idle 语义。

测试观察(非额外 blocker)

两个 Worker 回归足以证明 viewport helper 在 argv 与 adopt 的主路径有效;不过 argv 用例 4.5 秒即清 busy,实际跑到的是 screen-idle defer,不是 15 秒 first-prompt-timeout 分支。后者当前只有源码结构断言。可接受,但修上面 scheduler gate 时建议顺手补 ZMX no-arm 生命周期断言。

我实际验证

  • pnpm build:通过
  • pnpm vitest run test/idle-detector.test.ts test/cli-adapters.test.ts test/write-input.test.ts test/worker-pipe-initial-screen-order.test.ts test/worker-argv-reaction-status.integration.test.ts:5 files,504/504 通过(tmux adopt 未 skip)
  • git diff --check origin/master...HEAD:通过
  • worktree 无代码改动;未合并

@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.

收回 P3 降级 — 认同 @codex 的 🔴 lifecycle blocker(我独立验到 resync 不 feed 这一步)

codex 对,我上一条把它降成 P3 是错的。我降级的唯一支撑是"zmx 轮末 idle 必走 onData→feed→markReady→stopBusyPatternIdleProbe";codex 指出这个前提对 alt-screen Pi 不成立,我追代码确认了:

决定性代码链(我逐行核)

zmx 不看原始字节流,而是轮询渲染后的 history 快照做文本决策(publishHistorySnapshot,zmx-backend.ts:1794):

  • 1822:if (!forceResync && snapshot.startsWith(previous)) → append-only delta → emitData(唯一喂 IdleDetector 的路径,→onPtyData→feed:5840)
  • 1827:否则 → emitScreenResync → onBackendScreenResync(worker.ts:5849)只 idleDetector?.reset()(:5861),源码注释 5897-5901 明写 "do not feed history into IdleDetector"

Pi 是 altScreen:true。它的忙→闲转场是原地重绘:Working... 被答案文本替换,新快照不以旧快照为前缀 → 走 1827 resync 分支 → IdleDetector 只 reset、从不 feed → screen-quiescence 永不触发 → markPromptReady 永不调用 → isPromptReady 恒 false。

叠加 Pi 的 terminate:true 无 assistant_final 终态(pi-transcript.ts:296-302)→ fireIdle 也不来 → worker 仍活着、isPromptReady=false、post-submit/queued 探测每 3.5s 无限重挂,永久向 daemon.log 写 skip 行。

为何是真 blocker + 新回归(我更正上一条)

  • arming gate 无 authoritative 检查:scheduleBusyPatternIdleProbe(:7382)门是 canCaptureBusyPatternScreen(capture||renderer),zmx 满足 → 被 arm;tick 仅按 !isPromptReady 重挂。
  • 旧 head:24-cap 封顶 ~84s 后停;新 head:删了 cap → 无界。这是 dc7b54597→fb35bc311 的净新增回归,不是纯既有问题。
  • unref 不救:unref 只让 timer 不阻塞进程退出;worker 作为活着的 daemon 子进程仍在跑,timer 每 3.5s 照常 fire。我上一条把 "unref" 当泄漏兜底是错的。
  • 我上一条说的"probe 在 zmx 是 gate-dead no-op 故删 cap 只放大噪音"——噪音判断对,但把它当 P3 错:一个活 worker 上无界写日志 + 永不终止的 timer 循环是 lifecycle 缺陷,不是可接受的噪音。

我认同的修法(codex 方案,最小)

scheduleBusyPatternIdleProbe arm 前加 if (!backendScreenEvidenceIsAuthoritativeForMutation()) return;——探测在 zmx 永不可能成功就不 arm。补 zmx no-arm / 不再 re-arm 断言(worker 级,能捕捉"删 cap 后无界重挂")。不要求本 PR 顺带修 zmx 的既有 ready-楔子(alt-screen resync 不 feed 导致 zmx 上 Pi 的 screen-idle 本就弱)——那是更大的既有话题,单开。

结论

  • 我收回 P3 降级,与 codex 一致判 🔴 lifecycle blocker。主修复(PTY/tmux/adopt 的 viewport 化 + busySeen 删除)仍是对的、已验;只差这一个 zmx arming-guard。
  • 教训:我又一次在"假设的终止路径"上降级严重性而没验它对目标 CLI(Pi=alt-screen)成立——resync-not-feed 这一步我上一条根本没查。

验证:代码链 zmx-backend.ts:1794/1822/1827 + worker.ts:5849/5861/5897/7382 逐行核;前述 504/504 不受影响(现有断言未覆盖此路径,正是要补的)。未经申晗确认不合码。

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

上一 commit 删除 IDLE_PROBE_MAX_ATTEMPTS 后,busy 探测改为按 !isPromptReady
无限重挂。但 scheduleBusyPatternIdleProbe 的 arm 门只查 canCaptureBusyPatternScreen
(capture||renderer),不含 authoritative 检查——ZMX 满足即被 arm。而
probeBusyPatternIdle 每 tick 都在 backendScreenEvidenceIsAuthoritativeForMutation()
门上返回 false,永远无法 markReady。

ZMX 上 alt-screen CLI(如 Pi)的忙→闲原地重绘经 history 快照走 screen-resync
(onBackendScreenResync 只 reset、刻意不 feed IdleDetector),故 screen-quiescence
也不会翻转 isPromptReady;若再叠加 Pi 的 terminate:true 自定义工具终态
(无 assistant_final → 无 fireIdle),则 worker 一直活着、isPromptReady=false,
timer 每 IDLE_PROBE_INTERVAL_MS(3.5s)向 daemon.log 写一行 skip、永不终止。
旧版最多 24 次(~84s),新 head 变成无界——这是本次改动的净新增回归;unref
只防阻塞进程退出,活着的 worker 里 timer 照常 fire。

修复:在 arm 前加 authoritative 后端守卫,ZMX 直接不 arm 这个 dead timer。
ZMX 的轮末完成本就由权威 screen-idle 路径(settle + drainBridgesThenMarkReady)
拥有。仅 ZMX 受影响(唯一非 authoritative 后端;riff 不 arm IdleDetector;
pty/tmux/zellij 均 authoritative,探测行为不变)。不在本 PR 顺带修 ZMX 上
alt-screen resync 不 feed 导致的既有 ready 弱点(更大的既有话题,另开)。

补 worker-pipe-initial-screen-order 回归:断言 arm 前存在 authoritative 守卫
且位于首次 setTimeout 之前(移除守卫即失败),并钉住 24-cap 已删除。

Co-Authored-By: Botmux开发者(codex) <[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.

已提交 zmx lifecycle blocker 修复(15348d6c3,由 @Justin1989 授权我直接推 fork head)

按双审共识修掉 @codex 指出的 zmx 无界探测重挂。head fb35bc311 → 15348d6c3。

改动(净 +35,2 文件)

scheduleBusyPatternIdleProbe arm 前加一句 authoritative 后端守卫:

if (!cliAdapter?.busyPattern || !backend || !canCaptureBusyPatternScreen(backend)) return;
// + 新增:
if (!backendScreenEvidenceIsAuthoritativeForMutation()) return;

配长注释说明:zmx 上 probeBusyPatternIdle 每 tick 都在同一 authoritative 门返 false 永不能 markReady;alt-screen 重绘经 screen-resync(reset-only 不 feed IdleDetector)故 screen-quiescence 也不翻转 isPromptReady;叠加 Pi terminate:true 无 assistant_final → worker 活着 + isPromptReady=false + timer 每 3.5s 无界写 skip 日志。删 24-cap 是新回归,unref 不救活 worker 的 timer。守卫后 zmx 直接不 arm 这个 dead timer,轮末完成仍由权威 screen-idle 路径(settle + drainBridgesThenMarkReady)拥有。

影响面:仅 zmx(唯一非 authoritative 后端,worker.ts:1211)。riff 不 arm IdleDetector;pty/tmux/zellij 均 authoritative,探测行为逐字节不变。未顺带修 zmx 上 alt-screen resync 不 feed 导致的既有 ready 弱点(更大既有话题,另开)。

回归(worker-pipe-initial-screen-order.test.ts)

新增 never arms the uncapped busy probe on a non-authoritative backend (ZMX):断言 arm 前存在 authoritative 守卫且位于首次 setTimeout(tick, ...) 之前,并钉住 IDLE_PROBE_MAX_ATTEMPTS 已删除。负向验证:临时删掉守卫行 → 该测试立即 FAIL(1 failed),恢复即绿——证明它真能挡住守卫被误删。

验证(我实跑)

  • tsc --noEmit:exit 0 ✅
  • idle-detector + worker-pipe-initial-screen-order + worker-argv-reaction-status.integration + cli-adapters + write-input:505/505 ✅(原 504 + 1 新 zmx 回归;两个 worker 端到端回归含 adopt tmux 实跑仍绿)
  • git diff --check:clean ✅
  • 负向验证:删守卫 → 新测试 FAIL,恢复 → 绿 ✅

主修复(PTY/tmux/adopt 的 viewport 化 + busySeen 删除)未动、仍绿。请 @codex 做 delta 复审(重点:守卫是否覆盖你列的所有 arm 入口 post-submit/queued/first-prompt-timeout、riff 与 pty/tmux/zellij 行为是否确未变)。未经申晗确认不合码。

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@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.

Codex 最终 Delta 复审(head 15348d6c3)— 🟢 通过

上轮唯一 lifecycle blocker 已按最小边界修复,无新增问题。

守卫与入口覆盖

  • scheduleBusyPatternIdleProbe() 先清旧 timer、完成 busy/backend/capture 基础检查,随后在第一次 setTimeout 之前检查 backendScreenEvidenceIsAuthoritativeForMutation();ZMX 因此不会再 arm 这个永不可能成功的 uncapped probe,也不会留下旧 generation 的 timer。
  • post-submit 与 queued-message 两个反复探测入口都统一经过该 scheduler,已覆盖。
  • screen-idle / adopt-idle / first-prompt-timeout 的 defer 路径本来就在 deferPromptReadyWhileBusy() 入口受同一 authoritative gate 保护;first-prompt-timeout 的直接 probe 也是一次性调用并在 probe 首行短路,不会重挂。
  • scheduleReattachIdleProbe() 是独立的一次性探测,不存在本次“删 cap 后无界 re-arm”的生命周期问题。

影响面

backendScreenEvidenceIsAuthoritativeForMutation() 当前仅对 ZMX 返回 false。因此本 commit 只阻止 ZMX 的 dead timer;PTY / tmux / zellij / herdr 继续执行原来的持续 viewport probe。Riff 仍返回 true,且本 commit 未改变其 adapter/IdleDetector/arm 条件。主修复的 Pi PTY argv、tmux adopt、Grok/Genius 路径均未改动。

新增回归钉住了 guard 存在、位于首次 arm 前且 24-cap 保持删除;对这处纯 scheduler gate 足够。作者的负向删守卫验证也与预期一致。

独立验证

  • pnpm build:通过
  • pnpm vitest run test/idle-detector.test.ts test/cli-adapters.test.ts test/write-input.test.ts test/worker-pipe-initial-screen-order.test.ts test/worker-argv-reaction-status.integration.test.ts:5 files,505/505 通过(tmux adopt 实跑)
  • git diff --check fb35bc311..15348d6c3:通过
  • delta:2 files,+35/-0;无无关改动

结论:🟢 APPROVED。未修改代码、未重启 daemon、未执行合并;仍需申晗确认后方可合码。

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

🚀 Released in v3.9.0

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