[BUG] Edit tool and Agent tool don't auto-resolve paths to worktree, silently edit main repo files
Description
When operating inside a worktree (.claude/worktrees/<name>/), the Edit tool and Agent tool do not automatically map file paths to the worktree. This causes edits to silently land on the main repo files instead of the worktree copy.
The system prompt correctly states the worktree path, but tools operate on whatever absolute path is given. Since Agent (Explore subagent) returns paths rooted at the main repo (e.g., /path/to/repo/file.py instead of /path/to/repo/.claude/worktrees/<name>/file.py), subsequent Edit calls using those paths modify the wrong files.
Steps to Reproduce
- Start a session in worktree mode (CWD =
.claude/worktrees/<name>/) - Launch an Explore Agent to research the codebase
- Agent returns file paths like
/Users/x/repo/check_test_quality.py(main repo path) - Use
Edittool with that path — edit succeeds on the main repo file - Run tests in worktree — they see the old code, not the edit
- Confusion ensues until you realize the wrong file was edited
Impact
- Silent data corruption: main repo files get unintended changes
- Wasted debugging time: tests pass/fail unexpectedly because edits went to the wrong file
- In my case, I had to
git checkout -- file1 file2on the main repo to revert accidental edits, then redo all edits with the full worktree path
Expected Behavior
When the session is in a worktree, tools should either:
- Auto-resolve relative or main-repo paths to the worktree equivalent, or
- Reject edits to files outside the worktree with a clear error message
Option 1 is strongly preferred — the whole point of worktree mode is isolation.
Environment
- Claude Code Desktop (macOS)
- Model: claude-opus-4-6
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗