Skip to content

Make PRISM share-window reads scale with the window, not ledger history (1.x.x) - #115

Merged
djh58 merged 4 commits into
1.x.xfrom
dan/prism-window-final-pass
Aug 12, 2026
Merged

djh58 merged 4 commits into
1.x.xfrom
dan/prism-window-final-pass

Conversation

@djh58

@djh58 djh58 commented Aug 12, 2026 •

Copy link
Copy Markdown
Member

Summary

PRISM's reward-window readers find the window's lower boundary cheaply (the 4096-row paged cutoff walk introduced in #58), but then consume that boundary in a way the planner cannot use:

  • snapshot_at_job_issue (the job-build/audit window snapshot) joined the one-row page_cutoff CTE into its final ranking pass. A joined cutoff is applied as a post-scan join filter, so the backward pkey walk is free to visit the entire accepted history on every execution and discard everything below the boundary — O(history) per call, growing linearly as the ledger grows, on paths that run continuously (payout-artifact builds, payout publication, reconcile candidate preparation).
  • The server-side qbit_prism_window() function (settlement/audit paths) still used the pre-Reduce PRISM shared tip-refresh bundle latency #58 recursion: one lateral index probe per share, i.e. window-size probes per call.

This change:

  • consumes the cutoff as a scalar subquery, which plans as an InitPlan the backward index scan uses as its start bound, keeping the final pass O(window). (Observed as such on analyzed Postgres 16; a cost-based fallback to a different scan strategy can only affect cost, never results — the cutoff stays a single scalar evaluated under the same statement snapshot.);
  • ports qbit_prism_window() to the same paged walk with the same bounded final pass.

Window contents are unchanged: identical rows, cumulative sums, and partial-credit crossing behavior.

Verification

Against a synthetic 500k-share ledger on Postgres 16 (anchor mid-history so ~10% of rows are ineligible, including accept-lag rows straddling the anchor; ~3% rejected rows):

  • Row-for-row equivalence of old vs new qbit_prism_window(): EXCEPT ALL in both directions returns zero rows across a 27-weight boundary-stress grid — exact cumulative boundaries (cum(k)-1, cum(k), cum(k)+1 for k in {1, 2, 1000, 4095, 4096, 4097, 8192, 20000}), a single-unit window, and a beyond-total-history weight (447,565-row full window).
  • Byte-identical snapshot output of old vs new ranked pass: the full JSON snapshot compares equal at a 1-unit window and at a 20k-row window (~9.1 MB; the synthetic fixture is randomized per run, so byte-identity is old-vs-new within a run).
  • Plan/IO shape: at 500k rows with a 20k-row window the snapshot query drops from 16,090 to 3,536 shared buffers. The old shape's cost keeps growing with total history; the new one stays proportional to the window.
  • The schema file still applies idempotently (applied twice in sequence before testing).

Tests

  • python3 -m unittest tests.test_prism_share_ledger — 139 tests, OK (query/schema shape assertions updated: the cutoff must appear as the scalar-subquery start bound, and CROSS JOIN page_cutoff must not reappear)
  • python3 -m unittest tests.test_prism_reward_window_invariant tests.test_prism_incremental_payout_window — 5 tests, OK
  • make test-prism-postgres-ledger — live Postgres 16 end-to-end check, extended here to assert the bounded snapshot's crossing behavior at exact cumulative boundaries (window weights 1/103/104/205/206 over known share difficulties) and that a beyond-history weight returns the same rows as the unbounded snapshot. The rewritten qbit_prism_window() is executed by the same check via the audit window path.
  • The live check now also seeds a deterministic 9000-share fixture so both bounded readers cross real 4096-row page boundaries: exact row counts and boundary share IDs are asserted at weights straddling 4096/8192, a page-3 crossing, exact-history, and beyond-history (where the bounded and unbounded snapshots must agree).

🤖 Generated with Claude Code


Note

Cursor Bugbot is generating a summary for commit 3c2a42a. Configure here.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

djh58 and others added 3 commits August 12, 2026 10:27
The paged cutoff walk finds the reward-window boundary in O(window),
but the snapshot's final ranking pass consumed that cutoff by joining
the one-row cutoff CTE into the scan. The planner applies a joined
cutoff as a post-scan filter, so the pass was free to walk the entire
accepted history backwards and discard everything below the boundary,
making each execution cost O(history) instead of O(window) as the
ledger grows.

Consume the cutoff as a scalar subquery instead: it plans as an
InitPlan the backward index scan uses as its start bound, keeping the
final pass proportional to the window. Port qbit_prism_window() from
its one-row-per-step recursion (one lateral index probe per share) to
the same paged walk with the same bounded final pass.

Window contents are unchanged: identical rows, cumulative sums, and
partial-credit boundary behavior, verified row-for-row against the
previous shapes on a synthetic 500k-share ledger across a
boundary-stress weight grid (exact cumulative boundaries, page
boundaries at 4095/4096/4097, single-share and full-history windows),
plus byte-identical snapshot JSON output.

Co-Authored-By: Claude Fable 5 <[email protected]>
The bounded branch of snapshot_at_job_issue was only pinned by query-text
assertions; the live-Postgres check exercised just the unbounded branch.
Assert the crossing share is included and nothing past it at exact
cumulative boundaries, and that a beyond-history weight matches the
unbounded snapshot.

Co-Authored-By: Claude Fable 5 <[email protected]>
Every existing fixture fits inside one 4096-row cutoff page, so the
recursive page step of both bounded window readers never executed in
any committed test. Seed 9000 deterministic unit-difficulty shares and
assert exact row counts and boundary share IDs for the bounded snapshot
(weights straddling the 4096/8192 page boundaries, exact-history, and
beyond-history) and for the audit window path through
qbit_prism_window() (window weights 4096, 8192, and a page-3 crossing).

Also pin the schema to exactly one windowed difficulty sum, and note at
both walk sites that page-granular stopping relies on the strictly
positive share_difficulty constraint.

Co-Authored-By: Claude Fable 5 <[email protected]>
@djh58

djh58 commented Aug 12, 2026

Copy link
Copy Markdown
Member Author

CI note: the "Lint, compile, and test" job on this branch inherits a pre-existing hang on the current 1.x.x base (an infinite append-retry in the vardiff suite from test fakes missing the landing-fence timestamp fields — see #114's drive-by fix, whose run completes in ~2.5 min). Cancelled this branch's stuck runs; will re-trigger once #114 lands.

@blacksmith-sh

This comment has been minimized.

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