Skip to content

The agent that grows with you

fix: a committed ZIP swap keeps its journal while a backup it could not remove remains

Teknium588de49cbad4
diff --git a/hermes_cli/update_cmd_zip.py b/hermes_cli/update_cmd_zip.py
index fe4b5e9c05f..1fcc24cce99 100644
--- a/hermes_cli/update_cmd_zip.py
+++ b/hermes_cli/update_cmd_zip.py
@@ -447,16 +447,12 @@ def _stage_entries(extracted: str, entries: list[str], project_root: str,
return staged
-def _staging_left(root: Path, entries: list[str]) -> list[str]:
- """The declared staging paths still on disk (a cleanup that could not finish)."""
- return [item for item in entries if os.path.lexists(os.path.join(root, item + ".hermes-update-staging"))]
-
-
-def _drop_journal_if_clean(root: Path, entries: list[str]) -> None:
- """The journal is the only record of the staging paths: it goes only once every one of them is gone."""
- left = _staging_left(root, entries)
+def _drop_journal_if_clean(root: Path, entries: list[str], suffixes=(".hermes-update-staging",)) -> None:
+ """The journal is the only record of the swap's siblings (``<entry><suffix>``): it goes only once
+ every one of them is gone, else the next launch's recovery could not tell them from a user's file."""
+ left = [item for item in entries if any(os.path.lexists(os.path.join(root, item + x)) for x in suffixes)]
if left:
- print(f" ⚠ Could not remove the staged copies of {', '.join(left)}; the next `hermes` launch "
+ print(f" ⚠ Could not remove the update's own copies of {', '.join(left)}; the next `hermes` launch "
"removes them.")
return
(root / ZIP_SWAP_JOURNAL).unlink(missing_ok=True)
@@ -521,9 +517,12 @@ def _journaled_stage_and_swap(extracted: str, entries: list[str], root: Path, ta
_commit_staged_replacements(
staged, on_committed=lambda: write_zip_swap_journal(root, "committed", journal_entries, gen), tag=gen)
# Committed: the new tree is whole. A journal that cannot go now (AV/indexer holding it) says
- # "committed", which the next launch's recovery settles by keeping the new tree.
+ # "committed", which the next launch's recovery settles by keeping the new tree. It stays, too,
+ # while a backup (or backup temp) the best-effort cleanup could not remove is still on disk:
+ # recovery's own terminal check, else that backup is orphaned and wedges the retry (Q1/F79).
with suppress(OSError):
- (root / ZIP_SWAP_JOURNAL).unlink(missing_ok=True)
+ _drop_journal_if_clean(root, entries, (".hermes-update-staging", ".hermes-update-old",
+ f".hermes-update-old.{gen}.tmp"))
return staged
diff --git a/tests/hermes_cli/test_update_zip_two_phase.py b/tests/hermes_cli/test_update_zip_two_phase.py
index f96b7b15b0e..cfc169aa40a 100644
--- a/tests/hermes_cli/test_update_zip_two_phase.py
+++ b/tests/hermes_cli/test_update_zip_two_phase.py
@@ -780,3 +780,49 @@ def test_the_zip_gate_never_admits_a_conflict_marker_even_for_a_newer_python(tmp
assert not armed # refused before the commit point
assert (live / "hermes_cli" / "main.py").read_text(encoding="utf-8") == "OLD = 1\n"
+
+
+def _git_checkout(root: Path, files: dict[str, str]) -> None:
+ import subprocess
+
+ for name, text in files.items():
+ (root / name).parent.mkdir(parents=True, exist_ok=True)
+ (root / name).write_text(text, encoding="utf-8")
+ for args in (["init", "-q"], ["add", "."], ["-c", "user.name=t", "-c", "[email protected]",
+ "commit", "-qm", "pre"]):
+ subprocess.run(["git", *args], cwd=root, check=True, capture_output=True)
+
+
+@_POSIX_MODES
+def test_a_committed_swap_keeps_its_journal_until_the_backup_is_gone(tmp_path, monkeypatch):
+ """Q1/F79: the swap committed but its backup could not be removed (a read-only directory in the old
+ tree here; an AV scan holding it on Windows). The journal is that backup's only ownership record: it
+ stays ``committed``, the next launch's recovery drops the backup and then the journal, keeping the
+ new tree, and the retry's dirty-tree gate is clean again."""
+ import json
+
+ from hermes_cli import update_cmd_commit
+ from hermes_cli._early_recovery import ZIP_SWAP_JOURNAL, restore_interrupted_zip_swap
+
+ monkeypatch.setattr(update_cmd_commit, "arm_commit_obligations", lambda *a, **k: None)
+ live, extracted = tmp_path / "live", tmp_path / "extracted"
+ for side in (live, extracted): # identical payloads: only the swap's own leftovers can dirty the tree
+ side.mkdir()
+ _git_checkout(live, {"payload/ro/f.txt": "same"})
+ (extracted / "payload" / "ro").mkdir(parents=True)
+ (extracted / "payload" / "ro" / "f.txt").write_text("same", encoding="utf-8")
+ (live / "payload" / "ro").chmod(0o555) # git does not track it; the backup's rmtree cannot empty it
+ try:
+ assert update_cmd_zip._journaled_stage_and_swap(str(extracted), ["payload"], live, "b" * 40)
+ assert (live / "payload.hermes-update-old").exists(), "harness: the backup cleanup was expected to fail"
+ journal = live / ZIP_SWAP_JOURNAL
+ assert journal.is_file(), "the journal went while the swap's backup was still on disk"
+ assert json.loads(journal.read_text(encoding="utf-8"))["phase"] == "committed"
+ assert restore_interrupted_zip_swap(live) is False # the new tree stays: nothing to relaunch
+ finally:
+ for path in live.rglob("*"):
+ if path.is_dir() and not path.is_symlink():
+ path.chmod(0o755)
+ assert not (live / "payload.hermes-update-old").exists() and not (live / ZIP_SWAP_JOURNAL).exists()
+ assert update_cmd_zip._zip_overlay_block_reason(live) is None
+