From 45c8fe425699590e317b10518cfed3b60d2d0a8e Mon Sep 17 00:00:00 2001 From: justemu Date: Mon, 18 May 2026 21:07:23 +0800 Subject: [PATCH 1/2] fix(matrix): implement thread_require_mention to prevent multi-agent reply loops When multiple Hermes bots share a Matrix room with MATRIX_AUTO_THREAD=true (default), the in_bot_thread bypass in _resolve_message_context() skips @mention gating for all threaded messages. This causes infinite reply loops where every bot responds to every other bot's messages. Implement thread_require_mention for the Matrix adapter, mirroring the existing Discord adapter behavior: 1. Parse thread_require_mention from config.yaml (or env var MATRIX_THREAD_REQUIRE_MENTION) in __init__() 2. Add thread-level @mention check in _resolve_message_context(): even when in_bot_thread is True, require @mention unless thread_require_mention is disabled Default is false to maintain backward compatibility with single-bot rooms. Closes #27995 --- gateway/platforms/matrix.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/gateway/platforms/matrix.py b/gateway/platforms/matrix.py index 50d383f6f22d9..c5677e5c5cc2c 100644 --- a/gateway/platforms/matrix.py +++ b/gateway/platforms/matrix.py @@ -380,6 +380,12 @@ def __init__(self, config: PlatformConfig): self._require_mention: bool = os.getenv( "MATRIX_REQUIRE_MENTION", "true" ).lower() not in {"false", "0", "no"} + self._thread_require_mention: bool = ( + config.extra.get("thread_require_mention") + if isinstance(config.extra.get("thread_require_mention"), bool) + else os.getenv("MATRIX_THREAD_REQUIRE_MENTION", "false").lower() + in {"true", "1", "yes"} + ) free_rooms_raw = config.extra.get("free_response_rooms") if free_rooms_raw is None: free_rooms_raw = os.getenv("MATRIX_FREE_RESPONSE_ROOMS", "") @@ -1701,6 +1707,20 @@ async def _resolve_message_context( ) return None + # Thread-level @mention gating: even in a bot-participated thread, + # require @mention when thread_require_mention is enabled. + # Prevents infinite reply loops in multi-agent shared rooms + # where multiple bots all participate in the same thread. + elif self._thread_require_mention and in_bot_thread: + if not is_mentioned: + logger.debug( + "Matrix: ignoring message %s in thread %s — " + "no @mention (thread_require_mention=true)", + event_id, + thread_id, + ) + return None + # DM mention-thread. if is_dm and not thread_id and self._dm_mention_threads and is_mentioned: thread_id = event_id From 1b9f225eeff94e1635e4e9406bdc3cee7aa6e3b0 Mon Sep 17 00:00:00 2001 From: justemu Date: Mon, 18 May 2026 22:07:53 +0800 Subject: [PATCH 2/2] fix(matrix): implement thread_require_mention to prevent multi-agent reply loops When multiple Hermes bots share a Matrix room with MATRIX_AUTO_THREAD=true (default), the in_bot_thread bypass in _resolve_message_context() skips @mention gating for all threaded messages, causing infinite N-way reply loops. Implement thread_require_mention for the Matrix adapter, mirroring the existing Discord adapter behavior with proper YAML string/bool parsing. - Add _parse_thread_require_mention() static method that handles YAML booleans, string values, and env var fallback - Add thread-level @mention check in _resolve_message_context() that respects free_response_rooms for behavioral parity with Discord - Default false for backward compatibility with single-bot rooms Closes #27995 --- gateway/platforms/matrix.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/gateway/platforms/matrix.py b/gateway/platforms/matrix.py index c5677e5c5cc2c..28b086291ae87 100644 --- a/gateway/platforms/matrix.py +++ b/gateway/platforms/matrix.py @@ -380,12 +380,7 @@ def __init__(self, config: PlatformConfig): self._require_mention: bool = os.getenv( "MATRIX_REQUIRE_MENTION", "true" ).lower() not in {"false", "0", "no"} - self._thread_require_mention: bool = ( - config.extra.get("thread_require_mention") - if isinstance(config.extra.get("thread_require_mention"), bool) - else os.getenv("MATRIX_THREAD_REQUIRE_MENTION", "false").lower() - in {"true", "1", "yes"} - ) + self._thread_require_mention: bool = self._parse_thread_require_mention(config) free_rooms_raw = config.extra.get("free_response_rooms") if free_rooms_raw is None: free_rooms_raw = os.getenv("MATRIX_FREE_RESPONSE_ROOMS", "") @@ -474,6 +469,27 @@ def _is_duplicate_event(self, event_id) -> bool: self._processed_events_set.add(event_id) return False + @staticmethod + def _parse_thread_require_mention(config) -> bool: + """Parse thread_require_mention from config.extra or env var. + + Handles both YAML booleans and string values (``\"true\"``, ``\"false\"``, + ``\"yes\"``, ``\"no\"``, ``\"on\"``, ``\"off\"``, ``\"1\"``, ``\"0\"``). + Falls back to ``MATRIX_THREAD_REQUIRE_MENTION`` env var, default ``false``. + Mirrors Discord adapter's parsing pattern. + """ + configured = config.extra.get("thread_require_mention") + if configured is not None: + if isinstance(configured, bool): + return configured + if isinstance(configured, str): + return configured.lower() not in {"false", "0", "no", "off"} + # int, float, etc. — truthiness fallback + return bool(configured) + return os.getenv( + "MATRIX_THREAD_REQUIRE_MENTION", "false" + ).lower() in {"true", "1", "yes", "on"} + # ------------------------------------------------------------------ # E2EE helpers # ------------------------------------------------------------------ @@ -1711,7 +1727,8 @@ async def _resolve_message_context( # require @mention when thread_require_mention is enabled. # Prevents infinite reply loops in multi-agent shared rooms # where multiple bots all participate in the same thread. - elif self._thread_require_mention and in_bot_thread: + elif (self._thread_require_mention and in_bot_thread + and not is_free_room): if not is_mentioned: logger.debug( "Matrix: ignoring message %s in thread %s — "