Repository navigation
fix(matrix): skip events received before bot startup - #3575
Merged
Re-bin merged 1 commit intoMay 1, 2026
Merged
Conversation
Matrix sync replays the room timeline on each startup or `/restart`, causing already-handled messages to be reprocessed (HKUDS#3553). Even with `store_sync_tokens=True`, the sync token isn't reliably re-injected when restoring a session via access_token + load_store(), so the client re-reads recent timeline entries. Filter `event.server_timestamp` against the process start time so old events are dropped at the `_on_message` / `_on_media_message` entry points. Trade-off: messages received during downtime won't be processed, which matches the issue reporter's expectation. Closes HKUDS#3553 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Re-bin
approved these changes
May 1, 2026
Re-bin
left a comment
Collaborator
There was a problem hiding this comment.
This is the right fix for Matrix startup replay.
The timestamp guard is narrow: it only skips events whose server_timestamp predates this process start, and it applies before typing/message handling for both text and media paths. The tests cover both entry points.
I merged latest main locally and ran uv run --extra matrix pytest tests/channels/test_matrix_channel.py: 64 passed.
Ready to merge.
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.
Summary
Closes #3553 — Matrix channel reads old messages on startup /
/restart.self._started_at_mswhenstart()runs.server_timestamp < _started_at_msat the entry of_on_messageand_on_media_message._is_pre_startup_event(event)helper to keep the policy in one place.Why
Matrix sync replays the room timeline on each startup. Even with
store_sync_tokens=Truethe sync token is not reliably re-injected when a session is restored viaaccess_token+load_store()(vs full login), so the client re-reads recent timeline entries and_on_messagefires for already-handled messages — exactly the symptom reported in #3553.A timestamp guard is independent of sync-token state and matches the standard pattern used by matrix-nio bot examples. Trade-off: messages received while the bot is down won't be processed, which is what the issue reporter expects ("already read messages should not be interpreted again").
Test plan
pytest tests/channels/test_matrix_channel.py— 64 passedtest_on_message_skips_pre_startup_eventcovers text pathtest_on_media_message_skips_pre_startup_eventcovers media pathserver_timestampcontinue to be processed because_started_at_msdefaults to0andgetattrreturnsNone)🤖 Generated with Claude Code