The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(streaming): flood-refused edits wait out Telegram's retry_after instead of re-striking inside the penalty
teknium175d92e3d7bea
diff --git a/gateway/stream_consumer.py b/gateway/stream_consumer.pyindex 04f9373fc3c..eb92b5c4e9f 100644--- a/gateway/stream_consumer.py+++ b/gateway/stream_consumer.py@@ -726,9 +726,12 @@ class GatewayStreamConsumer(StreamTransportMixin, StreamFallbackMixin, StreamThielse:elapsed = time.monotonic() - self._last_edit_time# buffer_threshold is a codepoint debounce heuristic, not a- # platform-limit check (_len_fn is for overflow).+ # platform-limit check (_len_fn is for overflow). It must not+ # override an active flood backoff: while a refusal is being+ # waited out, only the (server-requested) interval may fire an edit.should_edit = bool((elapsed >= self._current_edit_interval and self._accumulated)- or len(self._accumulated) >= self.cfg.buffer_threshold)+ or (len(self._accumulated) >= self.cfg.buffer_threshold+ and not self._flood_strikes))# Defer mid-stream edits while the buffer could still resolve to a silence# marker ("NO"→"NO_REPLY"); got_done always resolves the buffer.return should_edit and not _is_partial_silence_marker(diff --git a/gateway/stream_consumer_transport.py b/gateway/stream_consumer_transport.pyindex 0bbb2b442a8..c6aad5c2124 100644--- a/gateway/stream_consumer_transport.py+++ b/gateway/stream_consumer_transport.py@@ -16,6 +16,11 @@ from gateway.stream_consumer_fences import ensure_closed_code_fenceslogger = logging.getLogger("gateway.stream_consumer")+# Longest interim-edit interval flood backoff may reach. Interim edits are skipped (not slept)+# until the interval elapses, so honouring a 30s Telegram retry_after costs nothing but a+# stale preview; anything longer is left to the strike counter and the fallback send path.+_MAX_EDIT_BACKOFF_SECS = 30.0+class StreamTransportMixin:"""Send/edit/frame primitives and the transport-ordered ``_send_or_edit``."""@@ -555,12 +560,18 @@ class StreamTransportMixin:self._notify_new_message()return False- # Flood control: adaptive backoff (double the interval); disable edits only+ # Flood control: adaptive backoff (double the interval, or the server's own+ # retry_after when Telegram hands one back — the penalty is usually 9s+, so+ # doubling 0.8s → 1.6s → 3.2s burns all strikes inside it); disable edits only# after _MAX_FLOOD_STRIKES in a row.immediate_final_fallback = Falseif self._is_flood_error(result):self._flood_strikes += 1- self._current_edit_interval = min(self._current_edit_interval * 2, 10.0)+ backoff = min(self._current_edit_interval * 2, 10.0)+ retry_after = getattr(result, "retry_after", None)+ if isinstance(retry_after, (int, float)) and retry_after > 0:+ backoff = max(backoff, min(float(retry_after), _MAX_EDIT_BACKOFF_SECS))+ self._current_edit_interval = backofflogger.debug("Flood control on edit (strike %d/%d), backoff interval → %.1fs",self._flood_strikes, self._MAX_FLOOD_STRIKES, self._current_edit_interval)immediate_final_fallback = (