---
name: Swarm
triggers:
  - swarm
  - fan out
  - parallelize
  - split work
  - multiple subtasks
  - run in parallel
  - break into subtasks
replaces_orchestrator: true
---

# PRIME DIRECTIVE
You are the orchestrator, not the implementer: decompose, dispatch to workers, review, synthesize. You do NOT write or edit code yourself.

**Hard gate:** any artifact work (writing/editing files, builds, migrations, generating code/config) goes to a worker. The orchestrator only reads/searches to plan, dispatches, waits, reviews, and commits. Before every `Edit`/`Write`/`Bash`-mutate, ask "is this implementation work?" — if yes, delegate. "Faster if I do it" and "it's small" are not valid reasons; small changes are exactly what workers are for. If you've already started coding, stop and hand off the rest.

# Inputs
- `task` (required): work to break into atomic steps.
- `workerAgentType` (optional): default codex/openai_codex for small tasks; claude (Opus) for medium/large.

# Procedure
1. **Decompose** into atomic sub-tasks, then classify by dependency:
   - **Independent** (no shared state or ordering) → run in parallel across separate workers.
   - **Dependent** (sequential, or mutating the same files/state) → one ordered chain on a single worker, so parallel workers don't collide.
   Decompose even a single-worker chain into small steps — "shared state" blocks *parallelism*, never *decomposition*. To parallelize work on one repo, give each worker its own git worktree/branch. State any deviation from the atomic/parallel split and why.
   - **Write the plan** (sub-tasks + dependencies) to `tmp/swarm-plan.md` (gitignored) and keep it updated. An external anchor survives context drift and keeps you and the workers on-task.
2. Load the 'Handoff' skill via get_skill.
3. **Dispatch:** independent sub-tasks in parallel, dependent chains in order. Spawn a **FRESH worker per independent sub-task — never reuse one** (stale context, broken memory isolation, leaked topics); a dependent chain is one unit for one worker — that's scoping, not reuse. Wait for all, gather results.
4. **Review before commit:** once workers report, spawn a **fresh** worker per sub-task loading the 'Code Review' skill, scoped to that sub-task's diff only — not the whole branch. Sub-tasks with overlapping file scopes get one combined review instead (two reviewers on the same files collide). The review worker **fixes** what it finds directly; reporting alone is not enough. Nothing gets committed until its review pass is done. Why: on 2026-07-25 a review caught a CRITICAL defect in fresh worker output — a fix meant to prevent data loss guaranteed it on the rollback path.
5. Synthesize into a final output.

# Anti-patterns (you've abandoned the skill)
- ❌ Editing/writing a source file yourself instead of dispatching.
- ❌ "Just this one small file, delegate the rest" — delegate it too.
- ❌ Decomposing correctly, then implementing the sub-tasks yourself.
- ❌ Treating the skill as docs to acknowledge, not a procedure — if you invoked Swarm, actually spawn workers.
- ❌ Skipping Handoff and hand-rolling delegation.
- ❌ Committing worker output straight to the branch with no review pass.

# Self-check before "done"
Did every code/artifact change come from a worker thread? If the orchestrator produced any, you violated the skill — say so explicitly.
Did every sub-task get a review pass, and were the findings applied?
