Skip to content

The agent that grows with you

fix(telegram): observe sibling-bot wake-word messages dropped by the bot-to-bot gate

KeyArgoe7509fa1a4d0
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index 2c14d192941..ced45f4b52f 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -5874,6 +5874,16 @@ class TelegramAdapter(BasePlatformAdapter):
logger.warning("[%s] Ignoring non-numeric Telegram message_thread_id: %r", self.name, thread_id)
return None
+ def _bot_sender_suppressed(self, message: Message) -> bool:
+ """True when ``bots_require_mention`` vetoes this other-bot message: another bot must
+ explicitly @mention us, its quote-replies and plain chatter do not count (two bots
+ answering each other's replies never stop otherwise)."""
+ return bool(
+ self._telegram_bots_require_mention()
+ and self._sender_is_other_bot(message)
+ and not self._message_mentions_bot(message)
+ )
+
def _should_observe_unmentioned_group_message(self, message: Message) -> bool:
"""Return True when a group message should be stored but not dispatched."""
if self._is_own_message(message) or not self._telegram_observe_unmentioned_group_messages() or not self._is_group_chat(message):
@@ -5887,9 +5897,15 @@ class TelegramAdapter(BasePlatformAdapter):
allowed = self._telegram_observe_allowed_chats()
if not allowed or chat_id_str not in allowed:
return False
- # Only observe messages the require_mention gate would skip.
+ # Free-response chats/topics dispatch every message, so they are never observed.
if chat_id_str in self._telegram_free_response_chats() or self._telegram_is_free_response_topic(message):
return False
+ # Only observe messages the require_mention gate would skip. The bot-to-bot loop breaker
+ # in ``_should_process_message`` skips another bot's message too, so a sibling bot
+ # addressing us by wake word (or quote-reply) is never dispatched and must still be
+ # kept as observed context (#115119).
+ if self._bot_sender_suppressed(message):
+ return True
if not self._telegram_require_mention() or self._is_reply_to_bot(message) or self._message_mentions_bot(message):
return False
return not self._message_matches_mention_patterns(message)
@@ -6126,11 +6142,7 @@ class TelegramAdapter(BasePlatformAdapter):
return True
# Bot-to-bot loop breaker: another bot must explicitly @mention us; its quote-reply or
# plain chatter does not count (two bots answering each other's replies never stop otherwise).
- if (
- self._telegram_bots_require_mention()
- and self._sender_is_other_bot(message)
- and not self._message_mentions_bot(message)
- ):
+ if self._bot_sender_suppressed(message):
return False
if not self._telegram_require_mention() or self._is_reply_to_bot(message):
return True
diff --git a/tests/gateway/test_telegram_group_gating.py b/tests/gateway/test_telegram_group_gating.py
index 20daa20fe5b..75d252a52cb 100644
--- a/tests/gateway/test_telegram_group_gating.py
+++ b/tests/gateway/test_telegram_group_gating.py
@@ -956,3 +956,98 @@ def test_human_reply_unaffected_by_bots_require_mention():
gated._should_process_message(_group_message("replying", reply_to_bot=True))
is True
)
+
+
+def test_sibling_bot_wake_word_message_is_observed_not_dropped():
+ """#115119: the bot-to-bot loop breaker drops a sibling bot's message that carries no explicit
+ @mention, so a wake-word (``mention_patterns``) match from that bot is never dispatched. It must
+ still land in observed group context instead of vanishing from both paths."""
+ async def _run():
+ adapter = _make_adapter(
+ require_mention=True,
+ bots_require_mention=True,
+ mention_patterns=["hermes"],
+ allowed_chats=["-100"],
+ group_allowed_chats=["-100"],
+ observe_unmentioned_group_messages=True,
+ )
+ store = _FakeSessionStore()
+ adapter._session_store = store
+ msg = _bot_sender_message("hermes, can you take this one?")
+ assert adapter._should_process_message(msg) is False, "loop breaker must still block dispatch"
+ update = SimpleNamespace(update_id=2001, message=msg, effective_message=None)
+
+ await adapter._handle_text_message(update, SimpleNamespace())
+
+ adapter._message_handler.assert_not_awaited()
+ assert len(store.messages) == 1
+ _session_id, message, _skip_db = store.messages[0]
+ assert message["observed"] is True
+ assert message["content"].endswith("hermes, can you take this one?")
+
+ asyncio.run(_run())
+
+
+def test_sibling_bot_quote_reply_is_observed_not_dropped():
+ """The loop breaker also drops a sibling bot's quote-reply; observation must not lose it too."""
+ adapter = _make_adapter(
+ require_mention=True,
+ bots_require_mention=True,
+ allowed_chats=["-100"],
+ group_allowed_chats=["-100"],
+ observe_unmentioned_group_messages=True,
+ )
+ msg = _bot_sender_message("auto-reply", reply_to_bot=True)
+
+ assert adapter._should_process_message(msg) is False
+ assert adapter._should_observe_unmentioned_group_message(msg) is True
+
+
+def test_sibling_bot_explicit_mention_still_dispatches_and_is_not_observed():
+ """An explicit @mention from another bot still dispatches, so it must NOT be observed as well."""
+ adapter = _make_adapter(
+ require_mention=True,
+ bots_require_mention=True,
+ mention_patterns=["hermes"],
+ allowed_chats=["-100"],
+ group_allowed_chats=["-100"],
+ observe_unmentioned_group_messages=True,
+ )
+ text = "@hermes_bot ping"
+ msg = _bot_sender_message(text, entities=[_mention_entity(text)])
+
+ assert adapter._should_process_message(msg) is True
+ assert adapter._should_observe_unmentioned_group_message(msg) is False
+
+
+def test_human_wake_word_message_is_dispatched_not_observed_with_bot_gate_on():
+ """The bot gate must not widen observation for human senders: a human wake-word match still
+ dispatches and is therefore not double-recorded as context."""
+ adapter = _make_adapter(
+ require_mention=True,
+ bots_require_mention=True,
+ mention_patterns=["hermes"],
+ allowed_chats=["-100"],
+ group_allowed_chats=["-100"],
+ observe_unmentioned_group_messages=True,
+ )
+ msg = _group_message("hermes, hello")
+
+ assert adapter._should_process_message(msg) is True
+ assert adapter._should_observe_unmentioned_group_message(msg) is False
+
+
+def test_bot_wake_word_message_still_dispatches_when_loop_breaker_is_off():
+ """Without ``bots_require_mention`` nothing changed: a bot's wake-word message is dispatched
+ normally and stays out of observed context."""
+ adapter = _make_adapter(
+ require_mention=True,
+ mention_patterns=["hermes"],
+ allowed_chats=["-100"],
+ group_allowed_chats=["-100"],
+ observe_unmentioned_group_messages=True,
+ )
+ msg = _bot_sender_message("hermes, take this")
+
+ assert adapter._should_process_message(msg) is True
+ assert adapter._should_observe_unmentioned_group_message(msg) is False