Skip to content

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

Merged
nc9 merged 2 commits into
mainfrom
fix/cloud-prefetch-payload-detach
Sep 7, 2026
Merged

nc9 merged 2 commits into
mainfrom
fix/cloud-prefetch-payload-detach

Conversation

@nc9

@nc9 nc9 commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Closes #253. The container-side twin of the retention fixed on the CLI side in #252.

What was wrong

buildCloudPagePayloads keeps a 6 KB text excerpt per page, plus a title, a description and up to twenty headings. Every one of those 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 exactly 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, so nothing else is keeping those pages alive, and the payloads were quietly holding all of them.

truncateUtf8Bytes is not a detach, which is the part that makes this easy to miss. It returns text.slice(0, maxBytes) unchanged whenever the slice already fits the byte budget, and for an all-ASCII page it always does. Only an overflowing multi-byte slice takes the TextEncoder round trip that would have detached it. There is now a test saying so, so the next reader does not take it for a detach.

It shows only in process.memoryUsage().external. Not in heapUsed, where it is a rounding error, and not in RSS, where the allocator absorbs it.

The measurement

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

arm retained across 24 pages
attached +186 MB
detached +3 MB

About 7.8 MB held per page before, 0.13 MB after. The harness fails loudly rather than passing when the attached arm does not clear the control by its margin, so a run that did not reproduce the retention cannot report success.

Also detached

The blocklist URLs and selectors and the gaps seeds. Those three are capped at 1,500, 500 and 50, so their worst case was bounded by the cap rather than by page count. A capped list of 1,500 attached URLs still pins up to 1,500 pages, though, so they get the same treatment.

Not detached: renderedPageUrls, whose entries are page.url read from a storage row rather than sliced out of HTML, and the Stage-0 metadata payloads, which are built from a bounded sample the collector is already retaining whole.

Parity

This is a memory property, not a payload change. Same fields, same values, same order, asserted directly and covered by the existing CLI-versus-container prefetch parity test. The streaming pre-rules golden and the canonical-versus-streaming golden are green.

Review follow-up

A codex pass returned no correctness defects and three efficiency notes, all taken in the second commit:

  • 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 far more occurrences than uniques, so the set is now tested before the copy is made. The resulting set is identical either way, since a duplicate never grew it.
  • The container's cleanSeed assembles its result character by character and returns parts.join(""), a fresh string referencing no page, so detaching a seed there was pure cost. Dropped. The CLI's twin still needs one, because it builds the same value with split/replace/trim whose results can be slices. That is a real difference between the two files, not a missed case, and it is commented as such.
  • The CLI's twin gets the same dedupe-before-clone, so the pair stays in lockstep where it should be. That is the only change here outside packages/audit-engine.

Nik Cubrilovic added 2 commits September 8, 2026 05:42
…ng 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]>
…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]>
@nc9
nc9 merged commit 3427863 into main Sep 7, 2026
13 checks passed
@nc9
nc9 deleted the fix/cloud-prefetch-payload-detach branch September 7, 2026 19:55
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.

The container's cloud-prefetch page payloads pin a whole page each

1 participant