anthropics / anthropics/claude-code

[BUG] Agent tool `isolation: "worktree"` creates worktree from stale session-start snapshot (old HEAD + old dirty files), not current HEAD

Open
#81,361 0 comments 0 reactions 0 assignees View on GitHub
area:agents
Dominant language
Python
Stars
145k
Forks
23.1k
PR merge metrics
PR metrics pending

Description

## 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 status` showing **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 `) and passes the path in the brief instead of using `isolation: "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.

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the issue by running a long CLI session through /compact, making commits after session start, and launching an Agent with isolation: "worktree". Compare the auto-created worktree's git log and git status with the current repository and the session-start state. Done means the worktree reflects the current HEAD rather than replaying stale commits or dirty files.

Written by the indexing model from the issue text.

Assessment

Tech stack
git
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.