fix: de-duplicate events returned by generic tag-filter subscriptions#694
Open
Priyanshubhartistm wants to merge 3 commits into
Open
Conversation
🦋 Changeset detectedLatest commit: 8b94b43 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Collaborator
There was a problem hiding this comment.
Pull request overview
This PR fixes a protocol-correctness bug where generic tag-filter subscriptions could deliver the same stored event multiple times due to events being left-joined to event_tags without de-duplication.
Changes:
- Switch tag-query
findByFilters()selection toSELECT DISTINCT events.*to prevent join row-multiplication from producing duplicate events. - Update unit tests that assert generated SQL for generic tag filters to expect
select distinct "events".*. - Add a changeset documenting the patch fix.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/repositories/event-repository.ts |
Applies DISTINCT for tag-filtered (non-search) queries to prevent duplicate event rows. |
test/unit/repositories/event-repository.spec.ts |
Updates SQL-string expectations for #e/#g/#p/#r/#d tag-filter tests to include DISTINCT. |
.changeset/duplicate-events-on-tag-filter-queries.md |
Documents the bug and the de-duplication fix as a patch release. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Description
EventRepository.findByFilters()left-joinsevent_tagswhen a filter contains a generic tag query (#e,#p,#g, etc.) and selectsevents.*with noDISTINCTorGROUP BY. If an event has more than oneevent_tagsrow matching the filter's criteria for the same query - for example a filter of{"#p": ["a", "b"]}matching an event that is tagged with bothaandb- the join produces one result row per matching tag row instead of one row per event. Those rows are streamed straight to the subscriber bySubscribeMessageHandler.fetchAndSend(), so the client receives duplicateEVENTmessages for the same event within a single subscription.The tag-query branch of
findByFilters()now usesbuilder.distinct('events.*')instead ofbuilder.select('events.*'), so each stored event is returned at most once regardless of how many tag rows it matches.countByFilters()was already unaffected - it already wraps its result incountDistinct({ count: 'event_id' }).Related Issue
#693
Motivation and Context
This is a protocol-correctness bug: NIP-01 subscribers expect each matching event once per subscription. Duplicate delivery wastes client bandwidth/processing and can confuse client-side dedup logic that isn't expecting repeats within the same
REQ.How Has This Been Tested?
test/unit/repositories/event-repository.spec.tsthat assert the exact generated SQL for#e,#g,#p,#r, and#dtag filters - they now expectselect distinct "events".*instead ofselect "events".*.test/unit/repositories/event-repository.spec.tssuite (62 tests) - all pass, including thecountByFilterstests, confirming that code path (already correct viacountDistinct) is untouched.pnpm lint,pnpm check:deps,pnpm run build,pnpm run build:check,pnpm run verify:cli:build,pnpm run test:unit(1393 passing),pnpm run test:cli(73 passing), andpnpm run cover:unit- all pass.Screenshots (if appropriate):
N/A - backend query change only.
Types of changes
Checklist: