The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(relay): use a reaction Telegram actually allows for the turn ack
Ben Barclayab24fb058181
diff --git a/gateway/relay/adapter.py b/gateway/relay/adapter.pyindex 01b4cedce66..5c1d9fde3a8 100644--- a/gateway/relay/adapter.py+++ b/gateway/relay/adapter.py@@ -2230,7 +2230,47 @@ class RelayAdapter(BasePlatformAdapter):meta["thread_id"] = str(thread_id)return meta- # ── Phase 3 ack lifecycle (👀 → ✅/❌) ────────────────────────────────+ # ── Phase 3 ack lifecycle (in-progress → outcome reaction) ───────────────++ # Telegram accepts only a CURATED reaction vocabulary — the 73 emoji listed+ # on `ReactionTypeEmoji` (https://core.telegram.org/bots/api#reactiontypeemoji,+ # "Reaction emoji. Currently, it can be one of …"). 👀 (U+1F440) is in that+ # set; ✅ (U+2705) and ❌ (U+274C) are NOT. So on Telegram the in-progress+ # ack landed and every completion ack was rejected by the Bot API — the+ # `turn_ack_reaction_lifecycle` finding ("👀 lands and is removed on+ # completion; the ✅ completion reaction never lands"). Because a react+ # failure is deliberately cosmetic (`_react` is best-effort, logged at+ # debug), it failed silently on every single Telegram turn.+ #+ # 👍/👎 are both in Telegram's set and carry the same success/failure sense.+ # Platforms with free-form reaction vocabularies (Slack, Discord, Matrix,+ # Signal) keep ✅/❌, which read better and are what their users already see.+ # Per-platform divergence here follows `_descriptor_for_chat`'s precedent:+ # platform capabilities genuinely differ, so one hardcoded set cannot serve+ # every lane a multi-platform gateway fronts.+ _ACK_EMOJI_DEFAULT = ("👀", "✅", "❌")+ _ACK_EMOJI_BY_PLATFORM = {+ "telegram": ("👀", "👍", "👎"),+ }++ def _ack_emoji(self, event, chat_id) -> tuple:+ """(in_progress, success, failure) for the lane this event arrived on.++ Prefers the EVENT's own platform: an ack always follows an inbound+ event, so the platform is on hand and needs no cache. `_chat_platform`+ is the fallback, and it can only answer for a chat already seen inbound+ — the descriptor's primary platform covers the rest.++ `Platform` is a plain `Enum`, so `str()` on a member yields+ "Platform.TELEGRAM", not "telegram" — read `.value` first or every+ lookup misses and silently falls back to the default set.+ """+ raw = getattr(getattr(event, "source", None), "platform", None)+ platform = str(getattr(raw, "value", raw) or "").lower()+ if not platform:+ lane = self._chat_platform(str(chat_id))+ platform = str(getattr(lane, "value", lane) or "").lower()+ return self._ACK_EMOJI_BY_PLATFORM.get(platform, self._ACK_EMOJI_DEFAULT)async def _react(self,@@ -2258,21 +2298,24 @@ class RelayAdapter(BasePlatformAdapter):return result is not Noneasync def on_processing_start(self, event) -> None:- """Add the 👀 in-progress reaction (op-gated; silent no-op otherwise)."""+ """Add the in-progress reaction (op-gated; silent no-op otherwise)."""message_id, chat_id = _event_ids(event)if message_id and chat_id:- await self._react(str(chat_id), str(message_id), "👀")+ eyes, _ok, _fail = self._ack_emoji(event, chat_id)+ await self._react(str(chat_id), str(message_id), eyes)async def on_processing_complete(self, event, outcome) -> None:- """Swap 👀 for ✅/❌ per outcome (op-gated; silent no-op otherwise)."""+ """Swap the in-progress reaction for the outcome one (op-gated; silent+ no-op otherwise)."""message_id, chat_id = _event_ids(event)if not (message_id and chat_id):return- await self._react(str(chat_id), str(message_id), "👀", remove=True)+ eyes, ok_emoji, fail_emoji = self._ack_emoji(event, chat_id)+ await self._react(str(chat_id), str(message_id), eyes, remove=True)if outcome == ProcessingOutcome.SUCCESS:- await self._react(str(chat_id), str(message_id), "✅")+ await self._react(str(chat_id), str(message_id), ok_emoji)elif outcome == ProcessingOutcome.FAILURE:- await self._react(str(chat_id), str(message_id), "❌")+ await self._react(str(chat_id), str(message_id), fail_emoji)# ── Phase 4 thread lifecycle ──────────────────────────────────────────diff --git a/tests/gateway/relay/test_relay_interactive.py b/tests/gateway/relay/test_relay_interactive.pyindex 4d1acbb31f2..bc3278ebd33 100644--- a/tests/gateway/relay/test_relay_interactive.py+++ b/tests/gateway/relay/test_relay_interactive.py@@ -12,7 +12,8 @@ Covers:through to normal dispatch;- the Discord type-3 hp1 decode (structured prompt_response replacing thebare-custom_id stub; foreign custom_ids keep the legacy text shape);- - on_processing_start/complete drive react ops (👀 → ✅/❌), op-gated and+ - on_processing_start/complete drive react ops (👀 → ✅/❌, or 👀 → 👍/👎 on+ Telegram, whose reaction vocabulary is a fixed set), op-gated andbest-effort."""@@ -24,7 +25,7 @@ from typing import Any, Dict, Optionalimport pytest-from gateway.config import PlatformConfig+from gateway.config import Platform, PlatformConfigfrom gateway.platforms.event import MessageEvent, MessageType, ProcessingOutcomefrom gateway.relay.adapter import RelayAdapterfrom gateway.relay.descriptor import CONTRACT_VERSION, CapabilityDescriptor@@ -228,12 +229,12 @@ def test_discord_component_interaction_decodes_prompt_token():# ── react ack lifecycle ──────────────────────────────────────────────────-def _reactable_event() -> MessageEvent:+def _reactable_event(platform=Platform.DISCORD) -> MessageEvent:return MessageEvent(text="do something",message_type=MessageType.TEXT,source=SessionSource(- platform="discord",+ platform=platform,chat_id="ch1",chat_type="channel",user_id="u1",@@ -243,6 +244,20 @@ def _reactable_event() -> MessageEvent:)+# Telegram's ENTIRE allowed reaction vocabulary, from `ReactionTypeEmoji`:+# https://core.telegram.org/bots/api#reactiontypeemoji — "Reaction emoji.+# Currently, it can be one of …". Transcribed verbatim (73 entries). The point+# of holding the whole list here rather than just the three emoji we use: the+# completion ack silently failed on every Telegram turn for want of exactly+# this check, so any future edit to the ack emoji is verified against the real+# vocabulary instead of someone's recollection of it.+TELEGRAM_ALLOWED_REACTIONS = frozenset(+ "❤ 👍 👎 🔥 🥰 👏 😁 🤔 🤯 😱 🤬 😢 🎉 🤩 🤮 💩 🙏 👌 🕊 🤡 🥱 🥴 😍 🐳"+ " 🌚 🌭 💯 🤣 ⚡ 🍌 🏆 💔 🤨 😐 🍓 🍾 💋 🖕 😈 😴 😭 🤓 👻 👀 🎃 🙈 😇 😨"+ " 🤝 ✍ 🤗 🫡 🎅 🎄 ☃ 💅 🤪 🗿 🆒 💘 🙉 🦄 😘 💊 🙊 😎 👾 🤷 😡".split()+) | {"❤🔥", "👨💻", "🤷♂", "🤷♀"}++@pytest.mark.asyncioasync def test_processing_lifecycle_reacts_eyes_then_check():adapter, stub = _adapter()@@ -258,6 +273,60 @@ async def test_processing_lifecycle_reacts_eyes_then_check():assert all(r["message_id"] == "m42" and r["chat_id"] == "ch1" for r in reacts)+# ── the ack vocabulary is per-platform (turn_ack_reaction_lifecycle) ──────+#+# Telegram rejects any reaction outside its curated set, so the ✅/❌ completion+# ack was refused by the Bot API on every turn while the 👀 that opened it+# succeeded. `_react` is best-effort by design, so nothing surfaced: in+# production this showed only as `relay.outbound` react spans alternating+# success/failure, one failure per completed turn.+++ "outcome,expected",+ [(ProcessingOutcome.SUCCESS, "👍"), (ProcessingOutcome.FAILURE, "👎")],+)+async def test_telegram_ack_uses_an_emoji_telegram_actually_allows(outcome, expected):+ adapter, stub = _adapter()+ event = _reactable_event(platform=Platform.TELEGRAM)+ await adapter.on_processing_start(event)+ await adapter.on_processing_complete(event, outcome)+ emojis = [a["emoji"] for a in stub.sent if a["op"] == "react"]+ assert emojis == ["👀", "👀", expected]+ # The real constraint, not just the literal we happened to choose.+ for emoji in emojis:+ assert emoji in TELEGRAM_ALLOWED_REACTIONS, (+ f"{emoji!r} is not in Telegram's ReactionTypeEmoji vocabulary; "+ "setMessageReaction will reject it and the ack will never land"+ )+++async def test_free_form_reaction_platforms_keep_the_check_mark():+ """Slack/Discord/Matrix/Signal take arbitrary emoji — they must NOT be+ dragged down to Telegram's vocabulary."""+ for platform in (Platform.SLACK, Platform.DISCORD, Platform.MATRIX, Platform.SIGNAL):+ adapter, stub = _adapter()+ event = _reactable_event(platform=platform)+ await adapter.on_processing_complete(event, ProcessingOutcome.SUCCESS)+ emojis = [a["emoji"] for a in stub.sent if a["op"] == "react"]+ assert emojis == ["👀", "✅"], f"{platform.value} should keep ✅, got {emojis}"+++async def test_ack_platform_falls_back_to_the_chat_lane_then_the_descriptor():+ """An event with no platform on its source still resolves: a multi-platform+ gateway records the chat's lane inbound, and the descriptor's primary+ platform covers a chat never seen inbound. A telegram-primary descriptor+ must therefore never emit ✅."""+ adapter, stub = _adapter() # make_desc default platform is telegram+ event = _reactable_event(platform=None)+ await adapter.on_processing_complete(event, ProcessingOutcome.SUCCESS)+ emojis = [a["emoji"] for a in stub.sent if a["op"] == "react"]+ assert emojis == ["👀", "👍"]++# ── fanned-out prompt answers (one press, many gateways) ─────────────────## The connector delivers a passthrough forward (a Discord button press) to