claude-code - 💡(How to fix) Fix Worktrees from `isolation: "worktree"` auto-reap when subagent returns without changes, breaking SendMessage resume [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#50889Fetched 2026-04-20 12:10:16
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
0
Author
Participants
Timeline (top)
labeled ×2

Subagents spawned via the Agent tool with isolation: "worktree" have their worktrees auto-cleaned up if the agent returns without making file changes. This breaks the SendMessage resume flow when the subagent's first turn is a clarifying-question checkpoint (reads files, stops to ask the parent agent for decisions before writing).

Root Cause

Subagents spawned via the Agent tool with isolation: "worktree" have their worktrees auto-cleaned up if the agent returns without making file changes. This breaks the SendMessage resume flow when the subagent's first turn is a clarifying-question checkpoint (reads files, stops to ask the parent agent for decisions before writing).

Fix Action

Fix / Workaround

Current workaround (documented in our /elephant command)

Worktree kept alive, but adds a spurious commit to rebase out before PR. Smells like a workaround to future session readers.

Code Example

git commit --allow-empty -m "scaffold: branch placeholder"
RAW_BUFFERClick to expand / collapse

Summary

Subagents spawned via the Agent tool with isolation: "worktree" have their worktrees auto-cleaned up if the agent returns without making file changes. This breaks the SendMessage resume flow when the subagent's first turn is a clarifying-question checkpoint (reads files, stops to ask the parent agent for decisions before writing).

Reproduction

  1. Parent spawns subagent with Agent({ ..., isolation: "worktree", name: "my-agent" }). Subagent is instructed to "ask clarifying questions before writing code."
  2. Subagent runs: pwd + git worktree list confirms isolated worktree. Reads files. Returns a question-list as its result without making any commits or edits.
  3. Harness reaps the worktree per documented behavior: "the worktree is automatically cleaned up if the agent makes no changes."
  4. Parent agent processes the questions and calls SendMessage({to: "my-agent", message: "<answers>"}) to resume.
  5. Subagent resumes but its worktree is now gone. pwd resolves to the primary working directory. Per preflight rules, the subagent stops rather than risking contamination.

Impact

The ask-first subagent pattern — where subagents surface unknowns to the parent before committing to an implementation path — is fundamentally broken under isolation: "worktree". Parents are forced into one of:

  • Over-specify prompts (pre-answering questions they don't know the answer to — defeats the point of delegation)
  • Instruct subagents to make throwaway "scaffold" commits before asking (band-aid; adds noise to PR history)
  • Skip worktree isolation (risks contention with parent agent's working tree)

None are clean. The ask-first checkpoint is a core pattern of the delegation model.

Proposed fix (any of)

  1. Worktrees from Agent calls with name set (i.e. resumable via SendMessage) should not auto-reap on first return. Cleanup deferred to session end.
  2. Add explicit keepAlive: boolean option to the Agent tool. When true, the harness persists the worktree until the parent explicitly indicates no further SendMessage calls.
  3. SendMessage automatically re-provisions the worktree from the subagent's preserved branch state before resuming.

Observed in

  • Anthropic Claude Code CLI, session on 2026-04-19
  • Pattern reproduced twice in one session with two separate subagents. Both were respawned with over-specified prompts to avoid the ask-first path; recovery worked but at the cost of re-reading files and re-accruing context.

Current workaround (documented in our /elephant command)

Kickoff prompts instruct subagents to make an empty scaffold commit before asking clarifying questions:

git commit --allow-empty -m "scaffold: branch placeholder"

Worktree kept alive, but adds a spurious commit to rebase out before PR. Smells like a workaround to future session readers.

extent analysis

TL;DR

To fix the issue with subagents losing their worktree after returning without making file changes, consider adding an option to defer worktree cleanup until the session end or until the parent agent explicitly indicates no further SendMessage calls.

Guidance

  • Identify subagents that require resumability via SendMessage and consider implementing one of the proposed fixes to prevent auto-reap of their worktrees.
  • Verify that the worktree is preserved by checking the pwd and git worktree list commands after the subagent returns without making changes.
  • If using the current workaround, ensure that the scaffold commit is properly removed before creating a pull request to avoid unnecessary commits in the history.
  • Consider the trade-offs between the proposed fixes, such as adding an explicit keepAlive option or automatically re-provisioning the worktree on SendMessage.

Example

# Current workaround: make an empty scaffold commit to keep the worktree alive
git commit --allow-empty -m "scaffold: branch placeholder"

Notes

The proposed fixes aim to address the issue without introducing significant changes to the existing workflow. However, the choice of fix may depend on the specific requirements and constraints of the project.

Recommendation

Apply the workaround by adding an explicit keepAlive option to the Agent tool, allowing parents to control when the worktree is cleaned up. This approach provides flexibility and avoids unnecessary commits or modifications to the existing workflow.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Worktrees from `isolation: "worktree"` auto-reap when subagent returns without changes, breaking SendMessage resume [1 participants]