Skip to content

fix(sandbox): stop discard from deleting renamed files with no recovery - #5124

Merged
pedrofrxncx merged 1 commit into
mainfrom
fix/sandbox-discard-rename-data-loss-w2
Jul 23, 2026
Merged

pedrofrxncx merged 1 commit into
mainfrom
fix/sandbox-discard-rename-data-loss-w2

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Jul 23, 2026 •

Copy link
Copy Markdown
Collaborator

Found by reading packages/sandbox/daemon/routes/git.ts for race/edge-case bugs in the sandbox daemon git route.

The bug: discard() in git.ts decides whether a requested file is "new" (delete it) or "modified" (restore it from HEAD/index) by checking readRefFile(repoDir, "HEAD", fp) === null. For a renamed file, fp is the new path — which never existed at HEAD under that name — so the check always resolves to "new" and the code just unlinkSyncs the working-tree file. Since the original path is already gone from disk (it was renamed away), this deletes the file's content completely, with no way to recover it, and leaves git status in a broken RD (renamed+deleted) state.

Repro (before the fix):

git mv old.txt new.txt
# call POST /git/discard { filepaths: ["new.txt"] }
# both old.txt and new.txt are now gone; git status shows "RD old.txt -> new.txt"

The fix: when the requested path corresponds to a rename (has an origPath in the parsed status), discard now unstages the rename and restores the original file from HEAD at its original path, then deletes the new path — actually undoing the rename instead of just deleting the renamed-to file.

Added a regression test (discard on a renamed file restores the original instead of deleting both) that reproduces the exact repro above and asserts the original file survives with its content intact and git status is clean afterward.

To verify: bun test packages/sandbox/daemon/routes/git.test.ts (31 pass). Also ran bunx tsc --noEmit in packages/sandbox (clean) and bun run fmt. Full CI validates the rest.


Summary by cubic

Fixes sandbox git discard to undo renames instead of deleting the renamed file and losing content. Discarding a renamed path now restores the original from HEAD, unstages the rename, deletes the new path, and leaves git status clean.

  • Bug Fixes
    • Updated discard() in packages/sandbox/daemon/routes/git.ts to detect renames via status, reset the new path, checkout the original from HEAD, then remove the new path.
    • Added a regression test in packages/sandbox/daemon/routes/git.test.ts to ensure the original file is restored and the working tree is clean.

Written for commit 02f45cb. Summary will update on new commits.

Review in cubic

Discarding a renamed file (git/discard) checked the new path against HEAD,
found no entry there (renames land at a new path HEAD never had), and
treated it as untracked — unlinking it outright. Since the original path
was already gone from the working tree post-rename, this destroyed the
file's content entirely with git status left showing a broken RD state.
@pedrofrxncx
pedrofrxncx merged commit 9367cc6 into main Jul 23, 2026
15 checks passed
@pedrofrxncx
pedrofrxncx deleted the fix/sandbox-discard-rename-data-loss-w2 branch July 23, 2026 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant