Skip to content

The agent that grows with you

fix(gateway): a slow viewer is bounded by bytes, not a 256-frame count

Tekniumd5f9cb15e27e
diff --git a/tests/gateway/fixtures/authority_stream_peer.py b/tests/gateway/fixtures/authority_stream_peer.py
index 2b12eeb4169..5b62f029016 100644
--- a/tests/gateway/fixtures/authority_stream_peer.py
+++ b/tests/gateway/fixtures/authority_stream_peer.py
@@ -272,6 +272,12 @@ def main():
peer = ThreadingHTTPServer(('127.0.0.1', 0), ModelPeer)
peer.requests = []
peer.blocked = os.environ.get('STREAM_PROBE_MODE') == 'blocked'
+ if peer.blocked:
+ # The stalled observer must hit a mailbox bound while the session replay buffer (4 MB) still
+ # holds the whole turn and the terminal frame (the whole reply) still fits the byte cap, so
+ # bound it by frames: the 330 chunks below exceed this.
+ from tui_gateway.transport import FanoutTransport
+ FanoutTransport._MAX_PENDING_FRAMES = 256
peer.acks = queue.Queue()
peer.command = 'printf owned-effect > ' + shlex.quote(str(target))
threading.Thread(target=peer.serve_forever, daemon=True).start()
diff --git a/tests/tui_gateway/test_fanout_burst_batching.py b/tests/tui_gateway/test_fanout_burst_batching.py
index 5a99ab58a42..7a5ab586c58 100644
--- a/tests/tui_gateway/test_fanout_burst_batching.py
+++ b/tests/tui_gateway/test_fanout_burst_batching.py
@@ -72,3 +72,31 @@ def test_the_drain_sends_a_queued_backlog_as_one_batch():
loop.call_soon_threadsafe(loop.stop)
runner.join(5)
loop.close()
+
+
+def test_a_viewer_a_turn_of_streamed_text_behind_keeps_its_subscription():
+ """A viewer stalled for a whole long reply (thousands of few-hundred-byte deltas, well under the
+ byte cap) is behind, not stuck: it keeps its subscription and gets every frame in order. A
+ frame-count cap tuned for big frames dropped it after 256, and a `chat -q` turn then ended
+ `session_replay_gap` on a merely loaded host."""
+ loop = asyncio.new_event_loop()
+ runner = threading.Thread(target=loop.run_forever, daemon=True)
+ runner.start()
+ try:
+ socket = _LoopSocket(loop)
+ fanout = FanoutTransport(socket)
+ gaps = []
+ fanout.write(_delta(0), overflow=lambda transport: gaps.append(transport) or None)
+ assert socket.first_send_started.wait(10), 'the drain never reached the socket'
+ behind = 3000 # ~12 KB of reply streamed 8 characters at a time, ~1 MB of frames
+ for index in range(1, behind + 1):
+ fanout.write(_delta('x' * 300 + str(index)), overflow=lambda transport: gaps.append(transport) or None)
+ assert not gaps and fanout.contains(socket), 'a viewer one reply behind lost its subscription'
+ loop.call_soon_threadsafe(socket.release.set)
+ wait_until(lambda: len(socket.frames) == behind + 1, timeout=30, what='the backlog delivered')
+ assert [f['params']['payload']['text'] for f in socket.frames[1:]] == [
+ 'x' * 300 + str(i) for i in range(1, behind + 1)]
+ finally:
+ loop.call_soon_threadsafe(loop.stop)
+ runner.join(5)
+ loop.close()
diff --git a/tests/tui_gateway/test_multi_client_fanout.py b/tests/tui_gateway/test_multi_client_fanout.py
index 54b24c69199..8437ddeadff 100644
--- a/tests/tui_gateway/test_multi_client_fanout.py
+++ b/tests/tui_gateway/test_multi_client_fanout.py
@@ -273,7 +273,7 @@ def test_backpressure_never_blocks_later_frames_or_other_subscribers(slow_first,
"message.start", "message.delta", "message.complete"]
# Exhaust only the slow peer's bounded backlog; pace the healthy
# reader by receipts so scheduler latency cannot make it overflow.
- for n in range(1024):
+ for n in range(FanoutTransport._MAX_PENDING_FRAMES + 64):
frame = {"params": {"type": "message.delta", "n": n}}
assert fan.write(frame)
assert healthy.receive() == frame
diff --git a/tui_gateway/transport.py b/tui_gateway/transport.py
index 6abcb8f4ab9..691f1d79902 100644
--- a/tui_gateway/transport.py
+++ b/tui_gateway/transport.py
@@ -152,7 +152,11 @@ class FanoutTransport:
Other sockets are unaffected. A write already in the OS cannot be revoked.
"""
- _MAX_PENDING_FRAMES = 256
+ # The byte cap is what bounds a slow viewer; the frame cap only backstops a flood of empty
+ # frames. Streamed text is a few hundred bytes per frame, so a 256-frame cap dropped a viewer
+ # that fell ~95 KB behind on a loaded host (a `chat -q` turn then ended `session_replay_gap`)
+ # while the 4 MB budget was 2% used.
+ _MAX_PENDING_FRAMES = 16384
_MAX_PENDING_BYTES = 4 * 1024 * 1024
def __init__(self, *transports: Transport) -> None: