The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(streaming): back fallback continuation cut up to last word boundary
Haisam Abbas317891373f45
diff --git a/gateway/stream_consumer_fallback.py b/gateway/stream_consumer_fallback.pyindex 44b0e2f35e8..335e5013e5a 100644--- a/gateway/stream_consumer_fallback.py+++ b/gateway/stream_consumer_fallback.py@@ -51,7 +51,21 @@ class StreamFallbackMixin:"""Return only the part of final_text the user has not already seen."""prefix = self._fallback_prefix or self._visible_prefix()if prefix and final_text.startswith(prefix):- return final_text[len(prefix):].lstrip()+ cut = len(prefix)+ # ``prefix`` is whatever the last successful edit put on screen. Edits+ # fire on a throttle tick, not at a word boundary, so that prefix can+ # end inside a word. Back the cut up to the last space or newline so+ # the continuation re-sends the broken word's tail and reads as an+ # ordinary continuation. A prefix with no boundary (one very long+ # token) keeps the original cut rather than re-sending the whole reply.+ if cut < len(final_text):+ boundary = max(+ final_text.rfind(" ", 0, cut),+ final_text.rfind("\n", 0, cut),+ )+ if boundary >= 0:+ cut = boundary + 1+ return final_text[cut:].lstrip()return final_text@staticmethoddiff --git a/tests/gateway/test_stream_consumer_continuation_cut.py b/tests/gateway/test_stream_consumer_continuation_cut.pynew file mode 100644index 00000000000..844a26ea80f--- /dev/null+++ b/tests/gateway/test_stream_consumer_continuation_cut.py@@ -0,0 +1,80 @@+"""Regression tests for the word-boundary fallback continuation cut (#116312).++Streaming edits fire on a throttle tick, not at a word boundary, so the prefix+(the last text a successful edit put on screen) can end inside a word. When+edits then stop working, _continuation_text must back the cut up to the last+space or newline so the fresh continuation re-sends the broken word's tail and+reads as an ordinary continuation instead of starting mid-word.+"""++from __future__ import annotations++from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock++from gateway.stream_consumer import GatewayStreamConsumer, StreamConsumerConfig+++def _make_consumer() -> GatewayStreamConsumer:+ """Minimal consumer whose send/edit surface is only exercised if a bug+ sends on the wrong field."""+ adapter = MagicMock()+ adapter.REQUIRES_EDIT_FINALIZE = False+ adapter.MAX_MESSAGE_LENGTH = 4096+ adapter.send = AsyncMock(return_value=SimpleNamespace(success=True, message_id="m"))+ adapter.edit_message = AsyncMock(return_value=SimpleNamespace(success=True, message_id="m"))+ return GatewayStreamConsumer(+ adapter=adapter,+ chat_id="chat",+ config=StreamConsumerConfig(cursor=" ▉"),+ )+++class TestContinuationWordBoundary:+ """#116312 — back the fallback cut up to the last space/newline."""++ def test_mid_word_prefix_backs_up_to_last_space(self):+ # Issue reproduction: the visible preview ends mid-word ("рядкам" of+ # "рядками"); the continuation must start at the word boundary.+ consumer = _make_consumer()+ consumer._last_sent_text = "Правлю точними живими рядкам ▉"+ final_text = "Правлю точними живими рядками (мій попередній EN-паттерн…"+ assert consumer._continuation_text(final_text) == (+ "рядками (мій попередній EN-паттерн…")++ def test_mid_word_prefix_backs_up_to_last_newline(self):+ consumer = _make_consumer()+ consumer._last_sent_text = "a\nb c\nword"+ assert consumer._continuation_text("a\nb c\nword-two") == "word-two"++ def test_one_long_token_keeps_original_cut(self):+ # No boundary to back up to: the original cut stands rather than+ # re-sending the whole message.+ consumer = _make_consumer()+ consumer._last_sent_text = "у" * 100+ final_text = ("у" * 100) + ("д" * 20)+ assert consumer._continuation_text(final_text) == "д" * 20++ def test_prefix_ending_on_space_changes_nothing(self):+ # Already at a word boundary: the cut stays exactly at the prefix end.+ consumer = _make_consumer()+ consumer._last_sent_text = "hello world "+ assert consumer._continuation_text("hello world foo") == "foo"++ def test_fallback_prefix_takes_precedence(self):+ consumer = _make_consumer()+ consumer._fallback_prefix = "first line\nkeep word"+ consumer._last_sent_text = "ignored older prefix "+ final_text = "first line\nkeep word-zone tail"+ assert consumer._continuation_text(final_text) == "word-zone tail"++ def test_unrelated_text_returns_whole_text(self):+ consumer = _make_consumer()+ consumer._last_sent_text = "something completely different"+ final_text = "let me start over"+ assert consumer._continuation_text(final_text) == final_text++ def test_full_match_returns_empty_tail(self):+ consumer = _make_consumer()+ consumer._last_sent_text = "всі слова повністю"+ assert consumer._continuation_text("всі слова повністю") == ""\ No newline at end of file