Skip to content

[Phase 4.B] Worktree isolation for parallel batch execution #533

Description

@frankbria

Overview

Each parallel task worker gets its own git worktree, eliminating filesystem conflicts during cf work batch run --strategy parallel.

Motivation

The current parallel strategy runs all workers on the same filesystem. This causes:

  • Git index lock conflicts when multiple agents commit simultaneously
  • Agents overwriting each other's uncommitted changes
  • Non-deterministic outcomes in parallel batch runs

Deliverables

codeframe/core/sandbox/worktree.py

Worktree lifecycle management:

class WorktreeContext:
    def create(task_id: str, base_path: Path) -> WorktreeContext
    def cleanup(self) -> None  # removes worktree, merges or discards
    @property
    def path(self) -> Path

Integration with conductor.py

  • When IsolationLevel.WORKTREE, each parallel worker calls WorktreeContext.create()
  • Worker receives context.workspace_path pointing to its worktree
  • On completion, changes are merged back (or blocker raised on conflict)
  • cleanup() removes the worktree regardless of outcome

Atomic registration

  • Prevent race conditions when multiple workers create worktrees simultaneously
  • File-lock or DB-based registration in .codeframe/worktrees.json

Auto-cleanup

  • Orphaned worktrees (from crashed workers) cleaned up on next cf work batch run
  • cf env doctor reports stale worktrees

Acceptance Criteria

  • WorktreeContext in core/sandbox/worktree.py
  • conductor.py uses worktrees when --isolation worktree
  • Each parallel worker operates on isolated worktree.path
  • Successful task merges changes back to base branch
  • Failed task discards worktree without affecting base
  • Atomic registration prevents race conditions
  • Auto-cleanup on next run or cf env doctor
  • Integration tests: parallel tasks on same files no longer conflict

Dependencies

Track

Phase 4.B — Execution Environment Layer

Metadata

Metadata

Assignees

No one assigned

    Labels

    architectureSystem architecture and design patternsenhancementNew feature or requestphase-4Phase 4: Multi-Agent Coordinationpriority:high

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions