Skip to content

test(audit-engine): give the detach memory tests a control they can survive CI with - #250

Merged
nc9 merged 1 commit into
mainfrom
fix/detach-memory-test-flake-1860
Sep 7, 2026
Merged

nc9 merged 1 commit into
mainfrom
fix/detach-memory-test-flake-1860

Conversation

@nc9

@nc9 nc9 commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Tests only. Fixes the flake that took #240 red on run 34139295533.

What failed and why

absorbing from a page-sized document does not retain the document failed with 541,604 bytes against an expected 531,770 — the attached and detached arms landing within 2% of each other on a shared runner. That is noise, not the fix regressing: it passed locally and on the pre-rebase run.

The measurement had no control. It compared the two real arms directly, so the parse, the transient DOMs and the arena's growth were inside both numbers, and the difference it asserted on was a couple of percent of each. On a quiet machine that works; on a shared runner the two numbers touch.

Its sibling in detach-from-page.test.ts had the same shape with a worse failure mode: when the arms collapsed it printed INCONCLUSIVE and passed. It had been doing that on every run since it was written, so the second memory gate in this area has been asserting nothing.

The harness

tests/helpers/retention.ts, used by both:

  • external, not heapUsed. A string's characters live in a backing store off the JS heap, and a slice pins the buffer rather than a heap object. Measured on a 150-page fixture, the attached arm's external was 287 MB against a control's 100 MB while the two heaps were 143 and 131. Reading heapUsed makes the signal a rounding error on the noise.
  • A retain-nothing arm, subtracted. Every arm builds the same sources and runs the same extraction; only what it keeps differs.
  • Multi-megabyte sources, deliberately node-poor. What has to be large is the buffer a slice can pin; a page of the same weight in small elements costs several times as much to hold as a live DOM for no extra signal. Sized from measurement rather than guessed: 24 pages of 3.8 MB put the attached arm 82 MB clear of the control, 48 of 6 MB put it at 234.
  • An unusable run fails loudly. If the attached arm is not clear of the control by the margin, the test reports the three numbers and fails, saying it is a measurement failure rather than a regression. The alternative is the INCONCLUSIVE branch that has been passing for a week.

Both fixtures build from per-source unique chunks. A corpus made with .repeat() is nearly free to hold, because JSC ropes share backing storage, and the attached arm would look detached.

Numbers

Three consecutive local runs, both tests, against a 150 MB margin and a 4x ratio:

test control attached detached
external-link occurrences, 48 x 6 MB 0-1 MB +234 MB within 1 MB of 0
raw slices, 48 x 6 MB 0 MB +275 MB +0 MB

The three numbers are printed on every run, pass or fail, so the next flake comes with its own diagnosis instead of leaving someone to re-derive it.

With the detachFromPage call site deleted, the detached arm jumps to +243 MB and the assertion fails, so the test still catches the thing it was written for.

…urvive CI with

Tests only. `absorbing from a page-sized document does not retain the document`
went red on a shared runner at 541,604 bytes against an expected 531,770 — the
attached and detached arms landing on top of each other, which is runner noise
rather than the fix regressing.

The measurement had no control. It compared the two real arms directly, so the
parse, the transient DOMs and the arena's growth were inside both numbers and
the difference it asserted on was a few percent of each. Its sibling in
detach-from-page.test.ts had the same shape, and when the arms collapsed it
printed INCONCLUSIVE and PASSED — on every run since it was written.

helpers/retention.ts now measures both, and four things changed:

- `external`, not heapUsed. A string's characters live off the JS heap and a
  slice pins the BUFFER. On a 150-page fixture the attached arm's external was
  287 MB against a control's 100 while the two heaps were 143 and 131, so
  heapUsed makes the signal a rounding error on the noise.
- A retain-nothing arm, subtracted. Every arm parses the same pages and runs the
  same extraction; only what it keeps differs.
- Multi-megabyte sources, node-poor so the DOM stays cheap and the buffer stays
  large. 24 pages of 3.8 MB put the attached arm 82 MB clear; 48 of 6 MB put it
  at 234, against a 150 MB margin.
- An unusable run FAILS and says which number was wrong, instead of returning
  early. A green run that asserted nothing is worse than a red one.

Measured over three consecutive local runs, both tests: control 0-1 MB, attached
+234 and +275 MB, detached within 1 MB of zero. With the detach call site
deleted the detached arm jumps to +243 MB and the assertion fails, so the test
still catches what it was written for.

Refs squirrelscan/repo#1860.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/detach-memory-test-flake-1860 branch from 8255ba2 to fc2717d Compare September 7, 2026 19:17
@nc9
nc9 merged commit 5c82d4f into main Sep 7, 2026
13 checks passed
@nc9
nc9 deleted the fix/detach-memory-test-flake-1860 branch September 7, 2026 19:23
nc9 added a commit that referenced this pull request Sep 7, 2026
…ng their pages (#255)

* fix(audit-engine): stop the container's cloud-prefetch payloads holding their pages #253

buildCloudPagePayloads keeps a 6 KB text excerpt per page, plus a title, a
description and up to twenty headings. Every one is a slice of that page's html,
and in JSC a retained slice pins the buffer it was cut from, so the payload list
held the whole crawl as UTF-16 for the length of the prefetch. That is the
page-count-scaled term the streamed pre-rules walk exists to remove: the walk
drops each batch as soon as the collectors have absorbed it, and nothing else is
keeping those pages alive.

truncateUtf8Bytes is not a detach. It returns text.slice(0, maxBytes) unchanged
whenever the slice already fits the byte budget, which is every all-ASCII page,
so the excerpt came back still attached. Pinned as its own test so the next
reader does not mistake it for one.

Measured with the #250 retention harness on per-page-distinct 4 MB pages, 24
pages, `external` with a retain-nothing control subtracted:

  attached  +186 MB
  detached    +3 MB

The blocklist urls and selectors and the gaps seeds get the same treatment.
Those three are capped (1,500 / 500 / 50) so their worst case was bounded by the
cap rather than by page count, but a capped list of 1,500 attached urls still
pins up to 1,500 pages.

Payloads are unchanged: same fields, same values, same order, asserted directly
and covered by the CLI-vs-container prefetch parity test. The CLI's twin of this
code was fixed in #252; this is the container side, which runs on every hosted
audit.

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

* perf(audit-engine): pay the payload detach per unique value, not per occurrence #253

Review follow-up on the commit above. Three cheap wins, no behaviour change:

- The blocklist url and selector sets cap UNIQUE values, but the clone was being
  paid per occurrence. A site that links the same forty hosts and reuses the same
  layout classes on every page has orders of magnitude more occurrences than
  uniques, so the set is now tested before the copy is made. The resulting set is
  identical either way — a duplicate never grew it.

- The container's `cleanSeed` assembles its result character by character and
  returns `parts.join("")`, a fresh string that references no page, so detaching
  a seed there was pure cost. Dropped, with a note saying why the CLI's twin
  still needs one: it builds the same value with split/replace/trim, whose
  results CAN be slices. A real difference between the two files rather than a
  missed case.

- The CLI's twin gets the same dedupe-before-clone, so the two stay in lockstep
  where they should be.

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

---------

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