claude-code - 💡(How to fix) Fix Sandbox mode: files created by background agents via Bash don't persist to real filesystem [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#45383Fetched 2026-04-09 08:06:42
View on GitHub
Comments
0
Participants
1
Timeline
6
Reactions
0
Participants
Timeline (top)
labeled ×6

When running Claude Code in /sandbox mode, files created by background agents (spawned via the Agent tool) using Bash commands (cp, cat >, echo >, etc.) are written to a sandbox overlay and are not visible on the user's real filesystem. The files appear to exist from the agent's Bash perspective (ls, cat, stat all work), but they don't exist on the actual disk.

Root Cause

Instruct agents to use the Write tool instead of Bash for all file creation. The Write tool bypasses the sandbox and writes to the real filesystem. This is fragile because:

  • It's easy to forget
  • Agents may use cp or mv which also don't persist
  • The failure is silent — everything appears to work until the user checks their filesystem

Fix Action

Workaround

Instruct agents to use the Write tool instead of Bash for all file creation. The Write tool bypasses the sandbox and writes to the real filesystem. This is fragile because:

  • It's easy to forget
  • Agents may use cp or mv which also don't persist
  • The failure is silent — everything appears to work until the user checks their filesystem
RAW_BUFFERClick to expand / collapse

Description

When running Claude Code in /sandbox mode, files created by background agents (spawned via the Agent tool) using Bash commands (cp, cat >, echo >, etc.) are written to a sandbox overlay and are not visible on the user's real filesystem. The files appear to exist from the agent's Bash perspective (ls, cat, stat all work), but they don't exist on the actual disk.

Steps to Reproduce

  1. Enable sandbox mode (/sandbox)
  2. Spawn a background agent via the Agent tool
  3. Have the agent create a file using Bash (e.g., echo "content" > /path/to/file.md)
  4. Agent reports success, file is visible via ls from agent's Bash
  5. Check from the user's terminal — file does not exist

Expected Behavior

Files created by agents in sandbox mode should persist to the real filesystem, same as files created by the main conversation's Bash commands.

Actual Behavior

Files are trapped in a per-agent sandbox overlay that is destroyed when the agent terminates. The content is permanently lost.

Workaround

Instruct agents to use the Write tool instead of Bash for all file creation. The Write tool bypasses the sandbox and writes to the real filesystem. This is fragile because:

  • It's easy to forget
  • Agents may use cp or mv which also don't persist
  • The failure is silent — everything appears to work until the user checks their filesystem

Impact

In a session involving many agents producing output files (e.g., documentation generation), all agent-written files can be silently lost, requiring complete re-generation. In our case, ~210KB of architecture documentation across 7 files was lost and had to be re-written.

Environment

  • Claude Code CLI with /sandbox enabled
  • Linux (WSL2)
  • Agents spawned via Agent tool with run_in_background: true

🤖 Generated with Claude Code

extent analysis

TL;DR

Instruct agents to use the Write tool instead of Bash commands for file creation to ensure files persist to the real filesystem in sandbox mode.

Guidance

  • Identify all instances where agents use Bash commands for file creation and replace them with the Write tool to prevent silent file loss.
  • Review agent scripts to ensure they do not use cp or mv commands, which also do not persist files in sandbox mode.
  • Consider implementing a check or validation step in the agent workflow to verify that files have been successfully written to the real filesystem.
  • Develop a strategy to monitor and detect silent file loss, such as periodic checks for missing files or unexpected agent behavior.

Example

No explicit code example is provided, but the issue implies that replacing Bash commands like echo "content" > /path/to/file.md with the equivalent Write tool command will resolve the issue.

Notes

The provided workaround is fragile and relies on consistent use of the Write tool. A more robust solution may involve modifying the sandbox mode to persist files created by agents or implementing additional checks to prevent silent file loss.

Recommendation

Apply the workaround by instructing agents to use the Write tool for file creation, as it is the only provided solution that ensures files persist to the real filesystem in sandbox mode. This approach, although fragile, can mitigate the issue until a more robust solution is available.

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