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.
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗