The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): ZIP recovery restores a dangling-symlink backup and retires its journal only once settled
Teknium89bcec268989
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.pyindex 639f4ad6d78..14b5184d742 100644--- a/hermes_cli/_early_recovery.py+++ b/hermes_cli/_early_recovery.py@@ -809,6 +809,9 @@ def restore_interrupted_zip_swap(project_root: Path | None = None) -> bool:phase, entries = parsedchanged = Falsefailed = False+ # Presence is the ENTRY's (lexists/lstat), never its target's: a dangling symlink backup is the+ # only copy of a tracked symlink, not "no backup" (review Z3).+ here = os.path.lexistsfor name, existed in reversed(entries):dst = root / namestaging, old = Path(f"{dst}{_ZIP_STAGING_SUFFIX}"), Path(f"{dst}{_ZIP_OLD_SUFFIX}")@@ -816,18 +819,18 @@ def restore_interrupted_zip_swap(project_root: Path | None = None) -> bool:if phase == "swapping":# Staging finished before this phase, so a backup now is the one the swap made# from the old entry: it existed, whatever an older journal recorded.- existed = existed or old.exists()- if existed and old.exists():+ existed = existed or here(old)+ if existed and here(old):if old.is_file() and not old.is_symlink() and not dst.is_dir():os.replace(old, dst) # a file entry never goes missing, not even hereelse:_drop_path(dst)- os.rename(old, dst)+ os.rename(old, dst) # moves a symlink itself, never its targetchanged = True- elif not existed and dst.exists() and not staging.exists():+ elif not existed and here(dst) and not here(staging):_drop_path(dst)changed = True- elif existed and not dst.exists() and old.exists():+ elif existed and not here(dst) and here(old):os.rename(old, dst) # the backup is the only copy leftchanged = True# ``<old>.tmp``: a backup copy killed before its rename (no-hardlink file systems).@@ -836,7 +839,14 @@ def restore_interrupted_zip_swap(project_root: Path | None = None) -> bool:except OSError as exc:failed = Trueprint(f"⚠ Could not settle {name} after an interrupted ZIP update: {exc}", file=sys.stderr)- if failed:+ # Retire the journal only on a verified terminal state, not an exception-free loop: no sibling+ # (staging copy, backup, backup temp) left that only this journal could still explain.+ siblings = (_ZIP_STAGING_SUFFIX, _ZIP_OLD_SUFFIX, _ZIP_OLD_SUFFIX + ".tmp")+ unsettled = [name for name, _existed in entries if any(here(f"{root / name}{s}") for s in siblings)]+ if failed or unsettled:+ if unsettled and not failed:+ print(f"⚠ The interrupted ZIP update left {', '.join(unsettled)} unsettled; its journal "+ f"{journal} stays and the next launch retries.", file=sys.stderr)return changed # the journal stays: the next launch retriesjournal.unlink(missing_ok=True)if changed:diff --git a/hermes_cli/update_cmd_zip.py b/hermes_cli/update_cmd_zip.pyindex 73230580703..f0a346552cc 100644--- a/hermes_cli/update_cmd_zip.py+++ b/hermes_cli/update_cmd_zip.py@@ -82,7 +82,7 @@ def _stage_replacement(src: str, dst: str) -> str:backup = f"{dst}.hermes-update-old"# A prior run may have died mid-swap leaving the backup as the ONLY copy. Restore it BEFORE# clearing leftovers, else deleting it then failing to stage (disk exhaustion) leaves a hole.- if not os.path.exists(dst) and os.path.exists(backup):+ if not os.path.lexists(dst) and os.path.lexists(backup): # a dangling symlink backup is still one (Z3)os.rename(backup, dst)# Fail closed: a leftover backup that survives would later be taken for this swap's own backup# (rollback and journal recovery put it back as the live entry).diff --git a/tests/hermes_cli/test_early_recovery.py b/tests/hermes_cli/test_early_recovery.pyindex 6a62e43d9be..15bc737281b 100644--- a/tests/hermes_cli/test_early_recovery.py+++ b/tests/hermes_cli/test_early_recovery.py@@ -225,6 +225,39 @@ def test_zip_journal_publication_never_deletes_a_file_at_its_old_fixed_temp_nameassert sorted(p.name for p in root.iterdir()) == sorted([er.ZIP_SWAP_JOURNAL, user.name])+def _symlink_or_skip(link: Path, target: str) -> None:+ try:+ link.symlink_to(target)+ except OSError:+ pytest.skip("symlinks need privileges here")++[email protected]("kind", ["dangling-symlink", "live-symlink", "regular"])+def test_an_interrupted_swap_restores_its_backup_by_entry_not_by_target(tmp_path, kind):+ """A swap killed after it moved a tracked symlink aside and installed a regular file: recovery took+ a DANGLING backup for "no backup" (exists() follows it), then deleted it and retired the journal+ (review Z3). The entry itself comes back, type and target intact; regular/live controls too."""+ root = tmp_path / "install"+ root.mkdir()+ (root / "target.txt").write_text("T", encoding="utf-8")+ old = root / "alpha.hermes-update-old"+ if kind == "regular":+ old.write_text("OLD", encoding="utf-8")+ else:+ _symlink_or_skip(old, "gone.txt" if kind == "dangling-symlink" else "target.txt")+ (root / "alpha").write_text("NEW", encoding="utf-8")+ er.write_zip_swap_journal(root, "swapping", [["alpha", True]])++ assert er.restore_interrupted_zip_swap(root) is True+ alpha = root / "alpha"+ if kind == "regular":+ assert not alpha.is_symlink() and alpha.read_text(encoding="utf-8") == "OLD"+ else:+ assert alpha.is_symlink() and os.readlink(alpha) == ("gone.txt" if kind == "dangling-symlink" else "target.txt")+ assert not os.path.lexists(old) and not (root / er.ZIP_SWAP_JOURNAL).exists()+ assert (root / "target.txt").read_text(encoding="utf-8") == "T"++@pytest.mark.parametrize("body", ["", "{not json", '{"phase": "swap", "entries": [["a.py", true]]}','{"phase": "swapping", "entries": [["../a.py", true]]}', "read-error"])def test_an_unparsed_zip_journal_keeps_itself_and_every_backup(tmp_path, monkeypatch, body):diff --git a/tests/hermes_cli/test_update_zip_two_phase.py b/tests/hermes_cli/test_update_zip_two_phase.pyindex b6085960013..f1f93b787ae 100644--- a/tests/hermes_cli/test_update_zip_two_phase.py+++ b/tests/hermes_cli/test_update_zip_two_phase.py@@ -287,6 +287,23 @@ def test_staging_restores_backup_when_dst_is_missing(tmp_path, monkeypatch):assert (live / "agent" / "version.txt").read_text() == "new"assert not [p for p in os.listdir(live) if "hermes-update" in p]+def test_staging_restores_a_dangling_symlink_backup_instead_of_deleting_it(tmp_path):+ """The leftover-backup restore tested ``exists()``: a dangling symlink backup (the only copy of a+ tracked symlink entry) read as absent and the leftover sweep deleted it (review Z3)."""+ live, new = tmp_path / "live", tmp_path / "new"+ live.mkdir()+ new.mkdir()+ (new / "alpha").write_text("NEW")+ backup = live / "alpha.hermes-update-old"+ try:+ backup.symlink_to("gone.txt")+ except OSError:+ pytest.skip("symlinks need privileges here")+ update_cmd._stage_replacement(str(new / "alpha"), str(live / "alpha"))+ assert (live / "alpha").is_symlink() and os.readlink(live / "alpha") == "gone.txt"+ assert not os.path.lexists(backup)++def test_commit_failure_plus_discard_leaves_no_staging_litter(tmp_path, monkeypatch):"""Phase-2 failure must not orphan staging copies for unswapped entries.