The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): preserve streamed final after flood rejection
fangliquanflqd7e6461b5f29
diff --git a/gateway/stream_consumer.py b/gateway/stream_consumer.pyindex 65572c1b660..9fa637cafe6 100644--- a/gateway/stream_consumer.py+++ b/gateway/stream_consumer.py@@ -2217,12 +2217,23 @@ class GatewayStreamConsumer:self._already_sent = Trueself._fallback_prefix = ""self._fallback_preserve_partial_messages = False- if delivery == "ambiguous":+ if delivery in {"ambiguous", "preview"}:# A timeout may mean Telegram accepted the send but the- # client never received the response. Preserve duplicate- # suppression for that one uncertain outcome.+ # client never received the response. A flood rejection+ # leaves the complete, ACKed preview as the authoritative+ # delivery. Preserve duplicate suppression in both cases.self._final_content_delivered = True- self._delivery_ambiguous = True+ if delivery == "preview":+ # This branch is only reached when the ACKed preview+ # already shows the complete final text+ # (final_text == _visible_prefix()), so record it as+ # the turn-final payload: the gateway's reconciliation+ # then confirms delivery instead of re-sending a+ # second bubble next to the never-deleted preview+ # (#71047 Problem B).+ self._record_turn_final_payload(final_text)+ else:+ self._delivery_ambiguous = Trueelse:# A confirmed failure leaves the gateway free to perform# its normal final send.@@ -2387,8 +2398,9 @@ class GatewayStreamConsumer:"""Commit a completed answer after Telegram finalization fails.Returns ``delivered`` on confirmed success, ``failed`` when the- gateway can safely retry, and ``ambiguous`` when a timeout may have- reached the platform already.+ gateway can safely retry, ``ambiguous`` when a timeout may have+ reached the platform already, and ``preview`` when flood control+ leaves the complete streamed preview as the authoritative delivery."""# Tool/segment boundaries intentionally preserve the run-wide preview# IDs for normal fresh-final cleanup. This recovery replaces only the@@ -2423,6 +2435,8 @@ class GatewayStreamConsumer:)await asyncio.sleep(retry_delay)continue+ if self._is_flood_error(result):+ return "preview"return ("ambiguous"if self._send_failure_may_have_delivered(result)diff --git a/tests/gateway/test_telegram_final_delivery.py b/tests/gateway/test_telegram_final_delivery.pyindex e5d378dcee0..5535cb89391 100644--- a/tests/gateway/test_telegram_final_delivery.py+++ b/tests/gateway/test_telegram_final_delivery.py@@ -122,6 +122,33 @@ async def test_empty_tail_commit_honors_retry_after(monkeypatch):assert consumer.final_content_delivered is True+async def test_complete_preview_survives_long_flood_fallback_failure(monkeypatch):+ """A complete ACKed preview must not trigger a duplicate normal final."""+ adapter = _adapter()+ adapter.send.return_value = SendResult(+ success=False,+ error="flood_control:20.0",+ retry_after=20.0,+ )+ sleep = AsyncMock()+ monkeypatch.setattr("gateway.stream_consumer.asyncio.sleep", sleep)++ consumer = GatewayStreamConsumer(adapter, "chat-1")+ consumer._message_id = "preview-1"+ consumer._last_sent_text = "Final answer"+ consumer._already_sent = True+ consumer._fallback_final_send = True++ await consumer._send_fallback_final("Final answer")++ adapter.send.assert_awaited_once()+ sleep.assert_not_awaited()+ assert consumer.final_response_sent is False+ assert consumer.final_content_delivered is True+ assert consumer.delivered_final_matches("Final answer") is True++@pytest.mark.asyncioasync def test_telegram_long_flood_result_keeps_retry_after():"""The real adapter contract preserves the server delay for consumers."""