[BUG] Agent with isolation: "worktree" contaminates parent session CWD

Resolved 💬 3 comments Opened Mar 18, 2026 by Kalipso0505 Closed Mar 22, 2026

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

  1. Start a Claude Code session in a git repository (/home/user/projects/myrepo)
  2. In a single message, launch two agents in parallel using the Agent tool, both with isolation: "worktree" (e.g., two research/exploration tasks)
  3. Both agents create worktrees under .claude/worktrees/agent-<id>/
  4. Agents complete their read-only tasks and return results
  5. 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.json remains) but is no longer a git repository
  • git worktree list does 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 cd to 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)

View original on GitHub ↗

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