pi-crew 0.2.3 → 0.2.5

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 (348) hide show
  1. package/AGENTS.md +57 -32
  2. package/CHANGELOG.md +466 -448
  3. package/LICENSE +21 -21
  4. package/NOTICE.md +16 -16
  5. package/README.md +323 -323
  6. package/docs/FEATURE_INTAKE.md +126 -0
  7. package/docs/HARNESS.md +86 -0
  8. package/docs/HARNESS_BACKLOG.md +41 -0
  9. package/docs/TEST_MATRIX.md +49 -0
  10. package/docs/actions-reference.md +595 -595
  11. package/docs/architecture.md +180 -180
  12. package/docs/code-review-2026-05-11.md +592 -592
  13. package/docs/commands-reference.md +347 -347
  14. package/docs/comparison-pi-subagents-vs-pi-crew.md +303 -0
  15. package/docs/decisions/0001-durable-state.md +41 -0
  16. package/docs/decisions/0002-child-process-for-async.md +42 -0
  17. package/docs/decisions/0003-depth-guard.md +36 -0
  18. package/docs/decisions/0004-execfile-over-exec.md +34 -0
  19. package/docs/decisions/0005-no-parameter-properties.md +49 -0
  20. package/docs/decisions/0006-publish-bundled-esm.md +63 -0
  21. package/docs/decisions/0007-active-run-binary-index.md +54 -0
  22. package/docs/decisions/0008-child-pi-warm-pool.md +61 -0
  23. package/docs/decisions/README.md +23 -0
  24. package/docs/followup-review-round4-2026-05-13.md +107 -0
  25. package/docs/implementation-plan-top3.md +333 -0
  26. package/docs/live-mailbox-runtime.md +36 -36
  27. package/docs/next-upgrade-roadmap.md +808 -808
  28. package/docs/oh-my-pi-research.md +509 -0
  29. package/docs/perf/baseline-2026-05.md +113 -0
  30. package/docs/perf/final-report-2026-05.md +206 -0
  31. package/docs/perf/sprint-1-report.md +71 -0
  32. package/docs/perf/sprint-2-report.md +81 -0
  33. package/docs/perf/sprint-2.5-report.md +53 -0
  34. package/docs/perf/sprint-3-report.md +36 -0
  35. package/docs/perf/sprint-4-report.md +47 -0
  36. package/docs/perf/sprint-5-report.md +51 -0
  37. package/docs/perf/sprint-6-report.md +94 -0
  38. package/docs/perf/sprint-7-report.md +74 -0
  39. package/docs/perf/upgrade-plan-2026-05.md +147 -0
  40. package/docs/pi-subagents3-deep-analysis.md +508 -0
  41. package/docs/product/README.md +31 -0
  42. package/docs/product/platform.md +27 -0
  43. package/docs/product/runtime-safety.md +37 -0
  44. package/docs/product/team-run.md +39 -0
  45. package/docs/product/team-tool.md +37 -0
  46. package/docs/publishing.md +65 -65
  47. package/docs/resource-formats.md +134 -134
  48. package/docs/runtime-analysis-child-vs-live.md +171 -0
  49. package/docs/runtime-flow.md +148 -148
  50. package/docs/runtime-migration-in-process-analysis.md +250 -0
  51. package/docs/stories/README.md +30 -0
  52. package/docs/stories/backlog.md +36 -0
  53. package/docs/templates/decision.md +27 -0
  54. package/docs/templates/story.md +44 -0
  55. package/docs/templates/validation-report.md +32 -0
  56. package/docs/usage.md +238 -238
  57. package/index.ts +7 -6
  58. package/install.mjs +65 -65
  59. package/package.json +107 -100
  60. package/schema.json +222 -222
  61. package/skills/child-pi-spawning/SKILL.md +213 -0
  62. package/skills/context-artifact-hygiene/SKILL.md +32 -0
  63. package/skills/event-log-tracing/SKILL.md +299 -0
  64. package/skills/git-master/SKILL.md +225 -24
  65. package/skills/live-agent-lifecycle/SKILL.md +192 -0
  66. package/skills/mailbox-interactive/SKILL.md +300 -19
  67. package/skills/model-routing-context/SKILL.md +94 -0
  68. package/skills/multi-perspective-review/SKILL.md +88 -0
  69. package/skills/read-only-explorer/SKILL.md +250 -26
  70. package/skills/safe-bash/SKILL.md +307 -21
  71. package/skills/verification-before-done/SKILL.md +11 -2
  72. package/skills/widget-rendering/SKILL.md +258 -0
  73. package/skills/workspace-isolation/SKILL.md +202 -0
  74. package/skills/worktree-isolation/SKILL.md +202 -18
  75. package/src/adapters/claude-adapter.ts +25 -25
  76. package/src/adapters/codex-adapter.ts +21 -21
  77. package/src/adapters/cursor-adapter.ts +17 -17
  78. package/src/adapters/export-util.ts +137 -137
  79. package/src/adapters/index.ts +15 -15
  80. package/src/adapters/registry.ts +18 -18
  81. package/src/adapters/types.ts +23 -23
  82. package/src/agents/agent-config.ts +38 -38
  83. package/src/agents/agent-serializer.ts +38 -38
  84. package/src/agents/discover-agents.ts +121 -118
  85. package/src/config/config.ts +740 -858
  86. package/src/config/defaults.ts +96 -96
  87. package/src/config/drift-detector.ts +211 -211
  88. package/src/config/markers.ts +327 -327
  89. package/src/config/resilient-parser.ts +109 -108
  90. package/src/config/suggestions.ts +74 -74
  91. package/src/config/types.ts +199 -0
  92. package/src/extension/async-notifier.ts +123 -89
  93. package/src/extension/autonomous-policy.ts +169 -169
  94. package/src/extension/cross-extension-rpc.ts +104 -104
  95. package/src/extension/help.ts +47 -47
  96. package/src/extension/import-index.ts +69 -69
  97. package/src/extension/management.ts +395 -382
  98. package/src/extension/notification-router.ts +116 -116
  99. package/src/extension/notification-sink.ts +51 -51
  100. package/src/extension/project-init.ts +168 -168
  101. package/src/extension/register.ts +859 -668
  102. package/src/extension/registration/artifact-cleanup.ts +15 -15
  103. package/src/extension/registration/command-utils.ts +54 -54
  104. package/src/extension/registration/commands.ts +559 -452
  105. package/src/extension/registration/compaction-guard.ts +125 -125
  106. package/src/extension/registration/subagent-helpers.ts +102 -102
  107. package/src/extension/registration/subagent-tools.ts +220 -159
  108. package/src/extension/registration/team-tool.ts +159 -99
  109. package/src/extension/registration/viewers.ts +29 -0
  110. package/src/extension/result-watcher.ts +128 -128
  111. package/src/extension/run-bundle-schema.ts +89 -89
  112. package/src/extension/run-export.ts +73 -73
  113. package/src/extension/run-import.ts +84 -84
  114. package/src/extension/run-index.ts +94 -94
  115. package/src/extension/run-maintenance.ts +142 -142
  116. package/src/extension/session-summary.ts +8 -8
  117. package/src/extension/team-manager-command.ts +96 -96
  118. package/src/extension/team-recommendation.ts +188 -188
  119. package/src/extension/team-tool/api.ts +5 -2
  120. package/src/extension/team-tool/cancel.ts +224 -209
  121. package/src/extension/team-tool/config-patch.ts +36 -36
  122. package/src/extension/team-tool/context.ts +60 -60
  123. package/src/extension/team-tool/doctor.ts +242 -242
  124. package/src/extension/team-tool/handle-settings.ts +421 -195
  125. package/src/extension/team-tool/inspect.ts +41 -41
  126. package/src/extension/team-tool/lifecycle-actions.ts +139 -139
  127. package/src/extension/team-tool/parallel-dispatch.ts +156 -156
  128. package/src/extension/team-tool/plan.ts +19 -19
  129. package/src/extension/team-tool/respond.ts +112 -111
  130. package/src/extension/team-tool/run.ts +246 -229
  131. package/src/extension/team-tool/status.ts +110 -110
  132. package/src/extension/team-tool-types.ts +13 -13
  133. package/src/extension/team-tool.ts +344 -344
  134. package/src/extension/tool-result.ts +16 -16
  135. package/src/extension/validate-resources.ts +77 -77
  136. package/src/hooks/registry.ts +61 -61
  137. package/src/hooks/types.ts +40 -40
  138. package/src/i18n.ts +184 -184
  139. package/src/observability/correlation.ts +35 -35
  140. package/src/observability/event-to-metric.ts +68 -68
  141. package/src/observability/exporters/adapter.ts +30 -30
  142. package/src/observability/exporters/otlp-exporter.ts +106 -92
  143. package/src/observability/exporters/prometheus-exporter.ts +54 -54
  144. package/src/observability/metric-registry.ts +87 -87
  145. package/src/observability/metric-retention.ts +54 -54
  146. package/src/observability/metric-sink.ts +81 -56
  147. package/src/observability/metrics-primitives.ts +167 -167
  148. package/src/prompt/prompt-runtime.ts +72 -72
  149. package/src/runtime/adaptive-plan.ts +338 -0
  150. package/src/runtime/agent-control.ts +169 -169
  151. package/src/runtime/agent-memory.ts +72 -72
  152. package/src/runtime/agent-observability.ts +114 -114
  153. package/src/runtime/async-marker.ts +26 -26
  154. package/src/runtime/async-runner.ts +153 -153
  155. package/src/runtime/attention-events.ts +28 -28
  156. package/src/runtime/auto-resume.ts +100 -100
  157. package/src/runtime/background-runner.ts +122 -89
  158. package/src/runtime/cancellation.ts +61 -61
  159. package/src/runtime/capability-inventory.ts +116 -116
  160. package/src/runtime/child-pi-pool.ts +68 -0
  161. package/src/runtime/child-pi.ts +541 -461
  162. package/src/runtime/code-summary.ts +247 -247
  163. package/src/runtime/compaction-summary.ts +271 -271
  164. package/src/runtime/concurrency.ts +58 -58
  165. package/src/runtime/crash-recovery.ts +317 -301
  166. package/src/runtime/crew-agent-records.ts +379 -281
  167. package/src/runtime/crew-agent-runtime.ts +60 -60
  168. package/src/runtime/cross-extension-rpc.ts +72 -0
  169. package/src/runtime/custom-tools/irc-tool.ts +201 -201
  170. package/src/runtime/custom-tools/submit-result-tool.ts +90 -90
  171. package/src/runtime/deadletter.ts +47 -47
  172. package/src/runtime/delivery-coordinator.ts +176 -176
  173. package/src/runtime/delta-conflict.ts +360 -360
  174. package/src/runtime/diagnostic-export.ts +102 -102
  175. package/src/runtime/direct-run.ts +35 -35
  176. package/src/runtime/effectiveness.ts +82 -81
  177. package/src/runtime/errors/crew-errors.ts +166 -0
  178. package/src/runtime/event-stream-bridge.ts +92 -92
  179. package/src/runtime/foreground-control.ts +82 -82
  180. package/src/runtime/green-contract.ts +46 -46
  181. package/src/runtime/group-join.ts +234 -106
  182. package/src/runtime/heartbeat-watcher.ts +145 -124
  183. package/src/runtime/iteration-hooks.ts +267 -267
  184. package/src/runtime/live-agent-control.ts +88 -88
  185. package/src/runtime/live-agent-manager.ts +377 -179
  186. package/src/runtime/live-control-realtime.ts +36 -36
  187. package/src/runtime/live-session-runtime.ts +676 -600
  188. package/src/runtime/loop-gates.ts +129 -129
  189. package/src/runtime/manifest-cache.ts +263 -263
  190. package/src/runtime/mcp-proxy.ts +113 -113
  191. package/src/runtime/metric-parser.ts +40 -40
  192. package/src/runtime/model-fallback.ts +282 -274
  193. package/src/runtime/model-resolver.ts +118 -0
  194. package/src/runtime/output-validator.ts +187 -187
  195. package/src/runtime/overflow-recovery.ts +175 -175
  196. package/src/runtime/parallel-research.ts +44 -44
  197. package/src/runtime/parallel-utils.ts +156 -156
  198. package/src/runtime/parent-guard.ts +80 -80
  199. package/src/runtime/phase-progress.ts +217 -217
  200. package/src/runtime/pi-args.ts +165 -165
  201. package/src/runtime/pi-json-output.ts +111 -111
  202. package/src/runtime/pi-spawn.ts +167 -167
  203. package/src/runtime/policy-engine.ts +79 -79
  204. package/src/runtime/post-checks.ts +125 -125
  205. package/src/runtime/post-exit-stdio-guard.ts +86 -86
  206. package/src/runtime/process-status.ts +97 -73
  207. package/src/runtime/progress-event-coalescer.ts +43 -43
  208. package/src/runtime/recovery-recipes.ts +74 -74
  209. package/src/runtime/retry-executor.ts +81 -81
  210. package/src/runtime/role-permission.ts +39 -39
  211. package/src/runtime/run-tracker.ts +99 -0
  212. package/src/runtime/runtime-policy.ts +21 -0
  213. package/src/runtime/runtime-resolver.ts +94 -91
  214. package/src/runtime/scheduler.ts +294 -0
  215. package/src/runtime/semaphore.ts +131 -131
  216. package/src/runtime/sensitive-paths.ts +92 -92
  217. package/src/runtime/session-usage.ts +79 -79
  218. package/src/runtime/settings-store.ts +103 -0
  219. package/src/runtime/sidechain-output.ts +29 -29
  220. package/src/runtime/skill-instructions.ts +222 -222
  221. package/src/runtime/stale-reconciler.ts +198 -189
  222. package/src/runtime/streaming-output.ts +47 -0
  223. package/src/runtime/subagent-manager.ts +404 -400
  224. package/src/runtime/subprocess-tool-registry.ts +67 -67
  225. package/src/runtime/task-display.ts +38 -38
  226. package/src/runtime/task-graph-scheduler.ts +122 -122
  227. package/src/runtime/task-graph.ts +207 -207
  228. package/src/runtime/task-output-context.ts +177 -177
  229. package/src/runtime/task-packet.ts +93 -93
  230. package/src/runtime/task-quality.ts +207 -207
  231. package/src/runtime/task-runner/capabilities.ts +78 -78
  232. package/src/runtime/task-runner/live-executor.ts +131 -113
  233. package/src/runtime/task-runner/progress.ts +119 -119
  234. package/src/runtime/task-runner/prompt-builder.ts +139 -139
  235. package/src/runtime/task-runner/prompt-pipeline.ts +64 -64
  236. package/src/runtime/task-runner/result-utils.ts +14 -14
  237. package/src/runtime/task-runner/run-projection.ts +103 -103
  238. package/src/runtime/task-runner/state-helpers.ts +22 -22
  239. package/src/runtime/task-runner.ts +469 -459
  240. package/src/runtime/team-runner.ts +693 -945
  241. package/src/runtime/usage-tracker.ts +71 -0
  242. package/src/runtime/worker-heartbeat.ts +21 -21
  243. package/src/runtime/worker-startup.ts +57 -57
  244. package/src/runtime/workflow-state.ts +187 -187
  245. package/src/runtime/yield-handler.ts +190 -190
  246. package/src/schema/config-schema.ts +172 -168
  247. package/src/schema/team-tool-schema.ts +126 -126
  248. package/src/schema/validation-types.ts +151 -148
  249. package/src/skills/discover-skills.ts +67 -67
  250. package/src/skills/skill-templates.ts +374 -374
  251. package/src/state/active-run-registry.ts +227 -191
  252. package/src/state/artifact-store.ts +130 -129
  253. package/src/state/atomic-write.ts +262 -195
  254. package/src/state/blob-store.ts +116 -116
  255. package/src/state/contracts.ts +111 -111
  256. package/src/state/event-log-rotation.ts +161 -158
  257. package/src/state/event-log.ts +383 -303
  258. package/src/state/event-reconstructor.ts +217 -217
  259. package/src/state/jsonl-writer.ts +82 -82
  260. package/src/state/locks.ts +146 -146
  261. package/src/state/mailbox.ts +446 -405
  262. package/src/state/state-store.ts +364 -351
  263. package/src/state/task-claims.ts +44 -44
  264. package/src/state/types.ts +285 -285
  265. package/src/state/usage.ts +29 -29
  266. package/src/subagents/async-entry.ts +1 -1
  267. package/src/subagents/index.ts +3 -3
  268. package/src/subagents/live/control.ts +1 -1
  269. package/src/subagents/live/manager.ts +1 -1
  270. package/src/subagents/live/realtime.ts +1 -1
  271. package/src/subagents/live/session-runtime.ts +1 -1
  272. package/src/subagents/manager.ts +1 -1
  273. package/src/subagents/spawn.ts +1 -1
  274. package/src/teams/discover-teams.ts +116 -116
  275. package/src/teams/team-config.ts +27 -27
  276. package/src/teams/team-serializer.ts +38 -38
  277. package/src/types/diff.d.ts +18 -18
  278. package/src/ui/agent-management-overlay.ts +144 -144
  279. package/src/ui/crew-widget.ts +487 -370
  280. package/src/ui/dashboard-panes/agents-pane.ts +109 -28
  281. package/src/ui/dashboard-panes/cancellation-pane.ts +42 -42
  282. package/src/ui/dashboard-panes/capability-pane.ts +59 -59
  283. package/src/ui/dashboard-panes/health-pane.ts +30 -30
  284. package/src/ui/dashboard-panes/mailbox-pane.ts +35 -35
  285. package/src/ui/dashboard-panes/progress-pane.ts +30 -30
  286. package/src/ui/dashboard-panes/transcript-pane.ts +10 -10
  287. package/src/ui/heartbeat-aggregator.ts +63 -63
  288. package/src/ui/keybinding-map.ts +97 -94
  289. package/src/ui/live-conversation-overlay.ts +152 -0
  290. package/src/ui/live-run-sidebar.ts +180 -180
  291. package/src/ui/mascot.ts +442 -442
  292. package/src/ui/overlays/agent-picker-overlay.ts +57 -57
  293. package/src/ui/overlays/confirm-overlay.ts +58 -58
  294. package/src/ui/overlays/mailbox-compose-overlay.ts +144 -144
  295. package/src/ui/overlays/mailbox-compose-preview.ts +63 -63
  296. package/src/ui/overlays/mailbox-detail-overlay.ts +122 -122
  297. package/src/ui/pi-ui-compat.ts +57 -57
  298. package/src/ui/powerbar-publisher.ts +221 -197
  299. package/src/ui/render-scheduler.ts +216 -143
  300. package/src/ui/run-action-dispatcher.ts +118 -118
  301. package/src/ui/run-dashboard.ts +526 -464
  302. package/src/ui/run-event-bus.ts +208 -208
  303. package/src/ui/run-snapshot-cache.ts +826 -777
  304. package/src/ui/settings-overlay.ts +721 -0
  305. package/src/ui/snapshot-types.ts +86 -70
  306. package/src/ui/theme-adapter.ts +190 -190
  307. package/src/ui/tool-progress-formatter.ts +89 -0
  308. package/src/ui/transcript-cache.ts +94 -94
  309. package/src/ui/transcript-viewer.ts +335 -335
  310. package/src/utils/conflict-detect.ts +662 -0
  311. package/src/utils/file-coalescer.ts +86 -86
  312. package/src/utils/frontmatter.ts +68 -68
  313. package/src/utils/fs-watch.ts +88 -31
  314. package/src/utils/gh-protocol.ts +479 -0
  315. package/src/utils/ids.ts +17 -17
  316. package/src/utils/incremental-reader.ts +104 -104
  317. package/src/utils/internal-error.ts +6 -6
  318. package/src/utils/names.ts +27 -27
  319. package/src/utils/paths.ts +102 -63
  320. package/src/utils/redaction.ts +44 -44
  321. package/src/utils/safe-paths.ts +47 -47
  322. package/src/utils/scan-cache.ts +136 -136
  323. package/src/utils/sse-parser.ts +134 -134
  324. package/src/utils/task-name-generator.ts +337 -337
  325. package/src/utils/timings.ts +33 -33
  326. package/src/utils/visual.ts +243 -198
  327. package/src/workflows/discover-workflows.ts +139 -139
  328. package/src/workflows/validate-workflow.ts +40 -40
  329. package/src/workflows/workflow-config.ts +26 -26
  330. package/src/workflows/workflow-serializer.ts +32 -32
  331. package/src/worktree/branch-freshness.ts +45 -45
  332. package/src/worktree/cleanup.ts +75 -75
  333. package/src/worktree/worktree-manager.ts +188 -188
  334. package/teams/default.team.md +12 -12
  335. package/teams/fast-fix.team.md +11 -11
  336. package/teams/implementation.team.md +18 -18
  337. package/teams/parallel-research.team.md +14 -14
  338. package/teams/research.team.md +11 -11
  339. package/teams/review.team.md +12 -12
  340. package/tsconfig.json +19 -19
  341. package/workflows/default.workflow.md +30 -30
  342. package/workflows/fast-fix.workflow.md +23 -23
  343. package/workflows/implementation.workflow.md +43 -43
  344. package/workflows/parallel-research.workflow.md +46 -46
  345. package/workflows/research.workflow.md +22 -22
  346. package/workflows/review.workflow.md +30 -30
  347. package/skills/task-packet/SKILL.md +0 -28
  348. package/skills/verify-evidence/SKILL.md +0 -27
@@ -1,945 +1,693 @@
1
- import * as fs from "node:fs";
2
- import type { AgentConfig } from "../agents/agent-config.ts";
3
- import type { CrewLimitsConfig, CrewRuntimeConfig, CrewReliabilityConfig } from "../config/config.ts";
4
- import type { CrewRuntimeCapabilities } from "./runtime-resolver.ts";
5
- import { writeArtifact } from "../state/artifact-store.ts";
6
- import { executeHook, appendHookEvent } from "../hooks/registry.ts";
7
- import { appendEvent } from "../state/event-log.ts";
8
- import type { TeamConfig } from "../teams/team-config.ts";
9
- import type { ArtifactDescriptor, PolicyDecision, TeamRunManifest, TaskAttemptState, TeamTaskState } from "../state/types.ts";
10
- import { loadRunManifestById, saveRunManifest, saveRunManifestAsync, saveRunTasksAsync, updateRunStatus } from "../state/state-store.ts";
11
- import { aggregateUsage, formatUsage } from "../state/usage.ts";
12
- import type { WorkflowConfig, WorkflowStep } from "../workflows/workflow-config.ts";
13
- import { evaluateCrewPolicy, summarizePolicyDecisions } from "./policy-engine.ts";
14
- import { buildRecoveryLedger } from "./recovery-recipes.ts";
15
- import { buildTaskGraphIndex, refreshTaskGraphQueues, taskGraphSnapshot } from "./task-graph-scheduler.ts";
16
- import { buildExecutionPlan as buildDagExecutionPlan, getReadyTasks as getDagReadyTasks, type TaskNode } from "./task-graph.ts";
17
- import { checkBranchFreshness } from "../worktree/branch-freshness.ts";
18
- import { aggregateTaskOutputs } from "./task-output-context.ts";
19
- import { saveCrewAgents } from "./crew-agent-records.ts";
20
- import { recordsForMaterializedTasks } from "./task-display.ts";
21
- import { deliverGroupJoin, resolveGroupJoinMode } from "./group-join.ts";
22
- import { runTeamTask } from "./task-runner.ts";
23
- import { createWorkflowStateMachine, validatePhasePreconditions, transitionPhase, type PhaseState, type PhaseGuardContext } from "./workflow-state.ts";
24
- import { executeWithRetry, DEFAULT_RETRY_POLICY, type RetryPolicy } from "./retry-executor.ts";
25
- import { appendDeadletter } from "./deadletter.ts";
26
- import type { MetricRegistry } from "../observability/metric-registry.ts";
27
- import { childCorrelation, withCorrelation } from "../observability/correlation.ts";
28
- import { resolveBatchConcurrency } from "./concurrency.ts";
29
- import { mapConcurrent } from "./parallel-utils.ts";
30
- import { permissionForRole } from "./role-permission.ts";
31
- import { CrewCancellationError, buildSyntheticTerminalEvidence, cancellationReasonFromSignal } from "./cancellation.ts";
32
- import { effectivenessPolicyDecision, evaluateRunEffectiveness, formatRunEffectivenessLines } from "./effectiveness.ts";
33
-
34
- export interface ExecuteTeamRunInput {
35
- manifest: TeamRunManifest;
36
- tasks: TeamTaskState[];
37
- team: TeamConfig;
38
- workflow: WorkflowConfig;
39
- agents: AgentConfig[];
40
- executeWorkers: boolean;
41
- limits?: CrewLimitsConfig;
42
- runtime?: CrewRuntimeCapabilities;
43
- runtimeConfig?: CrewRuntimeConfig;
44
- parentContext?: string;
45
- parentModel?: unknown;
46
- modelRegistry?: unknown;
47
- modelOverride?: string;
48
- signal?: AbortSignal;
49
- reliability?: CrewReliabilityConfig;
50
- metricRegistry?: MetricRegistry;
51
- /** Skill override from the team tool. false disables skill injection for this run. */
52
- skillOverride?: string[] | false;
53
- /** Optional callback for JSON events from child Pi. Used for overflow recovery tracking. */
54
- onJsonEvent?: (taskId: string, runId: string, event: unknown) => void;
55
- }
56
-
57
- function findStep(workflow: WorkflowConfig, task: TeamTaskState): WorkflowStep {
58
- const step = workflow.steps.find((candidate) => candidate.id === task.stepId);
59
- if (!step) throw new Error(`Workflow step '${task.stepId}' not found for task '${task.id}'.`);
60
- return step;
61
- }
62
-
63
- function findAgent(agents: AgentConfig[], task: TeamTaskState): AgentConfig {
64
- const agent = agents.find((candidate) => candidate.name === task.agent);
65
- if (!agent) throw new Error(`Agent '${task.agent}' not found for task '${task.id}'.`);
66
- return agent;
67
- }
68
-
69
- function markBlocked(tasks: TeamTaskState[], reason: string): TeamTaskState[] {
70
- return tasks.map((task) => task.status === "queued" ? { ...task, status: "skipped", error: reason, finishedAt: new Date().toISOString(), graph: task.graph ? { ...task.graph, queue: "blocked" } : undefined } : task);
71
- }
72
-
73
- function mergeArtifacts(items: ArtifactDescriptor[]): ArtifactDescriptor[] {
74
- const byPath = new Map<string, ArtifactDescriptor>();
75
- for (const item of items) byPath.set(item.path, item);
76
- return [...byPath.values()];
77
- }
78
-
79
- function isNonTerminalTaskStatus(status: TeamTaskState["status"]): boolean {
80
- return status === "queued" || status === "running" || status === "waiting";
81
- }
82
-
83
- function shouldMergeTaskUpdate(current: TeamTaskState, updated: TeamTaskState): boolean {
84
- // Parallel workers receive the same input snapshot. A later result may still
85
- // contain stale queued/running copies of tasks that another worker already
86
- // completed. Never let those stale snapshots regress durable task state.
87
- if (!isNonTerminalTaskStatus(current.status) && isNonTerminalTaskStatus(updated.status)) return false;
88
- // Prevent a stale completed task from overwriting a fresher one.
89
- if (current.finishedAt && updated.finishedAt) {
90
- const currentFinished = new Date(current.finishedAt).getTime();
91
- const updatedFinished = new Date(updated.finishedAt).getTime();
92
- if (!Number.isNaN(currentFinished) && !Number.isNaN(updatedFinished) && updatedFinished < currentFinished) return false;
93
- }
94
- return updated.status !== current.status || updated.finishedAt !== current.finishedAt || updated.startedAt !== current.startedAt || Boolean(updated.resultArtifact) || Boolean(updated.error) || Boolean(updated.modelAttempts?.length) || Boolean(updated.usage) || Boolean(updated.attempts?.length);
95
- }
96
-
97
- export function __test__mergeTaskUpdates(base: TeamTaskState[], results: Array<{ tasks: TeamTaskState[] }>): TeamTaskState[] {
98
- let merged = base;
99
- for (const result of results) {
100
- for (const updated of result.tasks) {
101
- const current = merged.find((task) => task.id === updated.id);
102
- if (!current || !shouldMergeTaskUpdate(current, updated)) continue;
103
- merged = merged.map((task) => task.id === updated.id ? updated : task);
104
- }
105
- }
106
- return refreshTaskGraphQueues(merged);
107
- }
108
-
109
- interface AdaptivePlanTask {
110
- role: string;
111
- title?: string;
112
- task: string;
113
- }
114
-
115
- interface AdaptivePlanPhase {
116
- name: string;
117
- tasks: AdaptivePlanTask[];
118
- }
119
-
120
- interface AdaptivePlan {
121
- phases: AdaptivePlanPhase[];
122
- }
123
-
124
- const MAX_ADAPTIVE_TASKS = 12;
125
-
126
- function slug(value: string): string {
127
- return value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 32) || "task";
128
- }
129
-
130
- function extractAdaptivePlanJson(text: string): string | undefined {
131
- const markerMatch = text.match(/ADAPTIVE_PLAN_JSON_START\s*([\s\S]*?)\s*ADAPTIVE_PLAN_JSON_END/);
132
- if (markerMatch?.[1]) return markerMatch[1];
133
- const startIndex = text.indexOf("ADAPTIVE_PLAN_JSON_START");
134
- if (startIndex >= 0) return text.slice(startIndex + "ADAPTIVE_PLAN_JSON_START".length).trim();
135
- const fencedMatch = text.match(/```(?:json)?\s*([\s\S]*?)```/i);
136
- return fencedMatch?.[1];
137
- }
138
-
139
- export function __test__parseAdaptivePlan(text: string, allowedRoles: string[]): AdaptivePlan | undefined {
140
- const raw = extractAdaptivePlanJson(text);
141
- if (!raw) return undefined;
142
- let parsed: unknown;
143
- try { parsed = JSON.parse(raw); } catch { return undefined; }
144
- if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return undefined;
145
- const phasesRaw = Array.isArray((parsed as { phases?: unknown }).phases) ? (parsed as { phases: unknown[] }).phases : Array.isArray((parsed as { tasks?: unknown }).tasks) ? [{ name: "adaptive", tasks: (parsed as { tasks: unknown[] }).tasks }] : undefined;
146
- if (!phasesRaw) return undefined;
147
- const allowed = new Set(allowedRoles);
148
- const phases: AdaptivePlanPhase[] = [];
149
- let total = 0;
150
- for (const [phaseIndex, phaseRaw] of phasesRaw.entries()) {
151
- if (!phaseRaw || typeof phaseRaw !== "object" || Array.isArray(phaseRaw)) return undefined;
152
- const phaseObj = phaseRaw as { name?: unknown; tasks?: unknown };
153
- if (!Array.isArray(phaseObj.tasks) || phaseObj.tasks.length === 0) return undefined;
154
- const tasks: AdaptivePlanTask[] = [];
155
- for (const taskRaw of phaseObj.tasks) {
156
- if (!taskRaw || typeof taskRaw !== "object" || Array.isArray(taskRaw)) return undefined;
157
- const taskObj = taskRaw as { role?: unknown; title?: unknown; task?: unknown };
158
- if (typeof taskObj.role !== "string" || !allowed.has(taskObj.role)) return undefined;
159
- if (typeof taskObj.task !== "string" || !taskObj.task.trim()) return undefined;
160
- if (total >= MAX_ADAPTIVE_TASKS) return undefined;
161
- tasks.push({ role: taskObj.role, title: typeof taskObj.title === "string" ? taskObj.title : undefined, task: taskObj.task.trim() });
162
- total++;
163
- }
164
- phases.push({ name: typeof phaseObj.name === "string" && phaseObj.name.trim() ? phaseObj.name.trim() : `phase-${phaseIndex + 1}`, tasks });
165
- }
166
- return phases.length ? { phases } : undefined;
167
- }
168
-
169
- interface CloseUnbalancedJsonResult {
170
- text: string;
171
- status: "repaired" | "unstable";
172
- warning?: string;
173
- }
174
-
175
- function closeUnbalancedJson(raw: string): CloseUnbalancedJsonResult {
176
- let result = raw.trim();
177
- const stack: string[] = [];
178
- let inString = false;
179
- let escaped = false;
180
- for (const char of result) {
181
- if (escaped) {
182
- escaped = false;
183
- continue;
184
- }
185
- if (char === "\\" && inString) {
186
- escaped = true;
187
- continue;
188
- }
189
- if (char === '"') {
190
- inString = !inString;
191
- continue;
192
- }
193
- if (inString) continue;
194
- if (char === "{") stack.push("}");
195
- else if (char === "[") stack.push("]");
196
- else if ((char === "}" || char === "]") && stack.at(-1) === char) stack.pop();
197
- }
198
- while (stack.length) result += stack.pop();
199
- // If still in a string, the JSON string was truncated values may be semantically different
200
- if (inString) {
201
- return { text: result, status: "unstable", warning: "JSON string was truncated — values may be incorrect" };
202
- }
203
- return { text: result, status: "repaired" };
204
- }
205
-
206
- function salvageCompletePhaseObjects(raw: string): unknown | undefined {
207
- const phasesIndex = raw.indexOf('"phases"');
208
- if (phasesIndex < 0) return undefined;
209
- const arrayStart = raw.indexOf("[", phasesIndex);
210
- if (arrayStart < 0) return undefined;
211
- const phases: unknown[] = [];
212
- let objectStart = -1;
213
- let depth = 0;
214
- let inString = false;
215
- let escaped = false;
216
- for (let index = arrayStart + 1; index < raw.length; index++) {
217
- const char = raw[index];
218
- if (escaped) {
219
- escaped = false;
220
- continue;
221
- }
222
- if (char === "\\" && inString) {
223
- escaped = true;
224
- continue;
225
- }
226
- if (char === '"') {
227
- inString = !inString;
228
- continue;
229
- }
230
- if (inString) continue;
231
- if (char === "{") {
232
- if (depth === 0) objectStart = index;
233
- depth++;
234
- continue;
235
- }
236
- if (char === "}") {
237
- if (depth <= 0) continue;
238
- depth--;
239
- if (depth === 0 && objectStart >= 0) {
240
- try {
241
- phases.push(JSON.parse(raw.slice(objectStart, index + 1)));
242
- } catch {
243
- // Ignore malformed trailing phase objects and keep earlier complete phases.
244
- }
245
- objectStart = -1;
246
- }
247
- }
248
- }
249
- return phases.length ? { phases } : undefined;
250
- }
251
-
252
- function adaptiveRoleAlias(role: string, allowed: Set<string>): string | undefined {
253
- if (allowed.has(role)) return role;
254
- const normalized = slug(role);
255
- const aliases: Record<string, string[]> = {
256
- reviewer: ["code-reviewer", "review", "code-review", "critic"],
257
- "security-reviewer": ["security", "security-review", "sec-review"],
258
- "test-engineer": ["tester", "qa", "test"],
259
- executor: ["developer", "implementer", "coder", "engineer"],
260
- explorer: ["researcher", "scout"],
261
- analyst: ["analysis", "analyzer"],
262
- };
263
- for (const [target, names] of Object.entries(aliases)) if (allowed.has(target) && names.includes(normalized)) return target;
264
- return undefined;
265
- }
266
-
267
- export function __test__repairAdaptivePlan(text: string, allowedRoles: string[]): { plan?: AdaptivePlan; repaired: boolean; reason?: string } {
268
- const raw = extractAdaptivePlanJson(text);
269
- if (!raw) return { repaired: false, reason: "missing-json" };
270
- const closeResult = closeUnbalancedJson(raw);
271
- const candidates = [raw, closeResult.text];
272
- let parsed: unknown;
273
- let salvageUsed = false;
274
- for (const candidate of candidates) {
275
- try {
276
- parsed = JSON.parse(candidate);
277
- break;
278
- } catch {
279
- // Try the next repair candidate.
280
- }
281
- }
282
- if (!parsed) {
283
- parsed = salvageCompletePhaseObjects(raw);
284
- salvageUsed = parsed !== undefined;
285
- }
286
- if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return { repaired: false, reason: "invalid-json" };
287
- const phasesRaw = Array.isArray((parsed as { phases?: unknown }).phases) ? (parsed as { phases: unknown[] }).phases : Array.isArray((parsed as { tasks?: unknown }).tasks) ? [{ name: "adaptive", tasks: (parsed as { tasks: unknown[] }).tasks }] : undefined;
288
- if (!phasesRaw) return { repaired: false, reason: "missing-phases" };
289
- const allowed = new Set(allowedRoles);
290
- const phases: AdaptivePlanPhase[] = [];
291
- let total = 0;
292
- let repaired = salvageUsed || raw !== closeResult.text;
293
- for (const [phaseIndex, phaseRaw] of phasesRaw.entries()) {
294
- if (!phaseRaw || typeof phaseRaw !== "object" || Array.isArray(phaseRaw)) continue;
295
- const phaseObj = phaseRaw as { name?: unknown; tasks?: unknown };
296
- if (!Array.isArray(phaseObj.tasks)) continue;
297
- const tasks: AdaptivePlanTask[] = [];
298
- for (const taskRaw of phaseObj.tasks) {
299
- if (total >= MAX_ADAPTIVE_TASKS) {
300
- repaired = true;
301
- break;
302
- }
303
- if (!taskRaw || typeof taskRaw !== "object" || Array.isArray(taskRaw)) {
304
- repaired = true;
305
- continue;
306
- }
307
- const taskObj = taskRaw as { role?: unknown; title?: unknown; task?: unknown };
308
- const role = typeof taskObj.role === "string" ? adaptiveRoleAlias(taskObj.role, allowed) : undefined;
309
- const taskText = typeof taskObj.task === "string" ? taskObj.task.trim() : "";
310
- if (!role || !taskText) {
311
- repaired = true;
312
- continue;
313
- }
314
- tasks.push({ role, title: typeof taskObj.title === "string" ? taskObj.title : undefined, task: taskText });
315
- total++;
316
- }
317
- if (tasks.length) phases.push({ name: typeof phaseObj.name === "string" && phaseObj.name.trim() ? phaseObj.name.trim() : `phase-${phaseIndex + 1}`, tasks });
318
- if (total >= MAX_ADAPTIVE_TASKS) break;
319
- }
320
- return phases.length ? { plan: { phases }, repaired: true, reason: repaired ? "repaired" : "normalized" } : { repaired: false, reason: "empty-plan" };
321
- }
322
-
323
- function reconstructAdaptiveWorkflow(workflow: WorkflowConfig, tasks: TeamTaskState[]): WorkflowConfig {
324
- const existing = new Set(workflow.steps.map((step) => step.id));
325
- const steps: WorkflowStep[] = [];
326
- for (const task of tasks) {
327
- if (!task.stepId?.startsWith("adaptive-") || !task.adaptive?.task || existing.has(task.stepId)) continue;
328
- steps.push({ id: task.stepId, role: task.role, dependsOn: task.graph?.dependencies ?? task.dependsOn, parallelGroup: `adaptive-${slug(task.adaptive.phase)}`, task: task.adaptive.task });
329
- }
330
- return steps.length ? { ...workflow, steps: [...workflow.steps, ...steps] } : workflow;
331
- }
332
-
333
- function injectAdaptivePlanIfReady(input: { manifest: TeamRunManifest; tasks: TeamTaskState[]; workflow: WorkflowConfig; team: TeamConfig }): { tasks: TeamTaskState[]; workflow: WorkflowConfig; injected: boolean; missingPlan: boolean } {
334
- if (input.workflow.name !== "implementation") return { tasks: input.tasks, workflow: input.workflow, injected: false, missingPlan: false };
335
- if (input.tasks.some((task) => task.stepId?.startsWith("adaptive-"))) return { tasks: input.tasks, workflow: reconstructAdaptiveWorkflow(input.workflow, input.tasks), injected: false, missingPlan: false };
336
- const completedAssess = input.tasks.find((task) => task.stepId === "assess" && task.status === "completed");
337
- if (!completedAssess) return { tasks: input.tasks, workflow: input.workflow, injected: false, missingPlan: false };
338
- if (!completedAssess.resultArtifact?.path) {
339
- appendEvent(input.manifest.eventsPath, { type: "adaptive.plan_missing", runId: input.manifest.runId, taskId: completedAssess.id, message: "Adaptive planner result artifact is missing." });
340
- return { tasks: input.tasks, workflow: input.workflow, injected: false, missingPlan: true };
341
- }
342
- const assessTask = completedAssess;
343
- const resultPath = completedAssess.resultArtifact.path;
344
- let text = "";
345
- try { text = fs.readFileSync(resultPath, "utf-8"); } catch {
346
- appendEvent(input.manifest.eventsPath, { type: "adaptive.plan_missing", runId: input.manifest.runId, taskId: assessTask.id, message: "Adaptive planner result artifact could not be read." });
347
- return { tasks: input.tasks, workflow: input.workflow, injected: false, missingPlan: true };
348
- }
349
- const allowedRoles = input.team.roles.map((role) => role.name);
350
- let plan = __test__parseAdaptivePlan(text, allowedRoles);
351
- if (!plan) {
352
- const repair = process.env.PI_CREW_ADAPTIVE_REPAIR === "0" || process.env.PI_TEAMS_ADAPTIVE_REPAIR === "0" ? { repaired: false, reason: "disabled" } : __test__repairAdaptivePlan(text, allowedRoles);
353
- if (repair.plan) {
354
- plan = repair.plan;
355
- const repairArtifact = writeArtifact(input.manifest.artifactsRoot, { kind: "metadata", relativePath: "metadata/adaptive-repair.json", producer: assessTask.id, content: `${JSON.stringify({ reason: repair.reason, phases: repair.plan.phases.map((phase) => ({ name: phase.name, count: phase.tasks.length, roles: phase.tasks.map((task) => task.role) })) }, null, 2)}\n` });
356
- saveRunManifest({ ...input.manifest, updatedAt: new Date().toISOString(), artifacts: [...input.manifest.artifacts, repairArtifact] });
357
- appendEvent(input.manifest.eventsPath, { type: "adaptive.plan_repaired", runId: input.manifest.runId, taskId: assessTask.id, message: "Adaptive planner output was repaired before dynamic subagents were spawned.", data: { reason: repair.reason } });
358
- } else {
359
- appendEvent(input.manifest.eventsPath, { type: "adaptive.plan_repair_failed", runId: input.manifest.runId, taskId: assessTask.id, message: "Adaptive planner output could not be repaired.", data: { reason: repair.reason } });
360
- appendEvent(input.manifest.eventsPath, { type: "adaptive.plan_missing", runId: input.manifest.runId, taskId: assessTask.id, message: "Adaptive planner did not produce a valid plan; no dynamic subagents were spawned." });
361
- return { tasks: input.tasks, workflow: input.workflow, injected: false, missingPlan: true };
362
- }
363
- }
364
- const steps: WorkflowStep[] = [];
365
- const tasks: TeamTaskState[] = [];
366
- let previousStepIds = ["assess"];
367
- let counter = 0;
368
- for (const [phaseIndex, phase] of plan.phases.entries()) {
369
- const currentStepIds: string[] = [];
370
- for (const [taskIndex, planned] of phase.tasks.entries()) {
371
- counter++;
372
- const stepId = `adaptive-${phaseIndex + 1}-${taskIndex + 1}-${slug(planned.role)}`;
373
- const taskId = `adaptive-${String(counter).padStart(2, "0")}-${slug(planned.role)}`;
374
- steps.push({ id: stepId, role: planned.role, dependsOn: previousStepIds, parallelGroup: `adaptive-${slug(phase.name)}`, task: planned.task });
375
- tasks.push({
376
- id: taskId,
377
- runId: input.manifest.runId,
378
- stepId,
379
- role: planned.role,
380
- agent: input.team.roles.find((role) => role.name === planned.role)?.agent ?? planned.role,
381
- title: planned.title ?? stepId,
382
- status: "queued",
383
- dependsOn: previousStepIds,
384
- cwd: input.manifest.cwd,
385
- adaptive: { phase: phase.name, task: planned.task },
386
- graph: { taskId, dependencies: previousStepIds, children: [], queue: "blocked" },
387
- });
388
- currentStepIds.push(stepId);
389
- }
390
- previousStepIds = currentStepIds;
391
- }
392
- const dependencyTaskIdByStep = new Map<string, string>([["assess", assessTask.id], ...tasks.map((task) => [task.stepId ?? task.id, task.id] as const)]);
393
- const withGraph = tasks.map((task) => ({
394
- ...task,
395
- dependsOn: task.dependsOn.map((dep) => dependencyTaskIdByStep.get(dep) ?? dep),
396
- graph: task.graph ? { ...task.graph, dependencies: task.dependsOn.map((dep) => dependencyTaskIdByStep.get(dep) ?? dep), queue: "blocked" as const } : task.graph,
397
- }));
398
- const allTasks = refreshTaskGraphQueues([...input.tasks, ...withGraph]);
399
- appendEvent(input.manifest.eventsPath, { type: "adaptive.plan_injected", runId: input.manifest.runId, taskId: assessTask.id, message: `Injected ${withGraph.length} adaptive subagent task(s) across ${plan.phases.length} phase(s).`, data: { phases: plan.phases.map((phase) => ({ name: phase.name, count: phase.tasks.length, roles: phase.tasks.map((task) => task.role) })) } });
400
- return { tasks: allTasks, workflow: { ...input.workflow, steps: [...input.workflow.steps, ...steps] }, injected: true, missingPlan: false };
401
- }
402
-
403
- function formatTaskProgress(task: TeamTaskState): string {
404
- 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}` : ""}`;
405
- }
406
-
407
- function runEffectivenessLines(manifest: TeamRunManifest, tasks: TeamTaskState[], executeWorkers: boolean, runtimeConfig?: CrewRuntimeConfig): string[] {
408
- return formatRunEffectivenessLines(evaluateRunEffectiveness({ manifest, tasks, executeWorkers, runtimeConfig }));
409
- }
410
-
411
- function writeProgress(manifest: TeamRunManifest, tasks: TeamTaskState[], producer: string, executeWorkers = true, runtimeConfig?: CrewRuntimeConfig): TeamRunManifest {
412
- const counts = new Map<string, number>();
413
- for (const task of tasks) counts.set(task.status, (counts.get(task.status) ?? 0) + 1);
414
- const queue = taskGraphSnapshot(tasks);
415
- const progress = writeArtifact(manifest.artifactsRoot, {
416
- kind: "progress",
417
- relativePath: "progress.md",
418
- producer,
419
- content: [
420
- `# pi-crew progress ${manifest.runId}`,
421
- "",
422
- `Status: ${manifest.status}`,
423
- `Team: ${manifest.team}`,
424
- `Workflow: ${manifest.workflow ?? "(none)"}`,
425
- `Updated: ${new Date().toISOString()}`,
426
- `Task counts: ${[...counts.entries()].map(([status, count]) => `${status}=${count}`).join(", ") || "none"}`,
427
- `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}`,
428
- "",
429
- "## Tasks",
430
- ...tasks.map(formatTaskProgress),
431
- "",
432
- "## Effectiveness",
433
- ...runEffectivenessLines(manifest, tasks, executeWorkers, runtimeConfig),
434
- "",
435
- ].join("\n"),
436
- });
437
- return { ...manifest, updatedAt: new Date().toISOString(), artifacts: [...manifest.artifacts.filter((artifact) => !(artifact.kind === "progress" && artifact.path === progress.path)), progress] };
438
- }
439
-
440
- function applyPolicy(manifest: TeamRunManifest, tasks: TeamTaskState[], limits?: CrewLimitsConfig): TeamRunManifest {
441
- const branchFreshness = checkBranchFreshness(manifest.cwd);
442
- const branchArtifact = writeArtifact(manifest.artifactsRoot, {
443
- kind: "metadata",
444
- relativePath: "metadata/branch-freshness.json",
445
- producer: "branch-freshness",
446
- content: `${JSON.stringify(branchFreshness, null, 2)}\n`,
447
- });
448
- let decisions: PolicyDecision[] = evaluateCrewPolicy({ manifest, tasks, limits });
449
- if (branchFreshness.status === "stale" || branchFreshness.status === "diverged") {
450
- const branchDecision: PolicyDecision = {
451
- action: "notify",
452
- reason: "branch_stale",
453
- message: branchFreshness.message,
454
- createdAt: new Date().toISOString(),
455
- };
456
- decisions = [...decisions, branchDecision];
457
- appendEvent(manifest.eventsPath, { type: "branch.stale", runId: manifest.runId, message: branchFreshness.message, data: { branchFreshness } });
458
- }
459
- const policyArtifact = writeArtifact(manifest.artifactsRoot, {
460
- kind: "metadata",
461
- relativePath: "policy-decisions.json",
462
- producer: "policy-engine",
463
- content: `${JSON.stringify(decisions, null, 2)}\n`,
464
- });
465
- const recoveryLedger = buildRecoveryLedger(decisions);
466
- const recoveryArtifact = writeArtifact(manifest.artifactsRoot, {
467
- kind: "metadata",
468
- relativePath: "recovery-ledger.json",
469
- producer: "recovery-engine",
470
- content: `${JSON.stringify(recoveryLedger, null, 2)}\n`,
471
- });
472
- for (const item of decisions) appendEvent(manifest.eventsPath, { type: item.action === "escalate" ? "policy.escalated" : "policy.action", runId: manifest.runId, taskId: item.taskId, message: item.message, data: { action: item.action, reason: item.reason } });
473
- for (const item of recoveryLedger.entries) appendEvent(manifest.eventsPath, { type: item.state === "escalation_required" ? "recovery.escalated" : "recovery.attempted", runId: manifest.runId, taskId: item.taskId, message: item.message, data: { scenario: item.scenario, steps: item.steps, attempt: item.attempt, state: item.state } });
474
- return { ...manifest, updatedAt: new Date().toISOString(), policyDecisions: decisions, artifacts: [...manifest.artifacts.filter((artifact) => !(artifact.kind === "metadata" && (artifact.path.endsWith("policy-decisions.json") || artifact.path.endsWith("recovery-ledger.json") || artifact.path.endsWith("branch-freshness.json")))), branchArtifact, policyArtifact, recoveryArtifact] };
475
- }
476
-
477
- function retryPolicyFromConfig(config: CrewReliabilityConfig | undefined): RetryPolicy {
478
- return { ...DEFAULT_RETRY_POLICY, ...(config?.retryPolicy ?? {}) };
479
- }
480
-
481
- function failedTaskFrom(result: { tasks: TeamTaskState[] }, taskId: string): TeamTaskState | undefined {
482
- return result.tasks.find((item) => item.id === taskId && item.status === "failed");
483
- }
484
-
485
- function requiresPlanApproval(workflow: WorkflowConfig, runtimeConfig: CrewRuntimeConfig | undefined): boolean {
486
- return workflow.name === "implementation" && runtimeConfig?.requirePlanApproval === true;
487
- }
488
-
489
- function isPlanApprovalPending(manifest: TeamRunManifest): boolean {
490
- return manifest.planApproval?.required === true && manifest.planApproval.status === "pending";
491
- }
492
-
493
- function isMutatingTask(task: TeamTaskState): boolean {
494
- return permissionForRole(task.role) !== "read_only";
495
- }
496
-
497
- function ensurePlanApprovalRequested(manifest: TeamRunManifest, tasks: TeamTaskState[]): TeamRunManifest {
498
- if (manifest.planApproval) return manifest;
499
- const assessTask = tasks.find((task) => task.stepId === "assess" && task.status === "completed");
500
- const now = new Date().toISOString();
501
- const updated: TeamRunManifest = {
502
- ...manifest,
503
- updatedAt: now,
504
- planApproval: {
505
- required: true,
506
- status: "pending",
507
- requestedAt: now,
508
- updatedAt: now,
509
- planTaskId: assessTask?.id,
510
- planArtifactPath: assessTask?.resultArtifact?.path,
511
- },
512
- };
513
- saveRunManifest(updated);
514
- appendEvent(updated.eventsPath, { type: "plan.approval_required", runId: updated.runId, taskId: assessTask?.id, message: "Adaptive implementation plan requires explicit approval before mutating tasks run.", data: { planArtifactPath: assessTask?.resultArtifact?.path } });
515
- return updated;
516
- }
517
-
518
- function cancelPlanTasks(tasks: TeamTaskState[], reason: string): TeamTaskState[] {
519
- return tasks.map((task) => task.status === "queued" || task.status === "running" || task.status === "waiting" ? { ...task, status: "cancelled", finishedAt: new Date().toISOString(), error: reason, graph: task.graph ? { ...task.graph, queue: "done" } : undefined } : task);
520
- }
521
-
522
- function hasPendingMutatingAdaptiveTask(tasks: TeamTaskState[]): boolean {
523
- return tasks.some((task) => task.status === "queued" && task.adaptive && isMutatingTask(task));
524
- }
525
-
526
- /**
527
- * Check whether any task uses explicit `dependsOn` that would benefit from DAG-based
528
- * execution planning. If so, build an execution plan and use `getDagReadyTasks`
529
- * to augment the ready-set selection.
530
- */
531
- function dagReadyTaskIds(tasks: TeamTaskState[], completedIds: Set<string>): string[] | null {
532
- const hasExplicitDeps = tasks.some((t) => t.dependsOn.length > 0);
533
- if (!hasExplicitDeps) return null;
534
- const nodes: TaskNode[] = tasks.map((t) => ({
535
- id: t.id,
536
- dependsOn: t.dependsOn,
537
- phase: t.adaptive?.phase ?? t.stepId,
538
- }));
539
- const plan = buildDagExecutionPlan(nodes);
540
- if (plan.hasCycle) return null; // fall back to existing scheduler
541
- return getDagReadyTasks(plan, completedIds);
542
- }
543
-
544
- export async function executeTeamRun(input: ExecuteTeamRunInput): Promise<{ manifest: TeamRunManifest; tasks: TeamTaskState[] }> {
545
- let workflow = input.workflow;
546
- let manifest = updateRunStatus(input.manifest, "running", input.executeWorkers ? "Executing team workflow." : "Creating workflow prompts and placeholder results.");
547
-
548
- try {
549
- return await executeTeamRunCore(input, manifest, workflow);
550
- } catch (error) {
551
- // P1: Catch unhandled errors — ensure manifest is set to "failed" so it doesn't stay "running" forever.
552
- const message = error instanceof Error ? error.message : String(error);
553
- try {
554
- manifest = updateRunStatus(manifest, "failed", `Unhandled error in team runner: ${message}`);
555
- await saveRunManifestAsync(manifest);
556
- } catch {
557
- // Best-effort state write may also fail
558
- }
559
- const tasks = refreshTaskGraphQueues(input.tasks).map((task) =>
560
- task.status === "running" || task.status === "queued" || task.status === "waiting"
561
- ? { ...task, status: "failed" as const, finishedAt: new Date().toISOString(), error: message }
562
- : task,
563
- );
564
- return { manifest, tasks };
565
- }
566
- }
567
-
568
- async function executeTeamRunCore(
569
- input: ExecuteTeamRunInput,
570
- manifest: TeamRunManifest,
571
- workflow: WorkflowConfig,
572
- ): Promise<{ manifest: TeamRunManifest; tasks: TeamTaskState[] }> {
573
- // Execute before_run_start hook (non-blocking by default)
574
- const beforeRunReport = await executeHook("before_run_start", { runId: manifest.runId, cwd: manifest.cwd });
575
- appendHookEvent(manifest, beforeRunReport);
576
- if (beforeRunReport.outcome === "block") {
577
- manifest = updateRunStatus(manifest, "blocked", beforeRunReport.reason ?? "before_run_start hook blocked the run.");
578
- return { manifest, tasks: input.tasks };
579
- }
580
- let tasks = refreshTaskGraphQueues(input.tasks);
581
- let queueIndex = buildTaskGraphIndex(tasks);
582
- const canInjectAdaptivePlan = workflow.name === "implementation";
583
- let adaptivePlanInjected = false;
584
- let adaptivePlanMissing = false;
585
- const attemptAdaptivePlan = () => {
586
- if (!canInjectAdaptivePlan || adaptivePlanInjected || adaptivePlanMissing) return { injected: false, missing: false };
587
- const adaptivePlan = injectAdaptivePlanIfReady({ manifest, tasks, workflow, team: input.team });
588
- adaptivePlanInjected = adaptivePlanInjected || adaptivePlan.injected;
589
- adaptivePlanMissing = adaptivePlan.missingPlan;
590
- workflow = adaptivePlan.workflow;
591
- if (adaptivePlan.injected) tasks = adaptivePlan.tasks;
592
- return { injected: adaptivePlan.injected, missing: adaptivePlan.missingPlan };
593
- };
594
- const initialAdaptive = attemptAdaptivePlan();
595
- if (initialAdaptive.missing) {
596
- tasks = markBlocked(tasks, "Adaptive planner did not produce a valid subagent plan.");
597
- await saveRunTasksAsync(manifest, tasks);
598
- manifest = updateRunStatus(manifest, "blocked", "Adaptive planner did not produce a valid subagent plan.");
599
- return { manifest, tasks };
600
- }
601
- if (initialAdaptive.injected) {
602
- manifest = requiresPlanApproval(workflow, input.runtimeConfig) ? ensurePlanApprovalRequested(manifest, tasks) : manifest;
603
- queueIndex = buildTaskGraphIndex(tasks);
604
- } else if (requiresPlanApproval(workflow, input.runtimeConfig) && hasPendingMutatingAdaptiveTask(tasks)) {
605
- manifest = ensurePlanApprovalRequested(manifest, tasks);
606
- }
607
- if (manifest.planApproval?.status === "cancelled") {
608
- tasks = cancelPlanTasks(tasks, "Plan approval was cancelled.");
609
- await saveRunTasksAsync(manifest, tasks);
610
- manifest = updateRunStatus(manifest, "cancelled", "Plan approval was cancelled.");
611
- return { manifest, tasks };
612
- }
613
- manifest = writeProgress(manifest, tasks, "team-runner", input.executeWorkers, input.runtimeConfig);
614
- await saveRunManifestAsync(manifest);
615
- const runtimeKind = input.runtime?.kind ?? (input.executeWorkers ? "child-process" : "scaffold");
616
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
617
-
618
- // Build a workflow phase state machine from workflow steps for precondition tracking.
619
- const workflowPhases: PhaseState[] = workflow.steps.map((step): PhaseState => ({
620
- name: step.id,
621
- status: "pending",
622
- inputs: step.reads === false ? [] : Array.isArray(step.reads) ? step.reads : [],
623
- outputs: step.output === false ? [] : step.output ? [step.output] : [],
624
- }));
625
- let wfMachine = createWorkflowStateMachine(workflowPhases);
626
-
627
- while (tasks.some((task) => task.status === "queued")) {
628
- if (input.signal?.aborted) {
629
- const cancelReason = cancellationReasonFromSignal(input.signal);
630
- const message = `${cancelReason.message} (${cancelReason.code})`;
631
- const cancelledTaskIds: string[] = [];
632
- tasks = tasks.map((task) => {
633
- if (task.status !== "queued" && task.status !== "running" && task.status !== "waiting") return task;
634
- cancelledTaskIds.push(task.id);
635
- const base = { ...task, status: "cancelled" as const, finishedAt: new Date().toISOString(), error: message };
636
- if (task.status === "running") {
637
- return { ...base, terminalEvidence: [...(task.terminalEvidence ?? []), buildSyntheticTerminalEvidence("worker", cancelReason, task.startedAt)] };
638
- }
639
- return base;
640
- });
641
- await saveRunTasksAsync(manifest, tasks);
642
- for (const taskId of cancelledTaskIds) appendEvent(manifest.eventsPath, { type: "task.cancelled", runId: manifest.runId, taskId, message, data: { reason: cancelReason.code } });
643
- manifest = updateRunStatus(manifest, "cancelled", message, { data: { reason: cancelReason.code, cancelledTaskIds } });
644
- return { manifest, tasks };
645
- }
646
-
647
- const failed = tasks.find((task) => task.status === "failed");
648
- if (failed) {
649
- tasks = markBlocked(tasks, `Blocked by failed task '${failed.id}'.`);
650
- await saveRunTasksAsync(manifest, tasks);
651
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
652
- manifest = updateRunStatus(manifest, "failed", `Failed at task '${failed.id}'.`);
653
- return { manifest, tasks };
654
- }
655
-
656
- const snapshot = taskGraphSnapshot(tasks, queueIndex);
657
-
658
- // DAG-based execution plan: when tasks have explicit dependsOn, use the
659
- // topological wave planner to determine ready tasks. Fall back to the
660
- // existing task-graph-scheduler when no explicit deps exist (backward compat).
661
- const completedIds = new Set(tasks.filter((t) => t.status === "completed").map((t) => t.id));
662
- const dagReady = dagReadyTaskIds(tasks, completedIds);
663
- const effectiveReady = dagReady ?? snapshot.ready;
664
-
665
- // Workflow phase precondition check (non-blocking: log warnings only).
666
- if (wfMachine.currentPhaseIndex < wfMachine.phases.length) {
667
- const completedArtifacts = manifest.artifacts.filter((a) => a.kind === "result" || a.kind === "summary").map((a) => a.path);
668
- const previousPhaseStatus = wfMachine.currentPhaseIndex > 0 ? (wfMachine.phases[wfMachine.currentPhaseIndex - 1]?.status ?? "pending") : "completed";
669
- const wfContext: PhaseGuardContext = {
670
- completedArtifacts,
671
- previousPhaseStatus,
672
- taskResults: tasks.filter((t) => t.status === "completed").map((t) => ({ taskId: t.id, status: t.status, outputPath: t.resultArtifact?.path })),
673
- };
674
- const preconditions = validatePhasePreconditions(wfMachine, wfContext);
675
- if (!preconditions.ready) {
676
- appendEvent(manifest.eventsPath, { type: "workflow.preconditions", runId: manifest.runId, message: `Workflow phase '${wfMachine.phases[wfMachine.currentPhaseIndex]?.name}' is missing inputs: ${preconditions.blocking.join(", ")}`, data: { phaseIndex: wfMachine.currentPhaseIndex, phaseName: wfMachine.phases[wfMachine.currentPhaseIndex]?.name, blocking: preconditions.blocking } });
677
- } else {
678
- // Advance the machine past completed phases.
679
- while (wfMachine.currentPhaseIndex < wfMachine.phases.length && wfMachine.phases[wfMachine.currentPhaseIndex]?.status === "completed") {
680
- wfMachine = { ...wfMachine, currentPhaseIndex: wfMachine.currentPhaseIndex + 1 };
681
- }
682
- }
683
- }
684
-
685
- const readyRoles = effectiveReady.map((taskId) => tasks.find((task) => task.id === taskId)?.role).filter((role): role is string => Boolean(role));
686
- const concurrency = resolveBatchConcurrency({ workflowName: workflow.name, workflowMaxConcurrency: workflow.maxConcurrency, teamMaxConcurrency: input.team.maxConcurrency, limitMaxConcurrentWorkers: input.limits?.maxConcurrentWorkers, allowUnboundedConcurrency: input.limits?.allowUnboundedConcurrency, readyCount: effectiveReady.length, workspaceMode: manifest.workspaceMode, readyRoles });
687
- if (concurrency.reason.includes(";unbounded:")) {
688
- appendEvent(manifest.eventsPath, { type: "limits.unbounded", runId: manifest.runId, message: "Unbounded worker concurrency was explicitly enabled for this run.", data: { concurrencyReason: concurrency.reason, maxConcurrent: concurrency.maxConcurrent } });
689
- }
690
- const approvalPending = isPlanApprovalPending(manifest);
691
- const readyIds = approvalPending ? effectiveReady : effectiveReady.slice(0, concurrency.selectedCount);
692
- const candidateBatch = readyIds.map((id) => tasks.find((task) => task.id === id)).filter((task): task is TeamTaskState => Boolean(task));
693
- const readyBatch = approvalPending ? candidateBatch.filter((task) => !isMutatingTask(task)).slice(0, concurrency.selectedCount) : candidateBatch;
694
- if (readyBatch.length === 0) {
695
- if (approvalPending && candidateBatch.some(isMutatingTask)) {
696
- await saveRunTasksAsync(manifest, tasks);
697
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
698
- manifest = updateRunStatus(manifest, "blocked", "Plan approval required before mutating implementation tasks run.");
699
- return { manifest, tasks };
700
- }
701
- tasks = markBlocked(tasks, "No ready queued task; dependency graph may be invalid.");
702
- await saveRunTasksAsync(manifest, tasks);
703
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
704
- manifest = updateRunStatus(manifest, "blocked", "No ready queued task.");
705
- return { manifest, tasks };
706
- }
707
-
708
- appendEvent(manifest.eventsPath, { type: "task.progress", runId: manifest.runId, message: `Starting ready batch with ${readyBatch.length} task(s).`, data: { taskIds: readyBatch.map((task) => task.id), readyCount: snapshot.ready.length, blockedCount: snapshot.blocked.length, runningCount: snapshot.running.length, doneCount: snapshot.done.length, selectedCount: readyBatch.length, maxConcurrent: concurrency.maxConcurrent, defaultConcurrency: concurrency.defaultConcurrency, concurrencyReason: approvalPending ? `${concurrency.reason};plan-approval-read-only` : concurrency.reason } });
709
- // Execute before_task_start hooks for the batch
710
- for (const task of readyBatch) {
711
- const taskReport = await executeHook("before_task_start", { runId: manifest.runId, taskId: task.id, cwd: manifest.cwd });
712
- appendHookEvent(manifest, taskReport);
713
- if (taskReport.outcome === "block") {
714
- tasks = tasks.map((t) => t.id === task.id ? { ...t, status: "skipped" as const, error: taskReport.reason ?? "before_task_start hook blocked execution." } : t);
715
- manifest = updateRunStatus(manifest, manifest.status, `Task '${task.id}' blocked by hook.`);
716
- }
717
- }
718
- const batchTasks = readyBatch.filter((task) => tasks.find((t) => t.id === task.id && t.status !== "skipped"));
719
- if (batchTasks.length > 1) {
720
- appendEvent(manifest.eventsPath, { type: "task.parallel_start", runId: manifest.runId, message: `Launching ${batchTasks.length} tasks in PARALLEL (concurrency=${concurrency.selectedCount}): ${batchTasks.map((t) => `${t.role}(${t.id})`).join(", ")}`, data: { taskIds: batchTasks.map((t) => t.id), roles: batchTasks.map((t) => t.role), concurrency: concurrency.selectedCount } });
721
- }
722
- const results = await mapConcurrent(
723
- batchTasks,
724
- concurrency.selectedCount,
725
- async (task) => {
726
- const step = findStep(workflow, task);
727
- const agent = findAgent(input.agents, task);
728
- const teamRole = input.team.roles.find((role) => role.name === task.role);
729
- const baseInput = { manifest, tasks, task, step, agent, signal: input.signal, executeWorkers: input.executeWorkers, runtimeKind: input.runtime?.kind, runtimeConfig: input.runtimeConfig, parentContext: input.parentContext, parentModel: input.parentModel, modelRegistry: input.modelRegistry, modelOverride: input.modelOverride, teamRoleModel: teamRole?.model, teamRoleSkills: teamRole?.skills, skillOverride: input.skillOverride, limits: input.limits, onJsonEvent: input.onJsonEvent };
730
- if (input.reliability?.autoRetry !== true) return withCorrelation(childCorrelation(manifest.runId, task.id), () => runTeamTask(baseInput));
731
- let lastFailed: { manifest: TeamRunManifest; tasks: TeamTaskState[] } | undefined;
732
- let lastAttemptId: string | undefined;
733
- const attemptsSoFar: TaskAttemptState[] = [...(task.attempts ?? [])];
734
- const policy = retryPolicyFromConfig(input.reliability);
735
- try {
736
- return await executeWithRetry(async (attempt, info) => {
737
- const startedAt = new Date().toISOString();
738
- const inFlightAttempts: TaskAttemptState[] = [...attemptsSoFar, { attemptId: info.attemptId, startedAt }];
739
- input.metricRegistry?.counter("crew.task.retry_attempt_total", "Retry attempts by run and task").inc({ runId: manifest.runId, taskId: task.id });
740
- const fresh = loadRunManifestById(manifest.cwd, manifest.runId);
741
- const freshManifest = fresh?.manifest ?? manifest;
742
- const freshTasks = fresh?.tasks ?? tasks;
743
- const freshTask = freshTasks.find((item) => item.id === task.id) ?? task;
744
- if (freshTask.status !== "queued" && freshTask.status !== "running") return { manifest: freshManifest, tasks: freshTasks };
745
- const taskWithAttempt: TeamTaskState = { ...freshTask, attempts: inFlightAttempts };
746
- const result = await withCorrelation(childCorrelation(freshManifest.runId, task.id), () => runTeamTask({ ...baseInput, manifest: freshManifest, tasks: freshTasks, task: taskWithAttempt }));
747
- const failed = failedTaskFrom(result, task.id);
748
- const endedAt = new Date().toISOString();
749
- const finishedAttempt: TaskAttemptState = { attemptId: info.attemptId, startedAt, endedAt, ...(failed?.error ? { error: failed.error } : {}) };
750
- attemptsSoFar.push(finishedAttempt);
751
- const withAttempt = result.tasks.map((item) => item.id === task.id ? { ...item, attempts: [...attemptsSoFar] } : item);
752
- const enriched = { manifest: result.manifest, tasks: withAttempt };
753
- if (failed) {
754
- lastFailed = enriched;
755
- throw new Error(failed.error ?? `Task ${task.id} failed.`);
756
- }
757
- input.metricRegistry?.histogram("crew.task.retry_count", "Retries per task", [0, 1, 2, 3, 5, 10]).observe({ runId: manifest.runId, team: input.team.name }, Math.max(0, attempt - 1));
758
- return enriched;
759
- }, policy, {
760
- signal: input.signal,
761
- attemptId: (attempt) => `${manifest.runId}:${task.id}:attempt-${attempt}`,
762
- onAttemptFailed: (attempt, error, delayMs, info) => {
763
- lastAttemptId = info.attemptId;
764
- appendEvent(manifest.eventsPath, { type: "crew.task.retry_attempt", runId: manifest.runId, taskId: task.id, message: error.message, data: { attempt, attemptId: info.attemptId, delayMs }, metadata: { attemptId: info.attemptId } });
765
- input.metricRegistry?.histogram("crew.task.retry_delay_ms", "Retry backoff delay, milliseconds").observe({ runId: manifest.runId, taskId: task.id }, delayMs);
766
- },
767
- onRetryGivenUp: (attempts, error, info) => {
768
- lastAttemptId = info.attemptId;
769
- appendDeadletter(manifest, { runId: manifest.runId, taskId: task.id, reason: "max-retries", attempts, attemptId: info.attemptId, lastError: error.message, timestamp: new Date().toISOString() });
770
- input.metricRegistry?.counter("crew.task.deadletter_total", "Deadletter triggers by reason").inc({ reason: "max-retries" });
771
- input.metricRegistry?.histogram("crew.task.retry_count", "Retries per task", [0, 1, 2, 3, 5, 10]).observe({ runId: manifest.runId, team: input.team.name }, Math.max(0, attempts - 1));
772
- },
773
- });
774
- } catch (retryError) {
775
- if (retryError instanceof CrewCancellationError || input.signal?.aborted) {
776
- const reason = retryError instanceof CrewCancellationError ? retryError.reason : cancellationReasonFromSignal(input.signal);
777
- const fresh = loadRunManifestById(manifest.cwd, manifest.runId);
778
- const freshManifest = fresh?.manifest ?? manifest;
779
- const freshTasks = fresh?.tasks ?? tasks;
780
- const cancelledTasks = freshTasks.map((item) => item.id === task.id && (item.status === "queued" || item.status === "running") ? { ...item, status: "cancelled" as const, finishedAt: new Date().toISOString(), error: `${reason.message} (${reason.code})` } : item);
781
- appendEvent(freshManifest.eventsPath, { type: "task.cancelled", runId: freshManifest.runId, taskId: task.id, message: reason.message, data: { reason, phase: "retry" }, metadata: lastAttemptId ? { attemptId: lastAttemptId } : undefined });
782
- return { manifest: updateRunStatus(freshManifest, "cancelled", reason.message), tasks: cancelledTasks };
783
- }
784
- if (lastFailed) return lastFailed;
785
- const fresh = loadRunManifestById(manifest.cwd, manifest.runId);
786
- const freshManifest = fresh?.manifest ?? manifest;
787
- const freshTasks = fresh?.tasks ?? tasks;
788
- const freshTask = freshTasks.find((item) => item.id === task.id) ?? task;
789
- if (freshTask.status !== "queued" && freshTask.status !== "running") return { manifest: freshManifest, tasks: freshTasks };
790
- return withCorrelation(childCorrelation(freshManifest.runId, task.id), () => runTeamTask({ ...baseInput, manifest: freshManifest, tasks: freshTasks, task: freshTask }));
791
- }
792
- },
793
- );
794
- if (results.length === 0) break;
795
- manifest = { ...results.at(-1)!.manifest, artifacts: mergeArtifacts([manifest.artifacts, ...results.map((item) => item.manifest.artifacts)].flat()) };
796
- tasks = __test__mergeTaskUpdates(tasks, results);
797
-
798
- // Advance workflow phases whose tasks are all in terminal state
799
- const terminalStatuses = new Set(["completed", "failed", "skipped", "cancelled"]);
800
- const phaseTaskMap = new Map<string, string[]>();
801
- for (const task of tasks) {
802
- if (!task.stepId) continue;
803
- const existing = phaseTaskMap.get(task.stepId) ?? [];
804
- existing.push(task.id);
805
- phaseTaskMap.set(task.stepId, existing);
806
- }
807
- for (let pi = wfMachine.currentPhaseIndex; pi < wfMachine.phases.length; pi++) {
808
- const phase = wfMachine.phases[pi]!;
809
- const phaseTaskIds = phaseTaskMap.get(phase.name) ?? [];
810
- if (phaseTaskIds.length === 0) continue;
811
- const allTerminal = phaseTaskIds.every((taskId) => {
812
- const task = tasks.find((t) => t.id === taskId);
813
- return task ? terminalStatuses.has(task.status) : false;
814
- });
815
- if (!allTerminal) break;
816
- if (phase.status !== "completed" && phase.status !== "failed" && phase.status !== "skipped") {
817
- const completedArtifacts = manifest.artifacts.filter((a) => a.kind === "result" || a.kind === "summary").map((a) => a.path);
818
- const previousPhaseStatus = pi > 0 ? (wfMachine.phases[pi - 1]?.status ?? "pending") : "completed";
819
- const wfContext: PhaseGuardContext = {
820
- completedArtifacts,
821
- previousPhaseStatus,
822
- taskResults: tasks.filter((t) => t.status === "completed").map((t) => ({ taskId: t.id, status: t.status, outputPath: t.resultArtifact?.path })),
823
- };
824
- // Determine phase transition status based on individual task outcomes
825
- const phaseTasks = phaseTaskIds.map((taskId) => tasks.find((t) => t.id === taskId)).filter((t): t is NonNullable<typeof t> => t !== undefined);
826
- const hasFailedOrCancelled = phaseTasks.some((t) => t.status === "failed" || t.status === "cancelled");
827
- const phaseStatus = hasFailedOrCancelled ? "failed" : "completed";
828
- const transition = transitionPhase(wfMachine, pi, phaseStatus, wfContext);
829
- wfMachine = transition.machine;
830
- if (transition.guardResult && !transition.guardResult.allowed) {
831
- appendEvent(manifest.eventsPath, { type: "workflow.phase_guard_blocked", runId: manifest.runId, message: `Workflow phase '${phase.name}' guard blocked: ${transition.guardResult.reason ?? "unknown"}`, data: { phaseIndex: pi, phaseName: phase.name, reason: transition.guardResult.reason } });
832
- break;
833
- }
834
- appendEvent(manifest.eventsPath, { type: phaseStatus === "failed" ? "workflow.phase_failed" : "workflow.phase_completed", runId: manifest.runId, message: `Workflow phase '${phase.name}' ${phaseStatus}.`, data: { phaseIndex: pi, phaseStatus } });
835
- }
836
- wfMachine = { ...wfMachine, currentPhaseIndex: pi + 1 };
837
- }
838
-
839
- const cancelledResult = results.find((item) => item.manifest.status === "cancelled");
840
- if (cancelledResult || input.signal?.aborted) {
841
- const reason = input.signal?.aborted ? cancellationReasonFromSignal(input.signal) : undefined;
842
- const message = reason?.message ?? cancelledResult?.manifest.summary ?? "Run cancelled during task execution.";
843
- manifest = { ...manifest, status: "running" };
844
- manifest = updateRunStatus(manifest, "cancelled", message);
845
- await saveRunTasksAsync(manifest, tasks);
846
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
847
- await saveRunManifestAsync(manifest);
848
- appendEvent(manifest.eventsPath, { type: "run.cancelled", runId: manifest.runId, message, data: { reason, phase: "task-batch", cancelledResultRunId: cancelledResult?.manifest.runId } });
849
- return { manifest, tasks };
850
- }
851
- queueIndex = buildTaskGraphIndex(tasks);
852
- const injectedAfterBatch = attemptAdaptivePlan();
853
- if (injectedAfterBatch.missing) {
854
- tasks = markBlocked(tasks, "Adaptive planner did not produce a valid subagent plan.");
855
- await saveRunTasksAsync(manifest, tasks);
856
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
857
- manifest = updateRunStatus(manifest, "blocked", "Adaptive planner did not produce a valid subagent plan.");
858
- return { manifest, tasks };
859
- }
860
- if (injectedAfterBatch.injected) {
861
- manifest = requiresPlanApproval(workflow, input.runtimeConfig) ? ensurePlanApprovalRequested(manifest, tasks) : manifest;
862
- queueIndex = buildTaskGraphIndex(tasks);
863
- } else if (requiresPlanApproval(workflow, input.runtimeConfig) && hasPendingMutatingAdaptiveTask(tasks)) {
864
- manifest = ensurePlanApprovalRequested(manifest, tasks);
865
- }
866
- if (manifest.planApproval?.status === "cancelled") {
867
- tasks = cancelPlanTasks(tasks, "Plan approval was cancelled.");
868
- await saveRunTasksAsync(manifest, tasks);
869
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
870
- manifest = updateRunStatus(manifest, "cancelled", "Plan approval was cancelled.");
871
- return { manifest, tasks };
872
- }
873
- await saveRunTasksAsync(manifest, tasks);
874
- saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
875
- const completedBatch = batchTasks.map((task) => tasks.find((item) => item.id === task.id) ?? task);
876
- const batchArtifact = writeArtifact(manifest.artifactsRoot, {
877
- kind: "summary",
878
- relativePath: `batches/${batchTasks.map((task) => task.id).join("+")}.md`,
879
- producer: "team-runner",
880
- content: aggregateTaskOutputs(completedBatch, manifest),
881
- });
882
- const groupDelivery = deliverGroupJoin({ manifest, mode: resolveGroupJoinMode(input.runtimeConfig), batch: batchTasks, allTasks: tasks });
883
- manifest = { ...manifest, artifacts: mergeArtifacts([...manifest.artifacts, batchArtifact, ...(groupDelivery?.artifact ? [groupDelivery.artifact] : [])]) };
884
- manifest = writeProgress(manifest, tasks, "team-runner", input.executeWorkers, input.runtimeConfig);
885
- await saveRunManifestAsync(manifest);
886
- }
887
-
888
- const failed = tasks.find((task) => task.status === "failed");
889
- const waiting = tasks.find((task) => task.status === "waiting");
890
- const running = tasks.find((task) => task.status === "running");
891
- manifest = applyPolicy(manifest, tasks, input.limits);
892
- const effectiveness = evaluateRunEffectiveness({ manifest, tasks, executeWorkers: input.executeWorkers, runtimeConfig: input.runtimeConfig });
893
- const effectivenessDecision = effectivenessPolicyDecision(effectiveness);
894
- if (effectivenessDecision) {
895
- manifest = { ...manifest, policyDecisions: [...(manifest.policyDecisions ?? []), effectivenessDecision], updatedAt: new Date().toISOString() };
896
- appendEvent(manifest.eventsPath, { type: "run.effectiveness", runId: manifest.runId, message: effectivenessDecision.message, data: { effectiveness, policyDecision: effectivenessDecision } });
897
- }
898
- const blockingDecision = manifest.policyDecisions?.find((item) => item.action === "block" || item.action === "escalate");
899
- if (failed) {
900
- manifest = updateRunStatus(manifest, "failed", `Failed at task '${failed.id}'.`);
901
- } else if (waiting) {
902
- manifest = updateRunStatus(manifest, "blocked", `Waiting for response to task '${waiting.id}'.`);
903
- } else if (running) {
904
- manifest = updateRunStatus(manifest, "blocked", `Task '${running.id}' is still running.`);
905
- } else if (effectiveness.severity === "failed") {
906
- manifest = updateRunStatus(manifest, "failed", effectivenessDecision?.message ?? "Run effectiveness guard failed.");
907
- } else if (effectiveness.severity === "blocked") {
908
- manifest = updateRunStatus(manifest, "blocked", effectivenessDecision?.message ?? "Run effectiveness guard blocked completion.");
909
- } else if (blockingDecision) {
910
- manifest = updateRunStatus(manifest, "blocked", blockingDecision.message);
911
- } else {
912
- manifest = updateRunStatus(manifest, "completed", input.executeWorkers ? "Team workflow completed." : "Team workflow scaffold completed without launching child workers.");
913
- }
914
- manifest = writeProgress(manifest, tasks, "team-runner", input.executeWorkers, input.runtimeConfig);
915
- await saveRunManifestAsync(manifest);
916
- const usage = aggregateUsage(tasks);
917
- const summaryArtifact = writeArtifact(manifest.artifactsRoot, {
918
- kind: "summary",
919
- relativePath: "summary.md",
920
- producer: "team-runner",
921
- content: [
922
- `# pi-crew run ${manifest.runId}`,
923
- "",
924
- `Status: ${manifest.status}`,
925
- `Team: ${manifest.team}`,
926
- `Workflow: ${manifest.workflow ?? "(none)"}`,
927
- `Goal: ${manifest.goal}`,
928
- `Usage: ${formatUsage(usage)}`,
929
- "",
930
- "## Tasks",
931
- ...tasks.map(formatTaskProgress),
932
- "",
933
- "## Effectiveness",
934
- ...runEffectivenessLines(manifest, tasks, input.executeWorkers, input.runtimeConfig),
935
- "",
936
- "## Policy decisions",
937
- ...(manifest.policyDecisions?.length ? summarizePolicyDecisions(manifest.policyDecisions) : ["- (none)"]),
938
- "",
939
- ].join("\n"),
940
- });
941
- manifest = { ...manifest, updatedAt: new Date().toISOString(), artifacts: [...manifest.artifacts, summaryArtifact] };
942
- await saveRunManifestAsync(manifest);
943
- await saveRunTasksAsync(manifest, tasks);
944
- return { manifest, tasks };
945
- }
1
+ import * as fs from "node:fs";
2
+ import type { AgentConfig } from "../agents/agent-config.ts";
3
+ import type { CrewLimitsConfig, CrewRuntimeConfig, CrewReliabilityConfig } from "../config/config.ts";
4
+ import type { CrewRuntimeCapabilities } from "./runtime-resolver.ts";
5
+ import type { CrewRuntimeKind } from "./crew-agent-runtime.ts";
6
+ import { resolveTaskRuntimeKind } from "./runtime-policy.ts";
7
+ import { writeArtifact } from "../state/artifact-store.ts";
8
+ import { executeHook, appendHookEvent } from "../hooks/registry.ts";
9
+ import { appendEvent, appendEventFireAndForget } from "../state/event-log.ts";
10
+ import type { TeamConfig } from "../teams/team-config.ts";
11
+ import type { ArtifactDescriptor, PolicyDecision, TeamRunManifest, TaskAttemptState, TeamTaskState } from "../state/types.ts";
12
+ import { loadRunManifestById, saveRunManifest, saveRunManifestAsync, saveRunTasksAsync, updateRunStatus } from "../state/state-store.ts";
13
+ import { aggregateUsage, formatUsage } from "../state/usage.ts";
14
+ import type { WorkflowConfig, WorkflowStep } from "../workflows/workflow-config.ts";
15
+ import { evaluateCrewPolicy, summarizePolicyDecisions } from "./policy-engine.ts";
16
+ import { buildRecoveryLedger } from "./recovery-recipes.ts";
17
+ import { buildTaskGraphIndex, refreshTaskGraphQueues, taskGraphSnapshot } from "./task-graph-scheduler.ts";
18
+ import { buildExecutionPlan as buildDagExecutionPlan, getReadyTasks as getDagReadyTasks, type TaskNode } from "./task-graph.ts";
19
+ import { checkBranchFreshness } from "../worktree/branch-freshness.ts";
20
+ import { aggregateTaskOutputs } from "./task-output-context.ts";
21
+ import { readCrewAgents, saveCrewAgents } from "./crew-agent-records.ts";
22
+ import { recordsForMaterializedTasks } from "./task-display.ts";
23
+ import { deliverGroupJoin, resolveGroupJoinMode } from "./group-join.ts";
24
+ import { runTeamTask } from "./task-runner.ts";
25
+ import { terminateLiveAgentsForRun } from "./live-agent-manager.ts";
26
+ import { createWorkflowStateMachine, validatePhasePreconditions, transitionPhase, type PhaseState, type PhaseGuardContext } from "./workflow-state.ts";
27
+ import { executeWithRetry, DEFAULT_RETRY_POLICY, type RetryPolicy } from "./retry-executor.ts";
28
+ import { appendDeadletter } from "./deadletter.ts";
29
+ import type { MetricRegistry } from "../observability/metric-registry.ts";
30
+ import { childCorrelation, withCorrelation } from "../observability/correlation.ts";
31
+ import { resolveBatchConcurrency } from "./concurrency.ts";
32
+ import { mapConcurrent } from "./parallel-utils.ts";
33
+ import { permissionForRole } from "./role-permission.ts";
34
+ import { registerRunPromise, resolveRunPromise, rejectRunPromise } from "./run-tracker.ts";
35
+ import { clearTrackedTaskUsage } from "./usage-tracker.ts";
36
+ import { CrewCancellationError, buildSyntheticTerminalEvidence, cancellationReasonFromSignal } from "./cancellation.ts";
37
+ import { effectivenessPolicyDecision, evaluateRunEffectiveness, formatRunEffectivenessLines } from "./effectiveness.ts";
38
+
39
+ export interface ExecuteTeamRunInput {
40
+ manifest: TeamRunManifest;
41
+ tasks: TeamTaskState[];
42
+ team: TeamConfig;
43
+ workflow: WorkflowConfig;
44
+ agents: AgentConfig[];
45
+ executeWorkers: boolean;
46
+ limits?: CrewLimitsConfig;
47
+ runtime?: CrewRuntimeCapabilities;
48
+ runtimeConfig?: CrewRuntimeConfig;
49
+ parentContext?: string;
50
+ parentModel?: unknown;
51
+ modelRegistry?: unknown;
52
+ modelOverride?: string;
53
+ signal?: AbortSignal;
54
+ reliability?: CrewReliabilityConfig;
55
+ metricRegistry?: MetricRegistry;
56
+ /** Skill override from the team tool. false disables skill injection for this run. */
57
+ skillOverride?: string[] | false;
58
+ /** Optional callback for JSON events from child Pi. Used for overflow recovery tracking. */
59
+ onJsonEvent?: (taskId: string, runId: string, event: unknown) => void;
60
+ /** Workspace where this run was initiated — used for session-scoped live-agent visibility. */
61
+ workspaceId: string;
62
+ }
63
+
64
+ function findStep(workflow: WorkflowConfig, task: TeamTaskState): WorkflowStep {
65
+ const step = workflow.steps.find((candidate) => candidate.id === task.stepId);
66
+ if (!step) throw new Error(`Workflow step '${task.stepId}' not found for task '${task.id}'.`);
67
+ return step;
68
+ }
69
+
70
+ function findAgent(agents: AgentConfig[], task: TeamTaskState): AgentConfig {
71
+ const agent = agents.find((candidate) => candidate.name === task.agent);
72
+ if (!agent) throw new Error(`Agent '${task.agent}' not found for task '${task.id}'.`);
73
+ return agent;
74
+ }
75
+
76
+ function markBlocked(tasks: TeamTaskState[], reason: string): TeamTaskState[] {
77
+ return tasks.map((task) => task.status === "queued" ? { ...task, status: "skipped", error: reason, finishedAt: new Date().toISOString(), graph: task.graph ? { ...task.graph, queue: "blocked" } : undefined } : task);
78
+ }
79
+
80
+ function mergeArtifacts(items: ArtifactDescriptor[]): ArtifactDescriptor[] {
81
+ const byPath = new Map<string, ArtifactDescriptor>();
82
+ for (const item of items) byPath.set(item.path, item);
83
+ return [...byPath.values()];
84
+ }
85
+
86
+ function isNonTerminalTaskStatus(status: TeamTaskState["status"]): boolean {
87
+ return status === "queued" || status === "running" || status === "waiting";
88
+ }
89
+
90
+ function shouldMergeTaskUpdate(current: TeamTaskState, updated: TeamTaskState): boolean {
91
+ // Parallel workers receive the same input snapshot. A later result may still
92
+ // contain stale queued/running copies of tasks that another worker already
93
+ // completed. Never let those stale snapshots regress durable task state.
94
+ if (!isNonTerminalTaskStatus(current.status) && isNonTerminalTaskStatus(updated.status)) return false;
95
+ // Prevent a stale completed task from overwriting a fresher one.
96
+ if (current.finishedAt && updated.finishedAt) {
97
+ const currentFinished = new Date(current.finishedAt).getTime();
98
+ const updatedFinished = new Date(updated.finishedAt).getTime();
99
+ if (!Number.isNaN(currentFinished) && !Number.isNaN(updatedFinished) && updatedFinished < currentFinished) return false;
100
+ }
101
+ return updated.status !== current.status || updated.finishedAt !== current.finishedAt || updated.startedAt !== current.startedAt || Boolean(updated.resultArtifact) || Boolean(updated.error) || Boolean(updated.modelAttempts?.length) || Boolean(updated.usage) || Boolean(updated.attempts?.length);
102
+ }
103
+
104
+ export function __test__mergeTaskUpdates(base: TeamTaskState[], results: Array<{ tasks: TeamTaskState[] }>): TeamTaskState[] {
105
+ let merged = base;
106
+ for (const result of results) {
107
+ for (const updated of result.tasks) {
108
+ const current = merged.find((task) => task.id === updated.id);
109
+ if (!current || !shouldMergeTaskUpdate(current, updated)) continue;
110
+ merged = merged.map((task) => task.id === updated.id ? updated : task);
111
+ }
112
+ }
113
+ return refreshTaskGraphQueues(merged);
114
+ }
115
+
116
+ // 2.8: adaptive-plan parsing/repair/injection moved to src/runtime/adaptive-plan.ts.
117
+ // Re-export the test-only helpers so existing test imports still resolve.
118
+ export { __test__parseAdaptivePlan, __test__repairAdaptivePlan } from "./adaptive-plan.ts";
119
+ import { injectAdaptivePlanIfReady } from "./adaptive-plan.ts";
120
+
121
+ function formatTaskProgress(task: TeamTaskState): string {
122
+ 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}` : ""}`;
123
+ }
124
+
125
+ function runEffectivenessLines(manifest: TeamRunManifest, tasks: TeamTaskState[], executeWorkers: boolean, runtimeConfig?: CrewRuntimeConfig): string[] {
126
+ return formatRunEffectivenessLines(evaluateRunEffectiveness({ manifest, tasks, executeWorkers, runtimeConfig }));
127
+ }
128
+
129
+ function writeProgress(manifest: TeamRunManifest, tasks: TeamTaskState[], producer: string, executeWorkers = true, runtimeConfig?: CrewRuntimeConfig): TeamRunManifest {
130
+ const counts = new Map<string, number>();
131
+ for (const task of tasks) counts.set(task.status, (counts.get(task.status) ?? 0) + 1);
132
+ const queue = taskGraphSnapshot(tasks);
133
+ const progress = writeArtifact(manifest.artifactsRoot, {
134
+ kind: "progress",
135
+ relativePath: "progress.md",
136
+ producer,
137
+ content: [
138
+ `# pi-crew progress ${manifest.runId}`,
139
+ "",
140
+ `Status: ${manifest.status}`,
141
+ `Team: ${manifest.team}`,
142
+ `Workflow: ${manifest.workflow ?? "(none)"}`,
143
+ `Updated: ${new Date().toISOString()}`,
144
+ `Task counts: ${[...counts.entries()].map(([status, count]) => `${status}=${count}`).join(", ") || "none"}`,
145
+ `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}`,
146
+ "",
147
+ "## Tasks",
148
+ ...tasks.map(formatTaskProgress),
149
+ "",
150
+ "## Effectiveness",
151
+ ...runEffectivenessLines(manifest, tasks, executeWorkers, runtimeConfig),
152
+ "",
153
+ ].join("\n"),
154
+ });
155
+ return { ...manifest, updatedAt: new Date().toISOString(), artifacts: [...manifest.artifacts.filter((artifact) => !(artifact.kind === "progress" && artifact.path === progress.path)), progress] };
156
+ }
157
+
158
+ function applyPolicy(manifest: TeamRunManifest, tasks: TeamTaskState[], limits?: CrewLimitsConfig): TeamRunManifest {
159
+ const branchFreshness = checkBranchFreshness(manifest.cwd);
160
+ const branchArtifact = writeArtifact(manifest.artifactsRoot, {
161
+ kind: "metadata",
162
+ relativePath: "metadata/branch-freshness.json",
163
+ producer: "branch-freshness",
164
+ content: `${JSON.stringify(branchFreshness, null, 2)}\n`,
165
+ });
166
+ let decisions: PolicyDecision[] = evaluateCrewPolicy({ manifest, tasks, limits });
167
+ if (branchFreshness.status === "stale" || branchFreshness.status === "diverged") {
168
+ const branchDecision: PolicyDecision = {
169
+ action: "notify",
170
+ reason: "branch_stale",
171
+ message: branchFreshness.message,
172
+ createdAt: new Date().toISOString(),
173
+ };
174
+ decisions = [...decisions, branchDecision];
175
+ appendEvent(manifest.eventsPath, { type: "branch.stale", runId: manifest.runId, message: branchFreshness.message, data: { branchFreshness } });
176
+ }
177
+ const policyArtifact = writeArtifact(manifest.artifactsRoot, {
178
+ kind: "metadata",
179
+ relativePath: "policy-decisions.json",
180
+ producer: "policy-engine",
181
+ content: `${JSON.stringify(decisions, null, 2)}\n`,
182
+ });
183
+ const recoveryLedger = buildRecoveryLedger(decisions);
184
+ const recoveryArtifact = writeArtifact(manifest.artifactsRoot, {
185
+ kind: "metadata",
186
+ relativePath: "recovery-ledger.json",
187
+ producer: "recovery-engine",
188
+ content: `${JSON.stringify(recoveryLedger, null, 2)}\n`,
189
+ });
190
+ for (const item of decisions) appendEvent(manifest.eventsPath, { type: item.action === "escalate" ? "policy.escalated" : "policy.action", runId: manifest.runId, taskId: item.taskId, message: item.message, data: { action: item.action, reason: item.reason } });
191
+ for (const item of recoveryLedger.entries) appendEvent(manifest.eventsPath, { type: item.state === "escalation_required" ? "recovery.escalated" : "recovery.attempted", runId: manifest.runId, taskId: item.taskId, message: item.message, data: { scenario: item.scenario, steps: item.steps, attempt: item.attempt, state: item.state } });
192
+ return { ...manifest, updatedAt: new Date().toISOString(), policyDecisions: decisions, artifacts: [...manifest.artifacts.filter((artifact) => !(artifact.kind === "metadata" && (artifact.path.endsWith("policy-decisions.json") || artifact.path.endsWith("recovery-ledger.json") || artifact.path.endsWith("branch-freshness.json")))), branchArtifact, policyArtifact, recoveryArtifact] };
193
+ }
194
+
195
+ function retryPolicyFromConfig(config: CrewReliabilityConfig | undefined): RetryPolicy {
196
+ return { ...DEFAULT_RETRY_POLICY, ...(config?.retryPolicy ?? {}) };
197
+ }
198
+
199
+ function failedTaskFrom(result: { tasks: TeamTaskState[] }, taskId: string): TeamTaskState | undefined {
200
+ return result.tasks.find((item) => item.id === taskId && item.status === "failed");
201
+ }
202
+
203
+ function requiresPlanApproval(workflow: WorkflowConfig, runtimeConfig: CrewRuntimeConfig | undefined): boolean {
204
+ return workflow.name === "implementation" && runtimeConfig?.requirePlanApproval === true;
205
+ }
206
+
207
+ function isPlanApprovalPending(manifest: TeamRunManifest): boolean {
208
+ return manifest.planApproval?.required === true && manifest.planApproval.status === "pending";
209
+ }
210
+
211
+ function isMutatingTask(task: TeamTaskState): boolean {
212
+ return permissionForRole(task.role) !== "read_only";
213
+ }
214
+
215
+ function ensurePlanApprovalRequested(manifest: TeamRunManifest, tasks: TeamTaskState[]): TeamRunManifest {
216
+ if (manifest.planApproval) return manifest;
217
+ const assessTask = tasks.find((task) => task.stepId === "assess" && task.status === "completed");
218
+ const now = new Date().toISOString();
219
+ const updated: TeamRunManifest = {
220
+ ...manifest,
221
+ updatedAt: now,
222
+ planApproval: {
223
+ required: true,
224
+ status: "pending",
225
+ requestedAt: now,
226
+ updatedAt: now,
227
+ planTaskId: assessTask?.id,
228
+ planArtifactPath: assessTask?.resultArtifact?.path,
229
+ },
230
+ };
231
+ saveRunManifest(updated);
232
+ appendEvent(updated.eventsPath, { type: "plan.approval_required", runId: updated.runId, taskId: assessTask?.id, message: "Adaptive implementation plan requires explicit approval before mutating tasks run.", data: { planArtifactPath: assessTask?.resultArtifact?.path } });
233
+ return updated;
234
+ }
235
+
236
+ function cancelPlanTasks(tasks: TeamTaskState[], reason: string): TeamTaskState[] {
237
+ return tasks.map((task) => task.status === "queued" || task.status === "running" || task.status === "waiting" ? { ...task, status: "cancelled", finishedAt: new Date().toISOString(), error: reason, graph: task.graph ? { ...task.graph, queue: "done" } : undefined } : task);
238
+ }
239
+
240
+ function hasPendingMutatingAdaptiveTask(tasks: TeamTaskState[]): boolean {
241
+ return tasks.some((task) => task.status === "queued" && task.adaptive && isMutatingTask(task));
242
+ }
243
+
244
+ /**
245
+ * Check whether any task uses explicit `dependsOn` that would benefit from DAG-based
246
+ * execution planning. If so, build an execution plan and use `getDagReadyTasks`
247
+ * to augment the ready-set selection.
248
+ */
249
+ function dagReadyTaskIds(tasks: TeamTaskState[], completedIds: Set<string>): string[] | null {
250
+ const hasExplicitDeps = tasks.some((t) => t.dependsOn.length > 0);
251
+ if (!hasExplicitDeps) return null;
252
+ const nodes: TaskNode[] = tasks.map((t) => ({
253
+ id: t.id,
254
+ dependsOn: t.dependsOn,
255
+ phase: t.adaptive?.phase ?? t.stepId,
256
+ }));
257
+ const plan = buildDagExecutionPlan(nodes);
258
+ if (plan.hasCycle) return null; // fall back to existing scheduler
259
+ return getDagReadyTasks(plan, completedIds);
260
+ }
261
+
262
+ export async function executeTeamRun(input: ExecuteTeamRunInput): Promise<{ manifest: TeamRunManifest; tasks: TeamTaskState[] }> {
263
+ let workflow = input.workflow;
264
+ let manifest = updateRunStatus(input.manifest, "running", input.executeWorkers ? "Executing team workflow." : "Creating workflow prompts and placeholder results.");
265
+
266
+ const runPromise = registerRunPromise(manifest.runId);
267
+
268
+ const cleanupUsage = (): void => {
269
+ for (const task of input.tasks) clearTrackedTaskUsage(task.id);
270
+ };
271
+
272
+ try {
273
+ const result = await executeTeamRunCore(input, manifest, workflow);
274
+ resolveRunPromise(manifest.runId, result);
275
+ cleanupUsage();
276
+ // Terminate live agents for this run — agents are done when the run ends.
277
+ void terminateLiveAgentsForRun(manifest.runId, "completed", appendEvent, manifest.eventsPath).catch(() => {});
278
+ return result;
279
+ } catch (error) {
280
+ // P1: Catch unhandled errors — ensure manifest/tasks/agents are terminal so they don't stay "running" forever.
281
+ const message = error instanceof Error ? error.message : String(error);
282
+ const loaded = loadRunManifestById(input.manifest.cwd, input.manifest.runId);
283
+ const freshManifest = loaded?.manifest ?? manifest;
284
+ const freshTasks = refreshTaskGraphQueues(loaded?.tasks ?? input.tasks);
285
+ const failedAt = new Date().toISOString();
286
+ const tasks = freshTasks.map((task) =>
287
+ task.status === "running" || task.status === "queued" || task.status === "waiting"
288
+ ? { ...task, status: "failed" as const, finishedAt: failedAt, error: message }
289
+ : task,
290
+ );
291
+ manifest = freshManifest;
292
+ try {
293
+ await terminateLiveAgentsForRun(manifest.runId, "failed", appendEvent, manifest.eventsPath);
294
+ await saveRunTasksAsync(manifest, tasks);
295
+ const existingRuntimeByTask = new Map(readCrewAgents(manifest).map((agent) => [agent.taskId, agent.runtime]));
296
+ const globalRuntime = input.runtime?.kind ?? "child-process";
297
+ const runtimeForAgent = (agent: ReturnType<typeof recordsForMaterializedTasks>[number]): CrewRuntimeKind => {
298
+ const task = tasks.find((item) => item.id === agent.taskId);
299
+ return existingRuntimeByTask.get(agent.taskId) ?? resolveTaskRuntimeKind(globalRuntime, task?.role ?? agent.role, input.runtimeConfig?.isolationPolicy);
300
+ };
301
+ saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, globalRuntime).map((agent) => ({ ...agent, runtime: runtimeForAgent(agent) })));
302
+ manifest = updateRunStatus(manifest, "failed", `Unhandled error in team runner: ${message}`);
303
+ await saveRunManifestAsync(manifest);
304
+ } catch {
305
+ // Best-effort — state write may also fail
306
+ }
307
+ const result = { manifest, tasks };
308
+ rejectRunPromise(manifest.runId, error instanceof Error ? error : new Error(message));
309
+ cleanupUsage();
310
+ return result;
311
+ }
312
+ }
313
+
314
+ async function executeTeamRunCore(
315
+ input: ExecuteTeamRunInput,
316
+ manifest: TeamRunManifest,
317
+ workflow: WorkflowConfig,
318
+ ): Promise<{ manifest: TeamRunManifest; tasks: TeamTaskState[] }> {
319
+ // Execute before_run_start hook (non-blocking by default)
320
+ const beforeRunReport = await executeHook("before_run_start", { runId: manifest.runId, cwd: manifest.cwd });
321
+ appendHookEvent(manifest, beforeRunReport);
322
+ if (beforeRunReport.outcome === "block") {
323
+ manifest = updateRunStatus(manifest, "blocked", beforeRunReport.reason ?? "before_run_start hook blocked the run.");
324
+ return { manifest, tasks: input.tasks };
325
+ }
326
+ let tasks = refreshTaskGraphQueues(input.tasks);
327
+ let queueIndex = buildTaskGraphIndex(tasks);
328
+ const canInjectAdaptivePlan = workflow.name === "implementation";
329
+ let adaptivePlanInjected = false;
330
+ let adaptivePlanMissing = false;
331
+ const attemptAdaptivePlan = () => {
332
+ if (!canInjectAdaptivePlan || adaptivePlanInjected || adaptivePlanMissing) return { injected: false, missing: false };
333
+ const adaptivePlan = injectAdaptivePlanIfReady({ manifest, tasks, workflow, team: input.team });
334
+ adaptivePlanInjected = adaptivePlanInjected || adaptivePlan.injected;
335
+ adaptivePlanMissing = adaptivePlan.missingPlan;
336
+ workflow = adaptivePlan.workflow;
337
+ if (adaptivePlan.injected) tasks = adaptivePlan.tasks;
338
+ return { injected: adaptivePlan.injected, missing: adaptivePlan.missingPlan };
339
+ };
340
+ const initialAdaptive = attemptAdaptivePlan();
341
+ if (initialAdaptive.missing) {
342
+ tasks = markBlocked(tasks, "Adaptive planner did not produce a valid subagent plan.");
343
+ await saveRunTasksAsync(manifest, tasks);
344
+ manifest = updateRunStatus(manifest, "blocked", "Adaptive planner did not produce a valid subagent plan.");
345
+ return { manifest, tasks };
346
+ }
347
+ if (initialAdaptive.injected) {
348
+ manifest = requiresPlanApproval(workflow, input.runtimeConfig) ? ensurePlanApprovalRequested(manifest, tasks) : manifest;
349
+ queueIndex = buildTaskGraphIndex(tasks);
350
+ } else if (requiresPlanApproval(workflow, input.runtimeConfig) && hasPendingMutatingAdaptiveTask(tasks)) {
351
+ manifest = ensurePlanApprovalRequested(manifest, tasks);
352
+ }
353
+ if (manifest.planApproval?.status === "cancelled") {
354
+ tasks = cancelPlanTasks(tasks, "Plan approval was cancelled.");
355
+ await saveRunTasksAsync(manifest, tasks);
356
+ manifest = updateRunStatus(manifest, "cancelled", "Plan approval was cancelled.");
357
+ return { manifest, tasks };
358
+ }
359
+ manifest = writeProgress(manifest, tasks, "team-runner", input.executeWorkers, input.runtimeConfig);
360
+ await saveRunManifestAsync(manifest);
361
+ const runtimeKind = input.runtime?.kind ?? (input.executeWorkers ? "child-process" : "scaffold");
362
+ saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
363
+
364
+ // Build a workflow phase state machine from workflow steps for precondition tracking.
365
+ const workflowPhases: PhaseState[] = workflow.steps.map((step): PhaseState => ({
366
+ name: step.id,
367
+ status: "pending",
368
+ inputs: step.reads === false ? [] : Array.isArray(step.reads) ? step.reads : [],
369
+ outputs: step.output === false ? [] : step.output ? [step.output] : [],
370
+ }));
371
+ let wfMachine = createWorkflowStateMachine(workflowPhases);
372
+
373
+ while (tasks.some((task) => task.status === "queued")) {
374
+ if (input.signal?.aborted) {
375
+ const cancelReason = cancellationReasonFromSignal(input.signal);
376
+ const message = `${cancelReason.message} (${cancelReason.code})`;
377
+ const cancelledTaskIds: string[] = [];
378
+ tasks = tasks.map((task) => {
379
+ if (task.status !== "queued" && task.status !== "running" && task.status !== "waiting") return task;
380
+ cancelledTaskIds.push(task.id);
381
+ const base = { ...task, status: "cancelled" as const, finishedAt: new Date().toISOString(), error: message };
382
+ if (task.status === "running") {
383
+ return { ...base, terminalEvidence: [...(task.terminalEvidence ?? []), buildSyntheticTerminalEvidence("worker", cancelReason, task.startedAt)] };
384
+ }
385
+ return base;
386
+ });
387
+ await saveRunTasksAsync(manifest, tasks);
388
+ for (const taskId of cancelledTaskIds) appendEvent(manifest.eventsPath, { type: "task.cancelled", runId: manifest.runId, taskId, message, data: { reason: cancelReason.code } });
389
+ manifest = updateRunStatus(manifest, "cancelled", message, { data: { reason: cancelReason.code, cancelledTaskIds } });
390
+ return { manifest, tasks };
391
+ }
392
+
393
+ const failed = tasks.find((task) => task.status === "failed");
394
+ if (failed) {
395
+ tasks = markBlocked(tasks, `Blocked by failed task '${failed.id}'.`);
396
+ await saveRunTasksAsync(manifest, tasks);
397
+ saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
398
+ manifest = updateRunStatus(manifest, "failed", `Failed at task '${failed.id}'.`);
399
+ return { manifest, tasks };
400
+ }
401
+
402
+ const snapshot = taskGraphSnapshot(tasks, queueIndex);
403
+
404
+ // DAG-based execution plan: when tasks have explicit dependsOn, use the
405
+ // topological wave planner to determine ready tasks. Fall back to the
406
+ // existing task-graph-scheduler when no explicit deps exist (backward compat).
407
+ const completedIds = new Set(tasks.filter((t) => t.status === "completed").map((t) => t.id));
408
+ const dagReady = dagReadyTaskIds(tasks, completedIds);
409
+ const effectiveReady = dagReady ?? snapshot.ready;
410
+
411
+ // Workflow phase precondition check (non-blocking: log warnings only).
412
+ if (wfMachine.currentPhaseIndex < wfMachine.phases.length) {
413
+ const completedArtifacts = manifest.artifacts.filter((a) => a.kind === "result" || a.kind === "summary").map((a) => a.path);
414
+ const previousPhaseStatus = wfMachine.currentPhaseIndex > 0 ? (wfMachine.phases[wfMachine.currentPhaseIndex - 1]?.status ?? "pending") : "completed";
415
+ const wfContext: PhaseGuardContext = {
416
+ completedArtifacts,
417
+ previousPhaseStatus,
418
+ taskResults: tasks.filter((t) => t.status === "completed").map((t) => ({ taskId: t.id, status: t.status, outputPath: t.resultArtifact?.path })),
419
+ };
420
+ const preconditions = validatePhasePreconditions(wfMachine, wfContext);
421
+ if (!preconditions.ready) {
422
+ appendEvent(manifest.eventsPath, { type: "workflow.preconditions", runId: manifest.runId, message: `Workflow phase '${wfMachine.phases[wfMachine.currentPhaseIndex]?.name}' is missing inputs: ${preconditions.blocking.join(", ")}`, data: { phaseIndex: wfMachine.currentPhaseIndex, phaseName: wfMachine.phases[wfMachine.currentPhaseIndex]?.name, blocking: preconditions.blocking } });
423
+ } else {
424
+ // Advance the machine past completed phases.
425
+ while (wfMachine.currentPhaseIndex < wfMachine.phases.length && wfMachine.phases[wfMachine.currentPhaseIndex]?.status === "completed") {
426
+ wfMachine = { ...wfMachine, currentPhaseIndex: wfMachine.currentPhaseIndex + 1 };
427
+ }
428
+ }
429
+ }
430
+
431
+ const readyRoles = effectiveReady.map((taskId) => tasks.find((task) => task.id === taskId)?.role).filter((role): role is string => Boolean(role));
432
+ const concurrency = resolveBatchConcurrency({ workflowName: workflow.name, workflowMaxConcurrency: workflow.maxConcurrency, teamMaxConcurrency: input.team.maxConcurrency, limitMaxConcurrentWorkers: input.limits?.maxConcurrentWorkers, allowUnboundedConcurrency: input.limits?.allowUnboundedConcurrency, readyCount: effectiveReady.length, workspaceMode: manifest.workspaceMode, readyRoles });
433
+ if (concurrency.reason.includes(";unbounded:")) {
434
+ appendEvent(manifest.eventsPath, { type: "limits.unbounded", runId: manifest.runId, message: "Unbounded worker concurrency was explicitly enabled for this run.", data: { concurrencyReason: concurrency.reason, maxConcurrent: concurrency.maxConcurrent } });
435
+ }
436
+ const approvalPending = isPlanApprovalPending(manifest);
437
+ const readyIds = approvalPending ? effectiveReady : effectiveReady.slice(0, concurrency.selectedCount);
438
+ const candidateBatch = readyIds.map((id) => tasks.find((task) => task.id === id)).filter((task): task is TeamTaskState => Boolean(task));
439
+ const readyBatch = approvalPending ? candidateBatch.filter((task) => !isMutatingTask(task)).slice(0, concurrency.selectedCount) : candidateBatch;
440
+ if (readyBatch.length === 0) {
441
+ if (approvalPending && candidateBatch.some(isMutatingTask)) {
442
+ await saveRunTasksAsync(manifest, tasks);
443
+ saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
444
+ manifest = updateRunStatus(manifest, "blocked", "Plan approval required before mutating implementation tasks run.");
445
+ return { manifest, tasks };
446
+ }
447
+ tasks = markBlocked(tasks, "No ready queued task; dependency graph may be invalid.");
448
+ await saveRunTasksAsync(manifest, tasks);
449
+ saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
450
+ manifest = updateRunStatus(manifest, "blocked", "No ready queued task.");
451
+ return { manifest, tasks };
452
+ }
453
+
454
+ // 2.2 caller migration: batch progress is high-frequency informational.
455
+ appendEventFireAndForget(manifest.eventsPath, { type: "task.progress", runId: manifest.runId, message: `Starting ready batch with ${readyBatch.length} task(s).`, data: { taskIds: readyBatch.map((task) => task.id), readyCount: snapshot.ready.length, blockedCount: snapshot.blocked.length, runningCount: snapshot.running.length, doneCount: snapshot.done.length, selectedCount: readyBatch.length, maxConcurrent: concurrency.maxConcurrent, defaultConcurrency: concurrency.defaultConcurrency, concurrencyReason: approvalPending ? `${concurrency.reason};plan-approval-read-only` : concurrency.reason } });
456
+ // Execute before_task_start hooks for the batch
457
+ for (const task of readyBatch) {
458
+ const taskReport = await executeHook("before_task_start", { runId: manifest.runId, taskId: task.id, cwd: manifest.cwd });
459
+ appendHookEvent(manifest, taskReport);
460
+ if (taskReport.outcome === "block") {
461
+ tasks = tasks.map((t) => t.id === task.id ? { ...t, status: "skipped" as const, error: taskReport.reason ?? "before_task_start hook blocked execution." } : t);
462
+ manifest = updateRunStatus(manifest, manifest.status, `Task '${task.id}' blocked by hook.`);
463
+ }
464
+ }
465
+ const batchTasks = readyBatch.filter((task) => tasks.find((t) => t.id === task.id && t.status !== "skipped"));
466
+ if (batchTasks.length > 1) {
467
+ appendEvent(manifest.eventsPath, { type: "task.parallel_start", runId: manifest.runId, message: `Launching ${batchTasks.length} tasks in PARALLEL (concurrency=${concurrency.selectedCount}): ${batchTasks.map((t) => `${t.role}(${t.id})`).join(", ")}`, data: { taskIds: batchTasks.map((t) => t.id), roles: batchTasks.map((t) => t.role), concurrency: concurrency.selectedCount } });
468
+ }
469
+ const results = await mapConcurrent(
470
+ batchTasks,
471
+ concurrency.selectedCount,
472
+ async (task) => {
473
+ const step = findStep(workflow, task);
474
+ const agent = findAgent(input.agents, task);
475
+ const teamRole = input.team.roles.find((role) => role.name === task.role);
476
+ const perTaskRuntime = resolveTaskRuntimeKind(runtimeKind, task.role, input.runtimeConfig?.isolationPolicy);
477
+ const baseInput = { manifest, tasks, task, step, agent, signal: input.signal, executeWorkers: input.executeWorkers, runtimeKind: runtimeKind, taskRuntimeOverride: perTaskRuntime !== runtimeKind ? perTaskRuntime : undefined, runtimeConfig: input.runtimeConfig, parentContext: input.parentContext, parentModel: input.parentModel, modelRegistry: input.modelRegistry, modelOverride: input.modelOverride, teamRoleModel: teamRole?.model, teamRoleSkills: teamRole?.skills, skillOverride: input.skillOverride, limits: input.limits, onJsonEvent: input.onJsonEvent, workspaceId: input.workspaceId };
478
+ if (input.reliability?.autoRetry !== true) return withCorrelation(childCorrelation(manifest.runId, task.id), () => runTeamTask(baseInput));
479
+ let lastFailed: { manifest: TeamRunManifest; tasks: TeamTaskState[] } | undefined;
480
+ let lastAttemptId: string | undefined;
481
+ const attemptsSoFar: TaskAttemptState[] = [...(task.attempts ?? [])];
482
+ const policy = retryPolicyFromConfig(input.reliability);
483
+ try {
484
+ return await executeWithRetry(async (attempt, info) => {
485
+ const startedAt = new Date().toISOString();
486
+ const inFlightAttempts: TaskAttemptState[] = [...attemptsSoFar, { attemptId: info.attemptId, startedAt }];
487
+ input.metricRegistry?.counter("crew.task.retry_attempt_total", "Retry attempts by run and task").inc({ runId: manifest.runId, taskId: task.id });
488
+ const fresh = loadRunManifestById(manifest.cwd, manifest.runId);
489
+ const freshManifest = fresh?.manifest ?? manifest;
490
+ const freshTasks = fresh?.tasks ?? tasks;
491
+ const freshTask = freshTasks.find((item) => item.id === task.id) ?? task;
492
+ if (freshTask.status !== "queued" && freshTask.status !== "running") return { manifest: freshManifest, tasks: freshTasks };
493
+ const taskWithAttempt: TeamTaskState = { ...freshTask, attempts: inFlightAttempts };
494
+ const result = await withCorrelation(childCorrelation(freshManifest.runId, task.id), () => runTeamTask({ ...baseInput, manifest: freshManifest, tasks: freshTasks, task: taskWithAttempt }));
495
+ const failed = failedTaskFrom(result, task.id);
496
+ const endedAt = new Date().toISOString();
497
+ const finishedAttempt: TaskAttemptState = { attemptId: info.attemptId, startedAt, endedAt, ...(failed?.error ? { error: failed.error } : {}) };
498
+ attemptsSoFar.push(finishedAttempt);
499
+ const withAttempt = result.tasks.map((item) => item.id === task.id ? { ...item, attempts: [...attemptsSoFar] } : item);
500
+ const enriched = { manifest: result.manifest, tasks: withAttempt };
501
+ if (failed) {
502
+ lastFailed = enriched;
503
+ throw new Error(failed.error ?? `Task ${task.id} failed.`);
504
+ }
505
+ input.metricRegistry?.histogram("crew.task.retry_count", "Retries per task", [0, 1, 2, 3, 5, 10]).observe({ runId: manifest.runId, team: input.team.name }, Math.max(0, attempt - 1));
506
+ return enriched;
507
+ }, policy, {
508
+ signal: input.signal,
509
+ attemptId: (attempt) => `${manifest.runId}:${task.id}:attempt-${attempt}`,
510
+ onAttemptFailed: (attempt, error, delayMs, info) => {
511
+ lastAttemptId = info.attemptId;
512
+ appendEvent(manifest.eventsPath, { type: "crew.task.retry_attempt", runId: manifest.runId, taskId: task.id, message: error.message, data: { attempt, attemptId: info.attemptId, delayMs }, metadata: { attemptId: info.attemptId } });
513
+ input.metricRegistry?.histogram("crew.task.retry_delay_ms", "Retry backoff delay, milliseconds").observe({ runId: manifest.runId, taskId: task.id }, delayMs);
514
+ },
515
+ onRetryGivenUp: (attempts, error, info) => {
516
+ lastAttemptId = info.attemptId;
517
+ appendDeadletter(manifest, { runId: manifest.runId, taskId: task.id, reason: "max-retries", attempts, attemptId: info.attemptId, lastError: error.message, timestamp: new Date().toISOString() });
518
+ input.metricRegistry?.counter("crew.task.deadletter_total", "Deadletter triggers by reason").inc({ reason: "max-retries" });
519
+ input.metricRegistry?.histogram("crew.task.retry_count", "Retries per task", [0, 1, 2, 3, 5, 10]).observe({ runId: manifest.runId, team: input.team.name }, Math.max(0, attempts - 1));
520
+ },
521
+ });
522
+ } catch (retryError) {
523
+ if (retryError instanceof CrewCancellationError || input.signal?.aborted) {
524
+ const reason = retryError instanceof CrewCancellationError ? retryError.reason : cancellationReasonFromSignal(input.signal);
525
+ const fresh = loadRunManifestById(manifest.cwd, manifest.runId);
526
+ const freshManifest = fresh?.manifest ?? manifest;
527
+ const freshTasks = fresh?.tasks ?? tasks;
528
+ const cancelledTasks = freshTasks.map((item) => item.id === task.id && (item.status === "queued" || item.status === "running") ? { ...item, status: "cancelled" as const, finishedAt: new Date().toISOString(), error: `${reason.message} (${reason.code})` } : item);
529
+ appendEvent(freshManifest.eventsPath, { type: "task.cancelled", runId: freshManifest.runId, taskId: task.id, message: reason.message, data: { reason, phase: "retry" }, metadata: lastAttemptId ? { attemptId: lastAttemptId } : undefined });
530
+ return { manifest: updateRunStatus(freshManifest, "cancelled", reason.message), tasks: cancelledTasks };
531
+ }
532
+ if (lastFailed) return lastFailed;
533
+ const fresh = loadRunManifestById(manifest.cwd, manifest.runId);
534
+ const freshManifest = fresh?.manifest ?? manifest;
535
+ const freshTasks = fresh?.tasks ?? tasks;
536
+ const freshTask = freshTasks.find((item) => item.id === task.id) ?? task;
537
+ if (freshTask.status !== "queued" && freshTask.status !== "running") return { manifest: freshManifest, tasks: freshTasks };
538
+ return withCorrelation(childCorrelation(freshManifest.runId, task.id), () => runTeamTask({ ...baseInput, manifest: freshManifest, tasks: freshTasks, task: freshTask }));
539
+ }
540
+ },
541
+ );
542
+ if (results.length === 0) break;
543
+ manifest = { ...results.at(-1)!.manifest, artifacts: mergeArtifacts([manifest.artifacts, ...results.map((item) => item.manifest.artifacts)].flat()) };
544
+ tasks = __test__mergeTaskUpdates(tasks, results);
545
+
546
+ // Advance workflow phases whose tasks are all in terminal state
547
+ const terminalStatuses = new Set(["completed", "failed", "skipped", "cancelled"]);
548
+ const phaseTaskMap = new Map<string, string[]>();
549
+ for (const task of tasks) {
550
+ if (!task.stepId) continue;
551
+ const existing = phaseTaskMap.get(task.stepId) ?? [];
552
+ existing.push(task.id);
553
+ phaseTaskMap.set(task.stepId, existing);
554
+ }
555
+ for (let pi = wfMachine.currentPhaseIndex; pi < wfMachine.phases.length; pi++) {
556
+ const phase = wfMachine.phases[pi]!;
557
+ const phaseTaskIds = phaseTaskMap.get(phase.name) ?? [];
558
+ if (phaseTaskIds.length === 0) continue;
559
+ const allTerminal = phaseTaskIds.every((taskId) => {
560
+ const task = tasks.find((t) => t.id === taskId);
561
+ return task ? terminalStatuses.has(task.status) : false;
562
+ });
563
+ if (!allTerminal) break;
564
+ if (phase.status !== "completed" && phase.status !== "failed" && phase.status !== "skipped") {
565
+ const completedArtifacts = manifest.artifacts.filter((a) => a.kind === "result" || a.kind === "summary").map((a) => a.path);
566
+ const previousPhaseStatus = pi > 0 ? (wfMachine.phases[pi - 1]?.status ?? "pending") : "completed";
567
+ const wfContext: PhaseGuardContext = {
568
+ completedArtifacts,
569
+ previousPhaseStatus,
570
+ taskResults: tasks.filter((t) => t.status === "completed").map((t) => ({ taskId: t.id, status: t.status, outputPath: t.resultArtifact?.path })),
571
+ };
572
+ // Determine phase transition status based on individual task outcomes
573
+ const phaseTasks = phaseTaskIds.map((taskId) => tasks.find((t) => t.id === taskId)).filter((t): t is NonNullable<typeof t> => t !== undefined);
574
+ const hasFailedOrCancelled = phaseTasks.some((t) => t.status === "failed" || t.status === "cancelled");
575
+ const phaseStatus = hasFailedOrCancelled ? "failed" : "completed";
576
+ const transition = transitionPhase(wfMachine, pi, phaseStatus, wfContext);
577
+ wfMachine = transition.machine;
578
+ if (transition.guardResult && !transition.guardResult.allowed) {
579
+ appendEvent(manifest.eventsPath, { type: "workflow.phase_guard_blocked", runId: manifest.runId, message: `Workflow phase '${phase.name}' guard blocked: ${transition.guardResult.reason ?? "unknown"}`, data: { phaseIndex: pi, phaseName: phase.name, reason: transition.guardResult.reason } });
580
+ break;
581
+ }
582
+ appendEvent(manifest.eventsPath, { type: phaseStatus === "failed" ? "workflow.phase_failed" : "workflow.phase_completed", runId: manifest.runId, message: `Workflow phase '${phase.name}' ${phaseStatus}.`, data: { phaseIndex: pi, phaseStatus } });
583
+ }
584
+ wfMachine = { ...wfMachine, currentPhaseIndex: pi + 1 };
585
+ }
586
+
587
+ const cancelledResult = results.find((item) => item.manifest.status === "cancelled");
588
+ if (cancelledResult || input.signal?.aborted) {
589
+ const reason = input.signal?.aborted ? cancellationReasonFromSignal(input.signal) : undefined;
590
+ const message = reason?.message ?? cancelledResult?.manifest.summary ?? "Run cancelled during task execution.";
591
+ manifest = { ...manifest, status: "running" };
592
+ manifest = updateRunStatus(manifest, "cancelled", message);
593
+ await saveRunTasksAsync(manifest, tasks);
594
+ saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
595
+ await saveRunManifestAsync(manifest);
596
+ appendEvent(manifest.eventsPath, { type: "run.cancelled", runId: manifest.runId, message, data: { reason, phase: "task-batch", cancelledResultRunId: cancelledResult?.manifest.runId } });
597
+ return { manifest, tasks };
598
+ }
599
+ queueIndex = buildTaskGraphIndex(tasks);
600
+ const injectedAfterBatch = attemptAdaptivePlan();
601
+ if (injectedAfterBatch.missing) {
602
+ tasks = markBlocked(tasks, "Adaptive planner did not produce a valid subagent plan.");
603
+ await saveRunTasksAsync(manifest, tasks);
604
+ saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
605
+ manifest = updateRunStatus(manifest, "blocked", "Adaptive planner did not produce a valid subagent plan.");
606
+ return { manifest, tasks };
607
+ }
608
+ if (injectedAfterBatch.injected) {
609
+ manifest = requiresPlanApproval(workflow, input.runtimeConfig) ? ensurePlanApprovalRequested(manifest, tasks) : manifest;
610
+ queueIndex = buildTaskGraphIndex(tasks);
611
+ } else if (requiresPlanApproval(workflow, input.runtimeConfig) && hasPendingMutatingAdaptiveTask(tasks)) {
612
+ manifest = ensurePlanApprovalRequested(manifest, tasks);
613
+ }
614
+ if (manifest.planApproval?.status === "cancelled") {
615
+ tasks = cancelPlanTasks(tasks, "Plan approval was cancelled.");
616
+ await saveRunTasksAsync(manifest, tasks);
617
+ saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
618
+ manifest = updateRunStatus(manifest, "cancelled", "Plan approval was cancelled.");
619
+ return { manifest, tasks };
620
+ }
621
+ await saveRunTasksAsync(manifest, tasks);
622
+ saveCrewAgents(manifest, recordsForMaterializedTasks(manifest, tasks, runtimeKind));
623
+ const completedBatch = batchTasks.map((task) => tasks.find((item) => item.id === task.id) ?? task);
624
+ const batchArtifact = writeArtifact(manifest.artifactsRoot, {
625
+ kind: "summary",
626
+ relativePath: `batches/${batchTasks.map((task) => task.id).join("+")}.md`,
627
+ producer: "team-runner",
628
+ content: aggregateTaskOutputs(completedBatch, manifest),
629
+ });
630
+ const groupDelivery = deliverGroupJoin({ manifest, mode: resolveGroupJoinMode(input.runtimeConfig), batch: batchTasks, allTasks: tasks });
631
+ manifest = { ...manifest, artifacts: mergeArtifacts([...manifest.artifacts, batchArtifact, ...(groupDelivery?.artifact ? [groupDelivery.artifact] : [])]) };
632
+ manifest = writeProgress(manifest, tasks, "team-runner", input.executeWorkers, input.runtimeConfig);
633
+ await saveRunManifestAsync(manifest);
634
+ }
635
+
636
+ const failed = tasks.find((task) => task.status === "failed");
637
+ const waiting = tasks.find((task) => task.status === "waiting");
638
+ const running = tasks.find((task) => task.status === "running");
639
+ manifest = applyPolicy(manifest, tasks, input.limits);
640
+ const effectiveness = evaluateRunEffectiveness({ manifest, tasks, executeWorkers: input.executeWorkers, runtimeConfig: input.runtimeConfig });
641
+ const effectivenessDecision = effectivenessPolicyDecision(effectiveness);
642
+ if (effectivenessDecision) {
643
+ manifest = { ...manifest, policyDecisions: [...(manifest.policyDecisions ?? []), effectivenessDecision], updatedAt: new Date().toISOString() };
644
+ appendEvent(manifest.eventsPath, { type: "run.effectiveness", runId: manifest.runId, message: effectivenessDecision.message, data: { effectiveness, policyDecision: effectivenessDecision } });
645
+ }
646
+ const blockingDecision = manifest.policyDecisions?.find((item) => item.action === "block" || item.action === "escalate");
647
+ if (failed) {
648
+ manifest = updateRunStatus(manifest, "failed", `Failed at task '${failed.id}'.`);
649
+ } else if (waiting) {
650
+ manifest = updateRunStatus(manifest, "blocked", `Waiting for response to task '${waiting.id}'.`);
651
+ } else if (running) {
652
+ manifest = updateRunStatus(manifest, "blocked", `Task '${running.id}' is still running.`);
653
+ } else if (effectiveness.severity === "failed") {
654
+ manifest = updateRunStatus(manifest, "failed", effectivenessDecision?.message ?? "Run effectiveness guard failed.");
655
+ } else if (effectiveness.severity === "blocked") {
656
+ manifest = updateRunStatus(manifest, "blocked", effectivenessDecision?.message ?? "Run effectiveness guard blocked completion.");
657
+ } else if (blockingDecision) {
658
+ manifest = updateRunStatus(manifest, "blocked", blockingDecision.message);
659
+ } else {
660
+ manifest = updateRunStatus(manifest, "completed", input.executeWorkers ? "Team workflow completed." : "Team workflow scaffold completed without launching child workers.");
661
+ }
662
+ manifest = writeProgress(manifest, tasks, "team-runner", input.executeWorkers, input.runtimeConfig);
663
+ await saveRunManifestAsync(manifest);
664
+ const usage = aggregateUsage(tasks);
665
+ const summaryArtifact = writeArtifact(manifest.artifactsRoot, {
666
+ kind: "summary",
667
+ relativePath: "summary.md",
668
+ producer: "team-runner",
669
+ content: [
670
+ `# pi-crew run ${manifest.runId}`,
671
+ "",
672
+ `Status: ${manifest.status}`,
673
+ `Team: ${manifest.team}`,
674
+ `Workflow: ${manifest.workflow ?? "(none)"}`,
675
+ `Goal: ${manifest.goal}`,
676
+ `Usage: ${formatUsage(usage)}`,
677
+ "",
678
+ "## Tasks",
679
+ ...tasks.map(formatTaskProgress),
680
+ "",
681
+ "## Effectiveness",
682
+ ...runEffectivenessLines(manifest, tasks, input.executeWorkers, input.runtimeConfig),
683
+ "",
684
+ "## Policy decisions",
685
+ ...(manifest.policyDecisions?.length ? summarizePolicyDecisions(manifest.policyDecisions) : ["- (none)"]),
686
+ "",
687
+ ].join("\n"),
688
+ });
689
+ manifest = { ...manifest, updatedAt: new Date().toISOString(), artifacts: [...manifest.artifacts, summaryArtifact] };
690
+ await saveRunManifestAsync(manifest);
691
+ await saveRunTasksAsync(manifest, tasks);
692
+ return { manifest, tasks };
693
+ }