Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions gateway/platforms/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +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 = 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", "")
Expand Down Expand Up @@ -468,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
# ------------------------------------------------------------------
Expand Down Expand Up @@ -1701,6 +1723,21 @@ 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
and not is_free_room):
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
Comment on lines +1730 to +1739

# DM mention-thread.
if is_dm and not thread_id and self._dm_mention_threads and is_mentioned:
thread_id = event_id
Expand Down