Skip to content

perf(audit-engine): run a template-scoped rule once per cluster and fan its verdict out (#1951) - #279

Merged
nc9 merged 3 commits into
mainfrom
lane/1951-template-fanout
Sep 8, 2026
Merged

nc9 merged 3 commits into
mainfrom
lane/1951-template-fanout

Conversation

@nc9

@nc9 nc9 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Implements squirrelscan/repo#1951, the payoff for the #1026 template-clustering
work. Builds on #267 (the cluster key) and #269 (the classification and its gate).

In the streamed page-rule pass, a rule declaring verdictScope: "template" now
runs ONCE per template cluster and its verdict is copied onto the cluster's other
members. On gymshark.com that removes 5,850 of 48,906 rule invocations.

What makes it sound

Fanning a verdict out asserts something about pages the rule never visited, so
copying is only allowed where the copy cannot carry the page it was computed on:

  • Each member gets its own copy, checked by IDENTITY. detachFromPage is
    deliberately forgiving and returns its argument when structuredClone throws,
    which for its usual callers costs retention and never a wrong finding. Here it
    would be a wrong finding: the member would stamp pageUrl onto the cached
    objects and every later member would read the first one's url, collapsing the
    cluster onto one page. Anything that comes back uncopied is dropped from the
    cache, and those pages run the ordinary path.
  • The cached copy carries no pageUrl, so a member's stamp is a first write
    exactly as it is on a page that really ran. Nothing rewrites urls INSIDE a
    copied check, because the normalisation that would have to be inverted is not
    injective — the requirement is that there is nothing to rewrite, and
    template-verdict-page-independence.test.ts fails a declaration that breaks it
    by running each declaring rule under two urls sharing only scheme and host.
  • The grouping key is (template cluster, page ORIGIN). security/sri decides
    "cross-origin" by comparing a resolved script's origin to the page's, so the
    same markup at https:// and http:// genuinely differs. Origin stays out of
    page_features.template_fp, which should still call those one template.
  • A rule that threw is never cached — runOneRule turns a throw into a
    <ruleId>-error check quoting the exception, which can carry page content.
  • The soft-404 gate is consulted before a fanned verdict, because it is
    per-page where applicability is run-constant. No declared rule sets
    skipOnSoft404, and a test keeps it that way.

One rule demoted

core/charset moves from "template" to "page". Two pages with byte-identical
markup and no <meta charset>, one served Content-Type: text/html; charset=utf-8
and the other bare text/html, give pass and fail. The cluster key constrains
no response header. It measures constant on gymshark.com, openelectricity.org.au
and the authored parity corpus only because every page of all three declares the
charset in a <meta> tag, so the header branch is never reached and no
measurement over those corpora could have caught it. #269's own VerdictScope doc
already disqualifies header-readers; this one was missed. It is the only one of
the 26 that reads ctx.page.headers. The counterexample is pinned as a test.

Evidence

Three falsifiers, because a gate only proves something about the pages it runs:

gate what it runs
template-fanout-equivalence-golden.test.ts the fanned pass vs a RESIDENT loop that runs everything on everything, over an authored corpus with three multi-page clusters and two singletons
template-fanout-bench.ts --verify the same comparison against real crawls: gymshark.com (247 pages, 13 clusters), openelectricity.org.au (100, 12), a 150-page synthetic (1) — byte-identical on all three
template-verdict-page-independence.test.ts each declaring rule under two different urls, plus the charset counterexample and the soft-404 exclusion

Equal output is also what a feature that does nothing produces, so the gates count
calls: a rule declaring "template" must run once per CLUSTER and an undeclared one
once per PAGE, asserted with two rules that differ only in that field.

Flag

ON for the streamed pass, which is what both the CLI and the cloud run.
SQUIRREL_TEMPLATE_FANOUT=0 (or false/off/no) turns it off for a run.
The v1 runRulesOnStorage path is untouched: it does not stream and has no
cluster key.

Follow-ups filed

Part of squirrelscan/repo#1026.

Nik Cubrilovic added 3 commits September 8, 2026 17:39
…an its verdict out

Implements squirrelscan/repo#1951, the payoff for #1026. Builds on #267 (the
cluster key) and #269 (the classification and its gate).

In the streamed page-rule pass a rule declaring `verdictScope: "template"` now
runs once per template cluster and its verdict is copied onto the cluster's other
members. On gymshark.com that removes 5,850 of 48,906 rule invocations.

Copying a verdict asserts something about pages the rule never visited, so it is
allowed only where the copy cannot carry the page it was computed on:

- Each member gets its own copy, checked by IDENTITY. `detachFromPage` returns its
  argument when `structuredClone` throws, which for its usual callers costs
  retention and never a wrong finding; here it would let a member stamp `pageUrl`
  onto the cached objects and collapse the whole cluster onto one page. Anything
  that comes back uncopied is dropped and those pages run the ordinary path.
- The cached copy carries no `pageUrl`, so a member's stamp is a first write.
  Nothing rewrites urls inside a copied check: the normalisation that would have to
  be inverted is not injective, so the requirement is that there is nothing to
  rewrite, and a new test fails a declaration that breaks it by running each
  declaring rule under two urls sharing only scheme and host.
- The grouping key is (template cluster, page ORIGIN). `security/sri` decides
  cross-origin by comparing a resolved script's origin to the page's, so the same
  markup at https:// and http:// genuinely differs. Origin stays out of
  `page_features.template_fp`, which should still call those one template.
- A rule that threw is never cached, and the soft-404 gate is consulted before a
  fanned verdict because it is per-page where applicability is run-constant.

`core/charset` is DEMOTED to page scope. Two pages with identical markup and no
`<meta charset>`, served `text/html; charset=utf-8` and bare `text/html`, give
pass and fail. The cluster key constrains no response header. It measures constant
on gymshark.com, openelectricity.org.au and the authored corpus only because every
page of all three declares charset in a meta tag, so the header branch is never
reached; #269's own VerdictScope doc already disqualifies header-readers and this
one was missed. It is the only one of the 26 that reads `ctx.page.headers`.

Three falsifiers, because a gate only proves something about the pages it runs:
the equivalence gate compares the fanned pass against a resident loop over an
authored corpus with three multi-page clusters and two singletons; the bench
script's --verify does the same against real crawls (byte-identical on gymshark,
openelectricity and a 150-page synthetic); the page-independence test covers url
dependence, the charset counterexample and the soft-404 exclusion. Equal output is
also what a feature doing nothing produces, so the gates count calls: a declaring
rule must run once per CLUSTER and an undeclared one once per PAGE.

ON for the streamed pass, which both the CLI and the cloud run.
`SQUIRREL_TEMPLATE_FANOUT=0` turns it off. v1 `runRulesOnStorage` is untouched.

Follow-ups: #275 (the cluster key is chrome only, so a constructed pair can share
it and differ in a declared rule's inputs), #274 (per-invocation profiler payload
built whether or not profiling is on).

Signed-off-by: Nik Cubrilovic <[email protected]>
…and compare stored findings by key

Two findings from a second adversarial review of the template fan-out, plus the
secret-scan baseline.

A rule declaring BOTH `verdictScope: "template"` and `skipOnSoft404` is a
contradiction: the soft-404 gate is decided per page and the cluster key says
nothing about it. The runner's gate ordering already protected the RECIPIENT — a
soft-404 member takes its own skip rather than a sibling's real verdict — but not
the reverse, where a soft-404 REPRESENTATIVE cached its skip and handed it to
healthy members, reporting "skipped: soft 404" for pages that are fine. No
built-in rule declares both and a named test in packages/rules keeps it that way,
but a plugin registering one through `additionalNamespaces` only meets this at
runtime, so the fan-out now excludes such a rule outright. A test with exactly
that rule asserts it runs on every page; removing the exclusion drops it to 5.

The stored-finding assertion compared sorted keys and sorted fingerprints as two
independent lists, which passes on precisely the misattribution it exists for.
It now indexes both arms by (normalizedUrl, ruleId, checkName, locator) and
compares whole rows. Verified by mutation: moving a fingerprint onto a finding
that really has a different one now fails. The comment records what this can and
cannot show — a fingerprint swapped between members of ONE cluster is
undetectable on any corpus, because those fingerprints are legitimately equal by
the classification's own claim. The key set is the load-bearing half: a finding
stored against the representative's url shows up as a missing key.

The test file had also acquired three raw NUL bytes from an earlier edit, which
made it `file`-binary and silenced grep over it — which is how the first fix was
lost without any tool reporting a failure. Restored to the source escape; the
runtime value is unchanged.

`.secrets.baseline` is the line-number drift detect-secrets rewrites when a diff
moves lines in a file with baselined entries. No new candidate: the diff is two
lines of `line_number` and `generated_at`.

Signed-off-by: Nik Cubrilovic <[email protected]>
…th (#1951)

12.9% of the page-rule pass on gymshark.com and 17.6% on a single-template
synthetic, CPU time, minimum of 7 and 5 interleaved repeats, load average 2.2 to
3.8. Rule invocations fall from 48,906 to 43,056: each of the 25 declared rules
goes from 247 calls to 13. Three rules that scan the page's whole HTML are most of
it, and the 6,058 ms of rule bodies removed accounts for 93% of the CPU the same
profiled run moved.

Records why the number is 13% and not the 96.5% of page-rule time that sits on
non-first cluster members: that is the ceiling over ALL rules, and only 25 of 198
are declared. Also records the core/charset demotion and the residual risk in

Signed-off-by: Nik Cubrilovic <[email protected]>
#275, and updates #269's paragraph, which said 26 rules were declared.
@nc9
nc9 force-pushed the lane/1951-template-fanout branch from 3b84ba0 to 1cc8bfe Compare September 8, 2026 07:39
@nc9
nc9 merged commit 2c7b339 into main Sep 8, 2026
13 checks passed
@nc9
nc9 deleted the lane/1951-template-fanout branch September 8, 2026 07:46
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