Skip to content

Agent tool: isolation: worktree switches parent worktree's branch instead of creating isolated worktree #47548

Closed as not planned

Description

@gdennie11

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 branch agent/WORKTREE2. The primary clone lives at C:/Projects/Wild Rumpus.
  • Model: Claude Opus 4.6 (1M context)

Reproduction (happened twice in one session)

  1. Parent session running in a non-primary git worktree (C:/Projects/wild-rumpus-WORKTREE2, branch agent/WORKTREE2).
  2. Spawn an Agent tool call with isolation: "worktree", run_in_background: true, and a prompt that tells the subagent to create a new branch off main, make commits, and push.
  3. Subagent reports success — creates branch fix/ci-workflow-restore (first repro) / fix/sync-open-orders-test-mocks (second repro), commits, pushes, opens a PR.
  4. Parent session continues and eventually runs git status or git log, and discovers:
    • Current branch is now the subagent's branch (fix/ci-workflow-restore), not agent/WORKTREE2.
    • Working tree reflects that branch's state, so files the parent had committed to agent/WORKTREE2 appear "reverted" (they're on the original branch which still exists locally, just not checked out here).
  5. git worktree list still 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: worktree can'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-current hasn't changed, and alert the user if it has.

Activity

  1. added
    bug
    platform:windows
    has repro
    on Apr 13, 2026
  2. added
    stale
    on May 10, 2026
  3. macyabbey commented on May 12, 2026

    @macyabbey

    Confirming this reproduces on macOS (Darwin 24.6.0, zsh) — the platform:windows label is too narrow. I've seen this 11+ times across sessions in one monorepo.

    Trigger pattern in our environment:

    Reliable repro is parallel Agent tool dispatches with isolation: "worktree" that all base off main. 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 add from the same base branch, the second worktree add fails (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 --porcelain and git branch --show-current and 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: true on the Agent tool 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 when worktree add can'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 stale label.)

  4. removed
    stale
    on May 12, 2026
  5. added
    stale
    on May 28, 2026
  6. github-actions commented on Jun 12, 2026

    @github-actions

    Closing for now — inactive for too long. Please open a new issue if this is still relevant.

  7. github-actions commented on Aug 14, 2026

    @github-actions

    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.

  8. locked as resolved and limited conversation to collaborators on Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:agentsbughas reproplatform:windowsstale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions