The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(telegram): hop only the menu build off the loop; one invariant test covers both menu sites
teknium1660d4b8d8700
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.pyindex b7d48b29424..773722d3c21 100644--- a/plugins/platforms/telegram/adapter.py+++ b/plugins/platforms/telegram/adapter.py@@ -39,19 +39,6 @@ def _redact_telegram_error_text(error: object) -> str:return "<telegram error redacted>"-def _build_telegram_command_menu() -> tuple[list[tuple[str, str]], int, int]:- """Build the Telegram menu off the gateway event loop.-- Skill discovery resolves configured paths and can touch a slow filesystem, so it- must not run inline with reconnect housekeeping.- """- from hermes_cli.commands_platforms import telegram_menu_commands, telegram_menu_max_commands-- max_commands = telegram_menu_max_commands()- menu_commands, hidden_count = telegram_menu_commands(max_commands=max_commands)- return menu_commands, hidden_count, max_commands--def _consume_abandoned_task(task: asyncio.Task) -> None:"""Observe a detached task's terminal exception to avoid noisy loop logs."""try:@@ -2599,10 +2586,14 @@ class TelegramAdapter(BasePlatformAdapter):"""Register the command menu (from COMMAND_REGISTRY) in every scope — Telegram picks thenarrowest matching one per chat type; forum topics are handled lazily by _ensure_forum_commands."""from telegram import BotCommand, BotCommandScopeAllPrivateChats, BotCommandScopeAllGroupChats, BotCommandScopeDefault+ from hermes_cli.commands_platforms import telegram_menu_commands, telegram_menu_max_commandsif not self._bot:return# Telegram allows 100 commands but has an undocumented ~4KB payload limit; default cap 60.- menu_commands, hidden_count, max_commands = await asyncio.to_thread(_build_telegram_command_menu)+ max_commands = telegram_menu_max_commands()+ # Skill discovery resolves every skill path on disk; a slow filesystem after a reconnect must+ # not hold the gateway loop past the liveness watchdog (#110707). Only the Bot API call stays here.+ menu_commands, hidden_count = await asyncio.to_thread(telegram_menu_commands, max_commands=max_commands)bot_commands = [BotCommand(name, desc) for name, desc in menu_commands]for scope_cls in (BotCommandScopeDefault, BotCommandScopeAllPrivateChats, BotCommandScopeAllGroupChats):scope_name = getattr(scope_cls, "__name__", str(scope_cls))@@ -5895,7 +5886,8 @@ class TelegramAdapter(BasePlatformAdapter):returnfrom telegram import BotCommand, BotCommandScopeChatfrom hermes_cli.commands_platforms import telegram_menu_commands, telegram_menu_max_commands- menu_commands, _ = telegram_menu_commands(max_commands=telegram_menu_max_commands())+ menu_commands, _ = await asyncio.to_thread(+ telegram_menu_commands, max_commands=telegram_menu_max_commands())bot_commands = [BotCommand(name, desc) for name, desc in menu_commands]await self._bot.set_my_commands(bot_commands, scope=BotCommandScopeChat(chat_id=chat_id))self._forum_command_registered.add(chat_id)diff --git a/tests/gateway/test_telegram_forum_commands.py b/tests/gateway/test_telegram_forum_commands.pyindex 902d05b0b91..ee2ec61e538 100644--- a/tests/gateway/test_telegram_forum_commands.py+++ b/tests/gateway/test_telegram_forum_commands.py@@ -86,45 +86,35 @@ async def test_ensure_forum_commands_race_safety():assert adapter._bot.set_my_commands.await_count == 1-async def test_register_command_menu_builds_skill_menu_off_event_loop(monkeypatch):- """A slow skill scan must not starve Telegram's reconnect event loop."""- import telegram-- adapter = _make_test_adapter()+async def _menu_build_leaves_loop_free(adapter, run_site):+ """Drive ``run_site`` while the menu builder blocks; return whether the loop kept ticking."""scan_started = threading.Event()- release_scan = threading.Event()- loop_progressed = threading.Event()- observed = {}+ loop_ticked = threading.Event()def _blocking_menu(*, max_commands):- assert max_commands == 60scan_started.set()- release_scan.wait(timeout=1)- observed["loop_progressed"] = loop_progressed.is_set()- return [("help", "Show help"), ("skill", "Run a skill")], 0-- monkeypatch.setattr("hermes_cli.commands_platforms.telegram_menu_max_commands", lambda: 60)- monkeypatch.setattr("hermes_cli.commands_platforms.telegram_menu_commands", _blocking_menu)- monkeypatch.setattr(- telegram, "BotCommand", lambda command, description: SimpleNamespace(command=command, description=description))- # This releases a pre-fix synchronous scan so the regression test cannot hang the suite.- safety_release = threading.Timer(0.2, release_scan.set)- safety_release.start()- task = asyncio.create_task(adapter._register_command_menu())- try:- await asyncio.to_thread(scan_started.wait)- loop_progressed.set()- release_scan.set()+ return ([("help", "Show help")], 0) if loop_ticked.wait(timeout=1) else ([], 0)++ with patch("hermes_cli.commands_platforms.telegram_menu_commands", _blocking_menu), \+ patch("hermes_cli.commands_platforms.telegram_menu_max_commands", lambda: 60), \+ patch("telegram.BotCommand", lambda c, d: SimpleNamespace(command=c, description=d)), \+ patch("telegram.BotCommandScopeChat", lambda chat_id: SimpleNamespace(chat_id=chat_id)):+ task = asyncio.create_task(run_site())+ await asyncio.to_thread(scan_started.wait, 1)+ # This line only runs while the builder is still inside the wait if the loop is free.+ loop_ticked.set()await task- finally:- release_scan.set()- safety_release.cancel()- if not task.done():- task.cancel()- await task-- assert observed["loop_progressed"] is True- assert adapter._bot.set_my_commands.await_count == 3- for call in adapter._bot.set_my_commands.await_args_list:- assert [command.command for command in call.args[0]] == ["help", "skill"]+ return all(+ [c.command for c in call.args[0]] == ["help"] for call in adapter._bot.set_my_commands.await_args_list+ ) and adapter._bot.set_my_commands.await_count > 0+++async def test_command_menu_skill_scan_runs_off_the_event_loop():+ """A slow skill scan (#110707) must not block the gateway loop from either menu site:+ post-connect registration and lazy forum registration on an inbound message."""+ adapter = _make_test_adapter()+ assert await _menu_build_leaves_loop_free(adapter, adapter._register_command_menu)+ adapter = _make_test_adapter()+ forum_msg = _forum_message(chat_id=-123, is_forum=True)+ assert await _menu_build_leaves_loop_free(adapter, lambda: adapter._ensure_forum_commands(forum_msg))