Subagent commits silently delete unrelated files via broad git staging
Summary
Claude Code subagents (spawned via the Agent tool with isolation: "worktree") delete unrelated files when committing their work. The agent's commits include deletions of files the agent never touched, suggesting broad git staging (git add -A or git add .) despite the system prompt instructing "prefer adding specific files by name."
This has happened 3 times in our project over 2 days, deleting production source code — not just documentation.
Update: Initial report stated commits were "direct on master." Forensic reflog analysis revealed they were worktree branch commits fast-forward merged onto master. This potentially links to #44965. See comments for full correction.
Reproduction
Environment
- Claude Code CLI v2.1.92
- Claude Opus 4.6 (1M context)
- Linux (Proxmox LXC)
- Git repository with
.planning/phases/directories containing per-phase artifacts - Subagents spawned with
isolation: "worktree"(via GSD framework default)
Steps
- Have a repository with multiple "phase" directories containing planning docs and source code
- Use Claude Code to spawn an executor subagent (via
Agenttool withisolation: "worktree") to work on Phase N+2 - The subagent makes commits for its assigned work (e.g., adding test files)
- Observe: The subagent's commits also delete files from Phase N and Phase N+1 directories, plus production source code created by those phases
What happens
The subagent's commits include:
- The intended changes (test files, implementation)
- Unintended deletions of 20-30+ files from other phases
- Unintended modifications to shared files (ROADMAP.md, REQUIREMENTS.md, STATE.md) reverting them to older state
Expected behavior
The subagent should only stage and commit files it explicitly created or modified.
Evidence
Incident 1: Phase N+1 executor deletes Phase N files
- Intended: Add 3 test files
- Actually did: Deleted 22 files including:
- A 406-line production module
- An 883-line test suite
- Several utility scripts
- 8 Phase N planning files, 6 Phase N+1 planning files
- Modified a 7000+ line orchestrator file removing ~149 lines of Phase N wiring
- Verified: Parent commit confirmed to have all deleted files via
git ls-tree - Delivery: Worktree branch fast-forward merged onto master (initially misreported as direct commit)
Incident 2: Phase N+2 executor deletes Phase N and N+1 files (again)
- Intended: Add 3 test files for Phase N+2
- Actually did: Deleted 34 files including all Phase N, N+1, and N+2 planning files
- Same pattern: worktree branch, broad staging
Incident 3: Partial restoration also deleted
- A restore commit after Incident 2 fixed some files but left production source missing until manual forensic restoration
Forensic verification: No external automation
We thoroughly verified no system-level automation is responsible:
- No active git hooks (only
.samplefiles in.git/hooks/) - Claude Code hooks (
.claude/hooks/) are advisory-only (read-only, never modify git state) - No pre-commit framework
- Empty crontab
- No Syncthing, rsync, or file sync tools
- No IDE auto-staging or auto-commit configuration
- No background git processes
Impact
- Production source code deleted (not just documentation) — 406-line module, 883-line test suite, query scripts
- Orchestrator wiring reverted — phase integration code removed from main 7000+ line file
- Config reverted — YAML config sections removed
- 3 incidents in 2 days — recurring and escalating (each incident deletes more files)
- Manual forensic restoration required — reverse-patching via
git diff <commit>..<commit>^ | git apply - Silent data loss — no warning or error when the agent commits the deletions. Only discovered during milestone audit.
Root Cause Hypothesis
Updated: Two possible root causes, potentially compounding:
- Worktree branch point (#44965):
EnterWorktreemay create the worktree frommain(or another ref) instead of current HEAD. Files committed after that branch point are absent from the worktree. When the agent stages withgit add -A, these absent files become explicit deletions.
- Broad staging (original hypothesis): Even with a correct branch point, the subagent uses broad git staging (
git add -A,git add .) rather than staging only files it created/modified via Edit/Write tools. Any file absent from the working tree — for any reason — gets staged as a deletion.
The system prompt correctly instructs: "prefer adding specific files by name rather than using git add -A or git add ., which can accidentally include sensitive files or large binaries" — but the agent does not reliably follow this instruction.
Suggested Fix
- Hard enforcement: Claude Code's commit tooling should refuse or warn when a commit includes file deletions that weren't explicitly requested
- Staging guard: Before committing, check
git diff --cached --diff-filter=Dand require explicit confirmation for any deletions - Subagent scope restriction: Subagents should only be able to stage files within their assigned scope (e.g., files they created or modified via Edit/Write tools)
- Worktree fix (#44965): Ensure
EnterWorktreebranches from current HEAD, notmain
Workaround
Manually verifying commits via git show --stat after every subagent execution and maintaining restore scripts. This is fragile and labor-intensive.
This issue has 6 comments on GitHub. Read the full discussion on GitHub ↗