Worktree: Edit/Read tools use main workspace paths instead of worktree paths

Open 💬 12 comments Opened Mar 19, 2026 by primum-mobile

Bug Description

When Claude Code operates in a git worktree (created automatically by the harness), the Edit, Read, and Write tools consistently target files in the main workspace rather than the worktree directory, even though the system prompt correctly identifies the worktree as the primary working directory.

Steps to Reproduce

  1. Start a Claude Code session that creates/uses a git worktree (e.g. via the automatic branch creation feature)
  2. The system prompt correctly states:
  • Primary working directory: /path/to/repo/.claude/worktrees/name
  • Current branch: claude/name
  1. Ask Claude to edit a file (e.g. workspace_shell.py)
  2. The Edit tool targets /path/to/repo/workspace_shell.py (main workspace) instead of /path/to/repo/.claude/worktrees/name/workspace_shell.py (worktree)

Expected Behavior

All file operations should resolve relative to the worktree path when operating in a worktree. The system prompt explicitly provides the correct working directory, but file paths from Explore agents and subsequent tool calls use the main workspace root.

Actual Behavior

  • Explore agents report file paths using the main workspace root
  • Subsequent Edit/Read/Write calls use those paths verbatim
  • All changes land in the main workspace (often main branch), not the worktree side branch
  • The running app (via preview_start) executes from the worktree and sees none of the changes
  • This is completely invisible to the user — no error is raised, edits succeed silently on the wrong files

Impact

In our session, every edit across ~15 iterations went to the wrong file. The app ran the original unmodified code the entire time. Hours of debugging a "not working" visual change were wasted because the changes were never in the file being executed.

Environment

  • macOS (Darwin 25.3.0)
  • Claude Code CLI with worktree support
  • Model: claude-opus-4-6

Suggested Fix

When operating in a worktree, file tool calls should either:

  1. Resolve relative paths against the worktree root (not the main workspace), or
  2. Validate that absolute paths fall within the worktree and warn/redirect if they don't

View original on GitHub ↗

This issue has 12 comments on GitHub. Read the full discussion on GitHub ↗