[BUG] Background shell task completes after agent moves on, silently overwrites files with stale content
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
When a background shell task (spawned via the Bash tool with run_in_background: true) takes longer than the tool timeout and is moved to the background, the agent loses track of it and continues making further edits to the same files. When the background task eventually completes, it writes its output to the file system — silently overwriting the changes the agent made in the interim.
This creates non-deterministic file state that is invisible to both the agent and the user at the moment it occurs.
Impact
This is a correctness and safety issue, not just a UX inconvenience. In a software development context, it can cause:
- Previously passing tests to silently regress
- Completed work to be undone without any indication
What Should Happen?
One of the following safety guarantees should hold:
- A background task that is abandoned/superseded by the agent should be automatically cancelled when the agent moves on to edit the same resource
- OR the task completion should require agent acknowledgement before any file system writes are applied
- OR the agent should be blocked from editing a file that a background task still holds a pending write on
Error Messages/Logs
Steps to Reproduce
Steps to reproduce
- Agent spawns a background shell task that modifies a file (e.g. a Python script that rewrites a source file via regex)
- The task exceeds the tool execution timeout and is moved to background with a task ID
- Agent, believing the task failed or was superseded, proceeds to edit the same file using other tools (Edit, Write, or another Bash command)
- The original background task completes and writes its result to disk, overwriting the agent's subsequent edits
- Neither the agent nor the user is aware this happened until the file is inspected manually
Claude Model
_No response_
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.1.215
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
VS Code integrated terminal
Additional Information
_No response_
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗