Repository navigation
perf(audit-engine): run a template-scoped rule once per cluster and fan its verdict out (#1951) - #279
Merged
Merged
Conversation
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
force-pushed
the
lane/1951-template-fanout
branch
from
September 8, 2026 07:39
3b84ba0 to
1cc8bfe
Compare
This was referenced Sep 8, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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"nowruns 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:
detachFromPageisdeliberately forgiving and returns its argument when
structuredClonethrows,which for its usual callers costs retention and never a wrong finding. Here it
would be a wrong finding: the member would stamp
pageUrlonto the cachedobjects 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.
pageUrl, so a member's stamp is a first writeexactly 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.tsfails a declaration that breaks itby running each declaring rule under two urls sharing only scheme and host.
security/sridecides"cross-origin" by comparing a resolved script's origin to the page's, so the
same markup at
https://andhttp://genuinely differs. Origin stays out ofpage_features.template_fp, which should still call those one template.runOneRuleturns a throw into a<ruleId>-errorcheck quoting the exception, which can carry page content.per-page where applicability is run-constant. No declared rule sets
skipOnSoft404, and a test keeps it that way.One rule demoted
core/charsetmoves from "template" to "page". Two pages with byte-identicalmarkup and no
<meta charset>, one servedContent-Type: text/html; charset=utf-8and the other bare
text/html, givepassandfail. The cluster key constrainsno 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 nomeasurement over those corpora could have caught it. #269's own
VerdictScopedocalready 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:
template-fanout-equivalence-golden.test.tstemplate-fanout-bench.ts --verifytemplate-verdict-page-independence.test.tsEqual 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(orfalse/off/no) turns it off for a run.The v1
runRulesOnStoragepath is untouched: it does not stream and has nocluster key.
Follow-ups filed
and differ in a declared rule's inputs. Unreachable on all three corpora,
constructible, and feat(rules): classify which page rules may fan out across a template cluster, and gate it #269's accepted premise rather than something this
introduces. The issue holds the options and the measurement they need.
filterpasses per invocation whether or notSQUIRREL_RULE_PROFILEis set.Part of squirrelscan/repo#1026.