Agent isolation:'worktree' cwd pin drifts back to main repo mid-run — git-mutating commands land in wrong repo
Agent isolation:'worktree' cwd pin drifts back to main repo mid-run
Summary
A subagent spawned via the Agent tool with isolation: 'worktree' gets its
cwd pinned to a real, separate git worktree at launch (confirmed via git) — but the pin can silently drift back to the main repo
worktree list
root across the agent's own later tool calls. Git-mutating commands issued
after the drift (branch creation, commits) land in the main repo
checkout instead of the isolated worktree, defeating the isolation
guarantee and putting the main repo into an unexpected state.
This looks like the same underlying class of bug as #42282 (closed
not_planned, 2026-05-27), which described the inverse direction (parent
session cwd drifting into a subagent's worktree). Also related: #42837,
#31471, #30906 — all closed not_planned/duplicate/stale-bot-closed,
none with a linked fix. Filing fresh per the stale-close bot's own
instruction ("if you're experiencing a similar issue, please file a new
issue").
Repro
- Spawn a subagent via the Agent tool with
isolation: 'worktree'. - Harness creates a real worktree under
.claude/worktrees/agent-<id>
(confirmed on disk via git worktree list).
- Subagent runs several tool calls (file edits, test runs) inside the
worktree successfully.
- Subagent runs a git-mutating command (
git checkout -b <branch>, later
git commit) — these land in the main repo checkout, not the
worktree. Main repo ends up checked out on the subagent's branch, with
an unrelated in-progress branch's uncommitted work exposed underneath.
Had to manually stash and rescue the exposed file, then restore the main
repo to its correct branch, before continuing.
Environment
- Claude Code CLI 2.1.205
- macOS (Darwin 24.6.0)
- Monorepo using pnpm workspaces
- Worktree created via Agent tool
isolation: 'worktree'parameter (not
manual git worktree add, not EnterWorktree)
Expected behavior
A subagent's cwd pin to its isolated worktree should hold for the
subagent's entire lifetime — git-mutating commands should never be able to
execute against the main repo when the subagent was launched withisolation: 'worktree'.
Actual behavior
The pin drifts mid-run without any explicit cd by the subagent. Recurred
twice independently in the same project (once supervised, once in an
unattended parallel-agent run), despite prompt-level hardening telling the
subagent to stay inside its worktree — meaning this needs a harness-level
fix, not a prompt-level one.
Impact
Makes isolation: 'worktree' unsafe to rely on for autonomous/unattended
multi-agent runs that mutate git state (e.g. parallel backlog-clearing
workflows) — a subagent can silently corrupt the main repo's working
state instead of staying confined to its worktree.