@jaimevalasek/aioson 1.6.0 → 1.7.0

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 (252) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/README.md +729 -232
  3. package/docs/design-previews/pt.squarespace.com-homepage.html +889 -0
  4. package/docs/integrations/sdlc-genius-boundary.md +76 -0
  5. package/docs/integrations/sdlc-genius-eval-matrix.md +75 -0
  6. package/docs/integrations/sdlc-genius-install-checklist.md +93 -0
  7. package/docs/integrations/sdlc-genius-review-samples.md +86 -0
  8. package/docs/pt/README.md +3 -0
  9. package/docs/pt/agentes.md +1 -0
  10. package/docs/pt/comandos-cli.md +888 -2
  11. package/docs/pt/design-hybrid-forge.md +255 -6
  12. package/docs/pt/devlog-pipeline.md +270 -0
  13. package/docs/pt/fluxo-artefatos.md +178 -0
  14. package/docs/pt/hooks-session-guard.md +454 -0
  15. package/docs/pt/monitor-de-contexto.md +59 -5
  16. package/docs/pt/sdd-automation-scripts.md +557 -0
  17. package/docs/pt/site-forge.md +309 -0
  18. package/docs/pt/spec-learnings-pipeline.md +265 -0
  19. package/package.json +1 -1
  20. package/src/a2a/client.js +165 -0
  21. package/src/a2a/server.js +223 -0
  22. package/src/cli.js +235 -1
  23. package/src/commands/agent-audit.js +397 -0
  24. package/src/commands/agent-export-skill.js +229 -0
  25. package/src/commands/artifact-validate.js +189 -0
  26. package/src/commands/brief-gen.js +405 -0
  27. package/src/commands/brief-validate.js +65 -0
  28. package/src/commands/classify.js +256 -0
  29. package/src/commands/context-compact.js +49 -0
  30. package/src/commands/context-health.js +175 -0
  31. package/src/commands/context-monitor.js +71 -0
  32. package/src/commands/context-trim.js +177 -0
  33. package/src/commands/detect-test-runner.js +55 -0
  34. package/src/commands/devlog-export-brains.js +27 -0
  35. package/src/commands/devlog-process.js +292 -0
  36. package/src/commands/devlog-watch.js +131 -0
  37. package/src/commands/feature-close.js +165 -0
  38. package/src/commands/gate-check.js +228 -0
  39. package/src/commands/hooks-emit.js +253 -0
  40. package/src/commands/hooks-install.js +347 -0
  41. package/src/commands/learning-auto-promote.js +195 -0
  42. package/src/commands/learning-evolve.js +18 -9
  43. package/src/commands/learning-export.js +103 -0
  44. package/src/commands/learning-rollback.js +164 -0
  45. package/src/commands/live.js +25 -1
  46. package/src/commands/pattern-detect.js +33 -0
  47. package/src/commands/preflight-context.js +30 -0
  48. package/src/commands/preflight.js +208 -0
  49. package/src/commands/pulse-update.js +130 -0
  50. package/src/commands/runner-daemon.js +274 -0
  51. package/src/commands/runner-plan.js +70 -0
  52. package/src/commands/runner-queue-from-plan.js +166 -0
  53. package/src/commands/runner-queue.js +189 -0
  54. package/src/commands/runner-run.js +129 -0
  55. package/src/commands/runtime.js +47 -1
  56. package/src/commands/self-implement-loop.js +256 -0
  57. package/src/commands/session-guard.js +218 -0
  58. package/src/commands/sizing.js +165 -0
  59. package/src/commands/skill.js +65 -0
  60. package/src/commands/spec-checkpoint.js +177 -0
  61. package/src/commands/spec-status.js +79 -0
  62. package/src/commands/spec-sync.js +190 -0
  63. package/src/commands/spec-tasks.js +288 -0
  64. package/src/commands/squad-autorun.js +1220 -0
  65. package/src/commands/squad-bus.js +217 -0
  66. package/src/commands/squad-card.js +149 -0
  67. package/src/commands/squad-daemon.js +134 -0
  68. package/src/commands/squad-dependency-graph.js +164 -0
  69. package/src/commands/squad-review.js +106 -0
  70. package/src/commands/squad-scaffold.js +55 -0
  71. package/src/commands/squad-tool-register.js +157 -0
  72. package/src/commands/state-save.js +122 -0
  73. package/src/commands/update.js +2 -0
  74. package/src/commands/verify-gate.js +572 -0
  75. package/src/commands/workflow-execute.js +241 -0
  76. package/src/constants.js +9 -0
  77. package/src/install-profile.js +2 -2
  78. package/src/install-wizard.js +3 -2
  79. package/src/installer.js +6 -0
  80. package/src/lib/health-check.js +158 -0
  81. package/src/lib/hook-protocol.js +76 -0
  82. package/src/mcp/apps/squad-dashboard/app.js +163 -0
  83. package/src/mcp/apps/squad-dashboard/index.html +261 -0
  84. package/src/mcp/apps/squad-dashboard/mcp-manifest.json +23 -0
  85. package/src/mcp/resources/squad-state.js +130 -0
  86. package/src/preflight-engine.js +443 -0
  87. package/src/runner/cascade.js +97 -0
  88. package/src/runner/cli-launcher.js +109 -0
  89. package/src/runner/plan-importer.js +63 -0
  90. package/src/runner/queue-store.js +159 -0
  91. package/src/runtime-store.js +61 -3
  92. package/src/squad/agent-teams-adapter.js +264 -0
  93. package/src/squad/brief-validator.js +350 -0
  94. package/src/squad/bus-bridge.js +140 -0
  95. package/src/squad/context-compactor.js +265 -0
  96. package/src/squad/cross-ai-synthesizer.js +250 -0
  97. package/src/squad/hooks-generator.js +196 -0
  98. package/src/squad/inter-squad-events.js +175 -0
  99. package/src/squad/intra-bus.js +345 -0
  100. package/src/squad/learning-extractor.js +213 -0
  101. package/src/squad/pattern-detector.js +365 -0
  102. package/src/squad/preflight-context.js +296 -0
  103. package/src/squad/recovery-context.js +242 -71
  104. package/src/squad/reflection.js +365 -0
  105. package/src/squad/squad-scaffold.js +177 -0
  106. package/src/squad/state-manager.js +310 -0
  107. package/src/squad/task-decomposer.js +652 -0
  108. package/src/squad/verify-gate.js +303 -0
  109. package/src/updater.js +4 -5
  110. package/src/worker-runner.js +186 -1
  111. package/template/.aioson/agents/analyst.md +62 -1
  112. package/template/.aioson/agents/architect.md +61 -1
  113. package/template/.aioson/agents/design-hybrid-forge.md +14 -0
  114. package/template/.aioson/agents/dev.md +242 -24
  115. package/template/.aioson/agents/deyvin.md +66 -8
  116. package/template/.aioson/agents/discovery-design-doc.md +44 -0
  117. package/template/.aioson/agents/genome.md +14 -0
  118. package/template/.aioson/agents/neo.md +78 -1
  119. package/template/.aioson/agents/orache.md +50 -4
  120. package/template/.aioson/agents/orchestrator.md +197 -1
  121. package/template/.aioson/agents/pm.md +35 -0
  122. package/template/.aioson/agents/product.md +50 -5
  123. package/template/.aioson/agents/profiler-enricher.md +14 -0
  124. package/template/.aioson/agents/profiler-forge.md +14 -0
  125. package/template/.aioson/agents/profiler-researcher.md +14 -0
  126. package/template/.aioson/agents/qa.md +172 -21
  127. package/template/.aioson/agents/setup.md +79 -9
  128. package/template/.aioson/agents/sheldon.md +131 -6
  129. package/template/.aioson/agents/site-forge.md +1753 -0
  130. package/template/.aioson/agents/squad.md +162 -0
  131. package/template/.aioson/agents/tester.md +53 -0
  132. package/template/.aioson/agents/ux-ui.md +34 -1
  133. package/template/.aioson/brains/README.md +128 -0
  134. package/template/.aioson/brains/_index.json +16 -0
  135. package/template/.aioson/brains/scripts/query.js +103 -0
  136. package/template/.aioson/brains/site-forge/visual-patterns.brain.json +205 -0
  137. package/template/.aioson/config.md +143 -13
  138. package/template/.aioson/constitution.md +33 -0
  139. package/template/.aioson/context/project-pulse.md +34 -0
  140. package/template/.aioson/docs/LAYERS.md +79 -0
  141. package/template/.aioson/docs/README.md +76 -0
  142. package/template/.aioson/docs/example-external-api-context.md +72 -0
  143. package/template/.aioson/locales/en/agents/architect.md +17 -0
  144. package/template/.aioson/locales/en/agents/dev.md +79 -13
  145. package/template/.aioson/locales/en/agents/orache.md +6 -0
  146. package/template/.aioson/locales/en/agents/orchestrator.md +24 -0
  147. package/template/.aioson/locales/en/agents/product.md +50 -0
  148. package/template/.aioson/locales/en/agents/sheldon.md +115 -0
  149. package/template/.aioson/locales/en/agents/squad.md +14 -0
  150. package/template/.aioson/locales/en/agents/tester.md +6 -0
  151. package/template/.aioson/locales/es/agents/analyst.md +2 -0
  152. package/template/.aioson/locales/es/agents/architect.md +19 -0
  153. package/template/.aioson/locales/es/agents/dev.md +64 -4
  154. package/template/.aioson/locales/es/agents/deyvin.md +2 -0
  155. package/template/.aioson/locales/es/agents/discovery-design-doc.md +2 -0
  156. package/template/.aioson/locales/es/agents/genome.md +2 -0
  157. package/template/.aioson/locales/es/agents/neo.md +2 -0
  158. package/template/.aioson/locales/es/agents/orache.md +2 -0
  159. package/template/.aioson/locales/es/agents/orchestrator.md +26 -0
  160. package/template/.aioson/locales/es/agents/pair.md +2 -0
  161. package/template/.aioson/locales/es/agents/pm.md +2 -0
  162. package/template/.aioson/locales/es/agents/product.md +52 -0
  163. package/template/.aioson/locales/es/agents/profiler-enricher.md +2 -0
  164. package/template/.aioson/locales/es/agents/profiler-forge.md +2 -0
  165. package/template/.aioson/locales/es/agents/profiler-researcher.md +2 -0
  166. package/template/.aioson/locales/es/agents/qa.md +2 -0
  167. package/template/.aioson/locales/es/agents/setup.md +2 -0
  168. package/template/.aioson/locales/es/agents/sheldon.md +117 -0
  169. package/template/.aioson/locales/es/agents/squad.md +16 -0
  170. package/template/.aioson/locales/es/agents/tester.md +9 -0
  171. package/template/.aioson/locales/es/agents/ux-ui.md +2 -0
  172. package/template/.aioson/locales/fr/agents/analyst.md +2 -0
  173. package/template/.aioson/locales/fr/agents/architect.md +19 -0
  174. package/template/.aioson/locales/fr/agents/dev.md +64 -4
  175. package/template/.aioson/locales/fr/agents/deyvin.md +2 -0
  176. package/template/.aioson/locales/fr/agents/discovery-design-doc.md +2 -0
  177. package/template/.aioson/locales/fr/agents/genome.md +2 -0
  178. package/template/.aioson/locales/fr/agents/neo.md +2 -0
  179. package/template/.aioson/locales/fr/agents/orache.md +2 -0
  180. package/template/.aioson/locales/fr/agents/orchestrator.md +26 -0
  181. package/template/.aioson/locales/fr/agents/pair.md +2 -0
  182. package/template/.aioson/locales/fr/agents/pm.md +2 -0
  183. package/template/.aioson/locales/fr/agents/product.md +52 -0
  184. package/template/.aioson/locales/fr/agents/profiler-enricher.md +2 -0
  185. package/template/.aioson/locales/fr/agents/profiler-forge.md +2 -0
  186. package/template/.aioson/locales/fr/agents/profiler-researcher.md +2 -0
  187. package/template/.aioson/locales/fr/agents/qa.md +2 -0
  188. package/template/.aioson/locales/fr/agents/setup.md +2 -0
  189. package/template/.aioson/locales/fr/agents/sheldon.md +117 -0
  190. package/template/.aioson/locales/fr/agents/squad.md +16 -0
  191. package/template/.aioson/locales/fr/agents/tester.md +9 -0
  192. package/template/.aioson/locales/fr/agents/ux-ui.md +2 -0
  193. package/template/.aioson/locales/pt-BR/agents/analyst.md +64 -3
  194. package/template/.aioson/locales/pt-BR/agents/architect.md +42 -0
  195. package/template/.aioson/locales/pt-BR/agents/dev.md +147 -14
  196. package/template/.aioson/locales/pt-BR/agents/deyvin.md +47 -0
  197. package/template/.aioson/locales/pt-BR/agents/neo.md +62 -1
  198. package/template/.aioson/locales/pt-BR/agents/orchestrator.md +158 -2
  199. package/template/.aioson/locales/pt-BR/agents/pm.md +95 -1
  200. package/template/.aioson/locales/pt-BR/agents/product.md +145 -18
  201. package/template/.aioson/locales/pt-BR/agents/qa.md +16 -0
  202. package/template/.aioson/locales/pt-BR/agents/setup.md +101 -18
  203. package/template/.aioson/locales/pt-BR/agents/sheldon.md +132 -1
  204. package/template/.aioson/locales/pt-BR/agents/squad.md +14 -0
  205. package/template/.aioson/locales/pt-BR/agents/tester.md +449 -0
  206. package/template/.aioson/rules/README.md +69 -0
  207. package/template/.aioson/rules/data-format-convention.md +136 -0
  208. package/template/.aioson/rules/example-monetary-values.md +30 -0
  209. package/template/.aioson/schemas/squad-manifest.schema.json +124 -3
  210. package/template/.aioson/skills/design/pt.squarespace.com/.skill-meta.json +31 -0
  211. package/template/.aioson/skills/design/pt.squarespace.com/SKILL.md +66 -0
  212. package/template/.aioson/skills/design/pt.squarespace.com/references/components.md +368 -0
  213. package/template/.aioson/skills/design/pt.squarespace.com/references/design-tokens.md +150 -0
  214. package/template/.aioson/skills/design/pt.squarespace.com/references/motion.md +270 -0
  215. package/template/.aioson/skills/design/pt.squarespace.com/references/patterns.md +189 -0
  216. package/template/.aioson/skills/design/pt.squarespace.com/references/websites.md +165 -0
  217. package/template/.aioson/skills/process/aioson-spec-driven/SKILL.md +1 -0
  218. package/template/.aioson/skills/process/aioson-spec-driven/references/analyst.md +30 -0
  219. package/template/.aioson/skills/process/aioson-spec-driven/references/architect.md +23 -0
  220. package/template/.aioson/skills/process/aioson-spec-driven/references/dev.md +47 -0
  221. package/template/.aioson/skills/process/aioson-spec-driven/references/deyvin.md +27 -0
  222. package/template/.aioson/skills/process/aioson-spec-driven/references/maintenance-and-state.md +35 -0
  223. package/template/.aioson/skills/process/aioson-spec-driven/references/product.md +25 -0
  224. package/template/.aioson/skills/process/aioson-spec-driven/references/qa.md +30 -0
  225. package/template/.aioson/skills/process/aioson-spec-driven/references/sheldon.md +25 -0
  226. package/template/.aioson/skills/process/design-hybrid-forge/SKILL.md +4 -1
  227. package/template/.aioson/skills/process/design-hybrid-forge/references/output-contract.md +15 -0
  228. package/template/.aioson/skills/process/design-hybrid-forge/references/pair-compatibility.md +32 -0
  229. package/template/.aioson/skills/process/design-hybrid-forge/references/quality-gates.md +20 -0
  230. package/template/.aioson/skills/process/simplify/SKILL.md +173 -0
  231. package/template/.aioson/skills/static/context-budget-guide.md +46 -0
  232. package/template/.aioson/skills/static/harness-sensors.md +74 -0
  233. package/template/.aioson/skills/static/multi-agent-patterns.md +43 -0
  234. package/template/.aioson/skills/static/react-motion-patterns.md +22 -0
  235. package/template/.aioson/skills/static/static-html-patterns/checklists.md +43 -0
  236. package/template/.aioson/skills/static/static-html-patterns/css-tokens.md +609 -0
  237. package/template/.aioson/skills/static/static-html-patterns/motion.md +193 -0
  238. package/template/.aioson/skills/static/static-html-patterns/premium.md +711 -0
  239. package/template/.aioson/skills/static/static-html-patterns/structure.md +209 -0
  240. package/template/.aioson/skills/static/static-html-patterns/utilities.md +190 -0
  241. package/template/.aioson/skills/static/static-html-patterns.md +58 -1913
  242. package/template/.aioson/skills/static/threejs-patterns.md +929 -0
  243. package/template/.aioson/skills/static/web-research-cache.md +112 -0
  244. package/template/.aioson/tasks/implementation-plan.md +21 -1
  245. package/template/.claude/commands/aioson/agent/design-hybrid-forge.md +5 -0
  246. package/template/.claude/commands/aioson/agent/orache.md +5 -0
  247. package/template/.claude/commands/aioson/agent/sheldon.md +5 -0
  248. package/template/.claude/commands/aioson/agent/site-forge.md +5 -0
  249. package/template/AGENTS.md +55 -3
  250. package/template/CLAUDE.md +30 -0
  251. package/template/OPENCODE.md +4 -0
  252. package/template/researchs/.gitkeep +0 -0
@@ -0,0 +1,1220 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * aioson squad:autorun — Autonomous squad execution
5
+ *
6
+ * Given a high-level goal, this command:
7
+ * 1. Decomposes the goal into a task plan (heuristic or structured)
8
+ * 2. Activates the intra-squad bus for real-time communication
9
+ * 3. Runs each task through the worker-runner with optional reflection
10
+ * 4. Coordinator monitors the bus for blocks or feedback
11
+ * 5. Reports the final session summary
12
+ *
13
+ * Phase 1 additions:
14
+ * - Gap Closure Loop: failed tasks are retried up to 3x with failure context
15
+ * - Budget Gating: halts before a task if session token budget is exceeded
16
+ * - Heartbeat Protocol: posts bus heartbeat every 30s during long tasks
17
+ * - Anti-Analysis-Loop Guard: warns executor if N+ status without result
18
+ * - Squad STATE.md: cross-session memory updated at start/end
19
+ *
20
+ * Usage:
21
+ * aioson squad:autorun . --squad=content-team --goal="Create 3 podcast episodes"
22
+ * aioson squad:autorun . --squad=content-team --goal="..." --reflect --bus --mode=structured
23
+ * aioson squad:autorun . --squad=content-team --plan=SESSION_ID (resume from saved plan)
24
+ * aioson squad:autorun . --squad=content-team --plan=SESSION_ID --dry-run
25
+ *
26
+ * Flags:
27
+ * --goal High-level objective (required unless --plan is given)
28
+ * --reflect Run reflection after each task (default: false)
29
+ * --bus Enable intra-bus for inter-executor communication (default: true)
30
+ * --mode Decomposition mode: heuristic (default) | structured
31
+ * --plan Resume from existing session plan (session ID)
32
+ * --dry-run Show plan without executing
33
+ * --sequential Force sequential execution even for independent tasks (default: false)
34
+ * --timeout Per-task timeout in seconds (default: 120)
35
+ * --max-retries Gap closure max retry attempts (default: 3)
36
+ * --no-gap-closure Disable automatic retry on task failure
37
+ */
38
+
39
+ const fs = require('node:fs/promises');
40
+ const path = require('node:path');
41
+ const { randomUUID } = require('node:crypto');
42
+ const {
43
+ decompose,
44
+ getReadyTasks,
45
+ isPlanComplete,
46
+ updateTaskStatus,
47
+ loadPlan,
48
+ formatPlan
49
+ } = require('../squad/task-decomposer');
50
+ const bus = require('../squad/intra-bus');
51
+ const { reflect, formatReport } = require('../squad/reflection');
52
+ const { runWorker, listWorkers } = require('../worker-runner');
53
+ const stateManager = require('../squad/state-manager');
54
+ const { getUnresolvedBlocks } = require('../squad/intra-bus');
55
+ const interSquadEvents = require('../squad/inter-squad-events');
56
+ const { runHook, HOOK_DENY } = require('../lib/hook-protocol');
57
+ const { extractLearnings, persistAgentMemory } = require('../squad/learning-extractor');
58
+ const { validateBrief, autoFixBrief } = require('../squad/brief-validator');
59
+ const { resolveEngine, translateToTeamConfig, writeTeamConfig } = require('../squad/agent-teams-adapter');
60
+
61
+ const STATUS_ICON = {
62
+ pending: '○',
63
+ in_progress: '●',
64
+ completed: '✓',
65
+ failed: '✗',
66
+ escalated: '⚠',
67
+ skipped: '–'
68
+ };
69
+
70
+ function icon(status) {
71
+ return STATUS_ICON[status] || '?';
72
+ }
73
+
74
+ function nowIso() { return new Date().toISOString(); }
75
+
76
+ function sleep(ms) { return new Promise((r) => setTimeout(r, ms)); }
77
+
78
+ // ─── Budget helpers ───────────────────────────────────────────────────────────
79
+
80
+ /**
81
+ * Load token budget from squad manifest (optional field).
82
+ * Returns Infinity if not configured.
83
+ */
84
+ async function loadBudget(projectDir, squadSlug) {
85
+ const manifestPath = path.join(
86
+ projectDir, '.aioson', 'squads', squadSlug, 'squad.manifest.json'
87
+ );
88
+ try {
89
+ const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8'));
90
+ const budget = manifest.budget || {};
91
+ return {
92
+ maxTokensPerSession: budget.max_tokens_per_session || Infinity,
93
+ maxTokensPerTask: budget.max_tokens_per_task || Infinity,
94
+ actionOnExceed: budget.action_on_exceed || 'pause'
95
+ };
96
+ } catch {
97
+ return { maxTokensPerSession: Infinity, maxTokensPerTask: Infinity, actionOnExceed: 'pause' };
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Estimate tokens a task will consume (heuristic: chars/4 + overhead).
103
+ */
104
+ function estimateTaskTokens(task) {
105
+ const descLen = (task.description || '').length;
106
+ const criteriaLen = (task.acceptance_criteria || []).join(' ').length;
107
+ return Math.ceil((descLen + criteriaLen) / 4) + 500; // 500 base overhead
108
+ }
109
+
110
+ // ─── Heartbeat wrapper ────────────────────────────────────────────────────────
111
+
112
+ /**
113
+ * Run a task with heartbeat pulses on the bus every 30s.
114
+ * Clears the interval on task completion/failure regardless.
115
+ */
116
+ async function runTaskWithHeartbeat(projectDir, squadSlug, task, sessionId, options, runFn) {
117
+ const { enableBus } = options;
118
+ const startMs = Date.now();
119
+
120
+ let hbInterval = null;
121
+ if (enableBus) {
122
+ hbInterval = setInterval(async () => {
123
+ const elapsed = Math.round((Date.now() - startMs) / 1000);
124
+ await bus.post(projectDir, squadSlug, sessionId, {
125
+ from: 'coordinator',
126
+ to: '*',
127
+ type: 'heartbeat',
128
+ content: `${task.title} — ${elapsed}s elapsed`,
129
+ metadata: { task_id: task.id, elapsed_s: elapsed }
130
+ }).catch(() => {});
131
+ }, 30_000);
132
+ }
133
+
134
+ try {
135
+ return await runFn();
136
+ } finally {
137
+ if (hbInterval) clearInterval(hbInterval);
138
+ }
139
+ }
140
+
141
+ // ─── Anti-analysis-loop guard ─────────────────────────────────────────────────
142
+
143
+ /**
144
+ * Check if an executor appears to be in an analysis loop.
145
+ * Posts a coordinator feedback message if the threshold is exceeded.
146
+ */
147
+ async function checkAntiLoop(projectDir, squadSlug, sessionId, task, enableBus, threshold = 8) {
148
+ if (!enableBus || !task.executor) return;
149
+
150
+ const messages = await bus.read(projectDir, squadSlug, sessionId).catch(() => []);
151
+ if (bus.isAnalysisLoop(messages, task.executor, threshold)) {
152
+ await bus.post(projectDir, squadSlug, sessionId, {
153
+ from: 'coordinator',
154
+ to: task.executor,
155
+ type: 'feedback',
156
+ content: `Analysis loop detected for "${task.title}". You have posted ${threshold}+ status updates without a result. Stop analyzing — produce concrete output now, or post a "block" message explaining why you cannot proceed.`,
157
+ metadata: { task_id: task.id, anti_loop: true }
158
+ }).catch(() => {});
159
+ }
160
+ }
161
+
162
+ // ─── Core task runner ─────────────────────────────────────────────────────────
163
+
164
+ /**
165
+ * Run a single task (no retry logic here — handled by gap closure wrapper).
166
+ */
167
+ async function runTask(projectDir, squadSlug, task, sessionId, options, logger) {
168
+ const { enableBus, enableReflect, timeoutMs } = options;
169
+ const taskCtx = {
170
+ projectDir,
171
+ squadSlug,
172
+ executorSlug: task.executor || 'unknown',
173
+ taskTitle: task.title,
174
+ iteration: task._attempt || 1
175
+ };
176
+
177
+ // Post status to bus
178
+ if (enableBus) {
179
+ await bus.post(projectDir, squadSlug, sessionId, {
180
+ from: task.executor || 'coordinator',
181
+ to: '*',
182
+ type: 'status',
183
+ content: `Starting: ${task.title}${task._attempt > 1 ? ` (retry ${task._attempt})` : ''}`,
184
+ metadata: { task_id: task.id, attempt: task._attempt || 1 }
185
+ }).catch(() => {});
186
+ }
187
+
188
+ await updateTaskStatus(projectDir, squadSlug, sessionId, task.id, 'in_progress');
189
+
190
+ // Build worker input with fresh context pointers (2.2: paths, not inline content)
191
+ const workerInput = {
192
+ task_id: task.id,
193
+ title: task.title,
194
+ description: task.description,
195
+ acceptance_criteria: task.acceptance_criteria,
196
+ read_first_hints: task.read_first_hints || [], // executor reads these, not coordinator
197
+ must_haves: task.must_haves || null,
198
+ session_id: sessionId,
199
+ bus_enabled: enableBus,
200
+ ...(task._failure_context ? { failure_context: task._failure_context, attempt: task._attempt } : {})
201
+ };
202
+
203
+ let workerResult = null;
204
+ let taskOutput = null;
205
+ let workerRan = false;
206
+
207
+ const workers = await listWorkers(projectDir, squadSlug);
208
+ const workerConfig = workers.find(
209
+ (w) => w.slug === task.executor || w.slug === task.id
210
+ );
211
+
212
+ if (workerConfig) {
213
+ workerRan = true;
214
+ workerResult = await runWorker(projectDir, squadSlug, workerConfig.slug, workerInput, {
215
+ timeoutMs,
216
+ triggerType: 'autorun'
217
+ });
218
+ taskOutput = workerResult.ok
219
+ ? JSON.stringify(workerResult.output || '')
220
+ : `Worker failed: ${workerResult.error}`;
221
+ } else {
222
+ taskOutput = `[no-worker-script] Task "${task.title}" assigned to executor "${task.executor}". Run manually or scaffold a worker with: aioson squad:worker --squad=${squadSlug} create --slug=${task.executor}`;
223
+ workerResult = { ok: true, output: { message: taskOutput }, noScript: true };
224
+ }
225
+
226
+ // Anti-analysis-loop check after task runs
227
+ await checkAntiLoop(projectDir, squadSlug, sessionId, task, enableBus);
228
+
229
+ // Reflection pass — pass full task object so verify-gate can check must_haves
230
+ let reflectionResult = null;
231
+ if (enableReflect && taskOutput && workerResult.ok) {
232
+ reflectionResult = await reflect(taskOutput, {
233
+ ...taskCtx,
234
+ iteration: task._attempt || 1,
235
+ task // enables must_haves verification
236
+ });
237
+
238
+ if (enableBus) {
239
+ await bus.post(projectDir, squadSlug, sessionId, {
240
+ from: task.executor || 'coordinator',
241
+ to: '*',
242
+ type: 'feedback',
243
+ content: formatReport(reflectionResult, task.executor),
244
+ metadata: { task_id: task.id, verdict: reflectionResult.verdict }
245
+ }).catch(() => {});
246
+ }
247
+ }
248
+
249
+ // Determine final status
250
+ let finalStatus;
251
+ if (!workerResult.ok) {
252
+ finalStatus = 'failed';
253
+ } else if (reflectionResult && reflectionResult.verdict === 'ESCALATE') {
254
+ finalStatus = 'escalated';
255
+ } else {
256
+ finalStatus = 'completed';
257
+ }
258
+
259
+ await updateTaskStatus(projectDir, squadSlug, sessionId, task.id, finalStatus, {
260
+ worker_ran: workerRan,
261
+ output_summary: String(taskOutput || '').slice(0, 500),
262
+ reflection: reflectionResult
263
+ ? { verdict: reflectionResult.verdict, score: reflectionResult.score }
264
+ : null,
265
+ completed_at: nowIso()
266
+ });
267
+
268
+ if (enableBus) {
269
+ await bus.post(projectDir, squadSlug, sessionId, {
270
+ from: task.executor || 'coordinator',
271
+ to: '*',
272
+ type: 'result',
273
+ content: `${icon(finalStatus)} ${task.title} → ${finalStatus.toUpperCase()}${reflectionResult ? ` (reflection: ${reflectionResult.verdict})` : ''}`,
274
+ metadata: { task_id: task.id, status: finalStatus }
275
+ }).catch(() => {});
276
+ }
277
+
278
+ return { task, finalStatus, workerResult, reflectionResult };
279
+ }
280
+
281
+ // ─── Inter-squad dependency validation ───────────────────────────────────────
282
+
283
+ /**
284
+ * Validate inter-squad dependencies declared in manifest.depends_on.
285
+ * Returns { satisfied: boolean, unmet: Array<{squad, event}> }
286
+ */
287
+ async function validateInterSquadDependencies(projectDir, manifest) {
288
+ const deps = manifest.depends_on || [];
289
+ if (deps.length === 0) return { satisfied: true, unmet: [] };
290
+
291
+ const unmet = [];
292
+ for (const dep of deps) {
293
+ if (!dep.event) continue;
294
+ try {
295
+ const events = await interSquadEvents.consume(projectDir, {
296
+ toSquad: manifest.slug,
297
+ subscriptions: [dep.event]
298
+ });
299
+ if (events.length === 0) {
300
+ unmet.push({ squad: dep.squad || '(unknown)', event: dep.event });
301
+ }
302
+ } catch {
303
+ unmet.push({ squad: dep.squad || '(unknown)', event: dep.event });
304
+ }
305
+ }
306
+
307
+ return { satisfied: unmet.length === 0, unmet };
308
+ }
309
+
310
+ // ─── Bus coordinator intelligence ────────────────────────────────────────────
311
+
312
+ // ── Block classification helpers ─────────────────────────────────────────────
313
+
314
+ function extractKeyName(content) {
315
+ const m = content.match(/([A-Z_]{3,}_(?:KEY|TOKEN|SECRET|ID))/i);
316
+ return m ? m[1].toUpperCase() : 'API key';
317
+ }
318
+
319
+ function extractMissingPath(content) {
320
+ const m = content.match(/(?:file|path|found)[:\s]+([./\w-]+\.\w+)/i);
321
+ return m ? m[1] : 'unknown file';
322
+ }
323
+
324
+ function extractDependencyId(content) {
325
+ const m = content.match(/task[- _](\d{2})/i);
326
+ return m ? `task-${m[1]}` : null;
327
+ }
328
+
329
+ function classifyBlock(content) {
330
+ const c = String(content || '').toLowerCase();
331
+ if (/api\s*key|credential|secret|token\b|\.env/.test(c)) return 'api_key';
332
+ if (/file\s*not\s*found|no such file|enoent|missing file/.test(c)) return 'file_missing';
333
+ if (/permission\s*denied|access\s*denied|forbidden|403/.test(c)) return 'permission';
334
+ if (/timed?\s*out|etimedout|deadline|exceeded/.test(c)) return 'timeout';
335
+ if (/depends\s*on|waiting\s*for|requires\s*task|blocked\s*by/.test(c)) return 'dependency';
336
+ return 'generic';
337
+ }
338
+
339
+ /**
340
+ * Strategy map: each strategy returns an { action, message, metadata } object.
341
+ */
342
+ const BLOCK_STRATEGIES = {
343
+ api_key: (block, _ctx) => ({
344
+ action: 'human-gate',
345
+ message: `API key/credential required: ${extractKeyName(block.content)}. Add to .env and restart the session.`,
346
+ metadata: { resolution: 'human_escalation', key_hint: extractKeyName(block.content) }
347
+ }),
348
+
349
+ file_missing: (block, ctx) => {
350
+ const missingFile = extractMissingPath(block.content);
351
+ const prevResults = (ctx.recentResults || []).filter((r) => r.includes('.') || r.includes('/'));
352
+ const hint = prevResults.length > 0
353
+ ? `Check if a previous task created it. Recent results: ${prevResults.slice(0, 2).join(' | ')}`
354
+ : `Verify the file path and that the producing task ran successfully.`;
355
+ return {
356
+ action: 'retry-with-context',
357
+ message: `File not found: ${missingFile}. ${hint}`,
358
+ metadata: { resolution: 'context_hint', missing_file: missingFile }
359
+ };
360
+ },
361
+
362
+ dependency: (block, _ctx) => {
363
+ const depId = extractDependencyId(block.content);
364
+ return {
365
+ action: 'wait-and-retry',
366
+ message: depId
367
+ ? `Waiting for ${depId} to complete before proceeding.`
368
+ : `Dependency not satisfied. Wait for upstream tasks to complete.`,
369
+ metadata: { resolution: 'dependency_wait', dep_id: depId }
370
+ };
371
+ },
372
+
373
+ permission: (_block, _ctx) => ({
374
+ action: 'human-gate',
375
+ message: 'Permission denied — this operation requires manual intervention or elevated access.',
376
+ metadata: { resolution: 'human_escalation' }
377
+ }),
378
+
379
+ timeout: (_block, _ctx) => ({
380
+ action: 'abort-and-escalate',
381
+ message: 'Task exceeded its timeout. It may need to be decomposed into smaller steps.',
382
+ metadata: { resolution: 'escalate_decompose' }
383
+ }),
384
+
385
+ generic: (block, ctx) => {
386
+ const recentCtx = (ctx.recentResults || []).join('\n');
387
+ return {
388
+ action: 'coordinator-hint',
389
+ message: `Coordinator attempting to resolve block. Try proceeding with available information.${recentCtx ? `\nContext from other executors:\n${recentCtx}` : ''}`,
390
+ metadata: { resolution: 'coordinator_hint' }
391
+ };
392
+ }
393
+ };
394
+
395
+ /**
396
+ * After each wave, inspect the bus for unresolved block messages and
397
+ * attempt automatic resolution using the strategy pattern.
398
+ *
399
+ * Block types: api_key, file_missing, dependency, permission, timeout, generic
400
+ * Actions: human-gate, retry-with-context, wait-and-retry, abort-and-escalate, coordinator-hint
401
+ */
402
+ async function handleBusBlocks(projectDir, squadSlug, sessionId, logger) {
403
+ const allMessages = await bus.read(projectDir, squadSlug, sessionId).catch(() => []);
404
+ const unresolved = getUnresolvedBlocks(allMessages);
405
+
406
+ if (unresolved.length === 0) return;
407
+
408
+ const recentResultMessages = allMessages
409
+ .filter((m) => m.type === 'result')
410
+ .slice(-3)
411
+ .map((m) => `${m.from}: ${String(m.content || '').slice(0, 100)}`);
412
+
413
+ for (const block of unresolved) {
414
+ const blockType = classifyBlock(block.content);
415
+ const strategy = BLOCK_STRATEGIES[blockType] || BLOCK_STRATEGIES.generic;
416
+ const resolution = strategy(block, { recentResults: recentResultMessages });
417
+
418
+ if (resolution.action === 'human-gate' || resolution.action === 'abort-and-escalate') {
419
+ logger.log(` ⚠ Block [${blockType}] requires human attention: "${String(block.content || '').slice(0, 80)}"`);
420
+ }
421
+
422
+ await bus.post(projectDir, squadSlug, sessionId, {
423
+ from: 'coordinator',
424
+ to: block.from,
425
+ type: 'resolution',
426
+ content: resolution.message,
427
+ metadata: { block_id: block.id, block_type: blockType, ...resolution.metadata }
428
+ }).catch(() => {});
429
+ }
430
+ }
431
+
432
+ // ─── Gap Closure Loop ─────────────────────────────────────────────────────────
433
+
434
+ /**
435
+ * Run a task with automatic retry on failure (gap closure).
436
+ *
437
+ * On failure:
438
+ * 1. Capture the error as failure context
439
+ * 2. Re-run the task with the failure context injected into workerInput
440
+ * 3. Repeat up to maxRetries times
441
+ * 4. If still failing after maxRetries: escalate
442
+ *
443
+ * ESCALATE results are NOT retried — they require coordinator/human attention.
444
+ */
445
+ async function runTaskWithGapClosure(
446
+ projectDir, squadSlug, task, sessionId, options, logger,
447
+ maxRetries = 3
448
+ ) {
449
+ const { enableBus } = options;
450
+ let currentTask = { ...task, _attempt: 1 };
451
+ let lastError = null;
452
+
453
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
454
+ currentTask = { ...currentTask, _attempt: attempt };
455
+
456
+ // Announce gap closure retry on bus (only from attempt 2 onwards)
457
+ if (attempt > 1 && enableBus) {
458
+ await bus.post(projectDir, squadSlug, sessionId, {
459
+ from: 'coordinator',
460
+ to: task.executor || '*',
461
+ type: 'gap_closure_attempt',
462
+ content: `Retrying "${task.title}" (attempt ${attempt}/${maxRetries}). Previous failure: ${lastError}`,
463
+ metadata: { task_id: task.id, attempt, prev_error: lastError }
464
+ }).catch(() => {});
465
+ }
466
+
467
+ // Run with heartbeat
468
+ const result = await runTaskWithHeartbeat(
469
+ projectDir, squadSlug, task, sessionId, options,
470
+ () => runTask(projectDir, squadSlug, currentTask, sessionId, options, logger)
471
+ );
472
+
473
+ if (result.finalStatus === 'completed') {
474
+ if (attempt > 1) {
475
+ logger.log(` ↩ Gap closed after ${attempt} attempt(s)`);
476
+ }
477
+ return result;
478
+ }
479
+
480
+ // Don't retry escalated tasks — they need human/coordinator attention
481
+ if (result.finalStatus === 'escalated') {
482
+ return result;
483
+ }
484
+
485
+ // Capture failure context for next attempt
486
+ lastError = result.workerResult?.error || `task failed on attempt ${attempt}`;
487
+ currentTask = {
488
+ ...currentTask,
489
+ _failure_context: lastError
490
+ };
491
+
492
+ if (attempt < maxRetries) {
493
+ logger.log(` ↩ Gap closure attempt ${attempt}/${maxRetries} failed — retrying with context`);
494
+ }
495
+ }
496
+
497
+ // Exhausted retries — escalate
498
+ logger.log(` ✗ Gap closure exhausted (${maxRetries} attempts) — escalating`);
499
+
500
+ await updateTaskStatus(projectDir, squadSlug, sessionId, task.id, 'escalated', {
501
+ gap_closure_exhausted: true,
502
+ last_error: lastError,
503
+ completed_at: nowIso()
504
+ });
505
+
506
+ if (enableBus) {
507
+ await bus.post(projectDir, squadSlug, sessionId, {
508
+ from: 'coordinator',
509
+ to: '*',
510
+ type: 'block',
511
+ content: `Task "${task.title}" escalated after ${maxRetries} gap closure attempts. Last error: ${lastError}`,
512
+ metadata: { task_id: task.id, gap_closure_exhausted: true, attempts: maxRetries }
513
+ }).catch(() => {});
514
+ }
515
+
516
+ return {
517
+ task,
518
+ finalStatus: 'escalated',
519
+ workerResult: { ok: false, error: lastError, gap_closure_exhausted: true },
520
+ reflectionResult: null
521
+ };
522
+ }
523
+
524
+ // ─── Sampling-and-Voting (Plan 81 §Sprint 4) ────────────────────────────────
525
+
526
+ /**
527
+ * Synthesize votes from multiple worker instances.
528
+ * Returns { consensus, winningOutput, allVotes }.
529
+ *
530
+ * Consensus is the fraction of instances that agree on the same status.
531
+ * For completed tasks, compares output similarity via a simple hash.
532
+ */
533
+ function synthesizeVotes(votes) {
534
+ const statusCounts = {};
535
+ for (const v of votes) {
536
+ const s = v.finalStatus || 'unknown';
537
+ statusCounts[s] = (statusCounts[s] || 0) + 1;
538
+ }
539
+
540
+ let bestStatus = 'unknown';
541
+ let bestCount = 0;
542
+ for (const [s, count] of Object.entries(statusCounts)) {
543
+ if (count > bestCount) { bestStatus = s; bestCount = count; }
544
+ }
545
+
546
+ const consensus = bestCount / votes.length;
547
+ const winningVotes = votes.filter((v) => v.finalStatus === bestStatus);
548
+
549
+ return {
550
+ consensus,
551
+ bestStatus,
552
+ winningOutput: winningVotes[0],
553
+ allVotes: votes.map((v) => ({
554
+ finalStatus: v.finalStatus,
555
+ outputSummary: String(v.workerResult?.output || '').slice(0, 200)
556
+ }))
557
+ };
558
+ }
559
+
560
+ /**
561
+ * Run a task with sampling-and-voting for critical decisions.
562
+ *
563
+ * Spawns N instances of the same task in parallel, then synthesizes votes.
564
+ * If consensus < threshold, escalates to human-gate.
565
+ *
566
+ * @param {object} task — task with voting config: { instances, threshold }
567
+ * @param {number} instances — number of parallel instances (default: 3)
568
+ * @param {number} threshold — minimum consensus fraction (default: 0.66)
569
+ */
570
+ async function runTaskWithVoting(
571
+ projectDir, squadSlug, task, sessionId, options, logger,
572
+ instances = 3, threshold = 0.66
573
+ ) {
574
+ const { enableBus } = options;
575
+
576
+ if (enableBus) {
577
+ await bus.post(projectDir, squadSlug, sessionId, {
578
+ from: 'coordinator',
579
+ to: '*',
580
+ type: 'status',
581
+ content: `Sampling-and-Voting: running ${instances} instances of "${task.title}"`,
582
+ metadata: { task_id: task.id, voting: true, instances }
583
+ }).catch(() => {});
584
+ }
585
+
586
+ // Spawn N instances in parallel
587
+ const instancePromises = [];
588
+ for (let i = 1; i <= instances; i++) {
589
+ const instanceTask = { ...task, _attempt: 1, _voting_instance: i };
590
+ instancePromises.push(
591
+ runTaskWithHeartbeat(
592
+ projectDir, squadSlug, instanceTask, sessionId, options,
593
+ () => runTask(projectDir, squadSlug, instanceTask, sessionId, options, logger)
594
+ ).catch((err) => ({
595
+ task: instanceTask,
596
+ finalStatus: 'failed',
597
+ workerResult: { ok: false, error: err.message },
598
+ reflectionResult: null
599
+ }))
600
+ );
601
+ }
602
+
603
+ const votes = await Promise.all(instancePromises);
604
+ const result = synthesizeVotes(votes);
605
+
606
+ if (enableBus) {
607
+ await bus.post(projectDir, squadSlug, sessionId, {
608
+ from: 'coordinator',
609
+ to: '*',
610
+ type: 'feedback',
611
+ content: `Voting result for "${task.title}": consensus=${(result.consensus * 100).toFixed(0)}% status=${result.bestStatus} (${instances} instances)`,
612
+ metadata: { task_id: task.id, consensus: result.consensus, bestStatus: result.bestStatus }
613
+ }).catch(() => {});
614
+ }
615
+
616
+ // If consensus below threshold, escalate
617
+ if (result.consensus < threshold) {
618
+ logger.log(` ⚠ Voting: consensus ${(result.consensus * 100).toFixed(0)}% < ${(threshold * 100).toFixed(0)}% threshold — escalating`);
619
+
620
+ await updateTaskStatus(projectDir, squadSlug, sessionId, task.id, 'escalated', {
621
+ voting: { consensus: result.consensus, threshold, allVotes: result.allVotes },
622
+ completed_at: nowIso()
623
+ });
624
+
625
+ if (enableBus) {
626
+ await bus.post(projectDir, squadSlug, sessionId, {
627
+ from: 'coordinator',
628
+ to: '*',
629
+ type: 'block',
630
+ content: `Task "${task.title}" — voting consensus too low (${(result.consensus * 100).toFixed(0)}%). Requires human review.`,
631
+ metadata: { task_id: task.id, voting_escalated: true }
632
+ }).catch(() => {});
633
+ }
634
+
635
+ return {
636
+ task,
637
+ finalStatus: 'escalated',
638
+ workerResult: { ok: false, error: 'voting_consensus_below_threshold', voting: result },
639
+ reflectionResult: null
640
+ };
641
+ }
642
+
643
+ // Use the winning vote as the result
644
+ return result.winningOutput;
645
+ }
646
+
647
+ // ─── Evaluator-Optimizer Loop (Plan 82 §ITEM 4) ──────────────────────────────
648
+
649
+ /**
650
+ * Run a task through an evaluator-optimizer loop (dev→qa→dev...):
651
+ * 1. Generator phase: run the task as the implementer
652
+ * 2. Evaluator phase: run a review task against criteria (fresh context)
653
+ * 3. If PASS → done. If FAIL → inject structured feedback → repeat
654
+ * 4. Max iterations before escalating to human
655
+ *
656
+ * The key: the evaluator receives only the artifact, not the generator's reasoning.
657
+ * This prevents confirmation bias (same principle as verify-gate).
658
+ */
659
+ async function runWithEvalOptimize(
660
+ projectDir, squadSlug, task, sessionId, options, logger,
661
+ maxIterations = 3
662
+ ) {
663
+ const { enableBus } = options;
664
+ let lastFeedback = null;
665
+ let lastResult = null;
666
+
667
+ if (enableBus) {
668
+ await bus.post(projectDir, squadSlug, sessionId, {
669
+ from: 'coordinator',
670
+ to: '*',
671
+ type: 'status',
672
+ content: `Evaluator-Optimizer: starting loop for "${task.title}" (max ${maxIterations} iterations)`,
673
+ metadata: { task_id: task.id, eval_optimize: true, max_iterations: maxIterations }
674
+ }).catch(() => {});
675
+ }
676
+
677
+ for (let iteration = 1; iteration <= maxIterations; iteration++) {
678
+ // ── Generator phase ──────────────────────────────────────────────────────
679
+ const generatorTask = {
680
+ ...task,
681
+ _attempt: iteration,
682
+ ...(lastFeedback ? { _eval_feedback: lastFeedback, _eval_iteration: iteration } : {})
683
+ };
684
+
685
+ lastResult = await runTaskWithHeartbeat(
686
+ projectDir, squadSlug, generatorTask, sessionId, options,
687
+ () => runTask(projectDir, squadSlug, generatorTask, sessionId, options, logger)
688
+ );
689
+
690
+ if (lastResult.finalStatus !== 'completed') {
691
+ logger.log(` ↳ Eval-Optimize iteration ${iteration}: generator failed — escalating`);
692
+ return lastResult;
693
+ }
694
+
695
+ // ── Evaluator phase ──────────────────────────────────────────────────────
696
+ const reviewTask = {
697
+ id: `${task.id}-review-${iteration}`,
698
+ title: `Review: ${task.title} (iteration ${iteration})`,
699
+ description: [
700
+ `Evaluate the output of task "${task.title}".`,
701
+ ``,
702
+ `Criteria to check (ALL must pass):`,
703
+ ...(task.review_criteria || []).map((c) => `- ${c}`),
704
+ ``,
705
+ `Output PASS if all criteria are met.`,
706
+ `Output FAIL with structured feedback: specific file:line references, exact criterion violated, minimum change to pass.`,
707
+ ``,
708
+ `Do NOT consider how the implementer reasoned — evaluate only the artifact.`
709
+ ].join('\n'),
710
+ executor: task.reviewer || 'qa',
711
+ acceptance_criteria: ['Output either PASS or FAIL with structured feedback'],
712
+ _eval_artifact: lastResult.workerResult?.output || null
713
+ };
714
+
715
+ const evalResult = await runTaskWithHeartbeat(
716
+ projectDir, squadSlug, reviewTask, sessionId, options,
717
+ () => runTask(projectDir, squadSlug, reviewTask, sessionId, options, logger)
718
+ );
719
+
720
+ const evalOutput = String(evalResult.workerResult?.output || '');
721
+ const passed = /\bPASS\b/i.test(evalOutput) && !/\bFAIL\b/i.test(evalOutput);
722
+
723
+ if (enableBus) {
724
+ await bus.post(projectDir, squadSlug, sessionId, {
725
+ from: 'coordinator',
726
+ to: '*',
727
+ type: 'feedback',
728
+ content: `Eval-Optimize iteration ${iteration}/${maxIterations}: ${passed ? 'PASS' : 'FAIL'} — ${evalOutput.slice(0, 120)}`,
729
+ metadata: { task_id: task.id, iteration, verdict: passed ? 'PASS' : 'FAIL' }
730
+ }).catch(() => {});
731
+ }
732
+
733
+ if (passed) {
734
+ logger.log(` ↳ Eval-Optimize PASS at iteration ${iteration}`);
735
+ await updateTaskStatus(projectDir, squadSlug, sessionId, task.id, 'completed', {
736
+ eval_optimize: { iterations: iteration, verdict: 'PASS' },
737
+ completed_at: nowIso()
738
+ });
739
+ return lastResult;
740
+ }
741
+
742
+ lastFeedback = evalOutput.slice(0, 1000);
743
+ logger.log(` ↳ Eval-Optimize FAIL at iteration ${iteration} — applying feedback`);
744
+ }
745
+
746
+ // Max iterations reached — escalate
747
+ logger.log(` ✗ Eval-Optimize: ${maxIterations} iterations exhausted — escalating`);
748
+
749
+ await updateTaskStatus(projectDir, squadSlug, sessionId, task.id, 'escalated', {
750
+ eval_optimize: { iterations: maxIterations, verdict: 'FAIL', last_feedback: lastFeedback },
751
+ completed_at: nowIso()
752
+ });
753
+
754
+ if (enableBus) {
755
+ await bus.post(projectDir, squadSlug, sessionId, {
756
+ from: 'coordinator',
757
+ to: '*',
758
+ type: 'block',
759
+ content: `Task "${task.title}" — eval-optimize loop exhausted (${maxIterations} iterations). Requires human review.`,
760
+ metadata: { task_id: task.id, eval_optimize_exhausted: true }
761
+ }).catch(() => {});
762
+ }
763
+
764
+ return {
765
+ task,
766
+ finalStatus: 'escalated',
767
+ workerResult: { ok: false, error: 'eval_optimize_exhausted', last_feedback: lastFeedback },
768
+ reflectionResult: null
769
+ };
770
+ }
771
+
772
+ // ─── Main command ─────────────────────────────────────────────────────────────
773
+
774
+ async function runSquadAutorun({ args, options = {}, logger }) {
775
+ const targetDir = path.resolve(process.cwd(), args[0] || '.');
776
+ const squadSlug = String(options.squad || options.s || '').trim();
777
+
778
+ if (!squadSlug) {
779
+ logger.error('Error: --squad is required');
780
+ return { ok: false, error: 'missing_squad' };
781
+ }
782
+
783
+ const dryRun = Boolean(options['dry-run'] || options.dryRun);
784
+ const enableBus = options.bus !== false && options.bus !== 'false';
785
+ const enableReflect = Boolean(options.reflect);
786
+ const sequential = Boolean(options.sequential);
787
+ const mode = String(options.mode || 'heuristic').trim();
788
+ const timeoutMs = (options.timeout ? Number(options.timeout) : 120) * 1000;
789
+ const existingPlanId = String(options.plan || '').trim();
790
+ const enableGapClosure = options['no-gap-closure'] !== true && options['no-gap-closure'] !== 'true';
791
+ const maxRetries = Math.min(Math.max(Number(options['max-retries'] || 3), 1), 5);
792
+ const requestedEngine = String(options.engine || 'legacy').trim();
793
+ const ignoreDeps = Boolean(options['ignore-deps'] || options.ignoreDeps);
794
+ const waitDeps = Boolean(options['wait-deps'] || options.waitDeps);
795
+ const waitDepsTimeoutMs = (options['wait-deps-timeout'] ? Number(options['wait-deps-timeout']) : 60) * 1000;
796
+
797
+ // ── Resolve execution engine (Plan 81 §1.1) ──────────────────────────────
798
+ const engineResult = resolveEngine(requestedEngine);
799
+ if (engineResult.fallback) {
800
+ logger.log(`⚠ ${engineResult.reason} — falling back to legacy engine`);
801
+ }
802
+
803
+ // ── Load token budget ──────────────────────────────────────────────────────
804
+ const budget = await loadBudget(targetDir, squadSlug);
805
+ let sessionTokensUsed = 0;
806
+ let budgetExceeded = false;
807
+
808
+ // ── Load or create plan ────────────────────────────────────────────────────
809
+ let plan;
810
+ let sessionId;
811
+
812
+ if (existingPlanId) {
813
+ plan = await loadPlan(targetDir, squadSlug, existingPlanId);
814
+ if (!plan) {
815
+ logger.error(`Plan not found: session "${existingPlanId}" for squad "${squadSlug}"`);
816
+ return { ok: false, error: 'plan_not_found' };
817
+ }
818
+ sessionId = existingPlanId;
819
+ logger.log(`Resuming plan [${sessionId}] — ${plan.tasks.length} tasks`);
820
+ } else {
821
+ const goal = String(options.goal || '').trim();
822
+ if (!goal) {
823
+ logger.error('Error: --goal is required (or --plan to resume an existing plan)');
824
+ return { ok: false, error: 'missing_goal' };
825
+ }
826
+
827
+ sessionId = randomUUID();
828
+ logger.log(`Decomposing goal for squad "${squadSlug}" [${mode}]...`);
829
+ plan = await decompose(targetDir, squadSlug, goal, { sessionId, mode, save: !dryRun });
830
+ logger.log(`Plan ready: ${plan.tasks.length} tasks across ${Object.keys(plan.parallel_groups).length} parallel group(s)`);
831
+ }
832
+
833
+ // ── Show plan ──────────────────────────────────────────────────────────────
834
+ if (options.json) {
835
+ if (dryRun) return { ok: true, dryRun: true, plan };
836
+ } else {
837
+ logger.log('');
838
+ logger.log(formatPlan(plan));
839
+ logger.log('');
840
+ }
841
+
842
+ if (dryRun) {
843
+ logger.log('[dry-run] Plan shown above. No tasks executed.');
844
+ return { ok: true, dryRun: true, plan };
845
+ }
846
+
847
+ // ── Handle structured mode ─────────────────────────────────────────────────
848
+ if (mode === 'structured' && plan.structured_prompt) {
849
+ const promptPath = path.join(
850
+ targetDir, '.aioson', 'squads', squadSlug, 'sessions', sessionId, 'decompose-prompt.md'
851
+ );
852
+ const promptContent = [
853
+ '---',
854
+ `session_id: ${sessionId}`,
855
+ `squad: ${squadSlug}`,
856
+ `created_at: ${plan.created_at}`,
857
+ '---',
858
+ '',
859
+ plan.structured_prompt,
860
+ '',
861
+ '> After the agent fills in the JSON above, run:',
862
+ `> aioson squad:autorun . --squad=${squadSlug} --plan=${sessionId}`
863
+ ].join('\n');
864
+
865
+ const { ensureDir } = require('../utils');
866
+ await ensureDir(path.dirname(promptPath));
867
+ await fs.writeFile(promptPath, promptContent, 'utf8');
868
+
869
+ logger.log(`[structured] Decomposition prompt saved to: ${path.relative(targetDir, promptPath)}`);
870
+ logger.log('Activate your agent to fill in the plan, then resume with:');
871
+ logger.log(` aioson squad:autorun . --squad=${squadSlug} --plan=${sessionId}`);
872
+ return { ok: true, mode: 'structured', sessionId, promptPath: path.relative(targetDir, promptPath), plan };
873
+ }
874
+
875
+ // ── Load squad manifest (for inter-squad config and hooks) ─────────────────
876
+ let squadManifest = {};
877
+ try {
878
+ const manifestPath = path.join(targetDir, '.aioson', 'squads', squadSlug, 'squad.manifest.json');
879
+ squadManifest = JSON.parse(await fs.readFile(manifestPath, 'utf8'));
880
+ } catch { /* manifest is optional */ }
881
+
882
+ // ── Inter-squad dependency validation ─────────────────────────────────────
883
+ if (!ignoreDeps && (squadManifest.depends_on || []).length > 0) {
884
+ const depResult = await validateInterSquadDependencies(targetDir, squadManifest);
885
+ if (!depResult.satisfied) {
886
+ const depList = depResult.unmet.map((d) => `${d.squad}/${d.event}`).join(', ');
887
+ if (waitDeps) {
888
+ logger.log(`⏳ Waiting for inter-squad events: ${depList}`);
889
+ const deadline = Date.now() + waitDepsTimeoutMs;
890
+ let resolved = false;
891
+ while (Date.now() < deadline) {
892
+ await sleep(3000);
893
+ const retry = await validateInterSquadDependencies(targetDir, squadManifest);
894
+ if (retry.satisfied) { resolved = true; break; }
895
+ }
896
+ if (!resolved) {
897
+ logger.error(`✗ Dependencies still unmet after ${waitDepsTimeoutMs / 1000}s: ${depList}`);
898
+ return { ok: false, error: 'unmet_dependencies', unmet: depResult.unmet };
899
+ }
900
+ logger.log(' ✓ Dependencies satisfied');
901
+ } else {
902
+ logger.warn(`⚠ Unmet inter-squad dependencies: ${depList}`);
903
+ logger.warn(' Use --ignore-deps to run anyway, or --wait-deps to poll until satisfied');
904
+ return { ok: false, error: 'unmet_dependencies', unmet: depResult.unmet };
905
+ }
906
+ }
907
+ }
908
+
909
+ // ── 3.3 Hook Exit Code Protocol — pre_run hook ─────────────────────────────
910
+ const preRunHook = squadManifest.hooks?.pre_run;
911
+ if (preRunHook) {
912
+ logger.log(`Running pre_run hook: ${preRunHook.slice(0, 60)}${preRunHook.length > 60 ? '...' : ''}`);
913
+ const hookResult = runHook(preRunHook, {
914
+ squad: squadSlug,
915
+ session_id: sessionId,
916
+ goal: plan.goal,
917
+ project_dir: targetDir
918
+ });
919
+ if (hookResult.denied) {
920
+ logger.error(`✗ Pre-run hook denied execution${hookResult.stderr ? ': ' + hookResult.stderr : ''}`);
921
+ return { ok: false, error: 'hook_denied', hook: preRunHook, reason: hookResult.stderr };
922
+ }
923
+ if (hookResult.warn) {
924
+ logger.log(` ⚠ Pre-run hook exited ${hookResult.exitCode} (non-fatal)${hookResult.stderr ? ': ' + hookResult.stderr : ''}`);
925
+ } else {
926
+ logger.log(' ✓ Pre-run hook passed');
927
+ }
928
+ }
929
+
930
+ // ── 3.1 Inter-Squad Event Streaming — consume pending events ───────────────
931
+ const subscriptions = [
932
+ ...(squadManifest.subscriptions || []),
933
+ ...(squadManifest.depends_on || []).map((d) => d.event).filter(Boolean)
934
+ ];
935
+ let incomingEvents = [];
936
+ if (subscriptions.length > 0) {
937
+ incomingEvents = await interSquadEvents
938
+ .consume(targetDir, { toSquad: squadSlug, subscriptions })
939
+ .catch(() => []);
940
+ if (incomingEvents.length > 0) {
941
+ logger.log(`Inter-squad events received: ${incomingEvents.length}`);
942
+ for (const ev of incomingEvents) {
943
+ logger.log(` ← [${ev.fromSquad}] ${ev.event}${ev.payload ? ' · ' + JSON.stringify(ev.payload).slice(0, 80) : ''}`);
944
+ }
945
+ logger.log('');
946
+ }
947
+ }
948
+
949
+ // Inject incoming events into the plan goal context so executors are aware
950
+ if (incomingEvents.length > 0 && plan.tasks.length > 0) {
951
+ const firstTask = plan.tasks[0];
952
+ const eventSummary = incomingEvents
953
+ .map((e) => `[${e.fromSquad}] ${e.event}: ${JSON.stringify(e.payload || {})}`)
954
+ .join('\n');
955
+ firstTask._inter_squad_events = eventSummary;
956
+ }
957
+
958
+ // ── Record session start in STATE.md ───────────────────────────────────────
959
+ await stateManager.recordSessionStart(targetDir, squadSlug, sessionId, plan.goal).catch(() => {});
960
+
961
+ // ── Agent Teams engine path (Plan 81 §1.1) ────────────────────────────────
962
+ if (engineResult.engine === 'agent-teams') {
963
+ logger.log(`Engine: agent-teams (Claude Code ${engineResult.version})`);
964
+ const teamConfig = translateToTeamConfig(targetDir, squadManifest, plan, {
965
+ budget, enableBus
966
+ });
967
+ const configPath = await writeTeamConfig(targetDir, squadSlug, teamConfig);
968
+ logger.log(`Team config: ${path.relative(targetDir, configPath)}`);
969
+ logger.log(`Teammates: ${teamConfig.teammates.map((t) => t.name).join(', ')}`);
970
+ logger.log(`Tasks: ${teamConfig.tasks.length}`);
971
+ logger.log('');
972
+ logger.log('Agent Teams execution is configured. Use:');
973
+ logger.log(` claude --team ${path.relative(targetDir, configPath)}`);
974
+ logger.log('');
975
+
976
+ await stateManager.recordSessionEnd(targetDir, squadSlug, sessionId, []).catch(() => {});
977
+
978
+ return {
979
+ ok: true,
980
+ engine: 'agent-teams',
981
+ session_id: sessionId,
982
+ squad: squadSlug,
983
+ configPath: path.relative(targetDir, configPath),
984
+ teamConfig
985
+ };
986
+ }
987
+
988
+ // ── Execute tasks (legacy engine) ─────────────────────────────────────────
989
+ logger.log(`Starting execution — session [${sessionId}]`);
990
+ if (enableBus) logger.log('Intra-squad bus: enabled');
991
+ if (enableReflect) logger.log('Reflection: enabled');
992
+ if (enableGapClosure) logger.log(`Gap closure: enabled (max ${maxRetries} retries)`);
993
+ if (budget.maxTokensPerSession !== Infinity) {
994
+ logger.log(`Budget: ${budget.maxTokensPerSession.toLocaleString()} tokens/session`);
995
+ }
996
+ logger.log('');
997
+
998
+ const results = [];
999
+ let completedCount = 0;
1000
+ let failedCount = 0;
1001
+ let escalatedCount = 0;
1002
+ const startedAt = Date.now();
1003
+
1004
+ const runOptions = { enableBus, enableReflect, timeoutMs };
1005
+
1006
+ // Run in parallel group waves (or sequentially if --sequential)
1007
+ const groups = Object.keys(plan.parallel_groups).map(Number).sort((a, b) => a - b);
1008
+
1009
+ for (const group of groups) {
1010
+ if (budgetExceeded) {
1011
+ logger.log(`⚠ Session budget exceeded (${sessionTokensUsed.toLocaleString()} tokens used). Stopping.`);
1012
+ break;
1013
+ }
1014
+
1015
+ const groupTaskIds = plan.parallel_groups[group];
1016
+ const groupTasks = groupTaskIds
1017
+ .map((id) => plan.tasks.find((t) => t.id === id))
1018
+ .filter((t) => t && t.status === 'pending');
1019
+
1020
+ if (groupTasks.length === 0) continue;
1021
+
1022
+ logger.log(`── Group ${group} (${groupTasks.length} task${groupTasks.length > 1 ? 's' : ''})${groupTasks.length > 1 && !sequential ? ' — running in parallel' : ''}`);
1023
+
1024
+ // ── Budget gate: check before running this group ────────────────────────
1025
+ const groupEstimatedTokens = groupTasks.reduce((sum, t) => sum + estimateTaskTokens(t), 0);
1026
+ if (sessionTokensUsed + groupEstimatedTokens > budget.maxTokensPerSession) {
1027
+ logger.log(` ⚠ Budget gate: estimated ${(sessionTokensUsed + groupEstimatedTokens).toLocaleString()} tokens would exceed session limit of ${budget.maxTokensPerSession.toLocaleString()}.`);
1028
+ if (budget.actionOnExceed === 'abort') {
1029
+ logger.log(' Budget action: abort — stopping execution.');
1030
+ budgetExceeded = true;
1031
+ break;
1032
+ }
1033
+ // Default: 'pause' — warn and continue (user can see in summary)
1034
+ logger.log(' Budget action: pause — marking remaining tasks as skipped.');
1035
+ for (const task of groupTasks) {
1036
+ await updateTaskStatus(targetDir, squadSlug, sessionId, task.id, 'skipped', {
1037
+ skip_reason: 'budget_exceeded',
1038
+ estimated_tokens: estimateTaskTokens(task)
1039
+ });
1040
+ }
1041
+ budgetExceeded = true;
1042
+ break;
1043
+ }
1044
+
1045
+ // ── Run tasks in this group ────────────────────────────────────────────
1046
+ const runTask_ = async (task) => {
1047
+ // Brief validation guard (Plan 80 §1): validate brief before spawn
1048
+ if (task.brief_path) {
1049
+ const briefResult = await validateBrief(task.brief_path, targetDir);
1050
+ if (!briefResult.ready) {
1051
+ // Auto-fix simple fields (out_of_scope only)
1052
+ if (briefResult.issues.length === 1 && briefResult.issues[0].field === 'out_of_scope') {
1053
+ await autoFixBrief(task.brief_path, targetDir);
1054
+ logger.log(` ↩ Auto-fixed brief for ${task.id} (out_of_scope)`);
1055
+ } else {
1056
+ if (enableBus) {
1057
+ await bus.post(targetDir, squadSlug, sessionId, {
1058
+ from: 'coordinator', to: '*', type: 'block',
1059
+ content: `Brief NOT READY for ${task.id}: ${briefResult.issues.map((i) => i.message).join(', ')}`,
1060
+ metadata: { task_id: task.id, brief_validation: briefResult }
1061
+ }).catch(() => {});
1062
+ }
1063
+ logger.log(` ⚠ ${task.id}: brief NOT READY (${briefResult.issues.length} issues) — skipping`);
1064
+ await updateTaskStatus(targetDir, squadSlug, sessionId, task.id, 'skipped', {
1065
+ skip_reason: 'brief_not_ready', issues: briefResult.issues
1066
+ });
1067
+ return { task, finalStatus: 'skipped', workerResult: null, reflectionResult: null };
1068
+ }
1069
+ }
1070
+ }
1071
+
1072
+ logger.log(` ${icon('in_progress')} ${task.id}: ${task.title}`);
1073
+
1074
+ // Sampling-and-Voting path (Plan 81 §Sprint 4)
1075
+ if (task.voting) {
1076
+ const votingInstances = task.voting.instances || 3;
1077
+ const votingThreshold = task.voting.threshold || 0.66;
1078
+ logger.log(` ↳ Voting: ${votingInstances} instances, threshold ${(votingThreshold * 100).toFixed(0)}%`);
1079
+ const runner = runTaskWithVoting(
1080
+ targetDir, squadSlug, task, sessionId, runOptions, logger,
1081
+ votingInstances, votingThreshold
1082
+ );
1083
+ return runner.then((r) => {
1084
+ logger.log(` ${icon(r.finalStatus)} ${task.id}: ${r.finalStatus.toUpperCase()}`);
1085
+ return r;
1086
+ });
1087
+ }
1088
+
1089
+ // Evaluator-Optimizer path (Plan 82 §ITEM 4)
1090
+ if (task.review_loop) {
1091
+ const maxReviewIter = task.max_review_iterations || 3;
1092
+ logger.log(` ↳ Eval-Optimize: max ${maxReviewIter} iterations, reviewer=${task.reviewer || 'qa'}`);
1093
+ return runWithEvalOptimize(
1094
+ targetDir, squadSlug, task, sessionId, runOptions, logger, maxReviewIter
1095
+ ).then((r) => {
1096
+ logger.log(` ${icon(r.finalStatus)} ${task.id}: ${r.finalStatus.toUpperCase()}`);
1097
+ return r;
1098
+ });
1099
+ }
1100
+
1101
+ const runner = enableGapClosure
1102
+ ? runTaskWithGapClosure(targetDir, squadSlug, task, sessionId, runOptions, logger, maxRetries)
1103
+ : runTaskWithHeartbeat(targetDir, squadSlug, task, sessionId, runOptions,
1104
+ () => runTask(targetDir, squadSlug, task, sessionId, runOptions, logger));
1105
+ return runner.then((r) => {
1106
+ logger.log(` ${icon(r.finalStatus)} ${task.id}: ${r.finalStatus.toUpperCase()}`);
1107
+ return r;
1108
+ });
1109
+ };
1110
+
1111
+ let groupResults;
1112
+ if (sequential || groupTasks.length === 1) {
1113
+ groupResults = [];
1114
+ for (const task of groupTasks) {
1115
+ groupResults.push(await runTask_(task));
1116
+ }
1117
+ } else {
1118
+ groupResults = await Promise.all(groupTasks.map(runTask_));
1119
+ }
1120
+
1121
+ results.push(...groupResults);
1122
+
1123
+ // Accumulate token usage estimate for budget tracking
1124
+ for (const r of groupResults) {
1125
+ sessionTokensUsed += estimateTaskTokens(r.task);
1126
+ if (r.finalStatus === 'completed') completedCount++;
1127
+ else if (r.finalStatus === 'failed') failedCount++;
1128
+ else if (r.finalStatus === 'escalated') escalatedCount++;
1129
+ }
1130
+
1131
+ // Bus coordinator: resolve any unresolved blocks after this wave
1132
+ if (enableBus) {
1133
+ await handleBusBlocks(targetDir, squadSlug, sessionId, logger).catch(() => {});
1134
+ }
1135
+
1136
+ // Short pause between waves to allow bus writes to flush
1137
+ if (groups.length > 1) await sleep(100);
1138
+ }
1139
+
1140
+ // ── Record session end in STATE.md ─────────────────────────────────────────
1141
+ await stateManager.recordSessionEnd(targetDir, squadSlug, sessionId, results).catch(() => {});
1142
+
1143
+ // ── 5.1 Automatic Learning Extraction ─────────────────────────────────────
1144
+ if (completedCount > 0 || escalatedCount > 0) {
1145
+ const allBusMessages = enableBus
1146
+ ? await bus.read(targetDir, squadSlug, sessionId).catch(() => [])
1147
+ : [];
1148
+ const allReflections = results
1149
+ .filter((r) => r.reflectionResult)
1150
+ .map((r) => r.reflectionResult);
1151
+
1152
+ const learnings = await extractLearnings(targetDir, squadSlug, sessionId, {
1153
+ busMessages: allBusMessages,
1154
+ taskResults: results,
1155
+ reflectionReports: allReflections
1156
+ }).catch(() => []);
1157
+
1158
+ if (learnings.length > 0) {
1159
+ // Persist learnings to per-agent memory files (Plan 81 §Sprint 4)
1160
+ await persistAgentMemory(targetDir, squadSlug, learnings, results).catch(() => {});
1161
+ // Will be shown in the summary section below
1162
+ results._extractedLearnings = learnings.length;
1163
+ }
1164
+ }
1165
+
1166
+ // ── Session summary ────────────────────────────────────────────────────────
1167
+ const elapsed = Math.round((Date.now() - startedAt) / 1000);
1168
+ const busSummary = enableBus
1169
+ ? await bus.summary(targetDir, squadSlug, sessionId).catch(() => null)
1170
+ : null;
1171
+
1172
+ const summary = {
1173
+ ok: failedCount === 0 && escalatedCount === 0,
1174
+ session_id: sessionId,
1175
+ squad: squadSlug,
1176
+ goal: plan.goal,
1177
+ elapsed_s: elapsed,
1178
+ budget_used: sessionTokensUsed,
1179
+ budget_limit: budget.maxTokensPerSession,
1180
+ tasks: {
1181
+ total: plan.tasks.length,
1182
+ completed: completedCount,
1183
+ failed: failedCount,
1184
+ escalated: escalatedCount
1185
+ },
1186
+ bus: busSummary
1187
+ ? { total_messages: busSummary.total, blocks: busSummary.blocks.length }
1188
+ : null
1189
+ };
1190
+
1191
+ if (options.json) return summary;
1192
+
1193
+ logger.log('');
1194
+ logger.log('── Autorun complete ─────────────────────────────────────────');
1195
+ logger.log(`Session: ${sessionId}`);
1196
+ logger.log(`Elapsed: ${elapsed}s`);
1197
+ logger.log(`Tasks: ${completedCount}/${plan.tasks.length} completed ${failedCount > 0 ? `| ${failedCount} failed` : ''} ${escalatedCount > 0 ? `| ${escalatedCount} escalated` : ''}`);
1198
+ if (budget.maxTokensPerSession !== Infinity) {
1199
+ logger.log(`Tokens: ~${sessionTokensUsed.toLocaleString()} used / ${budget.maxTokensPerSession.toLocaleString()} budget`);
1200
+ }
1201
+ if (busSummary && busSummary.total > 0) {
1202
+ logger.log(`Bus: ${busSummary.total} messages${busSummary.blocks.length > 0 ? ` | ⚠ ${busSummary.blocks.length} block(s)` : ''}`);
1203
+ }
1204
+ if (escalatedCount > 0) {
1205
+ logger.log('');
1206
+ logger.log('⚠ Escalated tasks require coordinator attention:');
1207
+ for (const r of results.filter((r) => r.finalStatus === 'escalated')) {
1208
+ const exhausted = r.workerResult?.gap_closure_exhausted ? ' (gap closure exhausted)' : '';
1209
+ logger.log(` ${r.task.id}: ${r.task.title}${exhausted}`);
1210
+ }
1211
+ }
1212
+ logger.log('');
1213
+ logger.log(`Plan: .aioson/squads/${squadSlug}/sessions/${sessionId}/plan.json`);
1214
+ logger.log(`State: .aioson/squads/${squadSlug}/STATE.md`);
1215
+ if (enableBus) logger.log(`Bus: .aioson/squads/${squadSlug}/sessions/${sessionId}/bus.jsonl`);
1216
+
1217
+ return summary;
1218
+ }
1219
+
1220
+ module.exports = { runSquadAutorun };