The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(telegram): send unanchored when a reply anchor is not a message id (#139611)
PRATHAMESH7529cda7a5ef28
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.pyindex b11c2bdb65b..2b31399da10 100644--- a/plugins/platforms/telegram/adapter.py+++ b/plugins/platforms/telegram/adapter.py@@ -1004,10 +1004,24 @@ class TelegramAdapter(TelegramHeldInboundMixin, BasePlatformAdapter):topic_id = (metadata or {}).get("direct_messages_topic_id") or (metadata or {}).get("telegram_direct_messages_topic_id")return str(topic_id) if topic_id is not None else None+ @staticmethod+ def _telegram_message_id(value: Any) -> Optional[int]:+ """A reply anchor as a Telegram message id, or None when it is not one.++ Anchors are opaque event ids upstream: a route wake (kanban-wake, loop-tick) is admitted with a+ synthetic JSON identity (``session_automation.admit_route_wake``), and ``_reply_anchor_for_event``+ hands it to the send verbatim. Raw ``int()`` on that raised ValueError and failed every reply into+ the session (#139611); an unanchored reply is strictly better than a failed send."""+ if isinstance(value, bool):+ return None+ if isinstance(value, int):+ return value if value > 0 else None+ text = str(value).strip() if value is not None else ""+ return int(text) if text.isascii() and text.isdigit() and int(text) > 0 else None+@classmethoddef _metadata_reply_to_message_id(cls, metadata: Optional[dict[str, Any]]) -> Optional[int]:- reply_to = (metadata or {}).get("telegram_reply_to_message_id")- return int(reply_to) if reply_to is not None else None+ return cls._telegram_message_id((metadata or {}).get("telegram_reply_to_message_id"))@staticmethoddef _dm_topic_fallback(metadata: Optional[dict[str, Any]]) -> bool:@@ -1029,8 +1043,9 @@ class TelegramAdapter(TelegramHeldInboundMixin, BasePlatformAdapter):@classmethoddef _reply_to_message_id_for_send(cls, reply_to: Optional[str], metadata: Optional[dict[str, Any]] = None, reply_to_mode: Optional[str] = None) -> Optional[int]:- if reply_to:- return int(reply_to)+ anchor = cls._telegram_message_id(reply_to)+ if anchor is not None:+ return anchorif cls._dm_topic_fallback(metadata) and reply_to_mode != "off":return cls._metadata_reply_to_message_id(metadata)return None@@ -1428,7 +1443,9 @@ class TelegramAdapter(TelegramHeldInboundMixin, BasePlatformAdapter):metadata_reply_to = self._metadata_reply_to_message_id(metadata)private_dm_topic_send = self._is_private_dm_topic_send(chat_id, thread_id, metadata)dm_topic_reply_to_off = private_dm_topic_send and self._reply_to_mode == "off" and self._dm_topic_fallback(metadata)- reply_to_source = reply_to or (str(metadata_reply_to) if private_dm_topic_send and metadata_reply_to is not None else None)+ explicit_reply_to = self._telegram_message_id(reply_to)+ reply_to_source = (str(explicit_reply_to) if explicit_reply_to is not None else None) or (+ str(metadata_reply_to) if private_dm_topic_send and metadata_reply_to is not None else None)if private_dm_topic_send:should_thread = reply_to_source is not None and self._reply_to_mode != "off"else:diff --git a/tests/gateway/test_telegram_reply_mode.py b/tests/gateway/test_telegram_reply_mode.pyindex 96eb39751dc..4fd225251ba 100644--- a/tests/gateway/test_telegram_reply_mode.py+++ b/tests/gateway/test_telegram_reply_mode.py@@ -149,6 +149,41 @@ class TestTelegramYamlConfigLoading:assert os.environ.get("TELEGRAM_REPLY_TO_MODE") == "all"+class TestSyntheticReplyAnchor:+ """A route wake's synthetic identity reaches send() as ``reply_to`` (#139611): the reply goes out+ unanchored (or on the DM topic's own anchor) instead of failing on ``int()``."""++ WAKE_ID = '["kanban-wake","agent:main:telegram:dm:12345:42","0f1e2d3c4b5a69788796a5b4c3d2e1f0"]'++ @pytest.mark.asyncio+ async def test_synthetic_anchor_sends_unanchored(self, adapter_factory):+ adapter = adapter_factory(reply_to_mode="all")+ adapter._bot = MagicMock()+ adapter._bot.send_message = AsyncMock(return_value=MagicMock(message_id=1))+ adapter.truncate_message = lambda content, max_len, **kw: ["chunk1", "chunk2"]++ result = await adapter.send("12345", "test content", reply_to=self.WAKE_ID)++ assert result.success, result.error+ calls = adapter._bot.send_message.call_args_list+ assert len(calls) == 2+ assert all(call.kwargs.get("reply_to_message_id") is None for call in calls)++ @pytest.mark.asyncio+ async def test_synthetic_anchor_in_dm_topic_uses_the_topic_anchor(self, adapter_factory):+ adapter = adapter_factory()+ adapter._bot = MagicMock()+ adapter._bot.send_message = AsyncMock(return_value=MagicMock(message_id=1))+ adapter.truncate_message = lambda content, max_len, **kw: ["chunk1"]+ metadata = {"thread_id": "42", "telegram_dm_topic_reply_fallback": True,+ "telegram_reply_to_message_id": "12345"}++ result = await adapter.send("12345", "test content", reply_to=self.WAKE_ID, metadata=metadata)++ assert result.success, result.error+ assert adapter._bot.send_message.call_args.kwargs.get("reply_to_message_id") == 12345++class TestDMTopicFallbackReplyToMode:"""Tests for reply_to_mode enforcement on DM topic fallback paths.