Repository navigation
perf(audit-engine): take untouched carried pages as a store aggregate #497 - #503
Merged
Merged
Conversation
3 tasks
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
force-pushed
the
perf/497-complete-store-aggregate
branch
from
October 6, 2026 21:08
7eafab5 to
b1b3072
Compare
This was referenced Oct 6, 2026
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
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.
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
runCloudSmartAuditsin 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 normalOpenFindingPage, plus the untouched pages' report sample asUntouchedSamplePages. Both come in one sequence, in cursor order. A store may stream an untouched page in full instead; that is always allowed.aggregate(UntouchedCarriedAggregate):foldGroupKeyparts): occurrences, members, pages, thepagesTruncatedfloor and the newestlastSeenAt;complete-store-fold.ts:retainUntouchedSamplefeeds 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.addUntouchedadds the totals: the warn half is dropped for an advisory rule, and dirty-page counts feed the clean-carried pass denominator.CARRIED_REPORT_SAMPLE_PER_RULEuntouched 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,addChecksToTallyonce per page and rule, and the class counter, which is now shared.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: anitemsobject counted by a fractionallength, and hugeoccurrences. 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 pastMAX_SAFE_INTEGER, a sample page in the crawled or removed set, and totals that miss a sampled class.provenance→carriedon the rows that lack it).persistedFindingsand 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:
ISSUE_PENALTY_ITEM_CAP, and 20 payload shapes, malformed ones included (invalid JSON, non-object JSON, string/number/booleanitems, non-objectdetails, string, negative and fractional numerics);lastSeenAton a dropped row;Variants:
MAX_SAFE_INTEGER + 1occurrence weight);Mutation-checked: each of 15 mutations makes at least one test fail. The mutations:
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
handlePublishFinalizeon 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.lengthand hugeoccurrences, 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
itemsis truthy and not iterable (5,true, an object) makesfoldGroupthrow once its rule folds. That is pre-existing, and the full fold and the split behave the same on it.Changelog