Skip to content

perf(engine): start page rules on pages whose cloud prefetch has finished (#2121) - #2185

Merged
nc9 merged 4 commits into
mainfrom
boat/engine-early-rules-2121
Nov 22, 2026
Merged

nc9 merged 4 commits into
mainfrom
boat/engine-early-rules-2121

Conversation

@nc9

@nc9 nc9 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2121 (option A, planning's decision).

What changed

  • CloudPrefetchAccumulator (src/cloud-prefetch.ts) gains per-page readiness on its existing state: pageEnvelopes(url) returns every envelope the final store will hold for that page, or null while one can still change; changed() wakes a reader when a batch lands, a service finishes, Stage 0 decides or the run settles/abandons; result exposes the settled result. No new event or budget mechanism: onCall, the budgets and the dispatch are untouched, and the prefetch's own store is still written only by the batch-order merge.
  • LateCloudResults (src/late-cloud-results.ts, exported) binds a running prefetch to the rules. It owns the store the runner reads: a page about to run gets its own final envelopes copied in; on settle the store is rebuilt as an exact copy of the result store (batch order, abandon skips included).
  • runStreamingRules({ lateCloudResults }): universe and site-fetch overlap the prefetch; the runner is built once Stage 0 decides the site profile; streamPageRules waits per page (cloudReady, bounded by deadlineAt); the site rules run on the settled store. Plugin rule packs wait for settle before the page pass. Without the option nothing changes.
  • prefetchCloudDataWithDeadline takes an optional caller-owned accumulator; runContainerCloudPrefetchFromPayloads passes input.accumulator on its deadline path.

Cache key: option (i)

Pages whose batch is keyed before the prefetch settles bypass the rule cache (null key: no read, no write). lateBoundRuleCache binds on the first key asked after settle, hashing the settled store with the same computeRunContextHash inputs as the serial path, so those pages use exactly the serial key. The serial branch hashes scope as before (same inputs, same call).

  • Serial key and hits unchanged: rule cache: the serial key and hits are unchanged; late pages after settle hit them, early ones neither read nor write. A serial cold run stores 24 entries and a serial warm run replays all 24. A late run that settles first replays the same 24 and adds no new key. A late run with render batch 0 held stores 0 and replays 0, and its report is still identical.
  • the run-context hash of the settled late store equals the serial store's.

Acceptance criteria

  • AC1 Rules start on pages whose prefetch finished while later batches render: src/late-cloud-results.ts pageReady, src/cloud-prefetch.ts pageEnvelopes, src/streaming.ts cloudReady, wired in src/adapter.ts runStreamingRules. Test AC1: page rules run on pages whose prefetch finished while a later render batch is still out: the last render batch is held until a page's rules have written, then released by that write, not by the backstop. Mutation (the page pass waits for settle) fails it.
  • AC2 Final store in batch order: test AC2: with batch 0 slow, the awaited store is in batch order, and so is the late view once settled. The awaited prefetchCloudData render keys start with p0. The view, filled p1, p2, p3 before p0, is rebuilt in batch order on settle. Live writes go to the binder's view, never the prefetch store, so no delete-then-set is needed there; the rebuild is the reorder. Fail-before by mutation: patching the view instead of rebuilding it fails the test.
  • AC3 A late batch never overwrites abandon() skips: test AC3: a batch that lands after abandon() does not overwrite the abandon's skips. Abandon at 100 ms, then the held batch lands and merges into the prefetch's own store. The abandon result, pageEnvelopes and the rules' store all keep prefetch-out-of-time. Fail-before by mutation: binding from acc.store, or reading acc.store in the settled branch of pageEnvelopes, each fails it.
  • AC4 Byte-identical on the canonical golden fixture: tests/late-cloud-results-golden-2121.test.ts. 518 pages, every cloud rule on, deterministic stub cloud, render batch 0 at 150 ms and the rest at 10 ms. The serial and late runs give identical golden snapshots and identical report JSON (timestamps removed). Small-fixture twin: AC4 (small fixture).
  • AC5 Time saved (mocked latency, see model below): bun run packages/audit-engine/scripts/bench-late-cloud-results.ts --runs 2
    • run 1: serial 59.5 s, late 43.6 s, saved 16.0 s (27%), early pages 509, report identical
    • run 2: serial 58.7 s, late 43.6 s, saved 15.1 s (26%), early pages 509, report identical

Latency model: canonical golden fixture, 500 cloud page payloads, no network. Stage 0 site-metadata 1500 ms. Each site-unit service 500 ms. ai-parse and authority-signals 500 ms per batch. Render 2000 ms per batch, paid on submit. Batch size 10 (render's cap) with 4 batches in flight. Both arms are timed from prefetch start to rules returned. Page readiness needs every page-unit service, and render runs last, so the overlap covers the universe and site-fetch phases plus page rules during the render batches.

Gates

  • cd packages/audit-engine && bun run typecheck: clean. cd apps/cli && bun run typecheck: clean.
  • root bun run format:check and bun run lint: exit 0 (audit-engine has no format/lint script).
  • bun run scripts/test-suites.ts --check: test suites cover every test file.
  • bun run scripts/test-suites.ts --run audit-engine: 2375 pass, 3 skip, 0 fail (2378 tests, 250 files).
  • bun run scripts/test-suites.ts --run cli: 4574 pass, 14 skip, 0 fail (4588 tests, 395 files).

Notes

  • No changelog.d entry: no user sees a change until the private container runtime passes lateCloudResults (follow-up in the private repo). The CLI path stays serial.
  • Docs: none (internal engine API). Agent skill: no change needed.

Review fix round (c9f3cef)

  • Prefetch rejection after Stage 0 no longer poisons the cache. LateCloudResults.failed records a rejected prefetch, and the late cache binder in runStreamingRules then returns null, so every page in that run gets a null key (no read, no write). Test: a prefetch that rejects mid-run writes nothing to the rule cache, does not throw, and the report is produced (fails without the guard: entries get stored under a key hashing an absent store and site profile).
  • A deadline win drops the pageReady waiter. streamPageRules aborts the cloud wait when deadlineAt wins. pageReady(url, signal) and CloudPrefetchAccumulator.changed(signal) remove their waiter instead of leaving it parked until the next change. Test: deadlineAt expiring during a cloudReady wait ends the page pass and leaves no waiter parked (one waiter is parked before the deadline and none after; it fails without the abort, and it fails when pageReady ignores the signal).
  • New tests: stream batches straddling settle get null keys, then the serial key, in one pass (and a lateBoundRuleCache unit test of one key loop straddling settle). A plugin rule pack waits for settle. Stage-0 failure, skipAll (preflight failure and cloud disabled) and emptyResult each wake the waiters. A url listed in two batches is final only once both batches have returned, and it carries the later batch's envelope.
  • Gates at c9f3cef (merged with origin/main): format:check and lint exit 0. Both typechecks are clean. test-suites --check passes. --run audit-engine: 2395 pass, 3 skip, 0 fail. --run cli: 4583 pass, 14 skip, 0 fail.

Scope notes

  • Not wired into the container yet: the private runtime passing lateCloudResults is the follow-up squirrelscan/repo#5025. Until then, no run takes this path.
  • Per-batch head-of-line blocking (a page waits on its own batch, and the pages after it wait in order) limits speed. It is not a correctness bug.

nc9 added 4 commits November 22, 2026 12:18
…shed (#2121)

LateCloudResults binds a running prefetch to runStreamingRules: the runner
is built once Stage 0 decides the site profile, each page's rules run once
CloudPrefetchAccumulator.pageEnvelopes says its envelopes are final, and the
site rules run on an exact copy of the settled store (batch order, abandon
skips included). The prefetch's own store is never written live, so the
awaited result is unchanged. With a rule cache, pages keyed before the
prefetch settles bypass it; later pages use the serial run-context key.

Signed-off-by: Nik Cubrilovic <[email protected]>
…adline drops its cloud waiter #2121

Review fixes for pub#2185:
- LateCloudResults records a rejected prefetch (failed); runStreamingRules
  then never binds the late rule cache, so pages that ran on early results
  are never stored under a key hashing an absent store and site profile.
- streamPageRules aborts the cloudReady wait when deadlineAt wins;
  pageReady and CloudPrefetchAccumulator.changed take the signal and
  remove their waiter instead of leaving it parked.
- Tests: rejection mid-run, deadline during a cloudReady wait, stream
  batches straddling settle, a plugin pack waiting for settle, Stage-0
  failure / skipAll / emptyResult waking waiters, a url in two batches.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the boat/engine-early-rules-2121 branch from 2ab3376 to 149bee0 Compare November 22, 2026 12:25
@nc9
nc9 marked this pull request as ready for review November 22, 2026 12:46
@nc9
nc9 merged commit c1a42ed into main Nov 22, 2026
10 checks passed
@nc9
nc9 deleted the boat/engine-early-rules-2121 branch November 22, 2026 13:00
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.

engine: start rules while the cloud prefetch is still running, on main's onCall/accumulator code

1 participant