Skip to content

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

Merged
nc9 merged 11 commits into
mainfrom
claude/bound-complete-store-merge-ctljjj
Oct 8, 2026
Merged

nc9 merged 11 commits into
mainfrom
claude/bound-complete-store-merge-ctljjj

Conversation

@nc9

@nc9 nc9 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Refs #497

Summary

Criteria 1 and 2 landed in #503. This PR goes after criterion 3, the 2x bound, on the engine side.

With the untouched findings already handed over as an aggregate, the findings no longer grew the merge's cost. The site's page history did:

  • createMergeSession copied site_pages into a merged map, a merged array, an active set and a render-history set;
  • runCloudSmartAudits built two more (carriedPageUrls through a priorState map, and the sample's render history);
  • every page row went back to upsertSitePages on every publish, changed or not.

At 25,200 site pages, each of those Maps or Sets costs 5 to 8 ms on a cold isolate.

What changed:

  • merge-core.ts. The session indexes the prior pages once (indexPriorPages: first position, last row, the same as one Map.set per row) and keeps this run's crawled and removed pages in a run-sized map beside them.
    • activePageCount and the carried count are the prior counts, corrected from the run's own pages.
    • isActivePage, everRendered and carriedPageUrls read the index.
    • sitePages and activePageUrls are built only when read. The CLI's computeMerge still gets both, unchanged.
    • New changedSitePages: the rows this run wrote.
  • scoring.ts. New PageUrlSet: size, has, and iteration in order. A Set<string> satisfies it. The streaming merge passes a view over the prior pages instead of a copy. carriedPageUrls in MergedScoringInput and CompleteStoreTallyInput takes it.
  • merge-promise.ts.
    • It uses the session's answers instead of rebuilding them.
    • It upserts only changedSitePages (less the removed pages, as before). Every other row is a prior page exactly as getSitePages returned it, so the rows a store ends up holding are the same.
    • Store-facing change: the hosting side receives far fewer rows in upsertSitePages, with the same end state.
  • scripts/bench-untouched-carried.ts. New harness: one complete-store publish with the untouched split, 0 vs N carried findings.
  • benchmarks/2026-09-perf-program.md. Record of the measurement.

Acceptance criteria

  • Complete-store merge accepts untouched carried pages as an aggregate plus a bounded sample, and folds only touched pages row by row. Landed in perf(audit-engine): take untouched carried pages as a store aggregate #497 #503 (completeStore.untouchedCarried), unchanged here.

  • Parity suite: the row fold and the touched/aggregate split are byte-identical (tallies, score, coverage, union rule results) on mixed fixtures, including over-cap and advisory rules. Landed in perf(audit-engine): take untouched carried pages as a store aggregate #497 #503. Still green on this head: tests/complete-store-parity.test.ts, 34 pass (block (e) compares serialized tallies, score, coverage, union, persisted count and the store's rows).

  • 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.

    • Engine side, met.
    • Hosting side not re-measured: the criterion's measure is the hosting publish on a seeded Postgres, and that harness is not in this repo. That re-run is for the maintainer after this merges.

    Engine evidence: one runCloudSmartAudits publish (complete-store, untouched split, in-memory store, the store's aggregate computed before the clock starts).

    • Fixture: 2,000 fresh findings on 200 crawled pages, 40 page-scope rules, 10 carried findings per untouched page.
    • Method: base (origin/main at ccf9c1f) and head run the same script, one arm per process, alternating base/head and 0/250k, minimum of five. 4 vCPU Xeon 2.1 GHz, Bun 1.4.2.
    carried 0 carried 250,000 (25,200 site pages) ratio
    base CPU 41.4 ms 122.3 ms 2.95x
    head CPU 42.5 ms 67.4 ms 1.59x
    base wall 21.9 ms 67.1 ms 3.06x
    head wall 20.4 ms 35.1 ms 1.72x
    base site-page rows upserted 200 25,200
    head site-page rows upserted 200 200
    • Store calls are 5 in every arm.
    • The site-page rows handed to the store no longer grow with the site, which is where the hosting side's extra statements on the large site would come from.
    • At 100 carried findings per page (2,700 site pages), base and head are 1.46x and 1.43x CPU. The remaining excess there is the report's carried sample (25 checks per rule), which costs the same at 2,500 carried findings as at 250,000.
    • The hosting comment on the issue attributes the rest of its whole-handler excess to rendering that larger report. The engine bounds that report by rules, not by the site; the rendering happens outside this repo.

Behaviour is byte-identical

  • Same-commit A/B: every arm hashes the tallies, score, coverage, persisted count and serialized union. Base and head hash identically (806b03e2a37a7a4d at 0, 866f6c47e809d687 at 250k, e5f066f67202f421 at 250k/100 per page).

  • New tests/merge-session-pages.test.ts (13 tests) pins every new answer to the pre-change derivations, copied into the test as the oracle. It runs on 12 seeded fixtures with and without the unsampled signal, built with repeated urls, pages an older audit saw removed, overlapping crawled/removed sets and signal-only crawled pages. It covers:

    • sitePages, activePageUrls, activePageCount, isActivePage, everRendered;
    • carriedPageUrls (size, order, has);
    • writing changedSitePages over the prior rows giving the same rows as writing every page.

    A publish test checks that 5,000 known pages with 20 crawled upserts exactly those 20 and leaves every other row untouched.

  • Mutation-checked: each of six mutations fails at least one test: the removed-page carried correction dropped; the crawled/removed overlap subtracted twice; the active correction dropped; the repeat-url pass skipped; every page upserted; everRendered read as active.

Verification

  • bun test (audit-engine, run in batches of 5 files or fewer):
    • complete-store-parity, cloud-merge, bounded-carried-merge, carried-provenance-merge, sampled-audited-pages: 100 pass;
    • rate-limited-coverage, item-locator-resolve, fingerprint-parity, scan-truncated-carry, merge-scoring: 54 pass;
    • advisory-scoring, output-caps-score-parity, item-finding-identity, golden-baseline, streaming-scoring-golden: 33 pass;
    • merge-session-pages: 13 pass.
  • bun test (apps/cli), tests/audit/smart-audits.test.ts and tests/scoring.test.ts: 50 pass.
  • tsgo --noEmit (packages/audit-engine): clean.
  • bun run format:check and bun run lint (root): clean. The two oxlint warnings in complete-store-fold.ts were already there.
  • detect-secrets-hook --baseline .secrets.baseline on the changed files: clean.
  • Reproduce: cd packages/audit-engine && bun run scripts/bench-untouched-carried.ts --carried=0, then --carried=250000, alternating.

Agent skill

No change needed. Nothing user-facing or CLI-facing changes. This is the cloud publish merge's internal cost, and the CLI's local merge output is identical. No docs change for the same reason.

Changelog

  • Cloud: a publish to a site with a long page history no longer rewrites every one of its pages, and the merge's cost stays close to that of a site with none (engine side).

Checklist

  • I added or updated tests for behavioral changes.
  • I did not include secrets, customer data, or incompatible third-party material.
  • Every commit includes a DCO Signed-off-by line (git commit -s).

Generated by Claude Code

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]>
@claude

claude Bot commented Oct 8, 2026

Copy link
Copy Markdown

Reviewed the diff for #497 (merge-core, merge-promise, scoring, the new tests and the bench script). I found no blocking problems.

I traced the new count arithmetic against the old copies:

  • activePageCount corrects the prior active count per page the run touched.
  • carriedPageCount subtracts a prior-active url once, whether it was crawled, removed or both.
  • carriesPage uses crawledUrls only, not the resolution set. That matches the old carriedPageUrls derivation.
  • indexPriorPages keeps the first position and the last row for a repeated url, like one Map.set per row.
  • buildSitePages keeps the same order as the old map.

merge-session-pages.test.ts pins these against a copy of the old derivations. The fixtures include repeated urls and overlapping crawled and removed sets.

Findings:

  • (medium) merge-promise.ts (upsertSitePages): this changes the contract with stores. The rows a store ends up holding are the same only if it already holds every prior page from getSitePages. A SmartAuditStore that treats upsertSitePages as the full snapshot, or that returns getSitePages from a cache, would now drift. I only checked the engine side, and the PR notes the hosting side is not re-measured. Please add a doc comment on SmartAuditStore.upsertSitePages stating the new delta semantics, and confirm that every in-repo implementation (CLI stores included) copes with it.
  • (low) merge-core.ts (changedSitePages): it is built eagerly at session creation, so it will not reflect later mutation. That is fine today because thisRun is only written during construction. A short comment saying so would help.
  • (low) merge-core.ts (carriedPageUrls.has): it reads priorOf, so a url that is only in thisRun returns false. That is correct, since carried means prior-only. The behavior differs from a plain Set in non-obvious ways, so it is worth a one-line note on PageUrlSet.
  • (nit) indexPriorPages still allocates a pages array copy in the common no-duplicate case. You could reuse priorPages directly when index.size === priorPages.length and save one more O(site) copy.
  • (nit) The benchmark table reports 1.59x CPU, under the 2x bound. The PR text says the engine side is met, which is consistent. The 100-findings-per-page case at 1.43x to 1.46x is documented. Good.

I did not run the tests or the benchmark.

…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]>

nc9 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Round 1, addressed in 6734839:

  • (medium) SmartAuditStore.upsertSitePages now documents the delta semantics: insert or replace by url, leave every other row as it is. Only the pages this run crawled are passed; removed ones still go through markPagesRemoved. In-repo implementations:

    • The CLI's stores never see this path. apps/cli merges through computeMerge and still persists the full sitePages, which is unchanged.
    • Every test MemStore upserts by url.

    On the cache concern: an unchanged prior row used to be written back exactly as getSitePages returned it. With a stale cache, that write would have overwritten newer rows with stale ones. Skipping it removes that risk rather than adding one.

  • (low) changedSitePages: comment added. It is taken once because thisRun is complete by then.

  • (low) PageUrlSet.has: comment added. It answers only for the view's members, so a page known only from this run is not carried.

  • (nit) indexPriorPages now reuses priorPages when no url repeats.

Tests, typecheck, lint, format and the secret scan are clean. The bench digests are unchanged (806b03e2a37a7a4d at 0 carried, 866f6c47e809d687 at 250k).

@claude

claude Bot commented Oct 8, 2026

Copy link
Copy Markdown

Reviewed the diff (I didn't run the tests or the benchmark). The change looks sound overall. The new index and counts reproduce the old derivations, and the oracle-based tests are a good way to pin that.

  • (medium) merge-promise.ts (upsertSitePages): this changes the store contract from a snapshot to a delta. A hosting store that treats upsertSitePages as a full replace, or that prunes rows missing from the call, would silently drop the untouched pages. The PR says the hosting side isn't re-measured, so I'd also want an explicit confirmation that the hosting implementation is an insert-or-replace by normalizedUrl. The new JSDoc helps, but this is a behaviour change for an external implementer. Consider a note in the changelog or the store docs.
  • (low) merge-core.ts (carriedPageUrls): it is a lazy view over crawledUrls and removedUrls, and carriedPageCount is computed eagerly. If a caller mutates those sets after session creation, size and iteration/has can disagree. replayedUrls.clear() is a different set, so it looks safe today. A comment saying the sets must stay frozen would guard against regressions.
  • (low) merge-core.ts (activePageUrls getter): the sitePages and activePageUrls getters memoize lazily and return mutable arrays and sets. A caller that mutates one will affect later readers, where before each was just a plain field. This is acceptable, but readonly types would make the contract clearer.
  • (nit) scripts/bench-untouched-carried.ts: Number(hit.slice(...)) yields NaN on bad input and the script doesn't validate it.
  • (nit) The benchmark's 1.59x CPU ratio sits close to the 2x bound, and the hosting-side measurement is still outstanding. The PR is correct to leave that criterion unchecked.

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]>

nc9 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Store contract for upsertSitePages, in one push: f2efa67 merges main, then 6020063.

  • Doc comment on SmartAuditStore.upsertSitePages: it is a keyed upsert, never a snapshot replace. Each row is inserted, or replaces the stored row with the same (siteKey, normalizedUrl). A row not passed stays exactly as it is, and an implementation must never delete or reset a page because the call left it out. Callers may pass only the rows that changed, and runCloudSmartAudits does: the pages this run crawled. Removed pages still go through markPagesRemoved.
  • Test in tests/merge-session-pages.test.ts, run on 6 seeded fixtures. A KeyedUpsertStore reference implements exactly that contract and holds another site's rows alongside. On each fixture, the publish's changed-rows write leaves the same final rows as the previous behaviour, which wrote every merged page back in the same order of writes. The fixtures cover repeated urls, overlapping crawled and removed sets, and pages an older audit saw removed. The test also asserts that the publish upserted fewer rows than the full write, and no more than the crawled pages.
  • Mutation-checked: a store that replaces the site's rows as a snapshot fails all 6 cases, and so does dropping one changed row from the publish's write.

On the merged head:

  • tsgo --noEmit is clean.
  • The merge, parity, golden and CLI smart-audits tests pass (231 across 15 files).
  • Root format:check and lint, and the secret scan, are clean.

Not merging.

nc9 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Re the review of a13d9cf (the first push):

  • (medium) The store contract is addressed in 6734839 and 6020063:
    • The doc comment states that upsertSitePages is a keyed upsert and never a snapshot replace, and that callers may pass only changed rows.
    • A keyed-upsert reference store proves the same final rows as the old full write, on 6 seeded fixtures. The test fails for a snapshot-replacing store.
    • The maintainer confirmed that the hosted store's upsertSitePages is a keyed insert-or-update on (site, normalized url), so the change is compatible there.
    • There is no changelog note, since the contract has no public implementers outside this repo.
  • (low) Frozen scope sets, and readonly on the memoized getters: both are correct observations about today's code. No caller mutates crawledUrls or removedUrls after the session starts, and nothing writes to sitePages or activePageUrls. Both stay as they are unless another code push comes.
  • (nit) Bench argument parsing: the bench is a dev script with documented flags; it stays as is.

@claude

claude Bot commented Oct 8, 2026

Copy link
Copy Markdown

Review of #586 (carried-page copies in the merge session).

I found nothing that should block the merge.

  • (medium) merge-promise.ts (upsertSitePages call): runCloudSmartAudits now upserts only changedSitePages. This is safe for the in-repo store, because sqlite.ts uses INSERT OR REPLACE keyed on site and url. It silently breaks any SmartAuditStore that treats upsertSitePages as a snapshot replace and deletes or resets the pages it isn't handed. The hosted store lives outside this repo, so I couldn't check it. The new doc comment on the interface and the KeyedUpsertStore contract test help. Please confirm the hosted implementation is a keyed upsert before this ships.
  • (low) merge-core.ts (carriedPageUrls): has and the iterator are lazy views, and size is a precomputed count. If the size arithmetic and the has predicate drift apart (for example, a url that is in both crawled and removed), size goes wrong without any error. The seeded oracle tests cover this overlap case, which is good. The same applies to activePageCount.
  • (low) indexPriorPages: the duplicate-url path rebuilds the index and a second array. That is fine, since it only runs for hand-built inputs.
  • (nit) apps/cli/src/audit/smart-audits.ts still builds the full merged.sitePages and activePageUrls copies. The lazy getters keep it correct, so this is not a bug, but the local CLI doesn't get the speedup.
  • (nit) The getFindings: async () => [] stub in the bench script and the benchmark numbers in the doc can't be reproduced from the diff alone. A line pointing at the exact command would help.

Tests: merge-session-pages.test.ts compares the new session against a copy of the pre-#497 derivations, using the seeded rng fixtures. It includes duplicate urls, overlapping crawled and removed sets, and the unsampled-signal path. It also pins the 20-row upsert for a 5,000-page site. I didn't run the suite.

nc9 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Re the review of 6020063:

  • (medium) Confirmed: the maintainer checked the hosted store. Its upsertSitePages is a keyed insert-or-update on (site, normalized url), never a snapshot replace. That is the contract the doc comment states and KeyedUpsertStore pins.
  • (low) Count/predicate drift in carriedPageUrls and activePageCount: covered by the oracle tests, as the review notes, including the crawled-and-removed overlap. Kept as is.
  • (low) The duplicate-url path in indexPriorPages runs only on hand-built inputs. Kept as is.
  • (nit) The CLI's local merge keeps the full copies through the lazy getters. It is correct, and that path is outside this issue's scope.
  • (nit) The exact reproduction command is in the bench script's header comment, and the record names the harness. Kept as is.

No code change from this round.

nc9 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Status: done from my side. Every check on 6020063 is green (15 of 15). There were two bot review rounds:

  • Round 1's medium (the upsertSitePages contract) is fixed by the doc comment and the keyed-upsert reference-store test, and the maintainer confirmed the hosted store's behaviour.
  • Round 2 had no blocking findings; its lows and nits are answered above.

Criterion 3 is met on the engine side (1.59x CPU, 1.72x wall). Its hosted re-measure is for after merge. I'm not merging this.

@claude

claude Bot commented Oct 8, 2026

Copy link
Copy Markdown

Review of #586: avoiding the per-publish copies of the site's page history in the merge session.

I read the diff and the SQLite store's upsertSitePages. I did not run the tests or the benchmark. The design is sound: indexing the prior pages once with first-position/last-row semantics, building sitePages and activePageUrls lazily, and passing the carried pages as a PageUrlSet view are consistent with each other. The INSERT OR REPLACE in packages/crawler/src/storage/sqlite.ts is keyed, so writing only the changed rows leaves the same end state there.

  • (medium) merge-promise.ts (upsertSitePages(session.changedSitePages…)): this narrows what callers of SmartAuditStore.upsertSitePages receive. The new doc comment now requires a keyed upsert. Any store that treats the call as a snapshot replace (delete-then-insert, or one that prunes pages not passed) would now lose every untouched page. The hosting-side store lives outside this repo, so I could not check it. Please confirm it before merge, and consider a contract test that a store passes when given only a subset of rows.
  • (low) merge-core.ts, activePageCount / carriedPageCount: these are now hand-maintained counters corrected from the run's pages. carriedPageCount subtracts using crawledUrls and removedUrls, but thisRun also includes resolution.crawledUrls. That is consistent with carriesPage, which uses the same sets, so I think it is fine. A property-style test comparing the counters against a brute-force recomputation, including duplicate prior rows and overlap between crawled and removed URLs, would guard against drift. I only skimmed merge-session-pages.test.ts, so such a test may already be there.
  • (low) carriedPageUrls[Symbol.iterator] is a live view over known.pages. If a consumer iterates it after a later mutation, it will not match size. Nothing mutates it today, but a short note on the type would help.
  • (nit) bench-untouched-carried.ts: arg() returns NaN for a malformed value. A guard would make misuse fail loudly.
  • (nit) The benchmark reports 1.59x CPU against the 2x bound, but 1.43x to 1.46x at 100 carried findings per page was already the case on base. The PR text says so, which is good.

nc9 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Re the latest review (no code change):

  • (medium) Both asks are already in place:
    • Confirmed: the maintainer checked the hosted store, and its upsertSitePages is a keyed insert-or-update on (site, normalized url).
    • The contract test exists: the upsertSitePages as a keyed upsert block in tests/merge-session-pages.test.ts. It gives a KeyedUpsertStore only the changed rows and asserts the same final rows as the full write, on 6 seeded fixtures. It fails for a snapshot-replacing store.
  • (low) Counter drift: also already there. The merge session site pages == the copies they replace block compares activePageCount, carriedPageUrls.size, its order and has against a brute-force copy of the old derivations. It runs 12 seeded fixtures, with duplicate prior rows, overlapping crawled and removed sets, and the signal-crawled path.
  • (low) Live iterator: nothing mutates the scope sets after the session starts. It stays as is, as answered in the previous round.
  • (nit) arg() returning NaN: the bench is a dev script; it stays as is.

@claude

claude Bot commented Oct 8, 2026

Copy link
Copy Markdown

Review of #586 (diff only; I did not run the tests or the bench).

The session refactor looks correct. I traced the following and they hold:

  • indexPriorPages keeps the first position and the last row, which matches repeated Map.set.
  • activePageCount is the prior active count corrected page by page from thisRun.
  • carriedPageCount is consistent with carriesPage. markCrawled skips removed URLs, so no URL is counted twice.
  • buildSitePages reproduces the old ordering.

Findings:

  • (medium) merge-promise.ts (upsertSitePages): the call now passes only changed rows. That is only safe if every SmartAuditStore implementation treats upsertSitePages as a keyed upsert. A store that treated it as a snapshot replace, or that deletes pages missing from the call, would now drop the whole page history. The new doc comment states the contract, but nothing enforces it. Please check the in-repo and hosting-side stores, and consider a contract test in the store test suite, for example upsert one row and assert the other rows survive. The PR body flags this as a store-facing change, so release notes or a coordinated rollout are worth considering.
  • (low) carriedPageUrls in merge-core.ts: the view is lazy, so it reads crawledUrls and removedUrls at iteration time, not at build time. If anything mutates those sets after the session is created, size (computed eagerly) and iteration/has would disagree. Please confirm nothing mutates them, or snapshot them.
  • (low) merge-core.ts: PageUrlSet.has only answers for prior pages, as documented. Consumers that previously received a Set<string> may rely on has for run-only pages. A short check of the scoring.ts and complete-store-fold.ts call sites would settle this.
  • (low) Tests: merge-session-pages.test.ts is a good start. Please also cover the following, if not already covered: a duplicate prior URL combined with a crawled URL, a removed URL that is not in the prior pages, activePageCount against activePageUrls.size as a property, and an end-to-end store-state equivalence check between the old (full upsert) and new (changed-only upsert) paths.
  • (nit) everRendered and neverRendered now both read known.index, so the comment above neverRendered still talks about a set of its own. It could be tightened.

nc9 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Re the review of d8fd939 (no code change; each point is already covered):

  • (medium) The store contract is confirmed and pinned:
    • The hosted store's upsertSitePages is a keyed insert-or-update on (site, normalized url), checked by the maintainer.
    • The in-repo SQLite store is INSERT OR REPLACE on the same key.
    • The contract test exists: upsertSitePages as a keyed upsert in tests/merge-session-pages.test.ts. It passes the changed rows only, alongside another site's rows, and asserts every other row survives with the same final rows as the full write. A snapshot-replacing store fails it.
  • (low) Scope sets: nothing mutates crawledUrls or removedUrls after the session is created. runCloudSmartAudits settles both before createMergeSession and only reads them afterwards.
  • (low) has for run-only pages: the old carriedPageUrls set never held them either. It was built from prior pages minus crawled and removed. So the has call in complete-store-fold.ts gets the same answers, and scoring.ts reads only size there, or walks the set on the sampled path.
  • (low) Tests: already covered by the 12 seeded fixtures and the publish-level test:
    • prior urls listed twice (asserted present in each fixture), which crawled pages also hit;
    • removed urls drawn from 0 to 360 against 300 prior pages, so some are not prior;
    • activePageCount against the reference active set's size;
    • end-to-end final store rows, full upsert vs changed-only.
  • (nit) The neverRendered comment already says it reads the prior pages' index, not a set of its own.

@claude

claude Bot commented Oct 8, 2026

Copy link
Copy Markdown

Review of #586. I read the full diff and traced carriedPageUrls through withoutUnscorablePages and the fold. I did not run the tests or the bench.

The index-plus-overlay approach looks correct. These all match the old derivations:

  • first-position/last-row semantics for duplicate urls
  • the activePageCount correction
  • carriedPageCount, including urls that are both crawled and removed
  • the lazy sitePages and activePageUrls

The oracle-based test (reference()) is a good way to pin this. It uses duplicate urls, overlapping crawled/removed sets and the resolution signal.

  • (medium) merge-promise.ts (upsertSitePages): this is a behavior change for any SmartAuditStore implementation outside this repo. It now only works if upsertSitePages is a keyed upsert and the store already holds the rows getSitePages returned. The new JSDoc states the contract, but nothing enforces it. I'd call it out in the release notes or the hosting-side PR. A snapshot-replace implementation would silently drop pages, and also site_pages rows that no longer exist.
  • (low) merge-core.ts (carriedPageUrls): the view reads crawledUrls and removedUrls lazily. If a caller mutates those sets after the session is created, size is frozen but has and iteration change. runCloudSmartAudits doesn't appear to do this, but a one-line comment, or a snapshot, would make the invariant explicit.
  • (low) merge-core.ts (indexPriorPages): the duplicate-url branch runs on every call after the first Map pass, so a hand-built input pays a double index. That is fine. The comment says store rows are unique, so consider asserting this in the cloud path.
  • (nit) The bench script uses Bun.gc and Bun.CryptoHasher. That is fine for scripts/, but it won't run under Node.
  • (nit) The benchmark doc reports 1.59x CPU at 250k carried findings. The criterion's hosting side is explicitly not re-measured, so the checkbox wording in the PR body is right to leave it unchecked.

No correctness blockers found.

nc9 added 2 commits October 8, 2026 15:14
…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]>
…e-ctljjj' into claude/bound-complete-store-merge-ctljjj

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

nc9 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

The upsertSitePages contract is now enforced, in one push. f9e5bc3 merges main, 1f3fbf0 is the change, and 01b8246 merges the branch's own newer merge of main (#587).

  • Shared contract, in packages/audit-engine/tests/helpers/site-pages-contract.ts (describeSitePagesContract), with three cases:
    • upsert one row, then a different row: getSitePages returns both;
    • upserting an existing key replaces that row and only that row;
    • rows absent from a call, an empty call included, are never deleted or changed.
  • Every in-repo implementation runs it (11):
    • SQLite storage, from packages/crawler/tests/page-findings-store.test.ts;
    • the bench store, moved to scripts/bench-store.ts;
    • the test stores in cloud-merge, bounded-carried-merge, complete-store-parity, item-locator-resolve, sampled-audited-pages, carried-provenance-merge, scan-truncated-carry, rate-limited-coverage and merge-session-pages (two there).
  • Two violators, fixed: the EmptyStore in rate-limited-coverage and the bench store both ignored upsertSitePages, so getSitePages never returned what was upserted. Both now keep a keyed map. The bench output is unchanged: same digests, 200 page rows written.
  • Enforcement: tests/site-pages-contract-coverage.test.ts scans packages/ and apps/ for any upsertSitePages implementation. It fails when one does not run the contract, and it checks that the scan finds the known ones. The interface doc points at both files.
  • Mutation-checked:
    • a snapshot-replacing SQLite store fails 3 cases;
    • a snapshot-replacing bench store fails 3;
    • the old no-op EmptyStore fails 3;
    • removing the contract call from cloud-merge fails the guard.
  • Local runs on the merged head:
    • tsgo --noEmit is clean for audit-engine and crawler.
    • The contract, guard, merge, parity and golden tests pass (217 across 14 files), plus the crawler store test (17) and CLI smart-audits and scoring (50).
    • Root format and lint are clean, and the secret scan is clean.

Not merging; it's yours to land.

nc9 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Re the review posted at 15:13: it reviewed the head before 1f3fbf0, and the review of the current head is still running.

  • (medium) "Nothing enforces it" is addressed in 1f3fbf0:
    • Every in-repo upsertSitePages implementation runs the shared keyed-upsert contract.
    • The SQLite store is among them.
    • site-pages-contract-coverage.test.ts fails for any implementation that does not run it.
    • The maintainer confirmed that the hosted store is a keyed upsert, so the out-of-repo implementation is covered too.
  • (low) Lazy scope sets: answered above. Nothing mutates them after the session starts.
  • (low) Asserting unique urls on the cloud path: kept as is. The dedupe pass gives the same rows either way, and an assert would turn a harmless repeat into a failed publish.
  • (nits) Bun-only bench and benchmark wording: kept as is.

@claude

claude Bot commented Oct 8, 2026

Copy link
Copy Markdown

Review of #586 (diff read; I did not run the tests, and CI was still pending when I looked).

The approach is sound. The prior pages are indexed once, thisRun stays bounded by the run, and the merged list is built lazily. The first-position, last-row semantics in indexPriorPages match one Map.set per row. I traced the activePageCount and carriedPageCount corrections against the old derivation and they agree.

  • (medium) merge-promise.ts (upsertSitePages(session.changedSitePages…)): this is a behaviour change at the store contract. A hosting-side upsertSitePages that acts as a snapshot replace, or that bumps something like lastSeenAt or a retention clock for every row passed, would now behave differently. The end state is only the same for a strict keyed upsert. The new JSDoc and the contract tests cover the in-repo stores. Please call this out for the hosting maintainers before release, since that harness isn't in this repo.
  • (low) merge-core.ts: carriedPageUrls iteration and has rely on crawledUrls and removedUrls staying unmutated after session creation. The changedSitePages comment says nothing writes thisRun later, but the lazy getters and the view depend on this too. A short note on the MergeSession interface would help.
  • (low) scoring.ts: PageUrlSet.has on the view returns false for pages known only from this run. That is documented, but a Set<string> also satisfies the type, so the two have different semantics behind one interface. Consider a test that pins the view's has and iteration behaviour against a Set built from the old derivation. merge-session-pages.test.ts may already cover this.
  • (nit) The measured 1.59x CPU ratio is within the 2x target. The benchmark was a minimum of five runs on a single machine, so it will vary run to run.

nc9 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Re the review of the current head (no code change):

  • (medium) Hosting side: done. The maintainer checked the hosted store before this push: upsertSitePages is a keyed insert-or-update on (site, normalized url), with no per-row side effects for rows not passed. The interface doc states the contract, and every in-repo store runs it.
  • (low) Unmutated scope sets: they are settled before createMergeSession and only read afterwards. Kept as is.
  • (low) PageUrlSet view vs Set: already pinned. merge-session-pages.test.ts compares the view's size, iteration order and has (for 400 urls, prior, new and absent) against a Set built by the old derivation, on 12 seeded fixtures.
  • (nit) Measurement variance: noted. The method (alternating arms, minimum of five) and the machine are recorded in the benchmark record.

@claude

claude Bot commented Oct 8, 2026

Copy link
Copy Markdown

Reviewed the engine diff (merge-core, merge-promise, scoring, bench scripts). I could not run the tests, and CI was still pending when I looked.

  • (medium) merge-promise.ts upsert change: upsertSitePages now gets only changedSitePages. This is safe only for stores that treat it as a keyed upsert. The new contract doc and site-pages-contract-coverage.test.ts cover in-repo stores. Out-of-repo (hosting) stores that replace a snapshot would silently drop every untouched page. Call this out in the release notes, and confirm the hosting store before it ships.
  • (low) merge-core.ts: carriedPageUrls.size is computed by decrementing counters from crawledUrls/removedUrls, while has and iteration filter live. They agree only if the crawledUrls the session sees matches what markCrawled handled, including resolution.crawledUrls. A cheap dev-time assertion, or a test that compares size with [...carriedPageUrls].length on a fixture with resolution-only crawled pages, would guard against drift.
  • (low) sitePages and activePageUrls are lazy getters that memoize on first read. A caller that reads them before addPriorFindings or finish() would see a snapshot. That is fine today because thisRun is complete at construction, but a short comment saying so would help.
  • (nit) The bench shows 1.59x CPU at 250k carried findings, under the 2x bound. The PR text rightly notes the hosting side was not re-measured, so keep the acceptance checkbox unchecked.
  • (nit) bench-store.ts getSitePages returns its cached array by reference. A caller that mutates it would corrupt later reads, so consider Object.freeze or a copy outside the timed path.

The index/dedup logic (first position, last row wins) looks correct, and the parity and contract tests are the right coverage.

nc9 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Re the review at 15:41 (no code change): CI is green on 01b8246, 15 of 15.

  • (medium) The hosting store is confirmed as a keyed upsert (answered above), and the in-repo stores are enforced by the contract and the coverage test.
  • (low) size vs iteration with resolution-only crawled pages: already pinned. The with the unsampled signal fixtures in merge-session-pages.test.ts (6 seeds) add resolution-only crawled pages. On each one they assert carriedPageUrls.size, the iterated list and has against the old derivation.
  • (low) Lazy getters: answered above. thisRun is complete at construction, and the changedSitePages comment says so.
  • (nit) The bench store's cached array: it is a dev script with one reader. Kept as is.

@nc9

nc9 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

On the (medium): the hosting-side store was checked and implements upsertSitePages as a keyed insert-or-update (insert with on-conflict-update on the site and normalized url), never a snapshot replace, and the contract is now documented and enforced for every in-repo store by site-pages-contract-coverage.test.ts. The behaviour change goes in the release notes.

@nc9
nc9 merged commit b78bc6c into main Oct 8, 2026
15 checks passed
@nc9
nc9 deleted the claude/bound-complete-store-merge-ctljjj branch October 8, 2026 15:52
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