Skip to content

The agent that grows with you

fix: preserve runtime epoch floors across older backup restores

Teknium7fd8082e40d6
diff --git a/hermes_cli/backup.py b/hermes_cli/backup.py
index 83c16f70a4a..6904d0749b0 100644
--- a/hermes_cli/backup.py
+++ b/hermes_cli/backup.py
@@ -450,13 +450,55 @@ def _safe_restore_db(src: Path, dst: Path) -> bool:
"""Restore only while holding the destination authority's maintenance reservation."""
from gateway.runtime_ownership import OwnershipConflict, exclusive_maintenance
try:
- with exclusive_maintenance([dst.resolve().parent]):
- return _restore_db_pages(src, dst)
- except OwnershipConflict as exc:
+ with exclusive_maintenance([dst.absolute().parent, dst.resolve().parent]):
+ with _restore_epoch_source(src, dst) as prepared:
+ return _restore_db_pages(prepared, dst)
+ except (OwnershipConflict, OSError, sqlite3.Error) as exc:
logger.error("%s", exc)
return False
+def _restore_epoch(path: Path) -> int:
+ if not path.exists():
+ return 0
+ with closing(sqlite3.connect(path.resolve().as_uri() + "?mode=ro", uri=True)) as conn:
+ if not conn.execute("SELECT 1 FROM sqlite_master WHERE name='runtime_epoch'").fetchone():
+ return 0
+ row = conn.execute("SELECT epoch FROM runtime_epoch WHERE singleton=1").fetchone()
+ return int(row[0]) if row else 0
+
+
+@contextmanager
+def _restore_epoch_source(src: Path, dst: Path):
+ """Stage an epoch floor into the image BEFORE publishing it atomically.
+
+ Updating the destination after backup() would leave a crash window that
+ reuses an older worker epoch. Never modify the user's source snapshot.
+ Full restores retain all admissions/worker receipts; normal startup advances
+ this floor and reconciles started work to unknown, not replayable queued work.
+ An unreadable destination cannot prove its epoch floor: use transcript salvage
+ into a separate output rather than silently restoring with a recycled epoch.
+ """
+ floor = _restore_epoch(dst)
+ if floor <= _restore_epoch(src):
+ yield src
+ return
+ with tempfile.TemporaryDirectory(prefix='.restore-epoch-', dir=dst.parent) as work:
+ prepared = Path(work) / 'state.db'
+ if not _safe_copy_db(src, prepared):
+ raise OSError('Unable to stage a restore with a safe runtime epoch')
+ with closing(sqlite3.connect(prepared)) as conn:
+ conn.execute("""CREATE TABLE IF NOT EXISTS runtime_epoch (
+ singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
+ epoch INTEGER NOT NULL CHECK (epoch > 0), instance_id TEXT NOT NULL)""")
+ conn.execute("""INSERT INTO runtime_epoch VALUES(1,?, 'offline-restore')
+ ON CONFLICT(singleton) DO UPDATE SET epoch=excluded.epoch,
+ instance_id=excluded.instance_id""", (floor,))
+ conn.commit()
+ prepared.chmod(src.stat().st_mode)
+ yield prepared
+
+
def _restore_db_pages(src: Path, dst: Path) -> bool:
"""Restore snapshot *src* into live *dst* through the backup() API; unlink+move fallback.
@@ -801,7 +843,7 @@ def _import_db_member(
zf: zipfile.ZipFile, member: str, target: Path, new_file_mode: Optional[int] = None) -> None:
from gateway.runtime_ownership import OwnershipConflict, exclusive_maintenance
try:
- with exclusive_maintenance([target.resolve().parent]):
+ with exclusive_maintenance([target.absolute().parent, target.resolve().parent]):
_import_db_member_exclusive(zf, member, target, new_file_mode)
except OwnershipConflict as exc:
raise OSError(str(exc)) from exc
@@ -986,6 +1028,9 @@ def run_import(args) -> None:
t0 = time.monotonic()
restored, restored_external, errors, skipped_runtime, db_shrunk = _import_members(
zf, members, prefix, hermes_root, file_count)
+ if not restored and errors:
+ _print_capped("\nImport refused or failed; no files restored:", errors, " ")
+ sys.exit(1)
elapsed = time.monotonic() - t0
print(f"\nImport complete: {restored} files restored in {elapsed:.1f}s\n Target: {display_hermes_home()}")
if restored_external: