The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway/stream): a boundary drained in the overflow-split tick still applies
teknium1a09b36e82c26
diff --git a/gateway/stream_consumer.py b/gateway/stream_consumer.pyindex 235dfed8df3..7bf0b1c27a7 100644--- a/gateway/stream_consumer.py+++ b/gateway/stream_consumer.py@@ -574,23 +574,23 @@ class GatewayStreamConsumer(StreamTransportMixin, StreamFallbackMixin, StreamThiif self._should_edit(tick) and (self._accumulated or (self._use_native_streaming and self._tool_progress_active)):+ # Seal first: it clears the message id, so a remainder still over the limit+ # is split again below. A plain first send would let the adapter split it and+ # adopt only the LAST chunk as the preview; the next seal then overwrites that+ # chunk with the head of the whole remainder (duplicated + lost text, #25349).+ await self._seal_overflow_heads()# Overflow split. Native streaming bypasses this: the adapter# truncates against the stream protocol's own limit.if not self._use_native_streaming and self._first_send_overflows():if await self._split_first_send(tick):return- continue- await self._seal_overflow_heads()- # Sealing clears the message id, so the gate above is stale: a remainder still- # over the limit must be split again. A plain first send lets the adapter split- # it and adopt only the LAST chunk as the preview; the next seal overwrites that- # chunk with the head of the whole remainder (duplicated + lost text, #25349).- # `continue` like the gate above: the tail is still unsent, and the segment-break- # reset below would clear it.- if not self._use_native_streaming and self._first_send_overflows():- if await self._split_first_send(tick):- return- continue+ if 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.+ self._signal_flush(tick.flush_event)+ 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.await self._push_update(tick)if tick.got_done:@@ -792,11 +792,6 @@ class GatewayStreamConsumer(StreamTransportMixin, StreamFallbackMixin, StreamThiif tick.got_segment_break:self._fallback_final_send = Falseself._fallback_prefix = ""- if not self._accumulated:- return False- # Early `continue` skips the bottom-of-loop flush signal.- if tick.got_flush:- self._signal_flush(tick.flush_event)return Falsedef _overflows(self) -> bool:diff --git a/tests/gateway/test_stream_consumer_oversized_leftover.py b/tests/gateway/test_stream_consumer_oversized_leftover.pyindex 87a76e2ce0b..3a81f050588 100644--- a/tests/gateway/test_stream_consumer_oversized_leftover.py+++ b/tests/gateway/test_stream_consumer_oversized_leftover.py@@ -106,3 +106,36 @@ async def test_no_tail_line_is_published_twice_across_new_messages():f"{len(repeated)} tail line(s) reached the channel more than once as NEW "f"messages (first: {repeated[:3]}) - that is the interleaved duplicate")++[email protected]("boundary", ["commentary", "segment_break"])+async def test_boundary_in_the_sealing_tick_still_applies(boundary):+ """A commentary or tool boundary drained in the same tick that seals an oversized+ tail must still take effect: commentary is delivered, and post-boundary text starts+ a NEW message instead of being glued onto the pre-boundary preview."""+ adapter = _make_plain_adapter()+ sends, edits = _wire(adapter)+ config = StreamConsumerConfig(edit_interval=0.01, buffer_threshold=5, cursor="")+ consumer = GatewayStreamConsumer(adapter, "chat_plain", config)+ consumer.on_delta(HEAD)+ task = asyncio.create_task(consumer.run())+ await asyncio.sleep(0.06)+ # Enqueued synchronously, so one drain folds TAIL and stops at the boundary.+ consumer.on_delta(TAIL)+ if boundary == "commentary":+ consumer.on_commentary("COMMENTARY-MARKER")+ consumer.on_segment_break()+ consumer.on_delta("POST-TOOL-MARKER")+ await asyncio.sleep(0.12)+ consumer.finish()+ await asyncio.wait_for(task, timeout=10)++ texts = [c for c, _ in sends] + edits+ if boundary == "commentary":+ assert any("COMMENTARY-MARKER" in t for t in texts), "commentary was dropped"+ glued = [t for t in texts if "POST-TOOL-MARKER" in t and "tail line" in t]+ assert not glued, "post-boundary text was glued onto the pre-boundary preview"+ assert any("POST-TOOL-MARKER" in t for t in texts)+ joined = "".join(c for c, _ in sends)+ assert all(joined.count(f"tail line {i:03d}") <= 1 for i in range(240))