The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): a failed ZIP swap keeps a file the user created at an entry it never installed (review F32)
Teknium0b2f56dfdc5d
diff --git a/hermes_cli/update_cmd_zip.py b/hermes_cli/update_cmd_zip.pyindex 04a3df67922..778ff8ea289 100644--- a/hermes_cli/update_cmd_zip.py+++ b/hermes_cli/update_cmd_zip.py@@ -404,37 +404,34 @@ def _journaled_stage_and_swap(extracted: str, entries: list[str], root: Path, taexcept BaseException:_drop_journal_if_clean(root, entries) # _stage_entries dropped its copies, if it couldraise- try:- # TOCTOU re-check right before the swap: download + extract + staging can take minutes and- # work created meanwhile would be destroyed. Our own staging siblings are filtered out.- recheck_reason = _zip_overlay_block_reason(root, ignore_staging_artifacts=True, shipped=entries)- if recheck_reason is not None:- _discard_staged(staged)- _drop_journal_if_clean(root, entries)- print(f"✗ ZIP fallback aborted before the swap: {recheck_reason}.")- print(" Files appeared in the checkout while the update was downloading; committing the swap would delete them.")- print(_STASH_HINT)- _m().sys.exit(1)- # Pre-commit gate: a target whose startup modules do not compile is refused untouched- # (unless it requires a Python this interpreter is not: its syntax is not ours to judge).- pyproject = Path(extracted, "pyproject.toml")- newer_python = pyproject.is_file() and _commit.requires_other_python(pyproject.read_bytes())- for rel in () if newer_python else _UPDATE_CRITICAL_FILES:- path = os.path.join(extracted, *rel.split("/"))- if os.path.isfile(path):- with open(path, "rb") as handle:- compile(handle.read(), rel, "exec", dont_inherit=True)- # The commit point: tail + fleet restart owed before the first live rename.- _commit.arm_commit_obligations(root, target_sha or "")- write_zip_swap_journal(root, "swapping", journal_entries)- _commit_staged_replacements(- staged, on_committed=lambda: write_zip_swap_journal(root, "committed", journal_entries))- except Exception:- # Rollback restored swapped entries but staging copies for the rest remain; drop them or the- # retry's up-front free-space check (runs BEFORE per-entry leftover cleanup) fails on our litter.- # Safe post-rollback: _discard_staged skips paths that no longer exist.+ # A failure past here leaves the journal: the caller's restore_interrupted_zip_swap (or the next+ # launch) settles it and drops every staging copy, so the retry's free-space check finds no litter.+ # Never discard staging first: a staging copy still on disk is what proves its entry was never+ # installed, so recovery must not delete whatever (a user's new file) now sits at that path.+ # TOCTOU re-check right before the swap: download + extract + staging can take minutes and+ # work created meanwhile would be destroyed. Our own staging siblings are filtered out.+ recheck_reason = _zip_overlay_block_reason(root, ignore_staging_artifacts=True, shipped=entries)+ if recheck_reason is not None:_discard_staged(staged)- raise+ _drop_journal_if_clean(root, entries)+ print(f"✗ ZIP fallback aborted before the swap: {recheck_reason}.")+ print(" Files appeared in the checkout while the update was downloading; committing the swap would delete them.")+ print(_STASH_HINT)+ _m().sys.exit(1)+ # Pre-commit gate: a target whose startup modules do not compile is refused untouched+ # (unless it requires a Python this interpreter is not: its syntax is not ours to judge).+ pyproject = Path(extracted, "pyproject.toml")+ newer_python = pyproject.is_file() and _commit.requires_other_python(pyproject.read_bytes())+ for rel in () if newer_python else _UPDATE_CRITICAL_FILES:+ path = os.path.join(extracted, *rel.split("/"))+ if os.path.isfile(path):+ with open(path, "rb") as handle:+ compile(handle.read(), rel, "exec", dont_inherit=True)+ # The commit point: tail + fleet restart owed before the first live rename.+ _commit.arm_commit_obligations(root, target_sha or "")+ write_zip_swap_journal(root, "swapping", journal_entries)+ _commit_staged_replacements(+ staged, on_committed=lambda: write_zip_swap_journal(root, "committed", journal_entries))(root / ZIP_SWAP_JOURNAL).unlink(missing_ok=True)return stageddiff --git a/tests/hermes_cli/test_update_zip_two_phase.py b/tests/hermes_cli/test_update_zip_two_phase.pyindex e05d04208d0..8aa484b5a48 100644--- a/tests/hermes_cli/test_update_zip_two_phase.py+++ b/tests/hermes_cli/test_update_zip_two_phase.py@@ -539,3 +539,31 @@ def test_a_stale_backup_never_stands_in_for_the_live_entry(tmp_path, monkeypatchmodule = live / "pkg" / "m.py"assert module.is_file() and module.read_text() in ("LIVE_OLD", "NEW"), sorted(str(p.relative_to(live)) for p in live.rglob("*"))+++def test_a_failed_swap_keeps_a_file_the_user_made_at_a_never_installed_entry(tmp_path, monkeypatch):+ """The swap fails before ``brand_new`` is installed; a file the user created there meanwhile is+ theirs, and neither the rollback nor the journal recovery may delete it."""+ from hermes_cli import update_cmd_commit++ live, extracted = tmp_path / "live", tmp_path / "extracted"+ for side, text in ((live, "old"), (extracted, "new")):+ (side / "a").mkdir(parents=True)+ (side / "a" / "v.txt").write_text(text)+ (extracted / "brand_new").write_text("new entry")+ user_file = live / "brand_new"+ monkeypatch.setattr(update_cmd_commit, "arm_commit_obligations",+ lambda *a, **k: user_file.write_text("USER NOTE"))+ real_rename = os.rename++ def refuse_moving_a(src, dst): # Windows AV holding ``a`` open+ if os.fspath(src) == str(live / "a") and os.fspath(dst).endswith(".hermes-update-old"):+ raise PermissionError(13, "in use")+ return real_rename(src, dst)++ monkeypatch.setattr(update_cmd_zip.os, "rename", refuse_moving_a)+ _swap_or_recover(extracted, ["a", "brand_new"], live)+ monkeypatch.undo()+ assert user_file.is_file() and user_file.read_text() == "USER NOTE"+ assert (live / "a" / "v.txt").read_text() == "old"+ assert not [p.name for p in live.iterdir() if "hermes-update-staging" in p.name or p.name.endswith("-old")]