The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(pty): shrink a reattach replay that timed out
emozillafdec7131e151
diff --git a/hermes_cli/pty_session.py b/hermes_cli/pty_session.pyindex b79754b57bc..7f88c4f2596 100644--- a/hermes_cli/pty_session.py+++ b/hermes_cli/pty_session.py@@ -29,7 +29,8 @@ WS_CLOSE_VIEWER_STALLED_REASON = "Terminal viewer stopped reading; reconnect"_VIEWER_SEND_TIMEOUT_SECONDS = 5.0# A reattach replay is a single frame of up to the 1 MiB buffer (host-terminal clients take exactly# the next binary frame as the replay, so it is not chunked). It gets the stall limit plus the time-# to cross a link this slow (about 256 kbit/s; 37 s for 1 MiB), else a weak connection redials forever.+# to cross a link this slow (about 256 kbit/s; 37 s for 1 MiB). A replay that misses that deadline is+# halved for the session's next attach, so a viewer on a slower link redials onto a replay it carries._REPLAY_MIN_BYTES_PER_SECOND = 32 * 1024# A viewer send that fails because the browser is gone: Starlette raises WebSocketDisconnect for a# dead transport and RuntimeError for a socket already closed; OSError covers the TimeoutError of@@ -97,6 +98,8 @@ class PtySession:self.active_session_file = active_session_fileself.active_session_cleanup: Optional[Callable[[], None]] = Noneself.buffer = RingBuffer(buffer_cap)+ # How many of the buffer's newest bytes a reattach replays; a replay that times out halves it.+ self._replay_limit = buffer_capself.alive = Trueself.attached = False# Admission can fail before the first attach (e.g. lost metadata).@@ -229,6 +232,7 @@ class PtySession:try:async with output_lock:snap = self.buffer.snapshot()+ snap = snap[len(snap) - min(len(snap), self._replay_limit):]if old_ws is not ws:await _close_ws(old_ws, WS_CLOSE_SUPERSEDED)if self._ws is not ws:@@ -242,7 +246,11 @@ class PtySession:# frame never loses terminal-query replies on an empty history.if snap or initial_text is not None:replay_timeout = _VIEWER_SEND_TIMEOUT_SECONDS + len(snap) / _REPLAY_MIN_BYTES_PER_SECOND- await asyncio.wait_for(ws.send_bytes(snap), timeout=replay_timeout)+ try:+ await asyncio.wait_for(ws.send_bytes(snap), timeout=replay_timeout)+ except TimeoutError:+ self._replay_limit = len(snap) // 2+ raiseexcept _VIEWER_SEND_ERRORS:self.detach(ws)return Falsediff --git a/tests/hermes_cli/test_pty_session.py b/tests/hermes_cli/test_pty_session.pyindex 41e4fea9fb7..c3442cbd194 100644--- a/tests/hermes_cli/test_pty_session.py+++ b/tests/hermes_cli/test_pty_session.py@@ -214,6 +214,51 @@ async def test_a_full_replay_over_a_slow_link_is_not_a_stalled_viewer(monkeypatcstalled.release.set()+class NarrowLinkWS(FakeWS):+ """Carries a frame of up to ``capacity`` bytes within the deadline and never finishes a larger+ one: a viewer still reading, over a link too slow for the whole replay."""++ def __init__(self, capacity):+ super().__init__()+ self._capacity = capacity++ async def send_bytes(self, data):+ if len(data) > self._capacity:+ await asyncio.Event().wait()+ await super().send_bytes(data)+++async def test_redials_over_a_link_too_slow_for_the_whole_replay_reach_a_replay_it_carries(monkeypatch):+ """A replay that misses its deadline is halved for the session's next attach, so a viewer on a+ link of any speed gets the newest output it can carry after a few redials instead of timing out+ on every one. A client that dropped is not a slow link and keeps the whole replay."""+ from hermes_cli import pty_session+ from hermes_cli.pty_session import PtySession++ monkeypatch.setattr(pty_session, "_VIEWER_SEND_TIMEOUT_SECONDS", 0.01)+ monkeypatch.setattr(pty_session, "_REPLAY_MIN_BYTES_PER_SECOND", 1024 ** 3)+ history = bytes(range(256)) * 256+ session = PtySession("k", FakeBridge([]), buffer_cap=len(history), read_timeout=0.01)+ session.buffer.append(history)++ assert await session.attach(FailingWS()) is False+ whole = FakeWS()+ assert await session.attach(whole)+ assert whole.sent == [("bytes", history)]++ attached = []+ for _redial in range(12):+ viewer = NarrowLinkWS(4096)+ attached.append(await session.attach(viewer))+ if attached[-1]:+ break+ assert attached[-1], attached+ replay = viewer.sent[0][1]+ assert 0 < len(replay) <= 4096 and history.endswith(replay)+ assert session._ws is viewer and viewer.close_code is None++@pytest.mark.asyncioasync def test_reattach_can_force_complete_tui_redraw_after_replay():"""A fresh terminal cannot reconstruct a differential ANSI tail alone."""