Skip to content

The agent that grows with you

fix(checkpoints): clear-legacy trims to two real-path tests, aligns failure line with the log

teknium1d1bd778a5e3b
diff --git a/hermes_cli/checkpoints.py b/hermes_cli/checkpoints.py
index 3c86daafff0..d0ce78c3df0 100644
--- a/hermes_cli/checkpoints.py
+++ b/hermes_cli/checkpoints.py
@@ -181,7 +181,7 @@ def cmd_clear_legacy(args: argparse.Namespace) -> int:
result = clear_legacy()
print(f"Deleted {result['deleted']} archive(s), reclaimed {_fmt_bytes(result['bytes_freed'])}.")
if result["errors"]:
- print(f"Failed to delete {result['errors']} archive(s). See logs for details.")
+ print(f"Could not delete {result['errors']} archive(s) (see logs).")
return 2
return 0
diff --git a/tests/hermes_cli/test_checkpoints_clear_legacy.py b/tests/hermes_cli/test_checkpoints_clear_legacy.py
new file mode 100644
index 00000000000..2185d88d46f
--- /dev/null
+++ b/tests/hermes_cli/test_checkpoints_clear_legacy.py
@@ -0,0 +1,62 @@
+"""`hermes checkpoints clear-legacy` exit code must reflect whether the archives are gone.
+
+`clear_legacy()` counts failed deletions as ``errors`` and the CLI turns a non-zero count into
+exit 2 plus a "Could not delete" line, so a run that left every archive on disk is no longer
+indistinguishable from a clean sweep to scripts (GitHub issue #111776). Both tests run the real
+manager against a temp checkpoint base; only ``shutil.rmtree`` is faked for the failure case.
+"""
+
+from __future__ import annotations
+
+import argparse
+import shutil
+from pathlib import Path
+
+
+def _base_with_archives(tmp_path: Path, monkeypatch, names) -> Path:
+ import tools.checkpoint_manager as ckpt_mgr
+
+ base = tmp_path / "checkpoints"
+ for name in names:
+ (base / name).mkdir(parents=True)
+ (base / name / "blob").write_bytes(b"x" * 1024)
+ monkeypatch.setattr(ckpt_mgr, "CHECKPOINT_BASE", base)
+ return base
+
+
+def test_undeletable_archive_exits_two_and_is_reported(tmp_path, monkeypatch, capsys):
+ import tools.checkpoint_manager as ckpt_mgr
+ from hermes_cli import checkpoints as checkpoints_cli
+
+ base = _base_with_archives(tmp_path, monkeypatch, ["legacy-20200101-000000", "legacy-20200102-000000"])
+ stuck = base / "legacy-20200101-000000"
+ real_rmtree = shutil.rmtree
+
+ def _rmtree_read_only(path, *args, **kwargs):
+ if Path(path) == stuck:
+ raise OSError(13, "Access is denied", str(path))
+ return real_rmtree(path, *args, **kwargs)
+
+ monkeypatch.setattr(ckpt_mgr.shutil, "rmtree", _rmtree_read_only)
+
+ rc = checkpoints_cli.cmd_clear_legacy(argparse.Namespace(force=True))
+
+ out = capsys.readouterr().out
+ assert rc == 2
+ assert "Deleted 1 archive(s), reclaimed" in out
+ assert "Could not delete 1 archive(s) (see logs)." in out
+ assert stuck.exists()
+
+
+def test_clean_sweep_keeps_exit_zero_and_success_line(tmp_path, monkeypatch, capsys):
+ from hermes_cli import checkpoints as checkpoints_cli
+
+ base = _base_with_archives(tmp_path, monkeypatch, ["legacy-20200101-000000"])
+
+ rc = checkpoints_cli.cmd_clear_legacy(argparse.Namespace(force=True))
+
+ out = capsys.readouterr().out
+ assert rc == 0
+ assert "Deleted 1 archive(s), reclaimed" in out
+ assert "Could not delete" not in out
+ assert not list(base.glob("legacy-*"))
diff --git a/tests/hermes_cli/test_checkpoints_prune.py b/tests/hermes_cli/test_checkpoints_prune.py
index 6c4b61acaf9..909c1746a28 100644
--- a/tests/hermes_cli/test_checkpoints_prune.py
+++ b/tests/hermes_cli/test_checkpoints_prune.py
@@ -26,27 +26,6 @@ def _prune_result(**kwargs) -> dict:
return result
-def test_clear_legacy_returns_error_when_an_archive_cannot_be_deleted(monkeypatch, capsys):
- import hermes_cli.checkpoints as checkpoints_cli
- import tools.checkpoint_manager as ckpt_mgr
-
- monkeypatch.setattr(
- ckpt_mgr,
- "store_status",
- lambda: {"legacy_archives": [{"name": "legacy-read-only", "size_bytes": 1}]},
- )
- monkeypatch.setattr(
- ckpt_mgr,
- "clear_legacy",
- lambda: {"deleted": 0, "errors": 1, "bytes_freed": 0},
- )
-
- rc = checkpoints_cli.cmd_clear_legacy(_ns(force=True))
-
- assert rc == 2
- assert "Failed to delete 1 archive(s)." in capsys.readouterr().out
-
-
_V2_ORPHAN_ONLY_STATUS = {
"projects": [],
"pre_v2_projects": [],
@@ -143,3 +122,4 @@ def test_empty_preview_binds_empty_allowlist(monkeypatch, capsys):
+
diff --git a/tests/tools/test_checkpoint_manager.py b/tests/tools/test_checkpoint_manager.py
index 7756597b485..f36f97e1125 100644
--- a/tests/tools/test_checkpoint_manager.py
+++ b/tests/tools/test_checkpoint_manager.py
@@ -1187,50 +1187,6 @@ class TestClearFunctions:
# Store preserved
assert (base / "store" / "HEAD").exists()
- def test_clear_legacy_reports_partial_deletion_failures(self, tmp_path, monkeypatch):
- base = tmp_path / "checkpoints"
- failed = base / "legacy-failed"
- deleted = base / "legacy-deleted"
- for archive in (failed, deleted):
- archive.mkdir(parents=True)
- (archive / "data").write_bytes(b"x")
-
- real_rmtree = shutil.rmtree
-
- def fail_one(path, *args, **kwargs):
- if Path(path) == failed:
- raise OSError("read-only file")
- return real_rmtree(path, *args, **kwargs)
-
- monkeypatch.setattr("tools.checkpoint_manager.shutil.rmtree", fail_one)
-
- result = clear_legacy(base)
-
- assert result["deleted"] == 1
- assert result["errors"] == 1
- assert not deleted.exists()
- assert failed.exists()
-
- def test_clear_legacy_reports_all_deletion_failures(self, tmp_path, monkeypatch):
- base = tmp_path / "checkpoints"
- archives = [base / "legacy-first", base / "legacy-second"]
- for archive in archives:
- archive.mkdir(parents=True)
-
- def always_fail(*args, **kwargs):
- raise OSError("read-only file")
-
- monkeypatch.setattr(
- "tools.checkpoint_manager.shutil.rmtree",
- always_fail,
- )
-
- result = clear_legacy(base)
-
- assert result["deleted"] == 0
- assert result["errors"] == 2
- assert all(archive.exists() for archive in archives)
-
# =========================================================================
# Orphan pruning must not act on an unreachable volume
diff --git a/website/docs/reference/cli-commands.md b/website/docs/reference/cli-commands.md
index f61f1209587..dd240ae65a5 100644
--- a/website/docs/reference/cli-commands.md
+++ b/website/docs/reference/cli-commands.md
@@ -1040,7 +1040,7 @@ Inspect and manage the shadow git store at `~/.hermes/checkpoints/` — the stor
| `list` | Alias for `status`. |
| `prune` | Force a cleanup sweep — delete orphan and stale projects, GC the store, enforce the size cap. Ignores the 24h idempotency marker. |
| `clear` | Delete the entire checkpoint base. Irreversible; asks for confirmation unless `-f`. |
-| `clear-legacy` | Delete only the `legacy-<timestamp>/` archives produced by the v1→v2 migration. |
+| `clear-legacy` | Delete only the `legacy-<timestamp>/` archives produced by the v1→v2 migration. Exits `2` (after printing `Could not delete N archive(s)`) when any archive could not be removed, e.g. read-only git objects on Windows. |
### Options