Skip to content

perf(audit-engine): take untouched carried pages as a store aggregate #497 - #503

Merged
nc9 merged 1 commit into
mainfrom
perf/497-complete-store-aggregate
Oct 6, 2026
Merged

nc9 merged 1 commit into
mainfrom
perf/497-complete-store-aggregate

Conversation

@nc9

@nc9 nc9 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Refs #497. This is the engine half. The hosting side (the SQL aggregate and the bounded sample read) follows in the private repo and needs this merged first.

Score-bearing on the cloud path. With the new input absent, behaviour is unchanged. With it present, the output is byte-identical to the full row fold whenever the store's aggregate equals the JS reference (the parity suite below).

Cause

runCloudSmartAudits in complete-store mode (completeStore.openPages) reads and folds every open finding the site has, one row at a time, on every publish. Most of those rows sit on pages the run never touched. The merge carries each of them unchanged, yet the per-rule tally, the carried dirty-page counts, the coverage counts and the report's carried sample were all computed row by row inside the engine. No input existed that let the store supply those numbers instead.

Change

New optional completeStore.untouchedCarried(scope) (packages/audit-engine/src/merge-promise.ts). It is called once, with the run's settled crawled and removed sets, and returns:

  • pages: every TOUCHED page (crawled, removed, or holding a row from this audit) as a normal OpenFindingPage, plus the untouched pages' report sample as UntouchedSamplePages. Both come in one sequence, in cursor order. A store may stream an untouched page in full instead; that is always allowed.
  • aggregate (UntouchedCarriedAggregate):
    • per rule: fail and warn counts, fail units capped per (checkName, page) bucket, warn keys, and active untouched pages;
    • per report class (foldGroupKey parts): occurrences, members, pages, the pagesTruncated floor and the newest lastSeenAt;
    • the carried and unrendered counts.

complete-store-fold.ts:

  • retainUntouchedSample feeds sample rows to the report retention without counting them. It claims each rule's tally slot and report class at the position the page would have had, so map order (and so serialized order) matches the full fold.
  • addUntouched adds the totals: the warn half is dropped for an advisory rule, and dirty-page counts feed the clean-carried pass denominator.
  • Sample contract: the sample must contain AT LEAST, per rule, the first CARRIED_REPORT_SAMPLE_PER_RULE untouched rows plus each class's first row. Why that is enough: the full fold only ever retains rows from that set, and retention state only changes when a row is retained.
  • aggregateUntouchedCarried (exported) is the JS reference a store twin must agree with. It is built from the same functions the full fold uses: carriedFindingToCheck, addChecksToTally once per page and rule, and the class counter, which is now shared.
  • Exact domain (isAggregatableCarriedRow, UNTOUCHED_MAX_OCCURRENCES). The full fold adds each page into a running total, while the split adds a subtotal at the end. Those agree only while every term is an integer and the sums stay safe. Two malformed payload shapes break that: an items object counted by a fractional length, and huge occurrences. If ANY carried row of the site has one, the store must stream every page with an empty aggregate, which is the full fold itself. The merge refuses a non-empty aggregate beside such a row, a sample row outside the domain, a class total past MAX_SAFE_INTEGER, a sample page in the crawled or removed set, and totals that miss a sampled class.
  • Persistence: the merge never sees the untouched rows, so the store applies their carry itself (provenance → carried on the rows that lack it). persistedFindings and coverage count them.

Exported for the store's SQL twin, so it cannot drift from copies of these: CARRIED_REPORT_SAMPLE_PER_RULE, UNTOUCHED_MAX_OCCURRENCES, isAggregatableCarriedRow, aggregateUntouchedCarried, untouchedCarriedFinding, and the types.

Tests

tests/complete-store-parity.test.ts, new block (e): the full row fold vs the touched/aggregate split, computed by the reference from the same rows. It compares serialized bytes of the tallies (Map order included), the health score, coverage, unionRuleResults (order included), persistedFindings, and every row and page left in the store.

The fixture holds:

  • fresh, carried (steady and first-carry), resolved, stale, reappearing and unrendered findings;
  • pages an older audit saw removed;
  • a rule over the report's carried budget, a class first seen after the budget is spent, and 30 classes on one page;
  • an advisory rule;
  • rows of a site rule and of a rule absent from the shell;
  • multi-item buckets over ISSUE_PENALTY_ITEM_CAP, and 20 payload shapes, malformed ones included (invalid JSON, non-object JSON, string/number/boolean items, non-object details, string, negative and fractional numerics);
  • a touched page that is not crawled, in the middle of the backlog;
  • a newer lastSeenAt on a dropped row;
  • a rule first held only by untouched pages that must keep its tally slot before a later touched rule.

Variants:

  • exact sample, every untouched row as the sample, and a superset in between;
  • untouched pages streamed in full;
  • under the report cap;
  • the out-of-domain fallback (codex's fractional repro and a MAX_SAFE_INTEGER + 1 occurrence weight);
  • five refusal cases.

Mutation-checked: each of 15 mutations makes at least one test fail. The mutations:

  • sample fed after the touched pages;
  • no first-of-class rows in the sample;
  • advisory warns kept;
  • class pages, floor or lastSeen not merged;
  • dirty pages not added;
  • no tally slot claim;
  • sample counted twice;
  • unrendered or persisted not counted;
  • no store-side carry;
  • the domain guards removed.

I also checked the premise: with the fallback and the guard disabled, the tallies and union bytes differ on the odd-payload fixture.

Results:

  • bun test tests/complete-store-parity.test.ts: 34 pass.
  • bounded-carried-merge, cloud-merge, carried-provenance-merge, merge-scoring, item-locator-resolve, advisory-scoring, output-caps-score-parity, item-finding-identity, memory-harness, fingerprint-parity: all pass.
  • tsgo --noEmit (packages/audit-engine): clean.

The real-run proof (a real handlePublishFinalize on scratch Postgres at 0 vs 250k carried, and a SQL twin vs this reference on real Postgres with malformed payloads) is in the private PR that consumes this.

Codex review, 2 rounds: (1) float reassociation from fractional items.length and huge occurrences, fixed by the site-wide exact domain and guards; (2) class totals past 2^53, now refused. The sample-superset argument, the ordering and the absent-input behaviour were confirmed.

Found while testing, not changed here: a carried payload whose items is truthy and not iterable (5, true, an object) makes foldGroup throw once its rule folds. That is pre-existing, and the full fold and the split behave the same on it.

Changelog

  • Cloud: publishing to a site with a large backlog of open findings no longer re-reads every one of them on each publish (engine side).

@nc9
nc9 marked this pull request as ready for review October 6, 2026 21:08
…497

The complete-store merge folded every open finding of the site row by row,
so a publish cost what the site's whole history cost. A new optional
completeStore.untouchedCarried(scope) lets the store stream only touched
pages (crawled, removed, or holding a fresh row) and hand over the rest as
per-rule tally totals, per-class report counters and a bounded report
sample, interleaved in cursor order.

aggregateUntouchedCarried is the JS reference a store twin must match.
isAggregatableCarriedRow bounds the exact domain: one carried row outside
it and the store streams every page. With the input absent, nothing
changes.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the perf/497-complete-store-aggregate branch from 7eafab5 to b1b3072 Compare October 6, 2026 21:08
@nc9
nc9 merged commit 4f04970 into main Oct 6, 2026
14 checks passed
@nc9
nc9 deleted the perf/497-complete-store-aggregate branch October 6, 2026 21:17
nc9 added a commit that referenced this pull request Oct 8, 2026
…586)

* perf(audit-engine): bound the merge's site-page work by the run #497

With the untouched findings already an aggregate (#503), what still grew
with the site was its page history. The merge session copied site_pages
into a merged map, a merged array, an active set and a render-history
set, runCloudSmartAudits built two more (carried pages, the sample's
render history), and every page row was upserted on every publish.

The session now indexes the prior pages once and keeps this run's
changes beside them. Active and carried counts are corrected from the
run's own pages, carried pages are a PageUrlSet view instead of a copy,
sitePages/activePageUrls are built only when read (the CLI's
computeMerge still gets both), and the cloud merge upserts only the rows
this run changed. Every surface a caller reads is unchanged, and the
rows a store ends up holding are the same.

Measured with the new bench-untouched-carried.ts, base vs head
alternating, min of 5: at 250,000 carried findings on 25,200 site pages
vs none, CPU 2.95x -> 1.59x, wall 3.06x -> 1.72x, page rows upserted
25,200 -> 200, outputs hash identically.

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

* docs(audit-engine): state upsertSitePages delta semantics, reuse prio…

…r pages #497

Review round 1: document that runCloudSmartAudits hands upsertSitePages
only the pages this run crawled, note why changedSitePages is taken once
and what PageUrlSet.has answers for, and index the prior pages without
copying them when no url repeats.

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

* Merge remote-tracking branch 'origin/main' into claude/bound-complete…

…-store-merge-ctljjj

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

* test(audit-engine): pin upsertSitePages as a keyed upsert #497

State the contract on SmartAuditStore.upsertSitePages: a keyed insert or
replace on (siteKey, normalizedUrl) that never deletes or resets a row it
was not handed, so callers may pass only changed rows. A keyed-upsert
reference store shows the publish's changed-rows write leaves the same
rows as the previous write of every merged page, on six seeded fixtures
with another site's rows alongside.

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

* Merge branch 'main' into claude/bound-complete-store-merge-ctljjj



* Merge branch 'main' into claude/bound-complete-store-merge-ctljjj



* Merge remote-tracking branch 'origin/main' into claude/bound-complete…

…-store-merge-ctljjj

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

* Merge branch 'main' into claude/bound-complete-store-merge-ctljjj



* test(audit-engine): run every upsertSitePages store through one contr…

…act #497

The cloud merge hands upsertSitePages only the rows a run changed, which
is safe only for a keyed upsert. Nothing enforced that.

- tests/helpers/site-pages-contract.ts: the shared cases. A second upsert
  of a different row keeps the first, upserting an existing key replaces
  that row only, and rows absent from a call (an empty call included) are
  never deleted or changed.
- Every in-repo implementation runs it: the eight test stores, the bench
  store (now scripts/bench-store.ts) and SQLiteStorage from the crawler
  tests.
- Two stores violated it and are fixed: rate-limited-coverage's
  EmptyStore and the bench store both dropped every upserted page.
- tests/site-pages-contract-coverage.test.ts scans packages and apps for
  implementations and fails for one that does not run the contract.

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

* Merge remote-tracking branch 'origin/claude/bound-complete-store-merg…

…e-ctljjj' into claude/bound-complete-store-merge-ctljjj

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

* Merge branch 'main' into claude/bound-complete-store-merge-ctljjj
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