Contents Lesson 14 of 19

8 min read · professional

An alert that fires 300 times is mute

By the end of this lesson you will have a cooldown, hysteresis and a two-check debounce in your evaluator, every firing keyed by rule and observation time, firings from one window delivered as one message, and two counts from a replayed flapping series: firings with the guards off, and with them on.

One failure makes people turn alerts off, and a switched-off alert is worse than one you never built.

The flapping problem

Price sits at 319.98 with a rule at 320.00. It ticks to 320.01, then 319.99, then 320.02. Each crossing is real. You get three alerts in ninety seconds and, on a busy day, three hundred.

Three hundred alerts is not a noisy alert. It is no alert, because the person stops reading them — and they will also stop reading the one that mattered.

Three mechanisms, and you need all three

Cooldown. After firing, do not fire the same rule again for N minutes. lastFiredAt from the previous lesson is exactly this. One parameter, sensible default of an hour for a price threshold.

Hysteresis. Require the price to move back past a different boundary before the rule can arm again. Fires at 320.00, re-arms below 319.00. A cooldown limits how often; hysteresis is what actually stops flapping, because it changes the condition rather than the timing.

Debounce on the observation. Require the condition to hold for two consecutive checks before firing. Costs you one interval of latency and removes single-tick noise entirely.

They solve different problems and layering all three is normal rather than excessive.

Idempotency, because the loop will run twice

Your evaluator will be run twice on the same data — a retry, an overlapping schedule, two tabs from the previous lesson. So make firing keyed:

fired_key = `${rule.id}:${observationTimestamp}`

Write it before sending, ignore duplicates. Same discipline as the ULID idempotency in any queue you will ever build, and it is the difference between "the scheduler ran twice" being invisible and being two notifications at 3am.

Group the delivery

Five rules firing in the same minute is one message with five lines, not five messages. Batch by a short window before delivery.

And put the number in it: "AAPL.US 320.15, crossed above 320.00, 14:32 UTC." Symbol, value, rule, time. A notification that says "your alert triggered" makes you open the app to find out what happened, which means it did not do its job.

Let the person see what did not fire

A rules list showing each rule's current value and distance from its threshold is worth more than the alerts. It answers "is this thing actually watching" without waiting for something to happen, and it is the surface where you notice a rule you set up wrong.

The finance behind it

Why a screen full of live opinion changes your judgement, which is the human half of alert fatigue: What does watching other people's opinions in real time do to yours?

Try it now

Replay a volatile day's data through your evaluator with cooldown and hysteresis switched off, and count the firings. Then switch them on and count again. That pair of numbers is the argument for this lesson, measured on your own rules.

The prompt

Mock mode on, plan mode on, and paste:

Plan, then build, flap control in src/lib/rules.ts. Each rule gets a
cooldown in minutes (default 60) and a re-arm level on the other side of
its threshold, and it fires only when its condition held on two checks
in a row. Key each firing as rule id plus observation timestamp, and
ignore a key already seen. Add digest(fired), which turns the firings of
one window into one message, a line each with symbol, value, rule and
UTC time. The dashboard's checks run with the guards on, and its rules
list shows each rule's current value and its distance from the
threshold. Write tests/flapping.test.ts first: a literal series of prices
crossing a threshold back and forth, one check a minute for less than an
hour so the whole series sits inside one cooldown, replayed through
checkAll with a fake clock, counting firings with the guards off and on,
each test named with its count. No real data: the series is written in the test. Run npm test, typecheck, lint and
check:leak, and show me the diff.

A good answer changes the rules module and checkAll, adds the test, and makes both counts readable in the test's names or output. The guards are on wherever the dashboard checks its rules; tests that want the old behaviour switch them off explicitly.

Check it yourself

  1. The flapping test's counts read many firings with the guards off and a single one with them on. The series is shorter than the hour-long cooldown; a longer one may honestly fire again once the cooldown ends.
  2. Replay the same series through checkAll twice: the second run adds no firing, because every key was already seen.
  3. A firing message names the symbol, the value, the rule and the time, and never says only "your alert triggered".
  4. The dashboard's rules list shows each rule's current value and its distance from the threshold.
Compare with ours
// src/lib/rules.ts: three guards for three problems, in the order they are checked
const holds = conditionHolds(rule, now);                         // the plain comparison
const confirmed = holds && rule.heldLastCheck;                   // debounce: two checks in a row
const cooled =
  rule.lastFiredAt === null || at - rule.lastFiredAt >= rule.cooldownMin * 60_000;
const fires = rule.armed && confirmed && cooled;
// Hysteresis: once fired, stay disarmed until the price is back past the re-arm level.
const armed = fires ? false : rule.armed || pastRearm(rule, now);
const key = fires ? rule.id + ":" + quote.timestamp : null;      // the loop will run twice

The full state at the end of this unit is on the starter's branch ref/build-a-dashboard/u3.

Checkpoint

npm test && npm run typecheck
git status          # the files you expect, and no .env file
git add -A
git commit -m "Cooldown, hysteresis and a two-check debounce; firings keyed and grouped"

Then answer this lesson's questions.