Skip to content

fix(matrix): skip events received before bot startup - #3575

Merged
Re-bin merged 1 commit into
HKUDS:mainfrom
coldxiangyu163:fix/matrix-skip-pre-startup-events
May 1, 2026
Merged

Re-bin merged 1 commit into
HKUDS:mainfrom
coldxiangyu163:fix/matrix-skip-pre-startup-events

Conversation

@coldxiangyu163

Copy link
Copy Markdown
Contributor

Summary

Closes #3553 — Matrix channel reads old messages on startup / /restart.

  • Record self._started_at_ms when start() runs.
  • Drop events whose server_timestamp < _started_at_ms at the entry of _on_message and _on_media_message.
  • Add the same guard via a small _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=True the sync token is not reliably re-injected when a session is restored via access_token + load_store() (vs full login), so the client re-reads recent timeline entries and _on_message fires 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 passed
  • New test_on_message_skips_pre_startup_event covers text path
  • New test_on_media_message_skips_pre_startup_event covers media path
  • Existing tests still pass (events without server_timestamp continue to be processed because _started_at_ms defaults to 0 and getattr returns None)

🤖 Generated with Claude Code

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 Re-bin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Re-bin
Re-bin merged commit 15007af into HKUDS:main May 1, 2026
8 checks passed
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 channel reads old messages on startup

2 participants