Skip to content

3.x.x: capture a found block whose payout revision moves before its share's commit, instead of losing it to the append's fence (#657) - #674

Merged
RobertClarke merged 4 commits into
3.x.xfrom
RobertClarke/657-fence-capture
Oct 5, 2026
Merged

RobertClarke merged 4 commits into
3.x.xfrom
RobertClarke/657-fence-capture

Conversation

@RobertClarke

@RobertClarke RobertClarke commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

Closes #657. Refs #478, #504, #574, #632, #656.

The bug

submit_share admits a block-bearing share on the credit path at payout revision R, and the candidate rides in the share's append. append_prepared re-reads the revision under ORDER_LOCK. If a same-parent settlement committed in between, it bailed with payout revision changed before share commit before append_in and before the candidate insert. The transaction rolled back with the candidate inside it, and the miner got ledger-confirmation-failed. Nothing enqueued the block, so a valid found block was never offered.

#504 (#478) captures a block found on same-parent superseded work, but only when the supersession is already visible at the submit check (capture_stale).

The fix: capture inside the same transaction

I chose the in-attempt option over handing the candidate back to submit_share. It is the simpler of the two that is provably atomic, for two reasons:

  • One transaction decides and writes. The capture-or-credit decision and its write happen under the same ORDER_LOCK and the same FOR SHARE read, so an attempt commits either the credited share with its candidate or the capture, never both.
  • The capture does not depend on anyone still waiting. A block-bearing append is never refused at the acknowledgement deadline: it runs on and is followed. A fallback in submit_share would only run if the append finished in time. Here the capture is the append's own work, so an append that finishes after its answer still captures its block.

The changes:

  • Preparation (ledger/candidates.rs). When the append may capture, its candidate is prepared in two forms before the transaction opens. One is the credited form. The other is its capture: the same document with the solver share as deferred_share, its digest, and the deferred-share row. The block and the as-issued balances are #[serde(skip)] fields, so the capture costs one more O(1) serialization, and the transaction still inserts only prepared bytes. persist_prepared_candidate and persist_prepared_capture both write through persist_candidate_row, behind the same writer fence, as-issued snapshot and 3.x.x: after an async promotion, a block on a job issued in the replication gap is offered and lands on chain but can never be landed in the pool (window range incomplete) #619 window probe.
  • Fence (ledger/window.rs). The caller says what a moved revision means for a block, MovedRevision::{Capture, Refuse}:
    • Capture on, block-bearing share: append_prepared writes the capture (outbox row share_id NULL, plus the deferred share), runs the same commit-gate tail as a credited append, commits, and logs a WARN.
    • Share already recorded: the capture first probes for it, with the probe persist_block_only uses. A proof already credited with its candidate, then resubmitted as the revision moves, is answered duplicate-share and writes nothing. Without the probe it would fail on candidate identity conflict and be answered ledger-confirmation-failed.
    • Identical capture already enqueued: reported as a duplicate, the way the block-only path reports an existing outbox row.
    • Capture off, or no block: refused before any write with a typed error, PayoutRevisionChanged { expected, observed }, and still answered ledger-confirmation-failed.
    • AppendResult.captured reports the capture.
  • Coordinator (coordinator/submit_ledger.rs, coordinator/miner_submit.rs).
    • persist_share_pass passes Capture when PRISM_CAPTURE_OVERPAY_CEILING_BPS > 0, otherwise Refuse, the same switch the submit check's block_capture uses.
    • SubmitLedger now returns Appended::{Inserted, Recorded, Captured}.
    • A capture is answered the way a block-only proof is. The share waits up to share_commit_timeout for the block's disposition (deferred_share_disposition, extracted from persist_block_only), then is answered:
      • accepted, once confirmation credits the deferred share;
      • stale-job, if the capture is abandoned;
      • unknown, at the bound.
    • The sync-replication guard (sync_rep_unknown) applies to a captured COMMIT as it does to a credited one. The follow logger reports outcome="captured".
  • Ceiling. The capture is held to PRISM_CAPTURE_OVERPAY_CEILING_BPS at its offer by the existing reservation, exactly as a capture at the submit check is (offered/abandoned). It lands as issued, and its share is credited once on confirmation. With the ceiling at 0, capture is off: the fence refuses as before 3.x.x: a block found on the current tip is dropped when the payout revision moves between its submit check and its append's revision fence (#478 capture gap) #657, as the submit check refuses stale-job as before 3.x.x: An own block found 9 s after the previous landing was refused unknown-job and never reached the node #478.

Invariants and their tests

  • Same rows as a capture at the submit check. ledger::candidates::capture_form_tests (no database) pins that the fence's capture form is byte-identical to preparing the candidate with its share deferred, which is what capture_stale enqueues: same document, digest, deferred row and snapshot. Preparing it leaves the credited form unchanged.

  • One offer, one credit, audit and reconciliation intact (PostgreSQL, real Coordinator, scripted node). New tests in coordinator::commit_reconcile_tests. The race is driven deterministically: the test session holds ORDER_LOCK, waits until the submit's append queues behind it, then bumps the revision and releases the lock. That is a settlement committing behind a share already past its submit check.

    • …block_whose_revision_moves_before_its_commit_is_captured_and_lands_once. The outbox row is a capture: no crediting share, the share deferred, the block at its issued revision. The share is not credited before confirmation, and is not answered before its block's disposition. After the drive:

      • outcome submitted, offer decision offered with both revisions;
      • exactly 1 submitblock, exactly 1 ledger row;
      • pool block confirmed with 1 audit bundle;
      • carry-forward integrity report clean, accepted shares equal to credited headers;
      • no second candidate.

      Resubmitting the same proof credits and offers nothing more.

    • …credited_block_resubmitted_as_its_revision_moves_is_a_duplicate. A proof credited with its candidate, resubmitted into a moving revision:

      • answered duplicate-share;
      • the credited outbox row unchanged and nothing deferred;
      • then lands once and is credited once, integrity clean.

      With the probe disabled, this test fails on candidate identity conflict.

    • …block_append_queued_past_its_deadline_still_captures_its_block. Answered ledger-outcome-unknown at its deadline while still queued. When the lock frees, it still captures, lands once and credits once.

    • …block_whose_revision_moves_with_capture_off_is_refused_as_before. With ceiling 0:

      • answered ledger-confirmation-failed, with the typed reason logged;
      • nothing enqueued, offered or credited, and no INSERT reached.
    • …plain_share_whose_revision_moves_before_its_commit_writes_nothing. Answered ledger-confirmation-failed with the typed reason logged, no row, and no INSERT reached.

  • Coordinator unit tests (no database). coordinator::miner_tests::fence_capture covers:

    MemoryLedger now models the fence the way production does.

Evidence

Setup: local PostgreSQL 16.15, debug build.

  • The new database tests against the base product code (bd1ddeb, with only the test file applied, before review added the capture-off and duplicate tests):

    • the capture tests fail: no candidate ever appears, so the block is dropped;
    • the plain-share test fails only on the new typed message.
  • The same tests on this branch: all pass, as do all 21 commit_reconcile_tests.

  • Unit and equivalence tests: 5 of 5 pass.

  • The rest, before merging the latest 3.x.x: all pass:

    • the full qbit-prism-server lib suite against PostgreSQL: 652 passed, 2 ignored;
    • 17 integration binaries over the append, candidate and offer paths: append_admission 25, b478_capture_overpay 29, b478_payout_integrity 21, b478_stale_revision_block 20, b574_ack_cap 22, b602_order_lock_hold 22, b619_gap_work_after_promotion 6, candidate_storm 20, cluster_row_fences 1, ledger_cancelled_begin 18, ledger_deadline_atomicity 7, ledger_postgres 194, ledger_single_execution 22, ledger_wait_metrics 3, lost_race_pin 8, offer_lifecycle 22, stratum_admission_postgres 17.
  • After merging 3.x.x (07da6a7): all pass again:

    • the lib suite: 666 passed, 2 ignored;
    • append_admission 25, b478_capture_overpay 29, b478_payout_integrity 21, b478_stale_revision_block 20, b574_ack_cap 22, ctv_fanout_bookkeeping 24, ledger_postgres 199, ledger_single_execution 22, offer_lifecycle 22, stratum_admission_postgres 17.

    The lib suite ran on a fresh database. The reused local one had been migrated to 021 by the pre-merge runs and still held a running test instance, so 3.x.x: time CTV fanout claim leases on the taker's monotonic clock, and label session timeouts and hung refreshes by the dependency they wait on (#654, #655) #667's migration 022 refused it.

  • Python: scripts/check_prism_pool_acquires.py, tests.test_check_prism_pool_acquires and scripts/generate_prism_metrics.py --check pass. Locally, shard 2's tests all pass; four parity citations were skipped here for lack of docker and credential images.

  • Lint: cargo fmt --all --check and cargo clippy --workspace --all-targets -- -D warnings are clean.

Code review (/code-review high) and what changed

  • Fixed:
    • The capture skipped the duplicate probe → probe added (above).
    • The fence captured with capture off → MovedRevision.
    • The capture path had its own commit tail → one shared tail.
    • Dead code: the no-block arm, and the gate-state check before the sync-rep guard.
    • The disposition poll's retry log hard-coded block-only → it now names its path.
  • Kept deliberately:
    • No parent check at the fence. The offer probes the node and abandons a superseded parent, as for any capture. A fence check against a possibly lagging tip could drop a valid block.
    • A capture that commits in grace answers at the bound already passed. It is answered unknown, as 3.x.x: cap a captured block proof's acknowledgement wait at the share-commit timeout (follow-up to #562) #574 answers a block-only proof.
    • An identical capture already present answers duplicate-share. Same as persist_block_only.
    • The capture form is prepared before the lock. It is prepared only when capture is on and the append is fenced. The cost is O(document) per found block, kept off ORDER_LOCK.

Also

🤖 Generated with Claude Code

…hare's commit, instead of losing it to the append's fence (#657)

A block-bearing share was admitted on the credit path at payout revision
R, and its candidate rode in the share's append. append_prepared re-read
the revision under ORDER_LOCK. If a same-parent settlement had committed
in between, it bailed with "payout revision changed before share commit"
before append_in and the candidate insert. The transaction rolled back
with the candidate in it, the miner got ledger-confirmation-failed, and
nothing enqueued the block: the found block was never offered. #504
(#478) captures a block found on same-parent superseded work only when
the supersession is already visible at the submit check.

The fence now captures the block instead, in the same transaction. A
fenced append's candidate is prepared in two forms before the
transaction opens: the credited one, and its capture. The capture is the
same document with the solver share as deferred_share, its digest, and
the deferred share row. The block and the as-issued balances are skipped
fields, so the capture costs one more O(1) serialization, and the
transaction still inserts only prepared bytes.

When the revision moved, append_prepared writes the capture and commits
it. It does so under the same ORDER_LOCK and FOR SHARE read, through the
same writer fence, as-issued snapshot and #619 window probe as any
enqueue, and never also appends the share. A share without a block keeps
the refusal, now the typed PayoutRevisionChanged, still answered
ledger-confirmation-failed. AppendResult.captured reports the capture,
and SubmitLedger returns Appended::{Inserted, Recorded, Captured}.

The share path answers a capture the way it answers a block-only proof.
It waits up to share_commit_timeout for the block's disposition, using
deferred_share_disposition, extracted unchanged from persist_block_only.
The share is accepted once confirmation credits the deferred share,
answered stale-job if the capture is abandoned, and answered unknown at
the bound. The capture is the append's own work, so an append still
running past its acknowledgement deadline captures its block in the
background.

The capture is byte for byte the row capture_stale enqueues, and a test
pins that. So its offer is held to PRISM_CAPTURE_OVERPAY_CEILING_BPS
(offered, abandoned or disabled), it lands as issued, and its share is
credited once on confirmation.

Tests:
- coordinator::commit_reconcile_tests, PostgreSQL. The test holds
  ORDER_LOCK, bumps the revision behind a queued append and releases the
  lock. Four cases:
  - a block is captured, offered once, landed with its audit bundle and
    credited once; integrity is clean, accepted shares equal credited
    headers, and a resubmission adds nothing;
  - an append queued past its deadline is answered unknown and still
    captures;
  - with capture off the block is abandoned with the disabled decision
    and never offered;
  - a plain share writes nothing.
  Against the base product code the three block tests fail with no
  candidate ever enqueued.
- ledger::candidates::capture_form_tests: the capture form equals
  capture_stale's preparation.
- coordinator::miner_tests::fence_capture: #657's MemoryLedger
  reproduction, now captured; MemoryLedger models the fence as production
  does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-05T20:55:29.803613Z 93d7223 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

…ce-capture

# Conflicts:
#	docs/prism-native-metrics.md
@blacksmith-sh

This comment has been minimized.

RobertClarke and others added 2 commits October 5, 2026 16:43
…e fence, and capture off refuses as before

- The fence's capture probes for the share first, with the probe
  persist_block_only uses. A proof already credited with its candidate,
  resubmitted as the revision moves, is answered duplicate-share and
  writes nothing, instead of failing on "candidate identity conflict".
- With PRISM_CAPTURE_OVERPAY_CEILING_BPS=0 the fence refuses a
  block-bearing share as before (MovedRevision::Refuse), as the submit
  check refuses one whose revision already moved. No capture form is
  prepared when capture is off.
- The capture and the credited append share one commit-gate tail.
- persist_share_pass takes the block hash from submit_share, which
  removes the unreachable no-block arm. The sync-rep guard no longer
  checks the gate state first.
- The disposition poll's retry log names its path.
- scripts/prism_pool_acquires.json: the acquire that moved into
  deferred_share_disposition is classified there, which fixes CI Python
  shard 2.
- Tests: the capture-off refusal (unit and PostgreSQL), and a credited
  block resubmitted into a moving revision is a duplicate (PostgreSQL,
  gated). The capture-off docs are updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@RobertClarke

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 93d7223ef6

ℹ️ 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".

@RobertClarke
RobertClarke merged commit a77b5c4 into 3.x.x Oct 5, 2026
34 checks passed
@RobertClarke
RobertClarke deleted the RobertClarke/657-fence-capture branch October 5, 2026 21:17
RobertClarke added a commit that referenced this pull request Oct 5, 2026
Brings in #674 and #680; neither adds a migration, so 023 keeps its number.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RobertClarke added a commit that referenced this pull request Oct 6, 2026
… not ledger-confirmation-failed (#675) (#698)

A share whose payout revision moves between its submit check and the
append's read under ORDER_LOCK is refused by the append's fence before
any write. Since #674 the refusal is a typed PayoutRevisionChanged, but
it still reached the generic Failed arm and was answered
ledger-confirmation-failed ("share was not confirmed by the database"),
with a "share persistence failed" WARN. Its work was superseded, which
the submit check itself answers stale-job a moment later; the ledger is
healthy.

persist_share_pass now maps that refusal to Superseded(PayoutRevision),
beside the existing stale-authority gate mapping. The miner is answered
stale-job (code 21, "stale job"), still never credited, and the
payout_revision stale-job cause is counted once. A plain share's refusal
is counted, not logged, like the submit check's. With capture off, a
block-bearing share is refused the same way, and its found block is
never offered, so a WARN names the lost block.

The refusal no longer feeds PrismShareAppendFailures, which fired around
every revision bump at production share rates. The load harness classes
these refusals as expected landing races instead of backend refusals.
The tests that pinned the old answer now pin stale-job, the cause count,
and the absence of a persistence WARN. The rejection, metric and
PayoutRevisionChanged docs say where the refusal is counted.

Co-Authored-By: Claude Opus 5.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