Repository navigation
fix: traffic alerts that cry wolf, and past_due customers losing Pro - #9
Merged
Merged
Conversation
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>
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.
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 = 0on 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.updatedmapped every status exceptactivetofree. Onpast_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 tostatus=active.Alerts: fixing the window wasn't enough
I backtested candidate rules on production daily history, 2026-07-01 → 2026-10-11:
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:
GROUP BY dayper site replaces three queries, one of which scanned the site's entire history every day.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,trialingandpast_duekeep Pro.canceled,unpaid,incomplete_expiredandpausedend it. The webhook and the billing-page sync share one mapping (subscriptionKeepsPro). The sync no longer filters tostatus=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 typecheckis cleannpm test: 158 pass, 0 fail. New tests run the real SQL against in-memory SQLite with every migration applied (test/helpers/sqliteD1.ts).past_due/trialingand the billing-page sync. The tests that still pass pin behavior the old code also got right (canceledends Pro,alerts_enabled = 0is honored, unsigned webhooks are rejected).alerts_enabled. Two vacuous tests found this way were fixed.npm run test:smoke: 132/132, against a dev server withPUBLIC_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