Skip to content

The agent that grows with you

fix: preflight canonical epoch before restoring profile snapshot files

Teknium610a97ea48df
diff --git a/hermes_cli/backup.py b/hermes_cli/backup.py
index bb484d653ea..49c64a2d1b5 100644
--- a/hermes_cli/backup.py
+++ b/hermes_cli/backup.py
@@ -468,6 +468,17 @@ def _restore_epoch(path: Path) -> int:
return int(row[0]) if row else 0
+def _restore_destination_epoch(dst: Path) -> int:
+ try:
+ return _restore_epoch(dst)
+ except (OSError, sqlite3.Error, ValueError) as exc:
+ raise OSError(
+ f"Cannot read the canonical runtime epoch at {dst}; refusing in-place restore. "
+ "Use `hermes sessions recover --source <snapshot> --output <separate-path>` "
+ "to salvage transcripts into a separate output."
+ ) from exc
+
+
@contextmanager
def _restore_epoch_source(src: Path, dst: Path):
"""Stage an epoch floor into the image BEFORE publishing it atomically.
@@ -482,14 +493,7 @@ def _restore_epoch_source(src: Path, dst: Path):
if dst.name != 'state.db' and dst.resolve().name != 'state.db':
yield src
return
- try:
- floor = _restore_epoch(dst)
- except (OSError, sqlite3.Error, ValueError) as exc:
- raise OSError(
- f"Cannot read the canonical runtime epoch at {dst}; refusing in-place restore. "
- "Use `hermes sessions recover --source <snapshot> --output <separate-path>` "
- "to salvage transcripts into a separate output."
- ) from exc
+ floor = _restore_destination_epoch(dst)
if floor <= _restore_epoch(src):
yield src
return
@@ -1361,6 +1365,17 @@ def _restore_quick_snapshot_exclusive(snapshot_id: str, home: Path) -> bool:
if dst.suffix == '.db' and _is_within(dst, home_res):
homes.update([dst.absolute().parent, dst.resolve().parent])
with exclusive_maintenance(homes):
+ # Epoch safety is a whole-profile preflight: copying config first would
+ # both partially roll back the profile and report a refused DB as success.
+ for rel in meta.get("files", {}):
+ src, dst = snap_dir / rel, home / rel
+ if (src.exists() and _is_within(src, snap_res) and _is_within(dst, home_res)
+ and (dst.name == "state.db" or dst.resolve().name == "state.db")):
+ try:
+ _restore_destination_epoch(dst)
+ except OSError as exc:
+ logger.error("%s", exc)
+ return False
restored = 0
for rel in meta.get("files", {}):
src = snap_dir / rel
diff --git a/tests/hermes_cli/test_restore_own_holder_guard.py b/tests/hermes_cli/test_restore_own_holder_guard.py
index 050500dec4c..c5039171ad7 100644
--- a/tests/hermes_cli/test_restore_own_holder_guard.py
+++ b/tests/hermes_cli/test_restore_own_holder_guard.py
@@ -128,6 +128,20 @@ def test_safe_restore_refuses_unreadable_canonical_epoch(tmp_path, caplog):
assert (dst.read_bytes(), src.read_bytes()) == before
assert "separate output" in caplog.text
+ # Public whole-profile restore must not report success from an unrelated
+ # config copy while refusing the canonical database.
+ dst.unlink()
+ _make_db(dst, "snapshot-good")
+ config = tmp_path / "config.yaml"
+ config.write_text("model: snapshot-model\n")
+ snapshot = backup_mod.create_quick_snapshot(hermes_home=tmp_path)
+ assert snapshot is not None
+ config.write_text("model: current-model\n")
+ dst.write_bytes(b"unreadable canonical epoch")
+ before = config.read_bytes(), dst.read_bytes()
+ assert backup_mod.restore_quick_snapshot(snapshot, hermes_home=tmp_path) is False
+ assert (config.read_bytes(), dst.read_bytes()) == before
+
def test_update_autorestore_still_works_without_holder(tmp_path):
dst = tmp_path / "state.db"