[BUG] Agent tool `isolation: "worktree"` creates worktree from stale session-start snapshot (old HEAD + old dirty files), not current HEAD
Environment
- Claude Code v2.1.220, macOS (darwin 24.5.0), CLI
- Model: claude-fable-5 (coordinator) spawning subagents via the Agent tool
Bug
When a subagent is launched with isolation: "worktree" late in a long session (session had been running for hours and went through /compact at least once), the auto-created worktree is not based on the current HEAD of the repo. Instead it reproduces a snapshot of the repo state from the moment the session started: the old HEAD plus the exact set of dirty/untracked files that existed at session start.
Evidence (one session, 3 occurrences on 2026-07-26)
Main tree at delegation time: HEAD 70f5ead7, tests/aaf = 31 files.
Session start state (from the session's initial gitStatus snapshot): HEAD 2208e60d, 5 modified + 2 untracked files.
All three auto-created worktrees (.claude/worktrees/agent-*) contained:
- HEAD
2208e60d(= session-start HEAD, dozens of commits behind current) tests/aaf= 19 files (session-start count)git statusshowing exactly the same 5 modified + 2 untracked files as the session-start snapshot — files that had long been committed in the main tree
The dirty-file match makes it clear this is a cached session-start snapshot being replayed, not a race or a wrong-branch checkout.
Impact
In an earlier occurrence (same day, before we added guards) a subagent received the stale base and, in good faith, re-implemented 15 commits of already-finished work, which then had to be detected and discarded during review. Silent data-duplication risk for any workflow that delegates writing work to isolated subagents in long sessions.
Workarounds we use now
- Every writing-subagent brief starts with a base assert (
git log -1== expected SHA + a file-count signature; mismatch → STOP). This caught occurrences 2 and 3 before any edit. - Coordinator creates the worktree manually (
git worktree add --detach /tmp/wt-task <sha>) and passes the path in the brief instead of usingisolation: "worktree".
Expected behavior
isolation: "worktree" should create the worktree from the repo's current HEAD (and arguably current dirty state), or at minimum refresh its snapshot instead of reusing the session-start one after /compact/long uptime.