Background agents lose edits when parent switches git branches
Summary
When a background subagent (launched via Agent tool with run_in_background: true) is editing files using the Edit tool, running git checkout <other-branch> in the parent conversation silently destroys the subagent's work. The agent receives no error — its Edit calls succeed against the now-wrong file content — but the results are never committed because the working tree was replaced.
Steps to Reproduce
- On branch
feature-a, launch a background agent to edit files insrc/ - While the agent is running, switch to branch
feature-bwithgit checkout feature-b - The agent continues making
Editcalls — these may succeed (if the file exists on the new branch) or fail silently - Switch back to
feature-a— the agent's edits are gone - The agent reports success, but no files were actually changed
Expected Behavior
Either:
- The parent should be warned/blocked from switching branches while background agents are active
- Or agents should detect that the working tree changed underneath them
Workaround
Use isolation: "worktree" parameter when launching agents that need to edit files, especially if the parent may switch branches. This gives the agent an isolated git worktree.
Context
Discovered during a large-scale Lean theorem proving benchmark where parallel agents were working on different branches. The parent switched between run/taobench and run/taobenchmathlib branches while agents were editing files on one of them, causing ~13 solved problems to be silently lost.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗