Skip to content

The agent that grows with you

fix(update): an unpinned ZIP update arms the restart debt for the archive's commit (review F24)

Teknium62bcb1d69849
diff --git a/hermes_cli/update_cmd_zip.py b/hermes_cli/update_cmd_zip.py
index f09f4304f5b..0c89cc65076 100644
--- a/hermes_cli/update_cmd_zip.py
+++ b/hermes_cli/update_cmd_zip.py
@@ -309,6 +309,14 @@ def _extract_zip_safely(zip_path: str, tmp_dir: str) -> None:
zf.extractall(tmp_dir)
+def _archive_commit(zip_path: str) -> Optional[str]:
+ """The commit a GitHub archive was made from: ``git archive`` stores it as the ZIP comment."""
+ import zipfile
+ with zipfile.ZipFile(zip_path) as zf:
+ comment = zf.comment.decode("ascii", "replace").strip()
+ return comment if re.fullmatch(r"[0-9a-f]{40}", comment) else None
+
+
def _extracted_root(tmp_dir: str, branch: str) -> str:
"""GitHub ZIPs extract to ``hermes-agent-<branch>/``; fall back to the first non-``__MACOSX`` dir."""
extracted = os.path.join(tmp_dir, f"hermes-agent-{branch}")
@@ -458,8 +466,9 @@ def _journaled_stage_and_swap(extracted: str, entries: list[str], root: Path, ta
return staged
-def _download_and_swap_zip(branch: str, zip_url: str, target_sha: str | None = None) -> None:
- """Download the source ZIP for *branch* and two-phase swap it into the checkout.
+def _download_and_swap_zip(branch: str, zip_url: str, target_sha: str | None = None) -> Optional[str]:
+ """Download the source ZIP for *branch* and two-phase swap it into the checkout; return the commit it
+ installed (``target_sha``, else the archive's own, else None).
``sys.exit(1)`` on any failure; the install ends fully updated or fully rolled back.
Two-phase: stage every entry (dirs AND top-level files) beside its target, then swap all in with
same-filesystem renames, rolling back on failure — one-at-a-time replacement left a mixed, unbootable
@@ -473,6 +482,9 @@ def _download_and_swap_zip(branch: str, zip_url: str, target_sha: str | None = N
try:
zip_path = os.path.join(tmp_dir, f"hermes-agent-{branch}.zip")
urlretrieve(zip_url, zip_path)
+ # An unpinned branch ZIP still names its commit; without it the restart debt is armed for ''
+ # and can never be proven discharged.
+ target_sha = target_sha or _archive_commit(zip_path)
print("→ Extracting...")
_extract_zip_safely(zip_path, tmp_dir)
extracted = _extracted_root(tmp_dir, branch)
@@ -481,6 +493,7 @@ def _download_and_swap_zip(branch: str, zip_url: str, target_sha: str | None = N
_require_staging_space(extracted, entries, project_root)
staged = _journaled_stage_and_swap(extracted, entries, Path(project_root), target_sha)
print(f"✓ Updated {len(staged)} items from ZIP")
+ return target_sha
except Exception as e:
# The swap rolled itself back; a rollback that could not finish leaves the journal, which
# this settles now (or the next launch's _early_recovery does) instead of leaking siblings.
@@ -531,8 +544,8 @@ def _update_via_zip(args, *, had_desktop_app_before_update: bool = False,
if (not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", repository)
or any(part in (".", "..") for part in repository.split("/"))):
raise ValueError("ZIP update requires a GitHub owner/repository")
- _download_and_swap_zip(branch, f"https://github.com/{repository}/archive/{ref}.zip", target_sha)
- completion_request["expected_sha"] = target_sha
+ installed = _download_and_swap_zip(branch, f"https://github.com/{repository}/archive/{ref}.zip", target_sha)
+ completion_request["expected_sha"] = installed or target_sha
completion_request["apply_mode"] = "zip"
_complete_source_update(completion_request)
return True
diff --git a/tests/hermes_cli/test_update_zip_completion.py b/tests/hermes_cli/test_update_zip_completion.py
index 5dbd9856fb6..85c687be605 100644
--- a/tests/hermes_cli/test_update_zip_completion.py
+++ b/tests/hermes_cli/test_update_zip_completion.py
@@ -435,3 +435,28 @@ def test_installed_app_without_a_checkout_build_is_still_rebuilt(zip_update, mon
update_cmd._cmd_update_impl(SimpleNamespace(branch="main", yes=True), False)
assert built == [rebuilt]
+
+
+def test_unpinned_zip_update_owes_the_restart_for_the_archive_commit(tmp_path, monkeypatch):
+ """A branch ZIP (no target SHA) still names its commit: GitHub archives carry it as the ZIP comment
+ (``git archive``). The restart debt and the completion are armed for that commit, never for ''."""
+ from hermes_cli import update_cmd_commit
+
+ sha = "8192da90e0afb20010a1c2f5da83db305d05ac5a"
+ root = tmp_path / "checkout"
+ root.mkdir()
+ (root / "payload.txt").write_text("old", encoding="utf-8")
+ archive = tmp_path / "source.zip"
+ with zipfile.ZipFile(archive, "w") as out:
+ out.writestr("hermes-agent-main/payload.txt", "new")
+ out.comment = sha.encode()
+ monkeypatch.setattr("urllib.request.urlretrieve", lambda url, dst: urlretrieve(archive.as_uri(), dst))
+ monkeypatch.setattr(main, "PROJECT_ROOT", root)
+ monkeypatch.setattr(update_cmd_zip, "_abort_zip_update_if_dirty_tree", lambda: None)
+ armed, completed = [], []
+ monkeypatch.setattr(update_cmd_commit, "arm_commit_obligations", lambda _root, expected: armed.append(expected))
+ monkeypatch.setattr(update_cmd, "_complete_source_update", lambda request: completed.append(dict(request)))
+ update_cmd_zip._update_via_zip(SimpleNamespace(branch="main"), completion_request={})
+ assert (root / "payload.txt").read_text(encoding="utf-8") == "new"
+ assert armed == [sha]
+ assert [request["expected_sha"] for request in completed] == [sha]