Skip to content

fix(matrix): prevent echo loop by using /whoami user_id for self-message filtering#760

Open
sliverp wants to merge 1 commit intoRightNow-AI:mainfrom
sliverp:fix/matrix-bot-echo-loop
Open

fix(matrix): prevent echo loop by using /whoami user_id for self-message filtering#760
sliverp wants to merge 1 commit intoRightNow-AI:mainfrom
sliverp:fix/matrix-bot-echo-loop

Conversation

@sliverp
Copy link

@sliverp sliverp commented Mar 20, 2026

Summary

Fixes #757 — Matrix bot gets stuck in an infinite echo loop.

Root Cause

The Matrix adapter's echo guard compared incoming message senders against the config-provided user_id instead of the server-validated identity from /whoami. When these differ (wrong casing, empty config value, different homeserver), the bot fails to recognize its own messages and replies to them indefinitely.

Changes

1. Echo guard: use server-validated user_id (primary fix)

  • start() now uses the /whoami-validated user identity for echo filtering instead of self.user_id
  • Added a warning log when the config user_id diverges from the server-reported value

2. Fix txn_id reuse across message chunks

  • Moved uuid::Uuid::new_v4() generation inside the chunk loop in api_send_message()
  • Previously, a single txn_id was generated once and reused for all chunks — Matrix's PUT idempotency silently dropped all chunks after the first

3. Tests

  • test_matrix_adapter_empty_user_id: empty config user_id doesn't panic on construction
  • test_sender_echo_filter_semantics: case-sensitive exact match verification for the echo guard
  • test_txn_id_uniqueness: UUID uniqueness across message chunks

Testing

All existing tests pass. New tests cover the specific failure modes that caused #757.

…age filtering

The Matrix adapter used the config-provided user_id for echo filtering
instead of the server-validated identity from /whoami. When these differ
(wrong casing, empty config, different homeserver), the bot processes
its own messages and enters an infinite reply loop.

Changes:
- Use validated /whoami user_id for the echo guard instead of config value
- Warn when config user_id diverges from server-reported identity
- Generate unique txn_id per message chunk (fixes silent drop of multi-chunk
  messages due to Matrix PUT idempotency deduplication)
- Add tests for empty user_id handling, echo filter semantics, and txn_id
  uniqueness

Closes RightNow-AI#757
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Matrix bot stuck in loop

1 participant