Skip to content

Shorten PRISM payout delivery gate critical sections - #49

Merged
kiwidream merged 8 commits into
1.x.xfrom
shorten-prism-payout-gate
Jul 17, 2026
Merged

kiwidream merged 8 commits into
1.x.xfrom
shorten-prism-payout-gate

Conversation

@kiwidream

@kiwidream kiwidream commented Jul 17, 2026 •

Copy link
Copy Markdown
Member

Summary

  • move payout reconciliation, ledger/RPC work, accepted-block persistence, and candidate construction into a serialized preparation lane outside fleet delivery admission
  • atomically publish immutable payout generation identities, reject stale delivery generations, and prioritize the first current-generation fanout
  • make source supersession explicit and add preparation, publication, generation-relative gate wait, invalidation-to-first-delivery, and discarded-candidate metrics
  • add deterministic direct-block, external-tip, payout-only, admitted-delivery, supersession, priority, stale-generation, and 250-client shared-bundle coverage

Synchronization invariant

Payout ledger mutations and the ledger reads used to construct a signed job snapshot serialize on the preparation lock. Publication then closes admission and drains only deliveries already admitted under generation N before entering the short atomic section that swaps PublishedPayoutState to N+1 and clears the signed-bundle cache. After that swap, the gate rejects generation N and prioritizes the first N+1 current-tip delivery. A bundle can therefore be sent only under the same published generation captured with its payout snapshot; no old-generation job can cross the publication boundary.

Validation

  • python3 -m unittest discover -s tests -p test_*.py (901 tests)
  • Python 3.12 Docker compile plus focused coordinator, cache, tip-refresh, and ledger suites (401 tests)
  • make test-prism-postgres-ledger
  • Docker ShellCheck across repository shell scripts
  • PRISM coordinator Docker image build

No deployment performed.


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.


Note

High Risk
Changes core coordinator concurrency around payout ledger mutations, job stamping, and Stratum delivery ordering; mistakes could ship stale payout work or stall miners under tip churn.

Overview
Refactors how PRISM invalidates and publishes payout state so expensive ledger/RPC work no longer holds fleet job-delivery admission.

Preparation vs publication: Reconciliation, job-bundle ledger reads, and direct-block persistence run under a serialized _payout_state_prepare_lock. A prepared PayoutStateCandidate is published in a short publication() section that drains already-admitted sends, bumps PublishedPayoutState, clears the signed-bundle cache, and updates the gate—without blocking parallel socket I/O during preparation.

Delivery gate: _PayoutStateDeliveryGate now admits deliveries by generation (current / stale / future), can block admission while publication is pending, and prioritizes the first current-tip delivery after a new generation. Tip refresh and direct client sends record gate waits, mark deliveries, and treat pending publication as skipped work (_PayoutStatePublicationBlocked) rather than build failures.

Reorg / tip paths: reconcile_prism_pool_blocks_once prepares mutations outside the gate, fences builds when publication is needed, publishes with a bounded supersession retry budget, and surfaces superseded in trust checks. External tip observation can reserve payout sources without waiting on in-flight preparation.

Observability: New Prometheus histograms for payout preparation, publish, invalidation-to-first-delivery, gate wait by generation, plus a discarded-candidate counter. Tests cover direct-block uncertainty, 250-client shared bundles, priority ordering, and tip churn fences.

Reviewed by Cursor Bugbot for commit 3b8b6c9. Bugbot is set up for automated code reviews on this repo. Configure here.

Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved
@kiwidream
kiwidream force-pushed the shorten-prism-payout-gate branch from 610f97b to 0968de4 Compare July 17, 2026 19:27
Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved
@kiwidream
kiwidream force-pushed the shorten-prism-payout-gate branch from 5449ee3 to c84feeb Compare July 17, 2026 20:15
Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 120a9b4. Configure here.

Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved
@kiwidream
kiwidream merged commit 8e95b72 into 1.x.x Jul 17, 2026
9 checks passed
kiwidream pushed a commit that referenced this pull request Jul 18, 2026
* Skip PRISM payout republish on covered block replays

Since #49, every block-candidate finalization ran the reserve/publish
sequence even when confirm_accepted_block reported nothing new. A
candidate stuck in replay therefore bumped the payout-state generation,
wiped the job-bundle cache, and scheduled an immediate tip-refresh retry
on every block_submit_loop iteration, aborting each in-flight template
refresh ("payout state changed after refresh client selection") and
livelocking job delivery after a found block.

Peel off the fully idempotent replay: when confirmed_count == 0 and no
reserved payout source awaits publication, the published payout state
already covers the candidate, so skip source reservation, publication
blocking, and the post-lock force-publish fallbacks entirely. A replay
whose prior attempt confirmed rows but failed before publishing always
leaves its source reserved (or fenced as direct_block_uncertain), so it
still publishes through the existing path, and a genuinely new
confirmation (confirmed_count == 1) publishes exactly as before.

_advance_payout_state_generation no longer has production callers; keep
it for the tests that use it as the smallest complete invalidation
cycle, with a comment saying so.

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

* Model production confirm semantics in the replay skip test

qbit_confirm_pool_block reports confirmed_count=0 for an already-
confirmed block only once the active tip height moved past it; while the
block is still the tip an idempotent replay reports 1 and must keep
publishing because it is indistinguishable from a freshly flipped row.
Rework the regression test to replay as an active ancestor under an
advanced tip, and document the confirm semantics plus the restart case
at the covered-replay skip.

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

* Keep leaked publication fences healing through covered replays

A finalization attempt can force-block delivery after a superseding
source already published (its own reservation lost the race), then fail
before the republish lands. That leaks a delivery fence whose source
generation matches the published source, so the covered-replay skip
would bail out and nothing would ever clear the fence, blocking every
job build. Require a clear publication fence before skipping, so a
confirmed_count=0 replay of that tail still republishes and unblocks
delivery.

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

---------

Co-authored-by: Claude Fable 5 <[email protected]>
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