@entelligentsia/forgecli 0.10.1 → 0.11.2

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 (161) hide show
  1. package/CHANGELOG.md +78 -0
  2. package/README.md +21 -3
  3. package/dist/CHANGELOG-forge-plugin.md +22 -0
  4. package/dist/extensions/forgecli/add-pipeline.d.ts +19 -0
  5. package/dist/extensions/forgecli/add-pipeline.js +143 -0
  6. package/dist/extensions/forgecli/add-pipeline.js.map +1 -0
  7. package/dist/extensions/forgecli/add-task.d.ts +20 -0
  8. package/dist/extensions/forgecli/add-task.js +154 -0
  9. package/dist/extensions/forgecli/add-task.js.map +1 -0
  10. package/dist/extensions/forgecli/calibrate.d.ts +61 -0
  11. package/dist/extensions/forgecli/calibrate.js +488 -0
  12. package/dist/extensions/forgecli/calibrate.js.map +1 -0
  13. package/dist/extensions/forgecli/fix-bug.d.ts +9 -1
  14. package/dist/extensions/forgecli/fix-bug.js +70 -8
  15. package/dist/extensions/forgecli/fix-bug.js.map +1 -1
  16. package/dist/extensions/forgecli/forge-commands.js +15 -22
  17. package/dist/extensions/forgecli/forge-commands.js.map +1 -1
  18. package/dist/extensions/forgecli/forge-subagent.js +34 -7
  19. package/dist/extensions/forgecli/forge-subagent.js.map +1 -1
  20. package/dist/extensions/forgecli/forge-update-command.d.ts +9 -0
  21. package/dist/extensions/forgecli/forge-update-command.js +106 -7
  22. package/dist/extensions/forgecli/forge-update-command.js.map +1 -1
  23. package/dist/extensions/forgecli/health-check.d.ts +22 -1
  24. package/dist/extensions/forgecli/health-check.js +177 -4
  25. package/dist/extensions/forgecli/health-check.js.map +1 -1
  26. package/dist/extensions/forgecli/hook-dispatcher.d.ts +25 -1
  27. package/dist/extensions/forgecli/hook-dispatcher.js +104 -9
  28. package/dist/extensions/forgecli/hook-dispatcher.js.map +1 -1
  29. package/dist/extensions/forgecli/hooks/check-update.d.ts +81 -0
  30. package/dist/extensions/forgecli/hooks/check-update.js +308 -0
  31. package/dist/extensions/forgecli/hooks/check-update.js.map +1 -0
  32. package/dist/extensions/forgecli/hooks/forge-permissions.d.ts +32 -0
  33. package/dist/extensions/forgecli/hooks/forge-permissions.js +119 -0
  34. package/dist/extensions/forgecli/hooks/forge-permissions.js.map +1 -0
  35. package/dist/extensions/forgecli/hooks/triage-error.d.ts +23 -0
  36. package/dist/extensions/forgecli/hooks/triage-error.js +62 -0
  37. package/dist/extensions/forgecli/hooks/triage-error.js.map +1 -0
  38. package/dist/extensions/forgecli/hooks/write-guard.d.ts +28 -0
  39. package/dist/extensions/forgecli/hooks/write-guard.js +225 -0
  40. package/dist/extensions/forgecli/hooks/write-guard.js.map +1 -0
  41. package/dist/extensions/forgecli/index.js +60 -0
  42. package/dist/extensions/forgecli/index.js.map +1 -1
  43. package/dist/extensions/forgecli/init-context.d.ts +1 -1
  44. package/dist/extensions/forgecli/init-context.js +21 -6
  45. package/dist/extensions/forgecli/init-context.js.map +1 -1
  46. package/dist/extensions/forgecli/materialize.d.ts +16 -0
  47. package/dist/extensions/forgecli/materialize.js +195 -0
  48. package/dist/extensions/forgecli/materialize.js.map +1 -0
  49. package/dist/extensions/forgecli/migrate.d.ts +19 -0
  50. package/dist/extensions/forgecli/migrate.js +258 -0
  51. package/dist/extensions/forgecli/migrate.js.map +1 -0
  52. package/dist/extensions/forgecli/migration-engine.d.ts +111 -0
  53. package/dist/extensions/forgecli/migration-engine.js +533 -0
  54. package/dist/extensions/forgecli/migration-engine.js.map +1 -0
  55. package/dist/extensions/forgecli/quiz-agent.d.ts +17 -0
  56. package/dist/extensions/forgecli/quiz-agent.js +98 -0
  57. package/dist/extensions/forgecli/quiz-agent.js.map +1 -0
  58. package/dist/extensions/forgecli/remove-command.d.ts +17 -0
  59. package/dist/extensions/forgecli/remove-command.js +124 -0
  60. package/dist/extensions/forgecli/remove-command.js.map +1 -0
  61. package/dist/extensions/forgecli/report-bug.d.ts +25 -0
  62. package/dist/extensions/forgecli/report-bug.js +159 -0
  63. package/dist/extensions/forgecli/report-bug.js.map +1 -0
  64. package/dist/extensions/forgecli/retrospective.d.ts +19 -0
  65. package/dist/extensions/forgecli/retrospective.js +156 -0
  66. package/dist/extensions/forgecli/retrospective.js.map +1 -0
  67. package/dist/extensions/forgecli/run-sprint.js +34 -0
  68. package/dist/extensions/forgecli/run-sprint.js.map +1 -1
  69. package/dist/extensions/forgecli/run-task.d.ts +9 -1
  70. package/dist/extensions/forgecli/run-task.js +64 -10
  71. package/dist/extensions/forgecli/run-task.js.map +1 -1
  72. package/dist/extensions/forgecli/session-registry.d.ts +27 -2
  73. package/dist/extensions/forgecli/session-registry.js +52 -1
  74. package/dist/extensions/forgecli/session-registry.js.map +1 -1
  75. package/dist/extensions/forgecli/status-command.d.ts +19 -0
  76. package/dist/extensions/forgecli/status-command.js +140 -0
  77. package/dist/extensions/forgecli/status-command.js.map +1 -0
  78. package/dist/extensions/forgecli/store-query.d.ts +22 -0
  79. package/dist/extensions/forgecli/store-query.js +107 -0
  80. package/dist/extensions/forgecli/store-query.js.map +1 -0
  81. package/dist/extensions/forgecli/store-repair.d.ts +17 -0
  82. package/dist/extensions/forgecli/store-repair.js +123 -0
  83. package/dist/extensions/forgecli/store-repair.js.map +1 -0
  84. package/dist/extensions/forgecli/thread-switcher.js +213 -28
  85. package/dist/extensions/forgecli/thread-switcher.js.map +1 -1
  86. package/dist/extensions/forgecli/update-tools.d.ts +23 -0
  87. package/dist/extensions/forgecli/update-tools.js +136 -0
  88. package/dist/extensions/forgecli/update-tools.js.map +1 -0
  89. package/dist/extensions/forgecli/viewport-theme.js +4 -0
  90. package/dist/extensions/forgecli/viewport-theme.js.map +1 -1
  91. package/dist/forge-payload/.claude-plugin/plugin.json +1 -1
  92. package/dist/forge-payload/.schemas/config.schema.json +83 -0
  93. package/dist/forge-payload/.schemas/migrations.json +2049 -0
  94. package/dist/forge-payload/commands/regenerate.md +17 -1
  95. package/dist/forge-payload/meta/personas/README.md +16 -0
  96. package/dist/forge-payload/meta/personas/meta-architect.md +70 -0
  97. package/dist/forge-payload/meta/personas/meta-bug-fixer.md +73 -0
  98. package/dist/forge-payload/meta/personas/meta-collator.md +72 -0
  99. package/dist/forge-payload/meta/personas/meta-engineer.md +70 -0
  100. package/dist/forge-payload/meta/personas/meta-orchestrator.md +71 -0
  101. package/dist/forge-payload/meta/personas/meta-product-manager.md +82 -0
  102. package/dist/forge-payload/meta/personas/meta-qa-engineer.md +91 -0
  103. package/dist/forge-payload/meta/personas/meta-supervisor.md +92 -0
  104. package/dist/forge-payload/meta/skill-recommendations.md +154 -0
  105. package/dist/forge-payload/meta/skills/meta-architect-skills.md +43 -0
  106. package/dist/forge-payload/meta/skills/meta-bug-fixer-skills.md +43 -0
  107. package/dist/forge-payload/meta/skills/meta-collator-skills.md +41 -0
  108. package/dist/forge-payload/meta/skills/meta-engineer-skills.md +43 -0
  109. package/dist/forge-payload/meta/skills/meta-generic-skills.md +58 -0
  110. package/dist/forge-payload/meta/skills/meta-qa-engineer-skills.md +46 -0
  111. package/dist/forge-payload/meta/skills/meta-supervisor-skills.md +43 -0
  112. package/dist/forge-payload/meta/store-schema/bug.schema.md +71 -0
  113. package/dist/forge-payload/meta/store-schema/event.schema.md +76 -0
  114. package/dist/forge-payload/meta/store-schema/feature.schema.md +65 -0
  115. package/dist/forge-payload/meta/store-schema/sprint.schema.md +64 -0
  116. package/dist/forge-payload/meta/store-schema/task.schema.md +78 -0
  117. package/dist/forge-payload/meta/templates/meta-code-review.md +26 -0
  118. package/dist/forge-payload/meta/templates/meta-plan-review.md +28 -0
  119. package/dist/forge-payload/meta/templates/meta-plan.md +28 -0
  120. package/dist/forge-payload/meta/templates/meta-progress.md +25 -0
  121. package/dist/forge-payload/meta/templates/meta-retrospective.md +28 -0
  122. package/dist/forge-payload/meta/templates/meta-sprint-manifest.md +26 -0
  123. package/dist/forge-payload/meta/templates/meta-sprint-requirements.md +91 -0
  124. package/dist/forge-payload/meta/templates/meta-task-prompt.md +26 -0
  125. package/dist/forge-payload/meta/tool-specs/collate.spec.md +88 -0
  126. package/dist/forge-payload/meta/tool-specs/generation-manifest.spec.md +139 -0
  127. package/dist/forge-payload/meta/tool-specs/manage-config.spec.md +143 -0
  128. package/dist/forge-payload/meta/tool-specs/seed-store.spec.md +91 -0
  129. package/dist/forge-payload/meta/tool-specs/store-cli.spec.md +328 -0
  130. package/dist/forge-payload/meta/tool-specs/validate-store.spec.md +191 -0
  131. package/dist/forge-payload/meta/workflows/_fragments/context-injection.md +75 -0
  132. package/dist/forge-payload/meta/workflows/_fragments/event-emission-schema.md +73 -0
  133. package/dist/forge-payload/meta/workflows/_fragments/finalize.md +13 -0
  134. package/dist/forge-payload/meta/workflows/_fragments/friction-emit.md +73 -0
  135. package/dist/forge-payload/meta/workflows/_fragments/progress-reporting.md +38 -0
  136. package/dist/forge-payload/meta/workflows/_fragments/store-cli-verbs.md +39 -0
  137. package/dist/forge-payload/meta/workflows/meta-approve.md +119 -0
  138. package/dist/forge-payload/meta/workflows/meta-collate.md +89 -0
  139. package/dist/forge-payload/meta/workflows/meta-commit.md +93 -0
  140. package/dist/forge-payload/meta/workflows/meta-enhance.md +286 -0
  141. package/dist/forge-payload/meta/workflows/meta-fix-bug.md +501 -0
  142. package/dist/forge-payload/meta/workflows/meta-implement.md +132 -0
  143. package/dist/forge-payload/meta/workflows/meta-migrate.md +455 -0
  144. package/dist/forge-payload/meta/workflows/meta-orchestrate.md +993 -0
  145. package/dist/forge-payload/meta/workflows/meta-plan-task.md +133 -0
  146. package/dist/forge-payload/meta/workflows/meta-quiz-agent.md +135 -0
  147. package/dist/forge-payload/meta/workflows/meta-retrospective.md +65 -0
  148. package/dist/forge-payload/meta/workflows/meta-review-implementation.md +119 -0
  149. package/dist/forge-payload/meta/workflows/meta-review-plan.md +108 -0
  150. package/dist/forge-payload/meta/workflows/meta-review-sprint-completion.md +65 -0
  151. package/dist/forge-payload/meta/workflows/meta-sprint-intake.md +76 -0
  152. package/dist/forge-payload/meta/workflows/meta-sprint-plan.md +147 -0
  153. package/dist/forge-payload/meta/workflows/meta-update-implementation.md +76 -0
  154. package/dist/forge-payload/meta/workflows/meta-update-plan.md +76 -0
  155. package/dist/forge-payload/meta/workflows/meta-validate.md +111 -0
  156. package/dist/forge-payload/tools/check-structure.cjs +344 -0
  157. package/dist/forge-payload/tools/list-skills.js +76 -0
  158. package/dist/forge-payload/tools/store-cli.cjs +27 -1
  159. package/dist/forge-payload/tools/substitute-placeholders.cjs +60 -8
  160. package/dist/forge-payload/tools/verify-integrity.cjs +86 -0
  161. package/package.json +2 -2
@@ -0,0 +1,993 @@
1
+ ---
2
+ requirements:
3
+ reasoning: High
4
+ context: High
5
+ speed: Medium
6
+ audience: orchestrator-only
7
+ deps:
8
+ personas: [architect, engineer, supervisor, bug-fixer, collator, qa-engineer]
9
+ skills: [architect, engineer, supervisor, generic]
10
+ templates: []
11
+ sub_workflows: [plan_task, implement_plan, review_plan, review_code, fix_bug, architect_approve, commit_task, validate_task]
12
+ kb_docs: [architecture/stack.md]
13
+ context_pack: .forge/cache/context-pack.md
14
+ config_fields: [paths.engineering]
15
+ ---
16
+
17
+ # 🌊 Meta-Workflow: Orchestrate Task
18
+
19
+ ## Purpose
20
+
21
+ Wire the atomic workflows into a pipeline that drives a single task through
22
+ the complete lifecycle. This is the task state machine.
23
+
24
+ ## Pipeline Phases
25
+
26
+ Each phase has:
27
+ - `name` — identifier
28
+ - `agent` — which role executes
29
+ - `workflow` — which workflow file to load
30
+ - `requires` — prerequisite artifact
31
+ - `produces` — output artifact
32
+ - `max_iterations` — revision loop limit (for review phases)
33
+ - `gate_checks` — conditions that must pass before proceeding
34
+
35
+ ## Model Resolution
36
+
37
+ Detect cluster from env vars at session start, then dispatch accordingly:
38
+
39
+ | Env var | Purpose |
40
+ |---------|---------|
41
+ | `ANTHROPIC_DEFAULT_OPUS_MODEL` | What "opus" resolves to |
42
+ | `ANTHROPIC_DEFAULT_SONNET_MODEL` | What "sonnet" resolves to |
43
+ | `ANTHROPIC_DEFAULT_HAIKU_MODEL` | What "haiku" resolves to |
44
+
45
+ - **Single cluster** — all three vars equal (or unset): omit `model` on Agent spawns; subagents inherit the parent.
46
+ - **Tiered cluster** — vars differ: pass `model=tier` (opus/sonnet/haiku) based on ROLE_TIER mapping.
47
+ - **Unknown cluster** — no `ANTHROPIC_DEFAULT_*` vars: pass the canonical model ID from ROLE_TIER_DEFAULTS.
48
+ - **Per-phase override** — `model` field in `config.pipelines` phase takes highest precedence.
49
+
50
+ ### Role-to-Tier Mapping
51
+
52
+ | Role | Tier |
53
+ |------|------|
54
+ | `review-plan`, `review-code`, `validate`, `approve` | opus |
55
+ | `plan`, `implement` | sonnet |
56
+ | `commit`, `writeback` | haiku |
57
+
58
+ Unknown cluster canonical defaults: opus → `claude-opus-4-5`, sonnet → `claude-sonnet-4-6`, haiku → `claude-haiku-4-5`.
59
+
60
+ Phase announcement format: `→ TASK-ID [tier → resolved-model]` (e.g. `→ SPECT-T01 [opus → claude-opus-4-6]`).
61
+ On single cluster, show the model directly. On unknown, show `tier → canonical`.
62
+
63
+ ## Pipeline Resolution
64
+
65
+ The orchestrator supports pluggable pipelines. When starting a task:
66
+
67
+ 1. Read the task manifest from `.forge/store/tasks/{TASK_ID}.json`.
68
+ 2. If `task.pipeline` is set, look up that key in `.forge/config.json` → `pipelines`.
69
+ 3. If found, use the phases defined in that pipeline.
70
+ 4. If `task.pipeline` is not set or the key is not found, use the `default` pipeline
71
+ (either from `config.pipelines.default` or the hardcoded default below).
72
+
73
+ Each phase in a pipeline has:
74
+ - `command` — the slash command to invoke (passed the task ID as argument)
75
+ - `role` — semantic role (`plan`, `review-plan`, `implement`, `review-code`, `approve`, `commit`)
76
+ - `maxIterations` — for review roles, the revision loop limit (default 3)
77
+ - `on_revision` — (optional) command name of the phase to re-invoke on "Revision Required";
78
+ if absent, defaults to the nearest preceding phase whose role is not a review role
79
+
80
+ ## Default Pipeline
81
+
82
+ ```
83
+ plan → review-plan → [loop max 3] → implement → review-code → [loop max 3] → validate → [loop max 3] → approve → writeback → commit
84
+ ```
85
+
86
+ When no `pipelines` section exists in config, the orchestrator uses this
87
+ hardcoded default. Projects that define `config.pipelines.default` override it.
88
+
89
+ ## Context Isolation
90
+
91
+ **Each phase MUST run as a subagent (Agent tool call), NOT inline.**
92
+
93
+ Invoking phases inline accumulates context from every prior phase and task into
94
+ the orchestrator's window. This violates Forge's design principle of keeping
95
+ context light and nimble. By the time a sprint reaches its third or fourth task,
96
+ an inline orchestrator is carrying tens of thousands of tokens of prior work that
97
+ is irrelevant to the current phase.
98
+
99
+ The fix: use the Agent tool to spawn a subagent per phase. Each subagent:
100
+ - Starts with a fresh context window
101
+ - Receives only what it needs: the workflow file path and the task ID
102
+ - Receives a PROJECT_OVERLAY (task-scoped index slice) instead of reading MASTER_INDEX.md directly
103
+ - Writes results to disk (artifacts, task status updates)
104
+ - Returns to the orchestrator, which then reads the verdict from disk
105
+
106
+ The orchestrator itself stays minimal — it only holds the phase loop and event log.
107
+
108
+ ## Token Self-Reporting
109
+
110
+ Each phase subagent is responsible for reporting its own token usage via a sidecar file.
111
+
112
+ **Before returning, every subagent MUST:**
113
+
114
+ 1. Probe token usage for the session: invoke `/cost` if the host runtime
115
+ supports it (Claude Code only); on any other runtime treat as unavailable.
116
+ Do NOT shell out to a `cost-cli.cjs` — there is no such tool.
117
+ 2. Parse the output for the five fields:
118
+ `inputTokens`, `outputTokens`, `cacheReadTokens`, `cacheWriteTokens`, `estimatedCostUSD`.
119
+ 3. Write the usage sidecar via `node "$FORGE_ROOT/tools/store-cli.cjs" emit {sprintId} '{sidecar-json}' --sidecar` with the exact format:
120
+ ```json
121
+ {
122
+ "inputTokens": <integer>,
123
+ "outputTokens": <integer>,
124
+ "cacheReadTokens": <integer>,
125
+ "cacheWriteTokens": <integer>,
126
+ "estimatedCostUSD": <number>
127
+ }
128
+ ```
129
+
130
+ The `eventId` is computed by the orchestrator before spawning and passed in the subagent prompt —
131
+ it follows the format `{ISO_TIMESTAMP}_{TASK_ID}_{role}_{action}` (e.g.
132
+ `20260415T141523000Z_ACME-S02-T03_engineer_implement`).
133
+
134
+ The leading underscore on the sidecar filename marks it as ephemeral — `validate-store.cjs` skips
135
+ files prefixed with `_`, so the sidecar will never be treated as a real event record. If `/cost` is
136
+ unavailable or token data cannot be parsed, skip writing the sidecar silently — the orchestrator
137
+ handles missing sidecars gracefully (see Execution Algorithm below).
138
+
139
+ ## Role-to-Noun Mapping
140
+
141
+ The orchestrator resolves persona and skill file lookups using **noun-based**
142
+ filenames, not role-literal filenames. A role like `plan` maps to the noun
143
+ `engineer`, so the persona file is `engineer.md`, not `plan.md`.
144
+
145
+ ```
146
+ ROLE_TO_NOUN = {
147
+ "plan": "engineer",
148
+ "implement": "engineer",
149
+ "update-plan": "engineer",
150
+ "update-impl": "engineer",
151
+ "commit": "engineer",
152
+ "review-plan": "supervisor",
153
+ "review-code": "supervisor",
154
+ "validate": "qa-engineer",
155
+ "approve": "architect",
156
+ "writeback": "collator",
157
+ }
158
+ ```
159
+
160
+ The `.get(key, fallback)` pattern preserves the old role-literal behaviour for
161
+ any role not yet in the table, which is a safe degradation path for custom
162
+ pipeline roles.
163
+
164
+ ## Persona Injection Modes
165
+
166
+ Subagent prompts include a **role block** that tells the agent who it is
167
+ and what capabilities it has. Two modes are supported, selected by the
168
+ `FORGE_PROMPT_MODE` environment variable:
169
+
170
+ | Mode | Behaviour | Default |
171
+ |------|-----------|---------|
172
+ | `reference` | Compact summary from `.forge/cache/persona-pack.json`, plus a file_ref pointer to the full persona/skill definitions. | ✅ |
173
+ | `inline` | Legacy: inject the full verbatim persona and skill file contents. Kept for one version as a rollback path. | |
174
+
175
+ The pack is built by `/forge:regenerate` and `/forge:materialize` via
176
+ `forge/tools/build-persona-pack.cjs`. It compiles YAML frontmatter from
177
+ `$FORGE_ROOT/meta/personas/meta-*.md` and `$FORGE_ROOT/meta/skills/meta-*.md`
178
+ into `.forge/cache/persona-pack.json`.
179
+
180
+ ### Helper: `compose_role_block(persona_noun)`
181
+
182
+ ```
183
+ def compose_role_block(persona_noun):
184
+ mode = os.environ.get("FORGE_PROMPT_MODE", "reference")
185
+
186
+ if mode == "inline":
187
+ # Legacy behaviour — full persona + skill prose inline.
188
+ persona_content = read_file(f".forge/personas/{persona_noun}.md")
189
+ skill_content = read_file(f".forge/skills/{persona_noun}-skills.md")
190
+ return f"{persona_content}\n\n{skill_content}"
191
+
192
+ # Reference mode (default) — compact summary from the pack.
193
+ pack = read_json(".forge/cache/persona-pack.json")
194
+ persona = pack["personas"].get(persona_noun)
195
+ skill = pack["skills"].get(f"{persona_noun}-skills")
196
+
197
+ if not persona:
198
+ # Fail loud rather than silently degrade. Missing pack entry is a
199
+ # regeneration bug and should be reported via /forge:report-bug.
200
+ raise OrchestratorError(
201
+ f"persona '{persona_noun}' not in persona-pack. "
202
+ "Run /forge:regenerate to rebuild the pack."
203
+ )
204
+
205
+ lines = [
206
+ f"You are acting as the {persona['role']}.",
207
+ "",
208
+ f"Persona: {persona['id']} — {persona['summary']}",
209
+ "",
210
+ "Your responsibilities:",
211
+ ]
212
+ for r in persona.get("responsibilities", []):
213
+ lines.append(f"- {r}")
214
+ if persona.get("outputs"):
215
+ lines.append("")
216
+ lines.append(f"Your outputs: {', '.join(persona['outputs'])}")
217
+
218
+ if skill:
219
+ lines.append("")
220
+ lines.append("Skill capabilities you have available:")
221
+ for c in skill.get("capabilities", []):
222
+ lines.append(f"- {c}")
223
+
224
+ lines.append("")
225
+ lines.append(
226
+ f"Full persona definition: {persona['file_ref']}. "
227
+ + (f"Full skill definition: {skill['file_ref']}. " if skill else "")
228
+ + "The summary above is authoritative. If insufficient, escalate — "
229
+ + "do not read the full persona or skill file."
230
+ )
231
+ return "\n".join(lines)
232
+ ```
233
+
234
+ **Rollback:** set `FORGE_PROMPT_MODE=inline`. No persisted state to revert.
235
+ The `inline` branch will be removed one version after `reference` ships.
236
+
237
+ ## Execution Algorithm
238
+
239
+ The orchestrator MUST follow this procedure exactly. Do not deviate.
240
+
241
+ ```
242
+ # --- Persona symbol lookup (emoji, name, tagline) ---
243
+ PERSONA_MAP = {
244
+ "plan": ("🌱", "Engineer", "I plan what will be built before any code is written."),
245
+ "implement": ("🌱", "Engineer", "I build what was planned. I do not move forward until the code is clean."),
246
+ "update-plan": ("🌱", "Engineer", "I address what the Supervisor found. No more, no less."),
247
+ "update-impl": ("🌱", "Engineer", "I address what the Supervisor found. No more, no less."),
248
+ "commit": ("🌱", "Engineer", "I close out completed work with a clean, honest commit."),
249
+ "review-plan": ("🌿", "Supervisor", "I review before things move forward. I read the actual task prompt, not just the plan."),
250
+ "review-code": ("🌿", "Supervisor", "I review before things move forward. I read the actual code, not the report."),
251
+ "validate": ("🍵", "QA Engineer", "I validate against what was promised. The code compiling is not enough."),
252
+ "approve": ("🗻", "Architect", "I hold the shape of the whole. I give final sign-off before commit."),
253
+ "writeback": ("🍃", "Collator", "I gather what exists and arrange it into views."),
254
+ }
255
+
256
+ # --- Banner identity map (banner name per phase role) ---
257
+ # Maps each role to a banner in forge/tools/banners.cjs.
258
+ # Displayed by the orchestrator ONLY (badge before spawn, exit signal after return).
259
+ # Subagents do NOT display banners — the orchestrator owns phase announcements.
260
+ BANNER_MAP = {
261
+ "plan": "forge",
262
+ "implement": "forge",
263
+ "update-plan": "forge",
264
+ "update-impl": "forge",
265
+ "commit": "forge",
266
+ "review-plan": "oracle",
267
+ "review-code": "oracle",
268
+ "validate": "lumen",
269
+ "approve": "north",
270
+ "writeback": "drift",
271
+ }
272
+
273
+ for each task in dependency_sorted(tasks):
274
+ # --- Pre-task status guard ---
275
+ # If a task is already blocked or escalated from a prior sprint/run,
276
+ # skip it entirely rather than attempting any phase.
277
+ task_record = read_json(f".forge/store/tasks/{task.taskId}.json")
278
+ if task_record and task_record.get("status") in ("blocked", "escalated"):
279
+ print(f" ⚠ {task.taskId} — status is {task_record['status']}, skipping\n")
280
+ emit_event(task, phase=None, action="task_skipped",
281
+ notes=f"task status is {task_record['status']}")
282
+ continue
283
+
284
+ phases = resolve_pipeline(task) # from config.pipelines or default
285
+ iteration_counts = {} # keyed by phase command name
286
+ retry_count = {} # keyed by phase command name (subagent retry tracking)
287
+ i = 0
288
+
289
+ # --- Detect execution cluster from env vars (see Model Resolution) ---
290
+ opus_model = env("ANTHROPIC_DEFAULT_OPUS_MODEL", "")
291
+ sonnet_model = env("ANTHROPIC_DEFAULT_SONNET_MODEL", "")
292
+ haiku_model = env("ANTHROPIC_DEFAULT_HAIKU_MODEL", "")
293
+ if opus_model and opus_model == sonnet_model == haiku_model:
294
+ cluster = "single"
295
+ resolved_model = opus_model # all tiers same model
296
+ elif opus_model:
297
+ cluster = "tiered"
298
+ resolved_model = None # each tier resolves differently
299
+ else:
300
+ cluster = "unknown"
301
+ resolved_model = env("CLAUDE_CODE_SUBAGENT_MODEL", "unknown")
302
+
303
+ # --- Role-to-tier mapping for tiered cluster dispatch ---
304
+ ROLE_TIER = {
305
+ "review-plan": "opus",
306
+ "review-code": "opus",
307
+ "validate": "opus",
308
+ "approve": "opus",
309
+ "plan": "sonnet",
310
+ "implement": "sonnet",
311
+ "commit": "haiku",
312
+ "writeback": "haiku",
313
+ }
314
+
315
+ # --- Clear progress log for this sprint ---
316
+ progress_log_path = f".forge/store/events/{sprint_id}/progress.log"
317
+ run_bash(f'FORGE_ROOT=$(node -e "console.log(require(\'./.forge/config.json\').paths.forgeRoot)") && node "$FORGE_ROOT/tools/store-cli.cjs" progress-clear {sprint_id}')
318
+
319
+ while i < len(phases):
320
+ phase = phases[i]
321
+
322
+ # --- Resolve model for display and dispatch (see Model Resolution) ---
323
+ if phase.model: # per-phase override from config
324
+ display_model = phase.model
325
+ dispatch_model = phase.model # pass override to Agent tool
326
+ if env(f"ANTHROPIC_DEFAULT_{phase.model.upper()}_MODEL"):
327
+ resolved = env(f"ANTHROPIC_DEFAULT_{phase.model.upper()}_MODEL")
328
+ display_model = f"{phase.model} → {resolved}"
329
+ elif cluster == "single" and resolved_model:
330
+ display_model = resolved_model
331
+ dispatch_model = None # inherit parent model
332
+ elif cluster == "tiered":
333
+ tier = ROLE_TIER.get(phase.role, "sonnet")
334
+ resolved = env(f"ANTHROPIC_DEFAULT_{tier.upper()}_MODEL", tier)
335
+ display_model = f"{tier} → {resolved}" if resolved != tier else tier
336
+ dispatch_model = tier # pass tier name, Claude Code resolves
337
+ else:
338
+ # Unknown cluster: no ANTHROPIC_DEFAULT_*_MODEL vars set.
339
+ # Fall back to ROLE_TIER with canonical model defaults so subagents
340
+ # run on a predictable model instead of inheriting the orchestrator's own.
341
+ ROLE_TIER_DEFAULTS = {
342
+ "opus": "claude-opus-4-5",
343
+ "sonnet": "claude-sonnet-4-6",
344
+ "haiku": "claude-haiku-4-5",
345
+ }
346
+ tier = ROLE_TIER.get(phase.role, "sonnet")
347
+ canonical = ROLE_TIER_DEFAULTS[tier]
348
+ display_model = f"{tier} → {canonical}"
349
+ dispatch_model = canonical # pass full model id to Agent tool
350
+
351
+ # --- Compute eventId before spawning so the subagent can name its sidecar ---
352
+ start_ts = current_iso_timestamp() # e.g. "20260415T141523000Z"
353
+ event_id = f"{start_ts}_{task_id}_{phase.role}_{phase.action}"
354
+ sidecar_path = f".forge/store/events/{sprint_id}/_{event_id}_usage.json" # used by merge-sidecar
355
+
356
+ # --- Compute agent name for progress IPC ---
357
+ persona_noun = ROLE_TO_NOUN.get(phase.role, phase.role)
358
+ iteration = iteration_counts.get(phase.command, 0) + 1
359
+ agent_name = f"{task_id}:{persona_noun}:{phase.role}:{iteration}"
360
+
361
+ # --- Announce phase with identity banner (badge) + task context ---
362
+ emoji, persona_name, tagline = PERSONA_MAP.get(phase.role, ("🌊", "Orchestrator", "I move tasks through their lifecycle."))
363
+ banner_name = BANNER_MAP.get(phase.role, "forge")
364
+ run_bash(f'FORGE_ROOT=$(node -e "console.log(require(\'./.forge/config.json\').paths.forgeRoot)") && node "$FORGE_ROOT/tools/banners.cjs" --badge {banner_name}')
365
+ print(f" → {task_id} [{display_model}]\n")
366
+
367
+ # --- Start progress Monitor before spawning subagent ---
368
+ # The Monitor streams lines from the progress log as the subagent works.
369
+ # New lines arrive as notifications while the Agent tool blocks on the subagent.
370
+ start_monitor(
371
+ command=f"tail -n +1 -F {progress_log_path} 2>/dev/null || true",
372
+ description=f"Progress: {agent_name}",
373
+ persistent=False
374
+ )
375
+
376
+ # --- Pre-flight gate check (see Phase Gates below) ---
377
+ # Resolve FORGE_ROOT once so the CLI shim can locate the gate parser.
378
+ FORGE_ROOT = resolve_forge_root()
379
+ preflight_result = run_bash(
380
+ f'node "$FORGE_ROOT/tools/preflight-gate.cjs" --phase {phase.role} --task {task_id}'
381
+ )
382
+ if preflight_result.exit_code == 1:
383
+ # Gate failed: halt the orchestrator loop for THIS task. Do not retry,
384
+ # do not spawn. Missing prerequisites are listed on stderr.
385
+ print(f" ✗ {task_id} {phase.role} — gate failed\n{preflight_result.stderr}")
386
+ append_progress(progress_log_path, f"❌ Gate failed for {phase.role}: {preflight_result.stderr}")
387
+ emit_event(task, phase, action="gate_failed", notes=preflight_result.stderr)
388
+ # ---- ESCALATION (mandatory hard stop — do NOT continue) ----
389
+ run_bash(f'node "$FORGE_ROOT/tools/store-cli.cjs" update-status task {task_id} status escalated')
390
+ emit_event(task, phase, eventId=event_id, iteration=iteration,
391
+ action="escalated", verdict="escalated",
392
+ notes=f"gate_failed: {preflight_result.stderr}")
393
+ print(f" ⚠ Task {task_id} escalated: gate_failed: {preflight_result.stderr}\n")
394
+ print(f" Review artifact: {artifact_path}\n")
395
+ print(f" Resume with: /{phase.command} {task_id} after addressing the issues.\n")
396
+ break # stop processing this task
397
+ elif preflight_result.exit_code == 2:
398
+ # Misconfiguration (unknown phase, malformed gates block). Fail loud.
399
+ print(f" ⚠ {task_id} {phase.role} — gate misconfigured\n{preflight_result.stderr}")
400
+ # ---- ESCALATION (mandatory hard stop — do NOT continue) ----
401
+ run_bash(f'node "$FORGE_ROOT/tools/store-cli.cjs" update-status task {task_id} status escalated')
402
+ emit_event(task, phase, eventId=event_id, iteration=iteration,
403
+ action="escalated", verdict="escalated",
404
+ notes=f"gate_misconfigured: {preflight_result.stderr}")
405
+ print(f" ⚠ Task {task_id} escalated: gate_misconfigured: {preflight_result.stderr}\n")
406
+ print(f" Review artifact: {artifact_path}\n")
407
+ print(f" Resume with: /{phase.command} {task_id} after addressing the issues.\n")
408
+ break
409
+
410
+ # --- Invoke phase as subagent (fresh context per phase) ---
411
+ emit_event(task, phase, eventId=event_id, iteration=iteration, action="start")
412
+
413
+ # Symmetric Injection Assembly: Persona -> Skill -> Workflow
414
+ # Mode is governed by FORGE_PROMPT_MODE (default: "reference").
415
+ # See "Persona injection modes" below for the full helper definition.
416
+ role_block = compose_role_block(persona_noun)
417
+
418
+ # --- Compose prior-phase summary block (delta: last 3 phases only) ---
419
+ # <!-- See _fragments/context-injection.md for canonical definition -->
420
+ summary_block = compose_summary_block(task_id, record_type="task") if phase.context.prior_summaries != "none" else ""
421
+
422
+ # --- Compose architecture context block (conditional on phase.context.architecture) ---
423
+ # <!-- See _fragments/context-injection.md for canonical definition -->
424
+ architecture_block = (
425
+ compose_architecture_block(".forge/cache/context-pack.md", ".forge/cache/context-pack.json")
426
+ if phase.context.architecture else ""
427
+ )
428
+
429
+ # --- Materialize project overlay (replaces MASTER_INDEX.md read in subagent) ---
430
+ overlay_result = run_bash(
431
+ f'node "$FORGE_ROOT/tools/build-overlay.cjs" --task {task_id} --format md'
432
+ )
433
+ overlay_md = overlay_result.stdout if overlay_result.exit_code == 0 else ""
434
+
435
+ # --- Load finalize fragment (token reporting contract) ---
436
+ finalize_fragment = read_file(f"{FORGE_ROOT}/meta/workflows/_fragments/finalize.md") if file_exists(f"{FORGE_ROOT}/meta/workflows/_fragments/finalize.md") else ""
437
+
438
+ spawn_kwargs = dict(
439
+ prompt=(
440
+ f"Append progress entries to {progress_log_path} via store-cli "
441
+ f"(agent: {agent_name}, banner: {banner_name}) — see _fragments/progress-reporting.md.\n\n"
442
+ f"---\n\n"
443
+ f"{architecture_block}"
444
+ f"{summary_block}"
445
+ f"{role_block}\n\n"
446
+ f"### Project Context\n"
447
+ f"{overlay_md}\n\n"
448
+ f"### Current Working Context\n"
449
+ f"- Sprint Root: {sprint_root_path}\n"
450
+ f"- Task Root: {task_root_path}\n"
451
+ f"- Store Root: {store_root_path}\n\n"
452
+ f"Read `.forge/workflows/{phase.workflow}` and follow it. Task ID: {task_id}.\n\n"
453
+ f"{finalize_fragment}"
454
+ ),
455
+ description=f"{emoji} {persona_name} — {phase.name} for {task_id}",
456
+ )
457
+ if dispatch_model:
458
+ spawn_kwargs["model"] = dispatch_model
459
+ spawn_subagent(**spawn_kwargs)
460
+ # Subagent reads all context from disk, does its work, writes artifacts/status to disk, then exits.
461
+
462
+ # --- Stop progress Monitor ---
463
+ stop_monitor(progress_log_path)
464
+
465
+ # --- Subagent response validation (retry once, escalate on second failure) ---
466
+ # The subagent must produce a usable result. Three failure classes:
467
+ # 1. Empty response: subagent returned nothing or whitespace-only output
468
+ # 2. Subagent error: subagent exited non-zero (crash, OOM, tool error)
469
+ # 3. Timeout: subagent did not return within the session timeout
470
+ #
471
+ # On first failure: retry once with a simplified prompt that strips
472
+ # non-essential context (summary block, architecture block) and adds
473
+ # a direct instruction to produce a verdict or error report.
474
+ # On second failure: escalate to human — do NOT continue the phase loop.
475
+
476
+ if subagent_failed_or_empty(result):
477
+ if retry_count.get(phase.command, 0) == 0:
478
+ # First failure: retry with simplified prompt
479
+ retry_count[phase.command] = 1
480
+ print(f" ⚠ {task_id} {phase.role} — subagent response empty or errored, retrying with simplified prompt\n")
481
+ emit_event(task, phase, action="subagent_retry",
482
+ notes=f"first failure: {subagent_failure_reason(result)}")
483
+
484
+ # Simplify: remove summary_block and architecture_block from prompt
485
+ simplified_kwargs = dict(spawn_kwargs)
486
+ simplified_kwargs["prompt"] = (
487
+ f"### Progress Reporting\n"
488
+ f"- Agent name: {agent_name}\n"
489
+ f"- Progress log: {progress_log_path}\n"
490
+ f"- Banner key: {banner_name}\n\n"
491
+ f"Append progress entries as you work.\n\n"
492
+ f"---\n\n"
493
+ f"{role_block}\n\n"
494
+ f"### Current Working Context\n"
495
+ f"- Sprint Root: {sprint_root_path}\n"
496
+ f"- Task Root: {task_root_path}\n"
497
+ f"- Store Root: {store_root_path}\n\n"
498
+ f"Read `.forge/workflows/{phase.workflow}` and follow it. Task ID: {task_id}.\n\n"
499
+ f"{overlay_md}\n\n"
500
+ f"IMPORTANT: You MUST produce a result. If the workflow cannot complete, "
501
+ f"write a verdict or error report to the expected artifact path and return."
502
+ )
503
+ spawn_subagent(**simplified_kwargs)
504
+ stop_monitor(progress_log_path)
505
+
506
+ # Re-validate the retry result
507
+ if subagent_failed_or_empty(result):
508
+ # Second failure: escalate
509
+ print(f" ✗ {task_id} {phase.role} — subagent failed after retry, escalating\n")
510
+ emit_event(task, phase, action="subagent_escalated",
511
+ notes=f"second failure: {subagent_failure_reason(result)}")
512
+ # ---- ESCALATION (mandatory hard stop — do NOT continue) ----
513
+ run_bash(f'node "$FORGE_ROOT/tools/store-cli.cjs" update-status task {task_id} status escalated')
514
+ emit_event(task, phase, eventId=event_id, iteration=iteration,
515
+ action="escalated", verdict="escalated",
516
+ notes=f"subagent failed after retry: {subagent_failure_reason(result)}")
517
+ print(f" ⚠ Task {task_id} escalated: subagent {phase.role} failed after retry — {subagent_failure_reason(result)}\n")
518
+ print(f" Resume with: /{phase.command} {task_id} after addressing the issues.\n")
519
+ break
520
+ else:
521
+ # Already retried once — this is the second failure
522
+ print(f" ✗ {task_id} {phase.role} — subagent failed after retry, escalating\n")
523
+ emit_event(task, phase, action="subagent_escalated",
524
+ notes=f"second failure: {subagent_failure_reason(result)}")
525
+ # ---- ESCALATION (mandatory hard stop — do NOT continue) ----
526
+ run_bash(f'node "$FORGE_ROOT/tools/store-cli.cjs" update-status task {task_id} status escalated')
527
+ emit_event(task, phase, eventId=event_id, iteration=iteration,
528
+ action="escalated", verdict="escalated",
529
+ notes=f"subagent failed after retry: {subagent_failure_reason(result)}")
530
+ print(f" ⚠ Task {task_id} escalated: subagent {phase.role} failed after retry — {subagent_failure_reason(result)}\n")
531
+ print(f" Resume with: /{phase.command} {task_id} after addressing the issues.\n")
532
+ break
533
+
534
+ # --- Sidecar merge: merge token usage written by subagent via custodian ---
535
+ # The subagent wrote the sidecar via node "$FORGE_ROOT/tools/store-cli.cjs" emit {sprintId} '{sidecar-json}' --sidecar
536
+ # Merge the sidecar into the canonical event and delete the sidecar file
537
+ FORGE_ROOT = resolve_forge_root()
538
+ run: node "$FORGE_ROOT/tools/store-cli.cjs" merge-sidecar {sprint_id} {event_id}
539
+ # merge-sidecar reads the sidecar, merges token fields into the canonical event, and deletes the sidecar
540
+ # If the sidecar does not exist, merge-sidecar exits 1 — treat as non-fatal (subagent may have skipped it)
541
+ emit_event(task, phase, action="complete")
542
+
543
+ # --- Phase-exit signal ---
544
+ # Non-review phases always advance with a completion signal
545
+ if phase.role not in ("review-plan", "review-code", "validate"):
546
+ print(f" ✓ {task_id} {phase.role} — completed\n")
547
+ i += 1
548
+ # Compact context: all state is on disk; preserve loop bookkeeping in the summary
549
+ print(f"[checkpoint] task={task_id} sprint={sprint_id} phase_index={i} iterations={iteration_counts}")
550
+ /compact
551
+ continue
552
+
553
+ # --- Review phase: detect verdict via parse-verdict.cjs (see Verdict Detection below) ---
554
+ # The CLI returns exit 0/1/2 for approved/revision/unknown. Never pattern-match
555
+ # the **Verdict:** line manually — the closed vocabulary lives in the tool.
556
+ verdict_result = run_bash(
557
+ f'node "$FORGE_ROOT/tools/parse-verdict.cjs" {review_artifact_path(phase, task)}'
558
+ )
559
+ if verdict_result.exit_code == 0:
560
+ verdict = "Approved"
561
+ elif verdict_result.exit_code == 1:
562
+ verdict = "Revision Required"
563
+ else:
564
+ # exit 2: malformed, missing verdict line, or missing artifact. Never guess.
565
+ print(f" ⚠ {task_id} {phase.role} — verdict_malformed, escalating\n")
566
+ emit_event(task, phase, action="verdict_malformed",
567
+ notes=f"parse-verdict exit={verdict_result.exit_code}")
568
+ # ---- ESCALATION (mandatory hard stop — do NOT continue) ----
569
+ run_bash(f'node "$FORGE_ROOT/tools/store-cli.cjs" update-status task {task_id} status escalated')
570
+ emit_event(task, phase, eventId=event_id, iteration=iteration,
571
+ action="escalated", verdict="escalated",
572
+ notes="verdict_malformed: review artifact missing or verdict line unparseable")
573
+ print(f" ⚠ Task {task_id} escalated: verdict_malformed — review artifact missing or verdict line unparseable\n")
574
+ print(f" Review artifact: {review_artifact_path(phase, task)}\n")
575
+ print(f" Resume with: /{phase.command} {task_id} after addressing the issues.\n")
576
+ break
577
+
578
+ if verdict == "Approved":
579
+ print(f" ✓ {task_id} {phase.role} — Approved\n")
580
+ i += 1 # advance to next phase
581
+ # Compact context: all state is on disk; preserve loop bookkeeping in the summary
582
+ print(f"[checkpoint] task={task_id} sprint={sprint_id} phase_index={i} iterations={iteration_counts}")
583
+ /compact
584
+
585
+ elif verdict == "Revision Required":
586
+ iteration_counts[phase.command] = iteration_counts.get(phase.command, 0) + 1
587
+ print(f" ↻ {task_id} {phase.role} — Revision Required (iteration {iteration_counts[phase.command]})\n")
588
+
589
+ if iteration_counts[phase.command] >= phase.maxIterations: # default 3
590
+ # ---- ESCALATION (mandatory hard stop — do NOT continue) ----
591
+ run_bash(f'node "$FORGE_ROOT/tools/store-cli.cjs" update-status task {task_id} status escalated')
592
+ emit_event(task, phase, eventId=event_id, iteration=iteration,
593
+ action="escalated", verdict="escalated",
594
+ notes="max iterations reached")
595
+ print(f" ⚠ Task {task_id} escalated: max iterations reached\n")
596
+ print(f" Review artifact: {review_artifact_path(phase, task)}\n")
597
+ print(f" Resume with: /{phase.command} {task_id} after addressing the issues.\n")
598
+ break
599
+ break # stop processing this task
600
+
601
+ # Route back to the revision target
602
+ target = phase.on_revision or nearest_preceding_non_review(phases, i)
603
+ i = index_of(phases, target) # loop back
604
+ # Compact context: all state is on disk; preserve loop bookkeeping in the summary
605
+ print(f"[checkpoint] task={task_id} sprint={sprint_id} phase_index={i} iterations={iteration_counts}")
606
+ /compact
607
+
608
+ # No `else:` branch needed — parse-verdict.cjs already exhausts the
609
+ # possibilities (approved | revision | verdict_malformed), and the
610
+ # malformed case is handled above before this if/elif chain.
611
+ ```
612
+
613
+ ## Agent Naming Convention
614
+
615
+ Each subagent is assigned a structured name at spawn time:
616
+
617
+ ```
618
+ {taskId}:{persona_noun}:{phase.role}:{iteration}
619
+ ```
620
+
621
+ | Component | Source | Example |
622
+ |-----------|--------|---------|
623
+ | `taskId` | Task ID from manifest | `FORGE-S09-T01` |
624
+ | `persona_noun` | `ROLE_TO_NOUN` mapping | `engineer`, `supervisor`, `qa-engineer` |
625
+ | `phase.role` | Pipeline phase role | `plan`, `review-plan`, `implement` |
626
+ | `iteration` | 1-based revision count for this phase | `1`, `2`, `3` |
627
+
628
+ Examples:
629
+
630
+ - `FORGE-S09-T01:engineer:plan:1` — First plan attempt for T01
631
+ - `FORGE-S09-T01:supervisor:review-plan:1` — First plan review for T01
632
+ - `FORGE-S09-T01:engineer:update-impl:2` — Second implementation revision for T01
633
+
634
+ The agent name is passed in the subagent prompt and used in every progress log
635
+ entry the subagent writes. It provides identity and traceability for mid-task
636
+ feedback.
637
+
638
+ ## Progress Reporting
639
+
640
+ <!-- See _fragments/progress-reporting.md for canonical definition -->
641
+ > See `_fragments/progress-reporting.md` for the full progress log format and `store-cli progress` command reference.
642
+
643
+ Log path: `.forge/store/events/{sprintId}/progress.log`. Format: `{ISO_TIMESTAMP}|{agent_name}|{banner_key}|{status}|{detail}`. Clear at task start: `store-cli progress-clear {sprintId}`.
644
+
645
+ ## Phase-Exit Signals
646
+
647
+ After each subagent returns, the orchestrator prints a phase-exit signal:
648
+
649
+ | Outcome | Format |
650
+ |---------|--------|
651
+ | Non-review phase completed | ` ✓ {task_id} {phase_role} — completed` |
652
+ | Review verdict: Approved | ` ✓ {task_id} {phase_role} — Approved` |
653
+ | Review verdict: Revision Required | ` ↻ {task_id} {phase_role} — Revision Required (iteration {n})` |
654
+ | Escalated | ` ⚠ {task_id} {phase_role} — escalated to human` |
655
+
656
+ Examples:
657
+
658
+ ```
659
+ ✓ FORGE-S09-T01 plan — completed
660
+ ✓ FORGE-S09-T01 review-plan — Approved
661
+ ↻ FORGE-S09-T01 review-plan — Revision Required (iteration 2)
662
+ ⚠ FORGE-S09-T01 validate — escalated to human
663
+ ```
664
+
665
+ ## Verdict Detection
666
+
667
+ After each review phase completes, the orchestrator MUST read the verdict
668
+ before branching. Do not infer the verdict from conversation context alone —
669
+ always read the artifact.
670
+
671
+ | Phase role | Artifact to read | Verdict field |
672
+ |---------------|---------------------------------------------------------------------------|------------------------------|
673
+ | `review-plan` | `{engineering}/sprints/{sprintDir}/{taskDir}/PLAN_REVIEW.md` | Line matching `**Verdict:**` |
674
+ | `review-code` | `{engineering}/sprints/{sprintDir}/{taskDir}/CODE_REVIEW.md` | Line matching `**Verdict:**` |
675
+ | `validate` | `{engineering}/sprints/{sprintDir}/{taskDir}/VALIDATION_REPORT.md` | Line matching `**Verdict:**` |
676
+
677
+ The verdict line format is:
678
+
679
+ ```
680
+ **Verdict:** Approved
681
+ ```
682
+ or
683
+ ```
684
+ **Verdict:** Revision Required
685
+ ```
686
+
687
+ **Parse the verdict via `parse-verdict.cjs`** — do NOT pattern-match the
688
+ line manually. The tool enforces a closed verdict vocabulary so typos, case
689
+ drift, and reviewer prose cannot cause silent misclassification:
690
+
691
+ ```
692
+ FORGE_ROOT = resolve_forge_root()
693
+ result = run_bash(f'node "$FORGE_ROOT/tools/parse-verdict.cjs" {artifact_path}')
694
+ # exit 0 → approved (stdout "approved")
695
+ # exit 1 → revision (stdout "revision")
696
+ # exit 2 → unknown/malformed/missing (stdout "unknown")
697
+ ```
698
+
699
+ Recognised values (case-insensitive):
700
+
701
+ - **approved** — `Approved`, `Approve`, `[Approved]`
702
+ - **revision** — `Revision Required`, `Revision`, `Needs Revision`, `Changes Requested`
703
+
704
+ Anything else — including free-form prose, missing bold markers, a missing
705
+ verdict line, or a missing artifact — yields exit 2. Do NOT treat unknown
706
+ as approved or revision; halt the loop and escalate via `verdict_malformed`.
707
+
708
+ ## Escalation Procedure
709
+
710
+ > **NOTE:** The Escalation Procedure is inlined at every call site in the
711
+ > Execution Algorithm. This section remains as a reference. When adding new
712
+ > escalation points, inline the full procedure — do NOT call `escalate_to_human()`
713
+ > as a bare function name.
714
+
715
+ When escalating to the human:
716
+
717
+ 1. Update task status via `node "$FORGE_ROOT/tools/store-cli.cjs" update-status task {taskId} status escalated`
718
+ 2. Emit a final event with `verdict: "escalated"` and `notes` explaining the reason
719
+ 3. Output a clear message:
720
+ ```
721
+ ⚠ Task {TASK_ID} escalated: {reason}
722
+ Review artifact: {artifact_path}
723
+ Resume with: /{phase.command} {TASK_ID} after addressing the issues.
724
+ ```
725
+ 4. Stop processing this task. Continue to the next task in the sprint.
726
+
727
+ ## Phase Gates
728
+
729
+ Declarative pre-flight gates for each phase. The orchestrator evaluates these
730
+ via `forge/tools/preflight-gate.cjs` **before** every subagent spawn. A failing
731
+ gate halts the loop for this task — no retry, no fall-through to the subagent,
732
+ no silent recovery. Gates are data, not prose: the grammar is defined in
733
+ `forge/tools/parse-gates.cjs` and validated by its test suite.
734
+
735
+ Grammar (one directive per line):
736
+ - `artifact <path> [min=<bytes>]` — file must exist and meet size floor. Path
737
+ templates: `{sprint}` → sprintId, `{task}` → task suffix, `{bug}` → bugId.
738
+ - `require <field> <op> <value>` — predicate must hold. Ops: `==`, `!=`,
739
+ `in [v1, v2, ...]`. Fields are dotted paths against the store record, e.g.
740
+ `task.status`.
741
+ - `forbid <field> <op> <value>` — predicate must NOT hold.
742
+ - `after <phase> = <approved|revision>` — predecessor phase's review artifact
743
+ must carry the stated verdict (parsed by `parse-verdict.cjs`).
744
+
745
+ ```gates phase=plan
746
+ forbid task.status == committed
747
+ forbid task.status == abandoned
748
+ forbid task.status == blocked
749
+ forbid task.status == escalated
750
+ ```
751
+
752
+ ```gates phase=implement
753
+ artifact {engineering}/sprints/{sprint}/{task}/PLAN.md min=200
754
+ after review-plan = approved
755
+ forbid task.status == committed
756
+ forbid task.status == blocked
757
+ forbid task.status == escalated
758
+ ```
759
+
760
+ ```gates phase=review-plan
761
+ artifact {engineering}/sprints/{sprint}/{task}/PLAN.md min=200
762
+ forbid task.status == blocked
763
+ forbid task.status == escalated
764
+ ```
765
+
766
+ ```gates phase=review-code
767
+ after review-plan = approved
768
+ forbid task.status == blocked
769
+ forbid task.status == escalated
770
+ ```
771
+
772
+ ```gates phase=validate
773
+ after review-code = approved
774
+ forbid task.status == blocked
775
+ forbid task.status == escalated
776
+ ```
777
+
778
+ ```gates phase=approve
779
+ after review-code = approved
780
+ forbid task.status == blocked
781
+ forbid task.status == escalated
782
+ ```
783
+
784
+ ```gates phase=commit
785
+ after approve = approved
786
+ forbid task.status == blocked
787
+ forbid task.status == escalated
788
+ ```
789
+
790
+ Adjusting a gate is a data change — edit the block above, regenerate workflows
791
+ on the user side via `/forge:update`, and the new gate takes effect on the next
792
+ orchestrator run. No code change required to relax or tighten a gate.
793
+
794
+ ## Write-Boundary Contract
795
+
796
+ You MAY write Forge-owned JSON (`task.json`, `sprint.json`, `bug.json`,
797
+ events sidecars, `COLLATION_STATE.json`, `progress.log`) directly with the
798
+ `Write` or `Edit` tools. You do NOT need to route every write through
799
+ `store-cli` — the probabilistic layer is free to bypass deterministic tools.
800
+
801
+ However, **every write to a Forge-owned path is schema-validated at the
802
+ filesystem boundary** by the `PreToolUse` hook at
803
+ `hooks/validate-write.js`. A malformed write is rejected with a message
804
+ naming the offending field and pointing at the relevant
805
+ `forge/schemas/<kind>.schema.json`. Fix the data and retry — do NOT try to
806
+ disable the hook.
807
+
808
+ `store-cli` is still the most convenient path (it handles ID allocation,
809
+ referential integrity, ghost-event semantics, and sidecar merging), but it
810
+ is one route among several. The schema invariant is preserved whichever
811
+ route you take.
812
+
813
+ **Emergency bypass.** For operator-driven repair, set
814
+ `FORGE_SKIP_WRITE_VALIDATION=1` for a single turn. The hook will let the
815
+ write through and append an audit line to the affected sprint's
816
+ `progress.log`.
817
+
818
+ ## Iron Laws
819
+
820
+ <!-- Shared orchestrator laws live in generic-skills.md § Orchestrator Iron Laws. -->
821
+ > See `generic-skills.md § Orchestrator Iron Laws` for the six universal laws that apply to all orchestrators.
822
+
823
+ **Additional law specific to this pipeline:**
824
+
825
+ **YOU MUST NOT silently work around a blocker.** If a phase fails, a subagent
826
+ returns empty, a gate fails, or a verdict cannot be parsed, the orchestrator
827
+ MUST either retry once (for recoverable failures) or escalate to the human.
828
+ Skipping the phase, fabricating a result, assuming success without evidence,
829
+ or continuing with a degraded response is NEVER acceptable. Every failure MUST
830
+ produce a visible signal (✗ or ⚠) and a structured event. Silent continuation
831
+ is a violation of the Iron Laws.
832
+
833
+ ## Error Recovery
834
+
835
+ - Test/build failure: pass error to Engineer revision workflow, retry once
836
+ - Verdict "Revision Required": enter revision loop (up to max_iterations)
837
+ - Subagent empty/crash/timeout response: retry once with simplified prompt
838
+ (strip summary and architecture blocks). Escalate on second failure.
839
+ See Subagent Response Validation in the Execution Algorithm.
840
+ - Subagent non-zero exit code (not parse-verdict): same as above — retry
841
+ once, escalate on second failure. The crash reason is captured in the
842
+ escalation event notes.
843
+ - Verdict malformed or missing: escalate to human immediately. Never guess.
844
+ - Revision loop exhaustion: escalate to human immediately. Never approve
845
+ to unblock.
846
+ - Gate failure (preflight): escalate to human. No retry, no fall-through.
847
+ - Gate misconfiguration: escalate to human. No retry, no fall-through.
848
+ - Git hook failure: diagnose, fix, create new commit
849
+ - Merge conflict: escalate to human
850
+ - Task status is blocked or escalated: skip the task entirely. Do not
851
+ attempt any phase on it.
852
+
853
+ ## Event Emission
854
+
855
+ <!-- See _fragments/event-emission-schema.md for canonical contract -->
856
+ > See `_fragments/event-emission-schema.md` for the actor split (subagent
857
+ > writes judgement-only SUMMARY; orchestrator composes the canonical event
858
+ > from runtime telemetry + SUMMARY and emits it).
859
+
860
+ The **orchestrator** is the only actor that calls `store-cli emit` for phase
861
+ events. Phase subagents write `{PHASE}-SUMMARY.json` and return. After each
862
+ subagent returns, the orchestrator:
863
+
864
+ 1. Captures the subagent's runtime attribution (`model`, `provider`, token
865
+ usage) from the runtime stream.
866
+ 2. Records bracketed wall times around the spawn call (`startTimestamp`,
867
+ `endTimestamp`, `durationMinutes`).
868
+ 3. Reads the SUMMARY for the judgement blob (`verdict`, `notes`, `findings`).
869
+ 4. Composes the canonical event with `eventId`, `taskId`, `sprintId`, `role`,
870
+ `action`, `phase`, `iteration` from its own task state and `tokenSource:
871
+ "reported"` when the runtime surfaced usage.
872
+ 5. Calls `node "$FORGE_ROOT/tools/store-cli.cjs" emit {sprintId} '{event-json}'`
873
+ with the complete record.
874
+
875
+ Do not include hardcoded example `model` or `provider` strings in the
876
+ generated orchestrator prose — they are the seed of LLM hallucination.
877
+ Refer subagents to `.forge/schemas/event.schema.json` instead.
878
+
879
+ ## Generation Instructions
880
+ - Fill in concrete test/build/lint commands from .forge/config.json
881
+ - Reference generated workflows by exact filename in .forge/workflows/
882
+ - Include stack-specific gate checks
883
+ - Use the Execution Algorithm above verbatim — do not paraphrase or summarise it
884
+ - `spawn_subagent` = Agent tool call. Each phase invocation MUST use the Agent tool with
885
+ the exact workflow filename and task ID in the prompt. Never invoke phases inline.
886
+ - **Model dispatch uses cluster detection.** The generated workflow must include
887
+ the cluster detection block (reading `ANTHROPIC_DEFAULT_*_MODEL` env vars) and
888
+ the ROLE_TIER mapping table. On single clusters, omit `model` on Agent spawns
889
+ (subagents inherit the parent model). On tiered clusters, pass `model=tier`
890
+ based on the role-to-tier mapping. Only override this for per-phase `model`
891
+ fields from `config.pipelines`.
892
+ Do NOT generate a "Model Assignments" table — the Model Resolution section
893
+ above is the single source of truth.
894
+ - **Include the sidecar merge pattern.** After each subagent returns, run
895
+ `node "$FORGE_ROOT/tools/store-cli.cjs" merge-sidecar {sprintId} {eventId}` to merge token fields from the
896
+ sidecar into the canonical event and delete the sidecar. If the sidecar does not
897
+ exist (merge-sidecar exits 1), treat as non-fatal and emit the event without token
898
+ fields (graceful fallback — no error).
899
+ - **Include the role-to-noun mapping table.** The generated orchestrator MUST include
900
+ a `ROLE_TO_NOUN` dictionary (or equivalent in the host language) that maps every
901
+ pipeline phase role to a noun-based persona identifier. This table is used for
902
+ persona and skill file lookups, not for display. Example:
903
+
904
+ | Role | Noun | Persona File | Skill File |
905
+ |------|------|-------------|------------|
906
+ | `plan` | `engineer` | `.forge/personas/engineer.md` | `.forge/skills/engineer-skills.md` |
907
+ | `implement` | `engineer` | `.forge/personas/engineer.md` | `.forge/skills/engineer-skills.md` |
908
+ | `review-plan` | `supervisor` | `.forge/personas/supervisor.md` | `.forge/skills/supervisor-skills.md` |
909
+ | `review-code` | `supervisor` | `.forge/personas/supervisor.md` | `.forge/skills/supervisor-skills.md` |
910
+ | `validate` | `qa-engineer` | `.forge/personas/qa-engineer.md` | `.forge/skills/qa-engineer-skills.md` |
911
+ | `approve` | `architect` | `.forge/personas/architect.md` | `.forge/skills/architect-skills.md` |
912
+ | `commit` | `engineer` | `.forge/personas/engineer.md` | `.forge/skills/engineer-skills.md` |
913
+ | `writeback` | `collator` | `.forge/personas/collator.md` | `.forge/skills/collator-skills.md` |
914
+
915
+ Generated lookups must use `{persona_noun}.md` and `{persona_noun}-skills.md`,
916
+ never `{phase.role}.md` or `{phase.role}-skills.md`.
917
+ - **Phase banners are orchestrator-owned.** The generated orchestrator MUST NOT include
918
+ a "Your first action — run this banner command" instruction in subagent prompts.
919
+ The orchestrator displays the badge before spawning and the exit signal after return;
920
+ subagents do not display banners. Instead, include progress reporting instructions
921
+ in the subagent prompt with the agent name, progress log path, and banner key.
922
+ - **Include the progress IPC pattern.** Each generated orchestrator MUST:
923
+ 1. Clear the progress log at task start: `node "$FORGE_ROOT/tools/store-cli.cjs" progress-clear {sprintId}`
924
+ 2. Compute the agent name before each spawn: `{taskId}:{persona_noun}:{phase.role}:{iteration}`
925
+ 3. Start a Monitor on the progress log before each subagent spawn
926
+ 4. Include progress reporting instructions in the subagent prompt (agent name,
927
+ progress log path, banner key, and `store-cli progress` command examples)
928
+ 5. Stop the Monitor after the subagent returns
929
+ 6. Display phase-exit signals after each phase completes (see Phase-Exit Signals section)
930
+ - **Include phase-exit signals.** After each subagent returns (and after sidecar
931
+ merge and event emission), the generated orchestrator MUST print the appropriate
932
+ exit signal: `✓` for completed/approved, `↻` for revision required (with iteration
933
+ count), `⚠` for escalated.
934
+ - **Include the context pack injection.** Before spawning each subagent, the
935
+ generated orchestrator MUST read `.forge/cache/context-pack.md` (if it exists)
936
+ and inline it into the subagent prompt under the heading
937
+ `### Architecture context (summary — full docs available at paths listed below)`.
938
+ If the pack is absent, omit this block silently — the subagent falls back to
939
+ reading architecture docs directly. This is the mechanism that replaces per-phase
940
+ `Read engineering/architecture/stack.md` calls with a single cached summary.
941
+ Subagents instructed by this block should read full docs **only** when the
942
+ summary is insufficient.
943
+ - **Include post-phase /compact calls.** After each phase-exit signal (for every
944
+ non-escalation outcome), the generated orchestrator MUST:
945
+ 1. Print a checkpoint line: `[checkpoint] task={task_id} sprint={sprint_id} phase_index={i} iterations={iteration_counts}`
946
+ 2. Run `/compact` to free orchestrator context before the next phase.
947
+ All durable state is on disk; the checkpoint line ensures the compact summary
948
+ preserves the loop bookkeeping (task ID, sprint ID, current phase index,
949
+ iteration counts). Do NOT compact on escalation — the human needs full context.
950
+
951
+ ## Friction Emit
952
+
953
+ When the Orchestrator detects skill friction during orchestrate-task — a referenced skill is unused, fails on invocation, is missing from the registry, has gone stale relative to current architecture, or is redundant with another skill — emit a `friction` event so `/forge:enhance --phase 2` can act on the signal. This is the writer side of the channel whose reader landed in S13-T08; the reader is empty without these emits.
954
+
955
+ **Trigger conditions** (set `issue` to the matching token):
956
+
957
+ | Token | When to emit |
958
+ |--------------------|----------------------------------------------------------------------------------|
959
+ | `skill_unused` | A skill listed in the persona's skill block was loaded but never consulted. |
960
+ | `skill_failed` | A skill was consulted but its guidance produced an error or required correction. |
961
+ | `skill_missing` | The workflow needed guidance the available skills did not cover. |
962
+ | `skill_stale` | A skill's guidance contradicts current architecture / supersedes its own advice. |
963
+ | `skill_redundant` | Two skills provided overlapping or conflicting guidance for the same decision. |
964
+
965
+ **Two flavours of friction in orchestrate-task:**
966
+
967
+ 1. **Subagent-experienced friction** (the persona running plan / implement /
968
+ validate / etc. detects skill friction). The subagent records the signal
969
+ via `node "$FORGE_ROOT/tools/friction-emit.cjs" --workflow {wf} --persona {p}
970
+ --issue {token} [--subkind {token}] [--evidence '{...}']`, which appends a
971
+ judgement-only record to `.forge/cache/FRICTION-{wf}.jsonl`. After the
972
+ subagent returns, the orchestrator drains this file, stamps the
973
+ subagent's captured runtime attribution (model, provider, usage, wall
974
+ times, eventId) onto each record, and emits the resulting events via
975
+ `store-cli emit` as event type `"friction"`. The orchestrator truncates
976
+ the file only after all emits succeed.
977
+
978
+ 2. **Orchestrator-experienced friction** (spawn failure, sidecar missing,
979
+ FSM rejection, verdict malformed). The orchestrator emits inline using
980
+ its own model/provider attribution (`persona: "orchestrator"`,
981
+ `workflow: "orchestrate"`, `phase: "orchestrate"`). Same `store-cli emit`
982
+ path; no example record is reproduced here because the orchestrator
983
+ owns the field values — consult `.forge/schemas/event.schema.json` for
984
+ the required shape.
985
+
986
+ The schema enforces `{workflow, persona, issue}` as required when
987
+ `type === "friction"`. `subkind` is the frozen enum
988
+ `skill_unused|skill_failed|skill_missing|skill_stale|skill_redundant` or
989
+ experimental `^x_[a-z_]+$`. Emit one record per distinct friction signal
990
+ — do not coalesce.
991
+
992
+ The generated `orchestrate_task.md` MUST carry this section verbatim —
993
+ `/forge:enhance --phase 2` greps for it.