The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway/stream): a failed overflow head send no longer spins the event loop
teknium1f0d9bd2fc497
diff --git a/gateway/stream_consumer.py b/gateway/stream_consumer.pyindex b82a27c2c1d..43de1841322 100644--- a/gateway/stream_consumer.py+++ b/gateway/stream_consumer.py@@ -586,8 +586,11 @@ class GatewayStreamConsumer(StreamTransportMixin, StreamFallbackMixin, StreamThireturnif self._first_send_overflows():# A head send failed: keep the full text for the fallback final, and- # skip the boundary reset below that would clear it.+ # skip the boundary reset below that would clear it. Still yield: the+ # buffer stays over the debounce threshold, so `continue` alone re-enters+ # the split on every pass and spins the loop without ever sleeping.self._signal_flush(tick.flush_event)+ await asyncio.sleep(0.05)continue# The split tail goes out now, so a commentary or tool boundary drained in# this tick still lands after it instead of being dropped.diff --git a/tests/gateway/test_stream_consumer.py b/tests/gateway/test_stream_consumer.pyindex ca5ea8ddaf3..b2e18213d9e 100644--- a/tests/gateway/test_stream_consumer.py+++ b/tests/gateway/test_stream_consumer.py@@ -580,6 +580,34 @@ class TestFinalResponseDeliveryGuard:"wrongly suppress its fallback delivery (#10748)")+ @pytest.mark.asyncio+ async def test_failed_head_send_yields_the_loop_instead_of_spinning(self):+ """While the overflowing buffer waits for the fallback final after a head send failed,+ the run loop must still sleep between passes: the buffer stays over the debounce+ threshold, so re-entering the split on every pass without a yield starves every other+ task on the loop (WS fan-out, timers) for the rest of the turn."""+ adapter = MagicMock()++ async def failing_send(**_kw):+ await asyncio.sleep(0) # a real transport yields; without it the test could only hang+ return SimpleNamespace(success=False, error="network down")++ adapter.send = AsyncMock(side_effect=failing_send)+ adapter.edit_message = AsyncMock(return_value=SimpleNamespace(success=True))+ adapter.MAX_MESSAGE_LENGTH = 100+ adapter.truncate_message = MagicMock(side_effect=lambda text, limit: [text[:limit], text[limit:]])++ consumer = GatewayStreamConsumer(adapter, "chat_123", StreamConsumerConfig(edit_interval=0.01, buffer_threshold=5))+ consumer.on_delta("x" * 1200) # past the 500-codepoint floor of the safe limit+ task = asyncio.create_task(consumer.run())+ await asyncio.sleep(0.3)+ passes = adapter.send.await_count+ consumer.finish()+ await task++ # ~6 loop passes fit in 0.3s at the 50ms cadence; a spinning loop makes thousands.+ assert passes < 50, f"stream consumer spun {passes} head-send attempts in 0.3s without yielding"+@pytest.mark.asyncioasync def test_failed_first_send_leaves_no_uneditable_partial_preview(self):