Subagent (Task tool) resolves file paths to main tree instead of active worktree, causing edits to land in wrong tree

Resolved 💬 3 comments Opened Feb 26, 2026 by maurorozco Closed Mar 3, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

When running Claude Code in a git worktree (created via claude --worktree or EnterWorktree), subagents spawned via the Task tool (e.g., subagent_type: "Explore") return file paths rooted at the main repository instead of the active worktree. If those paths are then used with the Edit tool, the edits silently land in the main tree — not the worktree the user is working in.

This is a silent correctness/safety issue: there is no warning, no error, and the user ends up with uncommitted changes in the main tree they didn't intend.

Steps to Reproduce

  1. Start Claude Code in a worktree:

``
claude --worktree
`
Working directory becomes something like:
/path/to/project/.claude/worktrees/<name>`

  1. Ask Claude to investigate and fix something in the codebase. Claude spawns an Explore subagent via the Task tool.
  1. The subagent uses Glob/Grep/Read to find relevant files. The returned file paths are rooted at /path/to/project/src/... (the main tree), not /path/to/project/.claude/worktrees/<name>/src/... (the worktree).
  1. Claude uses those paths with the Edit tool. The edit succeeds — but it modifies the file in the main tree, not the worktree.
  1. Running git status in the worktree shows no changes. Running git diff in the main tree reveals the unintended edit.

What Should Happen?

When a session is running inside a worktree, subagents spawned via the Task tool should resolve all file paths relative to the worktree root, not the main repository root. Tools like Glob, Grep, and Read should return worktree-scoped paths so that subsequent Edit/Write calls target the correct tree.

Why This Is Distinct From Related Issues

  • #28248 — Permission prompts show main worktree path (UX issue)
  • #27343CLAUDE_PROJECT_DIR env var points to main repo (env var issue)
  • #28041.claude/ subdirectories not copied to worktree (file availability issue)

All of those are inconveniences. This issue causes edits to silently land in the wrong tree, which is a data integrity problem. The user has no indication anything went wrong until they inspect both trees manually.

Root Cause (Likely)

Same as the related issues: Claude Code resolves the project root via git rev-parse --git-common-dir, which always points to the main repository's .git directory. Subagents likely inherit this resolved path rather than the worktree-scoped working directory.

Error Messages/Logs

None — that's the problem. The edit succeeds silently against the wrong tree.

Claude Model

claude-opus-4-6

Is this a regression?

No, this never worked

Claude Code Version

Latest (as of 2026-02-26)

Platform

Anthropic API

Operating System

macOS (Darwin 24.6.0)

Terminal/Shell

fish

View original on GitHub ↗

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