Skip to content

The agent that grows with you

refactor(cli): one builder for a turn the owner never saw; pin the unretrieved-future fix

kshitijk4poor679ec082c8ae
diff --git a/hermes_cli/gateway_agent.py b/hermes_cli/gateway_agent.py
index a87b3b72838..59155892292 100644
--- a/hermes_cli/gateway_agent.py
+++ b/hermes_cli/gateway_agent.py
@@ -773,10 +773,7 @@ class GatewayAgent:
# Building the params consumes a ``/model --once`` pick (``_take_turn_route``).
notice = self._prepare_submit(turn)
if notice:
- # Refused before prompt.submit: nothing was admitted, so the CLI keeps the turn's
- # local state (no history row, the /model --once pick and notes) for the resend.
- return {**self._failed(history, user_message, notice, failure_reason="gateway_connection"),
- "messages": history, "admitted": False}
+ return self._unadmitted(history, user_message, notice)
params = self._submit_params(user_message, persist_user_message, turn_author)
if self._stop(turn)():
raise CallStopped("prompt.submit")
@@ -795,11 +792,7 @@ class GatewayAgent:
failure_reason="session_busy" if busy else None, retryable=busy)
except GatewayClientError as exc:
if str(exc) == NOT_SENT:
- # Never reached the owner: like a notice, the CLI keeps the turn's local state (no
- # history row, the /model --once pick and notes) so the resend is the same turn.
- self._give_back_turn_route()
- return {**self._failed(history, user_message, str(exc), failure_reason="gateway_connection"),
- "messages": history, "admitted": False}
+ return self._unadmitted(history, user_message, str(exc))
return self._failed(history, user_message, str(exc), failure_reason="gateway_connection")
if (receipt or {}).get("voice_stopped") is True:
return self._voice_stopped(history)
@@ -1280,6 +1273,13 @@ class GatewayAgent:
self._emit_print(t("cli.voice.stop_phrase_typed"))
return {**_RESULT_DEFAULTS, "messages": list(history), "voice_stopped": True, "session_id": self.session_id}
+ def _unadmitted(self, history, user_message, error):
+ """A turn the owner never saw (a pre-submit notice, or a submit that was never sent): the CLI
+ keeps its local state (no history row, the ``/model --once`` pick and notes) for the resend."""
+ self._give_back_turn_route()
+ return {**self._failed(history, user_message, error, failure_reason="gateway_connection"),
+ "messages": history, "admitted": False}
+
def _failed(self, history, user_message, error, *, failure_reason=None, retryable=False):
"""``failure_reason="gateway_connection"``: the CLI shows ``error`` as written instead of
classifying a lost gateway as a model failure ("Run /model to switch...")."""
diff --git a/hermes_cli/gateway_client.py b/hermes_cli/gateway_client.py
index e7bf8eade2e..c5740533333 100644
--- a/hermes_cli/gateway_client.py
+++ b/hermes_cli/gateway_client.py
@@ -239,7 +239,7 @@ class GatewayClient:
finally:
self.pending.pop(rid, None)
if future.done() and not future.cancelled():
- future.exception() # failed by the reader while the send raised: already reported above
+ future.exception() # retrieved, so a failure nobody awaited is never logged as unretrieved
def private_control_query(home: Path, request: bytes, timeout: float = 5) -> bytes:
diff --git a/tests/hermes_cli/test_gateway_client_rpc.py b/tests/hermes_cli/test_gateway_client_rpc.py
index cd9b0598480..e45da0bf887 100644
--- a/tests/hermes_cli/test_gateway_client_rpc.py
+++ b/tests/hermes_cli/test_gateway_client_rpc.py
@@ -139,3 +139,40 @@ async def test_a_request_after_the_reader_ended_is_not_sent():
await client.rpc('prompt.submit', session_id='s')
assert str(refused.value) == NOT_SENT
assert sent == [] and client.pending == {}
+
+
+async def test_a_send_that_fails_after_the_reader_failed_its_request_logs_nothing():
+ """websockets' send waits for the connection to be lost before it raises, so the reader fails the
+ pending request first; rpc raises its own error and must retrieve that one, or asyncio logs
+ "Future exception was never retrieved"."""
+ import gc
+
+ class _DiesMidSend(_Socket):
+ def __init__(self, reply):
+ super().__init__(reply)
+ self.closed = asyncio.Event()
+
+ async def send(self, raw):
+ self.closed.set()
+ await asyncio.wait({self.client.reader}, timeout=1) # the reader fails the request first
+ raise ConnectionClosedError(None, None)
+
+ async def __anext__(self):
+ await self.closed.wait()
+ raise ConnectionClosedError(None, None)
+
+ loop = asyncio.get_running_loop()
+ logged = []
+ loop.set_exception_handler(lambda _loop, context: logged.append(context.get('message')))
+ try:
+ socket = _DiesMidSend(lambda req: None)
+ async with GatewayClient(socket) as client:
+ socket.client = client
+ with pytest.raises(GatewayClientError):
+ await client.rpc('subagent.list', session_id='s')
+ gc.collect()
+ await asyncio.sleep(0)
+ finally:
+ loop.set_exception_handler(None)
+ assert not [m for m in logged if 'never retrieved' in str(m)]