Implements three complementary patterns for coordinating multi-agent swarms: 1. Status Polling (Fix 1): Orchestrator periodically spawns status-checker agents to monitor swarm health, detect stuck agents, and identify conflicts early. 2. File Claiming (Fix 2): Agents claim file ownership before editing via a claims registry (.claude/file-claims.md). Prevents multiple agents from editing the same file simultaneously. 3. Checkpoint-Based Orchestration (Fix 5): Separates swarm execution into phases - planning (read-only), conflict detection, resolution, then implementation with monitoring. Plugin contents: - /swarm command for full orchestrated workflow - status-checker agent (haiku, lightweight polling) - conflict-detector agent (analyzes plans for overlaps) - plan-reviewer agent (validates individual plans) - swarm-patterns skill with comprehensive documentation
7.4 KiB
description, argument-hint
| description | argument-hint | |
|---|---|---|
| Coordinate multi-agent swarm with conflict prevention, status polling, and checkpoint-based orchestration |
|
Coordinated Swarm Orchestration
You are orchestrating a multi-agent swarm to complete a complex task. Follow this checkpoint-based workflow to prevent conflicts and enable proactive monitoring.
Task Description
$ARGUMENTS
Phase 1: Initialization
Goal: Set up swarm coordination infrastructure
Actions:
-
Create coordination files:
.claude/swarm-status.json- Agent status tracking.claude/file-claims.md- File ownership registry.claude/swarm-plans/- Directory for agent plans
-
Initialize status file:
{
"swarm_id": "[generated-id]",
"task": "[task description]",
"started": "[timestamp]",
"phase": "planning",
"agents": {}
}
- Initialize file claims:
# File Claims Registry
| Agent ID | File Path | Claimed At | Status |
|----------|-----------|------------|--------|
- Create todo list tracking all phases
Phase 2: Planning (Parallel, Read-Only)
Goal: Have multiple agents analyze the codebase and create implementation plans WITHOUT making changes
Actions:
-
Launch 2-4 planning agents in parallel, depending on task complexity. Each agent should:
- Analyze a different aspect of the task
- Create a detailed implementation plan
- List ALL files they intend to modify/create/delete
- Identify dependencies on other files or agents
- CRITICAL: Agents must NOT edit any files - planning only
-
Each agent writes their plan to
.claude/swarm-plans/[agent-id].md:
## Agent Plan: [agent-id]
### Task Summary
[What this agent will accomplish]
### Files to Modify
- `path/to/file.ts`: [Description of changes]
### Files to Create
- `path/to/new-file.ts`: [Purpose]
### Dependencies
- Requires: [what this depends on]
- Blocks: [what depends on this]
### Implementation Steps
1. [Step 1]
2. [Step 2]
- Update swarm status as agents complete:
{
"agents": {
"agent-1": {"status": "plan_complete", "plan_file": ".claude/swarm-plans/agent-1.md"}
}
}
Phase 3: Conflict Detection
Goal: Review all plans and identify conflicts before implementation
Actions:
- Wait for ALL planning agents to complete
- Read all plans from
.claude/swarm-plans/ - Launch the conflict-detector agent to analyze all plans
- Review the conflict report
If conflicts found:
- Present conflict report to user
- Ask for resolution preference:
- Sequence: Execute conflicting agents one at a time
- Reassign: Move conflicting files to single agent
- Manual: User provides custom resolution
- Update plans based on resolution
- Re-run conflict detection to confirm resolution
If no conflicts:
- Proceed to Phase 4
Phase 4: File Claiming
Goal: Register file ownership before implementation begins
Actions:
- For each approved plan, register file claims in
.claude/file-claims.md:
| agent-1 | src/api/handler.ts | 2025-01-15T10:30:00Z | claimed |
| agent-1 | src/utils/auth.ts | 2025-01-15T10:30:00Z | claimed |
| agent-2 | src/db/queries.ts | 2025-01-15T10:30:00Z | claimed |
-
Determine execution order based on conflict analysis:
- Parallel batch 1: Agents with no conflicts or dependencies
- Sequential queue: Agents that must wait for others
-
Update swarm status:
{
"phase": "implementing",
"execution_order": [
{"batch": 1, "agents": ["agent-1", "agent-2"], "parallel": true},
{"batch": 2, "agents": ["agent-3"], "parallel": false, "waits_for": ["agent-1"]}
]
}
Phase 5: Implementation with Monitoring
Goal: Execute implementation with proactive status monitoring
Actions:
-
Launch first batch of implementation agents
-
Status Polling Loop (every 30-60 seconds during execution):
- Launch a status-checker agent (haiku model for speed)
- Review status report
- If issues detected:
- Conflict: Pause later agent, let first complete
- Stuck agent: Check logs, consider reassignment
- Failed agent: Report to user, decide whether to retry or skip
-
As each agent completes:
- Update swarm status:
"status": "completed" - Release file claims in
.claude/file-claims.md: change status toreleased - Launch next queued agents that were waiting
- Update swarm status:
-
Agent Instructions (include in each implementation agent's prompt):
## Coordination Requirements
Before editing any file:
1. Read `.claude/file-claims.md`
2. Verify the file is claimed by YOU (your agent ID)
3. If claimed by another agent, SKIP and note in your results
4. If not claimed, DO NOT edit - report the missing claim
After completing work:
1. Update your status in swarm communication
2. Report files modified for claim release
If you encounter a conflict:
1. STOP editing the conflicted file
2. Report the conflict immediately
3. Wait for orchestrator resolution
Phase 6: Verification
Goal: Verify swarm completed successfully
Actions:
-
Check all agents completed:
- Read final swarm status
- Verify all planned files were modified
- Check for any orphaned claims
-
Run integration checks:
- Build/compile if applicable
- Run tests if applicable
- Check for import/type errors
-
Clean up coordination files:
- Archive swarm status to
.claude/swarm-history/ - Clear file claims
- Remove plan files
- Archive swarm status to
Phase 7: Summary
Goal: Report swarm execution results
Actions:
-
Summarize:
- Total agents launched
- Files modified/created/deleted
- Conflicts detected and resolved
- Issues encountered
- Total execution time
-
Present to user:
- What was accomplished
- Any items requiring follow-up
- Suggested next steps
Error Handling
Agent Failure:
- Log failure in swarm status
- Release failed agent's file claims
- Ask user: retry, skip, or abort swarm
Unresolvable Conflict:
- Pause all conflicting agents
- Present options to user
- Wait for manual resolution
Stuck Swarm:
- If no progress for 5+ minutes, alert user
- Provide diagnostic information
- Offer to abort and roll back
File Claim Convention (For All Agents)
Include this instruction block in every implementation agent's system prompt:
## File Claiming Protocol
You are part of a coordinated swarm. Follow these rules strictly:
1. **Before ANY file edit**:
- Read `.claude/file-claims.md`
- Find your agent ID in the registry
- Only edit files claimed by YOUR agent ID
2. **If file is claimed by another agent**:
- DO NOT edit the file
- Note in your results: "Skipped [file] - claimed by [other-agent]"
- Continue with other work
3. **If file is not in claims registry**:
- DO NOT edit the file
- Report: "Cannot edit [file] - not in approved claims"
- This indicates a planning oversight
4. **Update your progress**:
- After each significant step, your status will be tracked
- If you encounter issues, report them clearly
Status Polling Schedule
During Phase 5, launch status-checker agent:
- After initial batch launch: wait 30 seconds, then check
- During active execution: check every 45-60 seconds
- After agent completion: immediate check to launch next batch
- On any reported issue: immediate check
Use haiku model for status-checker to minimize latency and cost.