[BUG] Main session CWD drifts to worktree after background agent completes

Resolved 💬 3 comments Opened Mar 24, 2026 by rtimmons Closed Mar 28, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

After spawning a background agent with isolation: "worktree" and run_in_background: true, the main session's working directory silently changes to the agent's worktree directory when the completion notification is delivered. Subsequent Bash tool calls in the main session run from the worktree path instead of the original project root.

What Should Happen?

The main session CWD should be unaffected by background agents. A child process cannot change a parent process's CWD in normal Unix semantics; the same should hold here.

Error Messages/Logs

**Actual behavior:** After the background agent finishes, the main session CWD is the worktree path (e.g. `/private/var/folders/.../worktree-abc123/`) instead of the original project root.

Steps to Reproduce

  1. Open Claude Code in a git repository
  2. Spawn a background agent: Agent({ isolation: "worktree", run_in_background: true, prompt: "..." })
  3. In the main session, run pwd or git branch --show-current before the agent completes — observe correct CWD
  4. Wait for the agent's completion notification
  5. Run pwd or git branch --show-current again in the main session — observe it now returns the worktree path and branch

Workaround: Use absolute paths for any scripts or commands run after agent completion, so the incorrect CWD doesn't affect path resolution. This causes sandbox permissions violations and thus adds an approval confirmation from a human and is a bad workaround.

Platform: macOS (darwin 25.3.0), zsh

Claude Model

Opus

Is this a regression?

I don't know

Last Working Version

_No response_

Claude Code Version

2.1.81 (Claude Code)

Platform

Anthropic API

Operating System

macOS

Terminal/Shell

iTerm2

Additional Information

_No response_

View original on GitHub ↗

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