| name | Agent Fork-Join |
| description | Multi-agent git workflow orchestrator with isolated worktrees, FIFO merge queue, and automated PR lifecycle management. |
Agent Fork-Join
A sophisticated multi-agent orchestration system that enables parallel agent work with isolated git worktrees, automatic conflict resolution, and full PR lifecycle automation.
Overview
Agent Fork-Join solves the fundamental challenge of multiple AI agents working on the same codebase simultaneously. Each agent operates in an isolated git worktree, and a Rust-based merge daemon coordinates the integration of all changes through a FIFO queue with automatic conflict resolution.
Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User Prompt (makes changes) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ UserPromptSubmit Hook โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ 1. Detect if prompt will make changes โ โ
โ โ 2. Create feature branch (if not on one) โ โ
โ โ 3. Start merge daemon (if not running) โ โ
โ โ 4. Initialize session state โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Orchestrator Spawns Agents โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ AgentSpawn โ โ AgentSpawn โ โ AgentSpawn โ
โ Hook โ โ Hook โ โ Hook โ
โ โ โ โ โ โ
โ Create worktree โ โ Create worktree โ โ Create worktree โ
โ agent/agent-1 โ โ agent/agent-2 โ โ agent/agent-3 โ
โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ Agent Works โ โ Agent Works โ โ Agent Works โ
โ (isolated) โ โ (isolated) โ โ (isolated) โ
โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ AgentComplete โ โ AgentComplete โ โ AgentComplete โ
โ Hook โ โ Hook โ โ Hook โ
โ โ โ โ โ โ
โ 1. Check diff โ โ 1. Check diff โ โ 1. Check diff โ
โ 2. Get commit โ โ 2. Get commit โ โ 2. Get commit โ
โ message โ โ message โ โ message โ
โ 3. Create branch โ โ 3. Create branch โ โ 3. Create branch โ
โ 4. Commit โ โ 4. Commit โ โ 4. Commit โ
โ 5. Queue merge โ โ 5. Queue merge โ โ 5. Queue merge โ
โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Merge Daemon (Rust) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ FIFO Merge Queue โ โ
โ โ โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ โ โ
โ โ โ Agent-1 โโ โ Agent-2 โโ โ Agent-3 โโ ... โ โ
โ โ โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ For each queued merge: โ
โ 1. Attempt merge into feature branch โ
โ 2. If conflict โ rebase agent branch โ request agent fix โ re-queue โ
โ 3. If success โ update feature branch โ remove from queue โ
โ 4. Track merge order and dependencies โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ (all agents done, all merges complete)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ PR Creation & Validation โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ 1. Create draft PR with: โ โ
โ โ - Original user prompt as description โ โ
โ โ - Aggregated commit messages โ โ
โ โ - Agent contribution summary โ โ
โ โ โ โ
โ โ 2. Run validation suite: โ โ
โ โ - Tests (configurable command) โ โ
โ โ - Linting โ โ
โ โ - Type checking โ โ
โ โ โ โ
โ โ 3. If validation fails: โ โ
โ โ - Pass results to orchestrator โ โ
โ โ - Spawn fix agents โ cycle repeats โ โ
โ โ โ โ
โ โ 4. If validation passes: โ โ
โ โ - Mark PR as ready for review โ โ
โ โ - Request reviews from CODEOWNERS or configured reviewers โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Components
1. Merge Daemon (Rust)
A lightweight, high-performance daemon that manages the merge queue.
Location: daemon/
Features:
- FIFO queue with priority support
- Concurrent merge attempts with locking
- Automatic conflict detection
- Rebase orchestration
- IPC via Unix domain socket
- Persistent queue state (survives restarts)
- Metrics and logging
API:
ENQUEUE <agent-id> <branch-name> <worktree-path>
DEQUEUE <agent-id>
STATUS
CONFLICTS <agent-id>
RETRY <agent-id>
SHUTDOWN
2. Hooks
UserPromptSubmit Hook (hooks/on-prompt-submit.sh)
Triggered when user submits a prompt. Determines if the prompt will make changes and initializes the fork-join session.
Logic:
- Analyze prompt for change indicators (keywords: "implement", "fix", "add", "modify", "refactor", "update", etc.)
- Check current branch - if on main/master, create feature branch
- Generate session ID
- Start merge daemon if not running
- Store session state (original prompt, feature branch, timestamp)
AgentSpawn Hook (hooks/on-agent-spawn.sh)
Triggered when an agent is spawned by the orchestrator.
Logic:
- Generate unique agent ID
- Create isolated git worktree:
git worktree add .worktrees/agent-<id> <feature-branch>
- Set agent's working directory to worktree
- Register agent with merge daemon
- Store agent metadata (spawn time, task description)
AgentComplete Hook (hooks/on-agent-complete.sh)
Triggered when an agent finishes its work.
Logic:
- Check for changes:
git diff --stat in agent's worktree
- If no changes โ cleanup worktree, deregister agent
- If changes exist:
a. Request commit message from agent (via IPC)
b. Create agent branch:
agent/<session-id>/<agent-id>
c. Stage and commit changes
d. Enqueue branch for merge
- Wait for merge result
- If conflict โ prepare conflict info, request agent to fix, re-enqueue
- Cleanup worktree on success
3. Scripts
scripts/create-pr.sh
Creates a draft PR when all agents complete and merges succeed.
Inputs:
- Original user prompt
- List of commit messages
- Agent contribution metadata
Output:
- Draft PR with formatted description
- Labels:
draft, ai-generated, needs-review
scripts/run-validation.sh
Runs the configured validation suite.
Steps:
- Run test command
- Run lint command
- Run typecheck command
- Collect results
- Return pass/fail with details
scripts/request-reviews.sh
Requests reviews when validation passes.
Logic:
- Parse CODEOWNERS file (if
useCodeowners: true)
- Filter to configured
maxReviewers limit
- Override with
defaultReviewers if configured
- Use
gh pr edit to request reviews
- Mark PR as ready for review
Implementation Plan
Phase 1: Core Infrastructure
1.1 Merge Daemon (Rust)
Files:
daemon/
โโโ Cargo.toml
โโโ src/
โ โโโ main.rs # Entry point, signal handling
โ โโโ lib.rs # Library exports
โ โโโ queue.rs # FIFO merge queue
โ โโโ merger.rs # Git merge operations
โ โโโ ipc.rs # Unix socket server
โ โโโ state.rs # Persistent state management
โ โโโ config.rs # Configuration loading
โ โโโ error.rs # Error types
โโโ tests/
โโโ queue_test.rs
โโโ merger_test.rs
Dependencies:
[dependencies]
tokio = { version = "1", features = ["full"] }
git2 = "0.18"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
rusqlite = "0.31"
tracing = "0.1"
tracing-subscriber = "0.3"
thiserror = "1"
1.2 Hook Scripts
Files:
hooks/
โโโ on-prompt-submit.sh
โโโ on-agent-spawn.sh
โโโ on-agent-complete.sh
โโโ lib/
โโโ common.sh # Shared functions
โโโ git-utils.sh # Git helper functions
โโโ daemon-client.sh # Merge daemon IPC client
Phase 2: Git Worktree Management
2.1 Worktree Lifecycle
2.2 Branch Management
Phase 3: Merge Queue & Conflict Resolution
3.1 Queue Management
3.2 Conflict Resolution Flow
Phase 4: PR Lifecycle
4.1 PR Creation
4.2 Validation Suite
4.3 Review Request
Phase 5: Integration & Polish
5.1 Configuration
5.2 Error Handling
5.3 Observability
Configuration
{
"agent-fork-join": {
"maxConcurrentAgents": 8,
"mergeStrategy": "rebase",
"maxMergeRetries": 3,
"testCommand": "npm test",
"validationCommands": ["npm run lint", "npm run typecheck"],
"defaultReviewers": ["@senior-dev", "@tech-lead"],
"maxReviewers": 3,
"useCodeowners": true,
"autoMergeOnSuccess": false,
"featureBranchPrefix": "feature/",
"agentBranchPrefix": "agent/",
"worktreeDir": ".worktrees",
"daemonSocket": "/tmp/merge-daemon.sock",
"sessionTimeout": 3600,
"cleanupOrphanedWorktrees": true
}
}
Usage
Automatic Mode
The plugin activates automatically when:
- A prompt is detected that will make changes
- Multiple agents are spawned to work on the task
Manual Commands
/agent-fork-join status
/agent-fork-join queue
/agent-fork-join cleanup
/agent-fork-join session
/agent-fork-join retry <agent-id>
/agent-fork-join cancel
State Machine
โโโโโโโโโโโโโโโ
โ IDLE โ
โโโโโโโโโโโโโโโ
โ
โ UserPromptSubmit (change detected)
โผ
โโโโโโโโโโโโโโโ
โ STARTED โ โ Feature branch created, daemon started
โโโโโโโโโโโโโโโ
โ
โ AgentSpawn
โผ
โโโโโโโโโโโโโโโ
โ WORKING โ โ Agents working in worktrees
โโโโโโโโโโโโโโโ
โ
โ AgentComplete (last agent)
โผ
โโโโโโโโโโโโโโโ
โ MERGING โ โ Processing merge queue
โโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ All merges succeed โ Conflict detected
โผ โผ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ VALIDATING โ โ RESOLVING โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ โ
โโโโโโโโโโโโโโโ โ Agent fixes conflict
โ โ โ
โ Pass โ Fail โโโโโโโโโโโโโ
โผ โผ โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ READY โ โ FIX_CYCLE โ โโโโโโโโโโโโโโโถ โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ โ
โ โ Spawn fix agents โ
โ โผ โ
โ โโโโโโโโโโโโโโโ โ
โ โ WORKING โ โโโโโโโโโโโโโโโโโโโ
โ โโโโโโโโโโโโโโโ
โ
โ Request reviews
โผ
โโโโโโโโโโโโโโโ
โ COMPLETE โ
โโโโโโโโโโโโโโโ
Error Recovery
Daemon Crash
- Queue state persisted to SQLite
- On restart, daemon recovers pending merges
- Agents notified to re-enqueue if needed
Agent Crash
- Worktree preserved for inspection
- Session continues with remaining agents
- Manual cleanup command available
Merge Deadlock
- Timeout after configurable period
- Escalate to orchestrator for manual resolution
- Option to force-merge or abort
Security Considerations
- Worktree Isolation: Each agent operates in a completely separate worktree, preventing accidental interference
- Branch Protection: Agent branches are prefixed and namespaced to prevent collision with user branches
- Daemon Socket: Unix socket with restricted permissions
- Credential Handling: Uses system git credential helpers, no credential storage
Performance
- Worktree creation: ~100ms per agent
- Merge operation: ~50-200ms depending on diff size
- Queue throughput: ~10 merges/second
- Memory overhead: ~5MB per active worktree
Dependencies
- Git 2.20+ (worktree support)
- Rust 1.70+ (for daemon compilation)
- gh CLI (for PR operations)
- Unix-like OS (for domain sockets)
Troubleshooting
Issue: Daemon won't start
Solution: Check if socket file exists at /tmp/merge-daemon.sock. Remove stale socket and retry.
Issue: Worktree creation fails
Solution: Ensure .worktrees directory exists and is writable. Check for branch name conflicts.
Issue: Merge conflicts not detected
Solution: Ensure merge daemon has read access to all worktrees. Check daemon logs.
Issue: PR creation fails
Solution: Verify gh CLI is authenticated. Check for branch push permissions.
Resources