The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): park a secondary profile during the snapshot handoff
kshitijk4poor73506be8bde3
diff --git a/hermes_cli/cli_snapshot.py b/hermes_cli/cli_snapshot.pyindex 6ed3dfcbc57..eb1649a0206 100644--- a/hermes_cli/cli_snapshot.py+++ b/hermes_cli/cli_snapshot.py@@ -61,13 +61,28 @@ def restore_snapshot_command(cli, parts):def _pause_owner(home, control_home, identity):+ """Hand the profile off; True when this restore parked a satellite and must serve it again."""from gateway.control_socket import pause_gateway_for_update, request_unserve_profilefrom hermes_cli.data_cleanup_holders import pause_acked+ from hermes_cli.profiles import parked_marker_pathsatellite = control_home != home+ ours = Falseif satellite:- ack = request_unserve_profile(control_home, home.name)- accepted = ack_confirmed(ack, "unserved", home.name) or (- isinstance(ack, dict) and ack.get("pending") and not ack.get("error"))+ marker = parked_marker_path(home)+ # Persist intent BEFORE teardown: the periodic rescan must not re-serve it mid-restore.+ try:+ marker.touch(exist_ok=False)+ ours = True+ except FileExistsError:+ pass # A profile the user parked stays parked and unserved.+ accepted = False+ try:+ ack = request_unserve_profile(control_home, home.name)+ accepted = ack_confirmed(ack, "unserved", home.name) or (+ isinstance(ack, dict) and ack.get("pending") and not ack.get("error"))+ finally:+ if ours and not accepted:+ marker.unlink(missing_ok=True)else:accepted = pause_acked(pause_gateway_for_update(control_home, timeout=8), identity)if not accepted:@@ -75,6 +90,7 @@ def _pause_owner(home, control_home, identity):retry = (f"Park the profile with `hermes -p {home.name} gateway stop` and retry."if satellite else "Stop the gateway and retry.")raise GatewayClientError(f"Gateway refused the snapshot maintenance handoff; nothing was restored. {retry}")+ return oursdef _restore_gateway_snapshot(cli, store, snap_id):@@ -83,6 +99,7 @@ def _restore_gateway_snapshot(cli, store, snap_id):from hermes_cli.data_cleanup_holders import manual_gateway_identityfrom hermes_cli.gateway_connection import GatewayConnectionfrom hermes_cli.gateway_runtime import control_home_for, discover_gateway_endpoint+ from hermes_cli.profiles import parked_marker_pathfrom hermes_cli.profiles_remove import wait_profile_unownedfrom hermes_constants import get_hermes_homefrom hermes_state_registry import close_all_under@@ -104,14 +121,14 @@ def _restore_gateway_snapshot(cli, store, snap_id):store.connection.close()unserved = Falsetry:- _pause_owner(home, control_home, identity)- unserved = control_home != home+ unserved = _pause_owner(home, control_home, identity)if not wait_profile_unowned(home):raise GatewayClientError("Gateway still owns the profile; nothing was restored. ""Wait for its running work to stop and retry.")return restore_quick_snapshot(snap_id)finally:if unserved:+ parked_marker_path(home).unlink(missing_ok=True) # serve-profile refuses a parked profileack = request_serve_profile_hot(control_home, home.name)if not ack_confirmed(ack, "served", home.name):logger.warning("Profile re-serve after snapshot restore: %s", ack)diff --git a/tests/hermes_cli/test_gateway_snapshot_restore_guards.py b/tests/hermes_cli/test_gateway_snapshot_restore_guards.pyindex c439b78f71d..9d0a5702a29 100644--- a/tests/hermes_cli/test_gateway_snapshot_restore_guards.py+++ b/tests/hermes_cli/test_gateway_snapshot_restore_guards.py@@ -9,6 +9,7 @@ from hermes_cli.gateway_client import GatewayClientErrorfrom hermes_cli.gateway_connection import GatewayConnectionfrom hermes_cli.gateway_runtime import GatewayDiscovery, GatewayEndpointfrom hermes_cli.gateway_session_store import GatewaySessionStore+from hermes_cli.profiles import parked_marker_pathclass _Connection:@@ -44,6 +45,8 @@ def _corrupt_manifest(sid):("invalid-id", lambda sid: False, "manual", "Snapshot not found: ../outside"),("other-pid-paused", None, "manual", "Gateway refused the snapshot maintenance handoff"),("satellite-unserve-error", None, "manual", "nothing was restored. Park the profile with `hermes -p state"),+ # A profile the user parked is restored but stays parked: the restore does not serve it.+ ("satellite-pre-parked", lambda sid: True, "manual", "Restored state from"),("corrupt-manifest", _corrupt_manifest, "manual", "was not fully restored"),# A remote gateway's chat restores this host's snapshot offline, as on main, and stays connected.("remote", lambda sid: True, None, "Restart recommended"),@@ -65,6 +68,9 @@ def test_restore_reply_follows_the_restore_not_the_rejoin(tmp_path, monkeypatch,cli = SimpleNamespace(_session_db=store, _gateway_session_open=True, session_id="kept",agent=object(), conversation_history=[])satellite = case.startswith("satellite-")+ marker = parked_marker_path(home)+ if case == "satellite-pre-parked":+ marker.touch()control_home = str(tmp_path) if satellite else str(home)endpoint = GatewayEndpoint(str(home), "owner", 5, 1, "http://127.0.0.1:1", "none",frozenset({"session-authority-v1"}), control_home)@@ -77,10 +83,18 @@ def test_restore_reply_follows_the_restore_not_the_rejoin(tmp_path, monkeypatch,monkeypatch.setattr("gateway.control_socket.pause_gateway_for_update",lambda *a, **kw: paused.append(a) or {"pausing": True,"pid": 99 if case == "other-pid-paused" else 4321})- monkeypatch.setattr("gateway.control_socket.request_unserve_profile",- lambda *a: {"unserved": home.name, "error": "teardown failed"}- if case == "satellite-unserve-error" else {"unserved": home.name})- monkeypatch.setattr("gateway.control_socket.request_serve_profile_hot", lambda *a: {"pending": True})+ parked_at = {} # The host's rescan skips a parked profile; serve-profile refuses one.++ def unserve(*a):+ parked_at["unserve"] = marker.exists()+ return ({"unserved": home.name, "error": "teardown failed"}+ if case == "satellite-unserve-error" else {"unserved": home.name})++ def serve(*a):+ parked_at["serve"] = marker.exists()+ return {"pending": True}+ monkeypatch.setattr("gateway.control_socket.request_unserve_profile", unserve)+ monkeypatch.setattr("gateway.control_socket.request_serve_profile_hot", serve)monkeypatch.setattr("hermes_cli.profiles_remove.wait_profile_unowned", lambda home: True)monkeypatch.setattr("hermes_cli.backup.restore_quick_snapshot",restore or (lambda sid: pytest.fail("restore ran without the identified owner's handoff")))@@ -96,3 +110,7 @@ def test_restore_reply_follows_the_restore_not_the_rejoin(tmp_path, monkeypatch,else:assert "Reconnecting to the gateway failed: permission_denied" in outassert store.connection is not old and store.connection.launch_api_key == "launch-only-key"+ if satellite:+ assert parked_at.pop("unserve") is True # parked before the teardown, so no rescan re-serves it+ assert parked_at == ({} if case in {"satellite-unserve-error", "satellite-pre-parked"} else {"serve": False})+ assert marker.exists() is (case == "satellite-pre-parked")diff --git a/tests/hermes_cli/test_gateway_snapshot_restore_live.py b/tests/hermes_cli/test_gateway_snapshot_restore_live.pyindex 14c40f7870c..18f335424cf 100644--- a/tests/hermes_cli/test_gateway_snapshot_restore_live.py+++ b/tests/hermes_cli/test_gateway_snapshot_restore_live.py@@ -18,6 +18,7 @@ from hermes_cli.cli_init_mixin import CLIInitMixinfrom hermes_cli.gateway_connection import GatewayConnectionfrom hermes_cli.gateway_runtime import discover_gateway_endpointfrom hermes_cli.gateway_session_store import GatewaySessionStore+from hermes_cli.profiles import parked_marker_pathfrom tests.gateway.fixtures.local_recovery_probe import Model, child_envfrom tests.waiting import wait_until@@ -136,7 +137,7 @@ def _legacy_row(home):@pytest.mark.platforms("posix") # The fixture exercises the POSIX default/named profile layout.@pytest.mark.parametrize("multiplex, mode", [(False, "retained"), (True, "missing"), (False, "legacy-leased")],ids=["standalone-resume-restored", "satellite-create-missing", "legacy-reimport-leased"])-def test_snapshot_restore_rejoins_the_restored_authority(local_gateway, multiplex, mode, capsys):+def test_snapshot_restore_rejoins_the_restored_authority(local_gateway, multiplex, mode, capsys, monkeypatch):from cli import HermesCLIfrom types import MethodTypehome = local_gateway(multiplex)@@ -174,6 +175,17 @@ def test_snapshot_restore_rejoins_the_restored_authority(local_gateway, multiple# The interactive CLI holds its real active-session lease for the whole chat.assert cli._claim_active_session("cli", stderr=True)retained = mode != "missing"+ rescans = []+ if multiplex:+ from gateway.control_socket import rescan_gateway_profiles+ from hermes_cli import profiles_remove+ unowned = profiles_remove.wait_profile_unowned++ def rescan_mid_handoff(target):+ # The host's periodic rescan lands between the unserve and the restore.+ rescans.append(rescan_gateway_profiles(target.parent.parent))+ return unowned(target)+ monkeypatch.setattr(profiles_remove, "wait_profile_unowned", rescan_mid_handoff)try:CLICommandsMixin._snapshot_restore(cli, ["/snapshot", "restore", snapshot])@@ -199,6 +211,8 @@ def test_snapshot_restore_rejoins_the_restored_authority(local_gateway, multipleassert after is not Noneassert _epoch(store) > before_epochif multiplex:+ assert rescans and home.name not in rescans[0]["added"], rescans+ assert not parked_marker_path(home).exists() # served again after the restoreassert before.instance_id == after.instance_idassert _default_profile_state(home) == default_beforeelse: