The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): stop a host terminal reader only where the 64-frame bound did, so one large paste keeps pongs flowing
unsupportedpastels252dbe786db5
diff --git a/hermes_cli/web_host_terminal_sessions.py b/hermes_cli/web_host_terminal_sessions.pyindex d9ad6a6ab36..a50869ea58e 100644--- a/hermes_cli/web_host_terminal_sessions.py+++ b/hermes_cli/web_host_terminal_sessions.py@@ -30,9 +30,11 @@ from hermes_constants import assert_named_profile_home_live, get_hermes_home# No survival across backend restart. Disconnects retain a bounded ANSI tail.RETENTION_SECONDS = 15 * 60_MAX_SHELLS = 16-# Inbound input buffered while one batch is written (backpressure bound), in bytes; each frame also-# costs ``_FRAME_OVERHEAD_BYTES``. Bytes, not frames: until it is reached the reader keeps reading the-# socket, which is where the server's keepalive pongs arrive (``_InputQueue``).+# Inbound input buffered while one batch is written (backpressure bound): full only once it holds+# both ``_INPUT_QUEUE_FRAMES`` frames and ``_INPUT_QUEUE_BYTES`` bytes, each frame also costing+# ``_FRAME_OVERHEAD_BYTES``. Until then the reader keeps reading the socket, which is where the+# server's keepalive pongs arrive (``_InputQueue``).+_INPUT_QUEUE_FRAMES = 64_INPUT_QUEUE_BYTES = 1024 * 1024_FRAME_OVERHEAD_BYTES = 256# How often a reader held up by that bound checks that the tab is still there (``_queue_frame``).@@ -320,13 +322,15 @@ def _frame_cost(item) -> int:class _InputQueue(asyncio.Queue):- """The frames a tab sent while a batch is written, full at ``_INPUT_QUEUE_BYTES``.+ """The frames a tab sent while a batch is written, full at ``_INPUT_QUEUE_FRAMES`` frames that+ together reach ``_INPUT_QUEUE_BYTES``.A full queue stops the reader, and with it the WebSocket library's own read of the socket once- that buffers its few frames: keepalive pongs then go unread, and a bind that pings (any+ that buffers 32 unread frames: keepalive pongs then go unread, and a bind that pings (anynon-loopback Webapp) closes a live tab with 1011 after the ping timeout, dropping the input- queued behind it. A frame count would fill on a few dozen keys typed while a paste waits on a- program that is not reading; a byte bound leaves the reader reading through any typing.+ queued behind it. A frame count alone fills on a few dozen keys typed while a paste waits on a+ program that is not reading, and a byte count alone on one large paste; needing both leaves the+ reader reading through any typing, and never stops it before the frame count alone would."""def __init__(self):@@ -334,7 +338,7 @@ class _InputQueue(asyncio.Queue):self.queued = 0def full(self) -> bool:- return self.queued >= _INPUT_QUEUE_BYTES+ return self.qsize() >= _INPUT_QUEUE_FRAMES and self.queued >= _INPUT_QUEUE_BYTESdef _put(self, item):self.queued += _frame_cost(item)diff --git a/tests/hermes_cli/test_host_terminal_viewer_ownership.py b/tests/hermes_cli/test_host_terminal_viewer_ownership.pyindex 2cf216ca1cf..ef9cc7b3c4a 100644--- a/tests/hermes_cli/test_host_terminal_viewer_ownership.py+++ b/tests/hermes_cli/test_host_terminal_viewer_ownership.py@@ -374,13 +374,15 @@ def unread_posix_pty(_monkeypatch):def keys_filling_the_input_queue(extra=0, *, text=False):"""``(frames, filled)``: keystroke frames whose first ``filled`` fill the host terminal's input- queue, then ``extra`` more. Each frame repeats one letter; the next frame has the next letter.- With ``text``, the frames are text frames, as a browser sends them, of 4-byte emoji: each has a- quarter as many characters as UTF-8 bytes."""+ queue, its frame bound of frames that together reach its byte bound, then ``extra`` more. Each+ frame repeats one letter; the next frame has the next letter. With ``text``, the frames are text+ frames, as a browser sends them, of 4-byte emoji: each has a quarter as many characters as UTF-8+ bytes."""from hermes_cli import web_host_terminal_sessions as host_sessions- size = host_sessions._INPUT_QUEUE_BYTES // 16- filled = -(-host_sessions._INPUT_QUEUE_BYTES // (size + host_sessions._FRAME_OVERHEAD_BYTES))+ filled = host_sessions._INPUT_QUEUE_FRAMES+ size = host_sessions._INPUT_QUEUE_BYTES // filled+ assert filled * (size + host_sessions._FRAME_OVERHEAD_BYTES) >= host_sessions._INPUT_QUEUE_BYTESif text:return [chr(0x1F600 + n % 26) * (size // 4) for n in range(filled + extra)], filledreturn [bytes([65 + n % 26]) * size for n in range(filled + extra)], filled@@ -411,6 +413,23 @@ def test_text_frames_count_their_utf8_bytes_against_the_input_queue():assert host_sessions._frame_cost({"text": "\ud800"}) == 3 + host_sessions._FRAME_OVERHEAD_BYTES+def test_a_paste_or_keys_alone_do_not_fill_the_input_queue():+ """The reader stops taking frames only once the input queue holds both its byte bound and 64+ frames. One paste over the byte bound, or many keys, leave it reading the socket, where the+ server's keepalive pongs arrive."""+ from hermes_cli import web_host_terminal_sessions as host_sessions++ inbox = host_sessions._InputQueue()+ for frame in [b"P" * 2 * host_sessions._INPUT_QUEUE_BYTES, *[b"k"] * 48]:+ inbox.put_nowait({"type": "websocket.receive", "bytes": frame})+ assert not inbox.full(), "a paste over the byte bound filled the input queue"+ while not inbox.empty():+ inbox.get_nowait()+ for _ in range(1024):+ inbox.put_nowait({"type": "websocket.receive", "bytes": b"k"})+ assert not inbox.full(), "keys filled the input queue by count alone"++@pytest.mark.asyncio@pytest.mark.parametrize("fill_inbox", [False, True], ids=["quiet", "full-inbox"])@pytest.mark.parametrize("terminal", [@@ -544,6 +563,18 @@ class AnsweringTab(BrowserTab):with self._lock:super().send(*frames)+ def _flush(self):+ # The socket's short timeout is for the answering thread's reads; a paste can take longer+ # than that to send.+ data = memoryview(b"".join(self.protocol.data_to_send()))+ deadline = time.monotonic() + 10+ while data:+ try:+ data = data[self.sock.send(data):]+ except TimeoutError:+ if time.monotonic() > deadline:+ raise+@propertydef closed_by_server(self):"""Whether the server's close frame has arrived, as a browser sees it before the route returns."""@@ -653,11 +684,19 @@ def test_a_tab_closed_behind_more_keys_than_the_input_queue_holds_is_detached(tmassert not session.attached and session._ws is None-def test_a_live_tab_typing_behind_a_waiting_write_answers_keepalive_pings(tmp_path, monkeypatch):[email protected]("typed", [+ # Far more frames than the WebSocket library buffers unread (32) and than 64 queued frames.+ [bytes([65 + n % 26]) for n in range(1024)],+ # A paste over the input queue's byte bound (one text frame from the browser), then more keys+ # than the WebSocket library buffers unread, but fewer frames than 64.+ [b"P" * 2 * 1024 * 1024, *(bytes([97 + n % 26]) for n in range(48))],+], ids=["keys", "paste-then-keys"])+def test_a_live_tab_typing_behind_a_waiting_write_answers_keepalive_pings(tmp_path, monkeypatch, typed):"""A non-loopback Webapp pings its sockets (``web_server._build_uvicorn_server``) and closes one- whose pong does not come back in time. Keys typed while a write waits on a slow program must not- stop the server reading the socket, where the pongs arrive: the tab, answering every ping, stays- attached through several ping timeouts, and every key reaches the program once it reads."""+ whose pong does not come back in time. Keys or a paste typed while a write waits on a slow+ program must not stop the server reading the socket, where the pongs arrive: the tab, answering+ every ping, stays attached through several ping timeouts, and everything reaches the program in+ order once it reads."""home = tmp_path / ".hermes"profile = home / "profiles" / "worker"profile.mkdir(parents=True)@@ -666,8 +705,6 @@ def test_a_live_tab_typing_behind_a_waiting_write_answers_keepalive_pings(tmp_paowner = HostOwner(("loopback", "test"), profile, "sh", str(tmp_path))bridge = SlowAccept()session = PtySession("terminal", bridge, buffer_cap=32, read_timeout=0.01)- # Far more frames than the WebSocket library buffers unread (32) and than 64 queued frames.- typed = [bytes([65 + n % 26]) for n in range(1024)]with served_host_terminal(monkeypatch, session, owner, ws_ping=(1.0, 2.0)) as (port, taken, finished):tab = AnsweringTab(port)try: