compound-engineering-plugin icon indicating copy to clipboard operation
compound-engineering-plugin copied to clipboard

feat(ce-work): codify worktree isolation for parallel subagent dispatch

Open piiiico opened this issue 7 months ago • 3 comments

Problem

When /ce:work selects the "parallel subagents" execution strategy (Phase 1 Step 4), it dispatches multiple Agent tool calls for independent implementation units. Currently, the skill does not instruct the use of isolation: "worktree" on these Agent calls — subagents all operate on the same working directory, relying on a "non-overlapping files" heuristic.

This creates risks:

  • Git operations can conflict between concurrent subagents
  • Test runs can interfere with each other
  • If the overlap assessment is wrong, file writes race

Evidence: it already works when the model chooses it

In a real session (Opus, 3 plans executed in parallel on a Rust+Chrome extension project), the orchestrating agent independently decided to use isolation: "worktree" on each Agent call. The result:

  • 3 worktrees created at .claude/worktrees/agent-{id}, each on its own branch
  • Subagents ran fully isolated with run_in_background: true
  • Orchestrator reviewed diffs from all 3 worktrees, then merged sequentially
  • 3 merge conflicts arose on overlapping files — resolved cleanly by the orchestrator

The flow worked well, but it was emergent behavior — the model happened to use the parameter. A different model or context could skip it entirely.

Proposal

When the parallel subagents strategy is selected in Phase 1 Step 4, the skill should instruct:

  1. Use isolation: "worktree" on each Agent dispatch
  2. Use run_in_background: true for concurrent execution
  3. After all subagents complete, review diffs from each worktree
  4. Merge branches sequentially in dependency order, resolving conflicts
  5. Clean up worktrees (note: .claude/worktrees/ should be in .gitignore to avoid accidental staging)

This makes the parallel strategy reliable rather than model-dependent.

Notes

  • The serial subagents and inline strategies do not need this — they are sequential by definition
  • PR #223 explored a similar pattern for external delegates but has not been merged
  • The git-worktree skill worktree-manager.sh is an alternative to the Agent tool built-in worktree support, but isolation: worktree is simpler since the harness manages the lifecycle automatically
  • ce:work-beta with Codex delegation forces serial execution, sidestepping the issue entirely

piiiico avatar Apr 12 '26 19:04 piiiico

I want to add that the native worktree features has been shown to be quite reliable. I'd consider refactoring the worktree skill to use hooks, there are plenty.

jrdncstr avatar Apr 14 '26 02:04 jrdncstr

The one thing I'm worried about is many people who use the plugin use it within Conductor or similar tools which operate by default in worktrees.

Even if you don't, some of hte plan execution depends on successive work being done and the orchestrator then validating. Have to carefully think through this as you mentioned. Example of my concern (in the case of Conductor):

  • Branch proliferation: N Conductor workspaces x M parallel subagents = N*M extra branches in the same repo.
  • Cleanup uncertainty: Claude Code’s isolation: "worktree" auto-cleans if no changes are made, but if the orchestrator crashes mid-merge or the session ends, orphaned worktrees and branches accumulate.
  • Path ambiguity: Where does .claude/worktrees/agent-{id} resolve... relative to the Conductor worktree or the main repo root? Matters for both the agent and cleanup.

The proposal says “merge branches sequentially in dependency order, resolving conflicts”, but the entire crux boils down to this:

  • The orchestrator is itself running in a Conductor worktree on its own branch. It needs to pull changes from each subagent branch, resolve conflicts, validate the combined result, then dispatch dependent units.
  • Conflict resolution quality: @piiiico's session had 3 conflicts in 3 worktrees (a high rate). Automated conflict resolution by the orchestrator is error-prone and risks introducing bugs.
  • Validation gap: Each subagent runs its own tests in isolation. Those tests don’t prove the combined, post-merge result works. The orchestrator needs a full test pass after each merge-in, which then serializes the “parallel” benefit.
  • Ordering fragility: If the independent by the orchestrator assessment was wrong, merging A+B might make C’s changes incompatible.

I think @piiiico correctly identifies a real risk (shared working directory for parallel agents), and the evidence that worktree isolation works is compelling. But codifying it as a default in ce:work requires solving the merge-back orchestration problem which i think is difficult to get right. Conductor (or simmilar worktree primary tools) means a lot of users (incl me) are already one level deep in worktrees, making this a nested-isolation problem rather than a simple isolation problem.

Perhaps the safer path would be:

  1. Short-term: Improve the overlap detection heuristic and add an automatic sequential fallback when overlap is detected (or when git index contention is likely). This is low-risk and addresses the most common failure mode.
  2. Medium-term: If parallel-with-isolation is pursued, use explicit worktree management with budget checks, cleanup, and crash recovery rather than relying on isolation: "worktree". In fact, our newest skill ce-optimize contributed by @huntharo does this. We then also spec the merge-back workflow with the same rigor as the dispatch workflow

tmchow avatar Apr 14 '26 05:04 tmchow

@piiiico @jrdncstr Take a look at this PR which is a short-term solve which I think is safer while we wrestle with the worktree isolation issue longer term

https://github.com/EveryInc/compound-engineering-plugin/pull/557

tmchow avatar Apr 14 '26 06:04 tmchow