Skip to content

perf(audit-engine): bound the complete-store merge by the run, not the site's open-findings history #497

Description

@nc9

The cloud's complete-store merge (runCloudSmartAudits with completeStore.openPages) reads and folds every open finding the site has, one row at a time, on every publish. Its cost therefore grows with the site's whole finding history instead of with the run. One big historical audit makes every later publish for that site expensive.

The hosting side has since stopped rewriting carried rows that the merge leaves unchanged, which was the larger half. The read and the fold are still O(open findings). Measured on a seeded local Postgres with 2,000 fresh findings: a site holding 250,000 carried open findings costs about 1,000 cursor round trips and roughly 25x the CPU of a site holding none, and every one of those rows changes nothing in the score from one publish to the next.

Why the API cannot bound it alone

For each carried row, these are computed inside the engine:

  • the per-rule IssueTally contribution: fail units per (check, page) capped at ISSUE_PENALTY_ITEM_CAP, plus details.additional and the warn keys (addChecksToTally);
  • carriedDirtyPagesByRule, which cleanCarriedPasses subtracts from carriedPageUrls.size;
  • the carried and unrendered counts (coverage);
  • the report's carried sample: the first 25 checks per rule in primary-key order, the first check of every later class, and per class the occurrences, members, pages, pagesTruncated floor, newest lastSeenAt, and whether every member is carried or every member is unrendered (retainCarried, collapseCarriedReport, reconcileCarriedAggregate).

If a caller leaves the untouched pages out of openPages, the fold counts them as clean carried pages, and both the score and the report sample change. No input exists for supplying those numbers.

Proposed change

Let the complete-store mode take the UNTOUCHED carried pages as an aggregate instead of as rows. A page is untouched when it is not crawled this run, not removed, and has no fresh row. On such a page the merge's decision for every prior is "carry", so its contribution depends only on the stored rows, the rule meta and the site's render history.

  1. Touched pages still stream as rows. That covers crawled pages, removed pages and pages with fresh rows, which keeps the merge's resolve/stale/carry decisions and the one-call-per-page fold invariant exactly as they are.
  2. Score: an aggregate per rule. The caller supplies, per page-scope rule, the IssueTally delta of the untouched pages (computed per (check, page) bucket with the same unit/cap/additional rules), the number of untouched pages with a finding for the rule (added to carriedDirtyPagesByRule), and the carried/unrendered counts. On the API side this is one GROUP BY over page_findings, so the store does a single scan instead of a JS row walk.
  3. Report: a bounded sample read. The caller supplies the first CARRIED_REPORT_SAMPLE_PER_RULE untouched rows per rule in primary-key order, plus the first row of each later class, interleaved with the touched pages' carried checks in the same order retainCarried sees them today. It also supplies the per-class counts the stamps need (occurrences, members, pages, pagesTruncated floor, newest lastSeenAt, all-carried / all-unrendered).
  4. Parity tests. Extend complete-store-parity.test.ts: on fixtures with fresh, carried, resolved, stale, reappearing and unrendered findings, folding everything as rows (today) and the touched/aggregate split must give byte-identical tallies, scores, coverage and unionRuleResults. Include a rule over the sample cap, a class first seen after the budget is spent, and an advisory (severity: "info") rule.

Touched files: packages/audit-engine/src/merge-promise.ts (new input, driving the touched stream and the aggregate) and packages/audit-engine/src/complete-store-fold.ts (accepting tally deltas, dirty-page counts and a pre-ordered sample alongside foldPage). merge-core.ts should not need changes.

The open risk is the SQL twin of addChecksToTally and carriedFindingToCheck (payload items length, details.additional, the m/v/e stash). It has to read the payload exactly as the JS does (non-numbers ignored, floors, a non-array items), or the score drifts. The parity suite has to run it against real Postgres, not only against the JS fold.

Private tracker: the hosting-side half is in place (unchanged rewrites skipped); this is the remaining read-side bound.

Acceptance Criteria

  • Complete-store merge accepts untouched carried pages as an aggregate plus a bounded sample, and folds only touched pages row by row
  • Parity suite: row fold vs touched/aggregate split byte-identical (tallies, score, coverage, union rule results) on mixed fixtures, including over-cap and advisory rules
  • CPU and round trips of a publish on a site with 250k carried open findings within 2x of the same publish on a site with none

Activity

  1. added
    enhancement
    P2
    size: L
    on Oct 6, 2026
  2. nc9 commented on Oct 7, 2026

    @nc9
    ContributorAuthor

    Current state (2026-10-07): criteria 1-2 are done.

    Remaining: criterion 3 is close but not met. The hosting PR measured a 250k-carried publish (2k fresh) against a 0-carried publish:

    • statements: 46 vs 38 (met);
    • whole-handler CPU: 489 ms vs 172 ms, 2.8x;
    • the merge alone: about 2.3x.

    The excess is the larger report rendered at 250k.

  3. nc9 commented on Oct 7, 2026

    @nc9
    ContributorAuthor

    Triage 2026-10-07: the criteria heading is renamed to the exact ## Acceptance Criteria, and the existing list is kept as is. Suggested updates to fold in, from a read of the thread and current main:

    • The complete-store merge accepts untouched carried pages as an aggregate plus a bounded sample (completeStore.untouchedCarried) and folds only touched pages row by row
    • The parity suite in packages/audit-engine/tests/complete-store-parity.test.ts shows the row fold and the touched/aggregate split produce byte-identical tallies, score, coverage and unionRuleResults on mixed fixtures, including a rule over the sample cap, a class first seen after the budget is spent and an advisory (severity: "info") rule
    • On a seeded local Postgres benchmark with 2,000 fresh findings, a publish for a site holding 250,000 carried open findings uses at most 2x the CPU and database round trips of the same publish for a site holding none, with the measurement posted in a comment here

    Note: Criteria 1-2 are done on main (completeStore.untouchedCarried plus parity tests); only the 2x CPU bound remains, last measured at 2.8x whole-handler and about 2.3x for the merge, so it stays open on the performance criterion alone. The body already uses a lowercase ## Acceptance criteria heading that needs renaming.

  4. nc9 commented on Oct 8, 2026

    @nc9
    ContributorAuthor

    Status: #586 is ready for review and green on 6020063. All 15 checks pass, including the engine golden tests, the CLI tests and the bot review.

    It goes after criterion 3 on the engine side. With the findings already an aggregate (#503), what still grew with the site was its page history: the merge made several copies of site_pages and upserted every page row on each publish. Now the merge indexes the prior pages once, takes its counts from the run's own pages, and upserts only the rows the run changed. upsertSitePages is documented as a keyed upsert, and a reference-store test pins it.

    Measured on one complete-store publish with 2,000 fresh findings, 0 vs 250,000 carried, base and head alternating, minimum of five:

    carried 0 carried 250,000 ratio
    CPU before 41.4 ms 122.3 ms 2.95x
    CPU after 42.5 ms 67.4 ms 1.59x
    wall after 20.4 ms 35.1 ms 1.72x
    site-page rows written after 200 200

    Before, a publish wrote 25,200 site-page rows at 250,000 carried findings. The outputs hash identically before and after.

    Remaining for criterion 3: re-run the hosted publish on seeded Postgres (2,000 fresh, 250k vs 0 carried) once #586 merges, and post the numbers here.

  5. added
    size: S
    and removed
    size: L
    on Oct 10, 2026
  6. nc9 commented on Oct 10, 2026

    @nc9
    ContributorAuthor

    Ticked 1 and 2: completeStore.untouchedCarried (#503, v0.0.105) with parity block (e) in complete-store-parity.test.ts, which covers the over-cap, late-class and advisory cases. #586 (v0.0.107) brought the engine-side merge to 1.59x CPU at 250k carried. Left: criterion 3, the hosted publish re-measured on seeded Postgres (2,000 fresh, 250k vs 0 carried). Resized to S, since only that measurement remains.

  7. nc9 commented on Oct 26, 2026

    @nc9
    ContributorAuthor

    Criterion 3 measured on current main (hosted publish finalize against a seeded local Postgres, finalize-memory-scale harness, 2,000 fresh findings, carried backlog in the steady state, untouched-page aggregate on, two runs each, engine at v0.0.136):

    carried open findings CPU statements (round trips) heap tuples read wall
    0 151 and 171 ms 47 6,201 169 and 194 ms
    250,000 353 and 305 ms 51 507,125 6,957 and 4,223 ms

    So the handler's CPU is 1.8x to 2.3x (mean 2.0x) and its round trips 1.09x, against the criterion's "within 2x": met on round trips, at the line on CPU. The remaining growth is the database read, not the engine: the aggregate query still scans the site's open findings (82x the heap tuples, 25x to 35x the wall time), which is a hosting-side cost (the GROUP BY twin in apps/api/src/lib/untouched-carried.ts), not the merge. Whether that residual needs its own issue (index or incremental per-site aggregate) is a call for the maintainers; I have not ticked criterion 3.

  8. nc9 commented on Oct 26, 2026

    @nc9
    ContributorAuthor

    The residual (the hosted aggregate query scan, 82x heap tuples and 4 to 7 s wall at 250,000 carried) is tracked privately as squirrelscan/repo#3507. Criterion 3 stays open until that lands or is restated.

  9. added theissue type on Oct 28, 2026
  10. nc9 commented on Oct 30, 2026

    @nc9
    ContributorAuthor

    Checked against main (23a34c1, v0.0.144). Criteria 1 and 2 hold on main. Criterion 3 stays open: the maintainer's measurement shows round trips at 1.09x and CPU at about 2.0x, but wall time at 25 to 35x and 82x heap tuples; the residual hosting query is tracked privately (repo#3507).

    nik

  11. added
    engine
    blocked
    on Nov 13, 2026
  12. nc9 commented on Nov 15, 2026

    @nc9
    ContributorAuthor

    No engine work is left: criteria 1 and 2 are done on main (untouched-carried fold from #503, plus the parity suite block (e)). Criterion 3 (CPU and round trips within 2x at 250k carried) measured 1.09x round trips and about 2.0x CPU on the hosted publish. The remaining cost is the hosted aggregate query, which is tracked on the hosting side, not in this repo. This stays open until that lands and the measurement is re-run; labelled blocked so agents skip it.

  13. nc9 commented on Nov 16, 2026

    @nc9
    ContributorAuthor

    Publish measurements from the cloud side (finalize-memory-scale harness, Postgres 17, 2,000 fresh findings, two runs per case):

    Case Round trips Process CPU Wall
    Empty site 54 297–388 ms 192–259 ms
    250k carried, aggregate 58 831–855 ms 4.6 s
    250k carried, old cursor 1,051 5.3–5.7 s ~5.7 s

    Round trips are within 2x (1.07x). Process CPU is about 2.1–2.9x and wall about 18–24x: the remaining cost is one aggregate query that scans every open finding. Leaving this open until that query is optimised; the fix is tracked privately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2blockedengineenhancementsize: S

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions