Skip to content

perf(audit-engine): detach what the page loop retains from the page it came from - #234

Merged
nc9 merged 2 commits into
mainfrom
perf/detach-collected-signals-1860
Sep 7, 2026
Merged

nc9 merged 2 commits into
mainfrom
perf/detach-collected-signals-1860

Conversation

@nc9

@nc9 nc9 commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

For squirrelscan/repo#1860 (epic #1866). This is the PR-F work, and it turned out not to be about capping findings at all.

What was actually holding the memory

Measurement run #2 established that the rules phase's rise is genuinely retained — post-collect RSS equals pre-collect — at roughly 8 MB per page, released only after the site pass. The obvious explanation is the volume of findings, but a census of the retained structures reports 96 KB per page.

The real cause: every string a rule extracts from a parsed page is a slice of that page's HTML, and JSC keeps a slice attached to the buffer it came from. Retaining one 20-character href retains the whole ~1 MB page as UTF-16.

This is invisible to every obvious measure, which is why it survived three rounds of looking:

  • JSON.stringify(x).length reports the logical size, so a census says 1 KB for a value holding 3.8 MB.
  • RSS reports nothing at all — the arena absorbs it. Every local run I did on RSS showed a flat page loop.
  • It appears only in heapUsed / external, which grow linearly with page count while RSS oscillates.

The fix

structuredClone at the two points where a per-page value outlives its batch: the collected site signal and the per-page findings. It detaches every string, and unlike a JSON round-trip it preserves the Sets in PageFingerprint rather than silently turning them into {}.

before after
retained 4020 KB/page 213 KB/page
peak in loop 499 MB 120 MB

Measured on a 150-page fixture built from a real saved drscholls page — 959 KB, 1051 nodes, almost all inline script. That is the opposite shape to the synthetic fixture used earlier (983 KB, 18,917 nodes), which could not reproduce this at all. The fixture also stores parsedData the way the crawler does, because buildSiteContext reuses it and a fixture without it measures a path production does not take.

Scope of the clone, which cost me a first attempt

Cloning the whole rule result silently did nothing. meta.optionsSchema is a Zod instance, 38 of 282 rules carry one, structuredClone throws on it, and one failure aborts the entire clone — so the fallback returned the original, findings unchanged and still attached to the page. No golden test can see that, because the findings are correct either way. It was caught by review, not by the suite.

So only the checks graph is cloned:

  • meta stays by reference — it is per-rule, not per-page, so it retains nothing.
  • parsed is excluded — nothing here reads it, and cloning it would copy the live document and strip SchemaCollection's methods.
  • One clone for the whole graph, so pageResults, pageRuleResults and ruleResultsMap keep referencing the same check objects instead of holding three copies. The code stamps check.pageUrl after the clone and relies on that identity.

Why this is safe

Every golden and parity gate passes unchanged — the byte-identity diff against v1, the 518-page baseline, the site-query universe diff, the collector-rules golden, the scoring golden and the complete-store parity. That is the point: the values are identical, only their attachment to the page is not.

tests/detach-from-page.test.ts pins the contract: Sets survive, values round-trip, sharing within one value is preserved, a non-cloneable value falls back to the original rather than throwing. Its memory assertion only runs where the attached case actually demonstrates the retention, since the measurement is environment-dependent and comparing two zeroes proves nothing.

Known remaining

When a page has no stored parsedData, the universe retains re-extracted strings the same way. Production stores it, so that is the CLI's path and a fallback. Detaching it needs the schemas rehydrated after the clone rather than a bare structuredClone, so it is deliberately not in this PR.

nc9 added 2 commits September 7, 2026 21:04
…t came from

The streamed rules pass held ~8 MB per page in production across the whole
page-rules loop, released only after the site pass. It is not the volume of
findings: a census of the retained structures reports 96 KB per page. It is
that every string a rule extracts from a parsed page is a SLICE of that page's
HTML, and JSC keeps a slice attached to the buffer it came from. Retaining one
20-character href retains the whole ~1 MB page as UTF-16.

That is invisible to every obvious measure, which is why it survived three
rounds of looking: `JSON.stringify(x).length` reports the logical size, and RSS
reports nothing at all because the arena absorbs it. It shows up only in
`heapUsed`/`external`.

Fixed at the two points where a per-page value outlives its batch — the
collected site signal, and the per-page findings — with structuredClone, which
detaches every string and, unlike a JSON round-trip, preserves the Sets in
PageFingerprint instead of silently emptying them.

Measured on a 150-page fixture built from a REAL saved drscholls page (959 KB,
1051 nodes, mostly inline script — the opposite shape to the synthetic fixture,
which had 18,917 nodes and could not reproduce this at all), with parsedData
stored as the crawler stores it:

  retained     4020 KB/page -> 213 KB/page   (19x)
  peak in loop  499 MB      -> 120 MB

Scope of the clone matters and cost me a first attempt. Cloning the whole rule
result silently did nothing: `meta.optionsSchema` is a Zod instance, 38 of 282
rules carry one, structuredClone throws on it, and one failure aborts the whole
clone — so the fallback returned the original, findings unchanged and still
attached. Golden tests cannot see that. Only the checks graph is cloned now,
with meta kept by reference (it is per-rule, not per-page, so it retains
nothing) and `parsed` excluded (nothing reads it here, and cloning it would
copy the live document and strip SchemaCollection's methods). One clone for the
whole graph, so pageResults, pageRuleResults and ruleResultsMap keep sharing
the same check objects rather than holding three copies.

Every golden and parity gate passes unchanged, which is the point: the values
are identical, only their attachment to the page is not.

Known remaining, pre-existing: when a page has no stored `parsedData` the
universe retains re-extracted strings the same way. Production stores it, so
that path is the CLI's and a fallback; detaching it safely needs the schemas
rehydrated afterwards rather than a bare clone.

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

Review findings on the detach change (#1860).

A `structuredClone` failure returned the original silently. That is
all-or-nothing per call, so ONE non-cloneable value in a check's `details`
restores page-sized retention for every page carrying it, with identical
findings and a green golden gate. Both call sites now name their boundary,
successes and fallbacks are counted per boundary, and the first three
fallbacks log the error TYPE only (a clone error's message can quote the
offending value, and that value is page content).

The tests only exercised handcrafted inputs, so removing either call site
left them green. Added tests/detach-production-boundaries.test.ts: it clones
a real `runPageRules` graph over the full rule set and asserts zero
fallbacks, then runs `runStreamingRules` and asserts BOTH boundaries
actually detached. Verified it has teeth by removing the collected-signal
call site (the run fails).

Where the memory control fails to demonstrate retention, the unit test now
says INCONCLUSIVE and asserts nothing instead of passing vacuously.

Also: the real-page fixture's nav links and its page records used different
path shapes, so at 150 pages no generated link pointed at a generated page.
One path function now serves both.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the perf/detach-collected-signals-1860 branch from 618379e to f147951 Compare September 7, 2026 11:04
@nc9
nc9 merged commit 91faaeb into main Sep 7, 2026
13 checks passed
@nc9
nc9 deleted the perf/detach-collected-signals-1860 branch September 7, 2026 11:11
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