Skip to content

[Phase 4] Worktree-Per-Task Isolation for Parallel Batch Execution #418

@frankbria

Description

@frankbria

Summary

When running parallel batch execution (cf work batch run --strategy parallel), give each task its own git worktree instead of having multiple agents modify files in the same working directory. Inspired by Gastown's polecat worktree model, but much simpler.

Motivation

Currently parallel batch execution runs multiple agents in the same workspace directory. This creates race conditions: two agents editing the same file, conflicting git add operations, and verification gates that can't tell which agent broke what.

Gastown solves this with per-agent worktrees — git's built-in mechanism for multiple working directories sharing a single .git object store. This is fast (no full clone), isolated (each worktree has its own branch), and mergeable (standard git merge/rebase).

Keep it simple: We don't need Gastown's persistent polecat pools, sandbox repair, or worktree lifecycle daemons. Just: create worktree before task, run agent in it, merge results after.

Design

For serial execution: no change

Single tasks continue working in the main workspace directory. No overhead.

For parallel execution: worktree per task

cf work batch run task-1 task-2 task-3 --strategy parallel

Creates:

<workspace>/
├── .git/                          # shared object store
├── .codeframe/
│   └── worktrees/
│       ├── task-1/                # git worktree on branch cf/task-1
│       ├── task-2/                # git worktree on branch cf/task-2
│       └── task-3/                # git worktree on branch cf/task-3
└── <normal working dir>           # main branch, untouched during parallel run

Lifecycle (per task in parallel batch)

  1. git worktree add .codeframe/worktrees/<task-id> -b cf/<task-id>
  2. Run agent with cwd set to the worktree path
  3. Agent modifies files in isolation
  4. Verification gates run inside the worktree
  5. On success: merge worktree branch back to main (or create PR)
  6. Cleanup: git worktree remove .codeframe/worktrees/<task-id>

Conflict handling

  • If merge has conflicts: create a blocker with the conflict details
  • Let the human resolve, or re-run the task on top of the merged result
  • For simple conflicts: attempt automatic resolution (ours/theirs for non-overlapping changes)

Scope

New module: core/worktrees.py

class TaskWorktree:
    def create(self, workspace_path: Path, task_id: str, base_branch: str = "main") -> Path:
        """Create an isolated worktree for a task. Returns worktree path."""
    
    def merge_back(self, workspace_path: Path, task_id: str) -> MergeResult:
        """Merge worktree branch back to base. Returns success/conflict."""
    
    def cleanup(self, workspace_path: Path, task_id: str) -> None:
        """Remove worktree and delete branch."""

Changes to existing files

  • core/conductor.py — in parallel strategy, create worktrees and pass worktree paths to agents
  • core/runtime.py — accept optional working_directory override for agent execution
  • cli/app.py — add --isolate flag to batch run (default: True for parallel, False for serial)

Acceptance Criteria

  • Parallel batch tasks each get their own worktree
  • Agent executes with cwd set to worktree path
  • Verification gates run inside the worktree
  • Successful tasks merge back to base branch
  • Merge conflicts create blockers with context
  • Worktrees cleaned up after merge (success or failure)
  • Serial execution is unchanged (no worktrees)
  • --no-isolate flag disables worktrees for parallel (old behavior)
  • Integration tests: parallel batch with worktree isolation

Dependencies

Complexity Note

This is deliberately simpler than Gastown's model:

  • No persistent polecat pools (worktrees created and destroyed per batch)
  • No sandbox reuse across batches (fresh worktree each time)
  • No daemon-managed lifecycle (CLI-driven)
  • No three-layer identity/sandbox/session split (just a worktree)

Metadata

Metadata

Assignees

No one assigned

    Labels

    architectureSystem architecture and design patternsenhancementNew feature or requestphase-4.4Phase 4.4: Batch Execution & Reconciliation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions