Skip to content

The agent that grows with you

fix(cli): `chat -q --resume <legacy id>` hands its lease over while the gateway imports the row

Teknium0b289663aa21
diff --git a/hermes_cli/cli_init_mixin.py b/hermes_cli/cli_init_mixin.py
index 3eeaec3bbc5..dbc918c774b 100644
--- a/hermes_cli/cli_init_mixin.py
+++ b/hermes_cli/cli_init_mixin.py
@@ -381,11 +381,20 @@ class CLIInitMixin:
"ignore_rules": getattr(self, "ignore_rules", False) or None,
"run_budget": getattr(self, "_explicit_run_budget", None),
"verbose": getattr(self, "verbose", False) or None}
+ # A resumed row this gateway has not imported yet (a legacy / in-process session) is imported
+ # under the active-session registry lock, which refuses while ANY live lease holds the id —
+ # including this process's own, claimed before the first turn. Hand the slot over around the
+ # attach, then re-claim it for the id the gateway returned (main's exclusivity is kept).
+ held = getattr(self, "_resumed", False) and getattr(self, "_active_session_lease", None) is not None
+ if held:
+ self._release_active_session()
sid = store.open_session(self, launch)
self._gateway_session_open = True
if sid and sid != self.session_id:
self.session_id = sid
getattr(self, "_write_terminal_breadcrumb", lambda: None)()
+ if held and not self._claim_active_session("cli", stderr=True):
+ raise RuntimeError("session_already_owned")
def _new_session_id(self, now, *, branch_of=None, title=None):
"""The id main mints for /new (``new_session_id``) and /branch (``mint_session_id``). On a gateway
diff --git a/tests/hermes_cli/test_gateway_session_store.py b/tests/hermes_cli/test_gateway_session_store.py
index 5f308ef2528..7c8fd8e7b9c 100644
--- a/tests/hermes_cli/test_gateway_session_store.py
+++ b/tests/hermes_cli/test_gateway_session_store.py
@@ -459,3 +459,27 @@ def test_explicit_reasoning_max_turns_and_checkpoints_reach_session_create(store
# An unparseable --reasoning keeps main's warn-and-ignore (never an invalid_params refusal).
CLIInitMixin._ensure_gateway_session(host(reasoning="bogus"))
assert "reasoning" not in client.calls[-1][1]
+
+
+def test_resume_hands_the_cli_lease_over_while_the_gateway_imports_the_row(store):
+ """The gateway imports a resumed legacy row under the active-session registry lock and refuses
+ (runtime_coordination_required) while any live lease holds that id. The -q/-Q path claims the
+ CLI's lease before the first turn, so the attach must run without it and re-claim afterwards."""
+ from types import SimpleNamespace
+
+ from hermes_cli.cli_init_mixin import CLIInitMixin
+ db, client = store
+ events = []
+ h = SimpleNamespace(model="m1", requested_provider="auto", enabled_toolsets=["terminal"],
+ session_id="local-old", _resumed=True, _session_db=GatewaySessionStore(db.connection),
+ _active_session_lease=object())
+ h._release_active_session = lambda: (events.append("release"), setattr(h, "_active_session_lease", None))
+ h._claim_active_session = lambda *a, **k: (events.append(("claim", h.session_id)), True)[1]
+ base = client.rpc
+
+ async def rpc(method, **params):
+ events.append((method, h._active_session_lease is None))
+ return await base(method, **params)
+ client.rpc = rpc
+ CLIInitMixin._ensure_gateway_session(h)
+ assert events == ["release", ("session.resume", True), ("claim", "local-old")], events