refactor(gateway): text-batch flush lives on BasePlatformAdapter; shield + cancel-race fixes reach all 8 adapters

Eight adapters (discord, telegram, wecom, matrix, whatsapp, simplex, feishu, weixin) each kept a
copy of the delayed text-batch flush that base._enqueue_text_event schedules. Two correctness
fixes had landed in single copies only: Discord's asyncio.shield around the dispatch (#12444 —
a late chunk cancelling the flush task aborted the in-flight agent turn) and WeCom/Weixin's
synchronous task-identity check before the pop (a superseded task waking late popped the event
and the successor found nothing). The other adapters carried both bugs latent.

The base now owns `_flush_text_batch` with both fixes, plus the `_pending_text_batches` /
`_pending_text_batch_tasks` dicts and `_SPLIT_THRESHOLD` / delay attrs (defaults; adapters set
their own). Platform policy goes through three small hooks instead of a copied body:
`_text_batch_delay_for(pending)` (Telegram's fast/short tiers, WeCom's attachment-only wait),
`_pop_text_batch(key)` (Feishu's side count table) and `_dispatch_text_batch(event)` (Feishu's
per-chat lock). Telegram keeps its `_flush_buffered` body because its contract differs on
purpose — a cancel after the pop must hold-and-re-raise so teardown can stop a flush — and gains
the identity check there. Matrix's `_split_threshold` is renamed to the shared `_SPLIT_THRESHOLD`;
SimpleX exposes its single delay through the shared attr names.

`helpers.TextBatchAggregator` (zero users) sits inside the revert-scheduled PLUGIN-COMPAT block
and is left for that revert.
This commit is contained in:
teknium1
2026-09-12 19:49:52 -07:00
committed by Teknium
parent 74a315bd32
commit aac36fd96f
12 changed files with 193 additions and 165 deletions
+4 -14
View File
@@ -2866,21 +2866,11 @@ class FeishuAdapter(BasePlatformAdapter):
prior_task.cancel()
task_map[key] = asyncio.create_task(flush_fn(key))
async def _flush_text_batch(self, key: str) -> None:
"""Flush after the quiet period; wait longer when the last chunk sits near Feishu's ~4096-char split."""
pending = self._pending_text_batches.get(key)
last_len = getattr(pending, "_last_chunk_len", 0) if pending else 0
near_split = last_len >= self._SPLIT_THRESHOLD # a continuation chunk is almost certain
delay = self._text_batch_split_delay_seconds if near_split else self._text_batch_delay_seconds
await self._delayed_flush(self._pending_text_batch_tasks, key, delay, self._flush_text_batch_now)
async def _flush_text_batch_now(self, key: str) -> None:
"""Dispatch the current text batch immediately."""
event = self._pending_text_batches.pop(key, None)
def _pop_text_batch(self, key: str) -> Optional[MessageEvent]:
self._pending_text_batch_counts.pop(key, None)
if not event:
return
logger.info("[Feishu] Flushing text batch %s (%d chars)", key, len(event.text or ""))
return self._pending_text_batches.pop(key, None)
async def _dispatch_text_batch(self, event: MessageEvent) -> None:
await self._handle_message_with_guards(event)
# --- Message content extraction and resource download ---