Skip to content

The agent that grows with you

fix: slot-busy skipped Telegram edits no longer count as shown text (review follow-up)

teknium1a57a5f90d2bb
diff --git a/gateway/stream_consumer_transport.py b/gateway/stream_consumer_transport.py
index c6aad5c2124..52c16dcc331 100644
--- a/gateway/stream_consumer_transport.py
+++ b/gateway/stream_consumer_transport.py
@@ -486,6 +486,12 @@ class StreamTransportMixin:
if not result.success:
return await self._on_edit_failure(result, text, finalize=finalize,
is_turn_final=is_turn_final)
+ raw_response = getattr(result, "raw_response", None)
+ if isinstance(raw_response, dict) and raw_response.get("skipped"):
+ # Adapter deferred the edit (Telegram's shared send+edit slot was busy): nothing
+ # changed on screen, so the visible prefix and flood state stay as they were and the
+ # next tick retries with the newer text.
+ return True
self._already_sent = True
self._track_preview_ids_from_result(result)
# Oversized edit split across continuations: message_id is now the LAST
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index ca0d6d65a20..6f627f130e4 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -3792,7 +3792,9 @@ class TelegramAdapter(BasePlatformAdapter):
# control. A final edit is never gated (the completed answer is always delivered). Sends wait for
# their slot; edits defer instead. Over-cap interim edits are exempt: the saturated-preview dedup
# below already throttles them to one real edit per ~4096-char growth. Consumed only when the
- # edit actually fires.
+ # edit actually fires. The skip is flagged in raw_response so the stream consumer does not
+ # record never-shown text as the visible prefix (a later flood fallback would then drop the
+ # tail the user never saw).
if (
not finalize
and utf16_len(content) <= self.MAX_MESSAGE_LENGTH
@@ -3801,7 +3803,7 @@ class TelegramAdapter(BasePlatformAdapter):
logger.debug(
"[%s] skipping interim edit for chat %s (shared send+edit budget: slot busy)",
self.name, chat_id)
- return SendResult(success=True, message_id=message_id)
+ return SendResult(success=True, message_id=message_id, raw_response={"skipped": True})
self._hold_chat_outbound_slot(chat_id)
# Rich finalize (Bot API 10.1): edit the preview IN PLACE via rich_message — no fresh send + delete.
# Before the 4,096 pre-flight because the rich cap is 32,768; falls back to legacy on rejection.
diff --git a/tests/gateway/test_telegram_shared_outbound_budget.py b/tests/gateway/test_telegram_shared_outbound_budget.py
index a59abc9bedb..a162eeba809 100644
--- a/tests/gateway/test_telegram_shared_outbound_budget.py
+++ b/tests/gateway/test_telegram_shared_outbound_budget.py
@@ -49,6 +49,38 @@ async def test_interim_edit_skipped_when_slot_busy_but_final_edit_never_gated():
assert adapter._bot.edit_message_text.await_count == 2 # the final edit is never gated
+async def test_skipped_interim_edit_leaves_visible_prefix_untouched():
+ """A slot-busy skip shows nothing new, so the stream consumer must not record the skipped text as
+ the on-screen prefix: a later turn-final flood must not treat the unseen tail as delivered."""
+ from gateway.stream_consumer import GatewayStreamConsumer, StreamConsumerConfig
+
+ adapter = _adapter(AsyncMock())
+ consumer = GatewayStreamConsumer(adapter, "c1", StreamConsumerConfig(cursor="▌"))
+ consumer._message_id = "900"
+ consumer._already_sent = True
+ consumer._flood_strikes = 1
+
+ assert await consumer._edit_existing("The answer▌", finalize=False, is_turn_final=False)
+ assert consumer._last_sent_text == "The answer▌"
+ assert consumer._flood_strikes == 0
+ consumer._flood_strikes = 1
+
+ assert adapter._chat_outbound_slot_remaining("c1") > 0 # slot held: next interim edit is skipped
+ assert await consumer._edit_existing("The answer is 42▌", finalize=False, is_turn_final=False)
+ assert adapter._bot.edit_message_text.await_count == 1
+ assert consumer._last_sent_text == "The answer▌" # screen still shows the older preview
+ assert consumer._flood_strikes == 1
+
+ from gateway.platforms.base import SendResult
+
+ adapter.edit_message = AsyncMock(return_value=SendResult(
+ success=False, error="Flood control exceeded. Retry in 9 seconds", retry_after=9))
+ assert not await consumer._edit_existing("The answer is 42", finalize=True, is_turn_final=True)
+ assert not getattr(consumer, "_final_content_delivered", False)
+ assert consumer._fallback_prefix == "The answer" # fallback re-sends " is 42", not nothing
+
+
@pytest.mark.asyncio
async def test_send_waits_for_held_slot():
"""A send to a chat whose slot is held defers until the slot opens and then fires exactly once."""