Skip to content

The agent that grows with you

fix(import): refuse a damaged backup archive before touching the home

kshitijk4poorb24b7b725ce8
diff --git a/hermes_cli/backup.py b/hermes_cli/backup.py
index 0daaa8d8eb3..c038f18b127 100644
--- a/hermes_cli/backup.py
+++ b/hermes_cli/backup.py
@@ -774,6 +774,28 @@ def _validate_backup_zip(zf: zipfile.ZipFile) -> tuple[bool, str]:
return True, ""
+def _find_corrupt_members(zf: zipfile.ZipFile, members: List[str]) -> List[str]:
+ """Return ``"<member>: <error>"`` for every member whose data does not decompress or
+ fails its CRC, streaming each one in 1 MiB chunks so a multi-GB ``state.db`` is never
+ held in memory.
+
+ ``is_zipfile()``/``namelist()`` only read the central directory, so an archive with a
+ rotten member passes them and the damage surfaces as ``zlib.error``/``BadZipFile`` in
+ the middle of the restore, after earlier members already replaced the user's files
+ (#121258). Not ``zf.testzip()``: it lets ``zlib.error`` escape and names at most the
+ first bad member.
+ """
+ bad: list[str] = []
+ for member in members:
+ try:
+ with zf.open(member) as src:
+ while src.read(1 << 20): # CRC is checked when the stream hits EOF
+ pass
+ except (zipfile.BadZipFile, zlib.error, EOFError) as exc:
+ bad.append(f"{member}: {exc}")
+ return bad
+
+
def _detect_prefix(zf: zipfile.ZipFile) -> str:
"""Detect if the zip has a common directory prefix wrapping all entries."""
names = [n for n in zf.namelist() if not n.endswith("/")]
@@ -982,7 +1004,10 @@ def _import_members(
raise
restored += 1
restored_external += external
- except (PermissionError, OSError, zipfile.BadZipFile, zlib.error) as exc:
+ except (PermissionError, OSError, zipfile.BadZipFile, zlib.error, EOFError) as exc:
+ # BadZipFile/zlib.error/EOFError: the pre-flight in run_import already refused
+ # archives that fail to decompress; this keeps a member that rots between the two
+ # passes (archive on failing media) from aborting the rest of the restore.
errors.append(f"{label}: {exc}")
if restored % 500 == 0:
@@ -1016,6 +1041,14 @@ def run_import(args) -> Optional[int]:
print(f"Detected archive prefix: {prefix!r} (will be stripped)")
if not args.force and not _confirm_import_overwrite(hermes_root):
return
+ # Every member is decompressed once here and once again below: a damaged archive
+ # must be refused while the home is still untouched, not half-way through the restore.
+ print("\nChecking archive integrity ...")
+ corrupt = _find_corrupt_members(zf, members)
+ if corrupt:
+ _print_capped(f"Error: backup archive is damaged ({len(corrupt)} member(s) fail to "
+ f"decompress or fail their CRC); nothing was restored:", corrupt, " ")
+ return 1
print(f"\nImporting {file_count} files ...")
hermes_root.mkdir(parents=True, exist_ok=True)
t0 = time.monotonic()
diff --git a/tests/hermes_cli/test_backup.py b/tests/hermes_cli/test_backup.py
index ee57e11fb24..53e5a706af6 100644
--- a/tests/hermes_cli/test_backup.py
+++ b/tests/hermes_cli/test_backup.py
@@ -1,5 +1,6 @@
"""Tests for hermes backup and import commands."""
+import io
import json
import os
import socket
@@ -553,37 +554,134 @@ class TestImport:
assert (hermes_home / "config.yaml").read_text() == "model: test\n"
- def test_import_skips_corrupt_deflate_member_and_continues(self, tmp_path, monkeypatch, capsys):
- """One damaged member warns without rolling back earlier files or stopping later ones."""
+ @staticmethod
+ def _corrupt_member(zip_path: Path, name: str, *, stored: bool = False) -> None:
+ """Damage *name*'s data in place, leaving the central directory (and so
+ ``is_zipfile``/``namelist``) intact: a deflate member gets reserved block type 3
+ (``zlib.error``); a stored member gets one payload byte flipped (``Bad CRC-32``)."""
+ with zipfile.ZipFile(zip_path) as zf:
+ info = zf.getinfo(name)
+ data_offset = info.header_offset + 30 + len(info.filename) + len(info.extra)
+ with zip_path.open("r+b") as archive:
+ archive.seek(data_offset)
+ first_byte = archive.read(1)[0]
+ archive.seek(data_offset)
+ archive.write(bytes([first_byte ^ 0xFF if stored else first_byte | 0b110]))
+
+ def _home_for_corrupt_import(self, tmp_path, monkeypatch) -> Path:
hermes_home = tmp_path / ".hermes"
hermes_home.mkdir()
+ (hermes_home / "config.yaml").write_text("model: live\n")
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ return hermes_home
+
+ @pytest.mark.parametrize("stored", [False, True], ids=["deflate-zlib.error", "stored-bad-crc"])
+ def test_import_refuses_damaged_archive_before_touching_home(self, tmp_path, monkeypatch, capsys, stored):
+ """#121258: a member that fails to decompress (or its CRC) must be found BEFORE the
+ first file is replaced -- not surface as a zlib.error traceback half-way through --
+ and the command must exit non-zero without any "restored" wording."""
+ hermes_home = self._home_for_corrupt_import(tmp_path, monkeypatch)
+ zip_path = tmp_path / "backup.zip"
+ compression = zipfile.ZIP_STORED if stored else zipfile.ZIP_DEFLATED
+ with zipfile.ZipFile(zip_path, "w", compression=compression) as zf:
+ zf.writestr("config.yaml", "model: restored\n")
+ zf.writestr("damaged.json", '{"will": "fail"}')
+ zf.writestr("sessions/after.json", '{"restored": true}')
+ self._corrupt_member(zip_path, "damaged.json", stored=stored)
+ assert zipfile.is_zipfile(zip_path)
+
+ from hermes_cli.backup import run_import
+ from hermes_cli.main import cmd_import
+
+ assert run_import(Namespace(zipfile=str(zip_path), force=True)) == 1
+ out = capsys.readouterr().out
+ assert "backup archive is damaged (1 member(s)" in out
+ assert "damaged.json:" in out
+ assert "Importing" not in out
+ assert "restored" not in out.replace("nothing was restored", "")
+ # The home is exactly as it was.
+ assert (hermes_home / "config.yaml").read_text() == "model: live\n"
+ assert sorted(p.name for p in hermes_home.iterdir()) == ["config.yaml"]
+ assert cmd_import(Namespace(zipfile=str(zip_path), force=True)) == 1
+
+ def test_import_lists_every_damaged_member(self, tmp_path, monkeypatch, capsys):
+ """All bad members are reported in one run (stdlib ``testzip()`` stops at the first
+ and lets ``zlib.error`` escape); the list is capped like the other summaries."""
+ self._home_for_corrupt_import(tmp_path, monkeypatch)
+ zip_path = tmp_path / "backup.zip"
+ names = [f"skills/s{i}/SKILL.md" for i in range(12)]
+ with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as zf:
+ zf.writestr("config.yaml", "model: restored\n")
+ for name in names:
+ zf.writestr(name, f"# {name}\n" * 20)
+ for name in names:
+ self._corrupt_member(zip_path, name)
+
+ from hermes_cli.backup import run_import
+
+ assert run_import(Namespace(zipfile=str(zip_path), force=True)) == 1
+ out = capsys.readouterr().out
+ assert "backup archive is damaged (12 member(s)" in out
+ assert sum(1 for line in out.splitlines() if line.strip().startswith("skills/s")) == 10
+ assert "... and 2 more" in out
+ def test_find_corrupt_members_reports_truncated_stream(self):
+ """A member whose data ends early (``EOFError`` from ZipExtFile) is damage too."""
+ from hermes_cli.backup import _find_corrupt_members
+
+ class _Truncated:
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *exc):
+ return False
+
+ def read(self, n):
+ raise EOFError
+
+ class _FakeZip:
+ def open(self, member):
+ if member == "short.bin":
+ return _Truncated()
+ return io.BytesIO(b"ok")
+
+ assert _find_corrupt_members(_FakeZip(), ["config.yaml", "short.bin"]) == ["short.bin: "]
+
+ def test_import_skips_member_that_rots_after_preflight_and_reports_incomplete(
+ self, tmp_path, monkeypatch, capsys
+ ):
+ """A member that passes the integrity pass but fails while being written (media going
+ bad under the archive) warns, leaves the damaged target absent, restores the rest and
+ exits 1 -- rather than escaping as a traceback."""
+ hermes_home = self._home_for_corrupt_import(tmp_path, monkeypatch)
zip_path = tmp_path / "backup.zip"
with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as zf:
zf.writestr("config.yaml", "model: restored\n")
zf.writestr("damaged.json", '{"will": "fail"}')
zf.writestr("sessions/after.json", '{"restored": true}')
- with zipfile.ZipFile(zip_path) as zf:
- info = zf.getinfo("damaged.json")
- data_offset = info.header_offset + 30 + len(info.filename) + len(info.extra)
- with zip_path.open("r+b") as archive:
- archive.seek(data_offset)
- first_byte = archive.read(1)
- archive.seek(data_offset)
- # DEFLATE block type 3 is reserved, so zlib rejects this member.
- archive.write(bytes([first_byte[0] | 0b110]))
+ import hermes_cli.backup as backup_mod
- from hermes_cli.backup import run_import
- run_import(Namespace(zipfile=str(zip_path), force=True))
+ real_extract = backup_mod._extract_member_atomically
+
+ def flaky_extract(zf, member, target, new_file_mode=None):
+ if member == "damaged.json":
+ raise EOFError("Compressed file ended before the end-of-stream marker was reached")
+ return real_extract(zf, member, target, new_file_mode)
+
+ monkeypatch.setattr(backup_mod, "_extract_member_atomically", flaky_extract)
+ assert backup_mod.run_import(Namespace(zipfile=str(zip_path), force=True)) == 1
+
+ out = capsys.readouterr().out
assert (hermes_home / "config.yaml").read_text() == "model: restored\n"
assert not (hermes_home / "damaged.json").exists()
assert not list(hermes_home.glob(".damaged.json.*.partial"))
assert (hermes_home / "sessions" / "after.json").read_text() == '{"restored": true}'
- assert "Warnings (1 files skipped):" in capsys.readouterr().out
+ assert "Warnings (1 files skipped):" in out
+ assert "Import incomplete" in out
+ assert "has been restored" not in out
def test_skipped_member_reports_incomplete_and_exits_1(self, tmp_path, monkeypatch, capsys):
"""A member that could not be written is a partial restore: the CLI must not print
@@ -946,13 +1044,20 @@ class _ExplodingMember:
def _break_member(monkeypatch, failing_member: str) -> None:
- """Make ``ZipFile.open`` hand back a dying stream for one member only."""
+ """Make ``ZipFile.open`` hand back a dying stream for one member only.
+
+ The first open of that member (run_import's integrity pre-flight, which reads every
+ member before anything is written) is served for real; the restore's own open dies.
+ """
real_open = zipfile.ZipFile.open
+ seen: list[str] = []
def _patched(self, name, *args, **kwargs):
filename = name.filename if isinstance(name, zipfile.ZipInfo) else name
if filename == failing_member:
- return _ExplodingMember()
+ seen.append(filename)
+ if len(seen) > 1:
+ return _ExplodingMember()
return real_open(self, name, *args, **kwargs)
monkeypatch.setattr(zipfile.ZipFile, "open", _patched)
diff --git a/website/docs/reference/cli-commands.md b/website/docs/reference/cli-commands.md
index 16916448ef5..2fc9cc35fb7 100644
--- a/website/docs/reference/cli-commands.md
+++ b/website/docs/reference/cli-commands.md
@@ -1183,7 +1183,7 @@ Restore a previously created Hermes backup into your Hermes home directory. All
Stop the gateway before importing to avoid conflicts with running processes.
:::
-**Exit status:** `1` when any file from the archive could not be restored (listed under `Warnings (N files skipped)` and summarised as `Import incomplete: …`). The files that did land stay in place, but a script or the dashboard will not report a partial restore as success. Runtime files the import deliberately keeps from this machine (`gateway.pid`, `gateway_state.json`, …) and the older-backup session warning below do not change the exit status.
+**Exit status:** `1` when the archive is damaged — before anything is written, every member is decompressed once and its CRC checked; if any fail, the command prints `Error: backup archive is damaged (N member(s) …)` with the offending members and stops with the Hermes home untouched. Also `1` when any file from the archive could not be restored (listed under `Warnings (N files skipped)` and summarised as `Import incomplete: …`). The files that did land stay in place, but a script or the dashboard will not report a partial restore as success. Runtime files the import deliberately keeps from this machine (`gateway.pid`, `gateway_state.json`, …) and the older-backup session warning below do not change the exit status.
### SQLite databases