pi-crew 0.9.50 → 0.9.52

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 (609) hide show
  1. package/CHANGELOG.md +150 -0
  2. package/README.md +216 -20
  3. package/dist/build-meta.json +2222 -1761
  4. package/dist/index.mjs +18636 -17603
  5. package/docs/actions-reference.md +431 -1
  6. package/package.json +19 -9
  7. package/schema.json +41 -0
  8. package/scripts/check-bundle-size.mjs +38 -0
  9. package/scripts/test-runner.mjs +11 -1
  10. package/skills/distill-software/SKILL.md +24 -1
  11. package/dist/index.mjs.map +0 -7
  12. package/docs/FEATURE_INTAKE.md +0 -126
  13. package/docs/HARNESS.md +0 -87
  14. package/docs/HARNESS_BACKLOG.md +0 -89
  15. package/docs/PROJECT_REVIEW.md +0 -270
  16. package/docs/PROJECT_REVIEW_FIXES.md +0 -342
  17. package/docs/PROJECT_REVIEW_ROUND4.md +0 -156
  18. package/docs/PROJECT_REVIEW_ROUND5.md +0 -86
  19. package/docs/REVIEW-FINDINGS-2026-06 +0 -357
  20. package/docs/REVIEW-FINDINGS-2026-07-CORE.md +0 -139
  21. package/docs/REVIEW-FINDINGS-2026-07.md +0 -125
  22. package/docs/TEST_MATRIX.md +0 -51
  23. package/docs/archive/README.md +0 -91
  24. package/docs/archive/followup-review-round3-2026-05-12.md +0 -342
  25. package/docs/archive/followup-review-round4-2026-05-13.md +0 -107
  26. package/docs/archive/pi-crew-bugs.md +0 -961
  27. package/docs/archive/pi-crew-test-final.md +0 -120
  28. package/docs/archive/pi-crew-test-results.md +0 -260
  29. package/docs/archive/pi-crew-test-round2.md +0 -136
  30. package/docs/archive/pi-crew-test-round4.md +0 -100
  31. package/docs/archive/pi-crew-test-round5.md +0 -70
  32. package/docs/archive/pi-crew-test-round6.md +0 -110
  33. package/docs/archive/pi-crew-v0.5.10-audit-fix-plan.md +0 -60
  34. package/docs/archive/pi-crew-v0.5.11-audit-fix-plan.md +0 -92
  35. package/docs/archive/pi-crew-v0.5.12-audit-fix-plan.md +0 -76
  36. package/docs/archive/pi-crew-v0.5.13-audit-fix-plan.md +0 -75
  37. package/docs/archive/pi-crew-v0.5.14-audit-fix-plan.md +0 -75
  38. package/docs/archive/pi-crew-v0.5.16-audit-fix-plan.md +0 -35
  39. package/docs/archive/pi-crew-v0.5.17-audit-fix-plan.md +0 -80
  40. package/docs/archive/pi-crew-v0.5.5-audit-fix-plan.md +0 -133
  41. package/docs/archive/pi-crew-v0.5.9-audit-fix-plan.md +0 -88
  42. package/docs/archive/pi-mono-opportunities.md +0 -969
  43. package/docs/archive/pi-mono-review.md +0 -291
  44. package/docs/archive/pi-subagent4-comparison.md +0 -261
  45. package/docs/archive/pi-subagents3-deep-analysis.md +0 -508
  46. package/docs/bugs/bug-021-notification-badge-counter-misleading.md +0 -293
  47. package/docs/bugs/bug-023-chain-windows-path-resolution.md +0 -106
  48. package/docs/bugs/bug-quota-display-truncation.md +0 -223
  49. package/docs/bugs/cross-session-notification-leakage.md +0 -82
  50. package/docs/code-review-2026-05-11.md +0 -592
  51. package/docs/coding-agent-optimization.md +0 -268
  52. package/docs/comparison-pi-subagents-vs-pi-crew.md +0 -303
  53. package/docs/decisions/0001-durable-state.md +0 -41
  54. package/docs/decisions/0002-child-process-for-async.md +0 -42
  55. package/docs/decisions/0003-depth-guard.md +0 -36
  56. package/docs/decisions/0004-execfile-over-exec.md +0 -34
  57. package/docs/decisions/0005-no-parameter-properties.md +0 -49
  58. package/docs/decisions/0006-publish-bundled-esm.md +0 -63
  59. package/docs/decisions/0007-active-run-binary-index.md +0 -54
  60. package/docs/decisions/0008-child-pi-warm-pool.md +0 -61
  61. package/docs/decisions/2026-07-21-broker-phase4-default-on.md +0 -77
  62. package/docs/decisions/2026-07-21-broker-windows-perms.md +0 -91
  63. package/docs/decisions/2026-07-22-broker-phase4-gated-on.md +0 -99
  64. package/docs/decisions/2026-07-24-oidc-trusted-publishing.md +0 -112
  65. package/docs/decisions/README.md +0 -26
  66. package/docs/deep-review-report.md +0 -384
  67. package/docs/distillation/cybersecurity-patterns.md +0 -294
  68. package/docs/feature-analysis-subagent4.md +0 -305
  69. package/docs/fix-plan-disabletools-exit-null.md +0 -219
  70. package/docs/fixes/BATCH_A_H1_H2.md +0 -86
  71. package/docs/fixes/bug-006-foreground-cancel-concurrent.md +0 -78
  72. package/docs/fixes/bug-007-async-notifier-stale-ctx.md +0 -112
  73. package/docs/fixes/bug-008-child-process-silent-timeout.md +0 -100
  74. package/docs/fixes/bug-009-executor-yield-limit-needs-attention.md +0 -75
  75. package/docs/fixes/bug-010-child-process-api-key-filtered.md +0 -109
  76. package/docs/fixes/bug-011-spawn-pi-enoent.md +0 -92
  77. package/docs/fixes/bug-012-essential-env-stripped.md +0 -89
  78. package/docs/fixes/bug-013-background-runner-death.md +0 -84
  79. package/docs/fixes/bug-014-infinite-retry-loop-needs-attention.md +0 -82
  80. package/docs/fixes/bug-015-background-runner-sigterm.md +0 -65
  81. package/docs/fixes/bug-017-background-runner-session-shutdown.md +0 -66
  82. package/docs/fixes/bug-017-background-runner-sigkill-double-fork.md +0 -28
  83. package/docs/fixes/bug-018-child-pi-worker-stdin-hang.md +0 -61
  84. package/docs/fixes/bug-019-phantom-runs-temp-workspace.md +0 -52
  85. package/docs/fixes/bug-020-infinite-retry-loop-needs-attention.md +0 -47
  86. package/docs/fixes/v0.9.10/locks-fix-verify.md +0 -3
  87. package/docs/fixes/v0.9.10/smoke-test.md +0 -12
  88. package/docs/followup-plan-2026-05-12.md +0 -463
  89. package/docs/followup-review-2026-05-12.md +0 -297
  90. package/docs/goals.md +0 -59
  91. package/docs/implementation-plan-top3.md +0 -333
  92. package/docs/issue-29-analysis.md +0 -189
  93. package/docs/live-mailbox-runtime.md +0 -36
  94. package/docs/migration/atomic-write-v2-migration.md +0 -299
  95. package/docs/next-upgrade-roadmap.md +0 -808
  96. package/docs/oh-my-pi-research.md +0 -509
  97. package/docs/optimization-plan-2026-07.md +0 -366
  98. package/docs/optimization-plan.md +0 -644
  99. package/docs/patterns/command-agent-skill.md +0 -71
  100. package/docs/perf/baseline-2026-05.md +0 -113
  101. package/docs/perf/final-report-2026-05.md +0 -206
  102. package/docs/perf/optimization-plan-2026-07-verified.md +0 -396
  103. package/docs/perf/performance-audit-report-2026-07.md +0 -948
  104. package/docs/perf/performance-review-2026-07.md +0 -287
  105. package/docs/perf/sprint-1-report.md +0 -71
  106. package/docs/perf/sprint-2-report.md +0 -81
  107. package/docs/perf/sprint-2.5-report.md +0 -53
  108. package/docs/perf/sprint-3-report.md +0 -36
  109. package/docs/perf/sprint-4-report.md +0 -47
  110. package/docs/perf/sprint-5-report.md +0 -51
  111. package/docs/perf/sprint-6-report.md +0 -94
  112. package/docs/perf/sprint-7-report.md +0 -74
  113. package/docs/perf/upgrade-plan-2026-05.md +0 -147
  114. package/docs/performance-optimization-execution-plan.md +0 -520
  115. package/docs/phase4-triage.md +0 -58
  116. package/docs/pi-crew-investigation-report.md +0 -411
  117. package/docs/product/README.md +0 -31
  118. package/docs/product/platform.md +0 -27
  119. package/docs/product/runtime-safety.md +0 -37
  120. package/docs/product/team-run.md +0 -39
  121. package/docs/product/team-tool.md +0 -37
  122. package/docs/runtime-analysis-child-vs-live.md +0 -171
  123. package/docs/runtime-migration-in-process-analysis.md +0 -250
  124. package/docs/skills/REFERENCE.md +0 -138
  125. package/docs/stories/README.md +0 -32
  126. package/docs/stories/US-DEPS-major-upgrade.md +0 -62
  127. package/docs/stories/backlog.md +0 -36
  128. package/docs/superpowers/plans/2026-06-09-fallow-patterns-adoption.md +0 -1270
  129. package/docs/templates/decision.md +0 -27
  130. package/docs/templates/story.md +0 -44
  131. package/docs/templates/validation-report.md +0 -32
  132. package/docs/ui-optimization-plan.md +0 -447
  133. package/src/adapters/claude-adapter.ts +0 -23
  134. package/src/adapters/codex-adapter.ts +0 -21
  135. package/src/adapters/cursor-adapter.ts +0 -17
  136. package/src/adapters/export-util.ts +0 -143
  137. package/src/adapters/index.ts +0 -15
  138. package/src/adapters/registry.ts +0 -18
  139. package/src/adapters/types.ts +0 -23
  140. package/src/agents/agent-config.ts +0 -194
  141. package/src/agents/agent-search.ts +0 -98
  142. package/src/agents/agent-serializer.ts +0 -38
  143. package/src/agents/discover-agents.ts +0 -623
  144. package/src/benchmark/benchmark-runner.ts +0 -313
  145. package/src/benchmark/feedback-loop.ts +0 -73
  146. package/src/config/config.ts +0 -1269
  147. package/src/config/defaults.ts +0 -193
  148. package/src/config/drift-detector.ts +0 -290
  149. package/src/config/markers.ts +0 -330
  150. package/src/config/resilient-parser.ts +0 -117
  151. package/src/config/role-tools.ts +0 -118
  152. package/src/config/suggestions.ts +0 -75
  153. package/src/config/types.ts +0 -278
  154. package/src/errors.ts +0 -194
  155. package/src/extension/action-suggestions.ts +0 -117
  156. package/src/extension/async-notifier.ts +0 -179
  157. package/src/extension/autonomous-policy.ts +0 -209
  158. package/src/extension/command-completions.ts +0 -120
  159. package/src/extension/context-status-injection.ts +0 -185
  160. package/src/extension/crew-autocomplete.ts +0 -131
  161. package/src/extension/crew-cleanup.ts +0 -168
  162. package/src/extension/crew-input-router.ts +0 -93
  163. package/src/extension/crew-shortcuts.ts +0 -87
  164. package/src/extension/crew-vibes/cat-frames.ts +0 -18
  165. package/src/extension/crew-vibes/config.ts +0 -195
  166. package/src/extension/crew-vibes/figures.ts +0 -101
  167. package/src/extension/crew-vibes/font-detect.ts +0 -71
  168. package/src/extension/crew-vibes/footer.ts +0 -292
  169. package/src/extension/crew-vibes/index.ts +0 -432
  170. package/src/extension/crew-vibes/provider-usage.ts +0 -396
  171. package/src/extension/crew-vibes/render.ts +0 -206
  172. package/src/extension/crew-vibes/speed.ts +0 -286
  173. package/src/extension/cross-extension-rpc.ts +0 -350
  174. package/src/extension/help.ts +0 -62
  175. package/src/extension/import-index.ts +0 -74
  176. package/src/extension/knowledge-injection.ts +0 -402
  177. package/src/extension/management.ts +0 -571
  178. package/src/extension/message-renderers.ts +0 -108
  179. package/src/extension/notification-router.ts +0 -152
  180. package/src/extension/notification-sink.ts +0 -54
  181. package/src/extension/pi-api.ts +0 -56
  182. package/src/extension/plan-orchestrate.ts +0 -302
  183. package/src/extension/project-init.ts +0 -170
  184. package/src/extension/register.ts +0 -113
  185. package/src/extension/registration/artifact-cleanup.ts +0 -20
  186. package/src/extension/registration/command-registration.ts +0 -58
  187. package/src/extension/registration/command-utils.ts +0 -58
  188. package/src/extension/registration/commands.ts +0 -1228
  189. package/src/extension/registration/compaction-guard.ts +0 -333
  190. package/src/extension/registration/context-builder.ts +0 -135
  191. package/src/extension/registration/crash-recovery-cache.ts +0 -54
  192. package/src/extension/registration/foreground-run-controller.ts +0 -238
  193. package/src/extension/registration/hook-registration.ts +0 -119
  194. package/src/extension/registration/lazy-configurers.ts +0 -124
  195. package/src/extension/registration/lifecycle-handlers.ts +0 -923
  196. package/src/extension/registration/lifecycle.ts +0 -259
  197. package/src/extension/registration/observability.ts +0 -322
  198. package/src/extension/registration/registration-types.ts +0 -158
  199. package/src/extension/registration/runtime-cleanup.ts +0 -230
  200. package/src/extension/registration/subagent-helpers.ts +0 -125
  201. package/src/extension/registration/subagent-manager-setup.ts +0 -330
  202. package/src/extension/registration/subagent-tools.ts +0 -456
  203. package/src/extension/registration/team-tool.ts +0 -220
  204. package/src/extension/registration/tool-registration.ts +0 -50
  205. package/src/extension/registration/ui.ts +0 -172
  206. package/src/extension/registration/viewers.ts +0 -121
  207. package/src/extension/registration/wire-cross-extension.ts +0 -37
  208. package/src/extension/result-watcher.ts +0 -139
  209. package/src/extension/rpc-hmac.ts +0 -256
  210. package/src/extension/run-bundle-schema.ts +0 -104
  211. package/src/extension/run-export.ts +0 -97
  212. package/src/extension/run-import.ts +0 -168
  213. package/src/extension/run-index.ts +0 -131
  214. package/src/extension/run-maintenance.ts +0 -190
  215. package/src/extension/session-summary.ts +0 -18
  216. package/src/extension/team-manager-command.ts +0 -153
  217. package/src/extension/team-onboard.ts +0 -177
  218. package/src/extension/team-recommendation.ts +0 -288
  219. package/src/extension/team-tool/anchor.ts +0 -172
  220. package/src/extension/team-tool/api.ts +0 -1244
  221. package/src/extension/team-tool/auto-summarize.ts +0 -142
  222. package/src/extension/team-tool/cache-control.ts +0 -19
  223. package/src/extension/team-tool/cancel.ts +0 -337
  224. package/src/extension/team-tool/chain-dispatch.ts +0 -95
  225. package/src/extension/team-tool/chain-executor.ts +0 -379
  226. package/src/extension/team-tool/config-patch.ts +0 -50
  227. package/src/extension/team-tool/context.ts +0 -149
  228. package/src/extension/team-tool/destructive-gate.ts +0 -52
  229. package/src/extension/team-tool/doctor.ts +0 -431
  230. package/src/extension/team-tool/explain.ts +0 -282
  231. package/src/extension/team-tool/failure-patterns.ts +0 -118
  232. package/src/extension/team-tool/goal-wrap.ts +0 -330
  233. package/src/extension/team-tool/goal.ts +0 -556
  234. package/src/extension/team-tool/handle-schedule.ts +0 -332
  235. package/src/extension/team-tool/handle-settings.ts +0 -520
  236. package/src/extension/team-tool/health-monitor.ts +0 -481
  237. package/src/extension/team-tool/inspect.ts +0 -88
  238. package/src/extension/team-tool/intent-policy.ts +0 -45
  239. package/src/extension/team-tool/lifecycle-actions.ts +0 -615
  240. package/src/extension/team-tool/orchestrate.ts +0 -98
  241. package/src/extension/team-tool/parallel-dispatch.ts +0 -183
  242. package/src/extension/team-tool/plan.ts +0 -50
  243. package/src/extension/team-tool/respond.ts +0 -150
  244. package/src/extension/team-tool/run-not-found.ts +0 -49
  245. package/src/extension/team-tool/run.ts +0 -1035
  246. package/src/extension/team-tool/status.ts +0 -260
  247. package/src/extension/team-tool/workflow-manage.ts +0 -261
  248. package/src/extension/team-tool-types.ts +0 -26
  249. package/src/extension/team-tool.ts +0 -1116
  250. package/src/extension/tool-result.ts +0 -16
  251. package/src/extension/validate-resources.ts +0 -108
  252. package/src/hooks/registry.ts +0 -200
  253. package/src/hooks/types.ts +0 -69
  254. package/src/i18n.ts +0 -212
  255. package/src/observability/correlation.ts +0 -50
  256. package/src/observability/event-bus.ts +0 -86
  257. package/src/observability/event-to-metric.ts +0 -170
  258. package/src/observability/exporters/adapter.ts +0 -27
  259. package/src/observability/exporters/otlp-exporter.ts +0 -356
  260. package/src/observability/exporters/prometheus-exporter.ts +0 -54
  261. package/src/observability/metric-registry.ts +0 -100
  262. package/src/observability/metric-retention.ts +0 -64
  263. package/src/observability/metric-sink.ts +0 -99
  264. package/src/observability/metrics-primitives.ts +0 -219
  265. package/src/plugins/plugin-define.ts +0 -6
  266. package/src/plugins/plugin-registry.ts +0 -32
  267. package/src/plugins/plugins/index.ts +0 -3
  268. package/src/plugins/plugins/nextjs.ts +0 -19
  269. package/src/plugins/plugins/vite.ts +0 -10
  270. package/src/plugins/plugins/vitest.ts +0 -9
  271. package/src/prompt/prompt-runtime.ts +0 -380
  272. package/src/runtime/adaptive-plan.ts +0 -563
  273. package/src/runtime/agent-control.ts +0 -215
  274. package/src/runtime/agent-memory.ts +0 -81
  275. package/src/runtime/agent-observability.ts +0 -122
  276. package/src/runtime/anchor-manager.ts +0 -475
  277. package/src/runtime/async-marker.ts +0 -32
  278. package/src/runtime/async-runner.ts +0 -381
  279. package/src/runtime/attention-events.ts +0 -28
  280. package/src/runtime/auto-summarize.ts +0 -344
  281. package/src/runtime/background-runner.ts +0 -837
  282. package/src/runtime/batch-barrier.ts +0 -147
  283. package/src/runtime/broker-issuer.ts +0 -37
  284. package/src/runtime/cancellation-token.ts +0 -99
  285. package/src/runtime/cancellation.ts +0 -99
  286. package/src/runtime/capability-inventory.ts +0 -137
  287. package/src/runtime/chain-parser.ts +0 -237
  288. package/src/runtime/chain-runner.ts +0 -606
  289. package/src/runtime/checkpoint.ts +0 -291
  290. package/src/runtime/child-pi-constants.ts +0 -42
  291. package/src/runtime/child-pi-kill.ts +0 -180
  292. package/src/runtime/child-pi-pool.ts +0 -68
  293. package/src/runtime/child-pi-spawn.ts +0 -287
  294. package/src/runtime/child-pi-steering.ts +0 -128
  295. package/src/runtime/child-pi-streams.ts +0 -296
  296. package/src/runtime/child-pi-transcript.ts +0 -169
  297. package/src/runtime/child-pi.ts +0 -1105
  298. package/src/runtime/coalesce-tasks.ts +0 -268
  299. package/src/runtime/code-summary.ts +0 -292
  300. package/src/runtime/command-trace.ts +0 -105
  301. package/src/runtime/compact-pipeline.ts +0 -56
  302. package/src/runtime/compact-stages/ansi-strip-stage.ts +0 -25
  303. package/src/runtime/compact-stages/blank-collapse-stage.ts +0 -31
  304. package/src/runtime/compact-stages/deduplicate-stage.ts +0 -34
  305. package/src/runtime/compact-stages/head-snap-stage.ts +0 -57
  306. package/src/runtime/compact-stages/index.ts +0 -23
  307. package/src/runtime/compact-stages/tail-capture-stage.ts +0 -77
  308. package/src/runtime/compact-stages/truncation-stage.ts +0 -74
  309. package/src/runtime/compaction-summary.ts +0 -278
  310. package/src/runtime/completion-guard.ts +0 -207
  311. package/src/runtime/concurrency.ts +0 -58
  312. package/src/runtime/crash-classification.ts +0 -229
  313. package/src/runtime/crash-recovery.ts +0 -577
  314. package/src/runtime/crew-agent-records.ts +0 -593
  315. package/src/runtime/crew-agent-runtime.ts +0 -62
  316. package/src/runtime/crew-broker-child.ts +0 -88
  317. package/src/runtime/crew-broker-client.ts +0 -673
  318. package/src/runtime/crew-broker-tokens.ts +0 -84
  319. package/src/runtime/crew-broker.ts +0 -1276
  320. package/src/runtime/crew-hooks.ts +0 -227
  321. package/src/runtime/cross-extension-rpc.ts +0 -143
  322. package/src/runtime/custom-tools/irc-tool.ts +0 -282
  323. package/src/runtime/custom-tools/submit-result-tool.ts +0 -102
  324. package/src/runtime/deadletter.ts +0 -47
  325. package/src/runtime/delivery-coordinator.ts +0 -211
  326. package/src/runtime/delta-conflict.ts +0 -348
  327. package/src/runtime/deterministic-ast.ts +0 -161
  328. package/src/runtime/diagnostic-export.ts +0 -171
  329. package/src/runtime/direct-run.ts +0 -45
  330. package/src/runtime/dwf-state-store.ts +0 -102
  331. package/src/runtime/dynamic-workflow-context.ts +0 -1016
  332. package/src/runtime/dynamic-workflow-runner.ts +0 -310
  333. package/src/runtime/effectiveness.ts +0 -90
  334. package/src/runtime/errors/crew-errors.ts +0 -162
  335. package/src/runtime/event-stream-bridge.ts +0 -98
  336. package/src/runtime/foreground-control.ts +0 -193
  337. package/src/runtime/foreground-watchdog.ts +0 -132
  338. package/src/runtime/global-worker-cap.ts +0 -96
  339. package/src/runtime/goal-achievement.ts +0 -148
  340. package/src/runtime/goal-evaluator.ts +0 -361
  341. package/src/runtime/goal-loop-runner.ts +0 -828
  342. package/src/runtime/goal-state-store.ts +0 -219
  343. package/src/runtime/green-contract.ts +0 -55
  344. package/src/runtime/group-join.ts +0 -267
  345. package/src/runtime/handoff-manager.ts +0 -598
  346. package/src/runtime/heartbeat-gradient.ts +0 -36
  347. package/src/runtime/heartbeat-watcher.ts +0 -239
  348. package/src/runtime/hidden-handoff.ts +0 -407
  349. package/src/runtime/important-line-classifier.ts +0 -140
  350. package/src/runtime/intercom-bridge.ts +0 -187
  351. package/src/runtime/iteration-hooks.ts +0 -305
  352. package/src/runtime/live-agent-control.ts +0 -136
  353. package/src/runtime/live-agent-manager.ts +0 -704
  354. package/src/runtime/live-control-realtime.ts +0 -56
  355. package/src/runtime/live-extension-bridge.ts +0 -148
  356. package/src/runtime/live-irc.ts +0 -97
  357. package/src/runtime/live-session-health.ts +0 -108
  358. package/src/runtime/live-session-runtime.ts +0 -1139
  359. package/src/runtime/loop-gates.ts +0 -128
  360. package/src/runtime/manifest-cache.ts +0 -375
  361. package/src/runtime/mcp-proxy.ts +0 -105
  362. package/src/runtime/metric-parser.ts +0 -36
  363. package/src/runtime/model-fallback.ts +0 -409
  364. package/src/runtime/model-resolver.ts +0 -126
  365. package/src/runtime/model-scope.ts +0 -158
  366. package/src/runtime/orphan-worker-registry.ts +0 -465
  367. package/src/runtime/output-validator.ts +0 -202
  368. package/src/runtime/overflow-recovery.ts +0 -206
  369. package/src/runtime/parallel-research.ts +0 -68
  370. package/src/runtime/parallel-utils.ts +0 -160
  371. package/src/runtime/parent-guard.ts +0 -134
  372. package/src/runtime/path-overlap.ts +0 -150
  373. package/src/runtime/peer-dep.ts +0 -292
  374. package/src/runtime/per-write-validator.ts +0 -181
  375. package/src/runtime/phase-progress.ts +0 -217
  376. package/src/runtime/phase-tracker.ts +0 -385
  377. package/src/runtime/pi-args.ts +0 -655
  378. package/src/runtime/pi-json-output.ts +0 -170
  379. package/src/runtime/pi-spawn.ts +0 -273
  380. package/src/runtime/pipeline-runner.ts +0 -523
  381. package/src/runtime/plan-templates.ts +0 -201
  382. package/src/runtime/policy-engine.ts +0 -115
  383. package/src/runtime/post-checks.ts +0 -142
  384. package/src/runtime/post-exit-stdio-guard.ts +0 -104
  385. package/src/runtime/process-lifecycle.ts +0 -491
  386. package/src/runtime/process-status.ts +0 -154
  387. package/src/runtime/progress-event-coalescer.ts +0 -44
  388. package/src/runtime/progress-tracker.ts +0 -124
  389. package/src/runtime/prose-compressor.ts +0 -162
  390. package/src/runtime/recovery-recipes.ts +0 -203
  391. package/src/runtime/replace.ts +0 -570
  392. package/src/runtime/resilient-edit.ts +0 -153
  393. package/src/runtime/result-extractor.ts +0 -213
  394. package/src/runtime/retry-executor.ts +0 -100
  395. package/src/runtime/retry-runner.ts +0 -336
  396. package/src/runtime/role-permission.ts +0 -59
  397. package/src/runtime/run-coalesced-task-group.ts +0 -308
  398. package/src/runtime/run-drift.ts +0 -219
  399. package/src/runtime/run-tracker.ts +0 -116
  400. package/src/runtime/runtime-policy.ts +0 -29
  401. package/src/runtime/runtime-resolver.ts +0 -172
  402. package/src/runtime/runtime-warmup.ts +0 -160
  403. package/src/runtime/scheduler.ts +0 -353
  404. package/src/runtime/semaphore.ts +0 -141
  405. package/src/runtime/sensitive-paths.ts +0 -93
  406. package/src/runtime/session-resources.ts +0 -25
  407. package/src/runtime/session-snapshot.ts +0 -59
  408. package/src/runtime/session-usage.ts +0 -79
  409. package/src/runtime/settings-store.ts +0 -155
  410. package/src/runtime/sidechain-output.ts +0 -35
  411. package/src/runtime/single-agent-compose.ts +0 -84
  412. package/src/runtime/skill-effectiveness.ts +0 -524
  413. package/src/runtime/skill-instructions.ts +0 -391
  414. package/src/runtime/stale-reconciler.ts +0 -680
  415. package/src/runtime/stream-preview.ts +0 -184
  416. package/src/runtime/streaming-output.ts +0 -50
  417. package/src/runtime/subagent-manager.ts +0 -561
  418. package/src/runtime/subprocess-tool-registry.ts +0 -70
  419. package/src/runtime/supervisor-contact.ts +0 -64
  420. package/src/runtime/task-display.ts +0 -52
  421. package/src/runtime/task-graph-scheduler.ts +0 -227
  422. package/src/runtime/task-graph.ts +0 -290
  423. package/src/runtime/task-health.ts +0 -83
  424. package/src/runtime/task-id.ts +0 -161
  425. package/src/runtime/task-output-context.ts +0 -602
  426. package/src/runtime/task-packet.ts +0 -268
  427. package/src/runtime/task-quality.ts +0 -199
  428. package/src/runtime/task-runner/capabilities.ts +0 -78
  429. package/src/runtime/task-runner/context-retrieval.ts +0 -163
  430. package/src/runtime/task-runner/live-executor.ts +0 -227
  431. package/src/runtime/task-runner/output-splitter.ts +0 -152
  432. package/src/runtime/task-runner/progress.ts +0 -157
  433. package/src/runtime/task-runner/prompt-builder.ts +0 -278
  434. package/src/runtime/task-runner/prompt-pipeline.ts +0 -88
  435. package/src/runtime/task-runner/result-utils.ts +0 -16
  436. package/src/runtime/task-runner/retrieval-orchestrator.ts +0 -310
  437. package/src/runtime/task-runner/run-projection.ts +0 -125
  438. package/src/runtime/task-runner/state-helpers.ts +0 -176
  439. package/src/runtime/task-runner/tail-read.ts +0 -34
  440. package/src/runtime/task-runner.ts +0 -1420
  441. package/src/runtime/team-runner-artifacts.ts +0 -13
  442. package/src/runtime/team-runner.ts +0 -1993
  443. package/src/runtime/tool-output-pruner.ts +0 -333
  444. package/src/runtime/tool-progress.ts +0 -278
  445. package/src/runtime/usage-tracker.ts +0 -73
  446. package/src/runtime/verification-gates.ts +0 -571
  447. package/src/runtime/verification-integrity.ts +0 -105
  448. package/src/runtime/verification-worktree.ts +0 -186
  449. package/src/runtime/worker-heartbeat.ts +0 -25
  450. package/src/runtime/worker-startup.ts +0 -83
  451. package/src/runtime/workflow-state.ts +0 -195
  452. package/src/runtime/workspace-lock.ts +0 -443
  453. package/src/runtime/workspace-tree.ts +0 -312
  454. package/src/runtime/yield-handler.ts +0 -224
  455. package/src/runtime/zombie-scanner.ts +0 -298
  456. package/src/schema/config-schema.ts +0 -327
  457. package/src/schema/team-tool-schema.ts +0 -415
  458. package/src/schema/validation-types.ts +0 -159
  459. package/src/skills/discover-skills.ts +0 -197
  460. package/src/skills/skill-templates.ts +0 -456
  461. package/src/skills/validate.ts +0 -285
  462. package/src/state/active-run-registry.ts +0 -439
  463. package/src/state/artifact-store.ts +0 -176
  464. package/src/state/atomic-write.ts +0 -903
  465. package/src/state/blob-store.ts +0 -308
  466. package/src/state/contracts.ts +0 -158
  467. package/src/state/crew-init.ts +0 -269
  468. package/src/state/decision-ledger.ts +0 -345
  469. package/src/state/event-log-rotation.ts +0 -342
  470. package/src/state/event-log.ts +0 -1311
  471. package/src/state/event-reconstructor.ts +0 -229
  472. package/src/state/gitignore-manager.ts +0 -46
  473. package/src/state/health-store.ts +0 -79
  474. package/src/state/hook-instinct-bridge.ts +0 -94
  475. package/src/state/hook-integrations.ts +0 -51
  476. package/src/state/instinct-store.ts +0 -275
  477. package/src/state/jsonl-writer.ts +0 -107
  478. package/src/state/locks.ts +0 -562
  479. package/src/state/mailbox.ts +0 -916
  480. package/src/state/observation-store.ts +0 -176
  481. package/src/state/run-cache.ts +0 -193
  482. package/src/state/run-graph.ts +0 -183
  483. package/src/state/run-metrics.ts +0 -165
  484. package/src/state/schedule.ts +0 -166
  485. package/src/state/session-state-map.ts +0 -51
  486. package/src/state/state-store.ts +0 -958
  487. package/src/state/task-claims.ts +0 -61
  488. package/src/state/tiered-eval.ts +0 -480
  489. package/src/state/types-eval.ts +0 -58
  490. package/src/state/types.ts +0 -440
  491. package/src/state/usage.ts +0 -138
  492. package/src/state/worker-atomic-writer.ts +0 -194
  493. package/src/subagents/async-entry.ts +0 -1
  494. package/src/subagents/index.ts +0 -3
  495. package/src/subagents/live/control.ts +0 -1
  496. package/src/subagents/live/manager.ts +0 -1
  497. package/src/subagents/live/realtime.ts +0 -1
  498. package/src/subagents/live/session-runtime.ts +0 -1
  499. package/src/subagents/manager.ts +0 -1
  500. package/src/subagents/spawn.ts +0 -1
  501. package/src/teams/discover-teams.ts +0 -187
  502. package/src/teams/team-config.ts +0 -27
  503. package/src/teams/team-serializer.ts +0 -38
  504. package/src/tools/safe-bash-extension.ts +0 -54
  505. package/src/tools/safe-bash.ts +0 -505
  506. package/src/types/diff.d.ts +0 -18
  507. package/src/types/new-api-types.ts +0 -35
  508. package/src/ui/agent-management-overlay.ts +0 -160
  509. package/src/ui/card-colors.ts +0 -139
  510. package/src/ui/crew-footer.ts +0 -102
  511. package/src/ui/crew-select-list.ts +0 -111
  512. package/src/ui/dashboard-panes/agents-pane.ts +0 -164
  513. package/src/ui/dashboard-panes/cancellation-pane.ts +0 -66
  514. package/src/ui/dashboard-panes/capability-pane.ts +0 -77
  515. package/src/ui/dashboard-panes/health-pane.ts +0 -31
  516. package/src/ui/dashboard-panes/mailbox-pane.ts +0 -35
  517. package/src/ui/dashboard-panes/metrics-pane.ts +0 -37
  518. package/src/ui/dashboard-panes/progress-pane.ts +0 -38
  519. package/src/ui/dashboard-panes/transcript-pane.ts +0 -10
  520. package/src/ui/deploy-bundled-themes.ts +0 -71
  521. package/src/ui/dwf-phase-display.ts +0 -151
  522. package/src/ui/dynamic-border.ts +0 -35
  523. package/src/ui/heartbeat-aggregator.ts +0 -74
  524. package/src/ui/key-utils.ts +0 -42
  525. package/src/ui/keybinding-map.ts +0 -248
  526. package/src/ui/layout-primitives.ts +0 -106
  527. package/src/ui/live-conversation-overlay.ts +0 -183
  528. package/src/ui/live-duration.ts +0 -55
  529. package/src/ui/live-run-sidebar.ts +0 -301
  530. package/src/ui/loaders.ts +0 -176
  531. package/src/ui/mascot.ts +0 -436
  532. package/src/ui/overlays/agent-picker-overlay.ts +0 -65
  533. package/src/ui/overlays/confirm-overlay.ts +0 -60
  534. package/src/ui/overlays/help-overlay.ts +0 -177
  535. package/src/ui/overlays/mailbox-compose-overlay.ts +0 -181
  536. package/src/ui/overlays/mailbox-compose-preview.ts +0 -78
  537. package/src/ui/overlays/mailbox-detail-overlay.ts +0 -154
  538. package/src/ui/pi-ui-compat.ts +0 -68
  539. package/src/ui/powerbar-publisher.ts +0 -459
  540. package/src/ui/render-coalescer.ts +0 -101
  541. package/src/ui/render-diff.ts +0 -160
  542. package/src/ui/render-scheduler.ts +0 -224
  543. package/src/ui/run-action-dispatcher.ts +0 -182
  544. package/src/ui/run-dashboard.ts +0 -913
  545. package/src/ui/run-event-bus.ts +0 -381
  546. package/src/ui/run-snapshot-cache.ts +0 -1057
  547. package/src/ui/settings-overlay.ts +0 -1067
  548. package/src/ui/snapshot-types.ts +0 -90
  549. package/src/ui/spinner.ts +0 -17
  550. package/src/ui/status-colors.ts +0 -143
  551. package/src/ui/syntax-highlight.ts +0 -136
  552. package/src/ui/terminal-status.ts +0 -269
  553. package/src/ui/theme-adapter.ts +0 -271
  554. package/src/ui/theme-discovery.ts +0 -199
  555. package/src/ui/tool-progress-formatter.ts +0 -93
  556. package/src/ui/tool-render.ts +0 -453
  557. package/src/ui/tool-renderers/brief-mode.ts +0 -225
  558. package/src/ui/tool-renderers/index.ts +0 -726
  559. package/src/ui/transcript-cache.ts +0 -128
  560. package/src/ui/transcript-entries.ts +0 -256
  561. package/src/ui/transcript-viewer.ts +0 -424
  562. package/src/ui/widget/index.ts +0 -357
  563. package/src/ui/widget/widget-formatters.ts +0 -182
  564. package/src/ui/widget/widget-model.ts +0 -110
  565. package/src/ui/widget/widget-renderer.ts +0 -182
  566. package/src/ui/widget/widget-types.ts +0 -36
  567. package/src/utils/bm25-search.ts +0 -235
  568. package/src/utils/completion-dedupe.ts +0 -63
  569. package/src/utils/conflict-detect.ts +0 -668
  570. package/src/utils/env-allowlist.ts +0 -30
  571. package/src/utils/env-filter.ts +0 -155
  572. package/src/utils/file-coalescer.ts +0 -98
  573. package/src/utils/fingerprint.ts +0 -180
  574. package/src/utils/frontmatter.ts +0 -70
  575. package/src/utils/fs-watch.ts +0 -37
  576. package/src/utils/gh-protocol.ts +0 -556
  577. package/src/utils/git.ts +0 -260
  578. package/src/utils/guards.ts +0 -107
  579. package/src/utils/ids.ts +0 -24
  580. package/src/utils/incremental-reader.ts +0 -220
  581. package/src/utils/internal-error.ts +0 -10
  582. package/src/utils/names.ts +0 -36
  583. package/src/utils/ndjson.ts +0 -115
  584. package/src/utils/paths.ts +0 -227
  585. package/src/utils/project-detector.ts +0 -160
  586. package/src/utils/redaction.ts +0 -347
  587. package/src/utils/resolve-shell.ts +0 -36
  588. package/src/utils/run-watcher-registry.ts +0 -152
  589. package/src/utils/safe-paths.ts +0 -383
  590. package/src/utils/scan-cache.ts +0 -144
  591. package/src/utils/session-utils.ts +0 -110
  592. package/src/utils/sleep.ts +0 -54
  593. package/src/utils/socket-path.ts +0 -127
  594. package/src/utils/sse-parser.ts +0 -129
  595. package/src/utils/task-name-generator.ts +0 -337
  596. package/src/utils/timings.ts +0 -33
  597. package/src/utils/token-counter.ts +0 -200
  598. package/src/utils/visual.ts +0 -207
  599. package/src/workflows/cost-estimator.ts +0 -34
  600. package/src/workflows/discover-workflows.ts +0 -290
  601. package/src/workflows/intermediate-store.ts +0 -166
  602. package/src/workflows/preflight-validator.ts +0 -168
  603. package/src/workflows/topology-analyzer.ts +0 -203
  604. package/src/workflows/validate-workflow.ts +0 -50
  605. package/src/workflows/workflow-config.ts +0 -70
  606. package/src/workflows/workflow-serializer.ts +0 -32
  607. package/src/worktree/branch-freshness.ts +0 -112
  608. package/src/worktree/cleanup.ts +0 -341
  609. package/src/worktree/worktree-manager.ts +0 -1200
@@ -1,1993 +0,0 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import type { AgentConfig } from "../agents/agent-config.ts";
4
- import type { CrewLimitsConfig, CrewReliabilityConfig, CrewRuntimeConfig } from "../config/config.ts";
5
- import { CrewError, ErrorCode } from "../errors.ts";
6
- import { appendHookEvent, executeHook } from "../hooks/registry.ts";
7
- import { childCorrelation, withCorrelation } from "../observability/correlation.ts";
8
- import type { MetricRegistry } from "../observability/metric-registry.ts";
9
- import { PluginRegistry } from "../plugins/plugin-registry.ts";
10
- import { NextJsPlugin, VitePlugin, VitestPlugin } from "../plugins/plugins/index.ts";
11
- import { hashArtifactContent as hashContent, writeArtifact } from "../state/artifact-store.ts";
12
- import { atomicWriteFile, flushPendingAtomicWrites } from "../state/atomic-write.ts";
13
- import { appendEvent, appendEventAsync, appendEventBuffered, appendEventFireAndForget, flushEventLogBuffer } from "../state/event-log.ts";
14
- import { HealthStore } from "../state/health-store.ts";
15
- import { withRunLock } from "../state/locks.ts";
16
- import { loadRunManifestById, saveRunManifestAsync, saveRunTasksAsync, updateRunStatus } from "../state/state-store.ts";
17
- import type { ArtifactDescriptor, PolicyDecision, TaskAttemptState, TeamRunManifest, TeamTaskState } from "../state/types.ts";
18
- import { aggregateUsage, formatTokens, formatUsage } from "../state/usage.ts";
19
- import type { TeamConfig } from "../teams/team-config.ts";
20
- import { logInternalError } from "../utils/internal-error.ts";
21
- import type { WorkflowConfig, WorkflowStep } from "../workflows/workflow-config.ts";
22
- import { checkBranchFreshness } from "../worktree/branch-freshness.ts";
23
- import { buildSyntheticTerminalEvidence, CrewCancellationError, cancellationReasonFromSignal } from "./cancellation.ts";
24
- import { buildDispatchUnits, type DispatchUnit, planCoalescedGroups } from "./coalesce-tasks.ts";
25
- import { resolveBatchConcurrency } from "./concurrency.ts";
26
- import { readCrewAgents, saveCrewAgents } from "./crew-agent-records.ts";
27
- import type { CrewRuntimeKind } from "./crew-agent-runtime.ts";
28
- import { crewHooks } from "./crew-hooks.ts";
29
- import { appendDeadletter } from "./deadletter.ts";
30
- import { effectivenessPolicyDecision, evaluateRunEffectiveness, formatRunEffectivenessLines } from "./effectiveness.ts";
31
- import { applyGoalAchievement, assessGoalAchievement } from "./goal-achievement.ts";
32
- import { deliverGroupJoin, resolveGroupJoinMode } from "./group-join.ts";
33
- import { terminateLiveAgentsForRun } from "./live-agent-manager.ts";
34
- import { filterReadyByWriteOverlap } from "./path-overlap.ts";
35
- import { evaluateCrewPolicy, summarizePolicyDecisions } from "./policy-engine.ts";
36
- import { buildRecoveryLedger, shouldRerunFailedTask } from "./recovery-recipes.ts";
37
- import { DEFAULT_RETRY_POLICY, executeWithRetry, type RetryPolicy } from "./retry-executor.ts";
38
- import { permissionForRole } from "./role-permission.ts";
39
- import { runCoalescedTaskGroup } from "./run-coalesced-task-group.ts";
40
- import { registerRunPromise, rejectRunPromise, resolveRunPromise } from "./run-tracker.ts";
41
- import { resolveTaskRuntimeKind } from "./runtime-policy.ts";
42
- import type { CrewRuntimeCapabilities } from "./runtime-resolver.ts";
43
- import { recordsForMaterializedTasks } from "./task-display.ts";
44
- import { buildExecutionPlan as buildDagExecutionPlan, getReadyTasks as getDagReadyTasks, type TaskNode } from "./task-graph.ts";
45
- import { buildTaskGraphIndex, refreshTaskGraphQueues, taskGraphSnapshot } from "./task-graph-scheduler.ts";
46
- import { aggregateTaskOutputs } from "./task-output-context.ts";
47
- import { clearStablePrefixCache, computeStablePrefixComponents } from "./task-runner/prompt-builder.ts";
48
- import { runTeamTask } from "./task-runner.ts";
49
- import { mergeArtifacts } from "./team-runner-artifacts.ts";
50
- import { clearTrackedTaskUsage } from "./usage-tracker.ts";
51
- import {
52
- createWorkflowStateMachine,
53
- type PhaseGuardContext,
54
- type PhaseState,
55
- transitionPhase,
56
- validatePhasePreconditions,
57
- } from "./workflow-state.ts";
58
-
59
- // Built-in plugin registry for framework awareness.
60
- // NOTE: This registry is registered here for future use. The integration
61
- // point (reading package.json deps, computing active plugin context, and
62
- // passing it into RunConfig / task-runner) is not yet implemented; see
63
- // `getPluginContext` in src/plugins/plugin-context.ts (planned).
64
- const builtInRegistry = new PluginRegistry();
65
- builtInRegistry.register(NextJsPlugin);
66
- builtInRegistry.register(VitestPlugin);
67
- builtInRegistry.register(VitePlugin);
68
-
69
- /**
70
- * Start a periodic heartbeat for the team-level run.
71
- *
72
- * The stale reconciler (src/runtime/stale-reconciler.ts) marks runs as failed
73
- * if their heartbeat is older than `NO_PID_HEARTBEAT_STALE_MS` (5 minutes).
74
- * Without this, long-running team runs (e.g. multi-phase workflows) get
75
- * cancelled by the reconciler as "stale" even when they are actively
76
- * executing. The team-runner has no periodic heartbeat today, so any
77
- * team run lasting >5min is at risk.
78
- */
79
- function startTeamRunHeartbeat(stateRoot: string, runId: string): () => void {
80
- const heartbeatPath = path.join(stateRoot, "heartbeat.json");
81
- const writeHeartbeat = (): void => {
82
- try {
83
- // lastTaskUpdateAt is written fresh on each tick so the heartbeat
84
- // never carries a stale creation-time timestamp. Previously this
85
- // captured manifest.updatedAt once at startup, making the value
86
- // permanently stale throughout the run.
87
- const now = new Date().toISOString();
88
- atomicWriteFile(
89
- heartbeatPath,
90
- JSON.stringify({
91
- pid: process.pid,
92
- at: Date.now(),
93
- runId,
94
- kind: "team-runner",
95
- lastTaskUpdateAt: now,
96
- }),
97
- { mode: 0o600 },
98
- );
99
- } catch {
100
- // best-effort
101
- }
102
- };
103
- writeHeartbeat();
104
- // NOTE: This interval is deliberately NOT unref'd. Unlike background-runner's
105
- // heartbeat and interrupt guard (both unref'd), the team heartbeat must keep
106
- // the event loop alive so the stale reconciler does not cancel long-running
107
- // team runs (>5 min) as "stale" while they are actively executing.
108
- // P13 (perf): tick every 60s instead of 30s. The stale-reconciler threshold
109
- // is 5min (300_000ms in crash-recovery.ts), so a 60s heartbeat still leaves
110
- // 5 ticks of slack before a run is misidentified as stale. Cuts the per-run
111
- // heartbeat.syscall count in half (1 write/30s → 1 write/60s) with no
112
- // behavioral change.
113
- const interval = setInterval(writeHeartbeat, 60_000);
114
- return () => clearInterval(interval);
115
- }
116
-
117
- export interface ExecuteTeamRunInput {
118
- manifest: TeamRunManifest;
119
- tasks: TeamTaskState[];
120
- team: TeamConfig;
121
- workflow: WorkflowConfig;
122
- agents: AgentConfig[];
123
- executeWorkers: boolean;
124
- limits?: CrewLimitsConfig;
125
- runtime?: CrewRuntimeCapabilities;
126
- runtimeConfig?: CrewRuntimeConfig;
127
- parentContext?: string;
128
- parentModel?: unknown;
129
- modelRegistry?: unknown;
130
- modelOverride?: string;
131
- signal?: AbortSignal;
132
- reliability?: CrewReliabilityConfig;
133
- metricRegistry?: MetricRegistry;
134
- /** Skill override from the team tool. false disables skill injection for this run. */
135
- skillOverride?: string[] | false;
136
- /** Optional callback for JSON events from child Pi. Used for overflow recovery tracking. */
137
- onJsonEvent?: (taskId: string, runId: string, event: unknown) => void;
138
- /** Workspace where this run was initiated — used for session-scoped live-agent visibility. */
139
- workspaceId: string;
140
- /** Total token budget for the run. When set, enables per-task budget enforcement. */
141
- budgetTotal?: number;
142
- /** Budget warning threshold as a fraction (0-1). Default: 0.8 (80%). */
143
- budgetWarning?: number;
144
- /** Budget abort threshold as a fraction (0-1). Default: 0.95 (95%). */
145
- budgetAbort?: number;
146
- /** When true, skip budget enforcement entirely. */
147
- budgetUnlimited?: boolean;
148
- }
149
-
150
- /**
151
- * Result of a per-task budget check against cumulative run usage.
152
- */
153
- export interface PerTaskBudgetCheckResult {
154
- /** Whether the abort threshold was exceeded. */
155
- abort: boolean;
156
- /** Whether the warning threshold was exceeded. */
157
- warning: boolean;
158
- /** IDs of tasks that exceeded their fair share (>50% of remaining budget). */
159
- fairShareViolators: string[];
160
- /** Total tokens used so far. */
161
- totalUsed: number;
162
- }
163
-
164
- /**
165
- * Check cumulative token usage against per-task budget thresholds.
166
- * Returns a structured result — callers decide how to act (warn vs abort).
167
- *
168
- * Exported for unit testing.
169
- */
170
- export function checkPerTaskBudget(
171
- tasks: TeamTaskState[],
172
- budgetTotal: number,
173
- budgetWarning: number,
174
- budgetAbort: number,
175
- fairShareFraction = 0.5,
176
- ): PerTaskBudgetCheckResult {
177
- const usage = aggregateUsage(tasks);
178
- const totalUsed = (usage?.input ?? 0) + (usage?.output ?? 0) + (usage?.cacheWrite ?? 0);
179
- const abort = totalUsed >= budgetAbort * budgetTotal;
180
- const warning = !abort && totalUsed >= budgetWarning * budgetTotal;
181
- // Fair share threshold based on TOTAL budget, not remaining budget.
182
- // This ensures a task that consumed 60% of total budget is flagged even
183
- // if only 40% remains (40% * 50% = 20% threshold would miss the 60% usage).
184
- const fairShareThreshold = budgetTotal * fairShareFraction;
185
- const fairShareViolators: string[] = [];
186
- for (const task of tasks) {
187
- if (!task.usage) continue;
188
- const taskTotal = (task.usage.input ?? 0) + (task.usage.output ?? 0) + (task.usage.cacheWrite ?? 0);
189
- // Only flag tasks that individually consumed a significant portion of the
190
- // budget (>10% of total) AND exceeded the fair share threshold.
191
- if (fairShareThreshold > 0 && taskTotal > fairShareThreshold && taskTotal > budgetTotal * 0.1) {
192
- fairShareViolators.push(task.id);
193
- }
194
- }
195
- return { abort, warning, fairShareViolators, totalUsed };
196
- }
197
-
198
- function findStep(workflow: WorkflowConfig, task: TeamTaskState): WorkflowStep {
199
- const step = workflow.steps.find((candidate) => candidate.id === task.stepId);
200
- if (!step)
201
- throw new CrewError(ErrorCode.ResourceNotFound, `Workflow step '${task.stepId}' not found for task '${task.id}'.`).withContext(
202
- `workflow step lookup (task=${task.id})`,
203
- );
204
- return step;
205
- }
206
-
207
- function findAgent(agents: AgentConfig[], task: TeamTaskState): AgentConfig {
208
- const agent = agents.find((candidate) => candidate.name === task.agent);
209
- if (!agent)
210
- throw new CrewError(ErrorCode.ResourceNotFound, `Agent '${task.agent}' not found for task '${task.id}'.`).withContext(
211
- `agent lookup (task=${task.id})`,
212
- );
213
- return agent;
214
- }
215
-
216
- function markBlocked(tasks: TeamTaskState[], reason: string): TeamTaskState[] {
217
- return tasks.map((task) =>
218
- task.status === "queued"
219
- ? {
220
- ...task,
221
- status: "skipped",
222
- error: reason,
223
- finishedAt: new Date().toISOString(),
224
- graph: task.graph ? { ...task.graph, queue: "blocked" } : undefined,
225
- }
226
- : task,
227
- );
228
- }
229
-
230
- function isNonTerminalTaskStatus(status: TeamTaskState["status"]): boolean {
231
- return status === "queued" || status === "running" || status === "waiting";
232
- }
233
-
234
- /**
235
- * Returns the finishedAt timestamp as a number, or Infinity for invalid/malformed dates.
236
- * This makes comparison logic in shouldMergeTaskUpdate more readable by abstracting
237
- * the NaN handling into a single well-named function.
238
- */
239
- function safeFinishedAt(task: TeamTaskState): number {
240
- if (!task.finishedAt) return -Infinity;
241
- const ms = new Date(task.finishedAt).getTime();
242
- return Number.isNaN(ms) ? Infinity : ms;
243
- }
244
-
245
- /**
246
- * Returns true when the current task has a malformed finishedAt (NaN/Infinity)
247
- * and the updated task has a valid finite finishedAt. Malformed finishedAt
248
- * should be replaced rather than persisting corruption.
249
- */
250
- function isMalformedFinishedAtReplacement(currentTime: number, updatedTime: number): boolean {
251
- return !Number.isFinite(currentTime) && Number.isFinite(updatedTime);
252
- }
253
-
254
- function shouldMergeTaskUpdate(current: TeamTaskState, updated: TeamTaskState): boolean {
255
- // Parallel workers receive the same input snapshot. A later result may still
256
- // contain stale queued/running copies of tasks that another worker already
257
- // completed. Never let those stale snapshots regress durable task state.
258
- if (current.status === "waiting" && updated.status === "running") return false;
259
- // Block terminal→non-terminal transitions (e.g. completed→running).
260
- // A task that has reached a terminal state must not be resurrected.
261
- const currentIsTerminal = !isNonTerminalTaskStatus(current.status);
262
- const updatedIsNonTerminal = isNonTerminalTaskStatus(updated.status);
263
- if (currentIsTerminal && updatedIsNonTerminal) return false;
264
- // Explicitly block completed↔needs_attention terminal-to-terminal transitions.
265
- // Both are success terminal states used interchangeably; stale worker updates must
266
- // not cause a completed task to appear as needs_attention or vice versa.
267
- if (current.status === "completed" && updated.status === "needs_attention") return false;
268
- if (current.status === "needs_attention" && updated.status === "completed") return false;
269
- // Explicitly block failed→completed resurrection. Both statuses are terminal,
270
- // but completed is the success terminal state and should not be reachable from
271
- // failed via a stale merge. The check above only guards non-terminal→terminal.
272
- if (current.status === "failed" && updated.status === "completed") return false;
273
- // Mirror that guard for the other dangerous terminal→terminal flips (reverse
274
- // audit CANCEL-3 + F3): a cancelled task must not be resurrected to completed,
275
- // and a completed task must not be demoted to failed, by a stale worker merge.
276
- // The task transition table (contracts.ts) only permits terminal→queued
277
- // (retry); these flips are always stale results. A worker that completed after
278
- // the task was cancelled, or a stale failed result arriving after completion,
279
- // must not flip a settled terminal status.
280
- if (current.status === "cancelled" && updated.status === "completed") return false;
281
- if (current.status === "completed" && updated.status === "failed") return false;
282
- // Guard: when current is "running" but has resultArtifact (another worker already
283
- // completed it), a stale updated with status="running" and no resultArtifact
284
- // must not overwrite the actual completed state.
285
- if (current.status === updated.status && updated.status === "running" && current.resultArtifact && !updated.resultArtifact)
286
- return false;
287
- // Guard: when current is "completed" and has resultArtifact but updated is also
288
- // "completed" without resultArtifact, block the stale update from overwriting
289
- // a task that successfully produced output.
290
- if (current.status === updated.status && current.status === "completed" && current.resultArtifact && !updated.resultArtifact)
291
- return false;
292
- // Prevent a stale completed task from overwriting a fresher one.
293
- // Restructure to handle undefined current.finishedAt as a special case:
294
- // - undefined current + valid updated: allow the update
295
- // - valid current + undefined updated: block the update (don't lose completion time)
296
- // - both undefined: finishedAt guard does not apply, fall through to heartbeat check
297
- // - both valid: compare timestamps as before
298
- if (current.finishedAt !== undefined && updated.finishedAt !== undefined) {
299
- const currentTime = safeFinishedAt(current);
300
- const updatedTime = safeFinishedAt(updated);
301
- // Malformed finishedAt (NaN) is treated as Infinity — invalid state should be
302
- // replaced rather than persisting corruption. Log warning for visibility.
303
- if (!Number.isFinite(currentTime)) {
304
- console.warn(`[team-runner] Task ${current.id} has malformed finishedAt: ${current.finishedAt}`);
305
- }
306
- if (isMalformedFinishedAtReplacement(currentTime, updatedTime)) {
307
- return true;
308
- }
309
- if (updatedTime < currentTime) return false;
310
- }
311
- // Block if updated is trying to establish a terminal status without a finishedAt
312
- // timestamp. Heartbeat-only updates (status='running', no finishedAt) are
313
- // allowed if heartbeat has changed (checked separately in hasMeaningfulUpdate).
314
- if (!updated.finishedAt && !isNonTerminalTaskStatus(updated.status)) return false;
315
- // Explicitly enumerate all fields that constitute a meaningful update so that
316
- // adding a new important field requires updating this list (rather than silently
317
- // losing data if a field is forgotten in the boolean OR chain below).
318
- const hasMeaningfulUpdate =
319
- updated.status !== current.status ||
320
- updated.finishedAt !== current.finishedAt ||
321
- updated.startedAt !== current.startedAt ||
322
- Boolean(updated.resultArtifact) !== Boolean(current.resultArtifact) ||
323
- (Boolean(updated.resultArtifact) && updated.resultArtifact !== current.resultArtifact) ||
324
- Boolean(updated.error) ||
325
- Boolean(updated.modelAttempts?.length) ||
326
- Boolean(updated.usage) ||
327
- Boolean(updated.attempts?.length) ||
328
- updated.heartbeat?.lastSeenAt !== current.heartbeat?.lastSeenAt ||
329
- updated.jsonEvents !== current.jsonEvents ||
330
- updated.agentProgress?.lastActivityAt !== current.agentProgress?.lastActivityAt;
331
- return hasMeaningfulUpdate;
332
- }
333
-
334
- // H4 fix: rename to descriptive name. Kept __test__ as alias for backward
335
- // compat test imports.
336
- // FIX (perf P10): replace O(N×M) .find() + .map() inside nested loops with a
337
- // single-pass Map-based merge. Build an index of `merged` once, then for each
338
- // incoming updated task do O(1) lookup; the final pass reassembles `merged`
339
- // preserving original order. For a 20-task run × 5-batch merger with
340
- // ~10 updates per result, this reduces from O(50×20) = 1000 ops to O(120).
341
- // Behavior is unchanged: skipped updates (shouldMergeTaskUpdate=false) still
342
- // leave the existing task in place.
343
- export function mergeTaskUpdatesPreservingTerminal(base: TeamTaskState[], results: Array<{ tasks: TeamTaskState[] }>): TeamTaskState[] {
344
- // Index current merged state by id for O(1) lookup during the merge pass.
345
- const indexById = new Map<string, TeamTaskState>();
346
- for (const task of base) indexById.set(task.id, task);
347
-
348
- let skipped = 0;
349
- for (const result of results) {
350
- for (const updated of result.tasks) {
351
- const current = indexById.get(updated.id);
352
- if (!current) continue;
353
- if (!shouldMergeTaskUpdate(current, updated)) {
354
- // Log skipped merges for visibility into rejected parallel updates.
355
- // In distributed systems with parallel workers, rejected merges may
356
- // indicate bugs (wrong status, timestamp corruption) if they accumulate.
357
- console.debug("[team-runner] Skipping stale merge for task", updated.id, {
358
- currentStatus: current.status,
359
- updatedStatus: updated.status,
360
- currentFinishedAt: current.finishedAt,
361
- updatedFinishedAt: updated.finishedAt,
362
- });
363
- skipped += 1;
364
- continue;
365
- }
366
- indexById.set(updated.id, updated);
367
- }
368
- }
369
- // Reassemble in original `base` order so downstream snapshots stay stable.
370
- const merged = base.map((task) => indexById.get(task.id) ?? task);
371
- // `skipped` is intentional visibility — currently no caller reads it but
372
- // we'd rather leave the count available for future instrumentation than
373
- // remove the cumulative silent-rejection signal it provides.
374
- void skipped;
375
- return refreshTaskGraphQueues(merged);
376
- }
377
- /** @deprecated Use mergeTaskUpdatesPreservingTerminal. Kept for backward test import compat. */
378
- export const __test__mergeTaskUpdates = mergeTaskUpdatesPreservingTerminal;
379
-
380
- // 2.8: adaptive-plan parsing/repair/injection moved to src/runtime/adaptive-plan.ts.
381
- // Re-export the test-only helpers so existing test imports still resolve.
382
- export {
383
- __test__parseAdaptivePlan,
384
- __test__repairAdaptivePlan,
385
- } from "./adaptive-plan.ts";
386
-
387
- import { injectAdaptivePlanIfReady } from "./adaptive-plan.ts";
388
-
389
- function formatTaskProgress(task: TeamTaskState): string {
390
- return `- ${task.id}: ${task.status} (${task.role} -> ${task.agent})${task.taskPacket ? ` scope=${task.taskPacket.scope}` : ""}${task.verification ? ` green=${task.verification.observedGreenLevel}/${task.verification.requiredGreenLevel}` : ""}${task.error ? ` - ${task.error}` : ""}`;
391
- }
392
-
393
- function runEffectivenessLines(
394
- manifest: TeamRunManifest,
395
- tasks: TeamTaskState[],
396
- executeWorkers: boolean,
397
- runtimeConfig?: CrewRuntimeConfig,
398
- ): string[] {
399
- return formatRunEffectivenessLines(
400
- evaluateRunEffectiveness({
401
- manifest,
402
- tasks,
403
- executeWorkers,
404
- runtimeConfig,
405
- }),
406
- );
407
- }
408
-
409
- // P6 (perf): Cache the last-rendered progress content so we can skip the
410
- // artifact write + redaction + atomic write + size/hash read when nothing
411
- // material changed (rare between batches, but happens between idle heartbeats).
412
- // The dedup filter also moved from O(N²) findIndex inside .filter(...)
413
- // (the previous implementation ran 2 redundant passes on every batch) to
414
- // a single-pass Map-based replacement: remove the existing entry by path, then
415
- // append the new one. Net complexity: O(N) build + O(1) replace per write.
416
- const lastProgressContentHash = new WeakMap<TeamRunManifest, string>();
417
-
418
- function writeProgress(
419
- manifest: TeamRunManifest,
420
- tasks: TeamTaskState[],
421
- producer: string,
422
- executeWorkers = true,
423
- runtimeConfig?: CrewRuntimeConfig,
424
- ): TeamRunManifest {
425
- const counts = new Map<string, number>();
426
- for (const task of tasks) counts.set(task.status, (counts.get(task.status) ?? 0) + 1);
427
- const queue = taskGraphSnapshot(tasks);
428
- const updatedAt = new Date().toISOString();
429
- const content = [
430
- `# pi-crew progress ${manifest.runId}`,
431
- "",
432
- `Status: ${manifest.status}`,
433
- `Team: ${manifest.team}`,
434
- `Workflow: ${manifest.workflow ?? "(none)"}`,
435
- `Updated: ${updatedAt}`,
436
- `Task counts: ${[...counts.entries()].map(([status, count]) => `${status}=${count}`).join(", ") || "none"}`,
437
- `Queue: ready=${queue.ready.length}, blocked=${queue.blocked.length}, running=${queue.running.length}, done=${queue.done.length}, failed=${queue.failed.length}, cancelled=${queue.cancelled.length}`,
438
- "",
439
- "## Tasks",
440
- ...tasks.map(formatTaskProgress),
441
- "",
442
- "## Effectiveness",
443
- ...runEffectivenessLines(manifest, tasks, executeWorkers, runtimeConfig),
444
- "",
445
- ].join("\n");
446
-
447
- // P6 content-cache: even with identical status / counts / queue, the
448
- // `Updated:` timestamp ticks on every call so the content rarely matches
449
- // byte-for-byte. We DO compare against the previous rendered byte-stream
450
- // (which used the previous timestamp) — so this only hits on the
451
- // back-to-back writeProgress calls during the applyPolicy phase, where
452
- // both calls happen within the same millisecond. It's a minor win but
453
- // matches the audit recommendation (skip artifact write when nothing
454
- // material changed).
455
- const prevHash = lastProgressContentHash.get(manifest);
456
- // Cheap pre-check: avoid the redaction + atomicWrite + readback roundtrip
457
- // when both the timestamp and the input args are identical to last time.
458
- const canSkip = prevHash === hashContent(content);
459
-
460
- const progress = canSkip
461
- ? (() => {
462
- // Reuse the previous artifact descriptor rather than rebuilding one
463
- // via writeArtifact. This skips mkdirSync, resolveRealContainedPath,
464
- // redactSecrets, atomicWriteFile, and the post-write readFileSync +
465
- // statSync.
466
- const existing = manifest.artifacts.find((a) => a.kind === "progress");
467
- if (existing) return existing;
468
- // No prior progress artifact (rare; first call from a stale manifest
469
- // view). Fall through to the normal write.
470
- return writeArtifact(manifest.artifactsRoot, {
471
- kind: "progress",
472
- relativePath: "progress.md",
473
- producer,
474
- content,
475
- });
476
- })()
477
- : writeArtifact(manifest.artifactsRoot, {
478
- kind: "progress",
479
- relativePath: "progress.md",
480
- producer,
481
- content,
482
- });
483
- lastProgressContentHash.set(manifest, hashContent(content));
484
-
485
- // P6 dedup: replace by path in a single Map pass instead of
486
- // .filter(...) // O(N) to remove the old entry
487
- // .filter((_, i, self) => self.findIndex(...) === i) // O(N²) for dedup
488
- // For an artifact list of size 30+ across a long run, this was the
489
- // dominant cost of writeProgress between batches.
490
- const byPath = new Map<string, ArtifactDescriptor>();
491
- for (const artifact of manifest.artifacts) {
492
- if (artifact.kind === "progress" && artifact.path === progress.path) continue;
493
- byPath.set(artifact.path, artifact);
494
- }
495
- byPath.set(progress.path, progress);
496
- const deduped = [...byPath.values()];
497
-
498
- return {
499
- ...manifest,
500
- updatedAt,
501
- artifacts: deduped,
502
- };
503
- }
504
-
505
- function applyPolicy(manifest: TeamRunManifest, tasks: TeamTaskState[], limits?: CrewLimitsConfig): TeamRunManifest {
506
- const branchFreshness = checkBranchFreshness(manifest.cwd);
507
- const branchArtifact = writeArtifact(manifest.artifactsRoot, {
508
- kind: "metadata",
509
- relativePath: "metadata/branch-freshness.json",
510
- producer: "branch-freshness",
511
- content: `${JSON.stringify(branchFreshness, null, 2)}\n`,
512
- });
513
- let decisions: PolicyDecision[] = evaluateCrewPolicy({
514
- manifest,
515
- tasks,
516
- limits,
517
- });
518
- if (branchFreshness.status === "stale" || branchFreshness.status === "diverged") {
519
- const branchDecision: PolicyDecision = {
520
- action: "notify",
521
- reason: "branch_stale",
522
- message: branchFreshness.message,
523
- createdAt: new Date().toISOString(),
524
- };
525
- decisions = [...decisions, branchDecision];
526
- appendEvent(manifest.eventsPath, {
527
- type: "branch.stale",
528
- runId: manifest.runId,
529
- message: branchFreshness.message,
530
- data: { branchFreshness },
531
- });
532
- }
533
- const policyArtifact = writeArtifact(manifest.artifactsRoot, {
534
- kind: "metadata",
535
- relativePath: "policy-decisions.json",
536
- producer: "policy-engine",
537
- content: `${JSON.stringify(decisions, null, 2)}\n`,
538
- });
539
- const recoveryLedger = buildRecoveryLedger(decisions);
540
- const recoveryArtifact = writeArtifact(manifest.artifactsRoot, {
541
- kind: "metadata",
542
- relativePath: "recovery-ledger.json",
543
- producer: "recovery-engine",
544
- content: `${JSON.stringify(recoveryLedger, null, 2)}\n`,
545
- });
546
- for (const item of decisions)
547
- appendEvent(manifest.eventsPath, {
548
- type: item.action === "escalate" ? "policy.escalated" : "policy.action",
549
- runId: manifest.runId,
550
- taskId: item.taskId,
551
- message: item.message,
552
- data: { action: item.action, reason: item.reason },
553
- });
554
- for (const item of recoveryLedger.entries)
555
- appendEvent(manifest.eventsPath, {
556
- type: item.state === "escalation_required" ? "recovery.escalated" : "recovery.attempted",
557
- runId: manifest.runId,
558
- taskId: item.taskId,
559
- message: item.message,
560
- data: {
561
- scenario: item.scenario,
562
- steps: item.steps,
563
- attempt: item.attempt,
564
- state: item.state,
565
- },
566
- });
567
- return {
568
- ...manifest,
569
- updatedAt: new Date().toISOString(),
570
- policyDecisions: decisions,
571
- artifacts: [
572
- ...manifest.artifacts.filter(
573
- (artifact) =>
574
- !(
575
- artifact.kind === "metadata" &&
576
- (artifact.path.endsWith("policy-decisions.json") ||
577
- artifact.path.endsWith("recovery-ledger.json") ||
578
- artifact.path.endsWith("branch-freshness.json"))
579
- ),
580
- ),
581
- branchArtifact,
582
- policyArtifact,
583
- recoveryArtifact,
584
- ],
585
- };
586
- }
587
-
588
- function retryPolicyFromConfig(config: CrewReliabilityConfig | undefined): RetryPolicy {
589
- return { ...DEFAULT_RETRY_POLICY, ...(config?.retryPolicy ?? {}) };
590
- }
591
-
592
- /**
593
- * #1 (assessment): decide whether the per-task retry path (executeWithRetry) is used.
594
- * Defaults to TRUE (opt-out) so transient worker hangs (ChildTimeout) are retried
595
- * automatically. Previously opt-in, which left the entire retry+recovery stack dormant.
596
- * Exported for unit testing.
597
- */
598
- export function shouldUseRetry(reliability: CrewReliabilityConfig | undefined): boolean {
599
- return reliability?.autoRetry !== false;
600
- }
601
-
602
- function failedTaskFrom(result: { tasks: TeamTaskState[] }, taskId: string): TeamTaskState | undefined {
603
- return result.tasks.find((item) => item.id === taskId && item.status === "failed");
604
- }
605
-
606
- function requiresPlanApproval(_workflow: WorkflowConfig, runtimeConfig: CrewRuntimeConfig | undefined): boolean {
607
- // ROADMAP T1.2: plan-level HITL applies to ANY workflow when
608
- // config.runtime.requirePlanApproval === true (not just 'implementation').
609
- // The gate fires at the read-only → mutating (plan → execute) boundary.
610
- return runtimeConfig?.requirePlanApproval === true;
611
- }
612
-
613
- function isPlanApprovalPending(manifest: TeamRunManifest): boolean {
614
- return manifest.planApproval?.required === true && manifest.planApproval.status === "pending";
615
- }
616
-
617
- function isMutatingTask(task: TeamTaskState): boolean {
618
- return permissionForRole(task.role) !== "read_only";
619
- }
620
-
621
- async function ensurePlanApprovalRequested(manifest: TeamRunManifest, tasks: TeamTaskState[]): Promise<TeamRunManifest> {
622
- if (manifest.planApproval) return manifest;
623
- const assessTask = tasks.find((task) => task.stepId === "assess" && task.status === "completed");
624
- // ROADMAP T1.2: for non-adaptive workflows, fall back to the most recent
625
- // completed read-only (planning) task as the plan reference.
626
- const planTask = assessTask ?? [...tasks].reverse().find((t) => t.status === "completed" && !isMutatingTask(t));
627
- const now = new Date().toISOString();
628
- const updated: TeamRunManifest = {
629
- ...manifest,
630
- updatedAt: now,
631
- planApproval: {
632
- required: true,
633
- status: "pending",
634
- requestedAt: now,
635
- updatedAt: now,
636
- planTaskId: planTask?.id,
637
- planArtifactPath: planTask?.resultArtifact?.path,
638
- },
639
- };
640
- await saveRunManifestAsync(updated);
641
- appendEvent(updated.eventsPath, {
642
- type: "plan.approval_required",
643
- runId: updated.runId,
644
- taskId: planTask?.id,
645
- message: "Plan requires explicit approval before mutating tasks run. Use: team api op=approve-plan runId=...",
646
- data: { planArtifactPath: planTask?.resultArtifact?.path },
647
- });
648
- return updated;
649
- }
650
-
651
- function cancelPlanTasks(tasks: TeamTaskState[], reason: string): TeamTaskState[] {
652
- return tasks.map((task) =>
653
- task.status === "queued" || task.status === "running" || task.status === "waiting"
654
- ? {
655
- ...task,
656
- status: "cancelled",
657
- finishedAt: new Date().toISOString(),
658
- error: reason,
659
- graph: task.graph ? { ...task.graph, queue: "done" } : undefined,
660
- }
661
- : task,
662
- );
663
- }
664
-
665
- function hasPendingMutatingAdaptiveTask(tasks: TeamTaskState[]): boolean {
666
- return tasks.some((task) => task.status === "queued" && task.adaptive && isMutatingTask(task));
667
- }
668
-
669
- /**
670
- * ROADMAP T1.2: gate detection for ANY workflow (not just adaptive).
671
- * Fires when there are pending mutating tasks whose prerequisites (read-only
672
- * tasks) have completed — i.e. the plan→execute boundary.
673
- */
674
- export function hasPendingMutatingTaskAtBoundary(tasks: TeamTaskState[]): boolean {
675
- const hasCompletedReadOnly = tasks.some((t) => t.status === "completed" && !isMutatingTask(t));
676
- const hasPendingMutating = tasks.some((t) => t.status === "queued" && isMutatingTask(t));
677
- return hasCompletedReadOnly && hasPendingMutating;
678
- }
679
-
680
- /**
681
- * Check whether any task uses explicit `dependsOn` that would benefit from DAG-based
682
- * execution planning. If so, build an execution plan and use `getDagReadyTasks`
683
- * to augment the ready-set selection.
684
- */
685
- function dagReadyTaskIds(tasks: TeamTaskState[], completedIds: Set<string>): string[] | null {
686
- const hasExplicitDeps = tasks.some((t) => t.dependsOn.length > 0);
687
- if (!hasExplicitDeps) return null;
688
- // FIX (goal-wrap runtime test): task.dependsOn stores STEP IDs (e.g. "execute"), not
689
- // task IDs (e.g. "02_execute"). The DAG scheduler compares deps against completedIds
690
- // (which are task IDs), so step-ID deps would never match → dependent tasks stuck blocked
691
- // forever. Map step IDs -> task IDs first (mirror dependencySatisfied in
692
- // task-graph-scheduler.ts which handles this via stepToTaskId). buildDagExecutionPlan +
693
- // getDagReadyTasks then work on consistent task IDs.
694
- const stepToTaskId = new Map<string, string>();
695
- for (const t of tasks) {
696
- if (t.stepId) stepToTaskId.set(t.stepId, t.id);
697
- }
698
- const nodes: TaskNode[] = tasks.map((t) => ({
699
- id: t.id,
700
- dependsOn: t.dependsOn.map((dep) => stepToTaskId.get(dep) ?? dep),
701
- phase: t.adaptive?.phase ?? t.stepId,
702
- }));
703
- const plan = buildDagExecutionPlan(nodes);
704
- if (plan.hasCycle) return null; // fall back to existing scheduler
705
- return getDagReadyTasks(plan, completedIds);
706
- }
707
-
708
- export async function executeTeamRun(input: ExecuteTeamRunInput): Promise<{ manifest: TeamRunManifest; tasks: TeamTaskState[] }> {
709
- const workflow = input.workflow;
710
-
711
- // DEFENSE-IN-DEPTH (advisory-only since v0.9.15): re-validate topology here in
712
- // case a caller bypassed the extension-layer handleRun guard. The extension
713
- // layer logs an advisory note and proceeds; this defense-in-depth does the same
714
- // for direct API callers (CLI, tests, scheduler). Never blocks.
715
- // Skip for synthetic direct-agent workflows (filePath="<generated>").
716
- if (workflow.filePath !== "<generated>") {
717
- // LAZY: defer preflight-validator import until the defense-in-depth guard actually runs.
718
- const { validateWorkflowUsage } = await import("../workflows/preflight-validator.ts");
719
- const preflight = validateWorkflowUsage(workflow, {
720
- force: input.reliability?.forcePreflight === true,
721
- });
722
- if (preflight.level === "warn" || preflight.level === "note" || preflight.level === "info") {
723
- const icon = preflight.level === "warn" ? "⚠️ " : preflight.level === "note" ? "✅ " : "ℹ️ ";
724
- console.warn(
725
- `${icon}[team-runner.preflight] ${preflight.level.toUpperCase()}: ${preflight.message} (workflow=${workflow.name})`,
726
- );
727
- if (preflight.suggestion) {
728
- console.warn(`[team-runner.preflight] → ${preflight.suggestion}`);
729
- }
730
- }
731
- }
732
-
733
- let manifest = updateRunStatus(
734
- input.manifest,
735
- "running",
736
- input.executeWorkers ? "Executing team workflow." : "Creating workflow prompts and placeholder results.",
737
- );
738
-
739
- // Persist budget fields on the manifest so all subsequent saveRunManifest
740
- // calls (there are many in executeTeamRunCore) preserve the budget config.
741
- // Without this, the spread in updateRunStatus and other transformations
742
- // can drop budget fields before they reach the persistence layer.
743
- if (input.budgetTotal !== undefined) manifest.budgetTotal = input.budgetTotal;
744
- if (input.budgetWarning !== undefined) manifest.budgetWarning = input.budgetWarning;
745
- if (input.budgetAbort !== undefined) manifest.budgetAbort = input.budgetAbort;
746
- if (input.budgetUnlimited !== undefined) manifest.budgetUnlimited = input.budgetUnlimited;
747
- if (manifest.budgetTotal !== undefined && manifest.budgetTotal > 0 && !manifest.budgetUnlimited) {
748
- await saveRunManifestAsync(manifest);
749
- }
750
-
751
- void registerRunPromise(manifest.runId);
752
-
753
- // FIX (Round 15, regression): Start a team-level heartbeat so the stale
754
- // reconciler does not cancel long-running team runs after 5 minutes
755
- // (NO_PID_HEARTBEAT_STALE_MS). Previously only sub-task runners wrote
756
- // heartbeats; the team-level run had no heartbeat, so any multi-phase
757
- // workflow lasting >5min was marked stale and cancelled.
758
- const stopTeamHeartbeat = startTeamRunHeartbeat(manifest.stateRoot, manifest.runId);
759
-
760
- const cleanupUsage = (): void => {
761
- for (const task of input.tasks) clearTrackedTaskUsage(task.id);
762
- };
763
-
764
- try {
765
- const result = await executeTeamRunCore(input, manifest, workflow);
766
- // #2 (assessment): goal-achievement detection — kill the silent false-green.
767
- // A code-mutating run that "completed" but left the git working tree clean
768
- // (and/or had a failed task) is a false-green. We expose goalAchieved on the
769
- // manifest + emit an event so the lie is never silent, and downgrade status
770
- // to "failed" only when a failed task corroborates it (conservative).
771
- const gaAssessment = assessGoalAchievement(result.manifest, result.tasks, workflow);
772
- const gaApplied = applyGoalAchievement(result.manifest, gaAssessment);
773
- if (gaApplied.manifest !== result.manifest) {
774
- result.manifest = gaApplied.manifest;
775
- try {
776
- await saveRunManifestAsync(result.manifest);
777
- } catch (persistError) {
778
- logInternalError(
779
- "team-runner.goalAchievement.persist",
780
- persistError instanceof Error ? persistError : new Error(String(persistError)),
781
- `runId=${manifest.runId}`,
782
- );
783
- }
784
- }
785
- appendEvent(manifest.eventsPath, {
786
- type: "run.goal_achievement",
787
- runId: manifest.runId,
788
- message: gaApplied.manifest.goalAchievementNote ?? "",
789
- data: {
790
- achieved: gaAssessment.achieved,
791
- downgraded: gaApplied.downgraded,
792
- reason: gaAssessment.reason,
793
- signals: gaAssessment.signals,
794
- },
795
- });
796
- if (gaApplied.downgraded)
797
- logInternalError(
798
- "team-runner.goalAchievement.falseGreen",
799
- new Error(gaApplied.manifest.goalAchievementNote ?? "false-green detected"),
800
- `runId=${manifest.runId}`,
801
- "error",
802
- );
803
- stopTeamHeartbeat();
804
- resolveRunPromise(manifest.runId, result);
805
- // Terminate live agents for this run — agents are done when the run ends.
806
- void terminateLiveAgentsForRun(manifest.runId, "completed", appendEvent, manifest.eventsPath).catch((error) =>
807
- logInternalError("team-runner.completed.terminate", error, `runId=${manifest.runId}`),
808
- );
809
-
810
- // Emit run completion hook (100% reliable, fire-and-forget)
811
- crewHooks.emit({
812
- type: "run_completed",
813
- timestamp: new Date().toISOString(),
814
- runId: manifest.runId,
815
- data: {
816
- status: result.manifest.status,
817
- taskCount: result.tasks.length,
818
- },
819
- });
820
-
821
- // Execute after_run_complete lifecycle hook (non-blocking)
822
- const afterRunReport = await executeHook("after_run_complete", {
823
- runId: manifest.runId,
824
- cwd: manifest.cwd,
825
- status: result.manifest.status,
826
- });
827
- appendHookEvent(manifest, afterRunReport);
828
- if (afterRunReport.outcome === "block") {
829
- logInternalError(
830
- "team-runner.after_run_complete.blocked",
831
- new Error(afterRunReport.reason ?? "after_run_complete hook blocked"),
832
- `runId=${manifest.runId}`,
833
- );
834
- }
835
-
836
- // M7: flush buffered task.progress events so the final state is durable
837
- // before the run returns. Buffered producer wins on latency (p95≈0µs);
838
- // this single flush at run-end coalesces any pending progress bursts
839
- // before manifest updates are observed by readers.
840
- await flushEventLogBuffer();
841
- return result;
842
- } catch (error) {
843
- // Round 27 (BUG 1): the success path calls stopTeamHeartbeat() but this
844
- // catch path did NOT. The team heartbeat is a non-unref'd setInterval
845
- // (30s) that deliberately keeps the event loop alive — without this
846
- // call, a failed team run leaves the interval firing forever and the
847
- // foreground pi process hangs (never returns to the prompt); in
848
- // background-runner mode the worker never exits. clearInterval is
849
- // idempotent so a double-call (if this runs after the success path)
850
- // is harmless.
851
- stopTeamHeartbeat();
852
- // P1: Catch unhandled errors — ensure manifest/tasks/agents are terminal so they don't stay "running" forever.
853
- const message = error instanceof Error ? error.message : String(error);
854
- // Re-read the latest persisted state from disk instead of trusting
855
- // input.tasks (the ORIGINAL start snapshot, still all "queued" — it is never
856
- // mutated by executeTeamRunCore). A late failure during closeout would
857
- // otherwise map every task to "failed", overwriting tasks that already
858
- // completed during the run. loadRunManifestById is the established
859
- // fresh-read pattern in this file (see ~line 1269); it is best-effort with
860
- // no lock, consistent with the lock-drop decision below. If the disk read
861
- // fails, fall back to input.tasks so the run is still marked terminal.
862
- const fresh = loadRunManifestById(manifest.cwd, manifest.runId);
863
- const freshManifest = fresh?.manifest ?? manifest;
864
- const freshTasks = refreshTaskGraphQueues(fresh?.tasks ?? input.tasks);
865
- const failedAt = new Date().toISOString();
866
- const tasks = freshTasks.map((task) =>
867
- task.status === "running" || task.status === "queued" || task.status === "waiting"
868
- ? {
869
- ...task,
870
- status: "failed" as const,
871
- finishedAt: failedAt,
872
- error: message,
873
- }
874
- : task,
875
- );
876
- manifest = freshManifest;
877
- try {
878
- await terminateLiveAgentsForRun(manifest.runId, "failed", appendEvent, manifest.eventsPath);
879
- await saveRunTasksAsync(manifest, tasks);
880
- const existingRuntimeByTask = new Map(readCrewAgents(manifest).map((agent) => [agent.taskId, agent.runtime]));
881
- const globalRuntime = input.runtime?.kind ?? "child-process";
882
- const runtimeForAgent = (agent: ReturnType<typeof recordsForMaterializedTasks>[number]): CrewRuntimeKind => {
883
- const task = tasks.find((item) => item.id === agent.taskId);
884
- return (
885
- existingRuntimeByTask.get(agent.taskId) ??
886
- resolveTaskRuntimeKind(globalRuntime, task?.role ?? agent.role, input.runtimeConfig?.isolationPolicy)
887
- );
888
- };
889
- saveCrewAgents(
890
- manifest,
891
- recordsForMaterializedTasks(manifest, tasks, globalRuntime).map((agent) => ({ ...agent, runtime: runtimeForAgent(agent) })),
892
- );
893
- manifest = updateRunStatus(manifest, "failed", `Unhandled error in team runner: ${message}`);
894
- await saveRunManifestAsync(manifest);
895
- } catch {
896
- // Best-effort — state write may also fail
897
- }
898
- const result = { manifest, tasks };
899
- rejectRunPromise(manifest.runId, error instanceof Error ? error : new Error(message));
900
- crewHooks.emit({
901
- type: "run_failed",
902
- timestamp: new Date().toISOString(),
903
- runId: manifest.runId,
904
- data: { status: manifest.status, error: message },
905
- });
906
- // M7: flush buffered events before returning on the error path so the
907
- // final buffered progress events are durable alongside the failure state.
908
- await flushEventLogBuffer();
909
- return result;
910
- } finally {
911
- // M7+follow-up (v0.9.24): drain buffer on the success path too.
912
- // Previously only the catch path flushed; under --test-force-exit the
913
- // success path left pending appendEventAsync promises that the test
914
- // runner detected as 'Promise resolution is still pending'. The catch
915
- // still has its own flush for M7 backward compat — flushEventLogBuffer
916
- // is idempotent on an empty queue.
917
- await flushEventLogBuffer();
918
- // A2-F1: clear tracked usage for this run's tasks. Consolidated in finally
919
- // (previously duplicated on the success + error paths) so cleanup is
920
- // guaranteed on EVERY exit path — a future throw in the success tail or
921
- // error handler can no longer leak entries. Safe to run last: nothing in
922
- // team-runner reads usage after the run resolves, and the TUI widget reads
923
- // live usage only while a task is running (before this point).
924
- cleanupUsage();
925
- // NEW-M1: clear the stable-prefix cache (keyed by runId) so it does not
926
- // grow unbounded across runs in a long-lived session.
927
- clearStablePrefixCache();
928
- }
929
- }
930
-
931
- async function executeTeamRunCore(
932
- input: ExecuteTeamRunInput,
933
- manifest: TeamRunManifest,
934
- workflow: WorkflowConfig,
935
- ): Promise<{ manifest: TeamRunManifest; tasks: TeamTaskState[] }> {
936
- // Execute before_run_start hook (non-blocking by default)
937
- const beforeRunReport = await executeHook("before_run_start", {
938
- runId: manifest.runId,
939
- cwd: manifest.cwd,
940
- });
941
- appendHookEvent(manifest, beforeRunReport);
942
- if (beforeRunReport.outcome === "block") {
943
- manifest = updateRunStatus(manifest, "blocked", beforeRunReport.reason ?? "before_run_start hook blocked the run.");
944
- return { manifest, tasks: input.tasks };
945
- }
946
- let tasks = refreshTaskGraphQueues(input.tasks);
947
- let queueIndex = buildTaskGraphIndex(tasks);
948
- const canInjectAdaptivePlan = workflow.name === "implementation";
949
- let adaptivePlanInjected = false;
950
- let adaptivePlanMissing = false;
951
- const attemptAdaptivePlan = async () => {
952
- if (!canInjectAdaptivePlan || adaptivePlanInjected || adaptivePlanMissing) return { injected: false, missing: false };
953
- const adaptivePlan = await injectAdaptivePlanIfReady({
954
- manifest,
955
- tasks,
956
- workflow,
957
- team: input.team,
958
- });
959
- adaptivePlanInjected = adaptivePlanInjected || adaptivePlan.injected;
960
- adaptivePlanMissing = adaptivePlan.missingPlan;
961
- workflow = adaptivePlan.workflow;
962
- if (adaptivePlan.injected) tasks = adaptivePlan.tasks;
963
- return {
964
- injected: adaptivePlan.injected,
965
- missing: adaptivePlan.missingPlan,
966
- };
967
- };
968
- const initialAdaptive = await attemptAdaptivePlan();
969
- if (initialAdaptive.missing) {
970
- tasks = markBlocked(tasks, "Adaptive planner did not produce a valid subagent plan.");
971
- await saveRunTasksAsync(manifest, tasks);
972
- manifest = updateRunStatus(manifest, "blocked", "Adaptive planner did not produce a valid subagent plan.");
973
- return { manifest, tasks };
974
- }
975
- if (initialAdaptive.injected) {
976
- manifest = requiresPlanApproval(workflow, input.runtimeConfig) ? await ensurePlanApprovalRequested(manifest, tasks) : manifest;
977
- queueIndex = buildTaskGraphIndex(tasks);
978
- } else if (
979
- requiresPlanApproval(workflow, input.runtimeConfig) &&
980
- (hasPendingMutatingAdaptiveTask(tasks) || hasPendingMutatingTaskAtBoundary(tasks))
981
- ) {
982
- manifest = await ensurePlanApprovalRequested(manifest, tasks);
983
- }
984
- if (manifest.planApproval?.status === "cancelled") {
985
- tasks = cancelPlanTasks(tasks, "Plan approval was cancelled.");
986
- await saveRunTasksAsync(manifest, tasks);
987
- manifest = updateRunStatus(manifest, "cancelled", "Plan approval was cancelled.");
988
- return { manifest, tasks };
989
- }
990
- manifest = writeProgress(manifest, tasks, "team-runner", input.executeWorkers, input.runtimeConfig);
991
- await saveRunManifestAsync(manifest);
992
- const runtimeKind = input.runtime?.kind ?? (input.executeWorkers ? "child-process" : "scaffold");
993
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
994
-
995
- // Build a workflow phase state machine from workflow steps for precondition tracking.
996
- const workflowPhases: PhaseState[] = workflow.steps.map(
997
- (step): PhaseState => ({
998
- name: step.id,
999
- status: "pending",
1000
- inputs: step.reads === false ? [] : Array.isArray(step.reads) ? step.reads : [],
1001
- outputs: step.output === false ? [] : step.output ? [step.output] : [],
1002
- }),
1003
- );
1004
- let wfMachine = createWorkflowStateMachine(workflowPhases);
1005
-
1006
- // ── OPT-01 streaming dispatch: track in-flight dispatch units so a new
1007
- // task can be dispatched as soon as a slot frees, without waiting for
1008
- // the entire batch to complete. Each entry maps a unit key (singleton
1009
- // task ID or coalesced-group ID) to the in-flight promise + member IDs. ──
1010
- const pendingUnits = new Map<string, { taskIds: string[]; promise: Promise<{ manifest: TeamRunManifest; tasks: TeamTaskState[] }> }>();
1011
-
1012
- while (tasks.some((task) => task.status === "queued") || pendingUnits.size > 0) {
1013
- if (input.signal?.aborted) {
1014
- const cancelReason = cancellationReasonFromSignal(input.signal);
1015
- const message = `${cancelReason.message} (${cancelReason.code})`;
1016
- const cancelledTaskIds: string[] = [];
1017
- tasks = tasks.map((task) => {
1018
- if (task.status !== "queued" && task.status !== "running" && task.status !== "waiting") return task;
1019
- cancelledTaskIds.push(task.id);
1020
- const base = {
1021
- ...task,
1022
- status: "cancelled" as const,
1023
- finishedAt: new Date().toISOString(),
1024
- error: message,
1025
- };
1026
- if (task.status === "running") {
1027
- return {
1028
- ...base,
1029
- terminalEvidence: [
1030
- ...(task.terminalEvidence ?? []),
1031
- buildSyntheticTerminalEvidence("worker", cancelReason, task.startedAt),
1032
- ],
1033
- };
1034
- }
1035
- return base;
1036
- });
1037
- await saveRunTasksAsync(manifest, tasks);
1038
- for (const taskId of cancelledTaskIds)
1039
- await appendEventAsync(manifest.eventsPath, {
1040
- type: "task.cancelled",
1041
- runId: manifest.runId,
1042
- taskId,
1043
- message,
1044
- data: { reason: cancelReason.code },
1045
- });
1046
- manifest = updateRunStatus(manifest, "cancelled", message, {
1047
- data: { reason: cancelReason.code, cancelledTaskIds },
1048
- });
1049
- return { manifest, tasks };
1050
- }
1051
-
1052
- const failed = tasks.find((task) => task.status === "failed");
1053
- if (failed) {
1054
- // #4 (assessment): honor limits.maxRetriesPerTask — re-queue an eligible
1055
- // failed task for a bounded whole-task rerun instead of immediately
1056
- // aborting the run. Before #4, the recovery ledger recorded `rerun_task`
1057
- // entries with state:"planned" but never executed them (decorative).
1058
- // Default-off: maxRetriesPerTask=0 → original abort behavior preserved.
1059
- const rerun = shouldRerunFailedTask(failed, input.limits);
1060
- if (rerun.rerun) {
1061
- tasks = tasks.map((item) =>
1062
- item.id === failed.id
1063
- ? {
1064
- ...item,
1065
- status: "queued" as const,
1066
- policy: {
1067
- ...(item.policy ?? {}),
1068
- retryCount: rerun.newRetryCount,
1069
- },
1070
- error: undefined,
1071
- finishedAt: undefined,
1072
- }
1073
- : item,
1074
- );
1075
- await saveRunTasksAsync(manifest, tasks);
1076
- await appendEventAsync(manifest.eventsPath, {
1077
- type: "recovery.rerun_task",
1078
- runId: manifest.runId,
1079
- taskId: failed.id,
1080
- message: `Re-queuing failed task for whole-task rerun: ${rerun.reason}`,
1081
- data: {
1082
- attempt: rerun.newRetryCount,
1083
- maxRetries: input.limits?.maxRetriesPerTask ?? 0,
1084
- scenario: "task_failed",
1085
- },
1086
- });
1087
- continue; // loop re-processes the re-queued task
1088
- }
1089
- tasks = markBlocked(tasks, `Blocked by failed task '${failed.id}'.`);
1090
- await saveRunTasksAsync(manifest, tasks);
1091
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
1092
- manifest = updateRunStatus(manifest, "failed", `Failed at task '${failed.id}'.`);
1093
- return { manifest, tasks };
1094
- }
1095
-
1096
- const snapshot = taskGraphSnapshot(tasks, queueIndex);
1097
-
1098
- // DAG-based execution plan: when tasks have explicit dependsOn, use the
1099
- // topological wave planner to determine ready tasks. Fall back to the
1100
- // existing task-graph-scheduler when no explicit deps exist (backward compat).
1101
- const completedIds = new Set(tasks.filter((t) => t.status === "completed" || t.status === "needs_attention").map((t) => t.id));
1102
- const dagReady = dagReadyTaskIds(tasks, completedIds);
1103
- const readyBeforeFilter = dagReady ?? snapshot.ready;
1104
-
1105
- // Workflow phase precondition check (non-blocking: log warnings only).
1106
- if (wfMachine.currentPhaseIndex < wfMachine.phases.length) {
1107
- const completedArtifacts = manifest.artifacts.filter((a) => a.kind === "result" || a.kind === "summary").map((a) => a.path);
1108
- const previousPhaseStatus =
1109
- wfMachine.currentPhaseIndex > 0 ? (wfMachine.phases[wfMachine.currentPhaseIndex - 1]?.status ?? "pending") : "completed";
1110
- const wfContext: PhaseGuardContext = {
1111
- completedArtifacts,
1112
- previousPhaseStatus,
1113
- taskResults: tasks
1114
- .filter((t) => t.status === "completed" || t.status === "needs_attention")
1115
- .map((t) => ({
1116
- taskId: t.id,
1117
- status: t.status,
1118
- outputPath: t.resultArtifact?.path,
1119
- })),
1120
- };
1121
- const preconditions = validatePhasePreconditions(wfMachine, wfContext);
1122
- if (!preconditions.ready) {
1123
- await appendEventAsync(manifest.eventsPath, {
1124
- type: "workflow.preconditions",
1125
- runId: manifest.runId,
1126
- message: `Workflow phase '${wfMachine.phases[wfMachine.currentPhaseIndex]?.name}' is missing inputs: ${preconditions.blocking.join(", ")}`,
1127
- data: {
1128
- phaseIndex: wfMachine.currentPhaseIndex,
1129
- phaseName: wfMachine.phases[wfMachine.currentPhaseIndex]?.name,
1130
- blocking: preconditions.blocking,
1131
- },
1132
- });
1133
- } else {
1134
- // Advance the machine past completed phases.
1135
- while (
1136
- wfMachine.currentPhaseIndex < wfMachine.phases.length &&
1137
- wfMachine.phases[wfMachine.currentPhaseIndex]?.status === "completed"
1138
- ) {
1139
- wfMachine = {
1140
- ...wfMachine,
1141
- currentPhaseIndex: wfMachine.currentPhaseIndex + 1,
1142
- };
1143
- }
1144
- }
1145
- }
1146
-
1147
- const readyRoles = readyBeforeFilter
1148
- .map((taskId) => tasks.find((task) => task.id === taskId)?.role)
1149
- .filter((role): role is string => Boolean(role));
1150
- const concurrency = resolveBatchConcurrency({
1151
- workflowName: workflow.name,
1152
- workflowMaxConcurrency: workflow.maxConcurrency,
1153
- teamMaxConcurrency: input.team.maxConcurrency,
1154
- limitMaxConcurrentWorkers: input.limits?.maxConcurrentWorkers,
1155
- allowUnboundedConcurrency: input.limits?.allowUnboundedConcurrency,
1156
- readyCount: readyBeforeFilter.length,
1157
- workspaceMode: manifest.workspaceMode,
1158
- readyRoles,
1159
- });
1160
-
1161
- // Round 25 (M5): serialize on write-path overlap when opted in.
1162
- // Opt-in via limits.serializeOnPathOverlap; default off (= no behavior change).
1163
- // filterReadyByWriteOverlap returns the same array when enabled=false, so
1164
- // production runs pay nothing for the unused code path. When the flag is on,
1165
- // `serializedReady` MAY be a strict subset of `readyBeforeFilter` (conflicting tasks
1166
- // deferred to next cycle).
1167
- const serializedReady = filterReadyByWriteOverlap(
1168
- readyBeforeFilter,
1169
- tasks,
1170
- workflow,
1171
- concurrency.maxConcurrent,
1172
- input.limits?.serializeOnPathOverlap === true,
1173
- );
1174
-
1175
- // Round 25 (M6): coalesce micro-tasks when opted in.
1176
- // Default off; when on, groups same-(role,cwd) tasks into coalesced groups
1177
- // (with write-path safety). In v0.9.17 first ship, we ONLY log the
1178
- // coalesced group count to the event stream (informational). Actual
1179
- // dispatching of one-multi-task worker instead of N workers is deferred
1180
- // to a follow-up — it's a non-trivial prompt-construction change that
1181
- // deserves its own PR. For now, every coalesced group => one info event.
1182
- const coalesceEnabled = workflow.coalesceMicroTasks === true;
1183
- if (coalesceEnabled) {
1184
- const coalescedGroups = planCoalescedGroups(serializedReady, tasks, workflow, true);
1185
- for (const group of coalescedGroups) {
1186
- if (group.tasks.length < 2) continue; // singletons are not interesting
1187
- await appendEventAsync(manifest.eventsPath, {
1188
- type: "task.coalesced",
1189
- runId: manifest.runId,
1190
- message: `Coalesced ${group.tasks.length} micro-tasks (role=${group.role}, cwd=${group.cwd})`,
1191
- data: {
1192
- groupId: group.id,
1193
- role: group.role,
1194
- cwd: group.cwd,
1195
- taskIds: group.tasks.map((task) => task.id),
1196
- },
1197
- });
1198
- }
1199
- }
1200
- if (concurrency.reason.includes(";unbounded:")) {
1201
- await appendEventAsync(manifest.eventsPath, {
1202
- type: "limits.unbounded",
1203
- runId: manifest.runId,
1204
- message: "Unbounded worker concurrency was explicitly enabled for this run.",
1205
- data: {
1206
- concurrencyReason: concurrency.reason,
1207
- maxConcurrent: concurrency.maxConcurrent,
1208
- },
1209
- });
1210
- }
1211
- // ── OPT-01 streaming dispatch: exclude tasks already in-flight, limit
1212
- // new dispatches to available concurrency slots. ──
1213
- const inFlightTaskIds = new Set<string>();
1214
- for (const pendingUnit of pendingUnits.values()) {
1215
- for (const taskId of pendingUnit.taskIds) inFlightTaskIds.add(taskId);
1216
- }
1217
- const slotsAvailable = Math.max(0, concurrency.maxConcurrent - pendingUnits.size);
1218
- const approvalPending = isPlanApprovalPending(manifest);
1219
- const dispatchableReady = serializedReady.filter((id) => !inFlightTaskIds.has(id));
1220
- const readyIds = approvalPending ? dispatchableReady : dispatchableReady.slice(0, slotsAvailable);
1221
- const candidateBatch = readyIds
1222
- .map((id) => tasks.find((task) => task.id === id))
1223
- .filter((task): task is TeamTaskState => Boolean(task));
1224
- const readyBatch = approvalPending
1225
- ? candidateBatch.filter((task) => !isMutatingTask(task)).slice(0, slotsAvailable)
1226
- : candidateBatch;
1227
- if (readyBatch.length === 0) {
1228
- if (pendingUnits.size > 0) {
1229
- // Tasks are in-flight — skip dispatch and proceed to wait phase.
1230
- // (No return; code falls through to the dispatch section which is
1231
- // a no-op with an empty readyBatch, then reaches the wait phase.)
1232
- } else if (approvalPending && candidateBatch.some(isMutatingTask)) {
1233
- await saveRunTasksAsync(manifest, tasks);
1234
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
1235
- manifest = updateRunStatus(manifest, "blocked", "Plan approval required before mutating implementation tasks run.");
1236
- return { manifest, tasks };
1237
- } else {
1238
- tasks = markBlocked(tasks, "No ready queued task; dependency graph may be invalid.");
1239
- await saveRunTasksAsync(manifest, tasks);
1240
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
1241
- manifest = updateRunStatus(manifest, "blocked", "No ready queued task.");
1242
- return { manifest, tasks };
1243
- }
1244
- }
1245
-
1246
- // 2.2 caller migration: batch progress is high-frequency informational (M7 wire).
1247
- void appendEventBuffered(manifest.eventsPath, {
1248
- type: "task.progress",
1249
- runId: manifest.runId,
1250
- message: `Starting ready batch with ${readyBatch.length} task(s).`,
1251
- data: {
1252
- taskIds: readyBatch.map((task) => task.id),
1253
- readyCount: snapshot.ready.length,
1254
- blockedCount: snapshot.blocked.length,
1255
- runningCount: snapshot.running.length,
1256
- doneCount: snapshot.done.length,
1257
- selectedCount: readyBatch.length,
1258
- maxConcurrent: concurrency.maxConcurrent,
1259
- defaultConcurrency: concurrency.defaultConcurrency,
1260
- concurrencyReason: approvalPending ? `${concurrency.reason};plan-approval-read-only` : concurrency.reason,
1261
- },
1262
- });
1263
- // Execute before_task_start hooks for the batch
1264
- for (const task of readyBatch) {
1265
- const taskReport = await executeHook("before_task_start", {
1266
- runId: manifest.runId,
1267
- taskId: task.id,
1268
- cwd: manifest.cwd,
1269
- });
1270
- appendHookEvent(manifest, taskReport);
1271
- if (taskReport.outcome === "block") {
1272
- tasks = tasks.map((t) =>
1273
- t.id === task.id
1274
- ? {
1275
- ...t,
1276
- status: "skipped" as const,
1277
- error: taskReport.reason ?? "before_task_start hook blocked execution.",
1278
- }
1279
- : t,
1280
- );
1281
- manifest = updateRunStatus(manifest, manifest.status, `Task '${task.id}' blocked by hook.`);
1282
- }
1283
- }
1284
- const batchTasks = readyBatch.filter((task) => tasks.find((t) => t.id === task.id && t.status !== "skipped"));
1285
- if (batchTasks.length > 1) {
1286
- await appendEventAsync(manifest.eventsPath, {
1287
- type: "task.parallel_start",
1288
- runId: manifest.runId,
1289
- message: `Launching ${batchTasks.length} tasks in PARALLEL (concurrency=${concurrency.selectedCount}): ${batchTasks.map((t) => `${t.role}(${t.id})`).join(", ")}`,
1290
- data: {
1291
- taskIds: batchTasks.map((t) => t.id),
1292
- roles: batchTasks.map((t) => t.role),
1293
- concurrency: concurrency.selectedCount,
1294
- },
1295
- });
1296
- }
1297
-
1298
- // M6 real dispatch: when coalesceMicroTasks is enabled, batch the
1299
- // ready tasks into dispatch units. Multi-task groups are dispatched
1300
- // as one worker (single cold-start) instead of N. Singletons fall
1301
- // through to per-task dispatch.
1302
- const coalescedGroups = planCoalescedGroups(
1303
- batchTasks.map((t) => t.id),
1304
- tasks,
1305
- workflow,
1306
- coalesceEnabled,
1307
- );
1308
- const dispatchUnits = buildDispatchUnits(
1309
- batchTasks.map((t) => t.id),
1310
- coalescedGroups,
1311
- );
1312
-
1313
- // NEW-M1: Pre-warm stable prefix cache for one representative task
1314
- // per unique cwd. Parallel siblings with the same cwd/step reuse
1315
- // the cached workspace tree, file retrieval, and knowledge fragment
1316
- // instead of recomputing them independently (~200-800ms per batch).
1317
- if (batchTasks.length > 1) {
1318
- const seenCwds = new Set<string>();
1319
- await Promise.all(
1320
- batchTasks
1321
- .filter((task) => {
1322
- if (seenCwds.has(task.cwd)) return false;
1323
- seenCwds.add(task.cwd);
1324
- return true;
1325
- })
1326
- .map((task) => {
1327
- const step = findStep(workflow, task);
1328
- return computeStablePrefixComponents(manifest, step, task);
1329
- }),
1330
- );
1331
- }
1332
-
1333
- // ── OPT-01 streaming dispatch: dispatch each unit into pendingUnits
1334
- // instead of awaiting the entire batch via mapConcurrent. Each unit's
1335
- // promise is stored so we can Promise.race on the next iteration. ──
1336
- const dispatchUnit = async (unit: DispatchUnit): Promise<{ manifest: TeamRunManifest; tasks: TeamTaskState[] }> => {
1337
- // M6 real dispatch path: single worker for N tasks.
1338
- if (unit.kind === "group") {
1339
- const groupTasks = unit.group.tasks;
1340
- const firstTask = groupTasks[0]!;
1341
- const step = findStep(workflow, firstTask);
1342
- const agent = findAgent(input.agents, firstTask);
1343
- const teamRole = input.team.roles.find((role) => role.name === firstTask.role);
1344
- const perTaskRuntime = resolveTaskRuntimeKind(runtimeKind, firstTask.role, input.runtimeConfig?.isolationPolicy);
1345
- return runCoalescedTaskGroup({
1346
- manifest,
1347
- tasks,
1348
- groupTasks,
1349
- step,
1350
- agent,
1351
- signal: input.signal,
1352
- executeWorkers: input.executeWorkers,
1353
- runtimeKind,
1354
- workspaceId: input.workspaceId,
1355
- onJsonEvent: input.onJsonEvent,
1356
- teamRole,
1357
- perTaskRuntime,
1358
- });
1359
- }
1360
- // Singleton path: original per-task dispatch.
1361
- const task = batchTasks.find((t) => t.id === unit.taskId)!;
1362
- const step = findStep(workflow, task);
1363
- const agent = findAgent(input.agents, task);
1364
- const teamRole = input.team.roles.find((role) => role.name === task.role);
1365
- const perTaskRuntime = resolveTaskRuntimeKind(runtimeKind, task.role, input.runtimeConfig?.isolationPolicy);
1366
- const baseInput = {
1367
- manifest,
1368
- tasks,
1369
- task,
1370
- step,
1371
- agent,
1372
- signal: input.signal,
1373
- executeWorkers: input.executeWorkers,
1374
- runtimeKind: runtimeKind,
1375
- taskRuntimeOverride: perTaskRuntime !== runtimeKind ? perTaskRuntime : undefined,
1376
- runtimeConfig: input.runtimeConfig,
1377
- parentContext: input.parentContext,
1378
- parentModel: input.parentModel,
1379
- modelRegistry: input.modelRegistry,
1380
- modelOverride: input.modelOverride,
1381
- teamRoleModel: teamRole?.model,
1382
- teamRoleSkills: teamRole?.skills,
1383
- skillOverride: input.skillOverride,
1384
- limits: input.limits,
1385
- onJsonEvent: input.onJsonEvent,
1386
- workspaceId: input.workspaceId,
1387
- };
1388
- // #1 (assessment): autoRetry now defaults ON (opt-out via reliability.autoRetry=false).
1389
- // The dominant v0.9.13 failure was ChildTimeout ("worker became unresponsive") with
1390
- // ZERO retries because this gate was opt-in. isRetryable() defaults to true when
1391
- // retryableErrors is empty, so transient hangs now retry up to maxAttempts (3) with
1392
- // exponential backoff. Set reliability.autoRetry=false to restore old single-shot behavior.
1393
- if (!shouldUseRetry(input.reliability))
1394
- return withCorrelation(childCorrelation(manifest.runId, task.id), () => runTeamTask(baseInput));
1395
- let lastFailed: { manifest: TeamRunManifest; tasks: TeamTaskState[] } | undefined;
1396
- let lastAttemptId: string | undefined;
1397
- const attemptsSoFar: TaskAttemptState[] = [...(task.attempts ?? [])];
1398
- const policy = retryPolicyFromConfig(input.reliability);
1399
- try {
1400
- return await executeWithRetry(
1401
- async (attempt, info) => {
1402
- const startedAt = new Date().toISOString();
1403
- const inFlightAttempts: TaskAttemptState[] = [...attemptsSoFar, { attemptId: info.attemptId, startedAt }];
1404
- input.metricRegistry?.counter("crew.task.retry_attempt_total", "Retry attempts by run and task").inc({
1405
- runId: manifest.runId,
1406
- taskId: task.id,
1407
- });
1408
- // NOTE: no withRunLock — best-effort only; concurrent writes may cause inconsistency
1409
- const fresh = loadRunManifestById(manifest.cwd, manifest.runId);
1410
- const freshManifest = fresh?.manifest ?? manifest;
1411
- const freshTasks = fresh?.tasks ?? tasks;
1412
- const freshTask = freshTasks.find((item) => item.id === task.id) ?? task;
1413
- if (freshTask.status !== "queued" && freshTask.status !== "running")
1414
- return {
1415
- manifest: freshManifest,
1416
- tasks: freshTasks,
1417
- };
1418
- const taskWithAttempt: TeamTaskState = {
1419
- ...freshTask,
1420
- attempts: inFlightAttempts,
1421
- };
1422
- const result = await withCorrelation(childCorrelation(freshManifest.runId, task.id), () =>
1423
- runTeamTask({
1424
- ...baseInput,
1425
- manifest: freshManifest,
1426
- tasks: freshTasks,
1427
- task: taskWithAttempt,
1428
- }),
1429
- );
1430
- const failed = failedTaskFrom(result, task.id);
1431
- const endedAt = new Date().toISOString();
1432
- const finishedAttempt: TaskAttemptState = {
1433
- attemptId: info.attemptId,
1434
- startedAt,
1435
- endedAt,
1436
- ...(failed?.error ? { error: failed.error } : {}),
1437
- };
1438
- attemptsSoFar.push(finishedAttempt);
1439
- const withAttempt = result.tasks.map((item) =>
1440
- item.id === task.id ? { ...item, attempts: [...attemptsSoFar] } : item,
1441
- );
1442
- const enriched = {
1443
- manifest: result.manifest,
1444
- tasks: withAttempt,
1445
- };
1446
- if (failed) {
1447
- lastFailed = enriched;
1448
- throw new CrewError(ErrorCode.TaskNotFound, failed.error ?? `Task ${task.id} failed.`).withContext(
1449
- `retry evaluation (run=${manifest.runId})`,
1450
- );
1451
- }
1452
- input.metricRegistry?.histogram("crew.task.retry_count", "Retries per task", [0, 1, 2, 3, 5, 10]).observe(
1453
- {
1454
- runId: manifest.runId,
1455
- team: input.team.name,
1456
- },
1457
- Math.max(0, attempt - 1),
1458
- );
1459
- return enriched;
1460
- },
1461
- policy,
1462
- {
1463
- signal: input.signal,
1464
- attemptId: (attempt) => `${manifest.runId}:${task.id}:attempt-${attempt}`,
1465
- onAttemptFailed: (attempt, error, delayMs, info) => {
1466
- lastAttemptId = info.attemptId;
1467
- appendEventAsync(manifest.eventsPath, {
1468
- type: "crew.task.retry_attempt",
1469
- runId: manifest.runId,
1470
- taskId: task.id,
1471
- message: error.message,
1472
- data: {
1473
- attempt,
1474
- attemptId: info.attemptId,
1475
- delayMs,
1476
- },
1477
- metadata: { attemptId: info.attemptId },
1478
- }).catch((error) => logInternalError("team-runner.retry-attempt", error, `taskId=${task.id}`));
1479
- input.metricRegistry?.histogram("crew.task.retry_delay_ms", "Retry backoff delay, milliseconds").observe(
1480
- {
1481
- runId: manifest.runId,
1482
- taskId: task.id,
1483
- },
1484
- delayMs,
1485
- );
1486
- },
1487
- onRetryGivenUp: (attempts, error, info) => {
1488
- lastAttemptId = info.attemptId;
1489
- appendDeadletter(manifest, {
1490
- runId: manifest.runId,
1491
- taskId: task.id,
1492
- reason: "max-retries",
1493
- attempts,
1494
- attemptId: info.attemptId,
1495
- lastError: error.message,
1496
- timestamp: new Date().toISOString(),
1497
- });
1498
- input.metricRegistry
1499
- ?.counter("crew.task.deadletter_total", "Deadletter triggers by reason")
1500
- .inc({ reason: "max-retries" });
1501
- input.metricRegistry?.histogram("crew.task.retry_count", "Retries per task", [0, 1, 2, 3, 5, 10]).observe(
1502
- {
1503
- runId: manifest.runId,
1504
- team: input.team.name,
1505
- },
1506
- Math.max(0, attempts - 1),
1507
- );
1508
- },
1509
- },
1510
- );
1511
- } catch (retryError) {
1512
- if (retryError instanceof CrewCancellationError || input.signal?.aborted) {
1513
- const reason =
1514
- retryError instanceof CrewCancellationError ? retryError.reason : cancellationReasonFromSignal(input.signal);
1515
- // NOTE: no withRunLock — best-effort only; concurrent writes may cause inconsistency
1516
- const fresh = loadRunManifestById(manifest.cwd, manifest.runId);
1517
- const freshManifest = fresh?.manifest ?? manifest;
1518
- const freshTasks = fresh?.tasks ?? tasks;
1519
- const cancelledTasks = freshTasks.map((item) =>
1520
- item.id === task.id && (item.status === "queued" || item.status === "running")
1521
- ? {
1522
- ...item,
1523
- status: "cancelled" as const,
1524
- finishedAt: new Date().toISOString(),
1525
- error: `${reason.message} (${reason.code})`,
1526
- }
1527
- : item,
1528
- );
1529
- appendEventAsync(freshManifest.eventsPath, {
1530
- type: "task.cancelled",
1531
- runId: freshManifest.runId,
1532
- taskId: task.id,
1533
- message: reason.message,
1534
- data: { reason, phase: "retry" },
1535
- metadata: lastAttemptId ? { attemptId: lastAttemptId } : undefined,
1536
- }).catch((error) => logInternalError("team-runner.cancelled", error, `taskId=${task.id}`));
1537
- return {
1538
- manifest: updateRunStatus(freshManifest, "cancelled", reason.message),
1539
- tasks: cancelledTasks,
1540
- };
1541
- }
1542
- if (lastFailed) return lastFailed;
1543
- // NOTE: no withRunLock — best-effort only; concurrent writes may cause inconsistency
1544
- const fresh = loadRunManifestById(manifest.cwd, manifest.runId);
1545
- const freshManifest = fresh?.manifest ?? manifest;
1546
- const freshTasks = fresh?.tasks ?? tasks;
1547
- const freshTask = freshTasks.find((item) => item.id === task.id) ?? task;
1548
- if (freshTask.status !== "queued" && freshTask.status !== "running") return { manifest: freshManifest, tasks: freshTasks };
1549
- return withCorrelation(childCorrelation(freshManifest.runId, task.id), () =>
1550
- runTeamTask({
1551
- ...baseInput,
1552
- manifest: freshManifest,
1553
- tasks: freshTasks,
1554
- task: freshTask,
1555
- }),
1556
- );
1557
- }
1558
- };
1559
- // ── OPT-01 streaming dispatch: dispatch units into pendingUnits ──
1560
- for (const unit of dispatchUnits) {
1561
- const unitKey = unit.kind === "singleton" ? unit.taskId : unit.group.id;
1562
- const unitTaskIds = unit.kind === "singleton" ? [unit.taskId] : unit.group.tasks.map((t) => t.id);
1563
- pendingUnits.set(unitKey, {
1564
- taskIds: unitTaskIds,
1565
- promise: dispatchUnit(unit),
1566
- });
1567
- }
1568
-
1569
- // ── OPT-01 wait phase: if no units are in-flight (e.g. all ready tasks
1570
- // were hook-skipped or readyBatch was empty), re-loop to re-evaluate. ──
1571
- if (pendingUnits.size === 0) continue;
1572
-
1573
- // Wait for ONE in-flight unit to complete. Promise.race returns as soon
1574
- // as the first wrapper resolves — others remain pending in pendingUnits
1575
- // and are re-raced on the next iteration.
1576
- const settled = await Promise.race(
1577
- [...pendingUnits.entries()].map(async ([key, pending]) => {
1578
- try {
1579
- const result = await pending.promise;
1580
- return {
1581
- unitKey: key,
1582
- result: result as { manifest: TeamRunManifest; tasks: TeamTaskState[] } | undefined,
1583
- error: undefined as Error | undefined,
1584
- };
1585
- } catch (error) {
1586
- return { unitKey: key, result: undefined, error: error instanceof Error ? error : new Error(String(error)) };
1587
- }
1588
- }),
1589
- );
1590
- const completedUnit = pendingUnits.get(settled.unitKey)!;
1591
- pendingUnits.delete(settled.unitKey);
1592
-
1593
- // Build the single result to merge. On rejection, synthesize a failed
1594
- // result so the run continues (mirrors the old validResults guard).
1595
- const resultToMerge: { manifest: TeamRunManifest; tasks: TeamTaskState[] } = settled.result ?? {
1596
- manifest,
1597
- tasks: tasks.map((t) =>
1598
- completedUnit.taskIds.includes(t.id)
1599
- ? { ...t, status: "failed" as const, error: settled.error!.message, finishedAt: new Date().toISOString() }
1600
- : t,
1601
- ),
1602
- };
1603
- const validResults = [resultToMerge];
1604
- // Reconstruct manifest from the last worker's snapshot. The .artifacts field
1605
- // is re-merged from both the team-runner's in-memory state and all workers'
1606
- // snapshots, so artifact writes by task-runner (which individually save manifest
1607
- // after writing artifacts) are safely persisted. The in-memory manifest is only
1608
- // used for the next batch iteration's orchestration — actual persistence is safe.
1609
- // Use updateRunStatus to recompute manifest status from merged tasks rather than
1610
- // relying on the last result's manifest (which is arbitrary due to mapConcurrent
1611
- // returning results in arbitrary order).
1612
- // Use the in-memory manifest as base (not the last-completing worker's snapshot).
1613
- // Recompute status from merged tasks so the manifest reflects actual task state,
1614
- // not the arbitrary order in which mapConcurrent returned results.
1615
- // Read committed manifest from disk inside the lock so artifact merge is based
1616
- // on committed state, not in-memory state that may differ from disk.
1617
- const mergeResult = await withRunLock(manifest, async () => {
1618
- // NEW-D1: flush any pending coalesced atomic writes before reading from
1619
- // disk. Without this, a worker's async manifest save (coalesced by
1620
- // atomic-write) may not be committed yet, causing a lost-update on the
1621
- // merge read. flushPendingAtomicWrites forces all queued writes to disk.
1622
- flushPendingAtomicWrites();
1623
- const disk = loadRunManifestById(manifest.cwd, manifest.runId);
1624
- const diskManifest = disk?.manifest ?? manifest;
1625
- const diskArtifacts = diskManifest.artifacts;
1626
- const reconciledArtifacts = mergeArtifacts([...diskArtifacts, ...validResults.map((item) => item.manifest.artifacts)].flat());
1627
- const resultManifest = updateRunStatus(
1628
- { ...diskManifest, artifacts: reconciledArtifacts },
1629
- "running",
1630
- "Merged task updates from parallel batch.",
1631
- );
1632
- // CANCEL-1: use the freshly-loaded disk tasks as the merge base instead
1633
- // of the in-memory `tasks` closure variable. The in-memory tasks reflect
1634
- // only team-runner's view; an external cancel (handleCancel, background
1635
- // race with SIGTERM arriving after cancel wrote but before merge ran)
1636
- // writes 'cancelled' to disk.tasks — using disk.tasks as base preserves
1637
- // that cancellation through the merge instead of overwriting it with the
1638
- // stale in-memory view. disk was loaded inside this lock, so it reflects
1639
- // the freshest committed state.
1640
- const resultTasks = mergeTaskUpdatesPreservingTerminal(disk?.tasks ?? tasks, validResults);
1641
- await saveRunManifestAsync(resultManifest);
1642
- await saveRunTasksAsync(resultManifest, resultTasks);
1643
- return { resultManifest, resultTasks };
1644
- });
1645
- manifest = mergeResult.resultManifest;
1646
- tasks = mergeResult.resultTasks;
1647
-
1648
- // Advance workflow phases whose tasks are all in terminal state
1649
- const terminalStatuses = new Set(["completed", "failed", "skipped", "cancelled", "needs_attention"]);
1650
- const phaseTaskMap = new Map<string, string[]>();
1651
- for (const task of tasks) {
1652
- if (!task.stepId) continue;
1653
- const existing = phaseTaskMap.get(task.stepId) ?? [];
1654
- existing.push(task.id);
1655
- phaseTaskMap.set(task.stepId, existing);
1656
- }
1657
- for (let pi = wfMachine.currentPhaseIndex; pi < wfMachine.phases.length; pi++) {
1658
- const phase = wfMachine.phases[pi]!;
1659
- const phaseTaskIds = phaseTaskMap.get(phase.name) ?? [];
1660
- if (phaseTaskIds.length === 0) continue;
1661
- const allTerminal = phaseTaskIds.every((taskId) => {
1662
- const task = tasks.find((t) => t.id === taskId);
1663
- return task ? terminalStatuses.has(task.status) : false;
1664
- });
1665
- if (!allTerminal) break;
1666
- if (phase.status !== "completed" && phase.status !== "failed" && phase.status !== "skipped") {
1667
- const completedArtifacts = manifest.artifacts.filter((a) => a.kind === "result" || a.kind === "summary").map((a) => a.path);
1668
- const previousPhaseStatus = pi > 0 ? (wfMachine.phases[pi - 1]?.status ?? "pending") : "completed";
1669
- const wfContext: PhaseGuardContext = {
1670
- completedArtifacts,
1671
- previousPhaseStatus,
1672
- taskResults: tasks
1673
- .filter((t) => t.status === "completed" || t.status === "needs_attention")
1674
- .map((t) => ({
1675
- taskId: t.id,
1676
- status: t.status,
1677
- outputPath: t.resultArtifact?.path,
1678
- })),
1679
- };
1680
- // Determine phase transition status based on individual task outcomes
1681
- const phaseTasks = phaseTaskIds
1682
- .map((taskId) => tasks.find((t) => t.id === taskId))
1683
- .filter((t): t is NonNullable<typeof t> => t !== undefined);
1684
- const hasFailedOrCancelled = phaseTasks.some((t) => t.status === "failed" || t.status === "cancelled");
1685
- const phaseStatus = hasFailedOrCancelled ? "failed" : "completed";
1686
- const transition = transitionPhase(wfMachine, pi, phaseStatus, wfContext);
1687
- wfMachine = transition.machine;
1688
- if (transition.guardResult && !transition.guardResult.allowed) {
1689
- await appendEventAsync(manifest.eventsPath, {
1690
- type: "workflow.phase_guard_blocked",
1691
- runId: manifest.runId,
1692
- message: `Workflow phase '${phase.name}' guard blocked: ${transition.guardResult.reason ?? "unknown"}`,
1693
- data: {
1694
- phaseIndex: pi,
1695
- phaseName: phase.name,
1696
- reason: transition.guardResult.reason,
1697
- },
1698
- });
1699
- break;
1700
- }
1701
- await appendEventAsync(manifest.eventsPath, {
1702
- type: phaseStatus === "failed" ? "workflow.phase_failed" : "workflow.phase_completed",
1703
- runId: manifest.runId,
1704
- message: `Workflow phase '${phase.name}' ${phaseStatus}.`,
1705
- data: { phaseIndex: pi, phaseStatus },
1706
- });
1707
- }
1708
- wfMachine = { ...wfMachine, currentPhaseIndex: pi + 1 };
1709
- }
1710
-
1711
- // Per-task budget enforcement: check cumulative usage after each batch merge.
1712
- // This prevents a single task from consuming 100% of the budget before
1713
- // abort triggers (the goal-loop only checks at turn boundaries).
1714
- if (input.budgetTotal !== undefined && input.budgetTotal > 0 && input.budgetUnlimited !== true) {
1715
- const warnThreshold = input.budgetWarning ?? 0.8;
1716
- const abortThreshold = input.budgetAbort ?? 0.95;
1717
- const budgetCheck = checkPerTaskBudget(tasks, input.budgetTotal, warnThreshold, abortThreshold);
1718
-
1719
- if (budgetCheck.abort) {
1720
- const message = `Per-task budget abort threshold exceeded: ${formatTokens(budgetCheck.totalUsed)}/${formatTokens(input.budgetTotal)} (${Math.round((budgetCheck.totalUsed / input.budgetTotal) * 100)}%)`;
1721
- console.warn(`[team-runner] ${message}`);
1722
- await appendEventAsync(manifest.eventsPath, {
1723
- type: "run.budget_abort",
1724
- runId: manifest.runId,
1725
- message,
1726
- data: {
1727
- budgetTotal: input.budgetTotal,
1728
- budgetUsed: budgetCheck.totalUsed,
1729
- threshold: "abort",
1730
- },
1731
- });
1732
- tasks = markBlocked(tasks, `Budget abort threshold exceeded: ${message}`);
1733
- await saveRunTasksAsync(manifest, tasks);
1734
- manifest = updateRunStatus(manifest, "failed", message);
1735
- return { manifest, tasks };
1736
- }
1737
-
1738
- if (budgetCheck.warning) {
1739
- const message = `Per-task budget warning threshold crossed: ${formatTokens(budgetCheck.totalUsed)}/${formatTokens(input.budgetTotal)} (${Math.round((budgetCheck.totalUsed / input.budgetTotal) * 100)}%)`;
1740
- console.warn(`[team-runner] ${message}`);
1741
- await appendEventAsync(manifest.eventsPath, {
1742
- type: "run.budget_warning",
1743
- runId: manifest.runId,
1744
- message,
1745
- data: {
1746
- budgetTotal: input.budgetTotal,
1747
- budgetUsed: budgetCheck.totalUsed,
1748
- threshold: "warning",
1749
- },
1750
- });
1751
- }
1752
-
1753
- // Fair-share warning: flag tasks that consumed >50% of remaining budget
1754
- // without killing them mid-execution.
1755
- for (const violatorId of budgetCheck.fairShareViolators) {
1756
- const violator = tasks.find((t) => t.id === violatorId);
1757
- if (!violator) continue;
1758
- const taskTotal = (violator.usage?.input ?? 0) + (violator.usage?.output ?? 0) + (violator.usage?.cacheWrite ?? 0);
1759
- const message = `Task '${violatorId}' consumed ${formatTokens(taskTotal)} (${Math.round((taskTotal / input.budgetTotal) * 100)}% of total budget) — exceeds fair share`;
1760
- console.warn(`[team-runner.fair-share] ${message}`);
1761
- await appendEventAsync(manifest.eventsPath, {
1762
- type: "task.budget_fair_share",
1763
- runId: manifest.runId,
1764
- taskId: violatorId,
1765
- message,
1766
- data: {
1767
- budgetTotal: input.budgetTotal,
1768
- taskUsage: taskTotal,
1769
- },
1770
- });
1771
- }
1772
- }
1773
-
1774
- const cancelledResult = resultToMerge.manifest.status === "cancelled" ? resultToMerge : undefined;
1775
- if (cancelledResult || input.signal?.aborted) {
1776
- const reason = input.signal?.aborted ? cancellationReasonFromSignal(input.signal) : undefined;
1777
- const message = reason?.message ?? cancelledResult?.manifest.summary ?? "Run cancelled during task execution.";
1778
- manifest = { ...manifest, status: "running" };
1779
- manifest = updateRunStatus(manifest, "cancelled", message);
1780
- // CANCEL-2: re-cancel non-terminal tasks here, mirroring the batch-loop
1781
- // cancel check at team-runner.ts:~925. A manifest cancelled mid-merge
1782
- // (e.g. signal abort during the merge's awaits) would otherwise save
1783
- // tasks without the cancel applied, leaving status=cancelled but tasks
1784
- // showing completed/running -- inconsistent and breaks handleRetry's
1785
- // filter for failed/cancelled tasks. Terminal tasks are NOT clobbered.
1786
- const cancelMessage = reason ? `${message} (${reason.code})` : message;
1787
- const reCancelledTasks = tasks.map((task) => {
1788
- if (task.status !== "queued" && task.status !== "running" && task.status !== "waiting") return task;
1789
- return {
1790
- ...task,
1791
- status: "cancelled" as const,
1792
- finishedAt: new Date().toISOString(),
1793
- error: cancelMessage,
1794
- };
1795
- });
1796
- await saveRunTasksAsync(manifest, reCancelledTasks);
1797
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, reCancelledTasks, runtimeKind));
1798
- await saveRunManifestAsync(manifest);
1799
- await appendEventAsync(manifest.eventsPath, {
1800
- type: "run.cancelled",
1801
- runId: manifest.runId,
1802
- message,
1803
- data: {
1804
- reason,
1805
- phase: "task-batch",
1806
-
1807
- cancelledResultRunId: cancelledResult?.manifest.runId,
1808
- },
1809
- });
1810
- return { manifest, tasks: reCancelledTasks };
1811
- }
1812
- queueIndex = buildTaskGraphIndex(tasks);
1813
- const injectedAfterBatch = await attemptAdaptivePlan();
1814
- if (injectedAfterBatch.missing) {
1815
- tasks = markBlocked(tasks, "Adaptive planner did not produce a valid subagent plan.");
1816
- await saveRunTasksAsync(manifest, tasks);
1817
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
1818
- manifest = updateRunStatus(manifest, "blocked", "Adaptive planner did not produce a valid subagent plan.");
1819
- return { manifest, tasks };
1820
- }
1821
- if (injectedAfterBatch.injected) {
1822
- manifest = requiresPlanApproval(workflow, input.runtimeConfig) ? await ensurePlanApprovalRequested(manifest, tasks) : manifest;
1823
- queueIndex = buildTaskGraphIndex(tasks);
1824
- } else if (
1825
- requiresPlanApproval(workflow, input.runtimeConfig) &&
1826
- (hasPendingMutatingAdaptiveTask(tasks) || hasPendingMutatingTaskAtBoundary(tasks))
1827
- ) {
1828
- manifest = await ensurePlanApprovalRequested(manifest, tasks);
1829
- }
1830
- if (manifest.planApproval?.status === "cancelled") {
1831
- tasks = cancelPlanTasks(tasks, "Plan approval was cancelled.");
1832
- await saveRunTasksAsync(manifest, tasks);
1833
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
1834
- manifest = updateRunStatus(manifest, "cancelled", "Plan approval was cancelled.");
1835
- return { manifest, tasks };
1836
- }
1837
- await saveRunTasksAsync(manifest, tasks);
1838
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
1839
- const completedBatch = tasks.filter((t) => completedUnit.taskIds.includes(t.id));
1840
- const batchArtifact = writeArtifact(manifest.artifactsRoot, {
1841
- kind: "summary",
1842
- relativePath: `batches/${completedUnit.taskIds.join("+")}.md`,
1843
- producer: "team-runner",
1844
- content: aggregateTaskOutputs(completedBatch, manifest),
1845
- });
1846
- const groupDelivery = deliverGroupJoin({
1847
- manifest,
1848
- mode: resolveGroupJoinMode(input.runtimeConfig),
1849
- batch: completedBatch,
1850
- allTasks: tasks,
1851
- });
1852
- manifest = {
1853
- ...manifest,
1854
- artifacts: mergeArtifacts([...manifest.artifacts, batchArtifact, ...(groupDelivery?.artifact ? [groupDelivery.artifact] : [])]),
1855
- };
1856
- manifest = writeProgress(manifest, tasks, "team-runner", input.executeWorkers, input.runtimeConfig);
1857
- await saveRunManifestAsync(manifest);
1858
- }
1859
-
1860
- const failed = tasks.find((task) => task.status === "failed");
1861
- const waiting = tasks.find((task) => task.status === "waiting");
1862
- const running = tasks.find((task) => task.status === "running");
1863
- manifest = applyPolicy(manifest, tasks, input.limits);
1864
-
1865
- // S02: Verify workflow-declared output files exist before marking completed
1866
- if (input.workflow?.steps) {
1867
- const missingOutputs: string[] = [];
1868
- for (const step of input.workflow.steps) {
1869
- if (step.output && typeof step.output === "string") {
1870
- const outputPath = path.join(manifest.artifactsRoot, step.output);
1871
- if (!fs.existsSync(outputPath)) {
1872
- missingOutputs.push(step.output);
1873
- }
1874
- }
1875
- }
1876
- if (missingOutputs.length > 0) {
1877
- // Emit warning event — run still completes normally to avoid hanging
1878
- appendEventFireAndForget(manifest.eventsPath, {
1879
- type: "run.deliverable_warning",
1880
- runId: manifest.runId,
1881
- message: `Missing workflow output files: ${missingOutputs.join(", ")}`,
1882
- data: { missingFiles: missingOutputs },
1883
- });
1884
- }
1885
- }
1886
-
1887
- const effectiveness = evaluateRunEffectiveness({
1888
- manifest,
1889
- tasks,
1890
- executeWorkers: input.executeWorkers,
1891
- runtimeConfig: input.runtimeConfig,
1892
- });
1893
- const effectivenessDecision = effectivenessPolicyDecision(effectiveness);
1894
- if (effectivenessDecision) {
1895
- manifest = {
1896
- ...manifest,
1897
- policyDecisions: [...(manifest.policyDecisions ?? []), effectivenessDecision],
1898
- updatedAt: new Date().toISOString(),
1899
- };
1900
- await appendEventAsync(manifest.eventsPath, {
1901
- type: "run.effectiveness",
1902
- runId: manifest.runId,
1903
- message: effectivenessDecision.message,
1904
- data: { effectiveness, policyDecision: effectivenessDecision },
1905
- });
1906
- }
1907
- const blockingDecision = manifest.policyDecisions?.find((item) => item.action === "block" || item.action === "escalate");
1908
- if (failed) {
1909
- manifest = updateRunStatus(manifest, "failed", `Failed at task '${failed.id}'.`);
1910
- } else if (waiting) {
1911
- manifest = updateRunStatus(manifest, "blocked", `Waiting for response to task '${waiting.id}'.`);
1912
- } else if (running) {
1913
- manifest = updateRunStatus(manifest, "blocked", `Task '${running.id}' is still running.`);
1914
- } else if (effectiveness.severity === "failed") {
1915
- manifest = updateRunStatus(manifest, "failed", effectivenessDecision?.message ?? "Run effectiveness guard failed.");
1916
- } else if (effectiveness.severity === "blocked") {
1917
- manifest = updateRunStatus(manifest, "blocked", effectivenessDecision?.message ?? "Run effectiveness guard blocked completion.");
1918
- } else if (blockingDecision) {
1919
- manifest = updateRunStatus(manifest, "blocked", blockingDecision.message);
1920
- } else if (tasks.some((task) => task.status === "queued")) {
1921
- // F1 defense-in-depth: the loop exited with queued tasks still pending
1922
- // (e.g. a hook skipped all ready tasks and downstream tasks never became
1923
- // runnable). This is NOT a completed run — mark it blocked rather than
1924
- // false-green "completed".
1925
- manifest = updateRunStatus(manifest, "blocked", "Run exited with queued tasks still pending.");
1926
- } else {
1927
- manifest = updateRunStatus(
1928
- manifest,
1929
- "completed",
1930
- input.executeWorkers ? "Team workflow completed." : "Team workflow scaffold completed without launching child workers.",
1931
- );
1932
- }
1933
- manifest = writeProgress(manifest, tasks, "team-runner", input.executeWorkers, input.runtimeConfig);
1934
- await saveRunManifestAsync(manifest);
1935
- const usage = aggregateUsage(tasks);
1936
- const summaryArtifact = writeArtifact(manifest.artifactsRoot, {
1937
- kind: "summary",
1938
- relativePath: "summary.md",
1939
- producer: "team-runner",
1940
- content: [
1941
- `# pi-crew run ${manifest.runId}`,
1942
- "",
1943
- `Status: ${manifest.status}`,
1944
- `Team: ${manifest.team}`,
1945
- `Workflow: ${manifest.workflow ?? "(none)"}`,
1946
- `Goal: ${manifest.goal}`,
1947
- `Usage: ${formatUsage(usage)}`,
1948
- "",
1949
- "## Tasks",
1950
- ...tasks.map(formatTaskProgress),
1951
- "",
1952
- "## Effectiveness",
1953
- ...runEffectivenessLines(manifest, tasks, input.executeWorkers, input.runtimeConfig),
1954
- "",
1955
- "## Policy decisions",
1956
- ...(manifest.policyDecisions?.length ? summarizePolicyDecisions(manifest.policyDecisions) : ["- (none)"]),
1957
- "",
1958
- ].join("\n"),
1959
- });
1960
- // Build the complete manifest BEFORE acquiring the lock so the artifacts array
1961
- // is already incorporated into the manifest object that will be atomically written.
1962
- // This prevents crash-between-mutation-and-lock from leaving inconsistent state.
1963
- const finalManifest = {
1964
- ...manifest,
1965
- updatedAt: new Date().toISOString(),
1966
- artifacts: [...manifest.artifacts, summaryArtifact],
1967
- };
1968
- // Joint atomic save: wrap manifest + tasks in a single run lock so they are
1969
- // written together or not at all. Crash between separate saveRunManifestAsync
1970
- // and saveRunTasksAsync calls could leave manifest/tasks.json out of sync.
1971
- await withRunLock(finalManifest, async () => {
1972
- await saveRunManifestAsync(finalManifest);
1973
- await saveRunTasksAsync(finalManifest, tasks);
1974
- });
1975
- manifest = finalManifest;
1976
- // Save health snapshot on run completion.
1977
- // BUG A (pts/2 hang investigation 2026-06-16): stateRoot = `<crewRoot>/state/runs/<runId>`,
1978
- // so the crew root is THREE dirnames up, not two. Two dirnames gave `<crewRoot>/state`
1979
- // (the state dir), and HealthStore then joined HEALTH_DIR (`.crew/state/health`)
1980
- // onto it → `<crewRoot>/state/.crew/state/health` — a double-joined BOGUS path.
1981
- // That wrote health snapshots to a nonexistent subtree (silently breaking the
1982
- // health feature) AND created junk dirs that the recursive state watcher then
1983
- // attached extra inotify watches to. Fix: compute the real crew root (3 up)
1984
- // and make HEALTH_DIR relative to it.
1985
- const crewRoot = path.dirname(path.dirname(path.dirname(finalManifest.stateRoot)));
1986
- const healthStore = new HealthStore(crewRoot);
1987
- healthStore.saveSnapshot({
1988
- runId: finalManifest.runId,
1989
- tasks: tasks.map((t) => ({ id: t.id, status: t.status })),
1990
- createdAt: finalManifest.createdAt,
1991
- });
1992
- return { manifest, tasks };
1993
- }