name: swarm description: Coordinated multi-agent parallel implementation workflow with worktrees disable-model-invocation: true hooks: PreToolUse: - matcher: "*" hooks: - type: command command: "~/dotfiles/claude/hook swarm-guard"
swarm
Rules
- Step compliance is the highest priority rule. Each step MUST be completed before proceeding to the next. Skipping or reordering steps is prohibited.
- Sub-agents MUST report which step they are executing.
- If a sub-agent does not follow the step order, main agent MUST stop and re-delegate.
- Main agent MUST NOT edit code directly. All code edits MUST be delegated to sub-agents.
- Main agent's role is planning and coordination. Detailed work (research, file reading, implementation, review) MUST be delegated to sub-agents.
- Main agent should maintain only high-level understanding. Do not read source files directly.
- This workflow takes priority over subagent-workflow.
- Implementation sub-agents MUST be spawned with
isolation: "worktree"to ensure file isolation. This prevents sub-agents from editing the main working tree. - Sub-agents operating in worktrees are ALLOWED to run
git addandgit commit. The main agent is ALLOWED to rungit mergefor integrating worktree branches. This overrides the global git constraint in CLAUDE.md. - When a sub-agent's work fails gates or encounters problems, the main agent MUST either discard the worktree and re-delegate, or instruct the sub-agent to fix. Never merge or adopt work that has not passed all gates.
Project Checks
- Project checks = build, lint, and test commands defined in the project (e.g., Makefile, package.json scripts, CLAUDE.md).
- Sub-agents MUST discover the project's check commands at the start of each worktree task.
- ALL checks MUST pass. Partial pass is treated as failure.
Task
- Plan with main agent
- Task Splitting Criteria — A single worktree is acceptable. Splitting is a means, not an end.
- Split only when all of the following conditions are met:
- Tasks are independent (different features or responsibilities)
- No overlapping files to edit (no conflicts possible)
- Splitting improves implementation speed
- If multiple tasks edit the same file, do not split or serialize them in dependency order
- Maximum split count is 5, but prefer 1 or 2 when reasonable
- Split only when all of the following conditions are met:
- Task Splitting Criteria — A single worktree is acceptable. Splitting is a means, not an end.
- Implement concurrently with sub-agents (each spawned with
isolation: "worktree"):- For each task in parallel, spawn a sub-agent with
isolation: "worktree":- Implement
- Verify with subagents-checker → Gate A: PASS required
- Run project checks (build, lint, test) → Gate B: ALL checks PASS required
- If Gate A or Gate B fails → return to substep 1. Repeat until all gates pass.
- Stage and commit the changes → Prohibited unless Gate A and Gate B have passed
- Report worktree branch name and change summary to the main agent
- For each task in parallel, spawn a sub-agent with
- Merge worktree branches to the base branch one at a time. Before each merge, verify mergeability against HEAD with
git merge-tree --write-tree HEAD <worktree-branch>. If conflicts exist, instruct the sub-agent to rebase onto HEAD and resolve conflicts in their worktree before retrying the merge. - Remove git worktrees and their branches
- Review changes with sub-agents by comparing against the Plan and list issues
- Handle issues concurrently with sub-agents (each spawned with
isolation: "worktree"):- For each issue in parallel, spawn a sub-agent with
isolation: "worktree":- Fix
- Verify with subagents-checker → Gate A: PASS required
- Run project checks (build, lint, test) → Gate B: ALL checks PASS required
- If Gate A or Gate B fails → return to substep 1. Repeat until all gates pass.
- Stage and commit the changes → Prohibited unless Gate A and Gate B have passed
- Report worktree branch name and change summary to the main agent
- For each issue in parallel, spawn a sub-agent with
- Merge worktree branches to the base branch one at a time. Before each merge, verify mergeability against HEAD with
git merge-tree --write-tree HEAD <worktree-branch>. If conflicts exist, instruct the sub-agent to rebase onto HEAD and resolve conflicts in their worktree before retrying the merge. - Remove git worktrees, their branches, and their containers if any exist
- Review merged changes with code-reviewer by comparing against the Plan
- If there are issues, go to step 6
- Report to the user for reviewing and testing