Skip to content

The agent that grows with you

fix(cli): re-supply the launch key when the classic CLI resumes a session

David Dudok de Wit7209c7accf5e
diff --git a/hermes_cli/gateway_connection.py b/hermes_cli/gateway_connection.py
index 0d2784af698..99fa6693d32 100644
--- a/hermes_cli/gateway_connection.py
+++ b/hermes_cli/gateway_connection.py
@@ -16,7 +16,7 @@ import queue
import threading
import time
-from hermes_cli.gateway_client import DEFAULT_RPC_TIMEOUT, GatewayClientError
+from hermes_cli.gateway_client import DEFAULT_RPC_TIMEOUT, GatewayClientError, GatewayRPCError
logger = logging.getLogger(__name__)
@@ -137,6 +137,9 @@ class GatewayConnection:
self._side_lock = threading.Lock()
# The wake events of :meth:`call_until` commands in flight, each on its own socket.
self._commands: set[threading.Event] = set()
+ # This CLI's launch-only key (``--api-key`` or a keyed startup alias), set by the session store:
+ # an owner restart revokes it, so every resume re-supplies it (the owner's ``rebind_launch_key``).
+ self.launch_api_key = None
@classmethod
def shared(cls):
@@ -248,13 +251,24 @@ class GatewayConnection:
if state.subscription_id is None:
continue
try:
- state.adopt_snapshot(await client.rpc("session.resume", session_id=session_id))
+ state.adopt_snapshot(await self._resume(client, session_id))
except GatewayClientError as exc:
if str(exc) not in _GONE:
raise
logger.debug("session.resume %s after reconnect: %s", session_id, exc)
state.subscription_id = None
+ async def _resume(self, client, session_id):
+ """``session.resume`` carrying :attr:`launch_api_key`. An owner refusal of the keyed resume (a session
+ launched without that key: a keyed startup alias on a Desktop/TUI session) resumes it as is."""
+ key = self.launch_api_key
+ try:
+ return await client.rpc("session.resume", session_id=session_id, **({"api_key": key} if key else {}))
+ except GatewayRPCError:
+ if not key:
+ raise
+ return await client.rpc("session.resume", session_id=session_id)
+
async def _pump(self, client):
while True:
frame = await client.events.get()
diff --git a/hermes_cli/gateway_session_store.py b/hermes_cli/gateway_session_store.py
index 916e584f8de..86da19a0f15 100644
--- a/hermes_cli/gateway_session_store.py
+++ b/hermes_cli/gateway_session_store.py
@@ -22,7 +22,7 @@ import re
import sys
import uuid
-from hermes_cli.gateway_client import GatewayClientError
+from hermes_cli.gateway_client import GatewayClientError, GatewayRPCError
from hermes_cli.gateway_connection import GatewayConnection
logger = logging.getLogger(__name__)
@@ -122,16 +122,25 @@ class GatewaySessionStore:
return sid
def attach(self, session_id):
- """``session.resume`` once per session id this viewer uses (idempotent)."""
+ """``session.resume`` once per session id this viewer uses (idempotent), carrying this CLI's launch
+ key as the thin client does. An owner refusal of the keyed resume (a session launched without that
+ key: a keyed startup alias resuming a Desktop/TUI session) attaches as is; its frozen route decides."""
if session_id in self.snapshots:
return self.snapshots[session_id]
- snapshot = self.call("session.resume", session_id=session_id)
+ key = self.connection.launch_api_key
+ try:
+ snapshot = self.call("session.resume", session_id=session_id, **({"api_key": key} if key else {}))
+ except GatewayRPCError:
+ if not key:
+ raise
+ snapshot = self.call("session.resume", session_id=session_id)
self._adopt(snapshot)
return snapshot
def open_session(self, cli, launch=None):
"""Create (or resume) the gateway session for this CLI and adopt its id. A resume target the
gateway does not know is left as is: main's own "Session not found" path reports it."""
+ self.connection.launch_api_key = (launch or {}).get("api_key") or None
if getattr(cli, "_resumed", False):
try:
snapshot = self.attach(cli.session_id)
diff --git a/tests/hermes_cli/test_gateway_resume_launch_key.py b/tests/hermes_cli/test_gateway_resume_launch_key.py
new file mode 100644
index 00000000000..5ee14a6a9af
--- /dev/null
+++ b/tests/hermes_cli/test_gateway_resume_launch_key.py
@@ -0,0 +1,129 @@
+"""The classic CLI re-supplies its launch-only key on every resume (refs #134889, RV-14).
+
+An owner restart revokes launch keys and takes the same key back on ``session.resume {api_key}``
+(``gateway/session_policy.py::rebind_launch_key``; the thin client sends it). The classic CLI resumed with
+the session id only, at startup (``--resume`` / ``-c``), on ``/resume`` and on a reconnect, so the first
+turn after any gateway restart failed with ``launch_credentials_unavailable``.
+
+Real owners (``initialize_session_authority`` again on the same DB is the restart), real
+``AuthorityConnection.dispatch``, real ``GatewayConnection`` and ``GatewaySessionStore``: each dial of the
+CLI's connection reaches the current owner through a bridge client."""
+import asyncio
+from contextlib import asynccontextmanager
+from types import SimpleNamespace
+
+import pytest
+
+from hermes_cli.gateway_client import GatewayClientError, rpc_error
+from hermes_cli.gateway_connection import GatewayConnection
+from hermes_cli.gateway_session_store import GatewaySessionStore
+
+RAW = 'UNIQUE-LAUNCH-ONLY-KEY'
+LAUNCH = {'model': 'm', 'provider': None, 'api_key': RAW, 'base_url': 'http://127.0.0.1:9/v1'}
+
+
+class _Life:
+ """One socket to one owner: requests run on the owner's loop (the test's)."""
+
+ def __init__(self, connection, loop):
+ self.connection, self.loop = connection, loop
+ self.events = asyncio.Queue()
+
+ async def rpc(self, method, *, _timeout=None, **params):
+ frame = {'id': 1, 'method': method, 'params': params}
+ reply = await asyncio.wrap_future(asyncio.run_coroutine_threadsafe(self.connection.dispatch(frame), self.loop))
+ if 'error' in reply:
+ raise rpc_error(reply['error'])
+ return reply['result']
+
+
+class _Owners:
+ """The profile's gateway across a restart: the first owner made a keyed and a keyless CLI session."""
+
+ async def start(self, tmp_path, monkeypatch):
+ from gateway import run
+ from gateway.config import GatewayConfig
+ from gateway.session import SessionStore
+ from gateway.session_controls import AuthorityConnection
+ from gateway.session_local import create_local_session
+ monkeypatch.setattr(run, '_load_gateway_config', lambda *a, **k: {'platform_toolsets': {'cli': []}})
+ self.profile, self.loop, self.lives = str(tmp_path), asyncio.get_running_loop(), []
+ self.store = SessionStore(tmp_path / 'sessions', GatewayConfig())
+ first = await self.boot('first')
+ actor = AuthorityConnection(first, object(), {'user_id': 'owner'}).actor
+ base = {'cwd': self.profile, 'model': 'm', 'toolsets': []}
+ self.keyed = create_local_session(first, actor, {**base, 'request_id': 'keyed', 'api_key': RAW,
+ 'base_url': LAUNCH['base_url']}).session_id
+ self.plain = create_local_session(first, actor, {**base, 'request_id': 'plain'}).session_id
+ return self
+
+ async def boot(self, instance_id):
+ from gateway.session_authority import initialize_session_authority
+ owner = SimpleNamespace(session_store=self.store, _session_db=self.store._db, adapters={}, _draining=False,
+ _cached_agent_for=lambda route: None, _evict_cached_agent=lambda route: None)
+ owner._adapter_for_source = lambda source: owner.adapters.get(source.platform)
+ self.current = await initialize_session_authority(owner, profile_id=self.profile, instance_id=instance_id)
+ return self.current
+
+ async def _open(self):
+ from gateway.session_controls import AuthorityConnection
+ self.lives.append(_Life(AuthorityConnection(self.current, object(), {'user_id': 'owner'}), self.loop))
+ return self.lives[-1]
+
+ @asynccontextmanager
+ async def dial(self):
+ yield await asyncio.wrap_future(asyncio.run_coroutine_threadsafe(self._open(), self.loop))
+
+ async def hang_up(self, conn):
+ thread = conn._thread
+ conn.loop.call_soon_threadsafe(self.lives[-1].events.put_nowait, GatewayClientError('Gateway disconnected'))
+ await asyncio.to_thread(thread.join, 5)
+ await self.lives[-1].connection.close()
+
+ def key_of(self, session_id):
+ """What the next turn's agent build reads (``launch_key``); ``launch_credentials_unavailable`` on main."""
+ from gateway.session_policy import launch_key, policy_for_source
+ return launch_key(self.current, policy_for_source(self.current.runner, self.current.sessions[session_id].source))
+
+ async def close(self, conn):
+ await asyncio.to_thread(conn.close)
+ for life in self.lives:
+ await life.connection.close()
+ self.store._db.close()
+
+
+async def test_resume_after_a_restart_resupplies_the_launch_key(tmp_path, monkeypatch):
+ """``hermes --resume <id> --api-key K`` (or ``-c`` with a keyed alias) once the gateway restarted; then
+ ``/resume`` of a session launched without that key still attaches."""
+ owners = await _Owners().start(tmp_path, monkeypatch)
+ await owners.boot('restarted')
+ conn = GatewayConnection(connect=owners.dial)
+ try:
+ store = GatewaySessionStore(conn)
+ cli = SimpleNamespace(_resumed=True, session_id=owners.keyed)
+ assert await asyncio.to_thread(store.open_session, cli, LAUNCH) == owners.keyed
+ assert owners.key_of(owners.keyed) == RAW
+ assert (await asyncio.to_thread(store.attach, owners.plain))['stored_session_id'] == owners.plain
+ finally:
+ await owners.close(conn)
+
+
+async def test_a_reconnect_after_a_restart_resupplies_the_launch_key(tmp_path, monkeypatch):
+ """A classic CLI left open across ``hermes gateway restart``: its redial resubscribes with the key, and
+ a session launched without it is still resubscribed."""
+ owners = await _Owners().start(tmp_path, monkeypatch)
+ conn = GatewayConnection(connect=owners.dial)
+ try:
+ store = GatewaySessionStore(conn)
+ await asyncio.to_thread(store.open_session, SimpleNamespace(_resumed=True, session_id=owners.keyed), LAUNCH)
+ await asyncio.to_thread(store.attach, owners.plain)
+ before = conn.states[owners.plain].subscription_id
+ await owners.hang_up(conn)
+ await owners.boot('restarted')
+ await asyncio.to_thread(conn.start, 10) # the redial resubscribes before it counts as ready
+ assert owners.key_of(owners.keyed) == RAW
+ assert conn.states[owners.plain].subscription_id not in (None, before)
+ finally:
+ await owners.close(conn)