Files
hermes-agent/tests/agent/test_worker_rpc_proxy.py
unsupportedpastels 13a8c2cc97 fix(worker): read the owner connection continuously and drop the library keepalive
The retained owner WebSocket was only read during a call. The owner
registers every worker connection as a live transport and broadcasts
sessions.changed to it, so a worker busy in a model call or tool piled up
more than websockets' 16-frame receive high-water mark, the library paused
socket reads, and pings and the close handshake stalled with them. The
next persistence call was then sent into a connection the owner was
already closing and failed with an unknown outcome, which the store
latches. Separately, the default 20 s client keepalive ran its own pong
deadline, so a short owner stall could close the connection inside a
call's 20 s receipt budget.

A reader thread now owns the socket for its lifetime: it discards pushed
events as they arrive and hands the reply for the one outstanding request
id (or the close exception) to the waiting caller. ping_interval=None
leaves the per-call receipt budget and the observed close as the only
liveness checks. Buffering stays bounded (library queue, at most one reply
plus a close in the hand-off queue); calls stay serialized under the lock
with per-request ids; an idle close observed before a send still redials
with a fresh ticket; any error after a send drops the connection and
propagates without a resend. Server checks still run per request.

Alternatives not taken: stopping broadcasts to worker connections would
need a new transport rule in the owner (it does not tell worker
connections apart today) and still leaves the client keepalive; an idle
timeout would add a redial per quiet stretch and still pause reads inside
the window.

Refs #134889 (KB-3)
Refs #139162
Refs #139011
2026-10-15 23:59:19 -04:00

40 lines
1.9 KiB
Python

"""Private loopback worker persistence does not use ambient HTTP proxies."""
import json
import queue
from types import SimpleNamespace
from websockets.exceptions import ConnectionClosed
def test_worker_persistence_explicitly_bypasses_proxy(tmp_path, monkeypatch):
from agent.runtime_session_store import WorkerRPC
monkeypatch.setenv('HTTPS_PROXY', 'http://127.0.0.1:1')
monkeypatch.delenv('NO_PROXY', raising=False)
endpoint = SimpleNamespace(api_origin='http://127.0.0.1:1234')
discoveries = iter([SimpleNamespace(state='ready', endpoint=endpoint),
SimpleNamespace(state='draining', endpoint=None)])
monkeypatch.setattr('hermes_cli.gateway_runtime.discover_gateway_endpoint',
lambda *a, **k: next(discoveries))
monkeypatch.setattr('hermes_cli.gateway_runtime.control_home_for', lambda home, endpoint: home)
monkeypatch.setattr('hermes_cli.gateway_runtime_discovery.query_identify', lambda *a, **k: {'pid': -1})
monkeypatch.setattr('hermes_cli.gateway_client._session_ticket', lambda *a, **k: 'ticket')
calls = []
def connect(url, **kwargs):
calls.append(kwargs)
frames = queue.SimpleQueue()
def recv():
frame = frames.get()
if frame is None:
raise ConnectionClosed(None, None)
return frame
def send(value):
frames.put(json.dumps({'id': json.loads(value)['id'], 'result': {'ok': True}}))
frames.put(None) # then the owner drops the idle connection: the next call redials (draining)
return SimpleNamespace(send=send, recv=recv, close=lambda: frames.put(None))
monkeypatch.setattr('websockets.sync.client.connect', connect)
rpc = WorkerRPC(tmp_path)
assert rpc('worker.persist') == {'ok': True}
rpc.link.reader.join(5)
assert rpc('worker.persist') == {'ok': True}
assert len(calls) == 2 and all(call['proxy'] is None for call in calls)