Skip to content

The agent that grows with you

fix(cli): refuse a supervised gateway before the snapshot handoff

kshitijk4poor23cf772f8dc0
diff --git a/hermes_cli/cli_snapshot.py b/hermes_cli/cli_snapshot.py
index d73e2f8c011..d39cbbe8979 100644
--- a/hermes_cli/cli_snapshot.py
+++ b/hermes_cli/cli_snapshot.py
@@ -65,14 +65,15 @@ def _local_snapshot_exists(snap_id):
return target.resolve().is_relative_to(root.resolve()) and (target / "manifest.json").is_file()
-def _pause_owner(home, control_home):
+def _pause_owner(home, control_home, identity):
from gateway.control_socket import pause_gateway_for_update, request_unserve_profile
if control_home != home:
ack = request_unserve_profile(control_home, home.name)
accepted = ack is not None and (ack.get("unserved") == home.name or ack.get("pending"))
else:
ack = pause_gateway_for_update(control_home, timeout=8)
- accepted = ack is not None and ack.get("pausing")
+ accepted = (ack is not None and (ack.get("pausing") or ack.get("already_stopping"))
+ and identity is not None and ack.get("pid") == identity.get("pid"))
if not accepted:
raise GatewayClientError("Gateway refused the snapshot maintenance handoff; nothing was restored. "
"Stop the gateway and retry.")
@@ -81,6 +82,7 @@ def _pause_owner(home, control_home):
def _restore_gateway_snapshot(cli, store, snap_id):
from gateway.control_socket import request_serve_profile_hot
from hermes_cli.backup import restore_quick_snapshot
+ from hermes_cli.data_cleanup_holders import manual_gateway_identity
from hermes_cli.gateway_connection import GatewayConnection
from hermes_cli.gateway_profile_lifecycle import _confirmed, _failure
from hermes_cli.gateway_runtime import control_home_for, discover_gateway_endpoint
@@ -94,13 +96,20 @@ def _restore_gateway_snapshot(cli, store, snap_id):
if observed.state != "ready" or observed.endpoint is None:
raise GatewayClientError("Local gateway is not ready for snapshot maintenance. Reconnect and retry.")
control_home = control_home_for(home, observed.endpoint).resolve()
+ identity = None
+ if control_home == home:
+ # A supervised owner is relaunched right after the pause and takes the profile back.
+ try:
+ identity = manual_gateway_identity(home)
+ except RuntimeError as exc:
+ raise GatewayClientError(f"Nothing was restored: {exc}.") from exc
store.close_reader()
close_all_under(home)
# A final viewer cannot redial from an idle poll while maintenance is replacing its database.
store.connection.close()
unserved = False
try:
- _pause_owner(home, control_home)
+ _pause_owner(home, control_home, identity)
unserved = control_home != home
if not wait_profile_unowned(home):
raise GatewayClientError("Gateway still owns the profile; nothing was restored. "
diff --git a/hermes_cli/data_cleanup_holders.py b/hermes_cli/data_cleanup_holders.py
index d2fe6a10693..3288a3e63ce 100644
--- a/hermes_cli/data_cleanup_holders.py
+++ b/hermes_cli/data_cleanup_holders.py
@@ -14,21 +14,36 @@ _OWNER_STOP_HINTS = {
}
-def _drain_manual_gateway(home: Path) -> None:
- from gateway.control_socket import identify_gateway, launch_supervisor, pause_gateway_for_update
- from gateway.status import get_process_start_time, get_running_pid_identity_strict, _pid_exists
+def manual_gateway_identity(home: Path) -> dict | None:
+ """The ``identify`` answer of the manually launched gateway owning ``home``, None when none runs.
+
+ Raises RuntimeError for a gateway this client may not drain: a supervisor (systemd, launchd,
+ Desktop, ...) relaunches it right after a pause, so it would take the profile back.
+ """
+ from gateway.control_socket import identify_gateway, launch_supervisor
+ from gateway.status import get_running_pid_identity_strict
identity = identify_gateway(home)
if identity is None:
if get_running_pid_identity_strict(home / "gateway.pid") is not None:
raise RuntimeError(f"gateway has no usable control socket for {home}; stop it through its owner and retry")
- return
+ return None
if not identity.get("hermes_home") or Path(identity["hermes_home"]).resolve() != home:
raise RuntimeError(f"gateway control identity does not match {home}")
owner = launch_supervisor(identity)
if owner != "manual":
hint = _OWNER_STOP_HINTS.get(owner or "", "stop it through its supervisor before retrying")
raise RuntimeError(f"{owner or 'unknown'} gateway still owns {home}; {hint}")
+ return identity
+
+
+def _drain_manual_gateway(home: Path) -> None:
+ from gateway.control_socket import identify_gateway, pause_gateway_for_update
+ from gateway.status import get_process_start_time, get_running_pid_identity_strict, _pid_exists
+
+ identity = manual_gateway_identity(home)
+ if identity is None:
+ return
if identity.get("served_profiles"):
raise RuntimeError("gateway serves multiple profiles; stop it explicitly before deleting one profile's data")
pid = identity.get("pid")