Agent tool: isolation: "worktree" creates worktree from default branch instead of parent HEAD
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
- In a git repo, check out a feature/epic branch that is not the repository's default branch and has commits beyond it.
- From a Claude Code session on that branch, dispatch a subagent with
isolation: "worktree". - Have the subagent run
git rev-parse HEADandgit log -1 --onelineas its first action. - 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
- Document what base ref
isolation: "worktree"uses (HEAD? default branch? something else?). - If it is not parent HEAD, expose a parameter (e.g.
worktreeBase: "HEAD" | <ref>) so callers can pin it.
4 Comments
Still reproduces on 2.1.138 (2026-05-10).
Test setup
mainat commit113ca78.``
`git checkout -b test-worktree-base-2026-05-10
date > .test-worktree-marker
git add .test-worktree-marker && git commit -m "test: marker commit"
b186e18Parent HEAD now
on the feature branch, one commit ahead ofmain(113ca78). The marker file.test-worktree-marker` exists only on the feature branch.isolation: "worktree". The probe agent's only job was to reportgit rev-parse HEAD,git log -1 --oneline,git branch -a, andls .test-worktree-marker. No file edits, no commits.Result
Probe's worktree:
The worktree was created from
mainrather than the parent's HEAD on the feature branch. The marker file added in the feature commit is absent.Cross-check
Initially I assumed v2.1.128's
EnterWorktreefix and v2.1.133'sworktree.baseRefsetting had resolved this. A probe run frommainitself returned the correct HEAD — but that's a false positive, since when the parent is on the default branch, parent HEAD ≡ default-branch-tip and the buggy and fixed paths are indistinguishable. The bug only surfaces when the parent is on a non-default branch with commits beyond it (the precondition in the original report). The Agent tool'sisolation: "worktree"therefore appears to be a different code path fromEnterWorktree, or the fix is incomplete.Update: workaround found, partially self-resolving.
After more research, this isn't strictly a bug — the behavior I observed is the documented default (
worktree.baseRef: "fresh"=origin/<default>). The fix for my use case is the project setting:This applies to all worktree surfaces —
--worktree,EnterWorktree, and Agent-toolisolation: "worktree"— confirmed by re-running the same feature-branch repro on v2.1.138 with the setting in place: worktree HEAD now matches parent HEAD as expected, and the marker file from the feature commit is present in the worktree. Setting takes effect mid-session.The docs gap that misled me: this is documented at https://code.claude.com/docs/en/worktrees ("Choose the base branch") but does not appear on the settings reference page, which is what I checked first. That gap is already tracked in #57148.
What I'd still like from this issue
The framing as "bug" is wrong, but two asks remain valid as feature requests:
worktree.baseRefis a session/settings-level knob; there's no way for a controller to dispatch one subagent rooted at parent HEAD and another rooted atorigin/main. AworktreeBase: "head" | "fresh" | <ref>parameter on the Agent tool would close this.isolation: "worktree"description should mention that base ref selection is governed byworktree.baseRef, so users don't need to discover it via the worktrees docs page.Feel free to close, repurpose, or split into a separate feature-request issue — whichever is most useful.
This issue was fixed as of version 2.1.133.
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.