Repository navigation
Description
Summary
When spawning a subagent via the Agent tool with isolation: "worktree" from inside an existing git worktree (not the primary clone), the parent worktree's checked-out branch gets replaced with the subagent's newly-created branch instead of the subagent operating on its own isolated worktree. The side-effect is invisible to the parent session until the next git status / git log, and can make it look like files were reverted when they were actually just on a different branch.
Environment
- Claude Code CLI on Windows 11 Pro 10.0.26200
- Shell: bash (git-bash)
- Parent working directory was a git worktree at
C:/Projects/wild-rumpus-WORKTREE2, checked out branchagent/WORKTREE2. The primary clone lives atC:/Projects/Wild Rumpus. - Model: Claude Opus 4.6 (1M context)
Reproduction (happened twice in one session)
- Parent session running in a non-primary git worktree (
C:/Projects/wild-rumpus-WORKTREE2, branchagent/WORKTREE2). - Spawn an
Agenttool call withisolation: "worktree",run_in_background: true, and a prompt that tells the subagent to create a new branch offmain, make commits, and push. - Subagent reports success — creates branch
fix/ci-workflow-restore(first repro) /fix/sync-open-orders-test-mocks(second repro), commits, pushes, opens a PR. - Parent session continues and eventually runs
git statusorgit log, and discovers:- Current branch is now the subagent's branch (
fix/ci-workflow-restore), notagent/WORKTREE2. - Working tree reflects that branch's state, so files the parent had committed to
agent/WORKTREE2appear "reverted" (they're on the original branch which still exists locally, just not checked out here).
- Current branch is now the subagent's branch (
git worktree liststill shows only the original worktrees — no new worktree was created for the subagent.
Expected
isolation: "worktree" should:
- Create a fresh worktree under
.claude/worktrees/(or similar) for the subagent, - Leave the parent worktree's branch and working tree untouched,
- Clean up the new worktree automatically if the subagent made no changes (this part is documented and presumably fine).
Actual
The subagent performs its work directly in the parent's worktree:
git checkout -b <new-branch>switches the parent worktree to the new branch.- Subsequent commits land on the new branch in the parent worktree.
- The parent session's in-memory expectation ("I'm still on
agent/WORKTREE2") silently diverges from git reality.
Impact
- Confusing: the parent session reports files as "reverted" or "missing" when they're actually on a branch that's no longer checked out in this worktree.
- Risky: if the parent session blindly commits/edits without checking the branch, commits can land on the subagent's branch unintentionally.
- The parent's recovery is simple (
git checkout <original-branch>), but only after noticing the issue.
Workaround
Parent session explicitly re-checks git branch --show-current before any commit or push, and runs git checkout <original-branch> if the current branch is unexpected. Not a fix — just defensive.
Session context
This happened in a long-running session working on PR #727 (wild-rumpus repo). The full chain: original parent branch agent/WORKTREE2 → subagent #1 created fix/ci-workflow-restore (repro 1) → subagent #2 created fix/sync-open-orders-test-mocks (repro 2). Both subagents claimed to be running in an isolated worktree in their own reports. Both instead modified the parent worktree's checked-out branch.
If it helps: the primary clone at C:/Projects/Wild Rumpus was also on main and held that worktree, so the subagent couldn't have checked out main there — which may be why it fell back to operating on the parent worktree instead of creating its own. Possibly related to a branch-already-checked-out conflict when trying to worktree add with the same branch name.
Suggested fix ideas
- When
isolation: worktreecan't create its target worktree (because the branch is checked out elsewhere or a path collision), fail the subagent spawn with a clear error instead of silently falling back to operating on the parent. - After subagent completion, the parent harness could automatically verify
git branch --show-currenthasn't changed, and alert the user if it has.
Activity
macyabbey commentedon May 12, 2026
Confirming this reproduces on macOS (Darwin 24.6.0, zsh) — the
platform:windowslabel is too narrow. I've seen this 11+ times across sessions in one monorepo.Trigger pattern in our environment:
Reliable repro is parallel
Agenttool dispatches withisolation: "worktree"that all base offmain. The first dispatch wins the worktree; subsequent concurrent dispatches silently fall back to the parent worktree and check out their new branches there. Serializing dispatches (waiting for each to complete before spawning the next) avoids it entirely.This corroborates @gdennie11's branch-collision hypothesis: when multiple subagents race to
git worktree addfrom the same base branch, the secondworktree addfails (because the base is already checked out by a sibling worktree) and the code path falls through to operating in the parent's working tree rather than erroring.Operational mitigation we've adopted (not a fix — blast-radius limiting):
A pre-dispatch hook that runs
git status --porcelainandgit branch --show-currentand halts if the working tree isn't on the expected branch. It catches the contamination at the next dispatch, but parallel dispatches already in flight when the collision happens still corrupt state.Why this isn't an edge case:
run_in_background: trueon theAgenttool is documented for parallelism. Any workflow that uses it as designed will hit this. Suggested fix #1 in the OP (fail fast with a clear error whenworktree addcan't create the target) would be sufficient — callers can serialize on the error instead of silently landing commits on the wrong branch.(Also — given the active-bug nature and the cross-platform repro, this probably shouldn't be carrying the
stalelabel.)feat(sprint): isolate sprint items in dedicated git worktrees
docs(agents): add worktree-dispatch safety rules to CLAUDE.md + AGENT…
docs(agents): add worktree-dispatch safety rules (PP-484c) (#1353)
fix(worktree): WorktreeCreate retry+flock hook + Husky main-worktree …
github-actions commentedon Jun 12, 2026
Closing for now — inactive for too long. Please open a new issue if this is still relevant.
github-actions commentedon Aug 14, 2026
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.