Skip to content

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

Closed

Description

@gkhngyk

Description

A brand can receive two Daily Pulse emails within the same hour whose bodies describe the identical tracking window.

It happened in production this morning for one large brand (~1.8k results/day): two mails, ~05:03 and ~06:19 local, both reporting the same totalResults because both were computed from the same previous-day tracking run. The second mail was effectively a stripped copy of the first — its warnings were empty only because the 7-day warning cooldown had already consumed them an hour earlier.

sent_pulses rows for that brand:

created pulse_date totalResults warnings trigger
05:03 previous day 1785 3 catch-up sweep
06:19 today 1785 0 end-of-run pulse

Two independent defects combine:

1. The catch-up sweep recovers a missed pulse with current data.
runPulseCatchUp() in server/src/lib/pulse/engine.js passes only pulseDate into generatePulseForBrand(); the metrics are still computed with now. So a pulse "recovered" for yesterday actually reports whatever window is current at sweep time. Because the sweep runs at the start of the daily cycle (wired into runDailyTracking in server/src/server.js), the anchor has not moved yet — it is still yesterday's run, i.e. exactly the window today's own pulse will use an hour later.

2. Dedupe is keyed on pulse_date, not on the data.
The sent_pulses (brand_id, pulse_date) unique key cannot see that two rows with different dates describe the same window. computePulseMetrics() anchors the daily KPI window to the latest completed run (latestCompletedRunTimes), so whenever that anchor has not advanced, every pulse fired that day repeats the previous mail.

Condition to trigger: a brand misses a pulse (process death, DB timeout, deploy) and its run anchor has not moved by the time the next cycle's own pulse fires. The second half held today because the brand's run was never stamped — filed separately.

Steps to reproduce

  1. Pick a brand with a completed tracking run and delete its sent_pulses row for that run's day (simulates the lost pulse).
  2. Make sure no newer completed run exists for that brand (so the KPI anchor stays on that run).
  3. Start the daily cycle so runPulseCatchUp() runs — a recovery mail goes out, dated to the missed day.
  4. Let the brand's own tracking job finish without stamping a newer run.
  5. A second mail goes out with the same numbers, dated today.

Expected behavior

At most one pulse email per brand per tracking window. A recovered pulse should either describe the window it claims to recover, or be suppressed when the brand is about to produce a pulse for the same window.

Actual behavior

Two emails an hour apart with identical KPIs, and the second one silently loses its warnings to the cooldown, so it reads as a degraded duplicate.

Deployment mode

Cloud (ansvisor.com)

Environment

Server (Node), production daily cycle.

Proposed fix

Both parts are small and independent:

  1. Anchor-aware dedupe (primary). Record the KPI window anchor (curTo, i.e. the latest completed run used by computePulseMetrics) in the sent_pulses payload, and have generatePulseForBrand() skip generation when a pulse for the same brand already exists with the same anchor. This kills identical-content mails regardless of which path fired them, and also protects against re-runs and manual triggers.
  2. Move the sweep out of the cycle start. At cycle start every eligible brand is about to produce its own pulse, so the sweep is racing them by design. Either run it at the end of the cycle, or skip brands that have a tracking job queued/running for the current cycle.

Optionally, if the recovery mail is meant to represent the missed day, generatePulseForBrand() should accept a now override so the metrics match the pulseDate it is filed under — otherwise the label is misleading even when no duplicate occurs.

Relevant code:

Introduced by #654.

Activity

  1. added
    bug
    on Aug 11, 2026
  2. self-assigned this
    on Aug 11, 2026
  3. added
    tracking
    on Aug 12, 2026
  4. added a commit that references this issue on Aug 13, 2026
    73b6dcb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugtracking

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions