Skip to content

Reduce PRISM accepted-block payout gate locking - #50

Merged
kiwidream merged 16 commits into
1.x.xfrom
reduce-payout-gate-locking
Jul 18, 2026
Merged

kiwidream merged 16 commits into
1.x.xfrom
reduce-payout-gate-locking

Conversation

@RobertClarke

@RobertClarke RobertClarke commented Jul 17, 2026 •

Copy link
Copy Markdown
Member

Summary

  • publish each accepted block's prospective carry-forward balances from its immutable issued job bundle at a short payout-delivery boundary
  • let child and descendant jobs consume that prospective state while audit rebuilding, verification, filesystem writes, and database persistence continue outside the delivery gate
  • serialize durable balance mutation and accepted-block finalization, including startup replay, retry ordering, terminal prepared-state cleanup, and active-chain revalidation
  • move job-issue share snapshots to the independent Postgres read path and verify durable balances as of the confirmed pool block
  • add concurrency and replay coverage for publication, invalidation, descendant delivery, retry priority, reorg handling, and durable equivalence

Why

Accepted-block finalization performs canonicalization, deep copies, audit-body writes, verification, and a bulk database transaction. Holding _payout_state_delivery_gate across that work blocks replacement jobs and correlates with memory spikes, refresh cancellations, and stale-share bursts.

Simply releasing the gate during persistence is insufficient: an H+1 job built from pre-confirmation database balances can be delivered and solved, then rejected as stale after H confirms. The coordinator now publishes the carry state already committed by H's immutable issued bundle before the expensive finalization work begins. H+1 work therefore uses the state that durable confirmation is required to produce.

Correctness model

  • _payout_balance_mutation_lock serializes accepted-block and reorg balance transitions without excluding ordinary job delivery.
  • Prospective publication briefly owns the delivery gate, advances the payout generation exactly once, and installs an immutable balance snapshot.
  • Exact children and later active-chain descendants wait for or consume the highest relevant prospective snapshot; waits are bounded and retried.
  • The rebuilt and verified final bundle must reproduce the issued snapshot, and the confirmed ledger view as of that block must match it before the override is removed.
  • Rejections, reorgs, and verification failures withdraw published state and invalidate its generation; successful durability catch-up does not cause a second invalidation.

Impact

Replacement jobs can reach miners throughout accepted-block audit and persistence work without being based on stale payout state. Reconciliation remains excluded from the landing-to-confirmation interval, startup replay preserves parent-before-child ordering, and Postgres job snapshots no longer queue behind the fenced writer connection.

Validation

  • python3 -m unittest discover -s tests -p 'test_*.py' — 939 tests passed
  • python3 -m unittest tests.test_prism_coordinator_job_cache tests.test_prism_coordinator_vardiff tests.test_prism_share_ledger — 386 tests passed
  • parent-persistence and parent-final-bundle concurrency regressions passed in 25 repeated runs
  • python3 -m py_compile on the changed coordinator, ledger, and test modules
  • git diff --check

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


Note

High Risk
Large changes to accepted-block payout publication, concurrency, and ledger confirmation invariants—errors could deliver wrong carry-forward state or race reconciliation.

Overview
Accepted blocks no longer hold the payout delivery gate across audit rebuild, verification, and ledger persistence. The coordinator publishes prospective carry-forward balances from the issued job’s payout_policy_manifest at a short boundary (new payout generation + compact preview), so child/descendant jobs can build on the post-accept state while slow finalization continues.

Coordinator behavior: _payout_balance_mutation_lock serializes durable balance work without blocking ordinary job delivery; landed transitions block reconciliation; children wait (bounded) for parent previews or fail closed on withdrawal/reorg tombstones. Block finalization moves into _land_and_confirm_block_candidate with early preview from cached prospective_prior_balances, verified-bundle checks, and prior_balances_after_pool_block equivalence before clearing the override. Share snapshots clamp anchor_job_issued_at_ms below in-flight commits via _pending_share_commit_floor; candidate retry keeps the lowest-height parent first and avoids double-counting accepted hashes.

Supporting changes: job-summary audit CLI output adds payout_policy_manifest; Postgres job snapshots use the read pool; ledger gains pool_block_state and as-of-block balance queries. Tests cover preview wait, idempotent publication, artifact patching, and anchor floors.

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

@RobertClarke
RobertClarke marked this pull request as ready for review July 17, 2026 19:12
Comment thread lab/prism/prism_coordinator.py Resolved
@RobertClarke

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ac87aa5b22

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

Comment thread lab/prism/prism_coordinator.py Resolved
@RobertClarke

Copy link
Copy Markdown
Member Author

@codex review

Comment thread lab/prism/prism_coordinator.py Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: bb445e2a79

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

Copy link
Copy Markdown
Member Author

@codex review

Comment thread lab/prism/prism_coordinator.py Resolved
Comment thread lab/prism/prism_coordinator.py Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: e372316ce8

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

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 44f9228b13

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

…ocking

# Conflicts:
#	lab/prism/prism_coordinator.py
#	tests/test_prism_coordinator_vardiff.py
…ocking

# Conflicts:
#	lab/prism/prism_coordinator.py
#	tests/test_prism_coordinator_job_cache.py
…ocking

# Conflicts:
#	crates/qbit-prism/src/bin/qbit-prism-build-audit-bundle.rs
#	lab/prism/prism_coordinator.py
…ocking

# Conflicts:
#	lab/prism/prism_coordinator.py
@RobertClarke

Copy link
Copy Markdown
Member Author

@codex review

Comment thread lab/prism/prism_coordinator.py Resolved
@RobertClarke

Copy link
Copy Markdown
Member Author

@codex review

@RobertClarke

Copy link
Copy Markdown
Member Author

@codex review

@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 1 potential issue.

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 19d6700. Configure here.

Comment thread lab/prism/prism_coordinator.py Resolved

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 19d6700b7c

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

Comment thread lab/prism/share_ledger.py Resolved
Codex review: the independent-pool job snapshot no longer orders after
in-flight share appends, so a stamped share committing after the MVCC
read with accepted_at <= anchor would be durable yet missing from the
issued window, breaking qbit_audit_share_window reproducibility at the
declared anchor. Track every stamped-but-undurable share (group-commit
queue, in-flight batch, block-candidate credit linked after landing)
and clamp job and payout-artifact snapshot anchors strictly below the
oldest one, releasing entries on commit, miner-facing rejection, or
terminal candidate abandonment. Payout artifacts now record their own
snapshot anchor and bundles built from an artifact declare it, since a
fresher job-issue anchor could cover shares the artifact predates.

Cursor review: once persist and confirm are durable, a lost forced
payout publication no longer aborts the candidate into outbox retry
churn. The candidate finalizes (outbox submitted, evidence and counts
recorded) while delivery stays fenced until the scheduled tip refresh
publishes the newest source.

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

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

…ocking

Reconciles the bounded build-supersession machinery (#57) with this
branch's prospective payout publication and snapshot anchor floor:

- job snapshot anchors clamp below pending share commits at the frozen
  per-generation issued time in _new_job_build_request, which also makes
  the frozen anchor's window byte-reproducible across rebuilds
- bundles built from a reused payout ledger artifact declare the
  artifact's own snapshot anchor
- child builds wait for a pending accepted-parent preview before
  acquiring any confirmed payout input, so no stale confirmed read is
  taken or cached into the published payout state artifact while the
  parent lands
- an accepted-block preview publication installs its preview balances as
  the published payout state artifact instead of reading the ledger,
  whose durable state only catches up after persistence
- accepted blocks cancel obsolete job builds before preview publication,
  preserving the #57 refresh handoff

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

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@kiwidream
kiwidream merged commit 4b981d5 into 1.x.x Jul 18, 2026
9 checks passed
RobertClarke added a commit that referenced this pull request Jul 29, 2026
…tion share rates (#102)

* Switch PRISM payout-ledger-artifact validity to anchor-scoped semantics

Production telemetry (2026-07-29) showed zero artifacts ever publish under
~35 accepted shares/s: the background build aborted at its pending-commit
anchor clamp or its accepted_before != accepted_after fence, and the
exact-count reuse fence rejected any artifact the moment one more share
became durable. The #99 spool + serve daemon sat idle while every
tip-refresh build kept paying ~3.5s ledger_snapshot + ~1.2s
serialization_copy.

An artifact's window is already exact at its own clean snapshot anchor:
_job_snapshot_anchor_ms keeps the anchor strictly below every pending
commit, so every covered share is durable and shares stamped above the
anchor deterministically belong to the next window (the reproducibility
contract documented at PayoutLedgerArtifact.snapshot_anchor_ms, which
reused bundles declare as anchor_job_issued_at_ms). Operationalize that
contract instead of fencing on global counts:

- Background build: the pending-commit clamp is anchor SELECTION (build at
  the clamped floor) rather than an abort; the before/after count fence is
  gone. The only abort left is a floor so old the artifact would arm past
  the staleness bound (wedged writer), which keeps the re-arm backoff
  meaningful. accepted_share_count is recorded as diagnostics only.
- _usable_payout_ledger_artifact: exact-count comparison replaced by a
  bounded anchor-staleness check (PRISM_PAYOUT_ARTIFACT_MAX_ANCHOR_AGE_SECONDS,
  default 10s) alongside the unchanged generation/difficulty checks and the
  untouched prior_balances sha256 fence. In-flight builds re-validate with
  ignore_anchor_age so queue delay past the bound cannot scrap a reuse that
  was fresh at selection into the full snapshot it was armed to avoid.
- Synchronous seeding: the anchor-count bracket capture and mid-read count
  fences are gone; ready builds always seed their anchored window, with a
  single informational count read.
- _install_payout_ledger_artifact orders snapshots by anchor (counts are
  unfenced reads now); a same-window rebuild at a fresher anchor keeps the
  armed generation but advances the staleness clock in place so quiet
  streams do not cycle identical rebuilds forever.

Tests updated from count-fence to anchor semantics, plus new coverage:
clamped-floor builds publish, pathological floors refuse before the window
walk, reuse survives commits landing after the anchor, the staleness bound
retires and re-arms, and a reused-anchor bundle produces byte-identical
payout-preview output to a fresh build at the same anchor driven through
the #50 issued-vs-verified preview guard (with a divergent-anchor negative
proving the guard discriminates). Metric names are unchanged.

Full unittest suite: failure set byte-identical to base 59a3d9d in this
environment (20 pre-existing sandbox failures, zero regressions).
cargo test -p qbit-prism: 182 passed.

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

* Harden anchor selection and reuse identity per PR #102 review

Codex P1 + Bugbot (clamp-to-snapshot race): a share stamped right after
_job_snapshot_anchor_ms returns can land in the same millisecond as the
clamp instant, unprotected by the pending floor; an anchor equal to that
instant would cover it on replay (the window predicate is anchor-inclusive)
while the snapshot never contained it. Select anchors strictly below the
clamp-time millisecond -- min(now - 1, floor - 1) -- so every stamp
assigned after selection sits above the anchor on both sides of the
replay. This also hardens the job-issue anchor freeze, which shared the
same tie under the old count fences.

Bugbot (in-place advance breaks reuse identity): the same-window anchor
refresh swaps the armed instance, so the in-build re-validation now
compares artifact identity fields (payout generation, artifact generation
-- the re-key authority -- and window sha) instead of object identity;
an in-flight build holding the pre-refresh instance completes its reuse
instead of being scrapped into the full snapshot it was armed to avoid.

Bugbot (retarget guard misses fresher anchors): the live-difficulty
retention guard now applies whether the delayed pre-retarget build's
anchor ties or leads; a wrong-difficulty install would fail every reuse
probe on the difficulty check, which never re-arms.

Tests: anchor selection strictly below the clamp instant (bare and
floor-clamped); in-flight build survives a same-window anchor refresh;
fresher-anchor pre-retarget install refused; vardiff clamp expectations
updated for the strict-below-now bound.

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

* Enforce the anchor-age bound on cached ready bundles

Codex round-2 P2: the staleness bound retired only the armed artifact.
A cached ready bundle could keep serving past the bound whenever the
bundle cache TTL outlives it, and a same-window re-anchor keeps the
artifact generation while older-anchored bundles sit cache-usable, so
issued jobs could keep declaring a window anchor older than
PRISM_PAYOUT_ARTIFACT_MAX_ANCHOR_AGE_SECONDS.

_job_bundle_entry_usable -- the single re-validation choke point for
cache probes, subscribed results, and lookups -- now rejects a ready
bundle whose declared anchor_job_issued_at_ms has aged past the bound;
collection bundles carry no payout window and are unaffected. With the
default TTL equal to the default bound this is behavior-neutral; it
closes the operator-mismatch corner and makes the invariant total: no
served job declares an anchor older than the configured limit.

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.

2 participants