Skip to content

perf(audit-engine): replay an unchanged page's rule results instead of re-running them (#1990) - #295

Merged
nc9 merged 7 commits into
mainfrom
perf/1990-rule-result-cache
Sep 9, 2026
Merged

nc9 merged 7 commits into
mainfrom
perf/1990-rule-result-cache

Conversation

@nc9

@nc9 nc9 commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

A second audit of an unchanged site already fetches nothing. It was not faster, because parse, page rules, the page-time collectors and report reconstruction re-ran in full on every page whether or not that page changed. Crawl reuse is a bandwidth feature; this is the one that makes a re-audit cheap.

A page now replays its stored rule results when every input its rules read is unchanged. It is never parsed and its rules never run. Site-scope rules always run.

What it is worth

2,500 pages of the mixed synthetic estate, both arms on the SAME build with SQUIRREL_RULE_CACHE=0 as the only difference, one pinned origin per pair, fresh content store per pair, load average 2.4 to 4.1.

stage wall rules phase report phase project.db
cache off, cold 178 s 116 s 22 s 258 MB
cache off, warm 143 s 110 s 18 s 514 MB
cache on, cold 177 s 124 s 19 s 291 MB
cache on, warm 44 s 12 s 18 s 581 MB

The warm rules phase falls from 110 s to 12 s, against the 50% the issue asked for. All 2,500 pages replayed, and the cache-on cold and warm reports are byte-identical across all 3,039,997 bytes once meta.timestamp and the replay disclosure are removed.

Full write-up in benchmarks/2026-09-perf-program.md.

NIK: this turns template fan-out (#279) off whenever the cache is on

They do not compose, and the reason is structural rather than fixable here. A fanned verdict belongs to the page's CLUSTER, so no per-page key can capture what it depends on: cache two pages of one cluster, change the FIRST one, and the second page replays the verdict it inherited from the representative's previous run while a fresh audit gives it the new one. Nothing about the second page changed, so nothing about its key can notice. Two designs that tried to keep both were written and rejected; the review reproduced the failure in each.

The trade, measured in the cold row above: the rules phase goes from 116 s to 124 s, 6.9%, with cold wall time unchanged. Every audit after the first takes the 89%. It also makes the report the one every rule actually computed rather than one where some verdicts were inherited: the cache-on cold report is 3,039,997 bytes against 3,043,863 with fan-out on, which is #275's open question about the chrome-only cluster key made visible.

If you would rather keep fan-out, SQUIRREL_RULE_CACHE=0 does that today and the alternative is the follow-up design (cache the cluster's verdict against its representative's identity). Say the word and I will file it.

What makes it sound

The key covers every input a page rule reads:

  • The page's exact HTML bytes. NOT content_hash, which is whitespace-NORMALIZED so the incremental crawler can call a reformatted page unchanged. Two pages differing only in whitespace share it and parse to different word counts, inline-script lengths and <pre> text, so keying on it would have replayed one page's verdicts onto another's markup — with every existing gate green, because no fixture contains that pair. A new pages.html_hash carries the exact hash, and it costs nothing to produce: the content store is already addressed by sha256(bytes).
  • The rest of the page record the rules and extractPageFeatures read, including the three response timings, because performance/ttfb reads them. That is why replay pays off on a REUSED page and not on a re-fetched one, and the benchmark shows why it must: two independent crawls of the same estate disagree on 27 pages of performance/ttfb.
  • The run context: the build (the CLI's release version, since @squirrelscan/rules has sat at 0.0.1 forever), the enabled page rules in order with their resolved options, the applicability escape hatch, the Stage-0 metadata, the prefetched cloud results, the SiteData entry fields page rules read, the current UTC year and the runtime time zone.

Gated out rather than approximated: a run with threat intel enabled does not replay at all. IntelContext is two functions over feeds that refresh daily behind a memoized handle, so its configuration identity is not an identity of its verdicts.

Review

Sixteen findings across three adversarial codex passes, all fixed. The third returned nothing above P2. Beyond the fan-out story:

  • content/date-agreement resolves a bare schema date through Date.parse, which reads it in LOCAL time, so the same page yields a different year under UTC and under Australia/Sydney. The time zone is in the key.
  • A long audit can cross UTC New Year while it runs, where content/stale-copyright reads the clock itself. Replay stops for the rest of such a run rather than freezing a year the rule cannot be told about.
  • rules.ignore_applicability changes a gated rule from a skipped check to its real verdict without touching the rule list or any option.
  • The warm lookup picks one row per key in SQL. It was selecting every retained audit's copy and decompressing all of them, which is work that grows with history rather than with the crawl.
  • The codec keeps Dates (including invalid ones, which serialize to null) and own __proto__ keys; the canonical form no longer merges NaN/Infinity/-Infinity, [] with new Array(1), a hole with undefined, or -0 with 0.

Gates

  • rule-cache-replay-golden.test.ts — cold, warm-populating and warm-replaying runs over one crawl; the full report must be byte-identical AND the replayed-page count must be right, because a cache that silently does nothing produces identical findings. Plus the template-cluster composition case, the exclusivity itself, and one invalidation case each for content (a whitespace-only edit), engine version, rule selection, rule options and applicability. The composition test was mutation-checked: it fails with the fix removed.
  • page-rule-site-context.test.ts — the declaration falsifier. Runs the real page-rule pass with SiteData behind a recording proxy, down to individual script and resource entries, and fails if a rule reads a field the key does not cover.
  • page-rule-cache-store.test.ts — the SQL half: gzip round-trip, carry-forward surviving retirement of the crawl it came from, retirement reclaiming the rows, chunked lookup, one payload per key across ten crawls, and html_hash null without a content store and moving on a whitespace-only edit.
  • rule-cache-codec.test.ts — SHA-256 vectors, canonical-form sensitivity, run-context ingredients, and the tagged codec.
  • Re-run green: the canonical 518-page v1↔v2 merge gate, both template fan-out gates, the streaming page-rule and collector goldens, the crawler retire and migration tests.

Surface

  • report.rulesCache — { pagesReplayed, pagesEvaluated, disabledReason? }, in the JSON meta and as <rules-cache/> in the LLM report. An agent cannot otherwise tell a replay from a fresh audit (fix(core-contracts): pin LINK_TARGET_SET_RULES to the link rules' emitted check names (repo#3022) #1981). LOCAL ONLY: slimForPublish strips it, because it describes how this machine produced the report rather than the site.
  • --refresh bypasses it, alongside the page cache. SQUIRREL_RULE_CACHE=0 is the kill switch, mirroring SQUIRREL_TEMPLATE_FANOUT. SQUIRREL_RULE_CACHE_DEBUG=1 prints a digest per run-context ingredient, which is how a cache that silently stops hitting gets diagnosed — it found one such defect during this work.
  • Docs: a "Rule results" section under Caching in docs/cli/audit.mdx. make validate passes.

Schema 28 adds page_rule_cache and pages.html_hash, both additive, both in the ALTER-columns guard, and page_rule_cache is retired with its crawl by retireCrawls. Every audit writes an entry for every page it scored, replayed pages included by SQL row copy rather than re-encoding, so pruning older audits never makes the next one cold.

Rebased onto the automatic retention window (#285) that landed while this was in review, and verified against it: five audits of a 40-page project leave two crawls retired, cache rows under exactly the three that remain, and every audit after the first still replays every page. That is what the carry-forward is for. The benchmark table was taken just before retention landed; retention adds tens of milliseconds to an audit, against a 44-second warm run.

The cloud passes no store and is unchanged: it has no project.db to cache into, and html_hash is written only on the content-store path, so nothing is added to the crawl that #1862 made memory-critical.

Closes squirrelscan/repo#1990.

Nik Cubrilovic added 5 commits September 8, 2026 20:56
…f re-running them #1990

A second audit of an unchanged site already fetched nothing and was not faster:
parse, page rules, the page-time collectors and report reconstruction re-ran in
full on every page whether or not that page changed. Crawl reuse is a bandwidth
feature; this is the one that makes a re-audit cheap.

A page now replays its stored rule results when every input its rules read is
unchanged. It is never parsed and its rules never run. Site rules always run.

At 2,500 pages, both arms on this build with SQUIRREL_RULE_CACHE=0 as the only
difference: the warm rules phase falls from 131 s to 12 s and the warm run from
171 s to 44 s, with the report byte-identical to its cold counterpart across all
3,044,401 bytes. The cold cost is below what the box can resolve.

The key covers the page's EXACT html bytes via a new pages.html_hash, not
content_hash, which is whitespace-normalized so the incremental crawler can call
a reformatted page unchanged; the rest of the page record the rules and
extractPageFeatures read, including the three timings performance/ttfb reads; and
a run context of the build version, the enabled page rules with their resolved
options, and the SiteData entry fields page rules read. A run with threat intel
enabled does not replay at all: IntelContext is two functions over feeds that
refresh daily, and its configuration identity is not an identity of its verdicts.

Template fan-out composes by abstention: a replayed page neither takes a cluster
verdict nor becomes a representative.

Signed-off-by: Nik Cubrilovic <[email protected]>
#1990

P1, and the interesting one: a replayed page has to RECORD into the template
fan-out, not abstain from it. Abstention makes which page runs a template-scoped
rule depend on what happens to be cached — on the audit after one page changes,
the changed page is the only fresh one and keeps its own verdict, where a fresh
audit would have the cluster's first page fan its verdict onto it. The next
fully-replayed audit then disagrees with a fresh audit of identical content and
persists the disagreement. A replayed page now records, keyed on the
page_features.template_fp its own run stored, which reproduces the fresh audit's
election exactly. Stored checks drop their own pageUrl stamp so what the fan-out
caches stays page-identity-free, and the replay re-stamps as the fresh path does.
Gated by a test that adds a viewport meta to one page of a cluster; the gate was
mutation-checked and fails without the fix.

P1: rules.ignore_applicability changes a gated rule from a skipped check to its
real verdict without touching the rule list or any rule's options, so it is now
part of the run context.

P2, five: the current UTC year is in the run context, because
content/stale-copyright reads it at execution and is the only page rule that
reads a clock; pages.content_hash and pages.parsed_data joined the page key, the
first because extractPageFeatures copies it verbatim and the second because
buildSiteContext prefers it over the HTML; the codec keeps Dates and own
__proto__ keys, both of which a plain-object walk drops; canonicalJson no longer
merges NaN/Infinity/-Infinity or [] with new Array(1); and the warm lookup picks
one row per key in SQL instead of decompressing every retained audit's copy,
which was work that grew with history rather than with the crawl.

Benchmarks re-measured on the fixed code, quiet box: the warm rules phase falls
from 106 s to 13 s at 2,500 pages and the report is byte-identical to its cold
counterpart.

Signed-off-by: Nik Cubrilovic <[email protected]>
…exclusive #1990

The second review round showed the fan-out fix closed only one direction. A
fanned verdict belongs to the page's CLUSTER, so no per-page key can capture what
it depends on: cache two pages of one cluster, change the FIRST one, and the
second replays the verdict it inherited from the representative's previous run
while a fresh audit gives it the new one. Nothing about the second page changed,
so nothing about its key can notice.

Enabling the cache therefore turns fan-out off for that run. Cost, measured: the
cold rules phase goes from 116 s to 124 s at 2,500 pages, 6.9%, and cold wall time
is unchanged; every audit after the first takes the 89% the cache buys. It also
makes the report the one every rule actually computed rather than one where some
verdicts were inherited, which is #275's open question made visible.

With nothing inheriting a verdict, the pageUrl stripping and record-on-replay the
previous commit added are gone: a stored check is what its own page produced.

Also from the second round: an invalid Date no longer decodes to the epoch, array
holes and -0 no longer canonicalize onto their neighbours, and the year-boundary
straddle is documented as bounded (the run stores under the old year's key, and
the next audit misses every one of those rows rather than serving one).

Gates: the composition test now compares like configurations, and a new one pins
the exclusivity directly - with a cache supplied the fan-out does zero work, which
an output diff cannot be relied on to show.

Signed-off-by: Nik Cubrilovic <[email protected]>
…aying across a year rollover #1990

Third review pass, both reproduced. content/date-agreement resolves a bare schema
date through Date.parse, which reads it in LOCAL time, so the same page yields a
different year under UTC and under Australia/Sydney: the runtime time zone is now
part of the run context. And a long audit can cross UTC New Year while it runs,
where content/stale-copyright reads the clock itself and a page replayed after
midnight would serve last year's verdict; rather than freeze a year the rule
cannot be told about, replay stops for the rest of such a run and those pages are
audited normally.

Also drops a header comment still describing the fan-out composition design that
was rejected two commits ago.

Signed-off-by: Nik Cubrilovic <[email protected]>
…1990

Five audits of a 40-page project after rebasing onto automatic retention (#285):
two crawls retired, cache rows under exactly the three that remain, and every
audit after the first still replays every page. The carry-forward is what makes
retiring the crawl an entry came from harmless.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the perf/1990-rule-result-cache branch from cbfd593 to beac093 Compare September 8, 2026 10:58
…1990

The entropy detector cannot tell a digest from a credential, and the vectors for
"" and "abc" are neither secret nor ours. The documented mitigation is the inline
pragma; detect-secrets-hook passes locally with it.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 merged commit 400617c into main Sep 9, 2026
13 checks passed
@nc9
nc9 deleted the perf/1990-rule-result-cache branch September 9, 2026 04:01
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