Contents Lesson 13 of 19

9 min read · professional

Why your alert did not fire

By the end of this lesson you will have a checkAll that takes the rules, a quote fetcher and the time as parameters, tested with a fake clock, a dashboard that says in one line that rules are checked only while the page is open, and your own alert loop priced in PLAN.md beside your allowance.

Your rules are documents and your evaluator is tested. The alert still will not work, and this lesson is about being honest regarding why.

Four things an alert needs

  1. Something that runs on a schedule. Not a page, which only runs when open.
  2. Data fresh enough that the alert is not already history when it arrives.
  3. Somewhere to keep state, so it does not tell you the same thing twice.
  4. A way to reach you when you are not looking at the tab.

Your cockpit has exactly one of these. Unit 1 gave you number 3 by moving persistence to the server. The other three are the subject of the rest of this course.

The browser cannot do this, and the reasons are worth knowing

It is not a limitation you can engineer around with a better setInterval:

  • A closed tab runs nothing you can schedule. No timer, and no worker you can wake on your own — a service worker only stirs when something outside pushes to it, and that something is a server you do not have.
  • A background tab is throttled. Browsers clamp timers in hidden tabs to about once a minute or less, so your five-second poll silently becomes something else.
  • A sleeping laptop runs nothing, and wakes up with a timer that thinks no time passed.
  • Two open tabs both evaluate, so you get two notifications and both write lastFiredAt.

The last one is the giveaway that this belongs somewhere else: correctness now depends on how many tabs you happen to have open.

Say it in the interface

One line, near the rules: "Rules are checked while this page is open."

This is the same honesty as Build Your First Tool, and it matters more now, because a cockpit looks like infrastructure. It has panels and alerts and a refresh indicator, and a person will reasonably assume it is watching for them. The worst outcome of this entire track is someone trusting an alert that was never going to fire.

What the real version costs, in calls

Price it before you build it, with the table from Build Your First Tool.

A server checking 12 symbols every 5 minutes during a 6.5-hour session: 78 checks × 12 symbols = 936 calls a day, since /real-time is billed per symbol. Every minute instead: 390 × 12 = 4,680.

Against a free allowance of 20 calls a day, neither is close. That gap is not a bug in your design — it is the wall, arriving with a number attached, and unit 4 is where you meet it properly.

Build the evaluator so it can move

Even though you are not deploying a scheduler in this course, write the evaluation loop so it does not care where it runs:

async function checkAll(rules: Rule[], fetchQuotes: Fetcher, now: number): Promise<Fired[]>

Time and fetching are parameters, not ambient. That makes it testable today with fixtures and a fake clock, and deployable tomorrow to a cron job without a rewrite.

This is Backtest Strategies's discipline again: a function whose inputs you control is a function you can test, and one that reads the clock directly is neither testable nor portable.

Try it now

Do the arithmetic for your own rules — symbols × checks per day — and write it in PLAN.md next to your allowance. That single line is the most useful thing in this unit, and it is the number the last lesson of the course comes back to.

The prompt

Mock mode on, plan mode on, and paste:

Plan, then build, src/lib/check-all.ts with checkAll(rules, fetchQuotes,
now). It asks fetchQuotes once for the distinct symbols of the enabled
rules, runs evaluate from src/lib/rules.ts on each rule, and returns what
fired with each rule's next lastValue. It reads no clock and imports no
fetch. Write tests/check-all.test.ts first with a fake fetcher and a
fixed time, including two rules on one symbol. In the dashboard, run
checkAll through the market store each time the dashboard's quotes
arrive, on load and when I press Refresh: no timer and no extra
request. Put "Rules are checked while this page is open." beside the
rules. Then,
with estimateCalls and no request, price a server checking my enabled
rules every five minutes through a 6.5-hour session, and every minute,
and write both under an Alerts heading in PLAN.md next to my daily
allowance. Run npm test, typecheck, lint and check:leak, and show me
the diff first.

A good answer adds src/lib/check-all.ts and its test, changes the container and PLAN.md, and states both prices, with the arithmetic, before writing them down.

Check it yourself

  1. grep -n "Date.now()\|new Date()\|fetch(" src/lib/check-all.ts prints nothing. Code that formats the time it was given is fine; reading the clock is not.
  2. In the test with two rules on one symbol, the fake fetcher was called once, with that symbol once in its list.
  3. The sentence about the page being open sits beside the rules on the dashboard.
  4. PLAN.md holds both prices and your allowance on neighbouring lines, and the agent showed its arithmetic before writing them.
Compare with ours
// src/lib/check-all.ts: time and fetching come in, so it can move to a scheduler unchanged
export async function checkAll(rules: Rule[], fetchQuotes: (s: string[]) => Promise<Quote[]>, now: number) {
  const live = rules.filter((r) => r.enabled);
  const quotes = await fetchQuotes([...new Set(live.map((r) => r.ticker))]);   // one request
  return live.map((rule) => {
    const quote = quotes.find((q) => q.code === rule.ticker);
    const verdict = quote ? evaluate(rule, quote) : { fires: false, nextLastValue: rule.lastValue };
    return { rule, ...verdict, checkedAt: now };
  });
}

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 "checkAll with time and fetching as parameters; the alert loop priced in PLAN.md"

Then answer this lesson's questions.