Worktree isolation cleanup deletes parent session working directory on agent ID collision

Resolved 💬 4 comments Opened Mar 30, 2026 by OAGr Closed May 3, 2026

Bug

When a Claude Code session is already running inside a worktree (e.g.,
.claude/worktrees/agent-ad324b64), spawning a sub-agent with isolation: "worktree" can delete the
parent session's working directory if the sub-agent's ID prefix collides with the existing worktree name.

## Reproduction

  1. Start a Claude Code session inside a worktree at .claude/worktrees/agent-XXXXXXXX
  2. Use the Agent tool with isolation: "worktree" to spawn a sub-agent
  3. If the sub-agent's ID first 8 chars match XXXXXXXX, it targets the same worktree path
  4. Sub-agent completes with no local changes → cleanup removes the worktree directory
  5. Parent session's working directory no longer exists — all Bash calls fail

## Observed behavior

  • Sub-agent ID ad324b64e63efcf48 was assigned while parent session was in

.claude/worktrees/agent-ad324b64

  • After sub-agent completed, all Bash calls failed with "Working directory no longer exists"
  • A second sub-agent with different ID prefix (a134e0f0../..) had no issues

## Expected behavior

Worktree cleanup should check whether the path is the current session's (or any other session's) working
directory before removing it.

## Impact

Session becomes completely unusable. Requires restart.

View original on GitHub ↗

This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗