fix(messages): prevent duplicate inbox messages from channel echoes - #893
Merged
Conversation
viethung0o0
marked this pull request as ready for review
August 4, 2026 10:06
viethung0o0
added a commit
that referenced
this pull request
Sep 7, 2026
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
When the bot replies on Instagram/Messenger, the outbound message is saved once (the Send API returns the
mid, stored assourceId). The channel then echoes that same message back a few seconds later (is_echo: true, samemid). In production this echo was saved a second time, so a single reply appeared twice in the inbox.The echo dedup already keys on
sourceId, but the guard read used the incoming row's owncreatedAtas its lower bound. BecausefindBySourceIdfilterscreatedAt >= sinceTime, the earlier row (saved ~1.4s before) fell outside the window and was never found, so a duplicate row was inserted.Fix
Widen the dedup guard read to look back a bounded window (1 day) so it can see the message saved moments earlier, reusing the existing
getSafeSinceTimehelper. Applied to every dedup lookup in bothcreateOrUpdateandcreateOrUpdateWithAttachments.LIMIT 1on a highly selectivesourceId.Test plan
pnpm --filter @chatbotx.io/database test— 377 passing, incl. 2 new regression tests (createOrUpdateandcreateOrUpdateWithAttachments) reproducing the send→echo duplicatepnpm --filter @chatbotx.io/database check-typespnpm lintpnpm check:circular— no new circular dependency