Context compaction loses absolute paths, causing wrong directory operations after compaction
Bug Description
When working in a git worktree with absolute paths, context compaction converts these paths to relative paths in the summary. After compaction, since Bash cwd resets to the original working directory, subsequent file operations target the wrong directory.
Steps to Reproduce
- Start Claude Code in main repository (e.g.,
/home/user/project) - Create and switch to a git worktree (e.g.,
/home/user/project-fix-123) - Perform file operations using absolute worktree paths
- Continue working until context compaction occurs
- After compaction, file operations target main repository instead of worktree
Expected Behavior
Context compaction summary should preserve absolute paths, especially when they differ from the current working directory.
Actual Behavior
The compaction summary converts absolute paths to relative paths:
Before compaction (line 340 of transcript):
Read file: /home/user/project-fix-123/apps/api/module/mapper.py
After compaction (line 345 - summary):
Was implementing in `apps/api/module/mapper.py`
Edit `apps/api/module/mapper.py` to add...
After compaction (line 350+):
Read file: /home/user/project/apps/api/module/mapper.py # Wrong path!
The worktree absolute path /home/user/project-fix-123/ is completely lost in the summary.
Impact
- Code changes applied to wrong branch (main instead of feature branch)
- Commits made to wrong repository
- Significant token waste from having to redo work
- Potential for accidentally pushing wrong changes
Environment
- Claude Code version: 2.1.29
- OS: Linux (WSL2)
- Model: claude-opus-4-5-20251101
Workaround
Added explicit instructions in custom skill files to always use absolute paths, but this doesn't prevent the compaction summary from losing path information.
Evidence
Full transcript available showing the path switch after compaction. The transcript clearly shows:
- Correct worktree path used before compaction
- Summary generated without absolute path
- Wrong main path used after compaction
Additional Context
This issue was more frequent in earlier versions but still occurs. The combination of:
- Bash cwd resetting after each command
- Context compaction losing absolute paths
Creates a particularly problematic failure mode for worktree-based workflows.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗