Skip to content

The agent that grows with you

fix(gateway): fallback flood retry never re-sends a partially delivered split payload

teknium1969898d4ff41
diff --git a/gateway/stream_consumer_fallback.py b/gateway/stream_consumer_fallback.py
index 12c42699e65..44b0e2f35e8 100644
--- a/gateway/stream_consumer_fallback.py
+++ b/gateway/stream_consumer_fallback.py
@@ -216,6 +216,9 @@ class StreamFallbackMixin:
retry_delay = self._fallback_flood_retry_delay(result)
if attempt or retry_delay is None:
break # non-flood error, long flood wait, or second failure
+ raw = getattr(result, "raw_response", None)
+ if isinstance(raw, dict) and raw.get("partial_overflow"):
+ break # split head already on screen: re-sending the whole content duplicates it
logger.debug(retry_log, retry_delay)
await asyncio.sleep(retry_delay)
return result
diff --git a/tests/gateway/test_stale_finalize_suppression.py b/tests/gateway/test_stale_finalize_suppression.py
index 679e4f7e3e1..3888778f0b6 100644
--- a/tests/gateway/test_stale_finalize_suppression.py
+++ b/tests/gateway/test_stale_finalize_suppression.py
@@ -631,3 +631,34 @@ async def test_empty_fallback_final_after_split_records_only_what_survives():
# The head is gone from the chat, so the complete answer was NOT delivered:
# the gateway must be told this is a mismatch and send it.
assert consumer.delivered_final_matches(complete) is False
+
+
+async def test_flood_retry_never_resends_full_payload_after_partial_split_delivery(monkeypatch):
+ """A short-wait flood result that carries ``partial_overflow`` means the head of a split
+ payload is already on screen: the fallback flood retry must NOT sleep and re-send the whole
+ content (that duplicates the head); it returns the partial failure to the caller."""
+ consumer = _consumer()
+ calls: list[str] = []
+
+ async def partial_flood_send(chat_id, content, reply_to=None, metadata=None):
+ calls.append(content)
+ return SendResult(
+ success=False, error="Flood control exceeded. Retry after 2 seconds",
+ raw_response={"partial_overflow": True, "delivered_chunks": 1, "total_chunks": 3},
+ )
+
+ monkeypatch.setattr(consumer.adapter, "send", partial_flood_send)
+ monkeypatch.setattr(consumer, "_fallback_flood_retry_delay", lambda result: 0.0)
+ slept: list[float] = []
+
+ async def fake_sleep(delay):
+ slept.append(delay)
+
+ monkeypatch.setattr(asyncio, "sleep", fake_sleep)
+
+ result = await consumer._send_with_flood_retry(content="HEAD TAIL", retry_log="retry %.1fs")
+
+ assert result.success is False and result.raw_response["partial_overflow"] is True
+ assert calls == ["HEAD TAIL"], f"full payload re-sent after partial delivery: {calls}"
+ assert slept == []