Repository navigation
3.x.x: compute the carry-forward integrity report's mismatches and drift once each, as migration 026 (#737) - #744
Merged
Conversation
…ift once each, as migration 026 (#737) 001's qbit_carry_forward_integrity_report() counts qbit_carry_forward_integrity_mismatches() and qbit_carry_forward_current_drift() in two scalar subqueries and lists them in two more, so each runs twice. Both are CPU-bound. On union's restored ledger, 2,753,849 active carry rows on a standby with replay paused, EXPLAIN ANALYZE put the validator at 20.29 s and the drift check at 9.14 s, and the report took 57.42 s. self-check, fatal-state clear and the import admission gate all wait for it, and a clear holds its transaction open that long while the chain tip may move. Migration 026 replaces the report in place. It keeps 001's subqueries word for word, the active row count and the two listings, and evaluates each once in a MATERIALIZED CTE, in 001's order: the count, the validator, then the drift check, so a run that fails stops where 001's does. Each set's count is its listing's length. Running each check once is estimated at about 29 s at production size, the sum of the two parts, not a timed run; on a fixture of 400,000 clean carry rows the report took 1.2 s instead of 2.3 s. The report feeds the recovery evidence's source and migrated summaries and the admission gate, so it must be evaluated as 001's is. Listing a materialized copy of each set, the first plan, is not. 001's mismatch order, block height then carry_forward_seq, ties for every finding without a carry row, whose seq is NULL, and a manifest that names an account twice repeats a seq. 001 lists tied findings in the order the validator's own sort leaves them, since its aggregate reads that sort as presorted. Rows read back from a CTE are sorted again, and once that sort spills it can reorder ties: with 2,000 findings at one height and work_mem at 64kB, that design and 001 differed at 1,405 of 2,000 positions. 001's own order of ties changes with work_mem, so no tiebreak is added. 026 is applied in the migration transaction with no capability and no shutdown proof, as 025 is, and an earlier binary accepts the unknown migration with a warning and reads the same report. migrate runs 026 again whenever it runs 001, as it runs 025 again after 011. 001 runs only without 3, and a recorded history without 3 is refused before any DDL, so for now that is only a fresh or 2.x.x source, where 26 is missing anyway. Two gated tests cover it. One checks the report against 001's, installed beside it, on empty, clean, legacy-break, as-issued and drift fixtures and on the 2,000 tied findings at the default and the smallest work_mem. It counts one run of each set per report, checks that a report whose sets both fail fails in the validator, as 001's does, and fails against the per-set CTE design. The other checks that 26 is recorded once, survives a migrate that runs 011 and 025 again and one with nothing to do, and is applied once to the record an earlier build left, and that a record with 26 but not 3 is refused with 026's report kept. ledger_2x's undo_006 restores 001's report with its version, and every version pin gains 26. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This was referenced Oct 7, 2026
RobertClarke
added a commit
that referenced
this pull request
Oct 8, 2026
Set VERSION and [workspace.package].version to 3.0.0-rc.5: rc.4 plus #737's fixes in #742 (fatal-state clear accepts a tip that only grew, takes --timeout-seconds, and runs the integrity report under its own statement timeout) and #744 (migration 026: the report computes each set once); the deferred share-hash backfill in #743 and #746 (migrate --defer-share-hashes, then the throttled backfill-share-hashes after go-live); #745 (migrate --offline-indexes, which composes with --defer-share-hashes); the cutover tooling merged after rc.4's tag (#728, #732, #736, #740); and #747, a test-only fix for #724. Both Cargo.lock files move only their workspace-member entries. doc/release-notes-3.0.0-rc.5.md, dated 2026-10-08, covers: - the changes, with the report's estimated 57 to 29 s since 026, the migrate --defer-share-hashes command measured on the pair at 5 min 06 s (539d6c9, before #745), backfill-share-hashes' throttle, record of 2 and safe rerun, the evidence scripts' changes, and the offline index builds' estimated gain; - upgrading: migrate is needed again (a binary-only upgrade refuses "missing migration(s) 26", and this export refuses a database without 026), the cutover's migrate step with both flags, and finishing the backfill after go-live under #738's snapshot rule, with self-check only at low share rates. Rolling back to rc.4 at fence 2 goes only by finishing the backfill first or from the pre-migrate backup or physical copy, and each rc.4 refusal is quoted where it applies: a start meets the cursor first and says to run migrate, which rc.4's migrate refuses. The rules still in force: #737's statement timeout for rc.4's tools, 026 or not, #738's snapshot rule, and fatal-state clear --timeout-seconds, whose bound ends before the commit; - the same evidence set as rc.4's, with each PR's merge SHA, the pair's serving evidence carried, and the rehearsal's full measurements left to the cutover's go/no-go record; - the known issues: rc.4's, minus #737's F1, F2, F4 and F5, plus #737's open part, #738 (its fix planned for rc.6), #739, #741, #731 and #734, with #582 closed and #712 and #724 updated. docs/prism-rust-migration.md now says how earlier candidates refuse a database at fence 2: a start with the backfill's own refusal, whose advice to run migrate holds only for this release, and their migrate, or a start with PRISM_POSTGRES_INIT_SCHEMA=1, with the fence. The development-line notes name 3.0.0-rc.5, and they and the 3.0.0 notes link all five candidates' notes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Summary
#737's F4.
qbit_carry_forward_integrity_report()as 001 defines it runs each of its two expensive sets twice: once to count it and once to list it. Migration 026 replaces it so each runs once. The report is evaluated as 001's is, by 001's own subqueries in 001's order.qbit_carry_forward_integrity_mismatches()at 20.29 s andqbit_carry_forward_current_drift()at 9.14 s, and the report took 57.42 s.self-check,fatal-state clearand the import admission gate all wait for it, and a clear holds its transaction open that long while the tip may move.work_memdoesn't help (1 GB: 54.98 s).What 026 changes
migrations/026_carry_forward_integrity_report_once.sqlrunsCREATE OR REPLACE FUNCTION qbit_carry_forward_integrity_report():checked_active_rowscount and the two listings. The listings are the mismatch and driftjsonb_aggs with their per-row objects,ORDER BYclauses andCOALESCE(…, '[]'). I checked this mechanically, as a whitespace-normalized substring match against 001's body.WITH listed AS MATERIALIZED (…), in 001's order: the count, the validator's listing, then the drift listing. A run that fails stops where 001's does, after the same work. Traced with notice-raising wrappers: 001 calls validator, drift, drift, validator; 026 calls validator, drift.MATERIALIZED, PostgreSQL inlines the CTE and copies each listing's subquery into every reference.mismatch_countandcurrent_drift_countarejsonb_array_length(...)of their listings. That is the same number 001's separatecount(*)pass returns, under the same snapshot.This deviates from the plan's SQL sketch (
WITH mismatch AS MATERIALIZED (SELECT * FROM …),count(*) FROM mismatch), because that design doesn't evaluate the listing the way 001 does. See the next section.Plumbing follows 025 (#710):
REQUIRED_SCHEMA_VERSIONSandNATIVE_MIGRATIONSgain 26.migrate_schemaruns 026 underif !versions.contains(&26) || !versions.contains(&3)and records 26 only when it is missing. So 026 runs again whenever 001 just ran, as 025 runs again after 011. 001 runs only without 3, and a recorded history without 3 is refused before any DDL (migration.rs~2886). For now the second clause therefore only ever fires on a fresh or 2.x.x source, where 26 is missing anyway (see Review).ONLINE_MIGRATIONSis untouched.scripts/prism-recovery-evidence.sql'srequired_versionsgains 26.ALL_VERSIONS: [i32; 25]gain 26.ledger_2x::undo_006deletes 26 beside 25 and restores 001's report by hand. That restore is still needed: a record that keeps 3 never runs 001 again. A comment says so. Itsvalidator_001()becamefunction_001(name), used for both the validator and the report.No capability and no quiesce proof. rc.4's
require_schema_versionaccepts an unknown recorded migration with a warning, and 026 declares nothing. So rc.4 frontends keep starting beside it and read the same report.Equivalence evidence
ORDER BY uniqueness.
encode(p2mr_program,'hex')is unique per row. Drift is a full join of two per-program aggregates on the program;p2mr_programisNOT NULLin bothqbit_payout_carry_forwardandqbit_payout_carry_forward_current, and hex is injective.(block_height, carry_forward_seq)is not unique since 011's as-issued rule, which 025 keeps. Every finding without a carry row has a NULL seq, so all of one height's such findings tie: block, manifest-account,evidence_missing,payout_entry_*andpayout_entry_unexpectedfindings. A manifest that names one account twice also yields two findings with the same non-NULL seq (reproduced: seq 11 twice,duplicate_evidenceandduplicate_evidence,gross_amount).001's order for tied rows is plan-dependent, so no tiebreak was added.
jsonb_aggreads the validator's own sort,(block_height, carry_forward_seq NULLS LAST, payout_order_key, miner_id), as presorted (PG16 presorted aggregate; no second Sort in the plan). Tied findings therefore come out in the validator's order.work_mem4MB and 64kB.Why the per-set CTE design fails.
Sort (block_height, carry_forward_seq)over the CTE scan.work_mem64kB, 001 and the per-set design differed at 1,405 of 2,000 positions, in one statement and one snapshot. At 4MB they agreed. 026 matched 001 in both cases, at8df472aband with this round's reordered body.Plans. The listing subtrees of 026's plan are node-for-node 001's (EXPLAIN VERBOSE diff; only alias numbering differs). Each set function is called once per 026 report and twice per 001 report (counted with sequence-bumping wrappers).
Other states compared on a scratch database, reordered body: the tie fixture with 16 NULL-seq findings at one height and 20 mismatches in all, the duplicated manifest account, and the 2,000 tied findings at 64kB and 4MB. All equal as jsonb and as text.
Flag for review: tied mismatch findings have no deterministic order in 001 itself, beyond the validator's four keys. That is harmless today: self-check, clear and the gate refuse any mismatch, and the evidence summarizer refuses any NULL-seq finding and drops the list from the summary. A tiebreak would change 001's output, so it's left to you.
Tests
New gated tests (in
test/prism-gated-tests.txt):offer_lifecycle::integrity_report_026_equals_001_on_clean_mismatch_and_drift_fixtures. 001's report is installed beside 026's (cut from the frozen 001 likevalidator_011()), and both run in one statement, each evaluated once. They must be equal jsonb and text on:work_memand at 64kB, in a transaction.It also counts each set run once per 026 report and twice per 001 report. With both sets made to raise, it checks that 001's report and 026's both fail in the validator.
offer_lifecycle::migration_026_is_recorded_once_and_survives_remigrate. On a v2.0.2 fix: bound candidate replay and audit finalization #258 source with 11 undone, 26 is recorded once. The report's catalog row (xmin, prosrc) survives a migrate that runs 011 and 025 again and one with nothing to do. A record without 26 over 001's report gets 026 once, and a later migrate leaves it. A record with 26 but not 3 is refused before any DDL ("… without migration 3"), and the report's source is still 026's, catalog row untouched.Mutation checks:
8df472ab(drift listing first, count outside the CTE), the equivalence test failed at the new order check, after passing every earlier step. 001's report failed in the validator; 026's failed withqbit_carry_forward_current_drift failed.migration_026_is_recorded_once_and_survives_remigratestill passes, as it must. The record with 26 but not 3 is refused before any DDL, so no migrate reaches 026's guard right after 001 with 26 recorded. Today no test can tell the clause apart.Gates for this round (host rules:
heavy.sh, 4 test threads, fresh database per run):cargo fmt --all --checkcargo clippy --locked --workspace --all-targets -- -D warningscargo test --locked -p qbit-prism-server --no-fail-fast --lib --test offer_lifecycle --test ledger_postgres --test migration_rollback -- --test-threads=4ledger_postgres(the only binary that includesledger_2x.rs, soundo_006) 210 passed;migration_rollback12 passed, 2 ignored;offer_lifecycle26 passedcheck_prism_pool_acquires.py,check_prism_settings.py,check_deleted_test_map.py,check_gate_env_reads.pypython3 -m unittest tests.test_prism_recovery_evidence_parallel tests.test_prism_recovery_evidenceFull suite at
8df472ab(the previous revision of this commit; this round changed 026's evaluation order, migrate's 026 guard, tests and docs, and CI re-runs everything):8df472ab--lib+ 85tests/*.rsbinaries butlive_regtest,--test-threads=4)test/prism-gated-tests.txt.live_regtesttests that check the report (real_two_server_mining_failover_audit_and_reorg,real_ctv_maturity_broadcast_and_confirmation, pinned qbitd 1.0.0)python3 -m unittest tests.test_prism_legacy_import_admission_gatewith a server binary built from the branchRealSchemaTests:PASS carry-forward integrity (mismatch 0, drift 0)on a schema migrated through 026…_parallel,…,…_jobs)Not run locally: the rest of
live_regtest, and the 13#[ignore]database contracts CI's shard runner selects with--ignored(issued_job_dependency,observability_database,compact_runtime_scale, onestratum_admission_postgrestest). None of them reads the report.Timing
PG16,
work_mem4MB, a scratch fixture of 400,000 clean legacy carry rows (2,000 blocks × 200 of 4,000 programs), host load about 6. Three alternating rounds, measured on the8df472abbody; this round's reorder changes no work:The validator alone took 0.74 s and the drift check 0.32 s, so 001 is about two of each and 026 about one. At production size, about 29 s is an estimate from N1's EXPLAIN ANALYZE parts (20.29 s + 9.14 s); no 026 run was timed there.
Docs
docs/prism-rust-migration.md:REQUIRED_SCHEMA_VERSIONS;docs/prism-ledger-ops.md: a 026 note after 025's bullets, covering the estimated time and the same evaluation as 001.Review
A
/code-review highof8df472abfound no correctness bug. Of its hardening and doc findings:Fixed:
migrate_schemanow runs 026 under!versions.contains(&26) || !versions.contains(&3), the clause by which 025's guard knows 011 ran, and records 26 only when it is missing.migrate_schemarefuses a recorded history without 3 before any DDL. I checked with the binary: the migrate exited 1 with "refusing a non-native qbit_prism_schema_migrations before any DDL … without migration 3", and the report's md5 and the record were unchanged. So 001 runs only on a fresh or 2.x.x source, where 26 is missing anyway. The new clause is the explicit 025-style dependency and keeps 026 after 001 if that refusal is ever relaxed.8df472aborder fails the new check withqbit_carry_forward_current_drift failed).docs/prism-rust-migration.md~104 and ~657,docs/prism-ledger-ops.md~1299, 026's header and the commit message.docs/prism-rust-migration.md~576): instead of "migrations 2 through 18", it now requires every native migration the release ships, from 2 on, and points atREQUIRED_SCHEMA_VERSIONS, so it can't go stale.Declined:
OFFSET 0subquery instead of theMATERIALIZEDCTE. The CTE's tuplestore holds one row. It spills only with very many findings, which every caller refuses anyway. The CTE form is what the equivalence test pins.cut_functiontest helper. It's a test-only refactor, not worth the churn now.🤖 Generated with Claude Code