Skip to content

The agent that grows with you

refactor(backup): share quick-snapshot id validation with the CLI pre-check

kshitijk4poor1442477c561f
diff --git a/hermes_cli/backup.py b/hermes_cli/backup.py
index 12a8864f681..606729fc67a 100644
--- a/hermes_cli/backup.py
+++ b/hermes_cli/backup.py
@@ -1592,34 +1592,33 @@ def _restore_snapshot_entry(rel: str, src: Path, dst: Path) -> bool:
return False
-def _restore_quick_snapshot_exclusive(snapshot_id: str, home: Path) -> bool:
- """Restore state from a quick snapshot while ``home`` is reserved."""
- from gateway.runtime_ownership import exclusive_maintenance
- root = _quick_snapshot_root(home)
-
+def quick_snapshot_dir(snapshot_id: str, hermes_home: Optional[Path] = None) -> Optional[Path]:
+ """The directory of a restorable quick snapshot, or None for an invalid or missing id."""
+ root = _quick_snapshot_root(hermes_home)
# Security: reject snapshot_id values that contain path separators or
# traversal sequences so that `root / snapshot_id` stays inside root.
if not snapshot_id or "/" in snapshot_id or "\\" in snapshot_id or snapshot_id in (".", ".."):
logger.error("Invalid snapshot_id: %s", snapshot_id)
- return False
-
+ return None
snap_dir = root / snapshot_id
-
# Confirm the resolved path is still inside root (handles symlinks etc.)
try:
snap_dir.resolve().relative_to(root.resolve())
except ValueError:
logger.error("Snapshot path traversal blocked for id: %s", snapshot_id)
- return False
+ return None
+ if not snap_dir.is_dir() or not (snap_dir / "manifest.json").exists():
+ return None
+ return snap_dir
- if not snap_dir.is_dir():
- return False
- manifest_path = snap_dir / "manifest.json"
- if not manifest_path.exists():
+def _restore_quick_snapshot_exclusive(snapshot_id: str, home: Path) -> bool:
+ """Restore state from a quick snapshot while ``home`` is reserved."""
+ from gateway.runtime_ownership import exclusive_maintenance
+ snap_dir = quick_snapshot_dir(snapshot_id, home)
+ if snap_dir is None:
return False
-
- with open(manifest_path, encoding="utf-8-sig") as f:
+ with open(snap_dir / "manifest.json", encoding="utf-8-sig") as f:
meta = json.load(f)
snap_res, home_res = snap_dir.resolve(), home.resolve()
diff --git a/hermes_cli/cli_snapshot.py b/hermes_cli/cli_snapshot.py
index d39cbbe8979..3e069fc8643 100644
--- a/hermes_cli/cli_snapshot.py
+++ b/hermes_cli/cli_snapshot.py
@@ -12,7 +12,7 @@ logger = logging.getLogger(__name__)
def restore_snapshot_command(cli, parts):
- from hermes_cli.backup import list_quick_snapshots, restore_quick_snapshot
+ from hermes_cli.backup import list_quick_snapshots, quick_snapshot_dir, restore_quick_snapshot
from hermes_cli.gateway_tools_notify import gateway_store
if len(parts) < 3:
print(f" {_t('snapshot.usage_restore')}")
@@ -33,7 +33,8 @@ def restore_snapshot_command(cli, parts):
store = gateway_store(cli)
gateway = store is not None and getattr(cli, "_gateway_session_open", False)
try:
- if gateway and _local_snapshot_exists(snap_id):
+ # An invalid/missing target must not take a working gateway offline.
+ if gateway and quick_snapshot_dir(snap_id) is not None:
restored = _restore_gateway_snapshot(cli, store, snap_id)
else:
local_db = getattr(cli, "_session_db", None)
@@ -55,16 +56,6 @@ def restore_snapshot_command(cli, parts):
print(f" {_t('snapshot.not_found', snapshot_id=snap_id)}")
-def _local_snapshot_exists(snap_id):
- """An invalid/missing target must not take a working gateway offline."""
- from hermes_cli.backup import _quick_snapshot_root
- root = _quick_snapshot_root()
- if not snap_id or "/" in snap_id or "\\" in snap_id or snap_id in {".", ".."}:
- return False
- target = root / snap_id
- return target.resolve().is_relative_to(root.resolve()) and (target / "manifest.json").is_file()
-
-
def _pause_owner(home, control_home, identity):
from gateway.control_socket import pause_gateway_for_update, request_unserve_profile
if control_home != home: