[Bug] Sub-agents in worktrees cause persistent working directory drift

Resolved 💬 4 comments Opened Apr 1, 2026 by rtimmons Closed May 27, 2026

Bug Description
Sub-agents in worktrees regularly result in "cwd drift" and require extensive hooks and checks to combat. This is clearly a bug. It seems related to anthropics/claude-code#27881 but different - it doesn't just happen after compacting. cwd drift is a big bug and causes massive headaches when resolving sub-agent workflows.

Environment Info

  • Platform: darwin
  • Terminal: iTerm.app
  • Version: 2.1.89
  • Feedback ID: cee86edb-ac7e-4d7e-82c1-bd25e26d2d8d

Every time a subagent completes with isolation: "worktree", the parent session's CWD is left pointing at the worktree directory. The worktree is often
cleaned up immediately after, so the next command in the parent session runs from a path that no longer exists, or from inside a stale worktree rather
than the project root.

This is not only a post-compaction issue as reported in #27881. It happens on every Agent tool call with isolation: "worktree", even in short sessions
with no compaction.

Steps to reproduce

  1. Open a project that is a git repo.
  2. In a Claude Code session, call Agent(isolation: "worktree", ...) with any prompt.
  3. After the agent completes, run pwd in the parent session.
  4. Observe: CWD is inside .claude/worktrees/<agent-id>/ rather than the project root.
  5. Run any subsequent git command — it operates from the wrong directory.

Expected behavior

After Agent(isolation: "worktree") returns, the parent session's CWD should be restored to exactly what it was before the call. ExitWorktree is supposed
to call restoreSessionToOriginalCwd, but it either doesn't run, runs too late, or doesn't take effect in the parent context reliably.

Actual behavior

CWD is left inside the worktree path. If the worktree was cleaned up, subsequent Bash commands fail silently or operate from a deleted directory. If the
worktree was not cleaned up, subsequent Agent(isolation: "worktree") calls create nested worktrees (the next worktree is created relative to the drifted
CWD, not the repo root), compounding the problem.

Impact

This breaks multi-agent workflows entirely. Any orchestrator pattern — spawning multiple subagents in sequence or parallel — requires defensive CWD
checks and recovery logic after every single Agent call. We've had to add three separate hooks (PostToolUse on Agent, PostCompact, and a PreToolUse
blocker) plus a documented recovery command just to make basic subagent workflows reliable. This is table-stakes correctness for a tool whose primary use
case is agentic workflows.

Workaround

Add a PostToolUse hook on Agent that checks whether CWD has left the project root and injects a recovery command:

cd "$(dirname "$(git rev-parse --git-common-dir)")"

This is a hack and is unreliable. CWD restoration after a tool call should be a framework guarantee, not something users need to paper over with hooks.

Environment

  • Claude Code (CLI)
  • macOS (darwin)
  • Git worktrees via Agent(isolation: "worktree")

View original on GitHub ↗

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