Skip to content

fix: traffic alerts that cry wolf, and past_due customers losing Pro - #9

Merged
scobb merged 2 commits into
mainfrom
fix/alerts-and-past-due
Oct 12, 2026
Merged

scobb merged 2 commits into
mainfrom
fix/alerts-and-past-due

Conversation

@scobb

@scobb scobb commented Oct 12, 2026 •

Copy link
Copy Markdown
Owner

Why

Two bugs from the 2026-10-12 review, both hitting the paying customer.

1. Traffic alerts were almost all false. The 09:00 UTC cron compared today, which at that point is 9 hours of traffic, against a full-day average with a flat −40% threshold. Against production data, 23 of 25 eligible sites would have alerted on 2026-10-12. The paying customer has alerts_enabled = 0 on 18 of their 19 sites. Alerts also ran on both cron triggers, so Mondays sent everything twice.

2. A failed card dropped Pro. customer.subscription.updated mapped every status except active to free. On past_due (Stripe retries for days), the customer fell to free-tier collection caps, and collect started rejecting their pageviews with 402. The billing page's Stripe sync had the same bug, because it filtered to status=active.

Alerts: fixing the window wasn't enough

I backtested candidate rules on production daily history, 2026-07-01 → 2026-10-11:

Rule Alerts / month (all sites) Catches simulated outages
Old rule, but on yesterday (complete day) ~420 all, buried in noise
+ Poisson significance ~64 31% (1-day)
+ site's own variability (3σ) ~30 10% (1-day)
Tracking stopped + big spikes (this PR) ~8 69–89% (2–3 day)

At 1–40 pageviews a day, a quiet day is normal and a dead day looks like a quiet one. What customers need is to hear when tracking stops (the June incident), plus genuinely large spikes:

  • Tracking stopped: a reliably active site (traffic on ≥20 of 28 days) has been silent longer than it ever was in the 28 days before. It fires once per outage and links to the installation check. Twenty days lets weekday-only sites (B2B, schools) qualify; at 21 they never could.
  • Spike: the site averages 10+/day, and the day is more than double that average and ≥3σ above it (σ floored at √mean).
  • Cost: one bounded GROUP BY day per site replaces three queries, one of which scanned the site's entire history every day.
  • Scheduling: alerts now run on the daily trigger only.

The ~8/month figure comes from replaying production history through the shipped TypeScript functions, and it matches the Python backtest exactly (15 tracking-stopped + 13 spikes over 3.4 months). A quarter of those alerts are on Beam's own sites. The old "traffic dropped" email is gone. The two new emails were rendered and checked visually.

Billing

active, trialing and past_due keep Pro. canceled, unpaid, incomplete_expired and paused end it. The webhook and the billing-page sync share one mapping (subscriptionKeepsPro). The sync no longer filters to status=active: with no filter, Stripe returns every non-canceled subscription.

CI: Node 20 → 24

The new tests use the built-in node:sqlite, which Node 20 lacks. Node 20 also reached end-of-life in April 2026, and CI was already logging deprecation warnings. Both jobs now run Node 24 LTS, matching local development, so the deploy uses the same runtime that was tested.

Verification

  • npm run typecheck is clean
  • npm test: 158 pass, 0 fail. New tests run the real SQL against in-memory SQLite with every migration applied (test/helpers/sqliteD1.ts).
  • Against the old code, every test targeting these bugs fails: 7 of 8 alert integration tests, plus past_due/trialing and the billing-page sync. The tests that still pass pin behavior the old code also got right (canceled ends Pro, alerts_enabled = 0 is honored, unsigned webhooks are rejected).
  • Targeted mutations all go red: fire-every-day, no active-days gate, no spike floor, no spike z-test, threshold back to 21, including the partial day, Monday double-run, and ignoring alerts_enabled. Two vacuous tests found this way were fixed.
  • npm run test:smoke: 132/132, against a dev server with PUBLIC_BASE_URL=http://localhost:8787. Production was checked afterwards: no localhost-referred writes.

After merge

Check the Deploy job. The first real run is 09:00 UTC tomorrow. Customers who turned alerts off won't hear the new ones unless they turn them back on. A short note to the paying customer may be worth sending.

🤖 Generated with Claude Code

Ralph (Keylight Digital) and others added 2 commits October 12, 2026 16:27
Traffic alerts
--------------
The daily cron ran at 09:00 UTC and compared "today" (nine hours of traffic)
against a full-day 28-day average with a flat -40% threshold, so an ordinary
day read as a ~60% drop. On 2026-10-12, 23 of 25 eligible sites would have
alerted. The paying customer had turned alerts off on 18 of 19 sites. Alerts
also ran on both cron triggers, so Mondays sent everything twice.

Fixing the window wasn't enough. Backtested on production history from
2026-07-01 to 2026-10-11, a complete-day -40% rule still fired ~420 times a
month: at 1-40 pageviews a day, a quiet day is normal. Replaced with:

- Tracking stopped: a reliably active site (traffic on >=20 of 28 days) has
  been silent longer than it ever was in the 28 days before. Fires once per
  outage, on the day the streak first exceeds the normal gap, and links to the
  installation check. 20 days lets weekday-only sites qualify.
- Spikes: sites averaging 10+/day, over double the average and >=3 standard
  deviations (floored at sqrt(mean)).

Replaying the same history through the shipped functions gives ~8 alerts a
month across ~24 sites (a quarter on Beam's own). The outage rule catches
69-89% of simulated 2-3 day outages. One bounded GROUP BY per site replaces
three queries, one of which scanned the site's entire history every day.
Alerts now run on the daily trigger only.

Billing
-------
customer.subscription.updated mapped every status except `active` to free.
A failed renewal (`past_due`, which Stripe retries for days) therefore dropped
a paying customer to free-tier collection caps, so their pageviews were
rejected with 402 while Stripe was still collecting. The billing page's
Stripe sync had the same bug via `status=active`. Now `active`, `trialing`
and `past_due` keep Pro; `canceled`, `unpaid`, `incomplete_expired` and
`paused` end it.

Tests run the real SQL against in-memory SQLite with every migration applied
(test/helpers/sqliteD1.ts). Against the old code, every test targeting one of
these bugs fails.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Node 20 reached end-of-life in April 2026, and the new tests use the built-in
node:sqlite module, which Node 20 doesn't have. Local development is already
on Node 24. Both jobs change, so the deploy runs on the same runtime that was
tested.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@scobb
scobb merged commit bcb5f1c into main Oct 12, 2026
2 checks passed
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.

1 participant