[BUG] Main session CWD drifts to worktree after background agent completes
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
- Open Claude Code in a git repository
- Spawn a background agent:
Agent({ isolation: "worktree", run_in_background: true, prompt: "..." }) - In the main session, run
pwdorgit branch --show-currentbefore the agent completes — observe correct CWD - Wait for the agent's completion notification
- Run
pwdorgit branch --show-currentagain 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_
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗