Background agents lose edits when parent switches git branches

Resolved 💬 3 comments Opened Mar 19, 2026 by cameronfreer Closed Apr 30, 2026

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

  1. On branch feature-a, launch a background agent to edit files in src/
  2. While the agent is running, switch to branch feature-b with git checkout feature-b
  3. The agent continues making Edit calls — these may succeed (if the file exists on the new branch) or fail silently
  4. Switch back to feature-a — the agent's edits are gone
  5. 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.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗