Skip to content

3.x.x: fence earlier builds off a pending share-hash backfill with the share_hash_backfill_pending capability, so recording 2 by hand cannot let one serve (#669) - #681

Merged
RobertClarke merged 14 commits into
3.x.xfrom
prism-669-backfill-fence
Oct 6, 2026
Merged

RobertClarke merged 14 commits into
3.x.xfrom
prism-669-backfill-fence

Conversation

@RobertClarke

@RobertClarke RobertClarke commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

Fixes #669.

#582 (#663) records migration 2 only after 002's share-hash backfill. Every start refuses the database until then, because every native build requires 2. The gap: an earlier build that meets "3 without 2" tells the operator to record 2 by hand once 002's objects exist. After that, the earlier build would serve with legacy headers unmapped and could credit a header twice. #663 only documented "never record 2 by hand".

Change: a pending-backfill capability, using the repo's capability mechanism

  • share_hash_backfill_pending = 1 is in this build's NATIVE_CAPABILITIES and in the evidence export's list of understood capabilities.
  • Declared only in the migration transaction that creates the backfill's cursor, once 006 has created the capability table. Nothing declares it later, not even a resume.
  • Removed by the transaction that drops the cursor and records 2. It is the one capability native code removes, because it describes a state, not a format.
  • Read again before that removal, under the migration lock. At any value but 1, a newer release declared it while the runner mapped between holds of that lock. The runner then refuses and leaves the cursor, the declaration and the record to that release (Codex P2).

Why only with a new cursor

An earlier build can't remove the fence, so the fence must never coexist with an earlier build's backfill runner. Such a runner would finish the backfill, drop the cursor and record 2, and the fence it doesn't know about would then refuse the database for good.

  • Migrations serialize on the migration lock, and no earlier build's migrate passes its capability check once the fence has committed.
  • So any runner an earlier build started belongs to a cursor an earlier build created, and that cursor carries no fence.
  • Codex found two ways a fence declared later could meet such a runner, both P1s, and declaring only at creation closes both:
    • declared by the resume's migration transaction, while the earlier runner was still mapping;
    • declared by the resume's runner after the runners' lock, with an earlier runner already queued for that lock.

The price: a backfill that a #582 build started before #669 stays unfenced until it finishes. The guide and the release note say so: resume it with this release, and don't record 2 by hand.

What it enforces, for a fenced backfill

Tests

Unit:

  • an earlier build's capability list, NATIVE_CAPABILITIES without the new entry, refuses the fence, and this build's accepts it;
  • the evidence export's list of understood capabilities is pinned to NATIVE_CAPABILITIES.

Integration (ledger_postgres::share_hash_backfill, all registered in test/prism-gated-tests.txt):

  • migration_002_fences_earlier_builds_until_its_backfill_is_recorded:
    • the fence is declared with the cursor;
    • it is kept when 2 is recorded by hand, and this build still refuses to start;
    • it is still declared while a resume maps, observed while a batch waits on an open insert;
    • it is removed with the cursor when 2 is recorded;
    • the mapping and the record are complete at the end.
  • a_resume_leaves_no_fence_when_an_earlier_runner_finishes_the_backfill:
  • a_crashed_resume_leaves_no_fence_for_an_earlier_runner_to_strand:
    • this release's runner resumes an unfenced backfill and is killed in the middle of a batch;
    • the test then plays an earlier build's runner finishing the backfill;
    • no fence is left, and migrate and a start succeed.
  • migration_002_refuses_a_backfill_fence_left_without_its_cursor:
    • after a hand-dropped cursor, migrate refuses the fence before any DDL and changes nothing;
    • with 2 also hand-recorded, migrate still refuses, and a start finds 13, 17 and 24 missing;
    • raised to 2 instead, the declaration is never treated as an orphan. With 3 recorded and not 2, the record's refusal comes first, as for any newer capability. Once 2 is recorded, migrate refuses the value as newer.
  • a_runner_leaves_a_newer_releases_fence_with_its_cursor_and_record:
    • a resume's batch waits on an open insert while the test declares the fence at 2, as a newer release would;
    • the runner then refuses to record 2, and leaves the cursor, the value and the record untouched;
    • this release's next migrate refuses the value as newer, and a start still refuses the pending backfill.
  • every_start_refuses_a_backfill_fence_on_a_migrated_database:
    • a stray fence on a fully migrated database is refused by every start and every migrate;
    • raised to 2, it is refused by both as a newer release's, without the deletion remedy.

Also:

Docs

🤖 Generated with Claude Code

…e share_hash_backfill_pending capability, so recording 2 by hand cannot let one serve (#669)

#582 records migration 2 only after 002's share-hash backfill. Every start
refuses the database until then, because every build requires 2. But an
earlier build that meets 3 without 2 tells the operator to record 2 by
hand once 002's objects are present, and then it would serve with legacy
headers unmapped. The docs said never to do that; nothing enforced it.

While the backfill is pending, the database now declares the capability
share_hash_backfill_pending = 1, using the standard capability mechanism:

- The migration transaction that creates the cursor declares it, once 006
  has created the capability table.
- Every resume declares it again (ON CONFLICT DO NOTHING). So a backfill a
  #663-era build started, or one whose row was deleted, is fenced by the
  next migrate before its batches start.
- The transaction that drops the cursor and records 2 removes it. It is
  the one capability native code removes, because it describes a state,
  not a format.

Every build before #669 refuses it at connect and at migrate, whatever the
migration record says. This release refuses the cursor itself, and so does
the evidence export, so recording 2 by hand unlocks nothing.

A declaration whose cursor is gone can only be left by dropping the cursor
by hand. Every migrate refuses it before any DDL, with that cause instead
of the "3 without 2, record it by hand" remedy. The connect gate and the
recovery evidence export refuse it too.

Tests:
- An earlier build's capability list refuses the fence; this build's
  accepts it.
- The evidence export's list of understood capabilities is pinned to
  NATIVE_CAPABILITIES.
- Fence lifecycle: declared with the cursor; kept when 2 is recorded by
  hand, with starts still refused; declared again by a resume after a
  hand deletion; removed with the cursor when 2 is recorded.
- A hand-dropped cursor leaves the fence, which migrate refuses; with 2
  also recorded, migrate still refuses and the start finds 13 and 17
  missing.
- A fence on a fully migrated database is refused by every start and
  migrate.
- The evidence export refuses a fence without its cursor.

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-06T00:12:13.139142Z 48ff102 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: 3cb91f2fda

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

…runners' lock, so an earlier runner cannot leave one behind (#669)

Codex (P1) and /code-review high on #681: a resume declared the fence in
its migration transaction, which does not take the runners' lock. A runner
of a #663-era build that was still mapping could then finish, drop the
cursor and record 2 without knowing the fence. This release's runner,
queued behind it, found 2 recorded and returned, and the orphaned fence
would have locked out a fully mapped ledger.

- The migration transaction now declares the fence only for a cursor it
  creates itself.
- A runner that resumes the backfill declares it again before its first
  batch, after acquiring the runners' lock. An earlier runner holding that
  lock has finished by then, and no fence is declared that it would leave.
- The native path reads the fence from the validated capability inventory
  (inspect_source_schema). This replaces a second, weaker lookup, and its
  own cursor probe gives way to a flag set where the cursor is created.
- The orphaned-fence refusal now offers a verified way out besides the
  full restore: remove the declaration once qbit_prism_share_hashes is
  shown to map every legacy header. The docs give the query.
- The docs and release notes say precisely how earlier builds react: a
  pre-#582 build's first migrate still suggests recording 2, which changes
  nothing.

Tests:
- A new test plays the earlier runner: it holds the runners' lock while a
  resume's transaction commits, then finishes the backfill without
  touching capabilities. The resume declares no fence meanwhile, and the
  database starts afterwards.
- The lifecycle test watches the resume's runner declare the fence again
  while a batch waits on an open insert, before removing it with the
  cursor.

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

Copy link
Copy Markdown
Member Author

/code-review high round (8 candidates) and Codex's P1, addressed in 4f0f004:

  • Fixed: an earlier runner could leave the fence behind (the Codex P1 and the review's top finding). A resume now declares the fence from its runner, under the online runners' lock. The migration transaction declares it only for a cursor it creates itself. The new race test plays a 3.x.x: map 002's legacy share hashes after the migration transaction, in bounded batches with a durable cursor, and record 2 last (#582) #663-era runner finishing behind the lock; no fence is left and the database starts.
  • Fixed: no way out of an orphaned fence except a full restore. The refusal and the docs now offer a verified alternative. The docs carry a query counting legacy headers not mapped to their earliest accepted share; once it returns 0, remove the declaration and migrate again.
  • Fixed: the docs overstated how earlier builds react. They now say:
  • Fixed: pending_declared was weaker than the capability reader and duplicated it. The native path now reads the fence from inspect_source_schema's validated inventory (schema resolution, relation kind, row security), and the helper is gone.
  • Fixed: a second cursor probe decided the declaration. A flag set where the cursor is created replaces it.
  • Kept: the evidence export's message wording. It keeps the Rust refusal's prefix, "declares share_hash_backfill_pending = 1, but migration 2's share-hash backfill cursor qbit_prism_share_hash_backfill is gone", which is what the tests match. The SQL export's messages are its own throughout.

CI on 3cb91f2 failed only in as_issued_snapshot_preparation_runs_off_the_runtime_on_both_write_paths (canceling statement due to statement timeout). That test is outside this diff, and the failure is the known 2-vCPU runner flake. The new push re-runs it.

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

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

…sume, so it cannot meet an earlier build's runner (#669)

Codex (P1) on 4f0f004: moving a resume's declaration into the runner,
after the runners' lock, still left a race. An earlier build's migrate that
passed its capability check before the fence existed can queue for the
lock. If this release's runner declares the fence and then stops on an
error, a cancel or a restart, the queued earlier runner finishes the
backfill, drops the cursor and records 2 without removing a fence it does
not know. Every later start and migrate would then refuse the database.

The fence is now declared only by the migration transaction that creates
the cursor, never on a resume. Migrations serialize on the migration lock,
and no earlier build's migrate passes its capability check once the fence
has committed. So a fence never coexists with an earlier build's runner:
any runner an earlier build started belongs to a cursor an earlier build
created, which carries no fence. The price is that a backfill a #582 build
started stays unfenced to its end; the docs say so.

Tests:
- New: a resume of an unfenced backfill crashes mid-batch without
  declaring a fence. The test then plays the earlier runner finishing the
  backfill, and migrate and starts succeed afterwards.
- The lifecycle test sees the creation-time fence held through a resume's
  batches and removed with the cursor.

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

@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: 7671768cc7

ℹ️ 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 and others added 4 commits October 5, 2026 18:11
7671768's rewrite left the blocker transaction mutable, but it only
rolls back, and clippy -D warnings refuses that.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…on (#669)

Codex (P2) on 7671768: the release note read as if every pending
backfill were fenced. A backfill that a #582 build started before #669
carries no declaration, and a resume does not add one, so recording 2 by
hand could still let a pre-#582 build start with legacy headers unmapped.
The note now says so, and the guide's list of how earlier builds react
says it describes a fenced backfill.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…aration (#669)

One test's doc still said a resume declares the fence from its runner,
which 7671768 removed, and the migrator's comments said a resumed
backfill is never fenced "again", which is wrong for a cursor an earlier
build created.

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

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

ℹ️ 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 and others added 4 commits October 5, 2026 18:32
… never as an orphaned fence to delete (#669)

Codex (P2) on c20fdf2: on migrate's native path, the orphaned-fence
check matched the capability by name and ran before
refuse_newer_native_database. A newer release that declares
share_hash_backfill_pending at another value, with no cursor, was told
its declaration was an orphaned fence = 1. The remedy offered was to
delete it after checking only the old share mapping, which could remove
that release's own safety declaration instead of saying "upgrade the
server".

The native path now validates capability rows before it reads the
record, as connect does. Then the orphan check, and the 3-without-2
check after it, only ever see values this release understands. The
orphan check also matches value 1 exactly, here and at connect, where
refuse_unknown_capabilities already ran first. The evidence export
already raised on any other value inside its loop, before its own
orphan check.

Tests: both cursorless-fence tests now also raise the fence to 2. The
migrated database is refused at start and at migrate as a newer
release's. In the 3-without-2 state, migrate refuses it the same way,
naming neither the orphan nor the record. Both fail on c20fdf2.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ence

# Conflicts:
#	crates/qbit-prism-server/src/ledger/migration.rs
#	docs/prism-rust-migration.md
#	scripts/prism-recovery-evidence.sql
…build started (#669)

The narrative said every earlier build is refused for as long as legacy
headers are unmapped. That holds only for a backfill this release started:
one a #582 build started carries no declaration, as the backfill section
and the release note (37b56cc) already say.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… value 1 as an orphaned fence (#669)

e05038c moved refuse_newer_native_database ahead of
refuse_undeclared_native_database and the record check, so the fence's
name at a newer release's value would be refused as newer. That broke
offer_lifecycle's
migrated_database_without_its_offer_lifecycle_declaration_is_refused_at_connect_and_at_migrate
(CI shard 2). An edited candidate_offer_lifecycle = 0 must be refused as
an edited declaration, before the newer check, as connect refuses it, and
it was refused as "a newer PRISM release wrote this database".

The order is back as it was. The orphaned-fence check still matches
value 1 only, which is Codex's other remedy: at another value the name is
a newer release's declaration, refused as any newer capability is and
never offered for deletion. A migrated database gets "upgrade the
server". With 3 recorded and not 2, the record's refusal comes first, as
it does for any newer capability, and the next migrate after 2 is
recorded refuses the value as newer.

The cursorless-fence test now probes value 2 in both record states;
set_fence replaces the inline updates.

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

@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: 1d3843b74e

ℹ️ 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 and others added 3 commits October 5, 2026 19:41
…t, reading the fence again before recording 2 (#669)

Codex (P2) on 1d3843b: the runner maps between holds of the migration
lock, so a newer release's migrate can commit meanwhile. If that release
declares share_hash_backfill_pending at its own value, the final
transaction deleted the declaration by name, dropped the cursor and
recorded 2, defeating that release's fence.

The final transaction now reads the fence again under the migration
lock. Absent (a cursor an earlier build created) or 1, it records 2 as
before. At any other value it refuses, naming the value, and leaves the
cursor, the declaration and the record to the newer release; this
release's next migrate refuses the value as newer.

Test: a_runner_leaves_a_newer_releases_fence_with_its_cursor_and_record
holds a resume's batch on an open insert, declares the fence at 2 as a
newer release would, and then requires the runner to refuse with the
cursor, the value and the record untouched. It fails on 1d3843b. The
guide's fence section and its refusals table describe the check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ence

# Conflicts:
#	docs/prism-rust-migration.md
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