Skip to content

The agent that grows with you

fix(cli): a send on a dead gateway socket no longer kills the spinner thread

Turgut Kural7e7d9a043db0
diff --git a/hermes_cli/cli_subagent_monitor.py b/hermes_cli/cli_subagent_monitor.py
index d5f2c45fede..220dcfc3f06 100644
--- a/hermes_cli/cli_subagent_monitor.py
+++ b/hermes_cli/cli_subagent_monitor.py
@@ -4,6 +4,7 @@ prompts) and scoped controls; no agent-loop state is changed. Process rows come
from __future__ import annotations
import json
+import logging
import time
from prompt_toolkit.utils import get_cwidth
@@ -12,6 +13,8 @@ from agent.i18n import t
from hermes_cli import cli_process_dock as procs
from hermes_cli import cli_session_dock as session_rows
+logger = logging.getLogger(__name__)
+
def _clip(value, width):
text = ' '.join(str(value or '').split())
@@ -124,8 +127,13 @@ class SubagentMonitor:
if now - self._last_poll < 1:
return
self._last_poll = now
- changed = self.refresh()
- if self._poll_tail() or changed:
+ try:
+ changed = self.refresh()
+ changed = self._poll_tail() or changed
+ except Exception: # health: allow BLE001 -- the spinner thread runs this tick; one raise would end that thread for the session
+ logger.debug("subagent monitor tick failed", exc_info=True)
+ return
+ if changed:
if self.app is not None:
self.invalidate()
else:
diff --git a/hermes_cli/gateway_client.py b/hermes_cli/gateway_client.py
index f7897479870..6b462673392 100644
--- a/hermes_cli/gateway_client.py
+++ b/hermes_cli/gateway_client.py
@@ -49,6 +49,10 @@ class GatewayClientError(ValueError):
pass
+_DISCONNECTED = ("Gateway disconnected; turn outcome is unknown. "
+ "Resume the printed session ID to check whether it completed or was interrupted; do not resend the work.")
+
+
class GatewayRPCError(GatewayClientError):
"""A received owner refusal, distinct from an ambiguous transport failure.
@@ -196,8 +200,7 @@ class GatewayClient:
except (ConnectionClosed, OSError, ValueError, TimeoutError):
pass
finally:
- error = GatewayClientError("Gateway disconnected; turn outcome is unknown. "
- "Resume the printed session ID to check whether it completed or was interrupted; do not resend the work.")
+ error = GatewayClientError(_DISCONNECTED)
for future in self.pending.values():
if not future.done():
future.set_exception(error)
@@ -216,8 +219,13 @@ class GatewayClient:
future = asyncio.get_running_loop().create_future()
self.pending[rid] = future
self.events.moved.set() # a reader waiting on the consumer reads on toward this answer
+ from websockets.exceptions import ConnectionClosed
try:
- await self.websocket.send(json.dumps({"jsonrpc": "2.0", "id": rid, "method": method, "params": params}))
+ try:
+ await self.websocket.send(json.dumps({"jsonrpc": "2.0", "id": rid, "method": method, "params": params}))
+ except (ConnectionClosed, OSError) as exc:
+ # The frame may have left before the socket died: the same unknown outcome as a lost answer.
+ raise GatewayClientError(_DISCONNECTED) from exc
budget = DEFAULT_RPC_TIMEOUT if _timeout is None else _timeout
return await asyncio.wait_for(future, None if budget == float("inf") else budget)
finally:
diff --git a/tests/hermes_cli/test_gateway_client_rpc.py b/tests/hermes_cli/test_gateway_client_rpc.py
index 1ed2dfa3da0..83e50e80367 100644
--- a/tests/hermes_cli/test_gateway_client_rpc.py
+++ b/tests/hermes_cli/test_gateway_client_rpc.py
@@ -4,7 +4,7 @@ import json
import pytest
-from hermes_cli.gateway_client import GatewayClient, GatewayRPCError
+from hermes_cli.gateway_client import GatewayClient, GatewayClientError, GatewayRPCError
class _Socket:
@@ -73,3 +73,19 @@ async def test_compress_waits_past_the_default_rpc_budget(monkeypatch):
assert result == {'status': 'applied'}
# The budget is a client-side knob, never a wire parameter the closed contract would refuse.
assert all('_timeout' not in req['params'] for req in sent)
+
+
+async def test_send_on_a_dead_socket_is_a_client_error_not_a_raw_websockets_one():
+ """A keepalive timeout closes the socket under a poller (the dock's ``subagent.list``): the send
+ must surface as the transport's own error, which every caller catches, never as websockets'."""
+ from websockets.exceptions import ConnectionClosedError
+
+ class _Dead(_Socket):
+ async def send(self, raw):
+ raise ConnectionClosedError(None, None)
+
+ async with GatewayClient(_Dead(lambda req: None)) as client:
+ with pytest.raises(GatewayClientError, match='turn outcome is unknown'):
+ await client.rpc('subagent.list', session_id='s')
+ assert client.pending == {}
diff --git a/tests/hermes_cli/test_subagent_monitor_gateway.py b/tests/hermes_cli/test_subagent_monitor_gateway.py
index b54397bcd68..e3d56c46ec9 100644
--- a/tests/hermes_cli/test_subagent_monitor_gateway.py
+++ b/tests/hermes_cli/test_subagent_monitor_gateway.py
@@ -172,3 +172,25 @@ def test_detail_view_paints_the_cached_tail_and_only_the_tick_asks_the_gateway(g
assert view.subagent_tail("sa-0-1", fetch=True) is None
assert view.subagent_tail("sa-0-1", fetch=True) is None # the failure is cached for the poll interval
assert tails() == 2
+
+
+def test_a_failing_tick_keeps_the_spinner_thread_alive(gateway, monkeypatch):
+ """The spinner thread runs ``tick`` in a bare loop: one raise would end it, and with it the
+ spinner and the dock, for the rest of the session. A failed poll skips a beat instead."""
+ from hermes_cli.cli_subagent_monitor import SubagentMonitor
+ agent, _client = gateway
+ repaints = []
+ monitor = SubagentMonitor(SimpleNamespace(agent=agent, _agent_running=True,
+ _invalidate=lambda: repaints.append(1)))
+ view = gateway_tools.process_view()
+ real_rows = view.subagent_rows
+
+ def dead(**_kw):
+ raise OSError("socket gone")
+ monkeypatch.setattr(view, "subagent_rows", dead)
+ monitor.tick() # must not raise
+ assert repaints == []
+ monkeypatch.setattr(view, "subagent_rows", real_rows)
+ monitor._last_poll = 0
+ monitor.tick()
+ assert [r["subagent_id"] for r in monitor.entries] == ["sa-0-1"] and repaints == [1]