Subagent worktree isolation: Write/Edit sandbox CWD detaches from the git worktree (nested-worktree dispatch)
Summary
When a subagent is dispatched with worktree isolation, its Write/Edit sandbox working directory can be a stray non-git directory that is NOT the actual git worktree where its Bash/git commands operate. File edits land in the stray dir while Bash/git/build run against the real worktree, so committed content silently diverges from edited content until the agent happens to notice.
Observed (nested-worktree dispatch)
The parent session was itself already running inside a linked worktree (e.g. .../auto-20260604T065258). A background build agent dispatched with isolation: worktree reported:
My sandboxed Write/Edit working directory (.../worktrees/agent-<id>) is a stray non-git directory, NOT the actual git worktree. The real worktree is its parent, which is where Bash/git operate. My file writes initially landed in the stray dir while git/test/build ran against the parent. I detected this viagit worktree list(the stray dir had no.git), copied all files into the real worktree, and re-ran all gates before committing. The Edit tool stays pinned to the stray path, so further edits need an edit-then-copy bridge.
A careful agent recovered (detected the split, bridged files, re-ran gates, verified the pushed result). A less careful agent would have committed an empty/partial change while believing its edits were in the tree.
Why it matters
- Silent divergence between what an agent edits and what it commits is a correctness hazard; it is invisible unless the agent inspects
git worktree list. - It forces an edit-then-copy bridge workaround that every nested-worktree subagent must rediscover.
Expected
For an isolation-worktree subagent, the Write/Edit sandbox CWD and the Bash/git CWD should resolve to the SAME path (the real linked worktree), especially when the parent session is already inside a worktree (the nested case). Alternatively, fail fast: if the isolation worktree path has no .git, abort the dispatch rather than hand the agent a detached sandbox dir.
Scope note
This is harness-level agent-isolation behavior (creation of the .claude/worktrees/agent-<id> worktree and the Write/Edit sandbox CWD). It cannot be fixed from a user PreToolUse hook: the agent worktree does not exist yet when the Agent-tool hook fires. Filing as the upstream half of a downstream tracking issue.
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗