Repository navigation
Remove redundant payout-ledger-artifact build from PRISM tip-refresh reconcile - #95
Conversation
In steady state both reconcile decision sites prepared a full payout state candidate -- including the multi-second ledger snapshot artifact (snapshot_at_job_issue over the whole reward window) -- before deciding whether publication was needed, then discarded it. Decide first via the captured source generation, prepare only on the publishing branch. Also coalesce concurrent same-tip reconcile callers behind one in-flight pass, and scope the trusted-reconcile memo per tip so one superseded pass no longer disarms the cache for every tip. Co-Authored-By: Claude Fable 5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: caeb554dca
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
A tip flip A->B->A inside the cache TTL could reuse tip A's pre-flip trusted outcome and skip the pass that re-proves pool-block chain state on the restored chain. Detection now ends the epoch of every other tip's memo entry (observation-sequenced, so stale observations cannot evict), and a pass finishing for a no-longer-current tip refuses to re-arm its evicted entry. Co-Authored-By: Claude Fable 5 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit aa8bbac. Configure here.
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Per-client callers reconcile straight off getbestblockhash, so a pass for a tip that observe_tip_for_refresh never saw can still mark pool blocks inactive/reactivated/matured. Cached trusted proofs for other tips were taken against pre-mutation rows; drop them whenever a pass applies row mutations, closing the unobserved A->B->A flip-back reuse. Passes that mutate nothing leave other entries armed: reconciliation is a pure function of ledger rows and chain state, and both are provably unchanged in that case. Co-Authored-By: Claude Fable 5 <[email protected]>
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |

Summary
Mainnet tip-refresh latency (measured 2026-07-26, after the reorg-convoy relief in qbit-tools#636 landed) is dominated by a redundant payout-ledger-artifact build:
tip_refresh_bundle_phase_seconds{phase="ledger_snapshot"}= 734s over 283 calls ≈ 2.6s per call, running ~2× per refresh — ~5s of serial cost before any miner can be handed work. Only 14 of 140 tip refreshes (10%) delivered work to any miner; refresh start → first miner served was 15.0s. Fanout is not the problem (last_delivery − first_delivery ≈ 1.1s for ~1,400 clients).Root cause:
_prepared_payout_state_candidateunconditionally called_build_payout_ledger_artifact(accepted_share_stats twice,snapshot_at_job_issue's recursive-pages CTE,current_prior_balances,to_prism_jsonover every share in the reward window) at both reconcile decision sites — before the publish decision. In steady statepayout_changedis False,candidate_to_publishstays None, and the entire multi-second artifact is discarded.Changes
_captured_payout_source_requires_publication(captured_source)— the exact comparison_payout_source_requires_publication(candidate)would have made, sincecandidate.source_generationiscaptured[1]. The candidate (and its embedded ledger snapshot) is materialized only on the branch that actually publishes.reconcile_prism_pool_blocks_once(tip_hash=...)callers arriving while a pass for the same tip is in flight await that pass's summary instead of queueing another full serialized pass behind_payout_balance_mutation_lock. Callers carrying side-effect obligations (_force_publish,_source_reserved) and tipless callers always run their own pass. Followers wait with a liveness backstop (30s default) and then fall back to their own serialized pass — worst case degrades to exactly the old behavior.ensure_reorg_reconciled_for_current_tipis now keyed by tip hash. Previously a single superseded pass (finish(trusted=False)) disarmed the single-slot memo globally, forcing every per-client job build into a redundant full pass until some later pass completed trusted. Now an untrusted outcome unarms only its own tip; a reconcile error still clears the entire memo (a partially applied ledger mutation invalidates every cached outcome); the liveqbit_chain_view_untrusted()check on every cache hit is unchanged, so job issuance still pauses immediately when headers run ahead. Entries are bound to a current-tip epoch (per review findings, so an A→B→A flip-back within the TTL can never reuse a pre-flip proof): detecting a newer tip evicts every other tip's entry (observation-sequenced, so stale observations cannot evict), a pass finishing for a no-longer-current tip refuses to re-arm, and any pass that applies orphan/maturity row mutations evicts every other tip's entry even when its tip was never observed. Passes that mutate nothing leave other entries armed — reconciliation is a pure function of (ledger rows, chain state), both provably unchanged in that residual case.Explicitly unchanged
reorg_watch_blocks→mark_pool_block_inactive/reactivate_pool_block/mark_mature_pool_payouts) runs exactly as before on every pass.payout_changed(and_force_publish) still force publication._build_payout_ledger_artifactalready returns None on any generation/share-count race); publication still falls back to_prepare_payout_state_artifactand schedules background artifact preparation when no artifact is carried. No payout-correctness exposure — every removed build was on a path that discarded its result.Tests
test_ledger_artifact_not_built_when_publication_not_required— steady-state pass still runs watch/mature but builds no artifact.test_payout_change_forces_publication_and_builds_artifact— an orphaned confirmed block with no source movement still bumps the payout generation, builds the artifact, and flips the row to inactive.test_concurrent_same_tip_reconciles_share_one_pass— deterministic leader/follower orchestration: one ledger pass, equal (copied) summaries, flight registry drained.test_forced_and_reserved_reconciles_bypass_flight_reuse— flagged callers never reuse an in-flight summary.test_untrusted_outcome_for_other_tip_keeps_memo_armed/test_reconcile_error_outcome_clears_every_memo_entry— memo scoping and error clearing.test_tip_flip_back_within_ttl_requires_fresh_reconcile/test_stale_pass_cannot_rearm_memo_after_newer_tip_detected/test_row_mutating_pass_evicts_other_tip_memo_entries— epoch eviction on detection, stale re-arm refusal, and mutation-driven eviction for unobserved tips.Success metric
Under 20–60s tip churn with 1,000+ simulated clients:
tip_refreshfirst_delivery p50 well under 5s (from 15.0s measured) and refresh→delivery ratio materially above 10% (from 14/140), with coverage no longer oscillating 0.0↔0.9 on artifact-build stalls.🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.Note
Medium Risk
Touches payout reconciliation, publication gating, and job-build admission around ledger mutations; behavior is heavily tested but errors in memo/flight logic could delay mining work or force extra reconciles.
Overview
Cuts tip-refresh latency by avoiding expensive payout ledger artifact builds on reconcile paths that never publish, and by not running duplicate full reconcile passes for the same tip under concurrency.
Reconcile publish decisions now use
_captured_payout_source_requires_publicationon the captured source before_prepared_payout_state_candidateruns, so steady-state passes that only watch/mature ledger rows skip the multi-second artifact work unlesspayout_changed,_force_publish, or publication need applies.reconcile_prism_pool_blocks_oncecoalesces unflagged same-tip callers behind one in-flight_ReconcileFlight(followers get a copied summary; 30s wait then own serialized pass)._force_publish,_source_reserved, and tipless callers bypass reuse. The serialized body moves to_reconcile_prism_pool_blocks_serialized.Trusted reconcile caching is per tip via
_reorg_reconcile_trusted_memoand_note_reorg_reconcile_outcome: untrusted outcomes unarm only that tip; row mutations evict other tips; errors clear the whole memo; new tip detection evicts stale tips; stale passes cannot re-arm after a newer tip is observed.ensure_reorg_reconciled_for_current_tipstill re-checksqbit_chain_view_untrusted()on cache hits.Reviewed by Cursor Bugbot for commit ffe1a7a. Bugbot is set up for automated code reviews on this repo. Configure here.