[Bug] Plan agent uses main branch path instead of git worktree directory
Bug Description
I understand the issue - when you're working in a git worktree like
/Users/dave/code/shingleai/.conductor/monaco-v1, the Plan agent is
incorrectly trying to access files from the main branch location at
~/code/shingleai instead of your current working directory.
This is a bug in how the Plan agent handles working directory context when
invoked. The Plan agent should inherit and respect the working directory
from the parent session, but it appears to be either:
- Not receiving the correct working directory context
- Making incorrect assumptions about the repository root
- Resolving paths relative to the main git directory instead of the
worktree
Recommended Actions
Report this issue:
You can report this at https://github.com/anthropics/claude-code/issues
with details like:
- You're using git worktrees
- Current working directory:
/Users/dave/code/shingleai/.conductor/monaco-v1
- Plan agent incorrectly tries to access: ~/code/shingleai
- Expected: Plan agent should use the current working directory
Temporary Workaround
In the meantime, when the Plan agent gives you a plan, you may need to:
- Review the plan carefully for any incorrect path assumptions
- Manually correct paths when executing the plan
- Or avoid using plan mode for tasks that require precise file location
awareness
Environment Info
- Platform: darwin
- Terminal: iTerm.app
- Version: 2.0.36
- Feedback ID:
Errors
[{"error":"AxiosError: Request failed with status code 403\n at WT (file:///Users/dave/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:51:1136)\n at Unzip.<anonymous> (file:///Users/dave/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:56:9845)\n at Unzip.emit (node:events:520:35)\n at Unzip.emit (node:domain:489:12)\n at endReadableNT (node:internal/streams/readable:1701:12)\n at process.processTicksAndRejections (node:internal/process/task_queues:90:21)\n at sGA.request (file:///Users/dave/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:58:2129)\n at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n at async ft2 (file:///Users/dave/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:3771:7832)","timestamp":"2025-11-08T02:08:15.925Z"},{"error":"AxiosError: Request failed with status code 403\n at WT (file:///Users/dave/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:51:1136)\n at Unzip.<anonymous> (file:///Users/dave/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:56:9845)\n at Unzip.emit (node:events:520:35)\n at Unzip.emit (node:domain:489:12)\n at endReadableNT (node:internal/streams/readable:1701:12)\n at process.processTicksAndRejections (node:internal/process/task_queues:90:21)\n at sGA.request (file:///Users/dave/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:58:2129)\n at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n at async _f8 (file:///Users/dave/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:868:467)\n at async Promise.all (index 0)\n at async file:///Users/dave/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:854:41014","timestamp":"2025-11-08T02:17:12.093Z"}]
16 Comments
This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.
This still occurs in 2.1 and also with the Explore agent. I'm not really sure how I can leverage worktrees to have multiple Claude instances work in parallel if they won't actually stay within their own worktree...
I'm experiencing it, too.
Same issue here on macOS. I can confirm the root cause:
Claude Code follows
git rev-parse --git-common-dir(which returns the main repo's.git) instead ofgit rev-parse --show-toplevel(which correctly returns the worktree path).From inside the worktree:
| Command | Output |
|---------|--------|
|
git rev-parse --show-toplevel|/path/to/my-worktree✅ ||
git rev-parse --git-common-dir|/path/to/main-repo/.git❌ |The worktree's
.gitfile containsgitdir: /path/to/main-repo/.git/worktrees/..., and Claude Code follows that pointer back to the main repo to resolve the project root.Result: Even when launching
claudefrom inside the worktree directory, the session environment showsWorking directory: /path/to/main-repo. Every Bash call resets to the main repo. Every file edit targets the main repo.cdinto the worktree doesn't persist.This completely breaks worktree-based isolation. In my case it led to accidental edits on the production branch and reverted work.
The fix: Use
--show-toplevelinstead of--git-common-dirwhen resolving the project root.--show-toplevelcorrectly respects worktrees.Also having this issue.
Also having this issue
Still having this issue too, making worktrees in claude unreliable to use...
also having this
@Anfed-2030 is this something you used to fix this locally for you or are you suggesting this to the Claude Code team?
Update:
I think the answer is this is a Claude Code side change that needs to happen.
As a work around i'm trying the following in my claude.md
Update 2:
Seems to work! My next plan after this had this at the top
As expected when clearing and executing the plan it landed back on main but did cd immediately to the worktree
``` shell
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
⏺ I'll start by reading the files in the worktree to understand the current state.
⏺ Bash(cd /Users/arjun/source/xxx/.claude/worktrees/mer-397-xxx-xxx-xxx/)
⎿ Done
Also seeing this on 2.1.74.
Additional note: The built-in /simplify skill seems to rely on
git diffto locate the plan file alongside implementation changes. However, since the plan file is written to the main repo path (outside the worktree),git diffin the worktree never picks it up. As a result, /simplify can't cross-reference the plan with the implementation—it only sees the code changes.(I haven't directly verified /simplify's internals, but the behavior matches this hypothesis.)
@bcherny this is a pretty big one mate. Surprised this is still open. Seeing a lot of dedupe bot running around tagging this but seems like it's not bubbling up at all?
At this point i'm hesitant to even submit bugs since it seems like they go into a void and never get addressed. CC has been accruing bugs lately. Do y'all have a plan for ranking severity / priority? The dedupe bot doesn't appear to be working and seems to just link many related issues together and then closing new bug reports without emphasizing the single representative ticket people should go upvote etc. This seems like a process failure.
suffering from this also with code-review:code-review skill subagents. Directed main agent to always include worktree path as mandatory into subagent's flow - works for some time or several PRs and then reverts back to this misbehavior. I even threatened to terminate the main if it allows this to reoccur, did not hold for long.
I notices that it most often happens when subagents are Haiku or Sonnet - less prompt-following models.
This is preventing me from using /ultraplan
I have recently moved to using Claude in devcontainers. Has several advantages (mostly security related) but it inadvertently solved this issue (I give Claude full access to /workspace where I mount my git repo).
The custom sub agents also does the same, so spawning parallel sub agents will got the bad results, too.
I am experiencing the same issue. I have a framework where I use multiple subagents and I end up with bunch of random changes in the main dir, not the worktree
Workaround: PreToolUse hook that blocks (and auto-corrects) main-tree edits from worktree sessions
Until this is fixed, you can fully stop the silent main-tree edits with a
PreToolUsehook. It only activates when the sessioncwdis under.claude/worktrees/, denies anyEdit/Write/NotebookEdittargeting a file outside the worktree, and - when the bad path points into the main repo - echoes the equivalent worktree path back to the model so the agent self-corrects on the next attempt. Hooks also run inside subagents, which is exactly where this bug bites (subagents inherit a project root fromgit rev-parse --git-common-dir, i.e. the main repo)..claude/hooks/guard-worktree.sh(chmod +xit):.claude/settings.json:Verified behavior:
| Scenario | Result |
|---|---|
| Main-repo absolute path from a worktree session | ❌ blocked, message suggests the corrected worktree path |
|
../../../escape out of the worktree | ❌ blocked || Path inside the worktree (absolute or relative) | ✅ allowed |
| Edits in a normal (non-worktree) session | ✅ allowed, hook is a no-op |
In practice the suggestion line matters: the agent retries with the corrected path immediately instead of getting stuck.
Known gap:
Bash-based file writes (sed -i,tee,git -C ..) aren't covered since arbitrary shell can't be reliably parsed - but agents do file changes via Edit/Write, so this catches the failure mode described in this issue. If your worktrees live somewhere other than.claude/worktrees/, adjust the twocasepatterns accordingly.