[Bug] Memory files loaded twice in git worktrees causing premature compaction and quota exhaustion
[Bug] Memory files loaded twice in git worktrees causing premature compaction and quota exhaustion
Description
When working inside a git worktree, memory files (.claude/rules/*.md and CLAUDE.md) are loaded twice in the context:
- Once with absolute path (
~/path/to/repo/.claude/rules/file.md) - Once with relative path (
.claude/rules/file.md)
This doubles the memory file token usage and causes:
- Context filling up immediately after
/clear - Premature context compaction (every 3-4 messages instead of normal behavior)
- Rapid session and weekly quota exhaustion due to excessive token consumption
Environment
- Claude Code version: 2.1.37
- OS: Linux 6.14.0-123037-tuxedo
- Model: claude-sonnet-4-5-20250929
- Context: Git worktree (
~/PhpstormProjects/neo/.worktrees/plan-05-mcp-meilisearch)
Steps to Reproduce
- Create a git worktree:
``bash``
git worktree add .worktrees/my-branch my-branch
- Navigate to the worktree:
``bash``
cd .worktrees/my-branch
- Start Claude Code:
``bash``
claude
- Run
/clearto reset context
- Immediately run
/contextto check token usage
Actual Behavior
After /clear, context is already at 73% capacity (146k/200k tokens) with:
- Memory files: 109k tokens (54.5%)
- Messages: 106 tokens (0.1%)
- All
.claude/rules/*.mdfiles are listed twice in/contextoutput
Example /context output:
Memory files · /memory
└ ~/PhpstormProjects/neo/CLAUDE.md: 1.1k tokens
└ ~/PhpstormProjects/neo/.claude/rules/00-regles-critiques.md: 1k tokens
└ ~/PhpstormProjects/neo/.claude/rules/01-environnement-docker.md: 351 tokens
[... 32 more files ...]
└ CLAUDE.md: 1.1k tokens ← DUPLICATE
└ .claude/rules/00-regles-critiques.md: 1k tokens ← DUPLICATE
└ .claude/rules/01-environnement-docker.md: 351 tokens ← DUPLICATE
[... 32 more duplicates ...]
File verification shows:
- Different inodes (physical files, not symlinks)
- Different MD5 hashes (contents have diverged slightly)
- Same number of files in both locations (35 rules files)
Expected Behavior
Memory files should be loaded once only, either from:
- The worktree location (relative path), OR
- The main repository location (absolute path)
Context after /clear should be ~25-30% capacity (50-60k tokens), not 73%.
Impact
Session Quota Exhaustion
With context at 73% capacity from the start:
- Only ~50k tokens remaining for conversation
- Compaction triggers after 3-4 normal messages
- Compacted context loses recent conversation details
- Session quota exhausted much faster than normal
Weekly Quota Exhaustion
Due to premature compaction and double token loading:
- Each session consumes 2x expected tokens
- Weekly quota depleted in ~50% normal time
- Significant cost impact for users with many worktrees
Investigation Details
File Analysis
# Different inodes (not symlinks)
$ ls -i ~/PhpstormProjects/neo/.worktrees/plan-05-mcp-meilisearch/.claude/rules/00-regles-critiques.md
54944005 ...
$ ls -i ~/PhpstormProjects/neo/.claude/rules/00-regles-critiques.md
50728835 ...
# Different content (MD5)
$ md5sum ~/PhpstormProjects/neo/.worktrees/plan-05-mcp-meilisearch/.claude/rules/*.md | head -3
8c56ba221170c9472ca20be251e88fd8 .../00-regles-critiques.md
b72f1b931986c51aaa79fabc04685dd3 .../01-environnement-docker.md
373ada24edb2cbd8edee9768c670e315 .../02-workflow-git.md
$ md5sum ~/PhpstormProjects/neo/.claude/rules/*.md | head -3
8c56ba221170c9472ca20be251e88fd8 .../00-regles-critiques.md # Same
b72f1b931986c51aaa79fabc04685dd3 .../01-environnement-docker.md # Same
373ada24edb2cbd8edee9768c670e315 .../02-workflow-git.md # Same
Worktree Setup
$ git worktree list
/home/atournayre/PhpstormProjects/neo [release/v27.0.0]
/home/atournayre/PhpstormProjects/neo/.worktrees/plan-05-mcp-meilisearch [feature/plan-05-mcp-meilisearch]
# ... 8 other worktrees with same issue
Suggested Fix
The memory file discovery logic should:
- Detect when running inside a git worktree
- Load memory files from either worktree or main repo (not both)
- Deduplicate by file content hash or inode to prevent double-loading
Alternative: Add configuration option to specify which location to use for memory files in worktrees.
Workaround
Temporarily, users can:
- Delete
.claude/directory from worktree to force loading from main repo only - Work from main repository instead of worktrees (not ideal for parallel development)
Additional Context
This issue affects all git worktrees in the project (9 worktrees confirmed with same behavior).
The bug was noticed after recent updates to Claude Code (v2.1.37), suggesting it may be a regression in memory file resolution logic.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗