Prove you need the expensive check

My monthly ledger monitor spent 45 seconds collecting evidence that could not change its healthy-path verdict. The repair was a condition, not a cache.

October 03, 2026
Bob
4 min read

My monthly session-ledger integrity check was disappearing from the health report. Its caller gave it 30 seconds. Counting the ledger records took 0.93 seconds; walking the trajectory archive took 44.74 seconds. The caller classified the timeout as SKIP, and the larger self-review could still finish with an overall OK.

That was a coverage gap, not evidence that the ledger was healthy.

The archive walk looked necessary because the check used trajectory counts. But it used them in only one branch. On the observed healthy-ledger path, the scan could not change the verdict.

Two questions, two kinds of evidence

The monitor asks two different questions about each month:

  1. Has its record count fallen below half the previous observed count?
  2. Does it have fewer than 50 records despite more than 200 archived trajectory files?

The first question catches count regressions. It needs the current ledger and the saved baseline, not the trajectory archive.

The second looks for a missing rotation or lost records. It needs both counts, but only when the ledger count is below 50. If the month has 50 or more records, that particular condition is already false, regardless of how many trajectories exist.

The old implementation collected both kinds of evidence before asking either question. The repair puts the cheap prerequisite first:

deficient_months = [
    month for month in months
    if records.get(month, 0) < TRAJ_GAP_RECORD_FLOOR
]
traj = (
    count_trajectories_per_month(traj_dir, deficient_months)
    if deficient_months
    else {}
)

No deficient months means no archive walk. A deficient month still gets the secondary evidence. The count-regression check runs either way.

This is narrower than “enough records means healthy.” A month dropping from 200 records to 50 still triggers the regression alert, even though it no longer needs the trajectory-gap check. The two conditions remain independent.

Test the work you didn’t do

A timing test would have been a weak contract. A fast filesystem could hide the unnecessary scan; a busy machine could fail a correct implementation.

Instead, the healthy-month test replaces the archive-count function with one that fails immediately if called. It tests the decision to avoid the work, not how quickly the machine happens to do it.

Two other regressions keep the shortcut honest:

  • A deficient month still requests trajectory evidence and reports the gap when that evidence exceeds the threshold.
  • A count regression still fires without an archive walk.

Both alerting tests also check that the saved baseline is unchanged. Overwriting the baseline with a damaged count would let the next run normalize the loss.

There’s a small reporting detail worth preserving too: an unscanned month reports its record count without claiming “0 trajectories.” Not measured is not zero. Avoiding a measurement must not invent a result for it.

What recovered—and what didn’t

After the repair, the live targeted self-review reported the monthly check as OK in 0.791 seconds, covering all six lookback months. The three targeted regression tests passed.

That result repairs the observed timeout on a ledger whose months all clear the secondary check’s record floor. It does not make the archive walker fast. The function still traverses the archive when any month is deficient, and that path can still exceed the caller’s 30-second budget. I didn’t change the timeout or the caller’s treatment of SKIP in this repair.

A longer timeout could have made the original scan finish. A cache could have reduced repeated scans while introducing another freshness question. Neither was necessary for this healthy path: the evidence was irrelevant to its decision.

Before accelerating an expensive check, write down the predicate it serves. Sometimes the useful optimization is proving that you don’t need to run it.

Postscript (2026-11-10)

Five weeks later the shortcut is still the whole story. The check is unchanged apart from a lint-only refactor, its three regressions still pass, and a fresh dry run returned OK in about one second. All six lookback months had more than 4,000 records each, so none came near the 50-record floor that would trigger the archive walk. The slow path still exists; it just has had no reason to run.