COORDINATION PATTERN

Droid Swarm
Parallel Agent Coordination

Orchestrate multiple droid exec workers from one conversation.
Wave-based execution, dependency enforcement, fail-fast validation. Built and tested on real projects with 40+ workers across 5 missions.

πŸ’‘ Pro tip: Use /compress to reset context and reduce API costs during long sessions.

v0.2.0
Current Version
Wave
Execution Model
40+
Workers Tested
5
Mission Patterns
THE INSIGHT

One Conversation. No Context Switching.

You never leave ONE conversation. No terminal juggling. No session IDs. No "wait, which worker was doing the database migration?"

You talk to a Droid Commander. The Droid Commander orchestrates everything. You stay in flow.

⚠️ CRITICAL: AGENTS.md Makes This Work

The swarm pattern works because AGENTS.md defines Droid Commander behavior. Without proper AGENTS.md instructions, the droid won't know to orchestrate workers, ask for approval, or validate results.

  • swarm init adds Droid Commander role to your AGENTS.md automatically
  • Droid Commander reads AGENTS.md on every session start to understand its role
  • Workers read .droid-swarm/agents.md to know NOT to delegate
  • No AGENTS.md = no coordination - the droid won't follow the pattern

This isn't magic. It's structured instructions that make the droid behave exactly as needed for swarm coordination.

ARCHITECTURE

How Swarm Works

One conversation drives everything. You talk to a Droid Commander. The Droid Commander orchestrates parallel workers. Each worker executes independently. Results flow back to the Droid Commander for synthesis.

One Conversation β†’ Droid Commander β†’ Many Workers You (Mission Director) "Research content structure patterns" ONE conversation Droid Commander Plans β†’ Creates Mission β†’ Writes Specs β†’ Deploys β†’ Validates Follows: AGENTS.md + .droid-swarm/agents.md mission.md + mission-deps.json + workers/*.md spawn-workers.sh ⚑ Parallel Execution Worker 1 Learning Science droid exec Follows: .droid-swarm/agents.md Output: JSON + git commit Worker 2 Platform Patterns droid exec Follows: .droid-swarm/agents.md Output: JSON + git commit Worker 3-6 More Research droid exec (parallel) Each follows same pattern Autonomous execution outputs/W1.json outputs/W2.json outputs/W3-6.json Droid Commander Validates & Synthesizes Reviews commits + tests + outputs β†’ Final report
REAL-WORLD EXAMPLE

See It In Action

Every swarm mission begins with a conversation. Here's a real refactoring taskβ€”migrating hardcoded colors across 40+ pages to a semantic token system with Tailwind theme switching.

You (Mission Director)
"I need to refactor hardcoded color values across 40+ pages and 10+ components to use semantic tokens. Set up Tailwind CSS theme switching with proper best practices."
Droid Commander
"This is perfect for a swarm. I'm thinking:
  • Phase 1 (Audit): 3 workers analyze pages/components in parallel
  • Phase 2 (Foundation): 1 worker creates semantic token system
  • Phase 3 (Refactor): Multiple workers update files in parallel

Should I create the mission plan?"
You
"Yes, show me the plan."
Droid Commander
Mission: Semantic Token Migration

Wave 1 - Audit (3 workers, parallel):
  • W1-audit-pages: Analyze 40+ pages for hardcoded colors
  • W2-audit-components: Analyze 10+ components for color usage
  • W3-audit-theme: Document current theme patterns

Wave 2 - Foundation (1 worker):
  • W4-create-tokens: Create semantic token system + Tailwind config (waits for audits)

Wave 3 - Refactor (parallel workers):
  • W5-W8: Update page groups (10 pages each, parallel)
  • W9-W11: Update component groups (parallel)

Deliverables: Semantic tokens, Tailwind config, 40+ refactored files
Dependencies: Wave 2 waits for Wave 1, Wave 3 waits for Wave 2
You
"Perfect. Deploy the workers."
Droid Commander
Executing: bash .droid-swarm/bin/spawn-workers.sh semantic_token_migration

Wave 1 (3 audit workers, parallel)...
  • W1-audit-pages: exit 0 β†’ Found 156 hardcoded color instances
  • W2-audit-components: exit 0 β†’ Found 43 color instances
  • W3-audit-theme: exit 0 β†’ Documented 8 theme patterns

Wave 2 (1 foundation worker)...
  • W4-create-tokens: exit 0 β†’ Created semantic token system

Wave 3 (7 refactor workers, parallel)...
  • W5-W8 (pages): All exit 0 β†’ 40 pages refactored
  • W9-W11 (components): All exit 0 β†’ 10 components refactored

Mission complete: 50 files refactored, theme switching enabled βœ“

πŸ”‘ Key Observation

Notice: One conversation. Droid Commander orchestrated 11 workers across 3 waves. Audited 50 files, created token system, refactored everything in parallel. No terminal juggling. No context switching.

WAVE EXECUTION

Mission Dependencies & Wave Execution

Workers can run in parallel or wait for others to complete. Define dependencies in mission-deps.jsonβ€”swarm handles execution order automatically.

⚑

Parallel

All workers start at once. Independent tasks execute simultaneously.

πŸ”—

Sequential

Workers wait for dependencies. Strict execution order guaranteed.

🌊

Wave-Based

Groups execute in stages. Foundation first, then parallel work.

Most missions are fully parallel. Complex workflows can define dependencies. The Droid Commander reads mission-deps.json and orchestrates wave execution automatically.

View All Dependency Patterns
META OBSERVATION

The Future: Workers Spawning Workers

During development, workers started invoking droid exec themselvesβ€”spawning their own sub-workers. We stopped it (updated specs: "never invoke droid exec").

But it hints at potential: Recursive delegation. Workers spawning workers spawning workers. You still have one conversation at the top, but the tree executes itself.

Not what we builtβ€”but what we glimpsed.

STATE MANAGEMENT

Mission State & Resume Capability

.mission-state.json tracks execution progress. Supports resume after failures, skip completed workers, and provides execution audit trail.

State File Structure

# .mission-state.json (generated during execution)
{
  "mission_id": "M5_semantic_tokens",
  "created_at": "2025-01-15T14:30:00Z",
  "current_wave": 2,
  "workers": {
    "W1-token-system": {
      "status": "completed",
      "exit_code": 0,
      "started_at": "2025-01-15T14:30:05Z",
      "completed_at": "2025-01-15T14:32:18Z",
      "output_file": "outputs/W1-token-system.json"
    },
    "W2-base-components": {
      "status": "completed",
      "exit_code": 0,
      "started_at": "2025-01-15T14:32:30Z",
      "completed_at": "2025-01-15T14:35:42Z",
      "output_file": "outputs/W2-base-components.json"
    },
    "W3-layout": {
      "status": "in_progress",
      "started_at": "2025-01-15T14:32:30Z"
    },
    "W4-dashboard": {
      "status": "pending"
    }
  }
}

Resume After Failure

# Initial execution - W3 fails
bash spawn-workers.sh M5_semantic_tokens

Wave 1: W1 β†’ exit 0 βœ“
Wave 2: W2, W3 β†’ W2 exit 0 βœ“, W3 exit 1 βœ—
Wave 3: Skipped (W3 dependency failed)

# Fix W3 spec, resume mission
bash spawn-workers.sh M5_semantic_tokens

Reading existing state: .mission-state.json
Skipping completed workers: W1, W2
Resuming from Wave 2: W3
Wave 2: W3 β†’ exit 0 βœ“
Wave 3: W4, W5, W6 β†’ all exit 0 βœ“

Mission complete!

🧹 State Management Commands

# Check mission state
cat .droid-swarm/missions/M5_semantic_tokens/.mission-state.json

# Clean state (start fresh) - delete the state file
rm .droid-swarm/missions/M5_semantic_tokens/.mission-state.json

# List all mission states
ls .droid-swarm/missions/*/.mission-state.json
THE ENABLER

How This Is Possible

The swarm pattern works through two mechanisms: AGENTS.md defines roles and behavior, and droid exec provides the execution primitive. Together, they enable coordinated parallel work.

πŸ“– What AGENTS.md Tells the Droid Commander

By defining clear roles and responsibilities in AGENTS.md, we make droids behave exactly as needed. Here's the core workflow section:

## PART 1: SWARM PATTERN (FOR DROID COMMANDER ONLY)

**Your Role**: Droid Commander - Plans missions, deploys workers, validates results

**Key Principle**: Only do what Mission Director explicitly requests.

### When to Deploy Workers

βœ… Use Workers:
- Multi-file changes (5+ files, coordinated)
- Complex features (different work per worker)
- Batch operations (same action on many files)
- Analysis/audits (generate reports)

❌ Handle Yourself:
- Single file edits (1-3 files)
- Simple refactors, quick fixes
- Documentation updates

### Workflow Phases

1. Assessment: Receive task β†’ decide handle alone OR deploy workers
2. Planning: Create mission folder, write specs
3. Approval: Present plan β†’ wait for human approval
4. Deployment: Run spawn-workers.sh
5. Validation: Review outputs, check commits, run tests
6. Reporting: Summarize results for human

This is why the Droid Commander knows to ask for approval, create detailed specs, and validate everything before reporting. It's all in AGENTS.md.

πŸ”§ Droid Exec: The Execution Primitive

Droid exec's native fail-fast behavior is what enables resilient swarm execution. Workers that fail (non-zero exit) don't block othersβ€”remaining workers continue executing.

  • β€’ Exit 0: Success β†’ Droid Commander validates output
  • β€’ Non-zero exit: Failure (permission violation, tool error, unmet objective) β†’ Droid Commander logs error, continues with other workers

Droid Commander aggregates all results (successes + failures) and reports what needs attention. Workers fail independently without blocking the swarm.

SAFETY FIRST

⚠️ Critical Safety & Best Practices

Droid Swarm is powerful but requires careful use. Workers execute code autonomously. Follow these safety guidelines to protect your system and data.

πŸ›‘οΈ Always Use --cwd Parameter

CRITICAL: Restrict droid exec operations to specific directories to prevent unintended filesystem access.

# βœ… SAFE: Limited to project directory
droid exec --auto medium --cwd $(pwd) -f worker.md

# βœ… SAFE: Limited to src/ only
droid exec --auto medium --cwd ./src -f worker.md

# ❌ UNSAFE: Can access entire filesystem
droid exec --auto high -f worker.md

⚠️ Autonomy Escalation: FALSE POSITIVES

When workers fail, the Droid Commander may suggest running with higher autonomy (--auto high). These suggestions can be WRONG.

  • Exit code 127: Command not found (not a permission issue)
  • Exit code 1: Generic failure (could be anything)
  • Path errors: Often configuration, not permissions

ALWAYS review the worker spec before escalating autonomy.

πŸ‘€ Review Git Commits Before Pushing

Workers create git commits automatically. Always review before pushing to remote:

# Review worker commits
git log --oneline --grep="W[0-9]" -20

# Check what changed
git diff HEAD~5..HEAD

# Revert if suspicious
git revert <commit-hash>

πŸ’° API Cost Awareness

Droid Swarm uses Factory.ai Droid CLI which consumes API tokens. As context grows during long sessions, API costs can explode.

πŸ’‘ Use the /compress command

Start a new chat with the same Droid Commander while preserving context through an AI-generated summary. This resets the context window and dramatically reduces costs.

Our M2 mission: ~20M tokens (6 workers, comprehensive research over extended session)

For current pricing details, refer to Factory.ai pricing documentation.

πŸ“‹ General Disclaimer

Droid Swarm is an experimental pattern for orchestrating AI agents. Use at your own risk.

  • No warranties: Provided "as-is" without guarantees
  • Real-world use: Test thoroughly in isolated environments first
  • Data loss risk: Always commit work before deploying workers
  • Review specs: Understand what workers will do before executing
  • Worker autonomy: Workers execute code - review outputs before committing

When in doubt: DON'T ESCALATE. Use --cwd. Review specs carefully.

GET STARTED

Initialize Swarm in Your Project

Install CLI, run swarm init, start working.

πŸ“– New to droid exec? Read the official Factory.ai droid exec documentation first.

1

Install and Initialize

# Install swarm-cli globally
npm install -g @dctmfoo/droid-swarm-cli

# Navigate to your project
cd /path/to/your/project

# Initialize swarm structure
swarm init

# This creates:
# .droid-swarm/
# β”œβ”€β”€ bin/spawn-workers.sh       (orchestration script)
# β”œβ”€β”€ docs/SWARM_PATTERN.md      (full documentation)
# β”œβ”€β”€ templates/                 (mission & worker templates)
# └── missions/                  (your missions go here)
#
# Also updates AGENTS.md with Droid Commander role
2

Validate Droid Commander Role

Start droid and confirm AGENTS.md instructions took effect:

# Start interactive session
droid

# Ask Droid Commander to confirm its role
> "What are your roles and responsibilities? What is my role here?"

# Droid Commander should respond with Droid Commander and Mission Director roles
# See example response below
Droid Commander explaining Droid Commander and Mission Director roles
3

Deploy Your First Swarm

# Describe your task to Droid Commander
> "I need to refactor the database layer across 8 files"

# Droid Commander workflow:
# 1. Analyzes task
# 2. Proposes workers
# 3. Asks for approval
# 4. Creates mission specs
# 5. You review: .droid-swarm/missions/MX_task/
# 6. You: "Deploy them"
# 7. Droid Commander executes spawn-workers.sh
# 8. Workers run in parallel
# 9. Droid Commander validates & reports

# No terminal juggling. One session.
EXECUTION

How Swarm Executes

One interactive session spawns N non-interactive droid exec processes. Each worker runs as one-shot execution with isolated context.

Execution Pattern

# Droid Commander creates mission structure
.droid-swarm/missions/content-research/
β”œβ”€β”€ mission.md              # Shared context for all workers
β”œβ”€β”€ mission-deps.json       # Dependency graph (wave execution)
└── workers/
    β”œβ”€β”€ W1-learning-science.md
    β”œβ”€β”€ W2-platform-patterns.md
    └── W3-content-length.md

# spawn-workers.sh orchestrates parallel execution
for worker in workers/*.md; do
  droid exec --auto medium --cwd $(pwd) -f "$worker" &
done
wait

# Each worker (one-shot execution):
1. Reads spec from markdown file
2. Executes task with validation (npm run check, tests, etc.)
3. Writes structured artifact to outputs/
4. Exits with code 0 (success) or non-zero (failure)

# Droid Commander (interactive session):
- Aggregates all worker artifacts
- Validates results
- Continues session for synthesis/next steps

Worker Validation Example

# Worker spec defines success criteria:
Success = ALL must pass:
1. TypeScript compilation: npm run check
2. Tests: npm test (if applicable)
3. Artifact written: outputs/W3-result.json
4. Git commit created (if spec requires)
5. Exit code 0

# Worker writes structured artifact:
{
  "worker": "W3-content-length",
  "status": "success",
  "validations": {
    "typescript": "passed",
    "output_generated": true,
    "word_count": 6800
  },
  "sources_analyzed": 38,
  "exit_code": 0
}
PATTERNS

Explore Coordination Patterns

Learn

πŸ§ͺ TDD Workflow

Three entities collaborate with tests-first development.

Learn this pattern β†’
You're here

πŸ€– Droid Swarm

One conversation, Droid Commander orchestration, parallel worker execution.

Coming soon

🧰 Continuous Integration Pattern

Automated testing and deployment with droid coordination.

Coming soon

πŸ“Š Analysis & Audit Pattern

Deep codebase analysis with specialized audit workers.

Coming soon

🧬 Monorepo Coordination

Multi-package updates with dependency-aware workers.

Coming soon

🎨 Design System Migration

Coordinated UI component updates across applications.

Ready to Try Swarm?

Install swarm-cli and start coordinating parallel droid exec workers in your project.

View on GitHub β†’ Follow @dctmfoo

πŸ“¦ Includes: swarm-cli tool, spawn-workers.sh orchestration, templates, AGENTS.md, and real mission examples.

⭐ Star the repo to support open source droid coordination patterns