Repository navigation
fix(pulse): never send two Daily Pulse emails for the same tracking window - #717
Merged
Merged
Conversation
…indow The daily KPI window anchors to the tracking-run ledger, but the dedupe slot is keyed on the calendar date. Those disagree whenever the anchor does not move between two pulses, and the mail then repeats verbatim: the catch-up sweep recovered a missed day at the start of a cycle and the brand's own run pulsed an hour later with identical figures, and a brand whose runs had stopped stamping received the same numbers again on each following day. Record the window each digest describes and refuse to generate a second one for it. Only run-anchored windows get a key — weekly digests and brands with no completed run end their window at the wall clock, which differs on every call, so keying them would suppress nothing and risk suppressing something it should not. The engine checks before it computes; a partial unique index on (brand_id, payload->window->>to) is the race guard, mirroring the role the date key already plays for same-day double-fires. It is partial on the extracted path so the 254 existing rows, which carry no window field, are excluded rather than colliding on NULL. Verified against production history: every pair of same-day duplicates shares one run anchor (1785/1785 and 294/294 an hour apart, plus two cross-day repeats), while the pairs that carry genuinely different data (299/294, 295/302) resolve to different anchors and both still send. Closes #701
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
A brand could receive two Daily Pulse emails carrying the identical window — an hour apart on the same morning, or the same figures again a day later.
The daily KPI window anchors to the tracking-run ledger (
computePulseMetricsresolves the same window the Insights 24h view uses), while the dedupe slot is keyed onpulse_date. The two disagree whenever the run anchor has not moved between two pulses, andsent_pulses (brand_id, pulse_date)cannot see it: the rows hold different dates while describing the same run.This records the window each digest describes and refuses to generate a second one for it:
computePulseMetricsnow returnswindow: { from, to, runAnchored }.pulseWindowKey(metrics)derives the dedupe key —window.to, but only when the window is run-anchored. Weekly digests and brands with no completed run end their window at the wall clock, which differs on every call; keying them would suppress nothing and only risk suppressing something it should not.generatePulseForBrandlooks for an existing pulse with that key before it commits, and skips withwindow_already_sent, logging which date the window already went out under. A failed lookup does not suppress the pulse — the index below is still the final guard.00052adds a partial unique index on(brand_id, payload->window->>to). The engine check is the mechanism; the index is the race guard, mirroring the role the date key already plays for same-day double-fires. It is partial on the extracted path so pre-existing rows, which carry nowindowfield, are excluded rather than colliding onNULL.Only the primary fix from the issue is implemented. Moving the catch-up sweep out of the cycle start (proposal 2) is unnecessary once the window key is in place: the sweep may still race the run's own pulse, but a race that produces the same window now resolves to one mail, and one that produces different windows legitimately produces two.
Related issue
Closes #701
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
Automated —
cd server && yarn test(205 pass; 6 new insrc/lib/pulse/engine.test.jscovering the production pair, distinct runs, clock-anchored windows, digests predating the field, and a run-anchored window with no end).Against a database:
tracking_runsrow and no newer one.generatePulseForBrand(brandId)— a pulse is generated and its payload carrieswindow.toequal to that run'scompleted_at.{ pulseDate: '<any earlier date>' }, which is what the catch-up sweep does. Before this change a second mail went out; now it returns{ skipped: 'window_already_sent' }and logs the date the window was already sent under.Verified against production history: every same-day duplicate pair resolves to one shared run anchor (two of them byte-identical at 1785/1785 and 294/294, plus two cross-day repeats of an already-sent window), while the pairs carrying genuinely different data (299/294, 295/302) resolve to different anchors and both still send.
Note for deployment: the migration is additive and idempotent, and no existing row carries the indexed path, so index creation cannot conflict with stored data.
Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromserver/)yarn formatpasses (run fromserver/)supabase/schema.sqlregenerated viabash supabase/build-schema.sh