claude-code - 💡(How to fix) Fix Agent tool: isolation: "worktree" creates worktree from default branch instead of parent HEAD [3 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#57768Fetched 2026-05-11 03:25:55
View on GitHub
Comments
3
Participants
2
Timeline
8
Reactions
0
Timeline (top)
labeled ×4commented ×3closed ×1

When dispatching a subagent via the Agent tool with isolation: "worktree", the created git worktree is sometimes based on the repository's default branch (main) or the initial commit, rather than the parent session's current HEAD. This causes subagents to operate on a stale tree and either silently regress files or fail downstream consistency checks.

Root Cause

When dispatching a subagent via the Agent tool with isolation: "worktree", the created git worktree is sometimes based on the repository's default branch (main) or the initial commit, rather than the parent session's current HEAD. This causes subagents to operate on a stale tree and either silently regress files or fail downstream consistency checks.

Fix Action

Workaround

We routed around it by avoiding isolation: "worktree" entirely and running subagents in the parent checkout sequentially. We also added a guard that captures EXPECTED_BASE=\$(git rev-parse HEAD) in the parent and asks the subagent to verify on entry; the guard reliably fires when the bug occurs, confirming the divergence is between parent HEAD and worktree HEAD (not stale-parent vs current-parent).

RAW_BUFFERClick to expand / collapse

Summary

When dispatching a subagent via the Agent tool with isolation: "worktree", the created git worktree is sometimes based on the repository's default branch (main) or the initial commit, rather than the parent session's current HEAD. This causes subagents to operate on a stale tree and either silently regress files or fail downstream consistency checks.

Environment

  • Claude Code 2.1.138
  • macOS (Darwin 25.3.0)
  • Repos affected: multiple, all using a feature/epic branch off develop (i.e. not the repository's default branch)

Reproduction

  1. In a git repo, check out a feature/epic branch that is not the repository's default branch and has commits beyond it.
  2. From a Claude Code session on that branch, dispatch a subagent with isolation: "worktree".
  3. Have the subagent run git rev-parse HEAD and git log -1 --oneline as its first action.
  4. Compare to the parent session's git rev-parse HEAD.

Expected: worktree HEAD == parent HEAD (or at minimum, contains parent HEAD as an ancestor with no divergence).

Actual: worktree HEAD is the repository's default branch tip, or in some cases the repository's initial commit. The current branch's commits are absent.

Impact

Subagents see "out-of-date" files (config, build manifests, lockfiles) and "helpfully fix" them, producing edits that look like regressions when merged. We have observed this twice in production multi-agent runs across two different repos.

Workaround

We routed around it by avoiding isolation: "worktree" entirely and running subagents in the parent checkout sequentially. We also added a guard that captures EXPECTED_BASE=\$(git rev-parse HEAD) in the parent and asks the subagent to verify on entry; the guard reliably fires when the bug occurs, confirming the divergence is between parent HEAD and worktree HEAD (not stale-parent vs current-parent).

Asks

  1. Document what base ref isolation: "worktree" uses (HEAD? default branch? something else?).
  2. If it is not parent HEAD, expose a parameter (e.g. worktreeBase: "HEAD" | <ref>) so callers can pin it.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Agent tool: isolation: "worktree" creates worktree from default branch instead of parent HEAD [3 comments, 2 participants]