Worktree feature is subdirectory-blind in monorepos (loses subdirectory-scoped CLAUDE.md)
Summary
When a Claude Code session is started from a subdirectory of a git repo and the worktree feature is invoked, the resulting worktree session loads the repo-root CLAUDE.md instead of the subdirectory's CLAUDE.md. This breaks the "subdirectory-as-agent-identity" pattern used in monorepos.
Repro
- Have a monorepo where subdirectories define distinct agent identities via per-folder
CLAUDE.md. Example layout:
````
/MyRepo
.git/
CLAUDE.md ← orchestrator identity
AgentA/
CLAUDE.md ← AgentA identity
AgentB/
CLAUDE.md ← AgentB identity
- Start a Claude Code session with cwd =
/MyRepo/AgentA/. Confirm session loads AgentA'sCLAUDE.md. - From that session, create a worktree (Claude Code worktree feature).
- The worktree is created at
/MyRepo/AgentA/.claude/worktrees/<name>/, but its.gitpointer resolves to the parent repo (/MyRepo/.git/worktrees/<name>), so the worktree's working tree is the full repo (withCLAUDE.md,AgentA/,AgentB/, etc. as direct children). - The worktree session opens at the worktree root → loads root
CLAUDE.md, notAgentA/CLAUDE.md.
Why this happens
git worktree add always creates a working tree of the entire repo — there is no subdirectory-scoped worktree in git. Claude Code's worktree feature uses git worktrees under the hood, then starts the session at the worktree root, which discards the original cwd's subdirectory position.
Expected behavior
When a session is invoked from subdirectory <S> of repo <R> and creates a worktree, the new worktree session should start with cwd = <worktree-root>/<S>, not <worktree-root>. That preserves the agent identity defined by the per-subdirectory CLAUDE.md.
Impact
For monorepos that use per-folder CLAUDE.md to scope agent identity (a common multi-agent pattern), the worktree feature silently swaps the agent under you. You think you're running AgentA, you're actually running the orchestrator. We discovered this only when the agent answered "who am I?" with the wrong identity mid-task.
Suggested fix
Record the subdirectory offset between the session cwd and the enclosing repo root at worktree-creation time. When starting the worktree session, cd into <worktree-root>/<offset> before loading instructions.
Workaround
Manually cd into the equivalent subdirectory inside the worktree and start a new session there.
---
Generated by AI (Claude)
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗