The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
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.pyindex 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 comefrom __future__ import annotationsimport json+import loggingimport timefrom prompt_toolkit.utils import get_cwidth@@ -12,6 +13,8 @@ from agent.i18n import tfrom hermes_cli import cli_process_dock as procsfrom 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:returnself._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.pyindex 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):passfinally:- 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] = futureself.events.moved.set() # a reader waiting on the consumer reads on toward this answer+ from websockets.exceptions import ConnectionClosedtry:- 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 excbudget = DEFAULT_RPC_TIMEOUT if _timeout is None else _timeoutreturn 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.pyindex 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 jsonimport pytest-from hermes_cli.gateway_client import GatewayClient, GatewayRPCError+from hermes_cli.gateway_client import GatewayClient, GatewayClientError, GatewayRPCErrorclass _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.pyindex 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(gassert view.subagent_tail("sa-0-1", fetch=True) is Noneassert view.subagent_tail("sa-0-1", fetch=True) is None # the failure is cached for the poll intervalassert 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]