Skip to content

fix(opencode): keep files on revert when snapshot is missing - #63475

Open
VISWA00723 wants to merge 1 commit into
anomalyco:v1from
VISWA00723:snapshot-revert-pruned
Open

VISWA00723 wants to merge 1 commit into
anomalyco:v1from
VISWA00723:snapshot-revert-pruned

Conversation

@VISWA00723

Copy link
Copy Markdown

Issue for this PR

Closes #54577

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Snapshot trees created by Snapshot.track have no git refs, so Snapshot.cleanup (git gc --prune=7.days) eventually prunes older trees (and moving a worktree creates a fresh snapshot store). When reverting a message whose snapshot tree is missing, git checkout <hash> -- <file> fails and single() in Snapshot.revert falls back to git ls-tree <hash> -- <rel>. Previously it only kept the file when tree.code === 0 && tree.text.trim(), so a missing tree (tree.code !== 0 with empty stdout) fell through to remove(op.file) and deleted every existing file touched in that turn.

This change only deletes a file when ls-tree succeeds (tree.code === 0) and returns empty output (proving the snapshot tree is readable and the file was absent). When tree.code !== 0 || tree.text.trim(), Snapshot.revert keeps the file on disk, logs a warning, and returns the kept paths so SessionRevert.revert can publish a Session.Event.Error listing the files that could not be restored.

How did you verify your code works?

  • Added tests in packages/opencode/test/snapshot/snapshot.test.ts verifying that pruning the snapshot store (git gc --prune=now) before snapshot.revert keeps both modified and newly added files on disk and returns their paths, while readable snapshots still delete newly added files and restore modified ones.
  • Added a test in packages/opencode/test/session/revert-compact.test.ts verifying that SessionRevert.revert after snapshot pruning preserves file contents on disk, updates session.revert, and publishes the Session.Event.Error naming the kept file.
  • Ran bun test --timeout 30000 test/session/revert-compact.test.ts test/snapshot/snapshot.test.ts in packages/opencode (64 passed, 0 failed), bun oxlint, and bun turbo typecheck.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

This branch has not been deployed

No deployments
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