Skip to content

The agent that grows with you

fix(checkpoints): refuse an empty restore selection and delete read-only test repos

unsupportedpastels99721dca80a5
diff --git a/tests/tools/test_checkpoint_gitlinks.py b/tests/tools/test_checkpoint_gitlinks.py
index 08f7d36c93d..358634c52f5 100644
--- a/tests/tools/test_checkpoint_gitlinks.py
+++ b/tests/tools/test_checkpoint_gitlinks.py
@@ -6,6 +6,7 @@ import subprocess
import pytest
from tools import checkpoint_manager as cm
+from utils import rmtree_readonly
@pytest.fixture
@@ -90,7 +91,9 @@ def test_unusual_filename_does_not_break_tree_inspection(checkpoint, name):
@pytest.mark.parametrize("spec", [None, "*", "tool/main.py"])
def test_deleted_nested_repository_is_not_reported_restored(checkpoint, safe, spec):
mgr, work, commit = checkpoint
- shutil.rmtree(work / "tool")
+ # Git for Windows makes loose objects read-only; plain rmtree fails there.
+ rmtree_readonly(work / "tool")
+ assert not (work / "tool").exists()
before = durable_state(work)
result = mgr.restore(str(work), commit, file_path=spec, safe=safe)
assert result["success"] is False
@@ -99,6 +102,21 @@ def test_deleted_nested_repository_is_not_reported_restored(checkpoint, safe, sp
assert durable_state(work) == before
[email protected]("notes", ["before\n", "after\n"])
+def test_empty_selection_refuses_without_mutation(checkpoint, tmp_path, notes):
+ # Checkout with an empty pathspec file switches HEAD instead of restoring
+ # nothing. "after" is the dirty-file control, which Git itself protects.
+ mgr, work, commit = checkpoint
+ (work / "notes.txt").write_text(notes)
+ (work / "tool").rename(tmp_path / "moved-tool")
+ before = durable_state(work)
+ result = mgr.restore(str(work), commit, file_path=":(exclude)*")
+ assert result["success"] is False, result
+ assert "matched no files" in result["error"]
+ assert not (work / "tool").exists()
+ assert durable_state(work) == before
+
+
@pytest.mark.parametrize("name", ["nested space", "nested-雪", "vendor/tool",
pytest.param("nested\tline\n", marks=pytest.mark.platforms("posix")),
pytest.param(b"nested-\xff", marks=pytest.mark.platforms("linux"))])
diff --git a/tools/checkpoint_manager.py b/tools/checkpoint_manager.py
index a94fa0fd4ce..a2081650696 100644
--- a/tools/checkpoint_manager.py
+++ b/tools/checkpoint_manager.py
@@ -1226,6 +1226,14 @@ class CheckpointManager:
for record in selected.split("\x00")
if "\t" in record
]
+ if not selected_paths:
+ # An exclusion-only spec can match nothing. Checkout
+ # with no paths would switch the store's HEAD instead
+ # of restoring files, so refuse before the snapshot.
+ return {
+ "success": False,
+ "error": f"Restore selection matched no files in checkpoint: {file_path}",
+ }
if blocked_repos:
paths = ", ".join(blocked_repos)