Global worktree isolation for subagents and teammates

Resolved 💬 2 comments Opened Feb 28, 2026 by cjeon Closed Feb 28, 2026

Problem

When running multiple subagents or teammates in parallel, they all modify the same repository simultaneously. This makes it impossible to reliably use Stop/SubagentStop hooks that depend on a clean repo state (e.g., linting, formatting, test runs), because the working tree is dirty with other agents' concurrent changes.

The isolation: "worktree" parameter exists on the Agent tool, but:

  1. It can only be set per-invocation by the caller — there's no way to enforce it globally
  2. SubagentStart hooks cannot modify spawn parameters — so there's no hook-based workaround to inject isolation: "worktree"
  3. Teammates (agent teams) have no worktree isolation at allSubagentStart/SubagentStop don't fire for teammates, and there's no equivalent mechanism

Proposed Solution

1. Global default isolation setting

A project-level setting to enforce worktree isolation for all subagents and/or teammates:

// .claude/settings.json
{
  "agentIsolation": "worktree"
}

This would make every subagent and teammate automatically run in its own git worktree without the caller needing to specify it.

2. Allow SubagentStart hooks to modify spawn parameters

If hooks could return parameter overrides (like isolation), users could conditionally enforce worktree isolation:

{
  "hookSpecificOutput": {
    "hookEventName": "SubagentStart",
    "parameterOverrides": {
      "isolation": "worktree"
    }
  }
}

3. Extend worktree isolation to teammates

Teammates spawned via agent teams should also support worktree isolation — either via the global setting above or per-teammate configuration.

4. Auto-merge hook on SubagentStop

A complementary feature: an option (or documented pattern) to auto-merge the subagent's worktree branch back into the main branch on SubagentStop, enabling a workflow like:

  • Subagent starts → gets its own worktree
  • Subagent works in isolation, commits freely
  • Subagent finishes → changes are fast-forward merged back
  • Stop hook runs on a clean, integrated repo

Use Case

I want to run formatting/linting/test hooks on SubagentStop and Stop, but concurrent agents make the repo dirty. With per-agent worktrees + auto-merge, each agent's hooks run against a clean snapshot, and the main branch accumulates changes cleanly.

Current Workaround

  • Define every agent as a custom .claude/agents/*.md file with isolation: worktree in frontmatter
  • Skip Stop hooks for subagents/teammates entirely (what I'm doing now)

Neither is satisfactory — the first doesn't scale and doesn't cover teammates, the second defeats the purpose of having hooks.

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗