pi-crew 0.9.16 → 0.9.18

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 (435) hide show
  1. package/CHANGELOG.md +130 -0
  2. package/README.md +19 -0
  3. package/dist/build-meta.json +21784 -0
  4. package/dist/index.mjs +83169 -0
  5. package/dist/index.mjs.map +7 -0
  6. package/docs/A +358 -0
  7. package/docs/M-A +357 -0
  8. package/docs/REVIEW-FINDINGS-2026-06 +357 -0
  9. package/docs/Y +357 -0
  10. package/docs/a +357 -0
  11. package/docs/aA +358 -0
  12. package/docs/archive/README.md +91 -0
  13. package/docs/migration/atomic-write-v2-migration.md +297 -0
  14. package/docs/patterns/command-agent-skill.md +1 -1
  15. package/docs/perf/performance-review-2026-07.md +287 -0
  16. package/docs/skills/REFERENCE.md +0 -17
  17. package/docs//303/242mmaAAA/303/242 +357 -0
  18. package/index.bundle.ts +25 -0
  19. package/index.ts +95 -4
  20. package/package.json +15 -4
  21. package/skills/iterative-audit/SKILL.md +0 -1
  22. package/skills/widget-rendering/SKILL.md +17 -0
  23. package/src/adapters/claude-adapter.ts +1 -3
  24. package/src/adapters/export-util.ts +16 -16
  25. package/src/adapters/index.ts +5 -5
  26. package/src/agents/agent-config.ts +20 -14
  27. package/src/agents/agent-serializer.ts +1 -1
  28. package/src/agents/discover-agents.ts +83 -39
  29. package/src/benchmark/benchmark-runner.ts +51 -51
  30. package/src/benchmark/feedback-loop.ts +1 -1
  31. package/src/config/config.ts +303 -591
  32. package/src/config/defaults.ts +28 -2
  33. package/src/config/drift-detector.ts +11 -14
  34. package/src/config/markers.ts +201 -203
  35. package/src/config/resilient-parser.ts +14 -6
  36. package/src/config/role-tools.ts +3 -3
  37. package/src/config/suggestions.ts +5 -12
  38. package/src/config/types.ts +9 -25
  39. package/src/errors.ts +151 -157
  40. package/src/extension/action-suggestions.ts +54 -9
  41. package/src/extension/async-notifier.ts +43 -13
  42. package/src/extension/autonomous-policy.ts +77 -37
  43. package/src/extension/command-completions.ts +9 -8
  44. package/src/extension/context-status-injection.ts +14 -12
  45. package/src/extension/crew-autocomplete.ts +8 -16
  46. package/src/extension/crew-cleanup.ts +2 -1
  47. package/src/extension/crew-shortcuts.ts +9 -6
  48. package/src/extension/cross-extension-rpc.ts +136 -42
  49. package/src/extension/help.ts +1 -1
  50. package/src/extension/import-index.ts +10 -5
  51. package/src/extension/knowledge-injection.ts +88 -15
  52. package/src/extension/management.ts +89 -349
  53. package/src/extension/message-renderers.ts +3 -4
  54. package/src/extension/notification-router.ts +22 -5
  55. package/src/extension/notification-sink.ts +6 -3
  56. package/src/extension/pi-api.ts +17 -8
  57. package/src/extension/plan-orchestrate.ts +8 -27
  58. package/src/extension/project-init.ts +21 -6
  59. package/src/extension/register.ts +329 -975
  60. package/src/extension/registration/artifact-cleanup.ts +9 -4
  61. package/src/extension/registration/command-utils.ts +5 -1
  62. package/src/extension/registration/commands.ts +806 -314
  63. package/src/extension/registration/compaction-guard.ts +15 -15
  64. package/src/extension/registration/lifecycle.ts +259 -0
  65. package/src/extension/registration/observability.ts +293 -0
  66. package/src/extension/registration/subagent-helpers.ts +29 -7
  67. package/src/extension/registration/subagent-tools.ts +253 -44
  68. package/src/extension/registration/team-tool.ts +19 -84
  69. package/src/extension/registration/ui.ts +168 -0
  70. package/src/extension/registration/viewers.ts +64 -24
  71. package/src/extension/result-watcher.ts +18 -7
  72. package/src/extension/run-bundle-schema.ts +21 -6
  73. package/src/extension/run-export.ts +15 -6
  74. package/src/extension/run-import.ts +64 -29
  75. package/src/extension/run-index.ts +28 -9
  76. package/src/extension/run-maintenance.ts +32 -12
  77. package/src/extension/session-summary.ts +1 -1
  78. package/src/extension/team-manager-command.ts +64 -7
  79. package/src/extension/team-onboard.ts +149 -150
  80. package/src/extension/team-recommendation.ts +121 -21
  81. package/src/extension/team-tool/anchor.ts +31 -64
  82. package/src/extension/team-tool/api.ts +929 -141
  83. package/src/extension/team-tool/auto-summarize.ts +14 -26
  84. package/src/extension/team-tool/cache-control.ts +1 -5
  85. package/src/extension/team-tool/cancel.ts +138 -38
  86. package/src/extension/team-tool/chain-dispatch.ts +5 -13
  87. package/src/extension/team-tool/chain-executor.ts +29 -51
  88. package/src/extension/team-tool/config-patch.ts +1 -1
  89. package/src/extension/team-tool/context.ts +40 -20
  90. package/src/extension/team-tool/destructive-gate.ts +10 -5
  91. package/src/extension/team-tool/doctor.ts +170 -51
  92. package/src/extension/team-tool/explain.ts +228 -214
  93. package/src/extension/team-tool/failure-patterns.ts +3 -9
  94. package/src/extension/team-tool/goal-wrap.ts +71 -29
  95. package/src/extension/team-tool/goal.ts +185 -35
  96. package/src/extension/team-tool/handle-schedule.ts +34 -15
  97. package/src/extension/team-tool/handle-settings.ts +94 -56
  98. package/src/extension/team-tool/health-monitor.ts +27 -108
  99. package/src/extension/team-tool/inspect.ts +42 -9
  100. package/src/extension/team-tool/intent-policy.ts +4 -1
  101. package/src/extension/team-tool/lifecycle-actions.ts +244 -48
  102. package/src/extension/team-tool/orchestrate.ts +7 -18
  103. package/src/extension/team-tool/parallel-dispatch.ts +51 -29
  104. package/src/extension/team-tool/plan.ts +29 -5
  105. package/src/extension/team-tool/respond.ts +49 -16
  106. package/src/extension/team-tool/run-not-found.ts +3 -8
  107. package/src/extension/team-tool/run.ts +80 -317
  108. package/src/extension/team-tool/status.ts +135 -43
  109. package/src/extension/team-tool/workflow-manage.ts +92 -25
  110. package/src/extension/team-tool-types.ts +8 -1
  111. package/src/extension/team-tool.ts +142 -551
  112. package/src/extension/validate-resources.ts +39 -8
  113. package/src/hooks/registry.ts +66 -17
  114. package/src/hooks/types.ts +1 -1
  115. package/src/i18n.ts +25 -11
  116. package/src/observability/correlation.ts +17 -3
  117. package/src/observability/event-bus.ts +51 -56
  118. package/src/observability/event-to-metric.ts +117 -15
  119. package/src/observability/exporters/otlp-exporter.ts +56 -17
  120. package/src/observability/exporters/prometheus-exporter.ts +1 -1
  121. package/src/observability/metric-registry.ts +16 -4
  122. package/src/observability/metric-retention.ts +8 -2
  123. package/src/observability/metric-sink.ts +11 -3
  124. package/src/observability/metrics-primitives.ts +42 -9
  125. package/src/plugins/plugin-define.ts +2 -2
  126. package/src/plugins/plugin-registry.ts +25 -25
  127. package/src/plugins/plugins/index.ts +1 -1
  128. package/src/plugins/plugins/nextjs.ts +16 -16
  129. package/src/plugins/plugins/vite.ts +7 -11
  130. package/src/plugins/plugins/vitest.ts +6 -11
  131. package/src/runtime/adaptive-plan.ts +225 -40
  132. package/src/runtime/agent-control.ts +58 -15
  133. package/src/runtime/agent-memory.ts +15 -9
  134. package/src/runtime/agent-observability.ts +10 -2
  135. package/src/runtime/anchor-manager.ts +27 -25
  136. package/src/runtime/async-marker.ts +7 -1
  137. package/src/runtime/async-runner.ts +39 -24
  138. package/src/runtime/auto-summarize.ts +7 -13
  139. package/src/runtime/background-runner.ts +189 -251
  140. package/src/runtime/batch-barrier.ts +18 -16
  141. package/src/runtime/cancellation-token.ts +16 -6
  142. package/src/runtime/cancellation.ts +46 -8
  143. package/src/runtime/capability-inventory.ts +6 -4
  144. package/src/runtime/chain-parser.ts +44 -11
  145. package/src/runtime/chain-runner.ts +63 -69
  146. package/src/runtime/checkpoint.ts +12 -56
  147. package/src/runtime/child-pi.ts +344 -79
  148. package/src/runtime/coalesce-tasks.ts +268 -0
  149. package/src/runtime/code-summary.ts +71 -26
  150. package/src/runtime/compact-stages/index.ts +15 -5
  151. package/src/runtime/compact-stages/tail-capture-stage.ts +7 -2
  152. package/src/runtime/compact-stages/truncation-stage.ts +4 -1
  153. package/src/runtime/compaction-summary.ts +17 -10
  154. package/src/runtime/completion-guard.ts +33 -16
  155. package/src/runtime/crash-classification.ts +28 -7
  156. package/src/runtime/crash-recovery.ts +177 -37
  157. package/src/runtime/crew-agent-records.ts +145 -47
  158. package/src/runtime/crew-hooks.ts +9 -24
  159. package/src/runtime/cross-extension-rpc.ts +25 -23
  160. package/src/runtime/custom-tools/irc-tool.ts +43 -15
  161. package/src/runtime/custom-tools/submit-result-tool.ts +16 -5
  162. package/src/runtime/delivery-coordinator.ts +34 -7
  163. package/src/runtime/delta-conflict.ts +9 -21
  164. package/src/runtime/deterministic-ast.ts +1 -1
  165. package/src/runtime/diagnostic-export.ts +53 -20
  166. package/src/runtime/direct-run.ts +12 -2
  167. package/src/runtime/dwf-state-store.ts +1 -1
  168. package/src/runtime/dynamic-workflow-context.ts +187 -59
  169. package/src/runtime/dynamic-workflow-runner.ts +42 -20
  170. package/src/runtime/effectiveness.ts +16 -8
  171. package/src/runtime/errors/crew-errors.ts +7 -11
  172. package/src/runtime/event-stream-bridge.ts +9 -3
  173. package/src/runtime/foreground-control.ts +54 -14
  174. package/src/runtime/foreground-watchdog.ts +9 -6
  175. package/src/runtime/goal-achievement.ts +27 -10
  176. package/src/runtime/goal-evaluator.ts +54 -19
  177. package/src/runtime/goal-loop-runner.ts +280 -99
  178. package/src/runtime/goal-state-store.ts +27 -17
  179. package/src/runtime/green-contract.ts +11 -2
  180. package/src/runtime/group-join.ts +64 -31
  181. package/src/runtime/handoff-manager.ts +37 -42
  182. package/src/runtime/heartbeat-gradient.ts +10 -2
  183. package/src/runtime/heartbeat-watcher.ts +32 -6
  184. package/src/runtime/hidden-handoff.ts +13 -31
  185. package/src/runtime/important-line-classifier.ts +12 -2
  186. package/src/runtime/iteration-hooks.ts +25 -15
  187. package/src/runtime/live-agent-control.ts +55 -7
  188. package/src/runtime/live-agent-manager.ts +130 -27
  189. package/src/runtime/live-control-realtime.ts +23 -3
  190. package/src/runtime/live-irc.ts +4 -1
  191. package/src/runtime/live-session-health.ts +12 -4
  192. package/src/runtime/live-session-runtime.ts +379 -117
  193. package/src/runtime/manifest-cache.ts +28 -12
  194. package/src/runtime/mcp-proxy.ts +10 -18
  195. package/src/runtime/metric-parser.ts +1 -5
  196. package/src/runtime/model-fallback.ts +55 -18
  197. package/src/runtime/model-resolver.ts +2 -6
  198. package/src/runtime/model-scope.ts +19 -3
  199. package/src/runtime/notebook-helpers.ts +60 -62
  200. package/src/runtime/orphan-worker-registry.ts +37 -26
  201. package/src/runtime/output-validator.ts +18 -3
  202. package/src/runtime/overflow-recovery.ts +5 -4
  203. package/src/runtime/parallel-research.ts +29 -5
  204. package/src/runtime/parallel-utils.ts +9 -11
  205. package/src/runtime/path-overlap.ts +150 -0
  206. package/src/runtime/peer-dep.ts +12 -17
  207. package/src/runtime/per-write-validator.ts +1 -3
  208. package/src/runtime/phase-tracker.ts +342 -330
  209. package/src/runtime/pi-args.ts +26 -8
  210. package/src/runtime/pi-json-output.ts +1 -1
  211. package/src/runtime/pi-spawn.ts +30 -19
  212. package/src/runtime/pipeline-runner.ts +56 -55
  213. package/src/runtime/plan-templates.ts +5 -6
  214. package/src/runtime/policy-engine.ts +43 -7
  215. package/src/runtime/post-checks.ts +12 -4
  216. package/src/runtime/post-exit-stdio-guard.ts +2 -2
  217. package/src/runtime/process-lifecycle.ts +20 -10
  218. package/src/runtime/process-status.ts +16 -5
  219. package/src/runtime/progress-event-coalescer.ts +2 -1
  220. package/src/runtime/progress-tracker.ts +103 -103
  221. package/src/runtime/prose-compressor.ts +9 -11
  222. package/src/runtime/recovery-recipes.ts +118 -24
  223. package/src/runtime/replace.ts +25 -10
  224. package/src/runtime/resilient-edit.ts +10 -25
  225. package/src/runtime/result-extractor.ts +2 -6
  226. package/src/runtime/retry-executor.ts +20 -4
  227. package/src/runtime/retry-runner.ts +27 -51
  228. package/src/runtime/role-permission.ts +6 -1
  229. package/src/runtime/run-coalesced-task-group.ts +256 -0
  230. package/src/runtime/run-drift.ts +14 -15
  231. package/src/runtime/run-tracker.ts +6 -28
  232. package/src/runtime/runtime-policy.ts +1 -6
  233. package/src/runtime/runtime-resolver.ts +80 -15
  234. package/src/runtime/scheduler.ts +47 -18
  235. package/src/runtime/semaphore.ts +5 -7
  236. package/src/runtime/sensitive-paths.ts +2 -1
  237. package/src/runtime/session-usage.ts +1 -1
  238. package/src/runtime/settings-store.ts +16 -12
  239. package/src/runtime/sidechain-output.ts +6 -2
  240. package/src/runtime/single-agent-compose.ts +1 -4
  241. package/src/runtime/skill-effectiveness.ts +29 -97
  242. package/src/runtime/skill-instructions.ts +40 -83
  243. package/src/runtime/stale-reconciler.ts +73 -197
  244. package/src/runtime/stream-preview.ts +1 -1
  245. package/src/runtime/streaming-output.ts +1 -1
  246. package/src/runtime/subagent-manager.ts +44 -169
  247. package/src/runtime/subprocess-tool-registry.ts +4 -1
  248. package/src/runtime/supervisor-contact.ts +10 -5
  249. package/src/runtime/task-display.ts +19 -5
  250. package/src/runtime/task-graph-scheduler.ts +95 -21
  251. package/src/runtime/task-graph.ts +5 -11
  252. package/src/runtime/task-health.ts +54 -47
  253. package/src/runtime/task-id.ts +12 -19
  254. package/src/runtime/task-output-context.ts +265 -81
  255. package/src/runtime/task-packet.ts +12 -20
  256. package/src/runtime/task-quality.ts +6 -14
  257. package/src/runtime/task-runner/context-retrieval.ts +4 -13
  258. package/src/runtime/task-runner/live-executor.ts +114 -36
  259. package/src/runtime/task-runner/output-splitter.ts +152 -0
  260. package/src/runtime/task-runner/progress.ts +50 -12
  261. package/src/runtime/task-runner/prompt-builder.ts +31 -7
  262. package/src/runtime/task-runner/prompt-pipeline.ts +31 -7
  263. package/src/runtime/task-runner/result-utils.ts +3 -1
  264. package/src/runtime/task-runner/retrieval-orchestrator.ts +310 -0
  265. package/src/runtime/task-runner/run-projection.ts +27 -8
  266. package/src/runtime/task-runner/state-helpers.ts +50 -14
  267. package/src/runtime/task-runner/tail-read.ts +2 -6
  268. package/src/runtime/task-runner.ts +180 -326
  269. package/src/runtime/team-runner-artifacts.ts +13 -0
  270. package/src/runtime/team-runner.ts +456 -974
  271. package/src/runtime/tool-output-pruner.ts +6 -9
  272. package/src/runtime/tool-progress.ts +11 -21
  273. package/src/runtime/verification-gates.ts +33 -24
  274. package/src/runtime/verification-integrity.ts +2 -7
  275. package/src/runtime/verification-worktree.ts +62 -12
  276. package/src/runtime/worker-heartbeat.ts +5 -1
  277. package/src/runtime/worker-startup.ts +29 -5
  278. package/src/runtime/workflow-state.ts +17 -9
  279. package/src/runtime/workspace-lock.ts +30 -35
  280. package/src/runtime/workspace-tree.ts +20 -32
  281. package/src/runtime/yield-handler.ts +43 -9
  282. package/src/runtime/zombie-scanner.ts +5 -4
  283. package/src/schema/config-schema.ts +266 -172
  284. package/src/schema/team-tool-schema.ts +29 -74
  285. package/src/schema/validation-types.ts +25 -17
  286. package/src/skills/discover-skills.ts +35 -10
  287. package/src/skills/skill-templates.ts +109 -27
  288. package/src/skills/validate.ts +26 -8
  289. package/src/state/active-run-registry.ts +87 -28
  290. package/src/state/artifact-store.ts +9 -5
  291. package/src/state/atomic-write-v2.ts +85 -63
  292. package/src/state/atomic-write.ts +106 -27
  293. package/src/state/blob-store.ts +47 -20
  294. package/src/state/contracts.ts +20 -5
  295. package/src/state/crew-init.ts +5 -22
  296. package/src/state/decision-ledger.ts +19 -73
  297. package/src/state/event-log-rotation.ts +41 -15
  298. package/src/state/event-log.ts +168 -53
  299. package/src/state/event-reconstructor.ts +11 -1
  300. package/src/state/gitignore-manager.ts +2 -8
  301. package/src/state/health-store.ts +57 -57
  302. package/src/state/hook-instinct-bridge.ts +1 -1
  303. package/src/state/hook-integrations.ts +1 -1
  304. package/src/state/instinct-store.ts +17 -5
  305. package/src/state/jsonl-writer.ts +1 -1
  306. package/src/state/locks.ts +23 -9
  307. package/src/state/mailbox.ts +151 -28
  308. package/src/state/observation-store.ts +20 -14
  309. package/src/state/run-cache.ts +142 -135
  310. package/src/state/run-graph.ts +6 -15
  311. package/src/state/run-metrics.ts +5 -18
  312. package/src/state/schedule.ts +15 -9
  313. package/src/state/state-store.ts +137 -47
  314. package/src/state/task-claims.ts +12 -2
  315. package/src/state/tiered-eval.ts +52 -43
  316. package/src/state/types-eval.ts +2 -2
  317. package/src/state/types.ts +24 -20
  318. package/src/state/usage.ts +20 -4
  319. package/src/state/worker-atomic-writer.ts +6 -3
  320. package/src/subagents/index.ts +2 -2
  321. package/src/teams/discover-teams.ts +21 -6
  322. package/src/tools/safe-bash-extension.ts +5 -6
  323. package/src/tools/safe-bash.ts +10 -11
  324. package/src/types/new-api-types.ts +6 -10
  325. package/src/ui/agent-management-overlay.ts +52 -40
  326. package/src/ui/card-colors.ts +13 -4
  327. package/src/ui/crew-footer.ts +8 -7
  328. package/src/ui/crew-select-list.ts +1 -1
  329. package/src/ui/dashboard-panes/agents-pane.ts +41 -25
  330. package/src/ui/dashboard-panes/cancellation-pane.ts +1 -1
  331. package/src/ui/dashboard-panes/capability-pane.ts +32 -15
  332. package/src/ui/dashboard-panes/health-pane.ts +2 -1
  333. package/src/ui/dashboard-panes/metrics-pane.ts +4 -1
  334. package/src/ui/dashboard-panes/progress-pane.ts +12 -9
  335. package/src/ui/heartbeat-aggregator.ts +15 -4
  336. package/src/ui/keybinding-map.ts +40 -7
  337. package/src/ui/live-conversation-overlay.ts +36 -20
  338. package/src/ui/live-duration.ts +1 -4
  339. package/src/ui/live-run-sidebar.ts +88 -25
  340. package/src/ui/loaders.ts +2 -8
  341. package/src/ui/mascot.ts +20 -36
  342. package/src/ui/overlays/agent-picker-overlay.ts +11 -3
  343. package/src/ui/overlays/confirm-overlay.ts +4 -2
  344. package/src/ui/overlays/help-overlay.ts +25 -14
  345. package/src/ui/overlays/mailbox-compose-overlay.ts +48 -11
  346. package/src/ui/overlays/mailbox-compose-preview.ts +20 -5
  347. package/src/ui/overlays/mailbox-detail-overlay.ts +27 -6
  348. package/src/ui/pi-ui-compat.ts +7 -7
  349. package/src/ui/powerbar-publisher.ts +120 -42
  350. package/src/ui/render-diff.ts +10 -3
  351. package/src/ui/render-scheduler.ts +12 -4
  352. package/src/ui/run-action-dispatcher.ts +81 -18
  353. package/src/ui/run-dashboard.ts +172 -76
  354. package/src/ui/run-event-bus.ts +59 -37
  355. package/src/ui/run-snapshot-cache.ts +276 -86
  356. package/src/ui/settings-overlay.ts +361 -74
  357. package/src/ui/status-colors.ts +12 -1
  358. package/src/ui/syntax-highlight.ts +1 -1
  359. package/src/ui/terminal-status.ts +1 -3
  360. package/src/ui/theme-adapter.ts +16 -14
  361. package/src/ui/theme-discovery.ts +13 -2
  362. package/src/ui/tool-progress-formatter.ts +7 -7
  363. package/src/ui/tool-render.ts +128 -56
  364. package/src/ui/tool-renderers/brief-mode.ts +45 -27
  365. package/src/ui/tool-renderers/index.ts +109 -43
  366. package/src/ui/transcript-cache.ts +32 -6
  367. package/src/ui/transcript-entries.ts +25 -23
  368. package/src/ui/transcript-viewer.ts +78 -29
  369. package/src/ui/widget/index.ts +99 -40
  370. package/src/ui/widget/widget-formatters.ts +13 -6
  371. package/src/ui/widget/widget-model.ts +19 -8
  372. package/src/ui/widget/widget-renderer.ts +23 -13
  373. package/src/ui/widget/widget-types.ts +1 -1
  374. package/src/utils/bm25-search.ts +199 -199
  375. package/src/utils/conflict-detect.ts +22 -21
  376. package/src/utils/env-filter.ts +14 -7
  377. package/src/utils/file-coalescer.ts +5 -1
  378. package/src/utils/fingerprint.ts +3 -6
  379. package/src/utils/frontmatter.ts +3 -1
  380. package/src/utils/fs-watch.ts +2 -6
  381. package/src/utils/gh-protocol.ts +119 -44
  382. package/src/utils/git.ts +13 -15
  383. package/src/utils/guards.ts +2 -5
  384. package/src/utils/ids.ts +9 -2
  385. package/src/utils/incremental-reader.ts +14 -3
  386. package/src/utils/internal-error.ts +2 -1
  387. package/src/utils/names.ts +12 -3
  388. package/src/utils/paths.ts +49 -5
  389. package/src/utils/project-detector.ts +2 -2
  390. package/src/utils/redaction.ts +29 -19
  391. package/src/utils/resolve-shell.ts +9 -7
  392. package/src/utils/run-watcher-registry.ts +19 -31
  393. package/src/utils/safe-paths.ts +46 -29
  394. package/src/utils/scan-cache.ts +9 -2
  395. package/src/utils/session-utils.ts +2 -4
  396. package/src/utils/sleep.ts +12 -5
  397. package/src/utils/sse-parser.ts +5 -17
  398. package/src/utils/visual.ts +52 -32
  399. package/src/workflows/discover-workflows.ts +41 -109
  400. package/src/workflows/intermediate-store.ts +5 -21
  401. package/src/workflows/preflight-validator.ts +6 -33
  402. package/src/workflows/topology-analyzer.ts +5 -21
  403. package/src/workflows/workflow-config.ts +7 -6
  404. package/src/worktree/branch-freshness.ts +66 -8
  405. package/src/worktree/cleanup.ts +130 -20
  406. package/src/worktree/worktree-manager.ts +212 -69
  407. package/skills/artifact-analysis-loop/SKILL.md +0 -303
  408. package/skills/detection-pipeline-design/SKILL.md +0 -286
  409. package/skills/hunting-investigation-loop/SKILL.md +0 -402
  410. package/skills/incident-playbook-construction/SKILL.md +0 -384
  411. package/skills/security-review/SKILL.md +0 -561
  412. package/skills/threat-hypothesis-framework/SKILL.md +0 -176
  413. package/skills/ui-render-performance/SKILL.md +0 -58
  414. /package/docs/{followup-review-round3-2026-05-12.md → archive/followup-review-round3-2026-05-12.md} +0 -0
  415. /package/docs/{followup-review-round4-2026-05-13.md → archive/followup-review-round4-2026-05-13.md} +0 -0
  416. /package/docs/{pi-crew-bugs.md → archive/pi-crew-bugs.md} +0 -0
  417. /package/docs/{pi-crew-test-final.md → archive/pi-crew-test-final.md} +0 -0
  418. /package/docs/{pi-crew-test-results.md → archive/pi-crew-test-results.md} +0 -0
  419. /package/docs/{pi-crew-test-round2.md → archive/pi-crew-test-round2.md} +0 -0
  420. /package/docs/{pi-crew-test-round4.md → archive/pi-crew-test-round4.md} +0 -0
  421. /package/docs/{pi-crew-test-round5.md → archive/pi-crew-test-round5.md} +0 -0
  422. /package/docs/{pi-crew-test-round6.md → archive/pi-crew-test-round6.md} +0 -0
  423. /package/docs/{pi-crew-v0.5.10-audit-fix-plan.md → archive/pi-crew-v0.5.10-audit-fix-plan.md} +0 -0
  424. /package/docs/{pi-crew-v0.5.11-audit-fix-plan.md → archive/pi-crew-v0.5.11-audit-fix-plan.md} +0 -0
  425. /package/docs/{pi-crew-v0.5.12-audit-fix-plan.md → archive/pi-crew-v0.5.12-audit-fix-plan.md} +0 -0
  426. /package/docs/{pi-crew-v0.5.13-audit-fix-plan.md → archive/pi-crew-v0.5.13-audit-fix-plan.md} +0 -0
  427. /package/docs/{pi-crew-v0.5.14-audit-fix-plan.md → archive/pi-crew-v0.5.14-audit-fix-plan.md} +0 -0
  428. /package/docs/{pi-crew-v0.5.16-audit-fix-plan.md → archive/pi-crew-v0.5.16-audit-fix-plan.md} +0 -0
  429. /package/docs/{pi-crew-v0.5.17-audit-fix-plan.md → archive/pi-crew-v0.5.17-audit-fix-plan.md} +0 -0
  430. /package/docs/{pi-crew-v0.5.5-audit-fix-plan.md → archive/pi-crew-v0.5.5-audit-fix-plan.md} +0 -0
  431. /package/docs/{pi-crew-v0.5.9-audit-fix-plan.md → archive/pi-crew-v0.5.9-audit-fix-plan.md} +0 -0
  432. /package/docs/{pi-mono-opportunities.md → archive/pi-mono-opportunities.md} +0 -0
  433. /package/docs/{pi-mono-review.md → archive/pi-mono-review.md} +0 -0
  434. /package/docs/{pi-subagent4-comparison.md → archive/pi-subagent4-comparison.md} +0 -0
  435. /package/docs/{pi-subagents3-deep-analysis.md → archive/pi-subagents3-deep-analysis.md} +0 -0
@@ -1,8 +1,8 @@
1
1
  import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
2
- import { listRecentRuns } from "../run-index.ts";
2
+ import type { ArtifactDescriptor, TeamRunManifest } from "../../state/types.ts";
3
3
  import { findRepoRoot } from "../../utils/paths.ts";
4
4
  import { extractSessionId } from "../../utils/session-utils.ts";
5
- import type { ArtifactDescriptor, TeamRunManifest } from "../../state/types.ts";
5
+ import { listRecentRuns } from "../run-index.ts";
6
6
 
7
7
  export interface RegisterCompactionGuardOptions {
8
8
  foregroundControllers: Map<string | symbol, AbortController>;
@@ -22,7 +22,10 @@ function contextWindow(ctx: { model?: { contextWindow?: number } }): number {
22
22
  return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : DEFAULT_CONTEXT_WINDOW;
23
23
  }
24
24
 
25
- function usageRatio(ctx: { getContextUsage(): { tokens: number | null } | undefined; model?: { contextWindow?: number } }): number | undefined {
25
+ function usageRatio(ctx: {
26
+ getContextUsage(): { tokens: number | null } | undefined;
27
+ model?: { contextWindow?: number };
28
+ }): number | undefined {
26
29
  const tokens = ctx.getContextUsage()?.tokens;
27
30
  if (tokens === null || tokens === undefined || !Number.isFinite(tokens)) return undefined;
28
31
  return tokens / contextWindow(ctx);
@@ -65,7 +68,9 @@ function formatCrewArtifactIndex(entries: CrewArtifactIndexEntry[]): string {
65
68
  if (!entries.length) return "";
66
69
  const lines = ["", "# pi-crew artifact index", "Preserve these run artifact references in the compaction summary:"];
67
70
  for (const entry of entries) {
68
- lines.push(`- ${entry.artifact.kind}: ${entry.artifact.path} (run=${entry.runId}, status=${entry.status}, team=${entry.team}, workflow=${entry.workflow ?? "none"}, producer=${entry.artifact.producer})`);
71
+ lines.push(
72
+ `- ${entry.artifact.kind}: ${entry.artifact.path} (run=${entry.runId}, status=${entry.status}, team=${entry.team}, workflow=${entry.workflow ?? "none"}, producer=${entry.artifact.producer})`,
73
+ );
69
74
  }
70
75
  return lines.join("\n");
71
76
  }
@@ -138,9 +143,7 @@ function formatResumeDirective(runs: TeamRunManifest[]): string {
138
143
  ];
139
144
  for (const run of runs) {
140
145
  const wf = run.workflow ? `, workflow=${run.workflow}` : "";
141
- lines.push(
142
- `- runId=${run.runId} (status=${run.status}, team=${run.team}${wf}): ${run.goal}`,
143
- );
146
+ lines.push(`- runId=${run.runId} (status=${run.status}, team=${run.team}${wf}): ${run.goal}`);
144
147
  }
145
148
  lines.push("");
146
149
  lines.push("To resume: call the `team` tool with action='status' to check progress, then");
@@ -159,15 +162,15 @@ function formatResumeDirective(runs: TeamRunManifest[]): string {
159
162
  */
160
163
  export function buildContinuationPrompt(runs: TeamRunManifest[]): string {
161
164
  if (!runs.length) return "";
162
- const lines = [
163
- "[pi-crew] Context was compacted while crew tasks were still in-flight. Continue the work — do not wait for me.",
164
- ];
165
+ const lines = ["[pi-crew] Context was compacted while crew tasks were still in-flight. Continue the work — do not wait for me."];
165
166
  for (const run of runs) {
166
167
  const wf = run.workflow ? `, workflow=${run.workflow}` : "";
167
168
  lines.push(`- runId=${run.runId} (status=${run.status}, team=${run.team}${wf}): ${run.goal}`);
168
169
  }
169
170
  lines.push("");
170
- lines.push("Resume: call `team` with action='status' to check progress, then action='wait' (join a running task), action='summary', or action='get' as appropriate. If a worker is still alive it continues independently — just re-attach. Do NOT restart completed work.");
171
+ lines.push(
172
+ "Resume: call `team` with action='status' to check progress, then action='wait' (join a running task), action='summary', or action='get' as appropriate. If a worker is still alive it continues independently — just re-attach. Do NOT restart completed work.",
173
+ );
171
174
  return lines.join("\n");
172
175
  }
173
176
 
@@ -294,10 +297,7 @@ export function registerCompactionGuard(pi: ExtensionAPI, options: RegisterCompa
294
297
  goal: r.goal,
295
298
  })),
296
299
  });
297
- ctx.ui.notify(
298
- `Context compacted. ${inFlight.length} pi-crew run(s) still in-flight — auto-resuming.`,
299
- "info",
300
- );
300
+ ctx.ui.notify(`Context compacted. ${inFlight.length} pi-crew run(s) still in-flight — auto-resuming.`, "info");
301
301
  // THE FIX: trigger automatic continuation. Without this, Pi stops
302
302
  // after threshold compaction and the user must type "continue".
303
303
  triggerContinuation(pi, ctx, inFlight);
@@ -0,0 +1,259 @@
1
+ /**
2
+ * Lifecycle installer (H3 + L2 — Phase 4 of cleanup plan).
3
+ *
4
+ * Owns the run-lifecycle subscriptions that should fire ONLY when a run is
5
+ * active. This is the "lazy" half of H3 — the heavy heartbeat/auto-repair
6
+ * graph stays out of cold start until a run actually starts.
7
+ *
8
+ * Extracted from src/extension/register.ts. State holder is `LifecycleState`;
9
+ * installer is `startLifecycleWatchers(ctx, state, deps)` which fires on
10
+ * session_start (when the first run becomes possible) and is no-op when
11
+ * `activeRunCount === 0`.
12
+ *
13
+ * Also owns DeliveryCoordinator + OverflowRecoveryTracker + NotificationRouter
14
+ * wiring since these are all "delivery lifecycle" services that share the
15
+ * same per-session install/dispose pattern.
16
+ */
17
+ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
18
+ import { loadConfig } from "../../config/config.ts";
19
+ import { DEFAULT_NOTIFICATIONS, DEFAULT_UI } from "../../config/defaults.ts";
20
+ import { logInternalError } from "../../utils/internal-error.ts";
21
+ import { projectCrewRoot } from "../../utils/paths.ts";
22
+ import type { AsyncNotifierState } from "../async-notifier.ts";
23
+ import type { NotificationDescriptor, NotificationRouter } from "../notification-router.ts";
24
+ import type { NotificationSink } from "../notification-sink.ts";
25
+
26
+ /**
27
+ * Mutable state owned by register.ts and read/written by this module.
28
+ * Tracks whether the async notifier is currently running so we can
29
+ * safely no-op duplicate calls.
30
+ */
31
+ export interface LifecycleState {
32
+ notifierStarted: boolean;
33
+ notificationSink: NotificationSink | undefined;
34
+ notificationRouter: NotificationRouter | undefined;
35
+ deliveryCoordinator: import("../../runtime/delivery-coordinator.ts").DeliveryCoordinator | undefined;
36
+ overflowTracker: import("../../runtime/overflow-recovery.ts").OverflowRecoveryTracker | undefined;
37
+ }
38
+
39
+ /** Dependencies passed in by register.ts so this module stays decoupled. */
40
+ export interface LifecycleDeps {
41
+ pi: ExtensionAPI;
42
+ notifierState: AsyncNotifierState;
43
+ isCleanedUp: () => boolean;
44
+ isContextCurrent: (ctx: ExtensionContext, ownerGeneration: number) => boolean;
45
+ ownerGeneration: number;
46
+ }
47
+
48
+ /**
49
+ * Start the async-run notifier. Idempotent — repeated calls while running
50
+ * are no-ops. Returns true if started now, false if already running or
51
+ * skipped due to config gate.
52
+ *
53
+ * NOTE: This module does NOT start heartbeat watchers or auto-repair timers.
54
+ * Those live in `observability.ts` (configurable + resource-heavy). This
55
+ * module only manages the lightweight async-run poller.
56
+ */
57
+ export function startLifecycleWatchers(ctx: ExtensionContext, state: LifecycleState, deps: LifecycleDeps): boolean {
58
+ if (state.notifierStarted) return false;
59
+ const loadedConfig = loadConfig(ctx.cwd);
60
+ state.notifierStarted = true;
61
+ try {
62
+ // LAZY: async-notifier pulls in debounce + cron helpers — defer
63
+ // until the first lifecycle install (deferred import within module).
64
+ void import("../async-notifier.ts").then(({ startAsyncRunNotifier }) => {
65
+ if (deps.isCleanedUp()) return;
66
+ startAsyncRunNotifier(ctx, deps.notifierState, loadedConfig.config.notifierIntervalMs ?? DEFAULT_UI.notifierIntervalMs, {
67
+ generation: deps.ownerGeneration,
68
+ isCurrent: (generation) => generation === deps.ownerGeneration && deps.isContextCurrent(ctx, deps.ownerGeneration),
69
+ });
70
+ });
71
+ return true;
72
+ } catch (error) {
73
+ state.notifierStarted = false;
74
+ logInternalError("register.startLifecycleWatchers", error);
75
+ return false;
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Stop the async-run notifier. Safe to call when not started.
81
+ */
82
+ export function stopLifecycleWatchers(state: LifecycleState, deps: LifecycleDeps): void {
83
+ if (!state.notifierStarted) return;
84
+ state.notifierStarted = false;
85
+ try {
86
+ void import("../async-notifier.ts").then(({ stopAsyncRunNotifier }) => {
87
+ stopAsyncRunNotifier(deps.notifierState);
88
+ });
89
+ } catch (error) {
90
+ logInternalError("register.stopLifecycleWatchers", error);
91
+ }
92
+ }
93
+
94
+ /** Dependencies for configureNotifications (UI-facing delivery wiring). */
95
+ export interface NotificationsDeps {
96
+ pi: ExtensionAPI;
97
+ widgetState: { notificationCount?: number };
98
+ getCurrentCtx: () => ExtensionContext | undefined;
99
+ getManifestCache: (cwd: string) => ReturnType<typeof import("../../runtime/manifest-cache.ts").createManifestCache>;
100
+ getRunSnapshotCache: (cwd: string) => ReturnType<typeof import("../../ui/run-snapshot-cache.ts").createRunSnapshotCache>;
101
+ requestPowerbarUpdate: (
102
+ events: Parameters<typeof import("../../ui/powerbar-publisher.ts").requestPowerbarUpdate>[0],
103
+ cwd: string,
104
+ uiConfig: Parameters<typeof import("../../ui/powerbar-publisher.ts").requestPowerbarUpdate>[2],
105
+ manifestCache: Parameters<typeof import("../../ui/powerbar-publisher.ts").requestPowerbarUpdate>[3],
106
+ snapshotCache: Parameters<typeof import("../../ui/powerbar-publisher.ts").requestPowerbarUpdate>[4],
107
+ ctx: ExtensionContext,
108
+ notificationCount: number,
109
+ ) => void;
110
+ }
111
+
112
+ /**
113
+ * Configure the notification router + JSONL sink. Idempotent — caller should
114
+ * dispose prior state first. Pulled out of register.ts as part of H3-L2.
115
+ *
116
+ * Gates:
117
+ * - `config.notifications?.enabled === false` → no-op.
118
+ */
119
+ export async function configureNotifications(ctx: ExtensionContext, state: LifecycleState, deps: NotificationsDeps): Promise<void> {
120
+ state.notificationRouter?.dispose();
121
+ state.notificationSink?.dispose();
122
+ state.notificationRouter = undefined;
123
+ state.notificationSink = undefined;
124
+ const config = loadConfig(ctx.cwd).config;
125
+ if (config.notifications?.enabled === false) return;
126
+
127
+ // LAZY: notification router — wires events to sinks
128
+ const { NotificationRouter } = await import("../notification-router.ts");
129
+ // LAZY: JSONL sink — file-backed notification storage
130
+ const { createJsonlSink } = await import("../notification-sink.ts");
131
+ // LAZY: follow-up helpers — shared with subagent handoff
132
+ const { sendFollowUp } = await import("./subagent-helpers.ts");
133
+ // LAZY: widget updater — refreshes the TUI status widget
134
+ const { updateCrewWidget } = await import("../../ui/widget/index.ts");
135
+
136
+ if (config.telemetry?.enabled !== false) {
137
+ state.notificationSink = createJsonlSink(
138
+ projectCrewRoot(ctx.cwd),
139
+ config.notifications?.sinkRetentionDays ?? DEFAULT_NOTIFICATIONS.sinkRetentionDays,
140
+ );
141
+ }
142
+ state.notificationRouter = new NotificationRouter(
143
+ {
144
+ dedupWindowMs: config.notifications?.dedupWindowMs ?? DEFAULT_NOTIFICATIONS.dedupWindowMs,
145
+ batchWindowMs: config.notifications?.batchWindowMs ?? DEFAULT_NOTIFICATIONS.batchWindowMs,
146
+ quietHours: config.notifications?.quietHours,
147
+ severityFilter: config.notifications?.severityFilter ?? [...DEFAULT_NOTIFICATIONS.severityFilter],
148
+ sink: (notification) => state.notificationSink?.write(notification),
149
+ },
150
+ (notification: NotificationDescriptor) => {
151
+ deps.widgetState.notificationCount = (deps.widgetState.notificationCount ?? 0) + 1;
152
+ sendFollowUp(
153
+ deps.pi,
154
+ [notification.title, notification.body, notification.runId ? `Run: ${notification.runId}` : undefined]
155
+ .filter((line): line is string => Boolean(line))
156
+ .join("\n"),
157
+ );
158
+ const currentCtx = deps.getCurrentCtx();
159
+ if (currentCtx) {
160
+ const uiConfig = loadConfig(currentCtx.cwd).config.ui;
161
+ updateCrewWidget(
162
+ currentCtx,
163
+ deps.widgetState as Parameters<typeof updateCrewWidget>[1],
164
+ uiConfig,
165
+ deps.getManifestCache(currentCtx.cwd),
166
+ deps.getRunSnapshotCache(currentCtx.cwd),
167
+ );
168
+ deps.requestPowerbarUpdate(
169
+ deps.pi.events,
170
+ currentCtx.cwd,
171
+ uiConfig,
172
+ deps.getManifestCache(currentCtx.cwd),
173
+ deps.getRunSnapshotCache(currentCtx.cwd),
174
+ currentCtx,
175
+ deps.widgetState.notificationCount ?? 0,
176
+ );
177
+ }
178
+ },
179
+ );
180
+ }
181
+
182
+ /** Dispose notification router + sink. */
183
+ export function disposeNotifications(state: LifecycleState): void {
184
+ state.notificationRouter?.dispose();
185
+ state.notificationRouter = undefined;
186
+ state.notificationSink?.dispose();
187
+ state.notificationSink = undefined;
188
+ }
189
+
190
+ /** Dependencies for configureDeliveryCoordinator. */
191
+ export interface DeliveryDeps {
192
+ pi: ExtensionAPI;
193
+ observabilityState: { metricRegistry?: import("../../observability/metric-registry.ts").MetricRegistry };
194
+ notifyOperator: (notification: NotificationDescriptor) => void;
195
+ sendFollowUp: (pi: ExtensionAPI, message: string) => void;
196
+ sendAgentWakeUp: (pi: ExtensionAPI, message: string) => void;
197
+ }
198
+
199
+ /**
200
+ * Configure the delivery coordinator + overflow tracker. Extracted from
201
+ * register.ts as part of H3-L2 (delivery lifecycle service).
202
+ */
203
+ export async function configureDeliveryCoordinator(state: LifecycleState, deps: DeliveryDeps): Promise<void> {
204
+ state.deliveryCoordinator?.dispose();
205
+ state.deliveryCoordinator = undefined;
206
+ state.overflowTracker?.dispose();
207
+ state.overflowTracker = undefined;
208
+ // LAZY: delivery coordinator — batches notification fan-out
209
+ const { DeliveryCoordinator } = await import("../../runtime/delivery-coordinator.ts");
210
+ // LAZY: overflow tracker — recovers from backlog overflow
211
+ const { OverflowRecoveryTracker } = await import("../../runtime/overflow-recovery.ts");
212
+ state.deliveryCoordinator = new DeliveryCoordinator({
213
+ emit: (event, data) => {
214
+ deps.pi.events?.emit?.(event, data);
215
+ },
216
+ sendFollowUp: (title, body) => {
217
+ deps.sendFollowUp(deps.pi, [title, body].filter((line): line is string => Boolean(line)).join("\n"));
218
+ },
219
+ sendWakeUp: (message) => {
220
+ deps.sendAgentWakeUp(deps.pi, message);
221
+ },
222
+ });
223
+ state.overflowTracker = new OverflowRecoveryTracker({
224
+ onPhaseChange: (phaseState, previousPhase) => {
225
+ if (deps.observabilityState.metricRegistry) {
226
+ deps.observabilityState.metricRegistry
227
+ .counter("crew.task.overflow_recovery_total", "Overflow recovery phase transitions")
228
+ .inc({
229
+ phase: phaseState.phase,
230
+ previous_phase: previousPhase,
231
+ });
232
+ }
233
+ deps.pi.events?.emit?.("crew.task.overflow", {
234
+ runId: phaseState.runId,
235
+ taskId: phaseState.taskId,
236
+ phase: phaseState.phase,
237
+ previousPhase,
238
+ });
239
+ },
240
+ onTimeout: (phaseState) => {
241
+ deps.notifyOperator({
242
+ id: `overflow_timeout_${phaseState.taskId}`,
243
+ severity: "warning",
244
+ source: "overflow-recovery",
245
+ runId: phaseState.runId,
246
+ title: `Task ${phaseState.taskId} overflow recovery timed out`,
247
+ body: `Phase: ${phaseState.phase}, compaction_count: ${phaseState.compactionCount}, retry_count: ${phaseState.retryCount}. The task may be stuck.`,
248
+ });
249
+ },
250
+ });
251
+ }
252
+
253
+ /** Dispose delivery coordinator + overflow tracker. */
254
+ export function disposeDeliveryCoordinator(state: LifecycleState): void {
255
+ state.deliveryCoordinator?.dispose();
256
+ state.deliveryCoordinator = undefined;
257
+ state.overflowTracker?.dispose();
258
+ state.overflowTracker = undefined;
259
+ }
@@ -0,0 +1,293 @@
1
+ /**
2
+ * Observability installer (H3 + L2 — Phase 4 of cleanup plan).
3
+ *
4
+ * Owns the observability lifecycle for the pi-crew extension:
5
+ * - MetricRegistry + EventToMetricSubscription (wireEventToMetrics)
6
+ * - Metric file sink (JSONL writer)
7
+ * - OTLPExporter (lazy-imported when otlp.enabled=true)
8
+ * - HeartbeatWatcher (per-session, polling)
9
+ * - Auto-repair timers (stale-reconcile + orphan-temp-dirs cleanup)
10
+ *
11
+ * Extracted from src/extension/register.ts so the orchestrator stays thin.
12
+ * The module exports `ObservabilityState` (mutable state holder) and
13
+ * `configureObservability(ctx, state, deps)` (the install function). The
14
+ * orchestrator keeps ownership of `state` so cleanupRuntime can dispose
15
+ * via `disposeObservability(state, isCleanedUp)`.
16
+ *
17
+ * This file is a TARGET for lazy loading (H3 follow-up): on systems where
18
+ * observability.enabled === false, `installObservability()` is never
19
+ * imported — the heavy observability module graph stays out of cold start.
20
+ */
21
+ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
22
+ import { loadConfig } from "../../config/config.ts";
23
+ import type { EventToMetricSubscription } from "../../observability/event-to-metric.ts";
24
+ import type { MetricRegistry } from "../../observability/metric-registry.ts";
25
+ import type { MetricSink } from "../../observability/metric-sink.ts";
26
+ import type { HeartbeatWatcher } from "../../runtime/heartbeat-watcher.ts";
27
+ import { logInternalError } from "../../utils/internal-error.ts";
28
+ import { projectCrewRoot } from "../../utils/paths.ts";
29
+ import type { NotificationDescriptor } from "../notification-router.ts";
30
+
31
+ /** Type-only alias for the lazy-loaded OTLPExporter (avoid static import). */
32
+ type OTLPExporterInstance = import("../../observability/exporters/otlp-exporter.ts").OTLPExporter;
33
+ type OTLPExporterCtor = new (
34
+ opts: import("../../observability/exporters/otlp-exporter.ts").OTLPExporterOptions,
35
+ registry: import("../../observability/metric-registry.ts").MetricRegistry,
36
+ ) => OTLPExporterInstance;
37
+
38
+ /**
39
+ * Mutable state owned by register.ts and read/written by this module.
40
+ * Pass this same object to `configureObservability` and `disposeObservability`
41
+ * so they can mutate the fields in-place.
42
+ */
43
+ export interface ObservabilityState {
44
+ metricRegistry: MetricRegistry | undefined;
45
+ eventMetricSub: EventToMetricSubscription | undefined;
46
+ metricSink: MetricSink | undefined;
47
+ heartbeatWatcher: HeartbeatWatcher | undefined;
48
+ autoRepairTimer: ReturnType<typeof setInterval> | undefined;
49
+ tempReconcileTimer: ReturnType<typeof setInterval> | undefined;
50
+ otlpExporter: OTLPExporterInstance | undefined;
51
+ }
52
+
53
+ /** Dependencies passed in by register.ts so this module stays decoupled. */
54
+ export interface ObservabilityDeps {
55
+ pi: ExtensionAPI;
56
+ getManifestCache: (cwd: string) => ReturnType<typeof import("../../runtime/manifest-cache.ts").createManifestCache>;
57
+ notifyOperator: (notification: NotificationDescriptor) => void;
58
+ isCleanedUp: () => boolean;
59
+ reconcileStaleRuns: (cwd: string, cache: ReturnType<ObservabilityDeps["getManifestCache"]>) => unknown[];
60
+ reconcileOrphanedTempWorkspaces: (now: number, opts: { cleanupOrphanedTempDirs?: boolean }) => unknown;
61
+ cleanupOrphanTempDirs: () => { cleaned: number; scanned: number; failed: number };
62
+ cleanupLegacyOrphanTempDirs: () => { cleaned: number; scanned: number; failed: number };
63
+ appendDeadletter: (
64
+ manifest: import("../../state/types.ts").TeamRunManifest,
65
+ entry: { taskId: string; runId: string; reason: string; attempts: number; timestamp: string },
66
+ ) => void;
67
+ importCrashRecovery: () => Promise<{
68
+ detectInterruptedRuns: (
69
+ cwd: string,
70
+ cache: ReturnType<ObservabilityDeps["getManifestCache"]>,
71
+ ) => Iterable<{ runId: string; resumableTasks: unknown[] }>;
72
+ }>;
73
+ }
74
+
75
+ let _cachedOTLPExporter: OTLPExporterCtor | undefined;
76
+ async function importOTLPExporter(): Promise<OTLPExporterCtor> {
77
+ if (!_cachedOTLPExporter) {
78
+ // LAZY: opt-in OTLP metric export — load only when otlp.enabled=true.
79
+ const mod = await import("../../observability/exporters/otlp-exporter.ts");
80
+ _cachedOTLPExporter = mod.OTLPExporter as unknown as OTLPExporterCtor;
81
+ }
82
+ return _cachedOTLPExporter;
83
+ }
84
+
85
+ /**
86
+ * Configure the observability stack for the current session. Idempotent
87
+ * (caller should call `disposeObservability` first on each session_start
88
+ * cycle to avoid stacking watchers).
89
+ *
90
+ * Gates:
91
+ * - `config.observability?.enabled === false` → no-op (skips all init).
92
+ * - `config.telemetry?.enabled !== false` → installs metric file sink.
93
+ * - `config.otlp?.enabled === true` → lazy-loads OTLPExporter.
94
+ * - `config.reliability?.autoRepairIntervalMs > 0` → starts reconcile timers.
95
+ * - `config.reliability?.autoRecover === true` → lazy-imports crash-recovery
96
+ * on a deferred setTimeout to avoid blocking session_start.
97
+ */
98
+ export async function configureObservability(ctx: ExtensionContext, state: ObservabilityState, deps: ObservabilityDeps): Promise<void> {
99
+ // Always start from a clean slate: dispose any prior-session state first.
100
+ disposeObservability(state, deps.isCleanedUp());
101
+
102
+ const config = loadConfig(ctx.cwd).config;
103
+ if (config.observability?.enabled === false) return;
104
+
105
+ // LAZY: observability stack — only paid for when observability is actually enabled
106
+ const { createMetricRegistry } = await import("../../observability/metric-registry.ts");
107
+ // LAZY: event→metric bridge
108
+ const { wireEventToMetrics } = await import("../../observability/event-to-metric.ts");
109
+ // LAZY: file-backed metric sink
110
+ const { createMetricFileSink } = await import("../../observability/metric-sink.ts");
111
+
112
+ state.metricRegistry = createMetricRegistry();
113
+ if (deps.pi.events) {
114
+ state.eventMetricSub = wireEventToMetrics(deps.pi.events, state.metricRegistry);
115
+ }
116
+ if (config.telemetry?.enabled !== false) {
117
+ state.metricSink = createMetricFileSink({
118
+ crewRoot: projectCrewRoot(ctx.cwd),
119
+ registry: state.metricRegistry,
120
+ retentionDays: config.observability?.metricRetentionDays ?? 7,
121
+ });
122
+ }
123
+
124
+ // OTLP export is opt-in. Lazy-loaded via dynamic import.
125
+ if (config.otlp?.enabled === true && config.otlp.endpoint) {
126
+ const otlpEndpoint = config.otlp.endpoint;
127
+ const otlpHeaders = config.otlp.headers;
128
+ const otlpInterval = config.otlp.intervalMs;
129
+ const owningRegistry = state.metricRegistry;
130
+ // LAZY: opt-in OTLP export — load the exporter module on first enable.
131
+ void importOTLPExporter()
132
+ .then((Ctor) => {
133
+ if (deps.isCleanedUp() || state.metricRegistry !== owningRegistry || !owningRegistry) return;
134
+ state.otlpExporter = new Ctor(
135
+ {
136
+ endpoint: otlpEndpoint,
137
+ headers: otlpHeaders,
138
+ intervalMs: otlpInterval,
139
+ },
140
+ owningRegistry,
141
+ );
142
+ state.otlpExporter?.start();
143
+ })
144
+ .catch((error: unknown) => logInternalError("register.otlp-lazy-import", error));
145
+ }
146
+
147
+ // LAZY: heartbeat watcher — polled per-session, wires deadletter + metric events.
148
+ const { HeartbeatWatcher } = await import("../../runtime/heartbeat-watcher.ts");
149
+ state.heartbeatWatcher = new HeartbeatWatcher({
150
+ cwd: ctx.cwd,
151
+ pollIntervalMs: config.observability?.pollIntervalMs ?? 5000,
152
+ manifestCache: deps.getManifestCache(ctx.cwd),
153
+ registry: state.metricRegistry,
154
+ router: {
155
+ enqueue: (notification) => {
156
+ deps.notifyOperator(notification);
157
+ return true;
158
+ },
159
+ },
160
+ deadletterTickThreshold: config.reliability?.deadletterThreshold ?? 3,
161
+ onDeadletterTrigger: (manifest, taskId) => {
162
+ deps.appendDeadletter(manifest, {
163
+ taskId,
164
+ runId: manifest.runId,
165
+ reason: "heartbeat-dead",
166
+ attempts: 0,
167
+ timestamp: new Date().toISOString(),
168
+ });
169
+ state.metricRegistry?.counter("crew.task.deadletter_total", "Deadletter triggers by reason").inc({ reason: "heartbeat-dead" });
170
+ deps.pi.events?.emit?.("crew.task.deadletter", {
171
+ runId: manifest.runId,
172
+ taskId,
173
+ reason: "heartbeat-dead",
174
+ });
175
+ },
176
+ });
177
+ state.heartbeatWatcher.start();
178
+
179
+ // Auto-repair timers: stale-run reconcile + orphan-temp cleanup.
180
+ const autoRepairIntervalMs = config.reliability?.autoRepairIntervalMs ?? 60_000;
181
+ if (autoRepairIntervalMs > 0) {
182
+ state.autoRepairTimer = setInterval(() => {
183
+ if (deps.isCleanedUp()) return;
184
+ try {
185
+ const staleResults = deps.reconcileStaleRuns(ctx.cwd, deps.getManifestCache(ctx.cwd));
186
+ if (Array.isArray(staleResults) && staleResults.length > 0) {
187
+ for (const result of staleResults) {
188
+ const repaired = (result as { repaired?: boolean }).repaired;
189
+ if (repaired) {
190
+ deps.notifyOperator({
191
+ id: `auto_repair_${(result as { runId: string }).runId}`,
192
+ severity: "info",
193
+ source: "auto-repair",
194
+ runId: (result as { runId: string }).runId,
195
+ title: `Auto-repaired stale run`,
196
+ body: (result as { detail?: string }).detail ?? "",
197
+ });
198
+ }
199
+ }
200
+ }
201
+ } catch (error) {
202
+ logInternalError("register.autoRepair", error);
203
+ }
204
+ }, autoRepairIntervalMs);
205
+ state.autoRepairTimer.unref();
206
+
207
+ // Less frequent (5x interval) — clean orphan temp dirs.
208
+ state.tempReconcileTimer = setInterval(() => {
209
+ if (deps.isCleanedUp()) return;
210
+ try {
211
+ deps.reconcileOrphanedTempWorkspaces(Date.now(), {
212
+ cleanupOrphanedTempDirs:
213
+ typeof config.reliability?.cleanupOrphanedTempDirs === "boolean"
214
+ ? config.reliability.cleanupOrphanedTempDirs
215
+ : undefined,
216
+ });
217
+ const orphanResult = deps.cleanupOrphanTempDirs();
218
+ if (orphanResult.cleaned > 0) {
219
+ deps.notifyOperator({
220
+ id: `layer4_temp_cleanup_${Date.now()}`,
221
+ severity: "info",
222
+ source: "temp-cleanup",
223
+ title: `Layer 4: cleaned ${orphanResult.cleaned} orphan temp dir(s)`,
224
+ body: `~/.pi/agent/pi-crew/tmp/ orphans older than 24h removed (scanned ${orphanResult.scanned}, failed ${orphanResult.failed}).`,
225
+ });
226
+ }
227
+ const legacyResult = deps.cleanupLegacyOrphanTempDirs();
228
+ if (legacyResult.cleaned > 0) {
229
+ deps.notifyOperator({
230
+ id: `layer5_legacy_temp_cleanup_${Date.now()}`,
231
+ severity: "info",
232
+ source: "temp-cleanup",
233
+ title: `Layer 5: cleaned ${legacyResult.cleaned} legacy /tmp/pi-crew-* orphan(s)`,
234
+ body: `Pre-fix /tmp/pi-crew-* prompt/task orphans (no .crew/state/runs/, >24h) removed (scanned ${legacyResult.scanned}, failed ${legacyResult.failed}).`,
235
+ });
236
+ }
237
+ } catch (error) {
238
+ logInternalError("register.tempAutoRepair", error);
239
+ }
240
+ }, autoRepairIntervalMs * 5);
241
+ state.tempReconcileTimer.unref();
242
+ }
243
+
244
+ // autoRecover → on session_start, lazy-import crash-recovery and prompt the
245
+ // operator about interrupted runs. Defers to a microtask so it never blocks
246
+ // session_start.
247
+ if (config.reliability?.autoRecover === true) {
248
+ const cwdSnapshot = ctx.cwd;
249
+ const cacheSnapshot = deps.getManifestCache(cwdSnapshot);
250
+ void deps
251
+ .importCrashRecovery()
252
+ .then(({ detectInterruptedRuns }) => {
253
+ if (deps.isCleanedUp()) return;
254
+ for (const plan of detectInterruptedRuns(cwdSnapshot, cacheSnapshot)) {
255
+ deps.notifyOperator({
256
+ id: `recovery_prompt_${plan.runId}`,
257
+ severity: "warning",
258
+ source: "crash-recovery",
259
+ runId: plan.runId,
260
+ title: `Run ${plan.runId} was interrupted`,
261
+ body: `${plan.resumableTasks.length} tasks pending recovery. Open dashboard to inspect before resuming.`,
262
+ });
263
+ }
264
+ })
265
+ .catch((error: unknown) => logInternalError("register.crash-recovery-lazy-import", error));
266
+ }
267
+ }
268
+
269
+ /**
270
+ * Dispose all observability resources. Safe to call when state is already
271
+ * partially or fully disposed. Caller passes `isCleanedUp` so timers can be
272
+ * gated by the orchestrator's overall cleanup state.
273
+ */
274
+ export function disposeObservability(state: ObservabilityState, _isCleanedUp: boolean): void {
275
+ state.heartbeatWatcher?.dispose();
276
+ state.heartbeatWatcher = undefined;
277
+ if (state.autoRepairTimer) {
278
+ clearInterval(state.autoRepairTimer);
279
+ state.autoRepairTimer = undefined;
280
+ }
281
+ if (state.tempReconcileTimer) {
282
+ clearInterval(state.tempReconcileTimer);
283
+ state.tempReconcileTimer = undefined;
284
+ }
285
+ state.metricSink?.dispose();
286
+ state.metricSink = undefined;
287
+ state.eventMetricSub?.dispose();
288
+ state.eventMetricSub = undefined;
289
+ state.otlpExporter?.dispose();
290
+ state.otlpExporter = undefined;
291
+ state.metricRegistry?.dispose();
292
+ state.metricRegistry = undefined;
293
+ }