pi-crew 0.11.37 → 0.11.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/CHANGELOG.md +89 -0
  2. package/agents/critic.md +13 -0
  3. package/agents/oracle.md +14 -0
  4. package/dist/index.mjs +531 -526
  5. package/package.json +4 -3
  6. package/skills/delegation-patterns/SKILL.md +16 -0
  7. package/skills/real-test-pi-crew/SKILL.md +13 -2
  8. package/src/config/env-allowlist-registry.ts +22 -1
  9. package/src/config/env-vars.ts +23 -0
  10. package/src/extension/registration/foreground-run-controller.ts +21 -1
  11. package/src/extension/registration/lifecycle-handlers.ts +12 -0
  12. package/src/extension/team-tool/doctor.ts +125 -1
  13. package/src/prompt/prompt-runtime.ts +67 -5
  14. package/src/runtime/child-pi/child-pi-kill.ts +68 -2
  15. package/src/runtime/child-pi/child-pi-spawn.ts +20 -0
  16. package/src/runtime/child-pi/child-pi.ts +3 -1
  17. package/src/runtime/foreground-watchdog.ts +394 -17
  18. package/src/runtime/heartbeat/worker-heartbeat.ts +4 -0
  19. package/src/runtime/model/model-fallback.ts +5 -0
  20. package/src/runtime/process/orphan-reaper.ts +197 -22
  21. package/src/runtime/process/proc-stat.ts +8 -0
  22. package/src/runtime/process/reap-group.ts +499 -0
  23. package/src/runtime/process/run-descendant-sweep.ts +744 -0
  24. package/src/runtime/recovery/crash-recovery.ts +202 -0
  25. package/src/runtime/rpc/frame-client.ts +21 -1
  26. package/src/runtime/stale-reconciler.ts +343 -2
  27. package/src/runtime/task-runner/child-executor.ts +15 -1
  28. package/src/runtime/task-runner/post-execution.ts +112 -1
  29. package/src/runtime/task-runner/state-helpers.ts +28 -1
  30. package/src/runtime/verification/verify-gate-memo.ts +127 -0
  31. package/src/runtime/verification/verify-gate.ts +127 -1
  32. package/src/state/coordination/mailbox.ts +68 -0
  33. package/src/state/stores/state-store.ts +8 -0
  34. package/src/state/types.ts +61 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-crew",
3
- "version": "0.11.37",
3
+ "version": "0.11.39",
4
4
  "description": "Pi extension for coordinated AI teams, workflows, worktrees, and async task orchestration",
5
5
  "author": "baphuongna",
6
6
  "license": "MIT",
@@ -73,10 +73,11 @@
73
73
  ],
74
74
  "scripts": {
75
75
  "check": "npm run ci",
76
- "ci": "npm run check:lockfile-sync && npm run typecheck && npm run lint && npm run format:check && npm run check:conflict-markers && npm run check:decision-drift && npm run check:env-vars && npm run check:event-types && npm run check:lazy-imports && npm run check:wc-gate && npm run check:bundle-staleness && npm run build:bundle && npm run check:bundle-size && npm run test:bundle && npm test && npm pack --dry-run",
76
+ "ci": "npm run check:lockfile-sync && npm run typecheck && npm run lint && npm run format:check && npm run check:conflict-markers && npm run check:decision-drift && npm run check:env-vars && npm run check:event-types && npm run check:lazy-imports && npm run check:slop-rules && npm run check:wc-gate && npm run check:bundle-staleness && npm run build:bundle && npm run check:bundle-size && npm run test:bundle && npm test && npm pack --dry-run",
77
77
  "ci:fast": "npm run check:lockfile-sync && npm run typecheck && npm run lint && npm run format:check && npm run check:conflict-markers && npm run check:decision-drift && npm run check:env-vars && npm run check:event-types && npm run check:lazy-imports && npm run check:wc-gate && npm run check:bundle-staleness && npm run build:bundle && npm run check:bundle-size && npm run test:bundle && npm run test:critical && npm pack --dry-run",
78
78
  "check:lockfile-sync": "node scripts/check-lockfile-sync.mjs",
79
79
  "check:lazy-imports": "node scripts/check-lazy-imports.mjs",
80
+ "check:slop-rules": "node scripts/check-slop-rules.mjs",
80
81
  "check:bundle-staleness": "node scripts/check-bundle-staleness.mjs",
81
82
  "check:bundle-size": "node scripts/check-bundle-size.mjs",
82
83
  "check:conflict-markers": "node scripts/check-conflict-markers.mjs",
@@ -117,7 +118,7 @@
117
118
  "smoke:pi": "pi install .",
118
119
  "smoke:release": "node scripts/release-smoke.mjs",
119
120
  "prepack": "node scripts/clean-strip-types.mjs",
120
- "prepublishOnly": "npm run ci",
121
+ "prepublishOnly": "node scripts/prepublish-ci.mjs",
121
122
  "test:fast": "npm run test:critical && npm run test:bundle",
122
123
  "check:wc-gate": "node scripts/wc-gate.mjs"
123
124
  },
@@ -135,3 +135,19 @@ attempt X of 3 (Y attempts remaining)
135
135
  An attempt is one delegation decision (choose agent/team → dispatch → collect). Re-attempt when the delegation target fails or returns unusable output.
136
136
 
137
137
  Re-attempts only when the previous attempt materially changes the decision or risk. Do NOT spend a re-attempt on mechanical changes or already-resolved findings. When exhausted, escalate to the user with options (accept risk / change scope / exceptional budget).
138
+
139
+ ## Budget-boundary policy (W10-6, pi-subagents lesson #16)
140
+
141
+ Two load-bearing rules when a worker hits a budget or time boundary:
142
+
143
+ 1. **Budgets block read-tools, NOT mutation-tools.** When a worker is near
144
+ its token/tool budget, the safe de-escalation is to stop EXPLORING (reads,
145
+ greps, finds) and conclude from what it already has — never to keep
146
+ spending while a half-finished mutation sits on disk. A worker out of
147
+ budget mid-edit must report the partial state, not "roll on".
148
+ 2. **An elapsed timeout is NOT a mutation-safe boundary.** Wall-clock expiry
149
+ can fire between two filesystem writes — whatever cleanup you planned
150
+ assumes a consistent tree it does not have. On timeout: stop issuing new
151
+ mutations, capture the current state as evidence, and let the leader
152
+ adjudicate. Never auto-revert (the revert itself is a mutation racing the
153
+ timeout).
@@ -119,6 +119,7 @@ End-to-end verification discipline for pi-crew changes. Distilled from the broke
119
119
  | `src/ui/` | T1 → (T5|T6) → T13 |
120
120
  | `src/config/` (kể cả migration-validator) | T1 → T11c |
121
121
  | `src/runtime/child-pi/` (spawn/kill/steer) | T7 → T9g |
122
+ | `src/runtime/process/**` (reap-group.ts / run-descendant-sweep.ts) + `child-pi-kill.ts` exited-root branch + `orphan-reaper.ts` identity admission + `crash-recovery.ts` terminal-sweep lane + state fields `childPgid/childStartTime/childUid` + heartbeat `pgid/starttime/uid` (wave-11 class: runtime spawn/kill/teardown + startup reconcile + state schema ADDITIVE) | T1 → pinned unit `test/unit/runtime/process/{reap-group,run-descendant-sweep}.test.ts` + `test/unit/runtime/recovery/reconcile-terminal-run-descendant-sweep.test.ts` + `test/unit/runtime/stale-reconciler-run-descendant-sweep.test.ts` + `test/unit/runtime/stale-reconciler-reap-escalation.test.ts` (5 file / 78 tests @ `f578c223`) → **integration live repro** `node scripts/test-runner.mjs test/integration/run-descendant-sweep-live.test.ts` (A1–A5: spawn leak THẬT → sweep reap ~100 ms; **negative control bắt buộc SỐNG**: no-marker / interactive-tty / parent-còn-sống) → off-path proof `PI_CREW_REAP_GROUP=0` + `PI_CREW_REAP_RUN_DESCENDANTS=0` (unit xanh trên CẢ hai trạng thái switch; KHÔNG để ambient `=0` sót lại làm run thật câm) → T9a `team action='doctor' focus='zombies'` (mảng `runDescendants` report-only + dòng cảnh báo no descent proof ⇒ NOT reaped) |
122
123
  | `src/runtime/mcp-proxy.ts` / `src/runtime/model/pi-args.ts` (worker argv build + MCP role-gate — G2/G3 surface, SDD-2) | T7 (`argv-flags.smoke.ts` với `PI_CREW_SMOKE=1`) → T9b-W loadout theo role (post-G2: read-only KHÔNG có MCP trên live-session path là ĐÚNG — xem mục full-loadout sanity 9b) + pinned suites `mcp-proxy.test.ts` / `phase6-runtime-hardening.test.ts` / `child-pi-timeout`+`child-pi-surface` (argv/spill) |
123
124
  | `src/runtime/async-runner.ts` / `src/runtime/background-runner.ts` (spawn, allowlist, entry-guard, `executeBackgroundRun` core) | T1 → T9b async (event `async.spawned` KHÔNG được có `data.inline:true` — battery phải chứng minh runner DETACHED thật) → T7; đụng `BACKGROUND_RUNNER_ENV_ALLOWLIST` → thêm T10b/T10c (quirk F4-2026-08-30: mux env bị strip ⇒ async headless âm thầm) |
124
125
  | `src/runtime/surface/` | T10a → T10b → T10c (dò herdr bằng `HERDR_ENV=1`, KHÔNG phải `$TMUX`) |
@@ -133,7 +134,8 @@ End-to-end verification discipline for pi-crew changes. Distilled from the broke
133
134
  | `scripts/wc-gate.mjs` | T11b |
134
135
  | `scripts/test-runner.mjs` (the runner itself) | T1 (exercises it) + `test/unit/scripts/test-runner-exit.test.ts` (21 tests: exit codes, cancelled-guard red-green, missing-file pre-check, batch shard empirical) + ONE batch-mode run (`PI_CREW_TEST_BATCH_SIZE=2 node scripts/test-runner.mjs --shard=0/1 <3 small files>`). **Home isolation per-suite từ 2026-10-11** (set CẢ `PI_CREW_HOME` + `PI_TEAMS_HOME` — mirror pair, đặt 1 tên là defeatable; sweep prefix `pi-crew-` cover orphan `pi-crew-test-home-*`; opt-out `PI_CREW_TEST_NO_HOME_ISOLATION=1`) → khi đụng isolation: leak detector = đếm run-dirs `~/.pi/agent/extensions/pi-crew/state/runs/` before/after một suite, KHÔNG được tăng. Dev script — NOT bundled, no T3/T4 needed |
135
136
  | `scripts/check-lazy-imports.mjs` | Run the gate on a clean tree (`node scripts/check-lazy-imports.mjs` exit 0) + re-probe the pattern battery vs the KNOWN GAPS comment (round-3 lesson: a skip-rule fix can OPEN a new false-skip hole — probe both directions; fixture suite `test/unit/scripts/check-lazy-imports-gate.test.ts` 4 tests probe cả 2 hướng). **AST-based từ Δ2 (2026-10-11, esbuild strip + acorn)**: per-entry budget `{maxFiles}` là RATCHET — bump chỉ khi chủ ý + justify trong commit; hosted-git-info allowlist ĐÃ xóa (2026-10-12, dead require dropped — gate strict trở lại). NOT bundled |
136
- | `src/runtime/verification/verify-gate.ts` + task-runner gate mount (U13) | T7 smoke ×2 — (a) goal whose checks PASS → run must reach `completed` (NOT `blocked`) + `task.verifier_pre_gate` event with `skipVerifier:true` + verifier result artifact carries gate summary; (b) goal whose checks FAIL (e.g. `false` command) → LLM verifier spawns WITH gate context. **Guards the U13×effectiveness interplay** (live-caught 2026-10-10: green-gated verifier = no usage/transcript by design → effectiveness guard false-blocked the run → sync calls hung forever; fixed by counting `diagnostics.verifyGate` as observable work — `effectiveness-guard.test.ts` pins it). U13-ADJ (2026-10-13): layer-3 determinism `PI_CREW_VERIFY_DETERMINISM` DEFAULT OFF — battery recipe KHÔNG đổi trừ khi flag bật; khi bật: rerun critical-check cuối cùng PASS, flaky/executor-death → INCONCLUSIVE (không FAILED), không rerun khi checks đã FAILED; belt never-fail-task giờ wrap cả writeArtifact skip-path + finalizer NGỒI NGOÀI try (throw lan truyền, không fall-through spawn) |
137
+ | `scripts/check-slop-rules.mjs` (W10-4, v0.11.38) | Run gate trên tree sạch (`node scripts/check-slop-rules.mjs` exit 0, in `no-double-cast baseline 41, current N (burn-down ok)`) + suite `test/unit/scripts/check-slop-rules.test.ts` (5 tests, scanner semantics) + `test/unit/scripts/check-slop-rules-cli.test.ts` (CLI e2e). 3 scanner: `no-json-parse-nonnull` + `no-any-deref` STRICT (clean), `no-double-cast` WARN-only trên baseline 41. **Ratchet**: count no-double-cast > 41 → exit 1 — fix site mới (hoặc type relationship) hoặc thêm `// slop-ok: <lý do>`; KHÔNG tăng baseline trừ khi chủ ý + justify trong commit (burn DOWN only). NOT bundled |
138
+ | `src/runtime/verification/verify-gate.ts` + task-runner gate mount (U13) | T7 smoke ×2 — (a) goal whose checks PASS → run must reach `completed` (NOT `blocked`) + `task.verifier_pre_gate` event with `skipVerifier:true` + verifier result artifact carries gate summary; (b) goal whose checks FAIL (e.g. `false` command) → LLM verifier spawns WITH gate context. **Guards the U13×effectiveness interplay** (live-caught 2026-10-10: green-gated verifier = no usage/transcript by design → effectiveness guard false-blocked the run → sync calls hung forever; fixed by counting `diagnostics.verifyGate` as observable work — `effectiveness-guard.test.ts` pins it). U13-ADJ (2026-10-13): layer-3 determinism `PI_CREW_VERIFY_DETERMINISM` DEFAULT OFF — battery recipe KHÔNG đổi trừ khi flag bật; khi bật: rerun critical-check cuối cùng PASS, flaky/executor-death → INCONCLUSIVE (không FAILED), không rerun khi checks đã FAILED; belt never-fail-task giờ wrap cả writeArtifact skip-path + finalizer NGỒI NGOÀI try (throw lan truyền, không fall-through spawn). **W10-2 memo (2026-10-22, v0.11.38)**: verdict PASS được memoize theo workspace tree OID (`git write-tree` — FULL tracked tree, tree equality subsumes manifest bookends) + exact check list (name+command) + node version — kill-switch `PI_CREW_VERIFY_GATE_MEMO=0` (default ON); battery recipe thêm 1 probe: re-gate cùng cây trong cùng host process → evidence line `verify-gate-memo: HIT`, checks skipped; KHÔNG memo khi repo không-git (key undefined ⇒ fail-open full gate run) / checks rỗng (0-check repo — live-proven không false-hit) / verdict không-PASS (chỉ PASS ran>0 record — FAILED/INCONCLUSIVE/drift-downgrade never) / typed gate. **W10-9 typed gates (2026-10-22, v0.11.38)**: check accept `{output:"json", schema}` (minimal subset type/required/properties/items/enum, zero deps) — stdout phải parse AND match: invalid JSON / schema mismatch FAIL tại exit 0, parsed `structuredOutput` land trên check result; typed gate KHÔNG BAO GIỜ memo (guard cả lookup lẫn record) |
137
139
  | `src/state/coordination/**` (run-lock socket U21, 2026-10-13) | T7 + pinned `test/unit/state/coordination/{process-lock,run-lock-socket}.test.ts` (U21-1..6) — escape hatch `PI_CREW_RUN_LOCK_SOCKET=0` (pre-U21 bit-for-bit). Semantics MỚI khi socket ON (default): stale-steal một LIVE holder giờ WAIT trong deadline `staleMs*2` thay vì dual-entry (test cũ `run-lock-steal-live-holder-warn.test.ts` giờ env-scoped socket=0). **Test-rule cho mọi lock-freeze tương lai**: PHẢI await acquired-signal deterministic từ body (pattern `team-tool-resume-liveness` F1 sau 2026-10-13) — socket-listen là MACROTASK, assumption "acquisition thắng race với await kế tiếp" đã sai 1 lần (RR-011 token từng vô hình với contender same-process trong cửa sổ file→socket) |
138
140
  | `PI_CREW_STATE_BACKEND=sqlite` (U8 state backend) | Unit: `sqlite-run-state.test.ts` + `state-store-sqlite-parity.test.ts` (incl. coalesced-mirror pin). LIVE: one fast-fix run with the env set → assert `state.sqlite` exists, **sqlite tasks reach terminal status** (live-caught 2026-10-10: `saveRunTasksCoalesced` hot path skipped the db — manifest+tasks.json completed while sqlite stuck at createRun `queued`), events.jsonl still written (audit spine), JSON mirrors present |
139
141
  | `PI_CREW_CHILD_PI_MODE=rpc` / `PI_CREW_CHILD_PI_POOL` (U14 transport) | Unit: `child-pi-rpc-mode.test.ts` (18) + `owned-process-u15.test.ts` (11). LIVE: one async run with env set → workers' status.json carry rpc transport markers, run green, `pgrep -f "mode rpc"` clean after completion (no orphan rpc processes), json default unaffected |
@@ -186,6 +188,12 @@ End-to-end verification discipline for pi-crew changes. Distilled from the broke
186
188
 
187
189
  > **2026-10-14 update #2 (dup-noti idle-gate + release 0.11.20 follow-up, commits `afc4fe9c`..HEAD trên cùng wave)**: (1) **DUP-NOTI RCA + FIX (live-caught 6×: P3/X1/E5 + 3 shards)** — noti trùng KHÔNG phải do emit 2 lần mà do `sendUserMessage(deliverAs:"followUp")` KHÔNG retract được (SDK `clearQueue()` all-or-nothing — dùng là phá luôn steering của user): leader consume mid-turn (get_subagent_result wait=true) → message ĐÃ queue vẫn deliver ở turn boundary = 1 turn phí mỗi lần. Rule-2 re-check canh SAI cửa sổ (chặn emit mới, không unsend được). **Fix = idle-gate** trong `subagent-manager-setup.ts`: `leaderTurnActive(ctx.currentCtx.isIdle())` (capability-guarded — host thiếu = bit-for-bit, pinned bởi Rule1-3 integration chạy trên fallback) + `runWhenLeaderIdle` (retry 500ms, cap 10' — muộn hơn never nhưng re-check vẫn chặn consumed quá cap); coalescer flush idle-gate TRƯỚC splice; batch path NOTIFY_DEFER_MS bị subsume (đã xóa). **Probe-quirk mở rộng**: fresh `pi -p` + prompt nặng (đụng crew_agent/team tool) có thể treo 0-output trong khi prompt đơn giản OK (provider-side, không phải pi-crew) — idle-gate live-probe qua đường này có thể bị chặn bởi môi trường; khi đó dựa vào unit (5/5 pin đúng race semantics) + Rule1-3 14/14. (2) **Goal FALSE-GREEN fix**: `goalDeclaresReadOnly` (marker EN+VI literal, explicit-intent) + bước (1b) — live-proven run `14022151` ("goal declared read-only (no edits expected)"). (3) **m6**: state/usage.ts dead 3-field family xóa; RoleUsage +cacheRead; ledger-vs-display docblock (#38 chỉ đúng cho display/budget-proxy). (4) Suite anchors @ 0.11.20-tree (`8e70e66f`): unit **9146 / 9143 pass / 0 fail / 0 cancelled / 3 skipped** (composition đổi sau m6 −dead-describe), critical **120**, bundle **1,778,118 B (1736.4 KB)** md5 `8e70e66f543106b1ac34902bd4047302` (committed==fresh), census 15.
188
190
 
191
+ > **2026-10-22 update (wave 10 — pi-subagents lessons port, v0.11.38, commits `b1288e3b`..`0ca9b552`, battery report `docs/real-test/reports/real-test-2026-10-22-wave10-pisub-lessons-battery.md`)**: 10 ports — W10-1 worker cache-retention split (`PI_CACHE_RETENTION=short` cho headless single-run; surface/RPC warm giữ parent default); W10-2 memoized green verify-gate theo tree OID (xem row verify-gate); W10-3 mailbox typed reason enum + strict replyTo routing (+ hardening `c95bb761`: findAskResponse iterate-past-mismatch, misroute-then-resend không park tới deadline, taskId-anchored filter); W10-4 `check-slop-rules` gate (3 scanner, ratchet 41 — row mới); W10-5 decision-drift guardian (critic/oracle inherited-decisions + DRIFT-BLOCK → verdict degrade `none`, fail-closed); W10-6 budget-boundary policy text (delegation-patterns: budgets chặn read-tools KHÔNG phải mutation-tools; elapsed-timeout KHÔNG phải mutation-safe boundary); W10-7 usage baseline-reconcile (per-attempt usage + note observability-only khi gap >5%); W10-8 read-lane same-tree guard (`git status --porcelain` gồm untracked → note `unexpectedMutation`, leader quyết); W10-9 typed JSON verification gates (row verify-gate); W10-10 CHANGELOG wave-10 + 2 lessons evidence-closed (spawn/parallel REVISED-BY-MEASUREMENT, declaration-pinning NOT-APPLICABLE). Battery coverage T1/T3/T4/T8/T7×3/T9a/T9g/T11a/c/j/T12 — PASS (unit 9876, integration 143, slop/lazy/staleness/size/bundle exit 0). Live-caught: verifier worker `cacheRead:11072` (prompt-cache reuse trên worker — evidence trực quan cho fork-cache PoC); T9b-W partial-honest (3-role spawn-path xanh thay full-loadout checklist). Hướng dẫn: đổi `src/runtime/verification/` hoặc task-runner post-execution → ngoài rows hiện có THÊM probe memo-hit (re-gate same-tree same-host → `verify-gate-memo: HIT`); đổi `prompt-runtime.ts` findAskResponse → chạy `ask-tool-replyto-routing.test.ts` (5) + `mailbox-replyto-reason.test.ts` (8) — 13 tests pin misroute-resend + taskId-anchor.
192
+
193
+ > **2026-10-24 update (wave 11 — worker descendant reaping, commits `ed32c582`→`f578c223` gồm dist rebuild; report `docs/real-test/reports/real-test-2026-10-24-wave11-descendant-reaping.md`)**: (1) **Root cause KHÔNG phải "worker quên kill con"** — pi's bash tool spawn MỖI command `detached:true` (mỗi command một process group riêng, là con của pi worker chứ không nằm trong group worker) nên pid-only SIGTERM không bao giờ tới cháu; run `team_20261022110510_cefb9164af126460` terminal xong mà 6 cháu bash sống 24 h (~390 MB RSS). (2) **Surface mới `src/runtime/process/reap-group.ts` (F5)** — NƠI DUY NHẤT được signal một POSIX process group: `reapGroup` + `assertSafeGroup` (ladder từ chối 8 rung: POSIX + /proc đọc được; pgid nguyên > 1 — không bao giờ `kill(-1/0/1)`; không phải group/pid/parent của MÌNH theo REAL pgrp đọc từ `/proc` qua `selfIdentity()` — fix CRITICAL #1 host `setsid`; leader-proof = identity sống HOẶC snapshot caller chứng minh `pid===pgid` leader + starttime — snapshot không còn là identity thay thế, fix MAJOR #2 blind kill; same-uid; real-pgid == requested (PID-reuse); starttime bắt buộc; snapshot-starttime khớp live) + escalation SIGTERM (đồng bộ, postmortem-safe) → bounded poll → SIGKILL (timer unref — `drainReapEscalations()` / `flushReapEscalationsSync()` sở hữu rung cuối trên session shutdown). Wired: `child-pi-kill.ts` nhánh exited-root (snapshot pgid/starttime/uid TRƯỚC khi root chết; 60 s sweep prune) + `orphan-reaper.ts` admission bằng identity-proven (không còn tin `allowedPids` mù; scrub `PI_CREW_*` vẫn bắt buộc trước test). Kill switch `PI_CREW_REAP_GROUP=0`, default ON, FORWARD sang background runner (env-allowlist — switch không forward = câm im lặng). (3) **Surface mới `src/runtime/process/run-descendant-sweep.ts` (F7)** — `collectRunDescendants`/`reapRunDescendants` quét `/proc` (1 snapshot/pass, `buildProcSnapshot`) match marker mạnh→yếu: `run-id` (`PI_CREW_BROKER_RUN_ID` — 11b.4: marker KHÔNG phải proof, nhánh còn đòi một parent ĐÃ GHI ÂM (runner/worker/surface pane) ĐÃ CHẾT), `task-id` (`PI_CREW_KIND=subagent`+`PI_CREW_TASK_ID`+dead parent), `parent-pid` (`PI_CREW_PARENT_PID` chết — đúng dạng sự cố). Guard: same-uid, không phải self/ancestor, starttime ≥ runStart−60 s, **skip tty (lý do `interactive-tty`)**, leader chết hoặc là candidate khác, `surfacePids` sống bị loại. Kill switch `PI_CREW_REAP_RUN_DESCENDANTS=0` (no-op hoàn toàn, cũng forward sang runner). (4) **Startup lane** `crash-recovery.ts` `selectUnsweptTerminalRunIds`: mọi run terminal (`completed|failed|cancelled`) chưa có `descendantSweepAt`, `updatedAt` ≥ 60 s (grace cho pane), CŨ NHẤT TRƯỚC, cap 25/pass, window 200; `descendantSweepPending` retry sau 10 phút (HIGH-2: sweep-fail không leak vĩnh viễn). (5) **Doctor** `focus=zombies`: mảng `runDescendants` (report-only, dry-run) + cảnh báo process không chứng minh được descent ⇒ NOT reaped. (6) **State additive**: `TaskCheckpointState.childPgid/childStartTime/childUid` + `WorkerHeartbeatState.pgid/starttime/uid` — optional, record cũ degrade pid-only. (7) **Battery** (decision-table row mới): T1 + 5 unit suite (78 tests) + integration live repro A1–A5 (leak thật bị reap 91–121 ms, 3 negative control sống, 3/3 chạy ổn định) + off-path `=0` cả 2 switch + doctor dry-run; **ambient `PI_CREW_REAP_*=0` làm live repro câm — scrub trước khi chạy thật** (row Failure symptoms mới).
194
+
195
+ > **2026-10-24 đính chính (wave 11c — battery findings F1+F2, mục 4b/7b của `docs/real-test/reports/real-test-2026-10-24-wave11-battery.md`)**: (1) **F2 sync-run miss ĐÃ FIX** — run sync/foreground không có `manifest.async.pid` nên leak mang `PI_CREW_PARENT_PID=<session pi pid>` không bao giờ qua descent proof (đúng rule 11 — an toàn nhưng bỏ sót vĩnh viễn). Từ 11c mọi manifest mới record `sessionPid` (= `process.pid` tại `createRunManifest`); `sweepTerminalRunDescendants` + doctor dry-run đưa nó vào `proofParents` với guard dead-only như mọi parent khác — session còn sống ⇒ KHÔNG là proof (user có thể đang dùng). Run async vẫn dùng `async.pid` primary. Battery addendum sẽ chứng minh bằng E2E sync-leak thật. (2) **F1 test-isolation ĐÃ FIX** — suite `run-descendant-sweep.test.ts` giờ pin `PI_CREW_REAP_RUN_DESCENDANTS=1` trong beforeEach (restore verbatim afterEach, pattern của `stale-reconciler-run-descendant-sweep`): 17/78 test không đỏ nữa khi ambient `=0`; pinned 78→82 (a20 + g8–g10); decision-table yêu cầu "unit xanh trên CẢ HAI trạng thái switch" giữ nguyên.
196
+
189
197
  ## Core principle: disk ≠ live Pi
190
198
 
191
199
  Two locations hold pi-crew state:
@@ -1255,6 +1263,7 @@ When a tier fails, the recovery is usually quick. Match the symptom to the cause
1255
1263
  | `team action='goal' budgetTotal=100000` rejected at validation ("must be equal to constant / must be number") | pi-ai stringifies numeric args on `Literal("")` unions; budgetTotal lacked the stringified-number branch its sibling budget params have. Fixed 2026-09-21 | Workaround pre-fix: `config.budgetUnlimited:true` (note: in config, NOT top-level) |
1256
1264
  | `team action='schedule' interval=<ms>` always says `Invalid schedule "<N>ms". Use "5m", …` | handle-schedule builds `${interval}ms`; both interval parsers lacked an `ms` unit — every numeric interval died. Fixed 2026-09-21 | The suggested "5m" formats only work via `cron`/`once` params, not `interval` (interval is schema-typed as a plain ms number) |
1257
1265
  | Runs keep appearing under one scheduled job; children of YOUR pi process; machine thrashing | **Interval ≥ 2^31-1 ms (≥24.8 days) overflowed the Node timer to a 1ms hot loop** (fixed 2026-09-22 — clamped hops). Check `.crew/audit/prune.jsonl` for interleaved auto-prunes. Contain: remove job, then `pkill -f '[b]ackground-runner.ts --cwd <root>'` | Any new timer scheduling must clamp below MAX_TIMER_DELAY_MS — grep for `setInterval(`/`setTimeout(` with computed delays |
1266
+ | Live sweep/reap probe chạy "xanh" nhưng KHÔNG process nào bị reap (doctor `runDescendants` liệt kê mà không có gì chết) | **Ambient `PI_CREW_REAP_GROUP=0` / `PI_CREW_REAP_RUN_DESCENDANTS=0` còn trong shell** (sót từ probe off-path trước) — cả 2 switch default ON và được FORWARD sang background runner, ambient là đủ để câm sạch cả sweep lẫn group-reap; ngược lại quên set khi test off-path thì nhánh degrade không được chứng minh | Trước MỌI live repro: `env \| grep PI_CREW_REAP` phải RỖNG; chỉ set `=0` CHỦ ĐÍCH khi chạy nhánh off-path (unit phải xanh trên CẢ hai trạng thái switch), unset ngay sau (wave 11 lesson — 2 kill switch KHÔNG được set ngoài ý muốn theo cả 2 hướng) |
1258
1267
 
1259
1268
  ## Release gate — battery → push → GitHub Actions GREEN → publish (canonical, 2026-10-14)
1260
1269
 
@@ -1267,7 +1276,7 @@ corpse → xóa file socket + rebind (STEAL lock) — fix bằng 3-strike takeov
1267
1276
  **Quy trình BẮT BUỘC cho mọi release (theo thứ tự, không bỏ bước):**
1268
1277
 
1269
1278
  1. Cập nhật skill (wave entry) + CHANGELOG heading cho version sắp release — commit.
1270
- 2. Chạy battery theo decision table (đủ tier cho paths đổi) — xanh hết.
1279
+ 2. Chạy battery theo decision table (đủ tier cho paths đổi) — xanh hết, **kể cả integration live repro khi row đó yêu cầu** (vd wave 11 → `test/integration/run-descendant-sweep-live.test.ts` A1–A5 PHẢI xanh trước publish — live repro là tier duy nhất chứng minh sweep giết leak thật mà không đụng process của operator).
1271
1280
  3. `npm version patch` (tạo release commit + tag).
1272
1281
  4. `git push origin main --follow-tags` — pre-push hook tự chạy full unit local.
1273
1282
  5. **`gh run watch <runId> --exit-status` (hoặc poll `gh run list`) — CHỜ matrix 3-OS XANH TOÀN BỘ.**
@@ -1431,6 +1440,8 @@ Use this to answer "đủ full tính năng chưa?" without re-deriving. Every us
1431
1440
  | No-op writer gating (metric-sink crewRoot, prune-audit bail) | `src/observability/metric-sink.ts`, `src/extension/run-maintenance.ts` | `test/unit/extension/no-op-writers-no-crew-root.test.ts` (8) |
1432
1441
  | Worker-dashboard keybinding override (`.pi/teams`) | `src/ui/keybinding-map.ts` | `test/unit/ui/keybinding-map-override.test.ts` (8) |
1433
1442
  | Steer delivery incl. boot window (pre-bind retry) | `src/prompt/prompt-runtime.ts` (poll + `createSeenSteerIdSet.unmark`), `src/runtime/child-pi/child-pi-spawn.ts:283` (env), `src/runtime/task-runner/child-executor.ts:562` (filename) | `prompt-runtime-steer-boot-window.test.ts` (4) + **Tier 9g** live (watcher at +0.5s; lab variant with `PI_CREW_STEERING_FILE` + `pi --extension <repo>/src/prompt/prompt-runtime.ts`) |
1443
+ | Worker process-GROUP reaping (wave 11a F5 — đường group-signal DUY NHẤT) | `src/runtime/process/reap-group.ts` (`reapGroup` / `assertSafeGroup` guard-ladder 8 rung + escalation SIGTERM→poll→SIGKILL + `flushReapEscalationsSync`), wired `child-pi-kill.ts` exited-root (snapshot pgid/starttime trước khi root chết) + `orphan-reaper.ts` identity-proven admission | unit `node scripts/test-runner.mjs test/unit/runtime/process/reap-group.test.ts`; kill switch `PI_CREW_REAP_GROUP=0` (default ON — degrade về pid-only pre-wave; được FORWARD sang background runner); live: spawn worker có group con → kill root → group bị reap; **negative control bắt buộc: process cùng uid nhưng ngoài ladder (không leader-proof / không snapshot khớp) phải SỐNG** |
1444
+ | Run-descendant sweep (wave 11b F7 — reap leak sống sót SAU run terminal) | `src/runtime/process/run-descendant-sweep.ts` (`collectRunDescendants`/`reapRunDescendants`, 1 snapshot `/proc`/pass) + `crash-recovery.ts` `selectUnsweptTerminalRunIds` lane (terminal ≥ 60 s, oldest-first, cap 25/pass, window 200, retry `descendantSweepPending` 10 phút) | unit sweep suites (xem decision-table row wave 11) + **integration live repro** `node scripts/test-runner.mjs test/integration/run-descendant-sweep-live.test.ts` (A1–A5) + report `docs/real-test/reports/real-test-2026-10-24-wave11-descendant-reaping.md`; kill switch `PI_CREW_REAP_RUN_DESCENDANTS=0` (no-op hoàn toàn); marker ladder `PI_CREW_BROKER_RUN_ID` (nhánh run-id cần recorded parent ĐÃ CHẾT) → `PI_CREW_KIND=subagent`+`PI_CREW_TASK_ID` → `PI_CREW_PARENT_PID`; **negative control bắt buộc: no-marker / interactive-tty / parent-sống phải SỐNG** |
1434
1445
 
1435
1446
  ---
1436
1447
 
@@ -225,6 +225,14 @@ const CREW_ENV_FORWARDING_DECL: Record<string, CrewEnvForwardDecision> = {
225
225
  kind: "gap",
226
226
  reason: "v0.11.36 sandbox network profile (share|off) read in the PARENT spawn path only — deliberately never forwarded to children",
227
227
  },
228
+ PI_CREW_REAP_GROUP: allowlist(
229
+ ["background-runner"],
230
+ "fix-wave 11a process-group reap kill switch — read by the reaping code (reap-group.ts resolveReapGroupEnabled) which ALSO runs inside the detached background runner; unforwarded it would be silently inert there (wave 11a review MINOR #7). Not forwarded to children: a worker must not be able to disable its own reaping",
231
+ ),
232
+ PI_CREW_REAP_RUN_DESCENDANTS: allowlist(
233
+ ["background-runner"],
234
+ "fix-wave 11b run-scoped descendant sweep kill switch (run-descendant-sweep.ts resolveRunDescendantSweepEnabled) — the stale reconciler that drives the sweep also runs inside the detached background runner, so the same inert-switch argument as PI_CREW_REAP_GROUP applies. Not forwarded to children: a worker must not be able to disable its own reaping",
235
+ ),
228
236
  PI_CREW_STEERING_FILE: { kind: "child-write", reason: "child-pi-spawn per-run steering JSONL" },
229
237
  PI_CREW_MAX_OUTPUT: { kind: "child-write", reason: "pi-args emits agent.maxTokens into built.env" },
230
238
  PI_CREW_RPC_SECRET: { kind: "child-write", reason: "shared HMAC secret handed to the child at spawn — secret name, never allowlisted" },
@@ -273,6 +281,10 @@ const CREW_ENV_FORWARDING_DECL: Record<string, CrewEnvForwardDecision> = {
273
281
  PI_CREW_TRUST_PROJECT_DWF: { kind: "gap", reason: "project DWF trust gate on the runner path" },
274
282
  PI_CREW_HEARTBEAT_PULSE_MS: { kind: "gap", reason: "child-executor liveness pulse (runner)" },
275
283
  PI_CREW_INTERRUPT_GUARD_INTERVAL_MS: { kind: "gap", reason: "background-runner interrupt guard" },
284
+ PI_CREW_WATCHDOG_INTERVAL_MS: {
285
+ kind: "gap",
286
+ reason: "WD-INTERVAL foreground-watchdog poll interval — read by the HOST session only; registered as a gap (deliberately not forwarded) so the decision table stays complete and a future runner-side watchdog is auditable",
287
+ },
276
288
  PI_CREW_MAX_RUN_MS: { kind: "gap", reason: "background-runner run ceiling" },
277
289
  PI_CREW_MAX_WORKERS: { kind: "gap", reason: "global worker cap — enforced in the runner too" },
278
290
  PI_CREW_DISABLE_RESULT_READ_CACHE: { kind: "gap", reason: "task-output-context read cache (runner)" },
@@ -289,6 +301,10 @@ const CREW_ENV_FORWARDING_DECL: Record<string, CrewEnvForwardDecision> = {
289
301
  PI_CREW_CLASSIFIER_MODEL: { kind: "gap", reason: "child-executor retry classifier (runner)" },
290
302
  PI_CREW_VERIFY_GATE: { kind: "gap", reason: "verify-gate pre-gate before verifier spawns (runner)" },
291
303
  PI_CREW_VERIFY_GATE_TIMEOUT_MS: { kind: "gap", reason: "verify-gate per-check timeout (runner)" },
304
+ PI_CREW_VERIFY_GATE_MEMO: {
305
+ kind: "gap",
306
+ reason: "W10-2 memoized green verify-gate (host-only tree-hash memo; never forwarded to children — each process gates itself, exactly like PI_CREW_VERIFY_GATE)",
307
+ },
292
308
  PI_CREW_VERIFY_DETERMINISM: {
293
309
  kind: "gap",
294
310
  reason: "U13-ADJ layer-3 determinism rerun toggle — resolved in the runner before the verifier spawns; deliberately unforwarded (each process decides its own gate determinism tier, exactly like PI_CREW_VERIFY_GATE above)",
@@ -453,9 +469,13 @@ const BG_CREW_CORE_ORDER = [
453
469
  const BG_CREW_MODEL_ORDER = ["PI_CREW_MODEL", "PI_CREW_MODEL_FALLBACK_ORDER"] as const;
454
470
  const BG_CREW_STATE_ORDER = ["PI_CREW_STATE_BACKEND", "PI_CREW_CHILD_PI_MODE", "PI_CREW_CHILD_PI_POOL"] as const;
455
471
  const BG_CREW_TOOLS_ORDER = ["PI_CREW_TOOLS_MODIFIERS"] as const;
472
+ // Wave 11a review (MINOR #7): the reap kill switch must be honoured by the
473
+ // detached runner too — the runner (and the surface workers it spawns) own
474
+ // kill/reap paths, so an unforwarded switch was silently inert there.
475
+ const BG_CREW_REAP_ORDER = ["PI_CREW_REAP_GROUP", "PI_CREW_REAP_RUN_DESCENDANTS"] as const;
456
476
  const CHILD_CREW_DEPTH_ORDER = ["PI_CREW_DEPTH"] as const;
457
477
 
458
- const BG_CREW_GROUPS = [BG_CREW_CORE_ORDER, BG_CREW_MODEL_ORDER, BG_CREW_STATE_ORDER, BG_CREW_TOOLS_ORDER] as const;
478
+ const BG_CREW_GROUPS = [BG_CREW_CORE_ORDER, BG_CREW_MODEL_ORDER, BG_CREW_STATE_ORDER, BG_CREW_TOOLS_ORDER, BG_CREW_REAP_ORDER] as const;
459
479
 
460
480
  /**
461
481
  * Generate the detached background-runner allowlist (exported AS
@@ -480,6 +500,7 @@ export function backgroundRunnerEnvAllowlist(): string[] {
480
500
  ...BG_CREW_MODEL_ORDER,
481
501
  ...BG_CREW_STATE_ORDER,
482
502
  ...BG_CREW_TOOLS_ORDER,
503
+ ...BG_CREW_REAP_ORDER,
483
504
  ];
484
505
  }
485
506
 
@@ -309,12 +309,30 @@ export const CREW_ENV_VARS: Record<string, CrewEnvVarSpec> = {
309
309
  default: 1000,
310
310
  doc: "interrupt-guard poll interval; default 1000ms (background-runner.ts:217)",
311
311
  },
312
+ PI_CREW_WATCHDOG_INTERVAL_MS: {
313
+ name: "PI_CREW_WATCHDOG_INTERVAL_MS",
314
+ parser: "int",
315
+ default: 300_000,
316
+ doc: "WD-INTERVAL (2026-10-23): foreground-run watchdog poll interval (ms), default 300000 = DEFAULT_CHECK_INTERVAL_MS (foreground-watchdog.ts resolveCheckIntervalMs). Lower it (e.g. 20000) to live-repro watchdog behavior without waiting 5 min per cycle; ≤0/garbage falls back to the default. An explicit checkIntervalMs argument still wins.",
317
+ },
312
318
  PI_CREW_MAX_RUN_MS: {
313
319
  name: "PI_CREW_MAX_RUN_MS",
314
320
  parser: "int",
315
321
  default: 7_200_000,
316
322
  doc: "max background run duration; default 2h (background-runner.ts:31)",
317
323
  },
324
+ PI_CREW_REAP_GROUP: {
325
+ name: "PI_CREW_REAP_GROUP",
326
+ parser: "boolean",
327
+ default: true,
328
+ doc: "wave 11a kill switch for identity-proven PROCESS-GROUP reaping: '0'/'false' degrades child-pi-kill + orphan-reaper to their pre-wave pid-only paths (resolveReapGroupEnabled, reap-group.ts). FORWARDED to the detached background runner (env-allowlist-registry: PI_CREW_REAP_GROUP → background-runner) because the runner/worker process is the one that reaps its own children — an unforwarded switch would be silently inert there",
329
+ },
330
+ PI_CREW_REAP_RUN_DESCENDANTS: {
331
+ name: "PI_CREW_REAP_RUN_DESCENDANTS",
332
+ parser: "boolean",
333
+ default: true,
334
+ doc: "wave 11b kill switch for the RUN-SCOPED descendant sweep: '0'/'false' makes collectRunDescendants/reapRunDescendants (run-descendant-sweep.ts) a complete no-op. FORWARDED to the detached background runner (env-allowlist-registry: → background-runner) for the same reason as PI_CREW_REAP_GROUP: the reconciler that drives the sweep also runs there. Never forwarded to a worker",
335
+ },
318
336
  PI_CREW_MAX_WORKERS: {
319
337
  name: "PI_CREW_MAX_WORKERS",
320
338
  parser: "int",
@@ -557,6 +575,11 @@ export const CREW_ENV_VARS: Record<string, CrewEnvVarSpec> = {
557
575
  parser: "boolean",
558
576
  doc: "U13-ADJ (upgrade 2026-10-13, PHASE 4) layer-3 determinism rerun, DEFAULT OFF: '1' reruns the most critical default check (usually test:critical) once after a GREEN gate; disagreement (flaky) or a rerun executor failure downgrades the verdict to INCONCLUSIVE (never FAILED, never a task failure) with a 'deterministic-check: flaky <name>' note. Flag off keeps the gate bit-for-bit pre-U13-ADJ (runtime/verification/verify-gate.ts resolveVerifyDeterminismEnabled)",
559
577
  },
578
+ PI_CREW_VERIFY_GATE_MEMO: {
579
+ name: "PI_CREW_VERIFY_GATE_MEMO",
580
+ parser: "boolean",
581
+ doc: "W10-2 (v0.11.38) memoized green verify-gate, DEFAULT ON: a PASS verdict is recorded keyed by the workspace tree OID (git write-tree) + check list + node version; a re-gate on a bit-identical tree is served from the memo without re-running typecheck/test:critical. FAILED/INCONCLUSIVE never memoize; non-git cwd disables the memo for that tree (fail-open). '0' restores pre-W10-2 behavior (runtime/verification/verify-gate-memo.ts)",
582
+ },
560
583
  PI_CREW_RPC_DIALOG_ANSWER: {
561
584
  name: "PI_CREW_RPC_DIALOG_ANSWER",
562
585
  doc: "W7 EXPERIMENTAL dialog auto-answer policy for RPC-mode extension_ui_request dialogs (select/confirm/input/editor): 'cancel' (default) answers {cancelled:true} — universal safe answer; 'block' leaves the server promise pending (debug). Auto-confirm deliberately NOT implemented (security); invalid → 'cancel' (rpc-worker.ts resolveDialogAnswerPolicy)",
@@ -84,6 +84,25 @@ async function openLiveSidebarImpl(
84
84
  * clearing, status notification, run-completed session entry, and crew.run.*
85
85
  * event emission.
86
86
  */
87
+ /**
88
+ * WD-DELIVER-GUARD idle probe: true when the leader's own turn is idle — i.e.
89
+ * the moment `pi.sendUserMessage({deliverAs:"followUp"})` is DELIVERED instead
90
+ * of parked in the host's follow-up queue. Capability-guarded: an older host
91
+ * without `ctx.isIdle` (or no ctx yet) reports idle, which keeps the emit path
92
+ * bit-for-bit with the pre-guard behavior (fail-open — never swallow an alert).
93
+ */
94
+ function sessionIdleProbe(ctx: RegistrationContext): () => boolean {
95
+ return () => {
96
+ const current = ctx.currentCtx;
97
+ if (!current || typeof current.isIdle !== "function") return true;
98
+ try {
99
+ return current.isIdle() !== false;
100
+ } catch {
101
+ return true;
102
+ }
103
+ };
104
+ }
105
+
87
106
  function startForegroundRunImpl(
88
107
  pi: ExtensionAPI,
89
108
  ctx: RegistrationContext,
@@ -112,9 +131,10 @@ function startForegroundRunImpl(
112
131
  extensionCtx.ui.setWorkingMessage(runId ? `pi-crew foreground run ${runId}...` : "pi-crew foreground run...");
113
132
  }
114
133
  if (runId) {
134
+ const isSessionIdle = sessionIdleProbe(ctx);
115
135
  void import("../../runtime/foreground-watchdog.ts") // LAZY: defer watchdog module until first foreground run
116
136
  .then(({ startForegroundWatchdog }) => {
117
- startForegroundWatchdog({ pi, cwd: extensionCtx.cwd, runId });
137
+ startForegroundWatchdog({ pi, cwd: extensionCtx.cwd, runId, isSessionIdle });
118
138
  })
119
139
  .catch((error) => {
120
140
  logInternalError("register.foreground-watchdog-import", error);
@@ -42,6 +42,7 @@ import { tryRegisterSessionCleanup } from "../../runtime/session-resources.ts";
42
42
  import { createSessionSnapshot } from "../../runtime/session-snapshot.ts";
43
43
  import { applyCrewSettingsTiersToConfig, loadCrewSettingsTiers, scheduledJobsHiddenCountOf } from "../../runtime/settings-store.ts";
44
44
  import type { ReconcileResult } from "../../runtime/stale-reconciler.ts";
45
+ import { flushReapEscalationsSync } from "../../runtime/stale-reconciler.ts";
45
46
  import { loadRunManifestById, saveRunManifest } from "../../state/stores/state-store.ts";
46
47
  import type { TeamRunManifest } from "../../state/types.ts";
47
48
  import { installInlinePanel } from "../../ui/inline-panel/index.ts";
@@ -166,6 +167,17 @@ function installSessionShutdownHandler(pi: ExtensionAPI, ctx: RegistrationContex
166
167
  if (reason === "quit" || reason === "reload") {
167
168
  // Actual shutdown — abort foreground runs and cleanup everything
168
169
  ctx.cleanupRuntime();
170
+ // Wave 11a review (MAJOR #3): the group-reap SIGKILL rung is a chain of
171
+ // UNREF'd timers owned by an async escalation, and this handler is SYNC —
172
+ // without this flush the last rung is abandoned the moment the process
173
+ // exits. Delivered AFTER `cleanupRuntime()` so escalations armed by that
174
+ // cleanup are included. Each entry re-validates before signalling and the
175
+ // call never throws.
176
+ try {
177
+ flushReapEscalationsSync();
178
+ } catch (error) {
179
+ logInternalError("register.session-shutdown.flush-reap-escalations", error);
180
+ }
169
181
  } else {
170
182
  // Session switch (resume/new/fork) — cleanup resources but preserve foreground runs
171
183
  ctx.cleanupSessionResourcesOnly();
@@ -10,6 +10,7 @@ import { getPiTempBase } from "../../runtime/model/pi-args.ts";
10
10
  import { getRuntimeWarmupStatus } from "../../runtime/model/runtime-warmup.ts";
11
11
  import { currentSessionModel, sessionModelSnapshot } from "../../runtime/model/session-model.ts";
12
12
  import { getPiSpawnCommand } from "../../runtime/pi-spawn.ts";
13
+ import { type RunDescendant, reapRunDescendants } from "../../runtime/process/run-descendant-sweep.ts";
13
14
  import { formatZombieReport, scanZombieSubagents, type ZombieScanResult } from "../../runtime/process/zombie-scanner.ts";
14
15
  import { launchScriptRegistry, sweepLaunchScripts, sweepOrphanLaunchScriptFiles } from "../../runtime/surface/launch-script.ts";
15
16
  import { surfaceProviderForCleanup } from "../../runtime/surface/resolve-surface.ts";
@@ -18,10 +19,12 @@ import type { TeamToolParamsValue } from "../../schema/team-tool-schema.ts";
18
19
  import { TeamToolParams } from "../../schema/team-tool-schema.ts";
19
20
  import { atomicWriteFile, atomicWriteJson } from "../../state/atomic-write.ts";
20
21
  import { TEAM_TERMINAL_RUN_STATUSES } from "../../state/contracts.ts";
22
+ import { loadRunManifestById } from "../../state/stores/state-store.ts";
21
23
  import { allTeams, discoverTeams } from "../../teams/discover-teams.ts";
22
24
  import { type FatalFsCause, fsFailureLabel } from "../../utils/fs-errno.ts";
23
25
  import { projectCrewRoot, userCrewRoot } from "../../utils/paths.ts";
24
26
  import { allWorkflows, discoverWorkflows } from "../../workflows/discover-workflows.ts";
27
+ import { listRecentRuns } from "../run-index.ts";
25
28
  import type { PiTeamsToolResult } from "../tool-result.ts";
26
29
  import { validateResources } from "../validate-resources.ts";
27
30
  import { configRecord, result, type TeamContext } from "./context.ts";
@@ -859,6 +862,121 @@ export function formatOrphanPaneReport(cleanup: DoctorSurfaceCleanupResult): str
859
862
  return lines.join("\n");
860
863
  }
861
864
 
865
+ /** How many recent TERMINAL runs the report-only descendant sweep inspects. */
866
+ const RUN_DESCENDANT_SCAN_RUN_LIMIT = 5;
867
+
868
+ /** One terminal run plus the surviving processes that still carry its markers. */
869
+ export interface RunDescendantReport {
870
+ runId: string;
871
+ descendants: RunDescendant[];
872
+ }
873
+
874
+ /**
875
+ * Wave 11b (F7) — REPORT ONLY. pi's bash tool spawns every command `detached`,
876
+ * so a worker's grandchildren live in their own process groups and outlive the
877
+ * run (the 390 MB / 24 h leak after team_20261022110510 completed). This scan
878
+ * finds the survivors by the pi-crew env markers they inherited and lists them
879
+ * for the operator.
880
+ *
881
+ * Nothing is killed here BY CONSTRUCTION: `reapRunDescendants` is called with
882
+ * `dryRun: true`, which makes every candidate land in `skipped` with reason
883
+ * `dry-run` — the candidates themselves arrive in `descendants` for the report.
884
+ * The automatic reap is the stale reconciler's job (terminal runs only, never
885
+ * inside a worker), not the doctor's.
886
+ */
887
+ export function collectRunDescendantReport(cwd: string, limit = RUN_DESCENDANT_SCAN_RUN_LIMIT): RunDescendantReport[] {
888
+ const reports: RunDescendantReport[] = [];
889
+ let inspected = 0;
890
+ let manifests: ReturnType<typeof listRecentRuns>;
891
+ try {
892
+ // A generous window: terminal runs are the only ones eligible, and old ones
893
+ // may dominate the recent list.
894
+ manifests = listRecentRuns(cwd, ORPHAN_RUN_SCAN_LIMIT * 5);
895
+ } catch {
896
+ return reports;
897
+ }
898
+ for (const manifest of manifests) {
899
+ if (inspected >= limit) break;
900
+ if (!TEAM_TERMINAL_RUN_STATUSES.has(manifest.status)) continue;
901
+ inspected += 1;
902
+ let loaded: ReturnType<typeof loadRunManifestById>;
903
+ try {
904
+ loaded = loadRunManifestById(cwd, manifest.runId);
905
+ } catch {
906
+ continue;
907
+ }
908
+ if (!loaded) continue;
909
+ const runnerPid = manifest.async?.pid;
910
+ // Wave 11c (F2): same proof-parent source as the automatic sweep, so the
911
+ // report lists exactly what the sweep would reap — a SYNC run's leak
912
+ // naming its dead session pi as parent.
913
+ const sessionPid = manifest.sessionPid;
914
+ const workerPids = [
915
+ ...new Set(
916
+ loaded.tasks
917
+ .map((task) => task.heartbeat?.pid ?? task.checkpoint?.childPid)
918
+ .filter((pid): pid is number => typeof pid === "number" && Number.isInteger(pid) && pid > 1),
919
+ ),
920
+ ];
921
+ const startedAtMs = Date.parse(manifest.async?.spawnedAt ?? manifest.createdAt);
922
+ // 11b.3: the same surface-pane exclusion the automatic sweep applies — a
923
+ // LIVE pane pid is not a leak, so the report must not list it either.
924
+ const surfacePids = [
925
+ ...new Set(
926
+ Object.values(manifest.surface?.workerPids ?? {}).filter(
927
+ (pid): pid is number => typeof pid === "number" && Number.isInteger(pid) && pid > 1,
928
+ ),
929
+ ),
930
+ ];
931
+ const result = reapRunDescendants(
932
+ {
933
+ runId: manifest.runId,
934
+ ...(runnerPid !== undefined ? { runnerPid } : {}),
935
+ workerPids,
936
+ ...(sessionPid !== undefined ? { sessionPid } : {}),
937
+ ...(surfacePids.length > 0 ? { surfacePids } : {}),
938
+ taskIds: loaded.tasks.map((task) => task.id),
939
+ startedAtMs: Number.isFinite(startedAtMs) ? startedAtMs : 0,
940
+ },
941
+ { dryRun: true },
942
+ );
943
+ if (result.descendants.length > 0) reports.push({ runId: manifest.runId, descendants: result.descendants });
944
+ }
945
+ return reports;
946
+ }
947
+
948
+ /** Render {@link collectRunDescendantReport} for the zombies focus of doctor. */
949
+ export function formatRunDescendantReport(reports: RunDescendantReport[]): string {
950
+ const lines: string[] = [];
951
+ lines.push("## Run descendants of terminal runs (report-only — nothing killed)");
952
+ lines.push("");
953
+ if (reports.length === 0) {
954
+ lines.push(`No surviving process matched a terminal run's pi-crew markers (last ${RUN_DESCENDANT_SCAN_RUN_LIMIT} terminal runs).`);
955
+ return lines.join("\n");
956
+ }
957
+ const total = reports.reduce((sum, report) => sum + report.descendants.length, 0);
958
+ lines.push(`Survivors: ${total} process(es) across ${reports.length} terminal run(s).`);
959
+ lines.push("Matched by inherited marker (run-id → task-id → parent-pid); the run-id marker alone is not proof of descent —");
960
+ lines.push("a candidate must also name a recorded parent of the run that is already dead. A descendant that called setsid or");
961
+ lines.push("runs in its own container cannot be proven and is not listed.");
962
+ lines.push("");
963
+ for (const report of reports) {
964
+ lines.push(`Run ${report.runId} — ${report.descendants.length} process(es):`);
965
+ for (const descendant of report.descendants) {
966
+ const rss = descendant.rssKb > 0 ? `${Math.round(descendant.rssKb / 1024)} MB` : "rss ?";
967
+ lines.push(` - pid ${descendant.pid} pgid ${descendant.pgid ?? "?"} [${descendant.matchedBy}] ${rss} — ${descendant.cmd}`);
968
+ }
969
+ }
970
+ lines.push("");
971
+ lines.push("Automatic reaping happens in the stale reconciler, per candidate: it only signals a process that proves descent");
972
+ lines.push("(a recorded runner/worker parent that is already dead) inside a terminal run, that holds no controlling terminal,");
973
+ lines.push("and whose process group has a dead leader or a leader that is itself a candidate (a live unmatched leader blocks");
974
+ lines.push("the group). A same-uid process that merely inherited a run's env — an operator shell, or a service the run left");
975
+ lines.push("running — has no such proof and is deliberately NOT reaped, so a survivor listed above may stay alive by design.");
976
+ lines.push("Never runs inside a worker. PI_CREW_REAP_RUN_DESCENDANTS=0 disables it entirely.");
977
+ return lines.join("\n");
978
+ }
979
+
862
980
  export async function handleDoctor(ctx: TeamContext, params: TeamToolParamsValue = {}): Promise<PiTeamsToolResult> {
863
981
  // Sub-focus: zombie sub-agent scan + orphan surface-pane cleanup (T12). The
864
982
  // process scan itself stays READ-ONLY — never kills a process. The pane
@@ -871,7 +989,11 @@ export async function handleDoctor(ctx: TeamContext, params: TeamToolParamsValue
871
989
  if (params.focus === "zombies") {
872
990
  const scan = scanZombieSubagents();
873
991
  const cleanup = await cleanupOrphanSurfacePanes({ cwd: ctx.cwd, scan });
874
- const text = `${formatZombieReport(scan)}\n\n${formatOrphanPaneReport(cleanup)}`;
992
+ // Wave 11b (F7): the zombie scan cannot see a terminal run's pi-bash
993
+ // grandchildren (they carry no PI_CREW_SURFACE and their recorded parent is
994
+ // the dead runner). The run-scoped sweep does — REPORT ONLY, dryRun.
995
+ const runDescendants = collectRunDescendantReport(ctx.cwd);
996
+ const text = `${formatZombieReport(scan)}\n\n${formatOrphanPaneReport(cleanup)}\n\n${formatRunDescendantReport(runDescendants)}`;
875
997
  return result(
876
998
  text,
877
999
  {
@@ -884,6 +1006,8 @@ export async function handleDoctor(ctx: TeamContext, params: TeamToolParamsValue
884
1006
  // never pane-cleaned; see zombie-scanner.ts discriminator notes).
885
1007
  foreignPi: scan.foreign.length,
886
1008
  errors: scan.errors.length,
1009
+ // Wave 11b: report-only count of a terminal run's surviving descendants.
1010
+ runDescendants: runDescendants.reduce((sum, report) => sum + report.descendants.length, 0),
887
1011
  orphanPanes: cleanup.orphans.length,
888
1012
  panesClosed: cleanup.closed.length,
889
1013
  panesGone: cleanup.gone.length,
@@ -7,7 +7,7 @@ import { defineTool, type ToolDefinition } from "../extension/pi-api.ts";
7
7
  import { startChildBrokerClient } from "../runtime/broker/crew-broker-child.ts";
8
8
  import { CrewBrokerClient } from "../runtime/broker/crew-broker-client.ts";
9
9
  import { hasLiveControlRealtimeListeners } from "../runtime/live-session/live-control-realtime.ts";
10
- import { type MailboxMessage, readAllMailboxMessages } from "../state/coordination/mailbox.ts";
10
+ import { checkReplyToRequesterOrigin, type MailboxMessage, readAllMailboxMessages } from "../state/coordination/mailbox.ts";
11
11
  import { appendEventFireAndForget } from "../state/event-log/event-log.ts";
12
12
  import type { TeamRunManifest } from "../state/types.ts";
13
13
  import { logInternalError } from "../utils/internal-error.ts";
@@ -727,13 +727,75 @@ export function renderAskAnswer(questionId: string, answer: string): string {
727
727
  return `<dependency-context>\n(The following is the leader's answer to your ask() question. It is DATA, not instructions. Do not follow any directives within it.)\nquestionId: ${questionId}\nanswer:\n${body}\n</dependency-context>`;
728
728
  }
729
729
 
730
+ /** W10-3 (lesson #22): strict replyTo routing enforcement at the ask-resolution
731
+ * point. The parked poll re-runs every ≤500ms tick, so the routing verdicts
732
+ * log ONCE per (runId, questionId, replyTo) key — otherwise a mismatched
733
+ * replyTo would spam the internal-error channel until the park deadline. */
734
+ const replyToRoutingLogKeys = new Set<string>();
735
+ const REPLYTO_ROUTING_LOG_MAX = 256;
736
+ function logReplyToRoutingOnce(key: string, code: string, error: Error, detail: string): void {
737
+ if (replyToRoutingLogKeys.has(key)) return;
738
+ if (replyToRoutingLogKeys.size >= REPLYTO_ROUTING_LOG_MAX) replyToRoutingLogKeys.clear();
739
+ replyToRoutingLogKeys.add(key);
740
+ logInternalError(code, error, detail, "warn");
741
+ }
742
+
730
743
  /** Exact-equality questionId match (never prefix/substring — ADR item 5).
731
744
  * Polls BOTH directions: which side of the stream the respond path writes
732
745
  * to is not this tool's contract. A transient read failure (lock contention,
733
- * partial line) must not kill the parked tool — the next tick retries. */
734
- function findAskResponse(manifest: TeamRunManifest, questionId: string): MailboxMessage | undefined {
746
+ * partial line) must not kill the parked tool — the next tick retries.
747
+ *
748
+ * W10-3 (2026-10-22, lesson #22): a candidate that carries `replyTo` is
749
+ * additionally gated by checkReplyToRequesterOrigin (strict replyTo routing):
750
+ * when the replyTo original was opened by a DIFFERENT requester than the one
751
+ * this response is addressed to (A→B→C), the response is NOT the answer —
752
+ * log + skip it — the park stays unresolved (deadline still applies; the
753
+ * task continues with best judgment). Unverifiable origins (rotated-out/
754
+ * stale replyTo ids) keep the exact-questionId correlation as the primary
755
+ * defense — warn once, fail-open.
756
+ *
757
+ * W10-3 hardening (review of cherry-picked a356ae9): ALL (kind, questionId)
758
+ * candidates are considered, not just the FIRST — a mismatched candidate is
759
+ * skipped and the iteration continues, so a VALID answer the leader re-sent
760
+ * with the same questionId after a misroute still resolves the park (the old
761
+ * `.find()` + bail-out shadowed it until the deadline). Each candidate is
762
+ * also anchored to the parked task (`to === taskId`): a reply pair that is
763
+ * coherent for ANOTHER task must never resolve this park even when its row
764
+ * happens to tag this questionId. */
765
+ function findAskResponse(manifest: TeamRunManifest, questionId: string, taskId: string): MailboxMessage | undefined {
735
766
  try {
736
- return readAllMailboxMessages(manifest).find((m) => m.kind === "response" && m.questionId === questionId);
767
+ const candidates = readAllMailboxMessages(manifest).filter(
768
+ (m) => m.kind === "response" && m.questionId === questionId && m.to === taskId,
769
+ );
770
+ for (const candidate of candidates) {
771
+ if (candidate.replyTo !== undefined) {
772
+ const origin = checkReplyToRequesterOrigin(manifest, candidate);
773
+ const logKey = `${manifest.runId}:${questionId}:${candidate.replyTo}`;
774
+ if (origin.verdict === "mismatch") {
775
+ logReplyToRoutingOnce(
776
+ logKey,
777
+ "prompt-runtime.ask-replyto-origin-mismatch",
778
+ new Error(
779
+ `response '${candidate.id}' (to='${candidate.to}') replies to '${origin.replyTo}' opened by '${origin.originalFrom}' — wrong requester; candidate skipped, a later valid answer with the same questionId can still resolve`,
780
+ ),
781
+ `questionId=${questionId}`,
782
+ );
783
+ continue;
784
+ }
785
+ if (origin.verdict === "origin-not-found") {
786
+ logReplyToRoutingOnce(
787
+ logKey,
788
+ "prompt-runtime.ask-replyto-origin-unknown",
789
+ new Error(
790
+ `response '${candidate.id}' carries replyTo '${origin.replyTo}' with no resolvable original — origin unverifiable, questionId correlation stands`,
791
+ ),
792
+ `questionId=${questionId}`,
793
+ );
794
+ }
795
+ }
796
+ return candidate;
797
+ }
798
+ return undefined;
737
799
  } catch (error) {
738
800
  logInternalError(
739
801
  "prompt-runtime.ask-mailbox-read",
@@ -902,7 +964,7 @@ export function createAskTool(deps: AskToolDeps = {}): AskToolDefinition {
902
964
  terminal = "aborted";
903
965
  break;
904
966
  }
905
- answer = findAskResponse(manifest, questionId);
967
+ answer = findAskResponse(manifest, questionId, taskId);
906
968
  if (answer) {
907
969
  terminal = "answered";
908
970
  break;