[BUG] Agent with isolation: "worktree" contaminates parent session CWD
Environment
- Claude Code CLI (latest as of 2026-03-18)
- Linux (WSL2, Ubuntu)
- Model: claude-opus-4-6
- Platform: linux
Description
When launching subagents via the Agent tool with isolation: "worktree", the parent session's CWD gets changed to the subagent's worktree path. When the subagent completes without changes and its worktree is automatically cleaned up, the parent session is left with a broken CWD pointing to a partially-deleted directory.
This effectively renders the parent session unusable — all subsequent Bash tool calls fail with "Working directory no longer exists", and cd commands to valid paths are rejected.
Steps to reproduce
- Start a Claude Code session in a git repository (
/home/user/projects/myrepo) - In a single message, launch two agents in parallel using the
Agenttool, both withisolation: "worktree"(e.g., two research/exploration tasks) - Both agents create worktrees under
.claude/worktrees/agent-<id>/ - Agents complete their read-only tasks and return results
- Worktrees are automatically cleaned up (git deregistered, source files removed)
Expected behavior
The parent session's CWD should remain at the original repository path (/home/user/projects/myrepo) throughout. Subagent worktree creation and cleanup should be fully isolated from the parent session.
Actual behavior
- The parent session's CWD is changed to one of the subagent's worktree paths (
.claude/worktrees/agent-<id>/) - After worktree cleanup, the directory still exists as a skeleton (only
.claude/settings.local.jsonremains) but is no longer a git repository git worktree listdoes not include this directory- All subsequent Bash commands show:
Shell cwd was reset to /path/.claude/worktrees/agent-<id> - The session cannot recover — even explicit
cdto valid paths is rejected with "Working directory no longer exists"
Evidence from the affected session
# Parent session's "Primary working directory" (from system prompt):
/home/user/projects/tarifservice/.claude/worktrees/agent-abe98f8c
# But git only knows about a different worktree:
$ git worktree list
/home/user/projects/tarifservice aac824a88 [chore/just-recipes-cleanup]
/home/user/projects/tarifservice/.claude/worktrees/agent-ac35f8a3 aac824a88 [...] locked
# The contaminated path exists but is empty:
$ ls -laR .claude/worktrees/agent-abe98f8c/
.claude/
settings.local.json # only file remaining
Two agents were launched in parallel:
- Agent
abe98f8c(research task, 118s) → worktree created, then cleaned up → this path became the parent CWD - Agent
ac35f8a3(exploration task, 68s) → worktree still exists and is locked
Hypothesis
When multiple agents with isolation: "worktree" are launched in parallel, the worktree creation of one agent may inadvertently set the parent session's CWD. This could be a race condition in the worktree setup logic.
Workaround
None found within the session. The only recovery is to exit and start a new session.
Related issues
- #29110 — Worktree cleanup destroys uncommitted agent work (similar cleanup issue, different symptom)
- #30906 — Worktree CWD not restored on session resume (CWD management issue, different trigger)
- #27749 — Worktree agents have pwd set to main repo (inverse of this bug)
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗