Repository navigation
perf(audit-engine): detach what the page loop retains from the page it came from - #234
Merged
Merged
Conversation
4 tasks done
…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
force-pushed
the
perf/detach-collected-signals-1860
branch
from
September 7, 2026 11:04
618379e to
f147951
Compare
This was referenced Sep 7, 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.
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).lengthreports the logical size, so a census says 1 KB for a value holding 3.8 MB.heapUsed/external, which grow linearly with page count while RSS oscillates.The fix
structuredCloneat 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 theSets inPageFingerprintrather than silently turning them into{}.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
parsedDatathe way the crawler does, becausebuildSiteContextreuses 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.optionsSchemais a Zod instance, 38 of 282 rules carry one,structuredClonethrows 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:
metastays by reference — it is per-rule, not per-page, so it retains nothing.parsedis excluded — nothing here reads it, and cloning it would copy the live document and stripSchemaCollection's methods.pageResults,pageRuleResultsandruleResultsMapkeep referencing the same check objects instead of holding three copies. The code stampscheck.pageUrlafter 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.tspins 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 barestructuredClone, so it is deliberately not in this PR.