Skip to content

The agent that grows with you

fix(telegram): split replies resume from the refused chunk, never re-send the head; per-chat send order + flood cooldown

teknium1595f3a289c78
diff --git a/gateway/platforms/base.py b/gateway/platforms/base.py
index a57ab1fbe35..00cb65741fd 100644
--- a/gateway/platforms/base.py
+++ b/gateway/platforms/base.py
@@ -3411,6 +3411,15 @@ class BasePlatformAdapter(ABC):
failures fall back to a plain-text send, exhausted retries notify the user."""
async def _send(text: str) -> "SendResult":
return await self.send(chat_id=chat_id, content=text, reply_to=reply_to, metadata=metadata)
+
+ async def _send_again(previous: "SendResult") -> "Optional[SendResult]":
+ """Retry: the whole payload normally; only the undelivered remainder after a partial split
+ delivery (``raw_response["partial_overflow"]``). ``None`` when the adapter cannot resume — the
+ caller then keeps the partial failure rather than re-sending the already-visible head."""
+ if not self._is_partial_delivery(previous):
+ return await _send(content)
+ return await self._resume_partial_send(chat_id, previous, reply_to=reply_to, metadata=metadata)
+
result = await _send(content)
if result.success or self._send_retry_is_final(result):
return result
@@ -3453,7 +3462,13 @@ class BasePlatformAdapter(ABC):
logger.warning("[%s] Send failed (attempt %d/%d, retrying in %.1fs): %s", self.name,
attempt, max_retries, delay, error_str)
await asyncio.sleep(delay)
- result = await _send(content)
+ resumed = await _send_again(result)
+ if resumed is None:
+ logger.warning(
+ "[%s] Split send partly delivered and the remainder cannot be resumed safely; "
+ "not re-sending the whole payload (would duplicate the visible head): %s", self.name, error_str)
+ return result
+ result = resumed
if result.success:
logger.info("[%s] Send succeeded on retry %d", self.name, attempt)
return result
@@ -3503,6 +3518,10 @@ class BasePlatformAdapter(ABC):
# Non-network / post-retry formatting failure: try plain text as fallback. A
# rate-limited error never reaches here: it classifies as network above and the
# loop only breaks on a non-transient, non-rate-limited error.
+ if self._is_partial_delivery(result):
+ # Part of a split payload is already on screen; a plain-text re-send of the whole would duplicate it.
+ logger.warning("[%s] Send failed after partial delivery: %s — not re-sending as plain text", self.name, error_str)
+ return result
logger.warning("[%s] Send failed: %s — trying plain-text fallback", self.name, error_str)
fallback_result = await self._send_plain_fallback(chat_id, content, reply_to=reply_to, metadata=metadata)
if not fallback_result.success:
@@ -3514,6 +3533,21 @@ class BasePlatformAdapter(ABC):
fallback can fix it (a structured auth/target refusal). Default: never."""
return False
+ @staticmethod
+ def _is_partial_delivery(result: "SendResult") -> bool:
+ """True when a split payload was PARTLY delivered (``raw_response["partial_overflow"]``, the
+ contract Telegram's send/edit-overflow paths set and the stream consumer reads): the visible
+ head must never be sent again."""
+ raw = getattr(result, "raw_response", None)
+ return isinstance(raw, dict) and bool(raw.get("partial_overflow"))
+
+ async def _resume_partial_send(
+ self, chat_id: str, result: "SendResult", *, reply_to: Optional[str], metadata: Any) -> "Optional[SendResult]":
+ """Deliver only the remainder of a partially delivered split payload. ``None`` (the default) means
+ this adapter cannot resume; ``_send_with_retry`` then returns the partial failure instead of
+ re-sending the whole payload. Override only where non-delivery of the remainder is CERTAIN."""
+ return None
+
async def _send_plain_fallback(
self, chat_id: str, content: str, *, reply_to: Optional[str], metadata: Any) -> "SendResult":
"""Last-resort send after a non-transient failure; platforms whose markup is not the
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index f55bb5a9ae7..4d1c7f0d9e0 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -1116,22 +1116,24 @@ class TelegramAdapter(BasePlatformAdapter):
async def _send_with_dm_topic_reply_anchor_retry(
self, send_fn: Any, send_kwargs: Dict[str, Any], metadata: Optional[Dict[str, Any]],
reply_to_message_id: Optional[int], media_label: str, reset_media: Optional[Any] = None) -> Any:
- """Retry stale private-topic media replies once without the topic anchor."""
- try:
- return await send_fn(**send_kwargs)
- except Exception as send_err:
- if not self._should_retry_without_dm_topic_reply_anchor(send_err, metadata, reply_to_message_id):
- raise
- logger.warning(
- "[%s] Reply target deleted for Telegram %s, retrying without reply/topic anchor: %s",
- self.name, media_label, _redact_telegram_error_text(send_err))
- if reset_media is not None:
- reset_media()
- retry_kwargs = dict(send_kwargs)
- retry_kwargs["reply_to_message_id"] = None
- retry_kwargs.pop("message_thread_id", None)
- retry_kwargs.pop("direct_messages_topic_id", None)
- return await send_fn(**retry_kwargs)
+ """Retry stale private-topic media replies once without the topic anchor. Serialized per chat with
+ ``send()`` so a file upload cannot land between two chunks of the text it accompanies."""
+ async with self._chat_send_lock(send_kwargs.get("chat_id")):
+ try:
+ return await send_fn(**send_kwargs)
+ except Exception as send_err:
+ if not self._should_retry_without_dm_topic_reply_anchor(send_err, metadata, reply_to_message_id):
+ raise
+ logger.warning(
+ "[%s] Reply target deleted for Telegram %s, retrying without reply/topic anchor: %s",
+ self.name, media_label, _redact_telegram_error_text(send_err))
+ if reset_media is not None:
+ reset_media()
+ retry_kwargs = dict(send_kwargs)
+ retry_kwargs["reply_to_message_id"] = None
+ retry_kwargs.pop("message_thread_id", None)
+ retry_kwargs.pop("direct_messages_topic_id", None)
+ return await send_fn(**retry_kwargs)
def _fallback_ips(self) -> list[str]:
"""Return validated fallback IPs from config (populated by _apply_env_overrides)."""
@@ -3401,7 +3403,7 @@ class TelegramAdapter(BasePlatformAdapter):
logger.warning(
"[%s] Telegram flood control on send (retry_after=%.1fs > %.0fs); failing closed instead of sleeping: %s",
self.name, wait, _FLOOD_INLINE_WAIT_CAP_SECS, safe_send_error)
- return _flood_cap_result(wait)
+ return self._record_send_flood_cooldown(chat_id, wait)
if _send_attempt < 2:
logger.warning(
"[%s] Telegram flood control on send (attempt %d/3), retrying in %.1fs: %s", self.name,
@@ -3416,7 +3418,7 @@ class TelegramAdapter(BasePlatformAdapter):
"[%s] Telegram flood control on send persisted across %d attempts; failing "
"closed so the delivery ledger owns the wait: %s",
self.name, _send_attempt + 1, safe_send_error)
- return _flood_cap_result(wait)
+ return self._record_send_flood_cooldown(chat_id, wait)
raise
async def _retrigger_typing(self, chat_id: str, metadata: Optional[Dict[str, Any]]) -> None:
@@ -3478,7 +3480,24 @@ class TelegramAdapter(BasePlatformAdapter):
# Skip whitespace-only text to prevent Telegram 400 empty-text errors.
if not content or not content.strip():
return SendResult(success=True, message_id=None)
+ # One chat at a time (held only around the API calls, never across the reconnect wait above), so
+ # two concurrent split replies to one chat cannot interleave their chunks (#114396).
+ async with self._chat_send_lock(chat_id):
+ return await self._send_text_locked(chat_id, content, reply_to, metadata)
+
+ async def _send_text_locked(
+ self, chat_id: str, content: str, reply_to: Optional[str], metadata: Optional[Dict[str, Any]]) -> SendResult:
+ """``send()`` body under the per-chat lock: rich fast-path, else MarkdownV2 chunks."""
+ # Re-checked under the lock: a burst queued behind a refused send must not each fire once.
+ cooldown = self._send_flood_cooldown_remaining(chat_id)
+ if cooldown is not None:
+ logger.warning(
+ "[%s] Telegram flood control still active for chat %s (%.0fs left); refusing locally without an API call",
+ self.name, chat_id, cooldown)
+ return _flood_cap_result(cooldown)
error_types = self._telegram_error_types()
+ chunks: List[str] = []
+ delivered: List[str] = []
try:
# Bot API 10.1 rich fast-path; falls through to legacy MarkdownV2 on permanent/capability
# errors or DM-topic skips; returns directly on success or transient failure (no legacy resend).
@@ -3495,39 +3514,95 @@ class TelegramAdapter(BasePlatformAdapter):
_separate_chunk_indicator_from_fence(re.sub(r" \((\d+)/(\d+)\)$", r" \\(\1/\2\\)", chunk))
for chunk in chunks
]
- message_ids = []
- thread_id = self._metadata_thread_id(metadata)
- requested_thread_id = self._message_thread_id_for_send(thread_id)
- used_thread_fallback = False
- for i, chunk in enumerate(chunks):
- outcome = await self._send_chunk_with_retries(
- chat_id, chunk, i, reply_to, metadata, thread_id, used_thread_fallback, error_types)
- if isinstance(outcome, SendResult):
- return outcome
- msg, used_thread_fallback = outcome
- message_ids.append(str(msg.message_id))
- await self._retrigger_typing(chat_id, metadata)
- return SendResult(
- success=True, message_id=message_ids[0] if message_ids else None,
- raw_response={
- "message_ids": message_ids, "requested_thread_id": requested_thread_id, "thread_fallback": used_thread_fallback})
+ return await self._send_chunks(chat_id, chunks, delivered, reply_to, metadata, error_types)
except Exception as e:
- safe_error = _redact_telegram_error_text(e)
- logger.error("[%s] Failed to send Telegram message: %s", self.name, safe_error)
- err_str = str(e).lower()
- error_kind = classify_send_error(e)
- # Content exceeded 4096 chars: fail so the stream consumer enters fallback mode.
- if "message_too_long" in err_str or "too long" in err_str:
- logger.debug("[%s] send() content too long, falling back to new-message continuation", self.name)
- return SendResult(success=False, error="message_too_long", error_kind="too_long")
- # TimedOut may have reached Telegram — non-retryable so _send_with_retry() doesn't re-send,
- # except a wrapped ConnectTimeout or an httpx pool timeout (safe to re-send).
- _to = error_types[2]
- is_timeout = (_to and isinstance(e, _to)) or "timed out" in err_str
- return SendResult(
- success=False, error=safe_error,
- retryable=(self._looks_like_connect_timeout(e) or self._looks_like_pool_timeout(e) or not is_timeout),
- error_kind=error_kind)
+ classified = self._classify_send_exception(e, error_types)
+ return self._with_partial_send(classified, chunks[len(delivered):], delivered, tail_certain=classified.retryable)
+
+ def _classify_send_exception(self, e: Exception, error_types: tuple) -> SendResult:
+ """The failed ``SendResult`` for an exception escaping the chunk loop. ``retryable`` doubles as
+ "non-delivery is certain": a plain ``TimedOut`` may have reached Telegram, so it is neither re-sent
+ by ``_send_with_retry`` nor resumed from; a wrapped ConnectTimeout / httpx pool timeout never left."""
+ safe_error = _redact_telegram_error_text(e)
+ logger.error("[%s] Failed to send Telegram message: %s", self.name, safe_error)
+ err_str = str(e).lower()
+ error_kind = classify_send_error(e)
+ # Content exceeded 4096 chars: fail so the stream consumer enters fallback mode.
+ if "message_too_long" in err_str or "too long" in err_str:
+ logger.debug("[%s] send() content too long, falling back to new-message continuation", self.name)
+ return SendResult(success=False, error="message_too_long", error_kind="too_long")
+ _to = error_types[2]
+ is_timeout = (_to and isinstance(e, _to)) or "timed out" in err_str
+ return SendResult(
+ success=False, error=safe_error,
+ retryable=(self._looks_like_connect_timeout(e) or self._looks_like_pool_timeout(e) or not is_timeout),
+ error_kind=error_kind)
+
+ async def _send_chunks(
+ self, chat_id: str, chunks: List[str], delivered: List[str], reply_to: Optional[str],
+ metadata: Optional[Dict[str, Any]], error_types: tuple) -> SendResult:
+ """Deliver formatted ``chunks`` in order, appending each landed message id to ``delivered``
+ (pre-seeded with the ids of an earlier partial send when resuming — the chunk index used for
+ reply routing continues from there). A mid-loop refusal returns the ``partial_overflow`` result."""
+ thread_id = self._metadata_thread_id(metadata)
+ requested_thread_id = self._message_thread_id_for_send(thread_id)
+ used_thread_fallback = False
+ prior = len(delivered)
+ for chunk in chunks:
+ outcome = await self._send_chunk_with_retries(
+ chat_id, chunk, len(delivered), reply_to, metadata, thread_id, used_thread_fallback, error_types)
+ if isinstance(outcome, SendResult):
+ # Every SendResult returned here is a DEFINITE non-delivery (flood cap, DM-topic refusal);
+ # ambiguous timeouts raise instead, so the remainder is safe to resume from.
+ return self._with_partial_send(outcome, chunks[len(delivered) - prior:], delivered)
+ msg, used_thread_fallback = outcome
+ delivered.append(str(msg.message_id))
+ await self._retrigger_typing(chat_id, metadata)
+ return SendResult(
+ success=True, message_id=delivered[0] if delivered else None,
+ raw_response={
+ "message_ids": list(delivered), "requested_thread_id": requested_thread_id, "thread_fallback": used_thread_fallback})
+
+ @staticmethod
+ def _with_partial_send(
+ result: SendResult, undelivered: List[str], delivered: List[str], *, tail_certain: bool = True) -> SendResult:
+ """Mark a split-send failure that happened after earlier chunks landed with the ``partial_overflow``
+ contract (the same key ``_edit_overflow_split`` sets and the stream consumer reads), so no caller
+ re-sends the already-visible head. ``undelivered`` (the formatted remainder, for
+ :meth:`_resume_partial_send`) is attached only when ``tail_certain``. No-op when nothing landed."""
+ if not delivered:
+ return result
+ raw = dict(result.raw_response) if isinstance(result.raw_response, dict) else {}
+ raw.update({
+ "partial_overflow": True, "delivered_chunks": len(delivered), "total_chunks": len(delivered) + len(undelivered),
+ "last_message_id": delivered[-1], "continuation_message_ids": tuple(delivered[1:])})
+ if tail_certain and undelivered:
+ raw["undelivered_chunks"] = tuple(undelivered)
+ raw["delivered_message_ids"] = tuple(delivered)
+ result.raw_response = raw
+ return result
+
+ async def _resume_partial_send(
+ self, chat_id: str, result: SendResult, *, reply_to: Optional[str], metadata: Optional[Dict[str, Any]]) -> Optional[SendResult]:
+ """Send only the chunks a partial ``send()`` could not deliver (``raw_response["undelivered_chunks"]``),
+ continuing the message-id sequence; ``None`` when the tail was not certain-undelivered."""
+ raw = result.raw_response if isinstance(result.raw_response, dict) else {}
+ undelivered = raw.get("undelivered_chunks")
+ if not undelivered or not self._bot:
+ return None
+ delivered = list(raw.get("delivered_message_ids") or ())
+ prior = len(delivered)
+ async with self._chat_send_lock(chat_id):
+ cooldown = self._send_flood_cooldown_remaining(chat_id)
+ if cooldown is not None:
+ return self._with_partial_send(_flood_cap_result(cooldown), list(undelivered), delivered)
+ error_types = self._telegram_error_types()
+ try:
+ return await self._send_chunks(chat_id, list(undelivered), delivered, reply_to, metadata, error_types)
+ except Exception as e:
+ classified = self._classify_send_exception(e, error_types)
+ return self._with_partial_send(
+ classified, list(undelivered)[len(delivered) - prior:], delivered, tail_certain=classified.retryable)
async def send_or_update_status(
self, chat_id: str, status_key: str, content: str, *, metadata: Optional[Dict[str, Any]] = None) -> SendResult:
@@ -3650,9 +3725,13 @@ class TelegramAdapter(BasePlatformAdapter):
retry_after = getattr(e, "retry_after", None)
if retry_after is not None or "retry after" in err_str:
wait = retry_after if retry_after else 1.0
- logger.warning("[%s] Telegram flood control, waiting %.1fs", self.name, wait)
if wait > _FLOOD_INLINE_WAIT_CAP_SECS:
+ # Log AFTER the cap check: "waiting 33.0s" followed by no wait misled an investigation.
+ logger.warning(
+ "[%s] Telegram flood control, refusing edit (retry_after %.1fs > %.0fs inline cap)",
+ self.name, wait, _FLOOD_INLINE_WAIT_CAP_SECS)
return _flood_cap_result(wait)
+ logger.warning("[%s] Telegram flood control, waiting %.1fs", self.name, wait)
await asyncio.sleep(wait)
try:
await self._edit_text(chat_id, message_id, content)
@@ -5150,6 +5229,54 @@ class TelegramAdapter(BasePlatformAdapter):
self._telegram_typing_cooldown_until.pop(str(chat_id), None)
return False
+ # --- per-chat send ordering + flood cooldown (#114396) ---------------------------------------------
+ # Both keyed by the Bot-API-normalized chat id: the text path passes the raw chat_id while the media
+ # funnel's send_kwargs carry the normalized value. ``__dict__.setdefault``: tests build adapters via
+ # ``object.__new__()`` (no __init__).
+
+ @contextlib.asynccontextmanager
+ async def _chat_send_lock(self, chat_id: Any):
+ """FIFO per-chat gate around outgoing API calls, reentrant within one asyncio task (media paths
+ nest: send_voice → send_document, and ``super().send_*`` fallbacks reach ``send()``; a plain
+ ``asyncio.Lock`` re-acquired by its holder would wedge that chat's sends for good)."""
+ key = str(normalize_telegram_chat_id(chat_id))
+ locks: Dict[str, asyncio.Lock] = self.__dict__.setdefault("_telegram_chat_send_locks", {})
+ owners: Dict[str, asyncio.Task] = self.__dict__.setdefault("_telegram_chat_send_lock_owners", {})
+ task = asyncio.current_task()
+ if owners.get(key) is task:
+ yield
+ return
+ lock = locks.setdefault(key, asyncio.Lock())
+ async with lock:
+ owners[key] = task
+ try:
+ yield
+ finally:
+ owners.pop(key, None)
+ if not getattr(lock, "_waiters", None): # nobody queued: drop the entry (bounded dict)
+ locks.pop(key, None)
+
+ def _record_send_flood_cooldown(self, chat_id: Any, wait: float) -> SendResult:
+ """A send refused with ``retry_after=wait`` arms a per-chat window during which ``send()`` fails
+ closed locally (same ``flood_control:<s>`` result, so ledger recognition and redelivery timing are
+ unchanged) instead of firing more requests into a penalty Telegram lengthens while it is hammered."""
+ until: Dict[str, float] = self.__dict__.setdefault("_telegram_send_cooldown_until", {})
+ until[str(normalize_telegram_chat_id(chat_id))] = asyncio.get_running_loop().time() + max(1.0, min(float(wait), 300.0))
+ return _flood_cap_result(wait)
+
+ def _send_flood_cooldown_remaining(self, chat_id: Any) -> Optional[float]:
+ """Seconds left in this chat's flood window, or ``None`` when sends may go out."""
+ until: Dict[str, float] = self.__dict__.setdefault("_telegram_send_cooldown_until", {})
+ key = str(normalize_telegram_chat_id(chat_id))
+ deadline = until.get(key)
+ if deadline is None:
+ return None
+ remaining = deadline - asyncio.get_running_loop().time()
+ if remaining > 0:
+ return remaining
+ until.pop(key, None)
+ return None
+
async def send_typing(self, chat_id: str, metadata: Optional[Dict[str, Any]] = None) -> None:
"""Send typing indicator."""
if not self._bot or self._typing_in_cooldown(chat_id):
diff --git a/tests/gateway/test_telegram_split_send_flood.py b/tests/gateway/test_telegram_split_send_flood.py
new file mode 100644
index 00000000000..95215628951
--- /dev/null
+++ b/tests/gateway/test_telegram_split_send_flood.py
@@ -0,0 +1,115 @@
+"""Split Telegram replies under flood control: no duplicated head, no interleaving, no hammering.
+
+A reply past ``MAX_MESSAGE_LENGTH`` goes out as several ``sendMessage`` calls. When chunk 2 is refused
+(``RetryAfter`` past the inline cap) the adapter must report the partial delivery via the existing
+``partial_overflow`` contract and ``_send_with_retry`` must resume from the undelivered remainder — never
+re-send chunk 1 (the reporter saw a duplicated head), never drop the tail.
+"""
+import asyncio
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+from gateway.config import PlatformConfig
+from plugins.platforms.telegram.adapter import TelegramAdapter
+
+
+class _FloodError(Exception):
+ def __init__(self, seconds: float):
+ super().__init__(f"Flood control exceeded. Retry in {seconds} seconds")
+ self.retry_after = seconds
+
+
+def _adapter(send_message: AsyncMock) -> TelegramAdapter:
+ adapter = TelegramAdapter(PlatformConfig(enabled=True, token="***"))
+ adapter._rich_send_disabled = True
+ adapter._bot = MagicMock()
+ adapter._bot.send_message = send_message
+ return adapter
+
+
+def _three_chunk_text() -> str:
+ return "\n".join(" ".join(f"w{i * 20 + j}" for j in range(20)) for i in range(80)) # ~9.5k chars, 3 chunks
+
+
+async def test_split_send_resumes_from_undelivered_tail_without_resending_head(monkeypatch):
+ """Chunk 2 refused with a 7s RetryAfter (> the 5s adapter cap, < the 60s base cap): the retry
+ delivers chunks 2..3 only — every chunk exactly once, in order."""
+ sent: list = []
+ calls = {"n": 0}
+
+ async def fake_send_message(text: str, **_kw):
+ calls["n"] += 1
+ if calls["n"] == 2:
+ raise _FloodError(7.0)
+ sent.append(text)
+ return MagicMock(message_id=1000 + calls["n"])
+
+ adapter = _adapter(AsyncMock(side_effect=fake_send_message))
+ monkeypatch.setattr("plugins.platforms.telegram.adapter.asyncio.sleep", AsyncMock())
+
+ async def _penalty_elapses(_delay): # the base retry sleep is mocked; model the window having passed
+ adapter._telegram_send_cooldown_until.clear()
+
+ monkeypatch.setattr("gateway.platforms.base.asyncio.sleep", _penalty_elapses)
+ content = _three_chunk_text()
+ expected = adapter.truncate_message(adapter.format_message(content), adapter.MAX_MESSAGE_LENGTH)
+ assert len(expected) >= 3
+
+ result = await adapter._send_with_retry(chat_id="4242", content=content)
+
+ assert result.success is True
+ assert [t.split()[0] for t in sent] == [c.split()[0] for c in expected] # 1, 2, 3 — no duplicate head
+ assert result.raw_response["message_ids"] == ["1001"] + [str(1002 + i) for i in range(1, len(expected))]
+
+
+async def test_over_cap_flood_returns_partial_overflow_and_arms_cooldown(monkeypatch):
+ """A refusal past the base inline cap is returned typed (ledger owns the wait) but now carries the
+ partial_overflow contract; the next send to that chat fails closed locally, another chat is unaffected."""
+ calls = {"n": 0}
+
+ async def fake_send_message(text: str, **_kw):
+ calls["n"] += 1
+ if calls["n"] == 2:
+ raise _FloodError(120.0)
+ return MagicMock(message_id=1000 + calls["n"])
+
+ adapter = _adapter(AsyncMock(side_effect=fake_send_message))
+ monkeypatch.setattr("plugins.platforms.telegram.adapter.asyncio.sleep", AsyncMock())
+
+ content = _three_chunk_text()
+ total = len(adapter.truncate_message(adapter.format_message(content), adapter.MAX_MESSAGE_LENGTH))
+ result = await adapter._send_with_retry(chat_id="4242", content=content)
+
+ assert result.success is False and result.error == "flood_control:120.0"
+ raw = result.raw_response
+ assert raw["partial_overflow"] is True and raw["delivered_chunks"] == 1 and raw["total_chunks"] == total >= 3
+ assert raw["last_message_id"] == "1001" and len(raw["undelivered_chunks"]) == total - 1
+ assert calls["n"] == 2
+ # Facet 3: a follow-up send inside the penalty window makes no API call; a different chat still sends.
+ again = await adapter.send("4242", "hello again")
+ assert again.success is False and again.error.startswith("flood_control:") and calls["n"] == 2
+ other = await adapter.send("999", "hello other")
+ assert other.success is True and calls["n"] == 3
+
+
+async def test_concurrent_split_sends_to_one_chat_do_not_interleave():
+ """Two 3-chunk sends racing on one chat arrive as A A A B B B, not A B A B A B."""
+ order: list = []
+
+ async def fake_send_message(text: str, **_kw):
+ await asyncio.sleep(0) # yield like a real round-trip so an unlocked loop interleaves
+ order.append(text.split()[0])
+ return MagicMock(message_id=len(order))
+
+ adapter = _adapter(AsyncMock(side_effect=fake_send_message))
+ long = lambda tag: "\n".join(" ".join([tag] * 30) for _ in range(90)) # noqa: E731
+
+ await asyncio.gather(adapter.send("1", long("REPORT")), adapter.send("1", long("ALERT")))
+
+ assert len(order) >= 4
+ switches = sum(1 for a, b in zip(order, order[1:]) if a != b)
+ assert switches == 1, order
diff --git a/website/docs/user-guide/messaging/telegram.md b/website/docs/user-guide/messaging/telegram.md
index e15fb677e3a..855bd222ca4 100644
--- a/website/docs/user-guide/messaging/telegram.md
+++ b/website/docs/user-guide/messaging/telegram.md
@@ -1040,6 +1040,8 @@ gateway:
When enabled, Hermes attaches Telegram's `LinkPreviewOptions(is_disabled=True)` to every outgoing message and falls back to the legacy `disable_web_page_preview` parameter on older `python-telegram-bot` versions.
+**Long replies and flood control.** A reply longer than Telegram's 4,096-character limit is sent as numbered parts (`(1/3)`, `(2/3)`, …). Sends to one chat are delivered one reply at a time, so a scheduled report and a DM answer landing together cannot interleave their parts, and a file upload cannot land between two parts of the text it accompanies. If Telegram's flood control refuses a part mid-way, Hermes resumes from the refused part once the penalty passes instead of re-sending the parts already on screen, and while a chat is inside a known penalty window further sends to it fail closed locally (no extra requests that would lengthen the penalty). A penalty longer than the gateway's inline wait cap is handed to the delivery ledger, which redelivers the reply with a "part of it may already have arrived above" note.
+
## Group Allowlisting
Telegram groups and forum chats have two orthogonal gates you can configure: