The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(telegram): push approval prompts instead of silently delivering them
liuhao1024e26d4cb3e342
diff --git a/gateway/run_turn_runner.py b/gateway/run_turn_runner.pyindex a587e2e67a6..00aeb82ac22 100644--- a/gateway/run_turn_runner.py+++ b/gateway/run_turn_runner.py@@ -1475,7 +1475,11 @@ class TurnRunner:fut = self._schedule(adapter.send_exec_approval(chat_id=ctx._status_chat_id, command=cmd, session_key=ctx.session_key or "",- description=desc, metadata=ctx._status_thread_metadata, **flags,+ description=desc,+ # Push, don't silently deliver (#132516): a card that lands with+ # disable_notification=True is indistinguishable from "no prompt" and+ # burns the whole approval window. Mirrors the cron-delivery convention.+ metadata={**(ctx._status_thread_metadata or {}), "notify": True}, **flags,),"send_exec_approval scheduling error",)@@ -1536,8 +1540,9 @@ class TurnRunner:# in Slack threads and reserved by Matrix clients.msg = _format_exec_approval_fallback(cmd, desc, getattr(adapter, "typed_command_prefix", "/"), **flags)try:- # Mark as approval prompt so WeCom routes through the control lane.- metadata = {**(ctx._status_thread_metadata or {}), "is_approval_prompt": True}+ # Mark as approval prompt so WeCom routes through the control lane; notify=True so+ # "important"-mode Telegram pushes instead of silently delivering the prompt (#132516).+ metadata = {**(ctx._status_thread_metadata or {}), "notify": True, "is_approval_prompt": True}fut = self._schedule(adapter.send(ctx._status_chat_id, msg, metadata=_interim_metadata(metadata)), "Approval text-send scheduling error",)diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.pyindex 9bfa5b5af5d..8ba2b4eef33 100644--- a/plugins/platforms/telegram/adapter.py+++ b/plugins/platforms/telegram/adapter.py@@ -889,8 +889,14 @@ class TelegramAdapter(BasePlatformAdapter):self._schedule_held_inbound_redispatch()def _notification_kwargs(self, metadata: Optional[Dict[str, Any]]) -> Dict[str, Any]:- """In "important" mode return disable_notification=True unless ``metadata["notify"]``."""- if getattr(self, "_notifications_mode", "important") != "important" or (metadata or {}).get("notify"):+ """In "important" mode return disable_notification=True unless ``metadata["notify"]``.++ An approval prompt never counts as "unimportant": silently delivered prompts are+ indistinguishable from "no prompt" and cost the full approval window before the+ command is refused (#132516)."""+ meta = metadata or {}+ if (getattr(self, "_notifications_mode", "important") != "important"+ or meta.get("notify") or meta.get("is_approval_prompt")):return {}return {"disable_notification": True}diff --git a/tests/gateway/test_exec_approval_timeout_notice.py b/tests/gateway/test_exec_approval_timeout_notice.pyindex 952a25fb914..f222af5ff2c 100644--- a/tests/gateway/test_exec_approval_timeout_notice.py+++ b/tests/gateway/test_exec_approval_timeout_notice.py@@ -30,16 +30,20 @@ class _ButtonAdapter:def __init__(self, *, editable: bool = True) -> None:self.sends: List[str] = []self.edits: List[tuple] = []+ self.card_metadata: List[Any] = []+ self.send_metadata: List[Any] = []self._editable = editabledef pause_typing_for_chat(self, chat_id: str) -> None:return Noneasync def send_exec_approval(self, *a: Any, **k: Any) -> SendResult:+ self.card_metadata.append(k.get("metadata"))return SendResult(success=True, message_id="card-1")async def send(self, chat_id: str, message: str, **k: Any) -> SendResult:self.sends.append(message)+ self.send_metadata.append(k.get("metadata"))return SendResult(success=True, message_id="m2")async def edit_message(self, chat_id: str, message_id: str, content: str, **k: Any) -> SendResult:@@ -47,6 +51,12 @@ class _ButtonAdapter:return SendResult(success=self._editable, error=None if self._editable else "cannot edit")+class _PlainAdapter(_ButtonAdapter):+ """No native buttons (``send_exec_approval is None`` per-class): plain-text fallback."""++ send_exec_approval = None++def _runner(adapter):from gateway.run_turn_runner import TurnRunner@@ -113,3 +123,24 @@ def test_other_settle_reasons_post_nothing(pending_entry, reason):pending_entry.settle(reason)assert adapter.edits == [] and adapter.sends == []+++def test_button_prompt_metadata_carries_notify(pending_entry):+ """#132516: the button card send must carry notify=True — a card that lands with+ disable_notification=True (Telegram "important" mode) is indistinguishable from+ "no prompt" and burns the whole approval window before failing closed."""+ adapter = _ButtonAdapter()+ _runner(adapter)._approval_notify_sync(dict(pending_entry.data))++ assert adapter.card_metadata == [{"thread_id": "t1", "notify": True}]+++def test_text_fallback_metadata_carries_notify_and_approval_marker(pending_entry):+ """#132516: the plain-text fallback must carry notify=True (push, don't silently+ deliver) plus the is_approval_prompt marker WeCom's control lane keys on."""+ adapter = _PlainAdapter()+ _runner(adapter)._approval_notify_sync(dict(pending_entry.data))++ assert adapter.send_metadata == [+ {"thread_id": "t1", "notify": True, "is_approval_prompt": True, "_interim_send": True}]+ assert adapter.send_exec_approval is None # really the fallback lanediff --git a/tests/plugins/platforms/telegram/test_notification_kwargs.py b/tests/plugins/platforms/telegram/test_notification_kwargs.pynew file mode 100644index 00000000000..e23e2cf4f4c--- /dev/null+++ b/tests/plugins/platforms/telegram/test_notification_kwargs.py@@ -0,0 +1,41 @@+"""Telegram notification-mode gating for approval prompts (#132516).++In "important" mode every send carries disable_notification=True unless the metadata+opts out. An approval prompt is the one payload that must ALWAYS opt out: a silently+delivered prompt is indistinguishable from "no prompt" and costs the full+approvals.timeout before the command is refused.+"""++from plugins.platforms.telegram.adapter import TelegramAdapter+++def _adapter(mode):+ adapter = object.__new__(TelegramAdapter)+ adapter._notifications_mode = mode+ return adapter+++def test_important_mode_silences_plain_sends():+ assert _adapter("important")._notification_kwargs({"thread_id": "t1"}) == {+ "disable_notification": True}+++def test_important_mode_pushes_notify_marked_sends():+ assert _adapter("important")._notification_kwargs({"notify": True}) == {}+++def test_important_mode_pushes_approval_prompts():+ """#132516: is_approval_prompt must defeat disable_notification even without an+ explicit notify marker — covers every surface that marks the prompt."""+ assert _adapter("important")._notification_kwargs({"is_approval_prompt": True}) == {}+ assert _adapter("important")._notification_kwargs(+ {"thread_id": "t1", "is_approval_prompt": True}) == {}+++def test_none_metadata_in_important_mode_stays_silent_for_plain_sends():+ assert _adapter("important")._notification_kwargs(None) == {"disable_notification": True}+++def test_all_mode_never_disables_notifications():+ for metadata in (None, {}, {"is_approval_prompt": True}, {"notify": False}):+ assert _adapter("all")._notification_kwargs(metadata) == {}