[bug] loop-sandbox drops changes committed by the sandboxed agent
Describe the bug
loop-sandbox loses edits committed by the command running inside its temporary worktree. Patch extraction uses git diff --cached against the sandbox's current HEAD, after the command has already advanced that HEAD. Cleanup then removes the worktree and its branch even when these committed edits were not captured.
With only committed edits, the sandbox reports "No changes were made" and returns no patch. With a commit followed by pending edits, the patch contains only the post-commit difference and may not apply to the original repository.
Steps to reproduce
In a synthetic Git repository with an initial committed README.md, run a sandbox command that writes that file, creates a binary file, stages both, and commits them. A Node command can use fs.writeFileSync and execFileSync('git', ['add', ...]) / execFileSync('git', ['commit', '-m', ...]) for this without any external service.
Repeat with additional unstaged edits to README.md, a staged new file and an untracked file after the commit. Inspect the returned patch and run git apply --check against the original repository.
I reproduced both cases with real temporary Git repositories on the current sandbox source. The two regression tests fail without the fix: the committed-only case reports no changes; the mixed case fails git apply --check on README.md.
Expected behavior
Capture the complete net file changes relative to the worktree's initial commit, regardless of whether the command committed some of them. The original branch must remain unchanged, and the captured binary/text patch must apply to that original baseline. Commit history itself need not be preserved.
Actual behavior
- Committed-only case:
hasChanges === false, no patch, then worktree/branch cleanup. - Mixed case: patch is relative to the command's new HEAD rather than the original baseline and fails to apply to the original checkout.
Proposed minimal fix: pin the created worktree's HEAD before spawning the command and use that commit explicitly for both git diff --cached --stat and git diff --cached --binary. This also respects --base because the baseline is read inside the created worktree. No new dependency or command option is needed.
This is separate from the in-flight child/cleanup race addressed by #713. Investigation and the proposed fix are AI-assisted and tested offline.
Environment
Windows, Node v22.16.0, @cobusgreyling/loop-sandbox 1.0.0, local base 68ccc887ccf1e0cf3a1a1d787ce2da48f4959081. Current main f1fa3e7794af299a78efdd7ead36422c4e9bdbaf is nine commits ahead with no changes under tools/loop-sandbox or tools/loop-worktree.