claude-code - 💡(How to fix) Fix Agent tool with isolation:"worktree" silently discards all file changes on cleanup [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#46470Fetched 2026-04-11 06:19:27
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
0
Participants
Timeline (top)
labeled ×2

When the Agent tool is invoked with isolation: "worktree", it runs in a temporary git worktree. On cleanup, if the agent has not pushed or committed to a remote branch, all file changes are automatically discarded. This is not prominently documented and causes a silent data loss pattern.

Root Cause

When the Agent tool is invoked with isolation: "worktree", it runs in a temporary git worktree. On cleanup, if the agent has not pushed or committed to a remote branch, all file changes are automatically discarded. This is not prominently documented and causes a silent data loss pattern.

Fix Action

Workaround

Do not use isolation: "worktree" for implementation tasks where file persistence is required. Use it only for read-only research or exploration agents.

RAW_BUFFERClick to expand / collapse

Summary

When the Agent tool is invoked with isolation: "worktree", it runs in a temporary git worktree. On cleanup, if the agent has not pushed or committed to a remote branch, all file changes are automatically discarded. This is not prominently documented and causes a silent data loss pattern.

Steps to reproduce

  1. Invoke the Agent tool with isolation: "worktree" and ask the agent to edit or write files.
  2. The agent runs, reports completion ("All files updated"), and the tool returns.
  3. Inspect the original working tree — no changes are present.

Expected behavior (from docs)

The Agent tool documentation mentions that worktrees are "automatically cleaned up if the agent makes no changes" — but the behavior when the agent DOES make changes is not clearly explained. Users expect that file edits persist.

Actual behavior

File edits made inside the worktree do NOT persist to the original working tree unless:

  • The agent pushes to a remote branch, OR
  • The agent commits to the worktree branch which is then explicitly merged

Agents that only write files (without committing/pushing) lose all changes on worktree cleanup.

Proposed fix

  1. Add a prominent warning in the Agent tool documentation for isolation: "worktree":

    Warning: File changes in a worktree agent do not automatically propagate to your working tree. Only committed+pushed changes survive cleanup.

  2. Consider returning the list of changed files (or a diff) when the worktree is cleaned up, so the orchestrating agent can detect the data loss.

Workaround

Do not use isolation: "worktree" for implementation tasks where file persistence is required. Use it only for read-only research or exploration agents.

Environment

Claude Code (CLI), Agent tool with isolation: "worktree".

extent analysis

TL;DR

To prevent silent data loss when using the Agent tool with isolation: "worktree", ensure that the agent either commits and pushes changes to a remote branch or consider an alternative isolation method for tasks requiring file persistence.

Guidance

  • When using isolation: "worktree", verify that the agent commits and pushes changes to a remote branch to persist file edits.
  • Review the Agent tool documentation for updated warnings regarding worktree behavior and file persistence.
  • For tasks that require file persistence, consider using an alternative isolation method instead of isolation: "worktree".
  • If file persistence is crucial, ensure that the agent's workflow includes committing and pushing changes to prevent data loss during worktree cleanup.

Example

No specific code snippet is provided as the issue focuses on the usage and behavior of the Agent tool with isolation: "worktree" rather than specific code implementation.

Notes

The proposed fix and workaround suggest that the primary issue is documentation and user understanding of the isolation: "worktree" behavior. Ensuring that users are aware of the need to commit and push changes for persistence will mitigate the silent data loss pattern.

Recommendation

Apply the workaround by using isolation: "worktree" only for read-only tasks or exploration agents where file persistence is not required, and opt for alternative isolation methods for tasks that need to persist file changes. This approach helps prevent data loss until the documentation and potentially the tool's behavior are updated to better handle file persistence in worktrees.

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