fix(matrix): prevent bot self-reply loop with user_id mismatch and event dedup#768
Open
voidborne-d wants to merge 1 commit intoRightNow-AI:mainfrom
Open
fix(matrix): prevent bot self-reply loop with user_id mismatch and event dedup#768voidborne-d wants to merge 1 commit intoRightNow-AI:mainfrom
voidborne-d wants to merge 1 commit intoRightNow-AI:mainfrom
Conversation
…ent dedup Two fixes for the Matrix bot stuck in infinite reply loop (RightNow-AI#757): 1. Use validated user ID from /whoami instead of config value for self-message filtering. Matrix server delegation or casing differences can cause the configured user_id to not match the sender field in timeline events, so the bot processes its own replies and enters an infinite loop. 2. Add event ID dedup set to prevent re-processing the same event on sync token races or reconnects. This is a defense-in-depth measure that also protects against edge cases where /sync returns overlapping event windows. Fixes RightNow-AI#757
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #757 — Matrix bot gets stuck in an infinite reply loop, responding to the same message every ~3 seconds.
Root Cause
Two issues combine to cause this:
User ID mismatch: The sync loop uses
self.user_id(config value) to filter out own messages, but Matrix server delegation or casing differences can cause this to not match thesenderfield in timeline events. The/whoamiendpoint returns the canonical user ID, butstart()discards this validated value and passes the config value to the spawn instead.No event dedup: If the sync token has a race condition on reconnect, events can be delivered more than once with no protection against re-processing.
Fix
Use the validated user ID from
/whoami(already called instart()) instead of the configself.user_idfor the self-message filter in the sync loop.Add an event ID dedup set (
HashSet<String>, capped at 500) as defense-in-depth to skip already-processed events on sync token races or reconnects.Changes
crates/openfang-channels/src/matrix.rs: 26 lines added, 4 removedlet user_id = validated_userinstead ofself.user_id.clone()seen_eventsHashSet for event dedup before processingevent_idextraction earlier for dedup check