Skip to content

3.x.x: Candidate list and abandon operator commands, and the legacy-drain runbook step - #425

Closed
kiwidream wants to merge 13 commits into
3.x.xfrom
candidate-list-and-abandon-commands
Closed

kiwidream wants to merge 13 commits into
3.x.xfrom
candidate-list-and-abandon-commands

Conversation

@kiwidream

@kiwidream kiwidream commented Sep 16, 2026 •

Copy link
Copy Markdown
Member

Closes #268.

Adds qbit-prism-server candidates list and candidates abandon — the two
operator commands the native server was missing — plus the cutover runbook step
that separates the legacy drain from the native one, and the #259 test-case
mapping.

Per #268's 2026-09-10 scope update this is the trimmed slice: list, abandon
and the runbook step only
. The "Proposed change" section's
candidates recover --block-hash ... [--apply] predates that update and is not
built here; it is filed as #418 (P2, 3.x.x: GA), and every plan-level link in
the deleted-test map now points there.

@Anatolie — crates/qbit-prism-server/src/tools.rs and docs/ are yours. This
adds one Candidates subcommand group, its CandidatesCommand enum and one
handler function; no other subcommand's code, help text or error path is touched,
and the only changed existing line is the bail import. Flagging it because #290's
fatal-state shares the file.

Why abandon is pending-only

The one outcome this must never produce is a submitblock that happened and a
ledger that no longer records it.

Since migration 011 (#266), pending is the only unfinished state from which no
submitblock can yet have been made — ledger/candidates.rs calls it "the only
state that may still be abandoned", ledger/blocks.rs and
migrations/011_offer_before_landing.sql say the same. So the safety property is
the statement's WHERE, not a check in the CLI, because a row can move between a
check and a write:

WHERE o.block_hash=$1
  AND o.state='pending'
  AND (o.claim_expires_at IS NULL OR o.claim_expires_at<=clock_timestamp())
  AND NOT EXISTS (SELECT 1 FROM qbit_pool_blocks b WHERE b.block_hash=o.block_hash)

The SET list is ledger/policy_transition.rs's supersession abandon verbatim,
with one substitution: the operator's --reason takes last_error's place.
next_attempt_at is deliberately untouched there and here, so an infinity left
in it stays as the evidence that the row had been parked.

The qbit_pool_blocks predicate mirrors the refusal policy_transition.rs
already makes — "never alter existing accounting to discard it" — and is the one
remaining way an operator could discard landed accounting.

When the statement changes no row, a read-only follow-up SELECT in the same
transaction classifies why, for the message and exit status alone. It gates no
write; the refusal is already decided. Ordering it the other way round is the
race this design exists to prevent.

Exit statuses

Code Outcome
0 abandoned
1 configuration or database failure
2 no such row
3 wrong state — offered to the node, never abandoned
4 already terminal
5 held by a live claim
6 pending, but its block is already in qbit_pool_blocks

3 and 6 are the two that protect the invariant. 4 is separate from 2 so re-running
a successful abandon reads as "nothing to do" rather than a lost row.

list

Opens a read-only pool in the shape Ledger::inspect_fatal_state uses
(default_transaction_read_only=on). That makes "takes no claim" a property
PostgreSQL enforces rather than one a reviewer checks, and it keeps list working
while the cluster is halted — precisely when an operator wants it. abandon, by
contrast, writes an ordinary ledger row, so it connects through
Ledger::connect_tool (#412): no heartbeat, and a halted cluster refused at
connect like any other one-shot writer.

Neither command builds a node client, reads a signing key or starts a listener.

The outbox has no height column, and qbit_pool_blocks.block_height is written
only at landing, so an unfinished candidate has none. Height is extracted
server-side as a scalar from the document — eight bytes on the wire — and the
candidate and block_bytes columns are never in the projection. A row whose
height is unreadable prints -, never 0.

Against a seeded database:

$ qbit-prism-server candidates list
block_hash                                                        state           height  attempts  next_attempt                      claim                                              sv  last_error
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  pending         902431  0         2026-09-16T15:48:07.994772+00:00  -                                                  1   -
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb  offer_reserved  902432  1         2026-09-16T15:48:07.995017+00:00  frontend-b until 2026-09-16T15:48:52.995017+00:00  1   -
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc  offered         902433  2         2026-09-16T15:48:27.995043+00:00  -                                                  1   -
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd  reconciliation  902434  7         2026-09-16T15:49:17.995059+00:00  -                                                  1   node rejected: bad-txnmrklroot
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee  pending         902435  3         parked                            -                                                  1   claimed candidate row failed validation: window digest mismatch
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff  pending         -       1         parked                            -                                                  2   candidate storage_version 2 is not supported by this server; only versio…(truncated)

Ordering is the oldest-due claim lane's own, so the row a server works next prints
first and parked rows sort last. A parked row (next_attempt_at='infinity', #387)
is shown as parked rather than as a due time — it is quarantined, not retrying.
The last row is #285's parked unknown-storage_version case.

An empty list exits zero and prints no unfinished candidates: nothing-pending
is the cutover step's success case, and a nonzero status would abort a set -e
runbook on exactly the outcome it is waiting for. --json emits
qbit.prism.candidates.list.v1 with every unknown as null.

Docs

  • docs/prism-ledger-ops.md gains a Candidate commands subsection inside
    "Durable block candidates", beside the state table it depends on.
  • docs/prism-rust-migration.md keeps the legacy drain instruction unchanged
    and gains a native-era counterpart: every 2.x.x candidate must still be
    drained with the pinned legacy image before migrating, and the native commands
    act on native-era rows only — they cannot read a chunked v2 body or a
    pre-migration v1 document, and running them neither satisfies nor bypasses the
    migrator's drain check. A pre-migration row that reaches a native frontend is
    parked and shows up in list, but the answer is still the legacy drain.
  • docs/prism-deleted-test-map.md: the test_prism_pending_block_recovery.py row
    stays partial and its five recover behaviours now point at 3.x.x: Native candidate recover --apply for already-accepted pending blocks #418. The
    per-case mapping below it enumerates the file at 504846c, the revision the
    document pins, and says so explicitly — that file gained three chunked-body and
    storage-version cases in v2.0.2 fix: bound candidate replay and audit finalization #258, so a reader checking the v2.0.1 commit that
    3.x.x: Candidate list and abandon commands, and the legacy-drain runbook step #268's own reference link uses will find only fourteen.

Tests

Seven gated PostgreSQL tests in
crates/qbit-prism-server/tests/support/candidates_cli.rs, each driving the real
binary against a seeded database.

Criterion Test
all four unfinished states; no claim; no payload; exit zero list_shows_every_unfinished_state_without_claiming_or_loading_payloads
parked vs retrying, unknown storage_version list_separates_parked_rows_from_retrying_rows_and_shows_unknown_storage_versions
terminalizes exactly as supersession does abandon_terminalizes_a_pending_row_exactly_as_supersession_does
refuses each offered state, row byte-identical abandon_refuses_every_offered_state_and_leaves_the_row_byte_identical
refuses a live foreign claim, succeeds once expired abandon_refuses_a_live_foreign_claim_and_succeeds_once_it_expires
refuses a terminal row and a landed pending row abandon_refuses_a_terminal_row_and_a_pending_row_whose_block_landed
#412 one-shot-tool properties abandon_registers_no_heartbeat_and_keeps_the_halt_guard

Every test asserts the process made no RPC of any kind — submitblock
included — against a counting proxy in front of the fake node, and that no
instance row was registered. "Exactly as supersession does" runs the real
policy_transition UPDATE on a twin row and compares whole rows. "Byte-identical"
is to_jsonb(o) before and after. The no-payload assertion reads the projection
through pool.describe() on the statement the command actually runs, so it cannot
drift from the SQL.

Verified locally against PostgreSQL 16: serial ledger_postgres 139 passed, 0
failed
(132 on the base, so +7); fmt and clippy -D warnings clean; the gate
correctly fails rather than skips under GITHUB_JOB=prism-native-postgres
without a database; the seven manifest ids match the gate's own executed lines.

Six adversarial mutations were each caught by a named test: dropping
state='pending'; dropping the live-claim fence; adding candidate to the
projection; dropping the operator reason; making list take the claim it reports;
and swapping connect_tool for connect.

offer_lifecycle fails under parallel load on this machine, but it fails the same
way on the base commit without these changes — it is one of the binaries #410 did
not convert to a per-fixture database. Serial it is 18 passed, 0 failed.

Scope

No schema migration, no new candidate state, no new column. The claim lane, the
backoff schedule and the retry contract are untouched, as are coordinator.rs,
ledger/blocks.rs, ledger/policy_transition.rs, ledger/connect.rs and
migrations/.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 16, 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-09-17T14:05:29.595112Z c78b5c3 New commits
ℹ️ 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.

@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: 4aeac21551

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

@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: 04fcc4b585

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

@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: b4dbf016e3

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

@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: 552781ae9b

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

@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: b63536b00a

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

kiwidream and others added 13 commits September 17, 2026 09:57
`qbit-prism-server` had no way to see or finish a block candidate: a
native-era row that keeps failing was retried forever, and the only way
to inspect or stop it was editing rows by hand.

`candidates list` is an inventory. It opens the read-only pool shape
`fatal-state show` uses, so "takes no claim" is enforced by PostgreSQL
rather than by review, and it keeps working while the cluster is halted.
Neither payload column is projected: the height is extracted to a scalar
server-side, guarded by a digit pattern so a row an unknown writer left
behind reports an unknown height instead of failing the inventory. Rows
print in the oldest-due claim lane's own ordering, so the row a server
works next comes first and parked rows sort last. An empty inventory is a
success, because that is what a cutover runbook waits for.

`candidates abandon` applies to `pending` only, which is the one
unfinished state from which no `submitblock` can yet have been made. The
whole safety property is the statement's `WHERE`: `pending`, no live
claim, and no landed block. It is a predicate and never a preceding
`SELECT`, because a row can move between a check and a write; the
diagnosis that produces the operator's message runs read-only after it,
and gates nothing. The column set is the epoch-supersession path's,
verbatim, with the operator's reason taking `last_error`'s place, and
`next_attempt_at` deliberately untouched so a parked row keeps that
evidence. Each refusal has its own exit status, so a runbook can tell a
row that was never there from one already abandoned, and both from the
two refusals that protect the invariant: a block that may already have
been offered, and a block whose accounting has landed.

Both commands read PRISM_DATABASE_URL and nothing else. Neither builds a
node client, reads a signing key, loads `Config` or starts a listener, so
"never calls submitblock" holds structurally.

Co-Authored-By: Claude Opus 5 <[email protected]>
Six gated tests drive the real binary against a recorder in front of the
fake node and assert it made no RPC call at all, `submitblock` included,
and registered no instance. The child is given a database URL and a node
URL and nothing else, so a command that loaded `Config` or started a
listener could not have succeeded.

`list` is asserted not to claim three ways: it runs to completion while
the cluster is halted, which every write path refuses; every row is
byte-identical afterwards, with `claim_token`, `claim_instance_id`,
`claim_expires_at`, `attempt_count` and the dispatch sequence compared
explicitly; and the statement it issues is described by PostgreSQL, so
the assertion that neither payload column is projected reads the result
columns rather than the SQL text.

`abandon` is asserted to leave an `offer_reserved`, `offered` or
`reconciliation` row byte-identical, to refuse a live foreign claim and
then succeed once that claim has expired, to refuse an already-terminal
row and a pending row whose block has landed, and to refuse while the
cluster is halted or a legacy writer lease is live. On the one row it
does take, the resulting row is compared field by field with a row the
epoch-supersession statement abandoned.

Co-Authored-By: Claude Opus 5 <[email protected]>
`prism-ledger-ops.md` gains a `Candidate commands` subsection three lines
below the state table it explains: the two command lines, a real `list`
output block, what each column means, the parked-versus-retrying
distinction, the refusal table with its exit codes, and the two fences
`abandon` inherits from the ordinary ledger write path — a halted cluster
and a live legacy Python writer lease, which is what stops an operator
abandon racing the 2.x.x writer during a cutover.

`prism-rust-migration.md` gains the native counterpart to the legacy
drain, and says plainly that they are not the same drain. The legacy
instruction is unchanged: every 2.x.x candidate is still drained with the
pinned legacy image before migrating, and the migrator still refuses
while any remains. The native commands act on native-era rows only, and
running them neither satisfies nor bypasses that check.

`prism-deleted-test-map.md` keeps the pending-block-recovery row partial
and its open-gap link intact, adds the six new tests where they genuinely
cover a case, and fills in the per-case mapping the row promised. The
recovery command's plan ordering, fail-closed selection across a plan and
apply idempotence stay open; the cooperative-JSON codec cases stay
retired.

Co-Authored-By: Claude Opus 5 <[email protected]>
The state predicate is the abandon statement's version guard, so two
operator abandons completing out of order are safe without any lock on
top. Prove it end to end: after the successful abandon, a second one with
a different reason exits 4, says the row is already abandoned, and leaves
it byte-identical, so the reason the first operator recorded survives.

Co-Authored-By: Claude Opus 5 <[email protected]>
`abandon` writes an ordinary ledger row, so it belongs in the same bucket
as `import-audits`, `backfill-ctv`, `broadcast-ctv` and `self-check`, not
with recovery and migration: `Registration::Tool` refuses a halted
cluster at connect, exactly as a frontend would, instead of getting as
far as the write guard. The write guard call stays, because it is what
refuses a live legacy Python writer lease, which the connection does not
cover.

`list` keeps the read-only pool. It opens no ledger and writes nothing,
`default_transaction_read_only=on` is a stronger guarantee than a tool
connection would give, and a tool connection would refuse a halted
cluster — which is exactly when an operator needs to inspect candidates.

A new gated test gives `abandon` the four properties a one-shot tool
owes: a generated instance ID leaves no row on success or on a refusal, a
live frontend's row and session-owner token are untouched when the
command runs under that frontend's ID, the halt guard holds and the
halted run changes nothing, and once the real frontend stops no row is
left that the recovery command's instance gate would refuse. Substituting
the registering connection for the tool connection fails all five abandon
tests.

Co-Authored-By: Claude Opus 5 <[email protected]>
home of the unfinished recover work would become dead ends. Row 84's
"not covered" clause and every plan-level case in the per-case table now
link #418 instead. Row 84 stays partial, and the #285 and #270 links are
untouched.

The per-case table also says which revision it enumerates. The file has
seventeen cases at 504846c, the `origin/2.x.x` commit this document pins
throughout; three of them arrived with #258, so a reader who checks
v2.0.1 instead sees fourteen and none of those three. Saying so stops the
next reader concluding the extra rows are invented.

Co-Authored-By: Claude Opus 5 <[email protected]>
The per-case retarget moved every recover link from #268 to #418, but the
header still named #268 as one of the issues those links point at. No link
points there now, so the sentence described the document's previous state.
@kiwidream
kiwidream force-pushed the candidate-list-and-abandon-commands branch from 8739ee8 to c78b5c3 Compare September 17, 2026 14:02

@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: c78b5c30b2

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

@kiwidream

kiwidream commented Sep 17, 2026 •

Copy link
Copy Markdown
Member Author

Closing as superseded by #439, which was stacked on this branch and carried every commit here (identical trees, verified by patch-id against the squash commit 32e0db4 on 3.x.x). The one open Codex thread (treat orphaned as terminal in candidates abandon) was fixed in #439's commit 3b10f52 and is already on 3.x.x. Rebasing this branch onto 3.x.x leaves no remaining diff, so there is nothing left to merge. Note: #268 does not auto-close because this PR was closed rather than merged; the work it asked for is on 3.x.x via #439.

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