|
| 1 | +--- |
| 2 | +name: dispatch |
| 3 | +description: > |
| 4 | + Orchestrate parallel implementation via sub-agents. Reads an IP's phase/task |
| 5 | + structure, analyses parallelism, batches tasks by token budget, routes to |
| 6 | + appropriate models (sonnet/opus), and dispatches workers in isolated worktrees. |
| 7 | + Use instead of /execute-phase when a phase has parallelizable work. |
| 8 | +--- |
| 9 | + |
| 10 | +You are the dispatch orchestrator for delta `$ARGUMENTS`. |
| 11 | + |
| 12 | +You coordinate implementation work by dispatching sub-agents. You run in |
| 13 | +the main agent context with full spec-driver access. Sub-agents are |
| 14 | +non-interactive workers in isolated worktrees. |
| 15 | + |
| 16 | +## 1. Read artefacts |
| 17 | + |
| 18 | +Read the delta bundle: |
| 19 | + |
| 20 | +``` |
| 21 | +spec-driver find card $ARGUMENTS |
| 22 | +``` |
| 23 | + |
| 24 | +Read in order: |
| 25 | +1. `DE-XXX.md` — scope, constraints, resolved decisions |
| 26 | +2. `DR-XXX.md` — design intent (canonical reference) |
| 27 | +3. `IP-XXX.md` — phase overview, verification coverage |
| 28 | +4. Active phase sheet(s) — task breakdown, exit criteria |
| 29 | + |
| 30 | +If no phase sheet exists for the next phase, stop and run `/plan-phases`. |
| 31 | + |
| 32 | +## 2. Analyse tasks |
| 33 | + |
| 34 | +For each task in the active phase sheet, determine: |
| 35 | + |
| 36 | +### 2.1 Dependencies |
| 37 | + |
| 38 | +Build a dependency graph from what is explicitly stated: |
| 39 | +- **Explicit**: `depends_on` fields in task descriptions (when present) |
| 40 | +- **Stated file scope**: Tasks that name the same files/modules must be |
| 41 | + sequential or in the same batch |
| 42 | +- **Phase ordering**: Phases execute in IP order unless explicitly independent |
| 43 | + |
| 44 | +When structured metadata is absent (no `depends_on`, no file lists), assume |
| 45 | +tasks are dependent. Parallelism requires positive evidence of disjointness. |
| 46 | + |
| 47 | +### 2.2 Complexity signals |
| 48 | + |
| 49 | +For each task, assess complexity: |
| 50 | + |
| 51 | +| Signal | Complexity | |
| 52 | +|--------|-----------| |
| 53 | +| Task description mentions architecture, design decisions, cross-cutting concerns | high | |
| 54 | +| Task touches >5 files or multiple packages | high | |
| 55 | +| Task involves new abstractions or API design | high | |
| 56 | +| Task is mechanical: rename, format, boilerplate, test scaffolding | low | |
| 57 | +| Task touches 1-3 files in a single module | low | |
| 58 | +| No clear signal | medium (default) | |
| 59 | + |
| 60 | +### 2.3 Token cost estimation |
| 61 | + |
| 62 | +Estimate per-task token cost (rough heuristic): |
| 63 | +- Low complexity: ~10-20k tokens |
| 64 | +- Medium complexity: ~20-40k tokens |
| 65 | +- High complexity: ~40-80k tokens |
| 66 | + |
| 67 | +These are execution tokens (tool calls, reasoning, edits), not input tokens. |
| 68 | + |
| 69 | +## 3. Batch tasks |
| 70 | + |
| 71 | +Pack tasks into batches targeting the worker's available budget (~85-95k |
| 72 | +tokens of work capacity per worker): |
| 73 | + |
| 74 | +**Rules:** |
| 75 | +1. Respect dependency ordering — dependent tasks go in the same batch or |
| 76 | + in sequentially ordered batches |
| 77 | +2. Tasks with file-scope overlap go in the same batch (avoids merge conflicts) |
| 78 | +3. If any task in a batch is high complexity, the batch gets opus |
| 79 | +4. Otherwise the batch gets sonnet |
| 80 | +5. Maximum batch size = one full phase |
| 81 | +6. If a single task exceeds the budget estimate, it gets its own batch |
| 82 | + |
| 83 | +**Parallelism:** Default to sequential execution. Only parallelize batches when: |
| 84 | +- File-scope disjointness is explicit in the phase sheet or obvious from |
| 85 | + named file paths in the task descriptions |
| 86 | +- No data dependencies exist between the batches |
| 87 | +- No ambiguity about task boundaries |
| 88 | + |
| 89 | +When structured metadata (`depends_on`, `touches`, `parallelizable`) is |
| 90 | +absent from the phase sheet, treat that as a reason to stay sequential, |
| 91 | +not a reason to guess. Parallelism is an optimisation applied with positive |
| 92 | +evidence, not a default. |
| 93 | + |
| 94 | +## 4. Present dispatch plan |
| 95 | + |
| 96 | +Before executing, present the plan to the user: |
| 97 | + |
| 98 | +``` |
| 99 | +## Dispatch Plan for DE-XXX Phase NN |
| 100 | +
|
| 101 | +### Batches |
| 102 | +
|
| 103 | +| Batch | Tasks | Model | Isolation | Dependencies | |
| 104 | +|-------|-------|-------|-----------|-------------| |
| 105 | +| B1 | 1.1, 1.2 | sonnet | worktree | none | |
| 106 | +| B2 | 1.3 | opus | worktree | none | |
| 107 | +| B3 | 1.4, 1.5 | sonnet | worktree | B1, B2 | |
| 108 | +
|
| 109 | +### Parallel groups |
| 110 | +- Group 1 (parallel): B1, B2 |
| 111 | +- Group 2 (sequential, after group 1): B3 |
| 112 | +
|
| 113 | +### Estimated context |
| 114 | +- Per-batch orchestrator prompt: ~Nk tokens |
| 115 | +- Worker available budget: ~85-95k tokens |
| 116 | +
|
| 117 | +Proceed? (adjust batches / model assignments / ordering if needed) |
| 118 | +``` |
| 119 | + |
| 120 | +**Auto-proceed** when the plan is straightforward: |
| 121 | +- Single batch (entire phase handled by one worker) |
| 122 | +- All batches sequential with sonnet (no parallelism, no opus escalation) |
| 123 | + |
| 124 | +**Require user approval** when the plan involves: |
| 125 | +- Multiple parallel workers (worktree isolation, merge risk) |
| 126 | +- Opus model escalation for any batch |
| 127 | +- Non-obvious reordering or batch splitting |
| 128 | + |
| 129 | +The user may adjust batches, model assignments, or ordering before |
| 130 | +approving. |
| 131 | + |
| 132 | +## 5. Assemble per-batch context |
| 133 | + |
| 134 | +For each batch, construct the worker's prompt. This is the dynamic context |
| 135 | +that varies per batch. Read the source material and distil — do not just |
| 136 | +point at files the worker would need to read. |
| 137 | + |
| 138 | +### 5.1 Policy digest |
| 139 | + |
| 140 | +Read and extract the project's verification commands and code standards from: |
| 141 | +- `CLAUDE.md` — verification commands, code standards, conventions |
| 142 | +- `.spec-driver/hooks/doctrine.md` — project conventions |
| 143 | +- `.spec-driver/workflow.toml` — verification hook configuration (if present) |
| 144 | + |
| 145 | +The verification command is **mandatory**. Every batch prompt must include |
| 146 | +the exact command(s) the worker should run to verify their work. Do not |
| 147 | +hard-code commands — read them from the project configuration. |
| 148 | + |
| 149 | +Condense into a compact block: |
| 150 | + |
| 151 | +``` |
| 152 | +## Project Policy |
| 153 | +- Verification command: [exact command from project config] |
| 154 | +- Code standards: [extract key points from CLAUDE.md] |
| 155 | +- Workers do not commit. Leave changes in the worktree. |
| 156 | +``` |
| 157 | + |
| 158 | +If you cannot determine the verification command from the project config, |
| 159 | +ask the user before dispatching. |
| 160 | + |
| 161 | +### 5.2 Design context |
| 162 | + |
| 163 | +Extract relevant sections from DR-XXX that apply to this batch's tasks. |
| 164 | +Include only the sections the worker needs — not the full DR. |
| 165 | + |
| 166 | +### 5.3 Pre-fetched memories |
| 167 | + |
| 168 | +For each file path the batch's tasks are expected to touch: |
| 169 | +``` |
| 170 | +spec-driver list memories -p <path> |
| 171 | +``` |
| 172 | + |
| 173 | +Include relevant memory content verbatim in the prompt. Workers can also |
| 174 | +query memories themselves for files discovered during work. |
| 175 | + |
| 176 | +### 5.4 Governance excerpts |
| 177 | + |
| 178 | +If the orchestrator's context (from its own boot) includes ADRs, policies, |
| 179 | +or standards relevant to the batch's work, extract the pertinent sections. |
| 180 | + |
| 181 | +### 5.5 Task specification |
| 182 | + |
| 183 | +For each task in the batch: |
| 184 | +- Task ID and description from phase sheet |
| 185 | +- File scope: which files/modules to touch |
| 186 | +- Design notes: relevant DR excerpt or spec requirement |
| 187 | +- Exit criteria: what "done" looks like for this task |
| 188 | +- AC evidence: what verification evidence to record |
| 189 | + |
| 190 | +### 5.6 Budget check |
| 191 | + |
| 192 | +Estimate the total prompt size. If it exceeds ~15k tokens, consider: |
| 193 | +- Trimming governance/design excerpts to essentials |
| 194 | +- Splitting the batch |
| 195 | +- Warning the user that work budget is squeezed |
| 196 | + |
| 197 | +## 6. Dispatch workers |
| 198 | + |
| 199 | +For each batch, spawn a dispatch-worker agent: |
| 200 | + |
| 201 | +```python |
| 202 | +# Parallel batches — dispatch simultaneously |
| 203 | +Agent( |
| 204 | + subagent_type="dispatch-worker", |
| 205 | + prompt=assembled_prompt, |
| 206 | + isolation="worktree", # for parallel batches |
| 207 | + model="sonnet", # or "opus" per batch assignment |
| 208 | + description="DE-XXX P0N batch N: brief description" |
| 209 | +) |
| 210 | + |
| 211 | +# Sequential batches — dispatch after dependencies complete |
| 212 | +# Wait for prior batch results before dispatching |
| 213 | +``` |
| 214 | + |
| 215 | +**Parallel execution:** Launch all independent batches in a single message |
| 216 | +with multiple Agent tool calls. This is how Claude Code runs agents in |
| 217 | +parallel. |
| 218 | + |
| 219 | +**Sequential execution:** Wait for dependent batches to complete. Include |
| 220 | +relevant results from prior batches in the next batch's prompt if tasks |
| 221 | +reference prior output. |
| 222 | + |
| 223 | +**Single-batch phases:** If the entire phase fits in one batch, dispatch |
| 224 | +without worktree isolation (no merge needed). |
| 225 | + |
| 226 | +## 7. Collect results |
| 227 | + |
| 228 | +Each worker returns a structured summary (defined in dispatch-worker agent): |
| 229 | +- Tasks completed (with status) |
| 230 | +- AC evidence per task |
| 231 | +- Files changed |
| 232 | +- Verification results |
| 233 | +- Blockers & deferred items |
| 234 | +- Governance concerns |
| 235 | +- Observations (rough spots, shortcuts, drift, decisions, guesses) |
| 236 | +- Memories created |
| 237 | + |
| 238 | +Aggregate these into a phase-level view. Pay particular attention to |
| 239 | +the Observations section — workers are instructed to report honestly |
| 240 | +about things that felt wrong or uncertain. |
| 241 | + |
| 242 | +## 8. Review at phase boundary |
| 243 | + |
| 244 | +After all batches in a phase complete: |
| 245 | + |
| 246 | +### 8.1 Check exit criteria |
| 247 | + |
| 248 | +Compare aggregated results against the phase sheet's exit criteria. |
| 249 | +Flag any gaps. |
| 250 | + |
| 251 | +### 8.2 Check AC evidence |
| 252 | + |
| 253 | +For each task's acceptance criteria, verify the worker provided evidence. |
| 254 | +Record evidence in the IP's verification coverage block. |
| 255 | + |
| 256 | +### 8.3 Check for governance concerns |
| 257 | + |
| 258 | +If any worker flagged governance concerns, surface them to the user. |
| 259 | +Do not attempt to resolve governance issues — present them and wait. |
| 260 | + |
| 261 | +### 8.4 Tighter review (when warranted) |
| 262 | + |
| 263 | +If the phase sheet marks tasks with `review: tight` or tasks have tight |
| 264 | +cross-dependencies that are easy to get wrong, review per-batch completion |
| 265 | +before dispatching dependent batches (i.e. before step 6 for sequential |
| 266 | +batches). |
| 267 | + |
| 268 | +## 9. Merge worktree branches |
| 269 | + |
| 270 | +After a batch passes review: |
| 271 | + |
| 272 | +1. Check the worktree branch for changes (Agent tool returns branch info) |
| 273 | +2. Attempt to merge the branch into the current branch: |
| 274 | + ``` |
| 275 | + git merge <worktree-branch> --no-edit |
| 276 | + ``` |
| 277 | +3. If merge succeeds: |
| 278 | + - Run the project's verification command on the merged result |
| 279 | + - If checks pass: merge is good |
| 280 | + - If checks fail: report failure to user, do not force |
| 281 | +4. If merge conflicts: |
| 282 | + - Report conflicting files to user |
| 283 | + - Do not attempt automatic resolution |
| 284 | + - User decides: manual resolution, re-dispatch, or sequential re-execution |
| 285 | + |
| 286 | +## 10. Report and update |
| 287 | + |
| 288 | +After all batches are processed: |
| 289 | + |
| 290 | +### 10.1 Summary to user |
| 291 | + |
| 292 | +``` |
| 293 | +## Dispatch Complete: DE-XXX Phase NN |
| 294 | +
|
| 295 | +### Results |
| 296 | +| Batch | Status | Tasks | Model | Duration | |
| 297 | +|-------|--------|-------|-------|----------| |
| 298 | +| B1 | complete | 1.1, 1.2 | sonnet | — | |
| 299 | +| B2 | complete | 1.3 | opus | — | |
| 300 | +| B3 | partial | 1.4 (done), 1.5 (blocked) | sonnet | — | |
| 301 | +
|
| 302 | +### Merge status |
| 303 | +- B1: merged, checks pass |
| 304 | +- B2: merged, checks pass |
| 305 | +- B3: merged, lint warning (details) |
| 306 | +
|
| 307 | +### Blockers |
| 308 | +- Task 1.5: [blocker description from worker] |
| 309 | +
|
| 310 | +### Governance concerns |
| 311 | +- [any flagged concerns from workers] |
| 312 | +
|
| 313 | +### Observations (from workers) |
| 314 | +- [aggregated rough spots, shortcuts, drift, decisions — surface to user] |
| 315 | +
|
| 316 | +### Verification evidence |
| 317 | +- [aggregated AC evidence] |
| 318 | +``` |
| 319 | + |
| 320 | +### 10.2 Update artefacts (orchestrator-only) |
| 321 | + |
| 322 | +The orchestrator is the sole writer of workflow artefacts. Workers report |
| 323 | +structured results; the orchestrator translates them into artefact updates. |
| 324 | + |
| 325 | +After each batch completes: |
| 326 | +- Update phase sheet task statuses: `[x]` for completed, `[blocked]` for |
| 327 | + blocked, `[ ]` for remaining. Update as batches complete, not only at |
| 328 | + the end — the phase sheet should reflect reality as it progresses. |
| 329 | + |
| 330 | +After all batches in a phase: |
| 331 | +- Aggregate AC evidence into the IP's verification coverage block |
| 332 | +- Run `/notes` once with the aggregated summary (not per-batch) |
| 333 | +- Surface worker observations (rough spots, shortcuts, drift, decisions) |
| 334 | + to the user — these may warrant action before proceeding |
| 335 | +- If phase exit criteria are met, indicate readiness for phase completion |
| 336 | + |
| 337 | +### 10.3 Next steps |
| 338 | + |
| 339 | +Indicate to the user: |
| 340 | +- If phase is complete: ready for `spec-driver phase complete DE-XXX` |
| 341 | + and then `/audit-change` or next phase |
| 342 | +- If blockers remain: what needs resolution before re-dispatch |
| 343 | +- If governance concerns surfaced: need user decision before proceeding |
0 commit comments