Skip to content

fix(pulse): never send two Daily Pulse emails for the same tracking window - #717

Merged
gkhngyk merged 1 commit into
mainfrom
fix/701-pulse-window-dedupe
Aug 13, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/701-pulse-window-dedupe

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

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 (computePulseMetrics resolves the same window the Insights 24h view uses), while the dedupe slot is keyed on pulse_date. The two disagree whenever the run anchor has not moved between two pulses, and sent_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:

  • computePulseMetrics now returns window: { 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.
  • generatePulseForBrand looks for an existing pulse with that key before it commits, and skips with window_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.
  • Migration 00052 adds 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 no window field, are excluded rather than colliding on NULL.

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 feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

Automated — cd server && yarn test (205 pass; 6 new in src/lib/pulse/engine.test.js covering the production pair, distinct runs, clock-anchored windows, digests predating the field, and a run-anchored window with no end).

Against a database:

  1. Pick a brand with at least one completed tracking_runs row and no newer one.
  2. Call generatePulseForBrand(brandId) — a pulse is generated and its payload carries window.to equal to that run's completed_at.
  3. Call it again with { 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.
  4. Stamp a newer run for the brand and call it again — the anchor has moved, so it generates normally.

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

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from server/)
  • yarn format passes (run from server/)
  • supabase/schema.sql regenerated via bash supabase/build-schema.sh
  • Changes are focused — one concern per PR

…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
@gkhngyk
gkhngyk merged commit 73b6dcb into main Aug 13, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the fix/701-pulse-window-dedupe branch August 13, 2026 14:09
@gkhngyk gkhngyk mentioned this pull request Aug 24, 2026
7 of 12 tasks
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.

bug: duplicate Daily Pulse emails when the catch-up sweep and the run's own pulse share a window

1 participant