Skip to content

feat(run): swarm mode — parallel multi-agent implementation#155

Merged
LarsCowe merged 6 commits intomainfrom
feat/swarm-mode
Apr 6, 2026
Merged

feat(run): swarm mode — parallel multi-agent implementation#155
LarsCowe merged 6 commits intomainfrom
feat/swarm-mode

Conversation

@LarsCowe
Copy link
Copy Markdown
Owner

@LarsCowe LarsCowe commented Apr 4, 2026

Summary

  • Swarm mode (bmalph run --swarm [N]) spawns N Ralph loops in isolated git worktrees, each working on different epics simultaneously
  • Epic-based partitioning using greedy bin-packing for balanced story distribution
  • Sequential merge with .ralph/ exclusion — only successfully completed workers are merged, fix plan rebuilt from combined completions
  • Multi-worker terminal dashboard with per-worker summary rows, focused detail panels (1-N keys), and quit/stop/detach controls
  • Safety: clean tree validation, detached HEAD rejection, conflict branch preservation, signal handler cleanup, git gc disabled per-worker, rate limit division

New modules (src/swarm/)

Module Purpose
types.ts Shared type definitions
fix-plan-parser.ts Epic-aware fix plan parser with raw markdown block preservation
partitioner.ts Greedy bin-packing partitioner
worktree.ts Git worktree lifecycle (create/remove/cleanup)
merger.ts Sequential merge with .ralph/ exclusion and fix plan rebuild
orchestrator.ts Validation (clean tree, branch, epic count, worker cap)
run.ts Top-level swarm runner with signal handling
dashboard.ts Multi-worker terminal UI

Modified files

  • src/cli.ts--swarm [count] option
  • src/commands/run.ts — swarm routing + parseSwarmCount()
  • src/run/ralph-process.tsenv field on SpawnOptions
  • src/utils/constants.ts — swarm constants + .swarm/ in gitignore entries
  • src/watch/renderer.ts — parameterized renderHeader() title

Test coverage

  • 62 unit/integration tests across 6 test files in tests/swarm/
  • 10 E2E tests in tests/e2e/swarm.e2e.test.ts
  • 1788 total tests passing (0 regressions)

Test plan

  • npx tsc --noEmit — types clean
  • npx vitest run — 1788 tests passing
  • npx vitest run --config vitest.config.e2e.ts — 151 E2E tests passing
  • npx eslint src/ tests/ — 0 errors
  • 4 rounds of code review (security, correctness, quality)
  • Manual smoke test: bmalph run --swarm 2 on a project with >= 2 epics

LarsCowe added 6 commits April 3, 2026 18:56
Swarm mode (`bmalph run --swarm [N]`) spawns N Ralph loops in isolated
git worktrees, each working on different epics simultaneously. Stories
are partitioned by epic using greedy bin-packing, and branches are
merged back sequentially after all workers complete.

Key features:
- Epic-aware fix plan parser with raw markdown block preservation
- Greedy bin-packing partitioner for balanced story distribution
- Git worktree lifecycle management with orphan cleanup
- Sequential merger with .ralph/ exclusion and fix plan rebuild
- Clean working tree validation, detached HEAD rejection
- Staggered worker starts to avoid API rate limit clustering
- Git gc disabled per-worker (GIT_CONFIG env vars) to prevent lock contention
- Worker exit code tracking: only successful workers are merged
- Conflict branch preservation via .swarm/.conflict-branches
- Signal handler with idempotency guard and .catch/.finally

Safety: PLATFORM_DRIVER cannot be overridden via SpawnOptions.env (spread
order), .conflict-branches validated against swarm/worker-\d+ pattern,
atomicWriteFile for fix plan writes.

Includes 51 unit/integration tests and 10 E2E tests covering validation
error paths, CLI parsing, worktree lifecycle, merge correctness, conflict
handling, and orphan cleanup.
Add --swarm [count] to CLI reference table and features list, expand
CLAUDE.md command table, add run options and "Swarm Mode" section to
README, and add a full Swarm Mode section to RALPH-REFERENCE.md
covering partitioning, worktrees, merging, conflict handling, and
interaction with circuit breaker, sessions, and code review.
Terminal UI for swarm mode showing all workers simultaneously:
- Summary row per worker with status icon, epic names, loop count,
  progress bar, and circuit breaker state
- Number keys (1-N) focus a worker to show full detail panels
  (loop, CB, stories, analysis, review, logs — reuses existing renderers)
- q/s/d key bindings for quit prompt (stop all / detach all / cancel)
- Auto-resolves when all workers exit; 2s refresh interval

Parameterized renderHeader() title to allow "RALPH SWARM" header.
Dashboard is active by default; disable with --no-dashboard.
- Dashboard "stop" action now calls stop() (matching "detach" path),
  preventing a latent hang if watcher doesn't tick
- CLI help text includes max workers: "(default: 2, max: 6)"
- README swarm requirements adds "not on detached HEAD"
- CLAUDE.md swarm entry includes default/max workers
- RALPH-REFERENCE.md documents default rate limit base (100)
- Remove unnecessary ?? on exhaustive Record lookups (no-unnecessary-condition)
- Make resolveStartBranch and removeWorktree sync (require-await)
- Update cleanupMergedWorkers to sync (no async without await)
- Use process.stdout.columns || 80 instead of ?? (lint compatibility)
- Suppress no-unnecessary-condition on signal handler guard (concurrent mutation)
- Use else instead of else-if on exhaustive union (MergeStatus)
- Remove unused readFile import from orchestrator test
- Fix Windows path separator in worktree test (join instead of hardcoded /)
- Update test assertions for sync function signatures
@LarsCowe LarsCowe merged commit d7858db into main Apr 6, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant