Worktree rules loaded twice when worktree is inside .claude/worktrees/
Description
When using claude -w <name> to create a worktree, the worktree is placed inside .claude/worktrees/<name>/, which is a subdirectory of the main repository.
Claude Code's rule discovery walks upward from the current working directory to find project roots. In a worktree session, it finds two project roots:
- The worktree at
.claude/worktrees/<name>/(has its ownCLAUDE.md,AGENTS.md,.claude/rules/) - The main repo at the parent directory (has the original copies)
This causes every .claude/rules/*.md file, CLAUDE.md, and AGENTS.md to be loaded twice into the context — once from each project root.
Impact
~26K extra input tokens per message in my setup (13 rule files + CLAUDE.md + AGENTS.md, all duplicated). This adds up significantly over a session.
Both paths loaded
# From worktree (inner project root):
.claude/worktrees/<name>/CLAUDE.md
.claude/worktrees/<name>/AGENTS.md
.claude/worktrees/<name>/.claude/rules/*.md
# From main repo (outer project root):
CLAUDE.md
AGENTS.md
.claude/rules/*.md
These are identical files (same git content) loaded twice.
Suggested fix
When discovering project roots by walking upward, if two roots share the same git rev-parse --git-common-dir, they are the same repository (main + worktree). Deduplicate by only loading rules from the outermost root, or detect that the inner root is a git worktree of the outer root.
Current workaround
Using git sparse-checkout to exclude .claude/rules/, CLAUDE.md, and AGENTS.md from the worktree working copy. This way only the main repo copies are found during the upward walk.
cd .claude/worktrees/<name>
git sparse-checkout init --no-cone
git sparse-checkout set '/*' '!/.claude/rules/' '!/CLAUDE.md' '!/AGENTS.md'
This works but is fragile — it requires manual setup per worktree and can be disrupted by git sparse-checkout disable.
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗