Skip to content

Worktree Isolation for Parallel Agent Groups

When /exec runs parallel agent groups, each agent can optionally get its own isolated sub-worktree. This prevents file conflicts that occur when multiple agents write to the same files simultaneously.

Git supports creating worktrees inside existing worktrees. The .git file in the nested worktree correctly resolves to the main repository’s .git/worktrees/ directory.

Test result: git worktree add .exec-agents/agent-0 -b exec-agent-485-0 succeeds inside an issue worktree. No path conflicts with sequant’s conventions.

OperationTimeNotes
git worktree add~400msNo npm install needed
node_modules symlink~13msReuses issue worktree’s node_modules
git worktree remove~130msIncludes branch cleanup
Total per agent~550msNegligible vs. agent execution time

Selected: git merge --no-ff with temporary branches.

StrategyProsConsVerdict
git merge --no-ffBuilt-in conflict detection, standard markersRequires branch per agentSelected
git diff | git applyLightweightNo conflict markers, fails silentlyRejected
cherry-pickPreserves individual commitsComplex with multiple commitsRejected
rsync + git addSimple file copyNo conflict detectionRejected
Issue worktree: ../worktrees/feature/485-eval/
├── .exec-agents/ ← sub-worktree directory
│ ├── agent-0/ ← agent 0's isolated copy
│ ├── agent-1/ ← agent 1's isolated copy
│ └── agent-2/ ← agent 2's isolated copy
├── src/ ← issue worktree files
└── node_modules/ ← shared via symlink
  1. Create: For each agent in a parallel group, createSubWorktree() creates a git worktree at .exec-agents/agent-<N>/, symlinks node_modules, and copies environment files.

  2. Execute: Each agent works in its own sub-worktree. All changes are committed to a temporary branch (exec-agent-<issue>-<N>).

  3. Merge back: After all agents complete, mergeAllSubWorktrees() merges each agent’s branch into the issue branch using git merge --no-ff.

  4. Cleanup: Sub-worktrees and temporary branches are removed.

  • No conflict: New files from different agents merge cleanly.
  • Conflict detected: The merge is aborted, conflict files are reported, and the exec skill flags the conflict for the next iteration.
  • Partial merge: If agent A conflicts but agent B doesn’t, agent B’s changes are still merged. Only agent A’s changes are flagged.

In .sequant/settings.json:

{
"agents": {
"isolateParallel": true
}
}

Default: false (opt-in for v1).

Terminal window
npx sequant run 485 --isolate-parallel

The CLI flag overrides the settings value.

When isolateParallel is false (default), parallel agents share the issue worktree exactly as before. No sub-worktrees are created.

The .worktreeinclude file at the repository root lists files to copy into sub-worktrees (one path per line, # for comments):

.env
.env.local
.env.development
.claude/settings.local.json

If the file doesn’t exist, the module falls back to a hardcoded default list matching the same files above.

Sub-worktrees are automatically removed after merge-back (success or failure).

If a session is interrupted, orphaned sub-worktrees may remain. These are cleaned up by:

  • scripts/cleanup-worktree.sh — cleans .exec-agents/ before removing the issue worktree
  • cleanupOrphanedSubWorktrees() — programmatic cleanup via the worktree-isolation module
  • git worktree prune — git’s built-in stale worktree cleanup

createSubWorktree(issueWorktreePath, agentIndex)

Section titled “createSubWorktree(issueWorktreePath, agentIndex)”

Creates a sub-worktree for a parallel agent.

  • Returns: SubWorktreeInfo | null (null on failure)
  • Side effects: Creates directory, symlinks node_modules, copies env files

mergeAllSubWorktrees(issueWorktreePath, subWorktrees)

Section titled “mergeAllSubWorktrees(issueWorktreePath, subWorktrees)”

Merges all sub-worktree branches back into the issue branch.

  • Returns: MergeBackResult with per-agent results
  • Conflict handling: Aborts conflicting merges, continues with others

cleanupAllSubWorktrees(issueWorktreePath, subWorktrees?)

Section titled “cleanupAllSubWorktrees(issueWorktreePath, subWorktrees?)”

Removes sub-worktrees, branches, and orphaned entries.

Formats merge results for human-readable logging.