Subagents resolve to main repo root instead of worktree directory

Resolved 💬 3 comments Opened Mar 6, 2026 by marijnbakker2 Closed Mar 10, 2026

Bug Description

When running Claude Code inside a git worktree (e.g. /repo/.claude/worktrees/my-worktree), the main conversation correctly operates in the worktree directory (pwd returns the worktree path). However, when launching subagents via the Agent tool (e.g. subagent_type: Explore), the agents resolve file paths and operate against the main repository root instead of the worktree.

Steps to Reproduce

  1. Create a git worktree inside a repo (e.g. via EnterWorktree or manually) — the default location is /repo/.claude/worktrees/<name>
  2. Start a Claude Code conversation rooted in the worktree
  3. Launch an Explore agent asking it to run pwd and read files
  4. Observe that the agent reports paths from the main repo, not the worktree

Expected Behavior

Subagents should inherit the worktree as their working directory and read/search files from the worktree, not the main repo.

Actual Behavior

Subagents read from the main repo root. In our case, the main repo had a different branch checked out with different files, so the agents found code that doesn't exist in the worktree — leading to incorrect planning context.

Suggestion: Allow custom worktree location

The root cause is likely that the default worktree path (/repo/.claude/worktrees/<name>) is a subdirectory of the main repo, making it ambiguous which repo root to resolve to.

Suggestion: Allow users to specify a custom location for worktrees when creating them (e.g. next to the main repo rather than inside it). If the worktree lived at /sites/letsbook-worktree-4418/ instead of /sites/letsbook/.claude/worktrees/..., the path resolution issue would not occur since the worktree would not be a subdirectory of the main repo.

This could be a configuration option like:

{
  "worktreeLocation": "sibling"  // or "nested" (current default)
}

Or allow passing a custom path when entering a worktree.

Environment

  • macOS Darwin 24.6.0
  • Claude Code CLI (Opus 4.6)
  • Worktree path: /repo/.claude/worktrees/hashed-stargazing-pancake
  • Main repo path: /repo/ (different branch checked out)

View original on GitHub ↗

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