Skip to content

bug: MATRIX_AUTO_THREAD enables infinite reply loops when multiple bots share a Matrix room #27995

@justemu

Description

@justemu

Summary

When multiple Hermes Agent bots share a Matrix room, MATRIX_AUTO_THREAD=true (the default) causes severe infinite reply loops. The root cause is that in_bot_thread bypass logic skips @mention gating for threaded messages, and thread_require_mention is not implemented for the Matrix adapter.

Impact

  • Multi-agent rooms flood with hundreds of messages in minutes
  • API tokens wasted on loop traffic
  • Gateway instability amplifies the loop during restarts

Root Cause

Issue 1: in_bot_thread bypass eliminates @mention gating in shared threads

In _resolve_message_context() (~line 1692 of gateway/platforms/matrix.py):

in_bot_thread = bool(thread_id and thread_id in self._threads)
if self._require_mention and not is_free_room and not in_bot_thread:
    if not is_mentioned:
        return None  # only this path checks @mention

When multiple bots auto-thread the same message into a shared thread, in_bot_thread is true for all of them, bypassing @mention entirely. This design assumes one bot per room. Additionally, self._threads.mark(thread_id) at line 1727 executes unconditionally after any successful message processing, so even bots with MATRIX_AUTO_THREAD=false get registered once @mentioned into the thread.

Issue 2: thread_require_mention is Discord-only

matrix:
  thread_require_mention: true   # Set in config, but Matrix adapter ignores it

MatrixPlatform.__init__() never parses this field. It only exists for Discord, creating a false sense of security on Matrix.

Loop Mechanism

  1. Bot A (auto_thread=true) receives message M1, creates thread from event_id
  2. Bot A replies in thread (no @mention per convention)
  3. Bot B sees A's reply, in_bot_thread=True, @mention bypassed, B replies
  4. N-way positive feedback loop, hundreds of messages in minutes

Reproduction

  1. Set up 3+ Hermes bots with default MATRIX_AUTO_THREAD=true in one room
  2. Send a message @mentioning one bot
  3. Bots reply endlessly within the shared thread

Proposed Fix

Implement thread_require_mention for Matrix, mirroring Discord adapter behavior (default false for backward compatibility):

A. Parse config in __init__():

self._thread_require_mention: bool = (
    config.extra.get("thread_require_mention")
    if config.extra.get("thread_require_mention") is not None
    else os.getenv("MATRIX_THREAD_REQUIRE_MENTION", "false").lower()
         in {"true", "1", "yes"}
)

B. Add thread-level @mention check in _resolve_message_context():

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

Immediate Workaround

Set MATRIX_AUTO_THREAD=false on all bots sharing a room.

Environment

  • Hermes Agent latest (2026-05-18)
  • Matrix gateway adapter
  • 9-bot shared room reproducing the issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1High — major feature broken, no workaroundcomp/gatewayGateway runner, session dispatch, deliveryplatform/matrixMatrix adapter (E2EE)type/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions