stream_consumer_fallback.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. """Fallback delivery for GatewayStreamConsumer: continuation sends after edits
  2. stop working, chunking, cursor cleanup, commentary and silence retraction."""
  3. from __future__ import annotations
  4. import asyncio
  5. import contextlib
  6. import logging
  7. from typing import Any, Callable, Optional
  8. from gateway.platforms.base import BasePlatformAdapter as _BasePlatformAdapter
  9. from gateway.stream_consumer_fences import ensure_closed_code_fences
  10. logger = logging.getLogger("gateway.stream_consumer")
  11. class StreamFallbackMixin:
  12. """Non-streaming delivery paths used once progressive edits fail or the turn ends oddly."""
  13. async def _send_new_chunk(self, text: str, reply_to_id: Optional[str], *,
  14. final: bool = False) -> Optional[str]:
  15. """Send a new chunk threaded to ``reply_to_id``; returns the new message_id."""
  16. text = self._clean_for_display(text)
  17. if not text.strip():
  18. return reply_to_id
  19. try:
  20. result = await self.adapter.send(
  21. chat_id=self.chat_id, content=text, reply_to=reply_to_id,
  22. metadata=self._metadata_for_send(final=final, expect_edits=not final))
  23. if not (result.success and result.message_id):
  24. self._edit_supported = False
  25. return reply_to_id
  26. self._message_id = str(result.message_id)
  27. self._track_preview_ids_from_result(result)
  28. self._already_sent = True
  29. self._last_sent_text = text
  30. self._notify_new_message()
  31. return str(result.message_id)
  32. except Exception as e:
  33. logger.error("Stream send chunk error: %s", e)
  34. return reply_to_id
  35. def _visible_prefix(self) -> str:
  36. """Return the visible text already shown in the streamed message."""
  37. prefix = self._last_sent_text or ""
  38. if self.cfg.cursor and prefix.endswith(self.cfg.cursor):
  39. prefix = prefix[:-len(self.cfg.cursor)]
  40. return self._clean_for_display(prefix)
  41. def _continuation_text(self, final_text: str) -> str:
  42. """Return only the part of final_text the user has not already seen."""
  43. prefix = self._fallback_prefix or self._visible_prefix()
  44. if prefix and final_text.startswith(prefix):
  45. cut = len(prefix)
  46. # ``prefix`` is whatever the last successful edit put on screen. Edits
  47. # fire on a throttle tick, not at a word boundary, so that prefix can
  48. # end inside a word. Back the cut up to the last space or newline so
  49. # the continuation re-sends the broken word's tail and reads as an
  50. # ordinary continuation. A prefix with no boundary (one very long
  51. # token) keeps the original cut rather than re-sending the whole reply.
  52. # A prefix that already ends on a whole word needs no back-up: doing it
  53. # re-sent that word at the seam.
  54. if cut < len(final_text) and not final_text[cut].isspace() and not final_text[cut - 1].isspace():
  55. boundary = max(
  56. final_text.rfind(" ", 0, cut),
  57. final_text.rfind("\n", 0, cut),
  58. )
  59. if boundary >= 0:
  60. cut = boundary + 1
  61. return final_text[cut:].lstrip()
  62. return final_text
  63. @staticmethod
  64. def _split_text_chunks(text: str, limit: int, len_fn: Callable[[str], int] = len,
  65. ) -> list[str]:
  66. """Split text for fallback sends: newline-preferred, fence-balanced across chunks."""
  67. from gateway.platforms.helpers import split_text_fence_aware
  68. return split_text_fence_aware(text, limit, len_fn, prefer_paragraphs=False,
  69. balance_fences=True)
  70. def _truncate_for_stream(self, text: str, limit: int, len_fn: Callable[[str], int],
  71. ) -> list[str]:
  72. """Split via the adapter's canonical truncate_message (platform-specific rules);
  73. non-base test doubles / legacy adapters keep the two-argument call shape."""
  74. truncate = getattr(self.adapter, "truncate_message", None)
  75. if not callable(truncate):
  76. return self._split_text_chunks(text, limit, len_fn)
  77. if isinstance(self.adapter, _BasePlatformAdapter):
  78. chunks = truncate(text, limit, len_fn=len_fn)
  79. else:
  80. chunks = truncate(text, limit)
  81. if not isinstance(chunks, (list, tuple)) or not all(isinstance(c, str) for c in chunks):
  82. return self._split_text_chunks(text, limit, len_fn)
  83. return list(chunks)
  84. async def _send_fallback_final(self, text: str) -> None:
  85. """Send the final continuation after streaming edits stop working (one flood retry
  86. per chunk)."""
  87. if getattr(self, "_egress_declined", False):
  88. # The connector refused this destination earlier in the run. The
  89. # whole point of this path is to deliver the unseen tail as a NEW
  90. # message, which is exactly the re-addressing the egress guard
  91. # exists to stop.
  92. logger.warning(
  93. "suppressing the fallback continuation: the connector already "
  94. "declined this destination for this run"
  95. )
  96. return
  97. # Balance fences BEFORE computing the continuation so the closing fence
  98. # reaches the user even when only the tail is delivered.
  99. final_text = ensure_closed_code_fences(self._clean_for_display(text))
  100. continuation = self._continuation_text(final_text)
  101. self._fallback_final_send = False
  102. if not continuation.strip():
  103. continuation = await self._fallback_when_nothing_unseen(final_text)
  104. if continuation is None:
  105. return
  106. _len_fn, raw_limit = self._fallback_len_budget()
  107. chunks = self._split_text_chunks(continuation, max(500, raw_limit - 100), len_fn=_len_fn)
  108. stale_message_id = self._message_id # partial message to clean up
  109. last_message_id: Optional[str] = None
  110. last_successful_chunk = ""
  111. sent_any_chunk = False
  112. # Thread only a FULL resend (it replaces the preview); a tail continuation
  113. # keeps its existing unthreaded delivery on every platform.
  114. anchor = self._initial_reply_to_id if continuation == final_text else None
  115. for chunk in chunks:
  116. result = await self._send_with_flood_retry(
  117. content=chunk, reply_to=None if sent_any_chunk else anchor,
  118. retry_log="Flood control on fallback send, retrying in %.1fs")
  119. if not result or not result.success:
  120. # Partial continuation landed: do NOT set _final_response_sent (the
  121. # gateway must still deliver the full answer); _already_sent only
  122. # prevents a duplicate of the partial. Nothing landed: let the
  123. # gateway final send try once more.
  124. self._already_sent = sent_any_chunk
  125. self._message_id = last_message_id
  126. self._last_sent_text = last_successful_chunk
  127. self._fallback_prefix = ""
  128. return
  129. sent_any_chunk = True
  130. last_successful_chunk = chunk
  131. last_message_id = result.message_id or last_message_id
  132. self._notify_new_message()
  133. # Best-effort delete of the frozen partial — ONLY when the FULL final was
  134. # re-sent. If only the missing tail went out, the partial IS the head of
  135. # the answer ("sent only the second half" symptom).
  136. if (stale_message_id and stale_message_id != last_message_id
  137. and not self._fallback_preserve_partial_messages and continuation == final_text):
  138. await self._delete_previews([stale_message_id], label="Fallback partial",
  139. skip_sentinel=False)
  140. self._message_id = last_message_id
  141. self._already_sent = True
  142. # Recorder substitutes the unsplit ledger on a split turn.
  143. self._mark_final_delivered(record=final_text)
  144. self._last_sent_text = chunks[-1]
  145. self._fallback_prefix = ""
  146. self._fallback_preserve_partial_messages = False
  147. async def _fallback_when_nothing_unseen(self, final_text: str) -> Optional[str]:
  148. """Fallback entered but the visible prefix already covers ``final_text``: returns the
  149. continuation to send (the whole final when the prefix is from a *previous* segment)
  150. or None when the turn is settled here."""
  151. visible = self._visible_prefix()
  152. # Telegram clients can lose (part of) a streamed preview after a failed
  153. # final edit, so opt-in adapters commit a fresh final send.
  154. if (final_text.strip() and final_text == visible
  155. and getattr(self.adapter, "RESEND_FINAL_ON_EMPTY_STREAM_FALLBACK", False) is True):
  156. delivery = await self._send_empty_fallback_final(final_text)
  157. if delivery == "delivered":
  158. return None
  159. self._already_sent = True
  160. self._fallback_prefix = ""
  161. self._fallback_preserve_partial_messages = False
  162. # "ambiguous" (timeout: Telegram may have accepted) and "preview" (flood:
  163. # the complete ACKed preview is authoritative) keep dup suppression;
  164. # "failed" lets the gateway perform its normal final send.
  165. self._final_content_delivered = delivery in {"ambiguous", "preview"}
  166. if delivery == "preview":
  167. # This branch is only reached when the ACKed preview already shows the complete final text
  168. # (final_text == _visible_prefix()), so record it as the turn-final payload: the gateway's
  169. # reconciliation then confirms delivery instead of re-sending a second bubble next to the
  170. # never-deleted preview (#71047 Problem B).
  171. self._record_turn_final_payload(final_text)
  172. elif delivery == "ambiguous":
  173. self._delivery_ambiguous = True
  174. else:
  175. self._final_response_sent = False
  176. return None
  177. # The prefix may be from a *previous* segment (before a tool boundary),
  178. # wrongly reading as "already shown" — send final_text as-is.
  179. if final_text.strip() and final_text != visible:
  180. return final_text
  181. # Best-effort strip of a cursor left stuck by the edit failure.
  182. if (self._message_id and self._last_sent_text and self.cfg.cursor
  183. and self._last_sent_text.endswith(self.cfg.cursor)):
  184. clean_text = self._last_sent_text[:-len(self.cfg.cursor)]
  185. with contextlib.suppress(Exception):
  186. result = await self._edit_message(message_id=self._message_id, content=clean_text)
  187. if result.success:
  188. self._last_sent_text = clean_text
  189. self._already_sent = True
  190. # Recorder substitutes the full ledger on a split turn.
  191. self._mark_final_delivered(record=final_text)
  192. return None
  193. def _fallback_len_budget(self) -> tuple[Callable[[str], int], int]:
  194. """(len_fn, raw_limit) for fallback chunking — per-chat cap/unit on base adapters."""
  195. raw_limit = getattr(self.adapter, "MAX_MESSAGE_LENGTH", 4096)
  196. _len_fn: Callable[[str], int] = len
  197. if isinstance(self.adapter, _BasePlatformAdapter):
  198. _len_fn = self.adapter.message_len_fn
  199. try: # per-chat cap/unit (relay adapter fronting N platforms)
  200. raw_limit = self.adapter.max_message_length_for_chat(self.chat_id)
  201. _len_fn = self.adapter.message_len_fn_for_chat(self.chat_id)
  202. except Exception as e:
  203. logger.debug("per-chat limit resolution failed: %s", e)
  204. return _len_fn, raw_limit
  205. async def _send_with_flood_retry(self, *, content: str, retry_log: str, reply_to=None):
  206. """adapter.send(final metadata) with ONE bounded flood retry; returns the last
  207. SendResult. Exceptions propagate (callers decide whether a raise is "ambiguous")."""
  208. kwargs = dict(chat_id=self.chat_id, content=content,
  209. metadata=self._metadata_for_send(final=True))
  210. if reply_to is not None:
  211. kwargs["reply_to"] = reply_to
  212. result = None
  213. for attempt in range(2):
  214. result = await self.adapter.send(**kwargs)
  215. if getattr(result, "success", False):
  216. break
  217. retry_delay = self._fallback_flood_retry_delay(result)
  218. if attempt or retry_delay is None:
  219. break # non-flood error, long flood wait, or second failure
  220. raw = getattr(result, "raw_response", None)
  221. if isinstance(raw, dict) and raw.get("partial_overflow"):
  222. break # split head already on screen: re-sending the whole content duplicates it
  223. logger.debug(retry_log, retry_delay)
  224. await asyncio.sleep(retry_delay)
  225. return result
  226. async def _send_empty_fallback_final(self, final_text: str) -> str:
  227. """Commit a completed answer after Telegram finalization fails: "delivered", "failed"
  228. (gateway may retry), "ambiguous" (a timeout may have landed) or "preview" (flood
  229. control; the complete preview is authoritative)."""
  230. # Segment-scoped only: never delete an earlier finalized preamble.
  231. stale_ids = self._stale_preview_ids(segment_only=True)
  232. try:
  233. result = await self._send_with_flood_retry(
  234. content=final_text, reply_to=self._initial_reply_to_id,
  235. retry_log="Flood control on empty fallback final send; retrying in %.1fs")
  236. except Exception as exc:
  237. logger.debug("Empty fallback final send failed: %s", exc)
  238. return "ambiguous" if self._send_failure_may_have_delivered(exc) else "failed"
  239. if not getattr(result, "success", False):
  240. if self._is_flood_error(result):
  241. return "preview"
  242. return "ambiguous" if self._send_failure_may_have_delivered(result) else "failed"
  243. new_message_id = getattr(result, "message_id", None)
  244. # Telegram reports delete failure by returning False; the flood window that
  245. # broke the finalize can reject this too — one bounded retry.
  246. await self._delete_previews(stale_ids, skip=new_message_id, label="Empty fallback",
  247. retry_on_false=True)
  248. self._segment_preview_message_ids = set()
  249. self._message_id = new_message_id or "__no_edit__"
  250. self._already_sent = True
  251. self._mark_final_delivered()
  252. # Record VERBATIM, not via _record_turn_final_payload: the sealed previews
  253. # were just deleted, so the ledger (still holding sealed heads) would claim
  254. # delivery for text this path removed.
  255. self._delivered_final_text = self._display_payload(final_text)
  256. self._last_sent_text = final_text
  257. self._fallback_prefix = ""
  258. self._fallback_preserve_partial_messages = False
  259. self._notify_new_message()
  260. return "delivered"
  261. @staticmethod
  262. def _send_failure_may_have_delivered(result_or_exc: Any) -> bool:
  263. """Return True for timeout failures where retrying may duplicate."""
  264. if getattr(result_or_exc, "retryable", None) is True:
  265. return False
  266. error = str(getattr(result_or_exc, "error", None) or result_or_exc).lower()
  267. name = result_or_exc.__class__.__name__.lower()
  268. return "timeout" in error or "timed out" in error or "timeout" in name
  269. def _fallback_flood_retry_delay(self, result: Any) -> float | None:
  270. """Return a bounded retry delay for a fallback send, if safe to retry."""
  271. if not self._is_flood_error(result):
  272. return None
  273. try:
  274. delay = float(getattr(result, "retry_after", None) or 3.0)
  275. except (TypeError, ValueError):
  276. delay = 3.0
  277. if delay > self._max_fallback_flood_retry_seconds:
  278. logger.debug("Flood control requests %.1fs; leaving final delivery to the gateway",
  279. delay)
  280. return None
  281. return max(0.0, delay)
  282. def _is_flood_error(self, result) -> bool:
  283. """Check if a SendResult failure is due to flood control / rate limiting."""
  284. err_lower = (getattr(result, "error", "") or "").lower()
  285. return "flood" in err_lower or "retry after" in err_lower or "rate" in err_lower
  286. async def _flush_segment_tail_on_edit_failure(self) -> None:
  287. """Before a segment reset, send the unseen tail as a new message (and best-effort
  288. strip the stuck cursor from the partial)."""
  289. if getattr(self, "_egress_declined", False):
  290. return # a new message is exactly the re-addressing the egress guard refused
  291. if not self._fallback_final_send:
  292. await self._try_strip_cursor()
  293. visible = self._fallback_prefix or self._visible_prefix()
  294. # Clean the whole buffer before cutting: a trailing silence marker cut off on its own
  295. # would read as a bare marker and survive the cleaner.
  296. tail = self._clean_for_display(self._accumulated)
  297. if visible and tail.startswith(visible):
  298. tail = tail[len(visible):].lstrip()
  299. if not tail.strip():
  300. return
  301. try:
  302. # Interim: must never seal a native stream (see _send_commentary).
  303. _md = dict(self.metadata) if self.metadata else {}
  304. _md["_interim_send"] = True
  305. result = await self.adapter.send(chat_id=self.chat_id, content=tail, metadata=_md)
  306. if result.success:
  307. self._already_sent = True
  308. except Exception as e:
  309. logger.error("Segment-break tail flush error: %s", e)
  310. async def _try_strip_cursor(self) -> None:
  311. """Best-effort edit removing a stuck cursor when entering fallback mode."""
  312. prefix = self._visible_prefix()
  313. if not self._has_real_preview() or not prefix.strip():
  314. return
  315. with contextlib.suppress(Exception): # never block the fallback path
  316. result = await self._edit_message(message_id=self._message_id, content=prefix)
  317. if getattr(result, "success", False):
  318. self._last_sent_text = prefix
  319. async def _send_commentary(self, text: str) -> bool:
  320. """Send a completed interim assistant commentary message."""
  321. text = self._clean_for_display(text)
  322. if not text.strip():
  323. return False
  324. try:
  325. # Interim: a stream-is-the-message adapter's seal-interception must not
  326. # turn this into draft(final=true), which would seal the live stream
  327. # with interim text and orphan the true final.
  328. _md = self._metadata_for_send(final=False) or {}
  329. _md["_interim_send"] = True
  330. # reply_to only for reply-anchored threading; Discord/Telegram use
  331. # thread_id metadata and reply_to on every commentary is spam.
  332. _plat = getattr(getattr(self.adapter, "platform", None), "value", None)
  333. _platform_name = str(_plat or getattr(self.adapter, "name", "")).lower()
  334. _needs_reply_anchor = _platform_name in ("buzz", "slack", "mattermost", "feishu")
  335. result = await self.adapter.send(
  336. chat_id=self.chat_id, content=text,
  337. reply_to=self._initial_reply_to_id if _needs_reply_anchor else None, metadata=_md)
  338. # Do NOT set _already_sent: commentary is interim, and the flag would
  339. # suppress the real final after multiple tool calls.
  340. if result.success:
  341. self._notify_new_message()
  342. # Lets run.py confirm whether an interim send carried the final.
  343. # Record the exact delivered text so run.py can confirm whether an interim "preview"
  344. # actually carried the final response, vs. unrelated commentary delivered during a session
  345. # split (#14238).
  346. self._delivered_commentary_texts.append(text)
  347. return result.success
  348. except Exception as e:
  349. logger.error("Commentary send error: %s", e)
  350. return False
  351. def _raw_message_limit(self) -> int:
  352. """Per-chat length budget (``message_len_fn`` units) before overflow splits; rich
  353. adapters may raise it via ``streaming_overflow_limit`` so a reply that fits one rich
  354. message isn't fragmented at the edit limit."""
  355. base = getattr(self.adapter, "MAX_MESSAGE_LENGTH", 4096)
  356. # isinstance gate keeps MagicMock adapters (mock attrs, not ints) on base.
  357. if isinstance(self.adapter, _BasePlatformAdapter):
  358. try:
  359. base = self.adapter.max_message_length_for_chat(self.chat_id)
  360. except Exception as e:
  361. logger.debug("max_message_length_for_chat failed: %s", e)
  362. try:
  363. cap = self.adapter.streaming_overflow_limit()
  364. except Exception as e:
  365. logger.debug("streaming_overflow_limit check failed: %s", e)
  366. cap = None
  367. if isinstance(cap, int) and cap > base:
  368. return cap
  369. return base
  370. # Fresh send carried exactly ``text`` — record it so the gateway can reconcile the flag against the
  371. # completed response (#71643/#95382 content-vs-flag contract).
  372. async def _suppress_silence_marker(self) -> None:
  373. """Retract any streamed preview when the final reply is a bare silence marker. Flags
  374. stay False so the gateway's whole-response filter owns what goes out next: "" for a
  375. machinery turn, the visible fallback for a human one."""
  376. # A native-stream bubble isn't a deletable message — close an open one
  377. # (e.g. from an eager re-seed) with an empty finalize so it doesn't hang.
  378. if self._native_stream_opened:
  379. await self._close_empty_native_bubble("Silence-marker native stream close failed: %s")
  380. await self._delete_previews(self._stale_preview_ids(), label="Silence-marker")
  381. self._preview_message_ids = set()
  382. self._message_id = None
  383. self._accumulated = self._stream_ledger = self._last_sent_text = ""
  384. self._already_sent = False
  385. self._clear_turn_final_flags()
  386. logger.info("Suppressed streamed intentional-silence marker (chat=%s)", self.chat_id)