Skip to content

feat(rules): classify which page rules may fan out across a template cluster, and gate it - #269

Merged
nc9 merged 3 commits into
mainfrom
lane/1950-verdict-scope
Sep 8, 2026
Merged

nc9 merged 3 commits into
mainfrom
lane/1950-verdict-scope

Conversation

@nc9

@nc9 nc9 commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Targets main, rebased onto #267 now that it has merged. Carries one follow-up fix to #267 census script that codex found here: it opened the corpus writable (SQLiteStorage.init() runs migrations) and accepted a malformed --batch.

Tracks squirrelscan/repo#1950, part of squirrelscan/repo#1026.

Every page rule now declares meta.verdictScope: "template" | "page". Nothing fans out yet: this ships the classification and the two falsifiers that keep it honest. mayFanOutAcrossTemplate is the only reader and is closed by construction, so a site rule and an undeclared page rule both answer false.

The classification is deliberately narrower than the measurement

All 198 page rules were run on every page of two real crawls and grouped by the #1949 cluster key. 85 rules are constant in every multi-page cluster of both. 26 are declared "template".

The 59 demoted are constant on those crawls and page-scoped anyway, in four families:

why examples
Response headers, which the cluster key constrains not at all perf/cache-headers, security/cookie-flags, perf/source-maps, perf/http2
The page url, per-page by construction url/parameters, url/special-chars, security/https
Anything inside main content, which is what siblings differ in by definition security/mixed-content, security/form-https, legal/terms-of-service, most a11y/aria-*, every images/*, every schema/*
Detection-only rules that emit nothing when clean, so fan-out would assert that a page nobody looked at is clean integrity/obfuscated-script, integrity/kit-signature

That is the load-bearing judgement in this PR and it is stated on the VerdictScope type, next to the rules, rather than in a changelog. Widening it is a product call, not a code change: everything needed to widen it safely is here.

One comparison, two falsifiers

templateVerdictKey compares the whole check — message, value, expected, items, details — with only the page's own url and path normalised away. An earlier version compared name, status and message only, and two pages with different <meta refresh> destinations compared equal, because the evidence that distinguishes them lives in items. Both falsifiers import it, so they cannot drift.

  • packages/audit-engine/tests/template-fanout-parity-golden.test.ts runs in CI over an authored 13-page corpus: three templates of 4, 4 and 3 members plus two singletons, members differing the way real siblings do (more paragraphs, more images, more links, different titles, different JSON-LD, an image missing alt). It checks its own adversarialness before it is trusted: three real clusters, at least 15 page-declared rules genuinely disagreeing, and every declared rule compared on a non-empty verdict across two members of a cluster. Failure names the rule, the cluster and the two members.
  • apps/cli/scripts/template-rule-invariance.ts --check runs the same comparison against a real crawl, exits 2 on a falsification and 3 when the corpus could not falsify anything (no multi-page clusters, or a declared rule never compared). It probes a disposable copy of the database, because SQLiteStorage.init() opens the file writable and runs migrations.

The gate earned its keep twice

  • perf/compression is in the issue's own list of constant rules and is constant on both corpora, because every page of both is content-encoding: br so the rule emits one fixed message. Uncompressed, its message interpolates the page's byte count. The authored fixture caught it on the first run; neither real corpus could.
  • perf/critical-request-chains varies inside a gymshark cluster under the strict verdict.

Both are page-scoped.

The measurement is checked in

The crawls are third-party site content and cannot be, so the result is: packages/rules/tests/fixtures/template-invariance-measured.json.

corpus pages multi-page clusters rules constant
gymshark.com 247 8 89 of 198
openelectricity.org.au 100 3 142 of 198

rule-verdict-scope.test.ts asserts every declaration against both, so a rule whose inputs change and start varying becomes a named CI failure rather than silent fan-out. It also asserts every page rule declares, that no site rule does, and that an absent declaration never fans out.

(#1026 reported 101 and 150 comparing name/status/message only. This comparison is strictly stricter, hence the smaller sets.)

No behaviour change

toReportRuleResults projects rule meta onto an explicit field list, so verdictScope never reaches a published payload. Report output is byte-identical.

Reviewed by codex; seven findings, all addressed. Five of them are why the declared set went from 56 to 26 and why the verdict comparison includes items and details.

@nc9
nc9 changed the base branch from lane/1949-template-fp to main September 8, 2026 02:44
@nc9
nc9 force-pushed the lane/1950-verdict-scope branch 2 times, most recently from c210195 to 1cf8e21 Compare September 8, 2026 02:46
nc9 added 3 commits September 8, 2026 13:00
…-batch

Codex on #1950 found both in the sibling probe and they are the same two here.

`SQLiteStorage.init()` opens the file WRITABLE, switches it to WAL and runs
migrations, so censusing a corpus silently upgraded someone's crawl and a
mistyped path created an empty database instead of failing. It now copies the
db (and its -wal/-shm sidecars) to a scratch dir and reads that.

`parseInt` turns "1.5" into 1, "50garbage" into 50 and "1e3" into 1, all of
which pass the is-integer check that followed it. Validate the argument as text.

Re-verified: gymshark 247 pages -> 13 clusters, GROUP BY agrees;
openelectricity 100 -> 12, agrees.

Signed-off-by: Nik Cubrilovic <[email protected]>
…cluster #1950

Every page rule now declares `meta.verdictScope: "template" | "page"` — whether
its verdict is a property of the page's template, so #1951 may run it once per
cluster and fan the verdict to the members, or of the page. Nothing fans out
yet: this ships the classification and the two falsifiers that keep it honest.

`mayFanOutAcrossTemplate` is the only reader and is closed by construction: a
site rule and an undeclared page rule both answer false, so silence can never
become fan-out.

26 of 198 page rules are declared "template", not the 85 that are constant on
both real corpora. Empirical constancy over two crawls is a property of those
crawls: response headers are not constrained by the cluster key at all
(Cache-Control, Set-Cookie, SourceMap), the page url is per-page by
construction, anything inside main content is what siblings differ in by
definition, and a detection-only rule that emits nothing when clean would be
fanning out silence about a page nobody looked at. VerdictScope documents the
line.

Two falsifiers, sharing one comparison (`templateVerdictKey`) so they cannot
drift. It compares the WHOLE check — message, value, expected, items, details —
with only the page's own url and path normalised away; comparing name, status
and message alone let two pages with different meta-refresh destinations pass
as equal.

- `template-fanout-parity-golden.test.ts` runs in CI over an authored 13-page
  corpus: three templates of 4, 4 and 3 members plus two singletons, members
  differing the way real siblings do. It checks its own adversarialness first
  (three real clusters, >=15 page-declared rules genuinely disagreeing, every
  declared rule compared on a non-empty verdict across two members) and names
  the rule, the cluster and the two members on failure.
- `apps/cli/scripts/template-rule-invariance.ts --check` runs the same
  comparison against a real crawl and exits non-zero on a falsification or when
  the corpus could not falsify anything.

The gate earned its keep twice: `perf/compression` is constant on both real
corpora only because every page there is br-compressed, and its failure message
carries the page's byte count; `perf/critical-request-chains` varies inside a
gymshark cluster. Both are page-scoped.

The crawls cannot be checked in, so the measurement is recorded in
`tests/fixtures/template-invariance-measured.json` (gymshark 247 pages, 8
multi-page clusters, 89/198 constant; openelectricity 100 pages, 3 clusters,
142/198) and every declaration is asserted against both.

Report output is unchanged: `toReportRuleResults` projects rule meta onto an
explicit field list, so the new field never reaches a published payload.

Part of squirrelscan/repo#1026. Depends on #1949.

Signed-off-by: Nik Cubrilovic <[email protected]>
The number that decides the design: on a real storefront chrome clustering is
94.7% redundant and an exact DOM skeleton is 6.5%, and the synthetic bench
corpora report ~99% for both because they are generated from 1-6 templates.
Sizing this feature there would have shipped skeleton dedupe worth nothing.

Also records where the compute goes (96.5% of gymshark's page-rule time is on
non-first cluster members), how many rules are actually cluster-constant (89 of
198 on gymshark, 142 on openelectricity, 26 declared safe to fan out), and what
storing the key costs (0.04-0.08 ms/page against a walk already paid).

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the lane/1950-verdict-scope branch from 35b2091 to 1cb9740 Compare September 8, 2026 03:00
@nc9
nc9 merged commit 1f30d50 into main Sep 8, 2026
13 checks passed
@nc9
nc9 deleted the lane/1950-verdict-scope branch September 8, 2026 03:07
nc9 pushed a commit that referenced this pull request Sep 8, 2026
One conflict, and it is additive: #269 and #1864 each appended a section to the
benchmark record just before "Still open". Both kept, template clustering first.

Signed-off-by: Nik Cubrilovic <[email protected]>
nc9 pushed a commit that referenced this pull request Sep 8, 2026
…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 pushed a commit that referenced this pull request Sep 8, 2026
…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]>
nc9 pushed a commit that referenced this pull request Sep 8, 2026
…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 added a commit that referenced this pull request Sep 8, 2026
…an its verdict out (#1951) (#279)

* perf(audit-engine): run a template-scoped rule once per cluster and fan 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]>

* fix(audit-engine): never fan out a rule that is also soft-404 gated, 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]>

* docs(benchmarks): record what running a rule once per template is worth (#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.

---------

Signed-off-by: Nik Cubrilovic <[email protected]>
Co-authored-by: Nik Cubrilovic <[email protected]>
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