The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(telegram): network error on a resolved-prompt edit keeps the prompt
Teknium44678e564732
diff --git a/plugins/platforms/telegram/telegram_resolved_prompt.py b/plugins/platforms/telegram/telegram_resolved_prompt.pyindex 3a243abd26d..f8098ea9590 100644--- a/plugins/platforms/telegram/telegram_resolved_prompt.py+++ b/plugins/platforms/telegram/telegram_resolved_prompt.py@@ -8,18 +8,27 @@ rendering (``Message.text_html`` rebuilds it from the entities, code block inclufrom __future__ import annotations-import contextlibimport html+import loggingfrom typing import Any+logger = logging.getLogger(__name__)+async def edit_keeping_prompt(adapter: Any, query: Any, decision: str) -> None:"""Append *decision* to the prompt and drop its buttons; the adapter's decision-only edit runs whenthe prompt has no readable body or the appended line pushes it past Telegram's length cap."""+ from telegram.error import BadRequest, TelegramError+body = getattr(query.message, "text_html", None) if query.message else Noneif isinstance(body, str) and body:body = body.replace(adapter._ea_deadline_line(), "") # same escaping as text_html- with contextlib.suppress(Exception):+ try:await query.edit_message_text(text=f"{body}\n\n— {html.escape(decision)}", parse_mode="HTML", reply_markup=None)return+ except BadRequest: # too long / unparsable: the decision-only edit below still clears the buttons+ pass+ except TelegramError as exc: # network/timeout: the edit may have landed, so keep the prompt as is+ logger.debug("Telegram resolved-prompt edit failed: %s", exc)+ returnawait adapter._edit_md_quiet(query, decision)diff --git a/tests/gateway/test_telegram_approval_buttons.py b/tests/gateway/test_telegram_approval_buttons.pyindex b8654472b24..d6a0406a15e 100644--- a/tests/gateway/test_telegram_approval_buttons.py+++ b/tests/gateway/test_telegram_approval_buttons.py@@ -363,6 +363,7 @@ async def test_resolved_approval_keeps_the_request_and_appends_the_decision():"""The resolved prompt keeps its own rendering (command + reason, minus the moot deadline line) with thedecision appended; a prompt the appended line pushes past the cap falls back to the decision aloneinstead of keeping live buttons."""+ from telegram.error import BadRequestfor edit_fails in (False, True):adapter = _make_adapter()adapter._approval_state[7] = "agent:main:telegram:group:12345:99"@@ -373,7 +374,7 @@ async def test_resolved_approval_keeps_the_request_and_appends_the_decision():prompt_html = "<b>Dangerous command</b>\n<pre>rm -rf ./build && ls</pre>\nReason: delete"query.message.text_html = prompt_html + adapter._ea_deadline_line() # moot once decided: droppedquery.from_user = MagicMock(first_name="Ana & Bo", id="12345")- query.edit_message_text = AsyncMock(side_effect=[RuntimeError("Message is too long"), None] if edit_fails else None)+ query.edit_message_text = AsyncMock(side_effect=[BadRequest("Message is too long"), None] if edit_fails else None)with patch.dict(os.environ, {"TELEGRAM_ALLOWED_USERS": "*"}, clear=False), \patch("tools.approval.resolve_gateway_approval", return_value=1):await adapter._handle_callback_query(MagicMock(callback_query=query), MagicMock())@@ -385,3 +386,21 @@ async def test_resolved_approval_keeps_the_request_and_appends_the_decision():assert final["parse_mode"] == "HTML"assert final["text"].startswith(prompt_html + "\n\n— ")assert "Ana & Bo" in final["text"]+++async def test_resolved_approval_network_error_does_not_overwrite_the_prompt():+ """Only a rejected edit falls back to the decision-only text; a network error after an edit that may+ have landed must not replace the kept prompt."""+ from telegram.error import TimedOut+ adapter = _make_adapter()+ adapter._approval_state[7] = "agent:main:telegram:group:12345:99"+ query = AsyncMock()+ query.data = "ea:once:7"+ query.message = MagicMock(chat_id=12345, text_html="<b>Dangerous command</b>\n<pre>rm -rf ./build</pre>")+ query.from_user = MagicMock(first_name="Ana", id="12345")+ query.edit_message_text = AsyncMock(side_effect=[TimedOut("timed out"), None])+ with patch.dict(os.environ, {"TELEGRAM_ALLOWED_USERS": "*"}, clear=False), \+ patch("tools.approval.resolve_gateway_approval", return_value=1):+ await adapter._handle_callback_query(MagicMock(callback_query=query), MagicMock())+ assert query.edit_message_text.await_count == 1