The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(telegram): build PerChatUpdateProcessor on SimpleUpdateProcessor
kshitijk4poor03395f9de713
diff --git a/plugins/platforms/telegram/update_admission.py b/plugins/platforms/telegram/update_admission.pyindex 5e6bbb02246..4557902afe3 100644--- a/plugins/platforms/telegram/update_admission.py+++ b/plugins/platforms/telegram/update_admission.py@@ -17,7 +17,7 @@ from dataclasses import dataclassfrom functools import wrapsfrom telegram import Update-from telegram.ext import Application, ApplicationHandlerStop, BaseUpdateProcessor, ConversationHandler+from telegram.ext import Application, ApplicationHandlerStop, ConversationHandler, SimpleUpdateProcessorfrom gateway.platforms.helpers import bounded_putfrom utils import atomic_json_write@@ -125,7 +125,7 @@ class _ErrorCallback:return await self.__wrapped__(update, context)-class PerChatUpdateProcessor(BaseUpdateProcessor):+class PerChatUpdateProcessor(SimpleUpdateProcessor):"""Concurrent across chats, FIFO within one chat.PTB's default processor (max_concurrent_updates=1) awaits every update inline, so one slow@@ -165,16 +165,10 @@ class PerChatUpdateProcessor(BaseUpdateProcessor):else:prev.add_done_callback(release)- async def initialize(self) -> None:- pass-- async def shutdown(self) -> None:- pass-def build_update_processor(extra, name: str) -> PerChatUpdateProcessor:"""``platforms.telegram.extra.max_concurrent_updates`` (default 32); bad values warn and fall back."""- raw = (extra or {}).get("max_concurrent_updates", DEFAULT_MAX_CONCURRENT_UPDATES)+ raw = extra.get("max_concurrent_updates", DEFAULT_MAX_CONCURRENT_UPDATES)try:limit = int(raw)except (TypeError, ValueError):diff --git a/tests/gateway/conftest.py b/tests/gateway/conftest.pyindex a8ffb5bd934..bdffb2ba820 100644--- a/tests/gateway/conftest.py+++ b/tests/gateway/conftest.py@@ -166,11 +166,11 @@ def _ensure_telegram_mock() -> None:# PerChatUpdateProcessor subclasses this at import time: a MagicMock base# would turn the subclass itself into a mock that fails on its second call.- class BaseUpdateProcessor:+ class SimpleUpdateProcessor:def __init__(self, max_concurrent_updates):self.max_concurrent_updates = max_concurrent_updates- mod.BaseUpdateProcessor = BaseUpdateProcessor+ mod.SimpleUpdateProcessor = SimpleUpdateProcessorfor name in ("telegram",diff --git a/tests/plugins/test_telegram_update_admission.py b/tests/plugins/test_telegram_update_admission.pyindex 85d3b9edeb3..0f74738512c 100644--- a/tests/plugins/test_telegram_update_admission.py+++ b/tests/plugins/test_telegram_update_admission.py@@ -22,6 +22,7 @@ from gateway.config import PlatformConfigfrom gateway.platforms.event import MessageTypefrom plugins.platforms.telegram import adapter as tg_adapterfrom plugins.platforms.telegram.adapter import TelegramAdapter+from plugins.platforms.telegram.update_admission import DEFAULT_MAX_CONCURRENT_UPDATES, PerChatUpdateProcessorclass NoNetwork(BaseRequest):@@ -800,14 +801,14 @@ async def test_connect_builds_concurrent_update_processor(monkeypatch):update deafens every other chat (and local commands) until it finishes. The adaptermust enable a bounded concurrent processor, overridable via config extra (#125098)."""async with connected(monkeypatch) as (adapter, app, delivered):- assert app.concurrent_updates > 1- assert app._update_processor.max_concurrent_updates == app.concurrent_updates+ assert isinstance(app.update_processor, PerChatUpdateProcessor)+ assert app.concurrent_updates == DEFAULT_MAX_CONCURRENT_UPDATESasync with connected(monkeypatch, extra={"max_concurrent_updates": 7}) as (adapter, app, delivered):assert app.concurrent_updates == 7async with connected(monkeypatch, extra={"max_concurrent_updates": "lots"}) as (adapter, app, delivered):- assert app.concurrent_updates == 32+ assert app.concurrent_updates == DEFAULT_MAX_CONCURRENT_UPDATES# Cancelling a waiting same-chat update must neither fail its running predecessor nor# let the next update of that chat overtake it.