Skip to content

The agent that grows with you

test(cli): trim CLI-16 snapshot restore tests to the salvage bar

kshitijk4poor61b88bfbd0dc
diff --git a/tests/hermes_cli/test_gateway_snapshot_restore_guards.py b/tests/hermes_cli/test_gateway_snapshot_restore_guards.py
index 82215c41b03..0770c7f4a65 100644
--- a/tests/hermes_cli/test_gateway_snapshot_restore_guards.py
+++ b/tests/hermes_cli/test_gateway_snapshot_restore_guards.py
@@ -1,4 +1,4 @@
-"""Refused/failed CLI-16 restores never bypass ownership or abandon the CLI viewer."""
+"""CLI-16: the restore's own outcome decides the reply; a supervised owner or bad id never drains."""
from types import SimpleNamespace
import pytest
@@ -6,108 +6,76 @@ import pytest
from hermes_cli.backup import create_quick_snapshot
from hermes_cli.cli_commands_mixin import CLICommandsMixin
from hermes_cli.gateway_client import GatewayClientError
-from hermes_cli.gateway_connection import GatewayConnection, SessionState
+from hermes_cli.gateway_connection import GatewayConnection
from hermes_cli.gateway_runtime import GatewayDiscovery, GatewayEndpoint
from hermes_cli.gateway_session_store import GatewaySessionStore
class _Connection:
- def __init__(self, refusal=None):
+ def __init__(self):
self.final = False
- self.states = {}
- self.calls = []
- self.refusal = refusal
def close(self):
self.final = True
- def state(self, sid):
- return self.states.setdefault(sid, SessionState(sid))
-
def call(self, method, **params):
- self.calls.append(method)
- if self.refusal:
- raise GatewayClientError(self.refusal)
- assert method == "session.resume"
- return {"stored_session_id": "kept", "subscription_id": "fresh-subscription",
- "revision": 4, "execution_generation": 2, "messages": [], "info": {}}
+ # The rejoin after maintenance fails: it must not decide the restore's reply.
+ raise GatewayClientError("permission_denied")
+
+ def resume(self, call, session_id, *, typed=False):
+ return call("session.resume", session_id=session_id) # GatewayConnection.resume, no launch key
+
+
+def _restore_error(sid):
+ raise OSError("fixture restore refused")
-def viewer(tmp_path, monkeypatch):
- home = tmp_path / "state"
[email protected]("case, restore, supervisor, expected", [
+ ("restored", lambda sid: True, "manual", "Restored state from"),
+ ("restore-error", _restore_error, "manual", "fixture restore refused"),
+ ("supervised", None, "systemd", "stop its systemd unit"),
+ ("satellite-not-reserved", lambda sid: True, "manual", "is not served again yet: host operation is still pending"),
+ ("invalid-id", lambda sid: False, "manual", "Snapshot not found: ../outside"),
+ ("other-pid-paused", None, "manual", "Gateway refused the snapshot maintenance handoff"),
+])
+def test_restore_reply_follows_the_restore_not_the_rejoin(tmp_path, monkeypatch, capsys,
+ case, restore, supervisor, expected):
+ home = (tmp_path / "state").resolve()
home.mkdir()
monkeypatch.setenv("HERMES_HOME", str(home))
monkeypatch.delenv("HERMES_TUI_GATEWAY_URL", raising=False)
(home / "config.yaml").write_text("{}\n", encoding="utf-8")
snapshot = create_quick_snapshot(hermes_home=home)
- assert snapshot
- old, fresh = _Connection(), _Connection()
+ old = _Connection()
store = GatewaySessionStore(old)
- closed = []
- store._reader = SimpleNamespace(close=lambda: closed.append(True))
- store.snapshots["kept"] = {"subscription_id": "stale"}
cli = SimpleNamespace(_session_db=store, _gateway_session_open=True, session_id="kept",
- agent=object(), conversation_history=[], model="fixture",
- _goal_manager=object(), _loop_manager=object(), _heartbeat_manager=object())
+ agent=object(), conversation_history=[])
+ satellite = case == "satellite-not-reserved"
+ 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"}), str(home))
+ frozenset({"session-authority-v1"}), control_home)
monkeypatch.setattr("hermes_cli.gateway_runtime.discover_gateway_endpoint",
lambda home: GatewayDiscovery("ready", endpoint))
- monkeypatch.setattr(GatewayConnection, "shared", classmethod(lambda cls: fresh))
- monkeypatch.setattr("hermes_cli.gateway_tools.bind_session", lambda *args: None)
- monkeypatch.setattr("gateway.control_socket.pause_gateway_for_update", lambda *a, **kw: {"pausing": True})
+ monkeypatch.setattr(GatewayConnection, "shared", classmethod(lambda cls: _Connection()))
+ monkeypatch.setattr("gateway.control_socket.identify_gateway", lambda h, **kw: {
+ "pid": 4321, "hermes_home": str(home), "launch_supervisor": supervisor})
+ paused = []
+ 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})
+ monkeypatch.setattr("gateway.control_socket.request_serve_profile_hot", lambda *a: {"pending": True})
monkeypatch.setattr("hermes_cli.profiles_remove.wait_profile_unowned", lambda home: True)
- return cli, snapshot, old, fresh, closed
-
-
[email protected]("result", [False, "raise"])
-def test_failed_restore_still_rejoins_the_viewer(viewer, monkeypatch, result):
- cli, snapshot, old, fresh, closed = viewer
-
- def restore(sid):
- assert old.final and closed == [True]
- if result == "raise":
- raise OSError("fixture restore refused")
- return result
-
- monkeypatch.setattr("hermes_cli.backup.restore_quick_snapshot", restore)
- CLICommandsMixin._snapshot_restore(cli, ["/snapshot", "restore", snapshot])
- assert cli._session_db.connection is fresh
- assert fresh.calls == ["session.resume"]
- assert cli._session_db.current["subscription_id"] == "fresh-subscription"
- assert cli.agent is None
- assert cli._goal_manager is cli._loop_manager is cli._heartbeat_manager is None
+ monkeypatch.setattr("hermes_cli.backup.restore_quick_snapshot",
+ restore or (lambda sid: pytest.fail("restore ran without the identified owner's handoff")))
+ target = "../outside" if case == "invalid-id" else snapshot
+ CLICommandsMixin._snapshot_restore(cli, ["/snapshot", "restore", target])
[email protected]("boundary", ["refused", "still-owned"])
-def test_handoff_must_release_the_owner_before_restoring(viewer, monkeypatch, boundary):
- cli, snapshot, old, fresh, closed = viewer
- if boundary == "refused":
- monkeypatch.setattr("gateway.control_socket.pause_gateway_for_update", lambda *a, **kw: None)
+ out = capsys.readouterr().out
+ assert expected in out
+ if case in {"supervised", "invalid-id"}:
+ assert not paused and not old.final # refused before tearing down the viewer
else:
- monkeypatch.setattr("hermes_cli.profiles_remove.wait_profile_unowned", lambda home: False)
-
- def forbidden_restore(sid):
- raise AssertionError("restore must not run before ownership is released")
-
- monkeypatch.setattr("hermes_cli.backup.restore_quick_snapshot", forbidden_restore)
- CLICommandsMixin._snapshot_restore(cli, ["/snapshot", "restore", snapshot])
- assert old.final and closed == [True]
- assert cli._session_db.connection is fresh and fresh.calls == ["session.resume"]
-
-
-def test_resume_authorization_refusal_never_creates_another_session(viewer, monkeypatch):
- cli, snapshot, _old, fresh, _closed = viewer
- fresh.refusal = "permission_denied"
- monkeypatch.setattr("hermes_cli.backup.restore_quick_snapshot", lambda sid: True)
- CLICommandsMixin._snapshot_restore(cli, ["/snapshot", "restore", snapshot])
- assert fresh.calls == ["session.resume"]
- assert cli.session_id == "kept" and cli._session_db.connection is fresh
-
-
[email protected]("target", ["missing", "../outside", "0", "99999"])
-def test_invalid_target_does_not_disconnect_the_gateway(viewer, target):
- cli, _snapshot, old, fresh, closed = viewer
- CLICommandsMixin._snapshot_restore(cli, ["/snapshot", "restore", target])
- assert not old.final and closed == [] and fresh.calls == []
+ assert "Reconnecting to the gateway failed: permission_denied" in out
diff --git a/tests/hermes_cli/test_gateway_snapshot_restore_live.py b/tests/hermes_cli/test_gateway_snapshot_restore_live.py
index 2f4ba0ef226..2bbde1a5c1b 100644
--- a/tests/hermes_cli/test_gateway_snapshot_restore_live.py
+++ b/tests/hermes_cli/test_gateway_snapshot_restore_live.py
@@ -120,8 +120,8 @@ def _default_profile_state(home):
@pytest.mark.platforms("posix") # The fixture exercises the POSIX default/named profile layout.
[email protected]("multiplex", [False, True], ids=["standalone", "satellite"])
[email protected]("retained", [True, False], ids=["resume-restored", "create-missing"])
[email protected]("multiplex, retained", [(False, True), (True, False)],
+ ids=["standalone-resume-restored", "satellite-create-missing"])
def test_snapshot_restore_rejoins_the_restored_authority(local_gateway, multiplex, retained, capsys):
home = local_gateway(multiplex)
connection = GatewayConnection.shared()
@@ -152,10 +152,14 @@ def test_snapshot_restore_rejoins_the_restored_authority(local_gateway, multiple
CLICommandsMixin._snapshot_restore(cli, ["/snapshot", "restore", snapshot])
- assert connection.final, capsys.readouterr().out
+ out = capsys.readouterr().out
+ assert connection.final, out
+ assert "Restored state from" in out and "failed" not in out and "not served again" not in out, out
assert store.connection is not connection and not store.connection.final
assert old_reader._wrapped._conn is None and shared_reader._conn is None
assert cli.agent is None
+ from hermes_cli import gateway_tools
+ assert gateway_tools._view.connection is store.connection # /agents, /stop and the status bar
assert (cli.session_id == sid) is retained
history = json.dumps(cli.conversation_history)
assert "AFTER_SNAPSHOT" not in history