The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(webapp): count and write a host terminal frame from one payload rule
unsupportedpastels43a647062966
diff --git a/hermes_cli/web_host_terminal_sessions.py b/hermes_cli/web_host_terminal_sessions.pyindex a50869ea58e..39b4119a880 100644--- a/hermes_cli/web_host_terminal_sessions.py+++ b/hermes_cli/web_host_terminal_sessions.py@@ -309,16 +309,19 @@ async def _admit(ws: WebSocket, registry: HostTerminalRegistry):return session, owner+def _frame_payload(msg: dict) -> bytes:+ """What a frame writes to the program: a binary frame's bytes, a text frame's UTF-8. The+ WebSocket library decodes a text frame strictly, so its text always encodes back."""+ raw = msg.get("bytes")+ return (msg.get("text") or "").encode("utf-8") if raw is None else raw++def _frame_cost(item) -> int:"""What a queued item counts against ``_INPUT_QUEUE_BYTES``: its payload and the per-frame overhead."""if not isinstance(item, dict): # the reader's last item: None or the error that ended readingreturn _FRAME_OVERHEAD_BYTES- raw = item.get("bytes")- if raw is None:- # The UTF-8 ``_input_runs`` writes, not characters: a CJK or emoji paste is 3-4 bytes each.- # ``surrogatepass`` so counting a frame never fails the reader or the get that uncounts it.- raw = (item.get("text") or "").encode("utf-8", "surrogatepass")- return len(raw) + _FRAME_OVERHEAD_BYTES+ # The bytes ``_input_runs`` writes, not characters: a CJK or emoji paste is 3-4 bytes each.+ return len(_frame_payload(item)) + _FRAME_OVERHEAD_BYTESclass _InputQueue(asyncio.Queue):@@ -434,9 +437,7 @@ def _input_runs(frames: list[dict], resize_re):"""``frames`` in order: consecutive keystrokes joined into one run, resizes as ``(cols, rows)``."""run = b""for msg in frames:- raw = msg.get("bytes")- if raw is None:- raw = (msg.get("text") or "").encode("utf-8")+ raw = _frame_payload(msg)match = resize_re.fullmatch(raw) if raw else Noneif match is None:run += rawdiff --git a/tests/hermes_cli/test_host_terminal_viewer_ownership.py b/tests/hermes_cli/test_host_terminal_viewer_ownership.pyindex ef9cc7b3c4a..a30ee270957 100644--- a/tests/hermes_cli/test_host_terminal_viewer_ownership.py+++ b/tests/hermes_cli/test_host_terminal_viewer_ownership.py@@ -397,6 +397,7 @@ def test_text_frames_count_their_utf8_bytes_against_the_input_queue():"""A text frame is written as UTF-8, so it counts its UTF-8 bytes: a non-ASCII paste fills theinput queue at the same bound as the bytes it will write, not at a multiple of it."""from hermes_cli import web_host_terminal_sessions as host_sessions+ from hermes_cli.web_server_chat import _RESIZE_REkeys, _ = keys_filling_the_input_queue(text=True)inbox = host_sessions._InputQueue()@@ -408,9 +409,10 @@ def test_text_frames_count_their_utf8_bytes_against_the_input_queue():while not inbox.empty():inbox.get_nowait()assert inbox.queued == 0- # A lone surrogate never comes off a WebSocket (text frames are valid UTF-8), but counting one- # must not fail the reader.- assert host_sessions._frame_cost({"text": "\ud800"}) == 3 + host_sessions._FRAME_OVERHEAD_BYTES+ # A frame counts exactly what ``_input_runs`` writes for it.+ frame = {"type": "websocket.receive", "text": keys[0]}+ written, = host_sessions._input_runs([frame], _RESIZE_RE)+ assert host_sessions._frame_cost(frame) == len(written) + host_sessions._FRAME_OVERHEAD_BYTESdef test_a_paste_or_keys_alone_do_not_fill_the_input_queue():