Skip to content

fix: Refuse blob deletion a replaced index tier cannot decide - #5085

Merged
Sinity merged 1 commit into
masterfrom
fix/blob-gc-replaced-index-tier-authority
Sep 14, 2026
Merged

Sinity merged 1 commit into
masterfrom
fix/blob-gc-replaced-index-tier-authority

Conversation

@Sinity

@Sinity Sinity commented Sep 14, 2026

Copy link
Copy Markdown
Owner

Summary

Blob GC no longer reads a replaced index tier's silence as proof that a blob is unreferenced. The decision is made per candidate: a blob the source tier still claims is evaluated exactly as before, and only a blob whose sole possible referent was index.attachments is blocked, and only while GC's own durable witness says the index tier was replaced by a file holding fewer blobs.

Problem

index.db is the only owner of attachments.blob_hash (storage/blob_liveness.py:68) and it is rebuildable: a wipe or an index-generation swap leaves a schema-current, row-empty file. _reference_tier_blockers passes for any file SQLite can open and _schema_blockers checks table/column presence only, never rows, so a bootstrapped-empty index yielded UNREFERENCED for every hash only it knew about.

Executed on the live archive (read-only, ~/.local/share/polylogue):

index attachment hashes: 1446
in source direct owners: 2
in blob_refs (any type): 206     (ref_type='attachment': 204)
index-only (no source trace at all): 1240

Those 1,240 payloads (~425 MB) carry no blob_refs row, no direct source owner and no publication receipt. They are byte-for-byte indistinguishable, from inside source.db, from a blob nothing ever referenced. The reproduction in polylogue-kdt2f shows the consequence: with the index moved aside and re-bootstrapped, a pass reported candidate_count=1 deleted_count=1 skipped_referenced=0 blocked_reason=None and the blob was gone. daemon/blob_gc_periodic.py:26-41 runs this every 900s, and the catch-up gate (daemon/cli.py:196-221) is bounded at 1800s, which a from-scratch rebuild exceeds.

Solution

inspect_blob_liveness takes index_authority_blocker, applied after surfaces are gathered: surfaces present → LIVE unchanged; no surfaces and a blocker → BLOCKED with a reason instead of UNREFERENCED. Every GC route already funnels through _inspect_gc_protection, so all four entry points (run_blob_gc_report planning and dry-run recheck, _execute_gc_generation_members, and both unlink_unreferenced_blob_hashes_* seams) thread one per-pass value into a per-candidate decision. _final_gc_member_liveness keeps its historical signature, so existing fault-injection doubles stay valid.

Why not the preflight shape. A blanket refusal when the index "looks unmaterialized" blocks the whole pass, which makes GC unable to reclaim genuinely source-unreferenced bytes for the entire rebuild and turns four correct tests red — test_run_blob_gc_preserves_archive_source_referenced_blobs, test_run_blob_gc_does_not_stage_again_when_all_candidates_are_referenced, test_run_blob_gc_bounds_final_lock_rechecks_with_many_references, test_run_blob_gc_refuses_when_the_index_tier_cannot_be_read. Those exercise blobs the source tier decides, where index emptiness is irrelevant. None of them is modified here.

Why a witness rather than a row-count test. The census above rules out any purely per-candidate data signal: an index-only attachment and a true orphan look identical in source.db. "Index has no sessions" and "index has no rows" both over-block, because a legitimately empty index is a normal state (the bounds test collects an orphan against exactly that). The only honest discriminator is evidence that the index tier lost what it used to hold, and the index cannot witness its own loss. storage/blob_gc_index_watermark.py records, in the blob namespace next to the bytes it protects (no tier reset touches it), the resolved index file's (st_dev, st_ino) and its distinct blob-owner hash count. A population that falls within one tier file is an ordinary deletion and re-anchors the witness; a population that falls across a tier-file replacement is the wipe/rebuild shape and blocks index-only candidates until the replacement owns at least as many blobs again. The witness is only ever written when it is safe to do so, and a failed write leaves the older, more conservative value in place.

Also in scope: _readonly raises SchemaSkewError, which is not a sqlite3.Error, so _reference_tier_blockers and _database_has_table let it escape functions whose purpose is converting unreadability into a reason. Both now catch it. This was fail-loud, not a data-loss path. Pruning polylogue/storage/blob_gc.py from the sqlite-error-default pattern baseline is the ratchet shrink that follows.

Verification

devtools test tests/unit/storage/test_blob_gc.py tests/unit/storage/test_blob_liveness.py \
  tests/unit/storage/test_blob_gc_durable_intent.py tests/unit/storage/test_blob_conservation.py \
  tests/unit/storage/test_blob_ref_liveness.py tests/unit/storage/test_blob_gc_generation_gate.py \
  tests/unit/storage/test_blob_gc_ref_type_liveness.py \
  tests/unit/storage/test_blob_gc_raw_authority_verdict_invariant.py
→ devtools test: PASSED exit=0 diagnosis=pytest_passed   ("passed": 132)

devtools verify --quick
→ every gate ok, EXIT:0   (layering and patterns both green after the baseline prune)

Anti-vacuity, executed rather than asserted. With the gate disabled (if False and index_authority_blocker is not None) and nothing else changed:

devtools test tests/unit/storage/test_blob_gc.py::test_run_blob_gc_protects_index_only_blobs_after_the_index_tier_is_replaced
→ devtools test: FAILED exit=1 diagnosis=pytest_failed
E  AssertionError: a replaced index tier's silence unlinked an index-only blob
E  assert False
E   +  where False = exists('724cb8c8d0aefc6419b48d907e392ce4ef3509daed825aef1dbc9f1f371713d2')

The gate was restored and the suites above rerun green.

Two tests are added. The first proves an index-only blob survives a wiped, re-bootstrapped index; its first pass runs against the materialized index and reclaims nothing, which proves the candidate walk, age gate and liveness route are all reached. The second proves an index that never owned a blob is not treated as a wiped one: an orphan is still collected and a source-referenced blob still retained, on the first pass and on a later pass that runs against a witness this code wrote itself.

Derived schema closure: devtools schema closure polylogue/storage/blob_gc.py and .../blob_liveness.py both report IN. This moves the index tier's derived identity (verified locally: 0f4129a5… at master, cf792ff0… here), so it must land before a fresh rebuild starts, never during one.

Residuals

  • An archive whose very first GC pass happens after a wipe has no witness and is unprotected. On the live archive the index is currently materialized (23,496 sessions, 9,327 attachment rows), so one GC pass must run before the wipe for the 1,240 payloads to be covered.
  • While the witness says the tier was replaced, GC stops at the first index-only candidate and reports blocked_reason; it reclaims nothing further that pass. That is deliberate — during that window no unreferenced-looking blob can be proven dead — but it does mean no reclamation until the index catches up to its previous population.
  • Re-anchoring compares populations, not hash sets, so a rebuild that reaches the old count with a different set would unblock. Tightening that to a set comparison is possible but was left out of this PR.
  • The witness lives in the blob namespace rather than a durable tier, because a source.db column is a gated durable change train and this needed to land ahead of the rebuild. Deleting the file fails open to today's behavior.
  • The pre-wipe census of index-only hashes (wanted outcome 2 on polylogue-kdt2f) is not implemented here; only the GC-side refusal is.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DYUNBGoE652b3nfmq3CtqK

index.db is the only owner of attachments.blob_hash and is rebuildable: a
wipe or an index-generation swap leaves a schema-current, row-empty file.
Blob GC read that silence as "unreferenced", so the first pass after a wipe
unlinked every blob whose only liveness surface was the index, reporting
clean success with blocked_reason=None.

The decision is now per candidate, not per pass. A candidate the source tier
still claims stays live exactly as before; only a candidate whose sole
possible referent was the index is blocked, and only while GC's own durable
witness says that tier was replaced with a smaller blob population. GC
records that witness (the resolved index file identity plus its distinct
blob-owner hash count) beside the bytes it protects, in the blob namespace,
which no tier reset touches. A population that falls within one tier file is
an ordinary deletion and re-anchors the witness.

A pass-level preflight refusal was rejected: it blocks archives whose index
is legitimately empty and makes GC unable to reclaim source-unreferenced
bytes for the whole rebuild.

Also converts a schema-skewed reference tier into a GC blocker instead of a
propagating exception: _readonly raises SchemaSkewError, which is not a
sqlite3.Error, so _reference_tier_blockers and _database_has_table let it
escape the functions whose purpose is turning unreadability into a reason.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01DYUNBGoE652b3nfmq3CtqK
@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.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Sep 14, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 1 minute.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: f171ac7d-82d2-45ae-82d7-427318b072e9

📥 Commits

Reviewing files that changed from the base of the PR and between ab81d4e and 0919976.

📒 Files selected for processing (5)
  • devtools/patterns/baselines/sqlite-error-default.txt
  • polylogue/storage/blob_gc.py
  • polylogue/storage/blob_gc_index_watermark.py
  • polylogue/storage/blob_liveness.py
  • tests/unit/storage/test_blob_gc.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Sinity
Sinity merged commit 3e06e51 into master Sep 14, 2026
3 checks passed
@Sinity
Sinity deleted the fix/blob-gc-replaced-index-tier-authority branch September 14, 2026 15:56
Sinity added a commit that referenced this pull request Sep 14, 2026
## Summary

Reopens the work from #5083, rebased onto current master. The original
PR's base branch (#5079) was squash-merged and deleted, which closed it
and left the branch conflicting.

Deletes `archive-root-relocation` and `source-continuity-recovery` end
to end, plus the relocation coupling inside `durable_change_train.py`.

## Problem

Both are self-documented as acting on an old archive root: `--old-root`
names "the retired pre-move root", and the other is "a one-purpose
bridge for a committed **pre-#3868** receipt". Both are dead by
construction once the archive is rebuilt with current code. Tracked by
`polylogue-2x6xu`, whose earlier "already landed" claim was verified
false — all three route groups were fully live, including three
`assert_no_prepared_*` calls on the `polylogued run` startup path.

## Solution

−8,887/+29 across 23 files: operations modules, registered CLI groups,
generated CLI output schemas, docs sections, tests.

Also fixes a deletion hazard the sweep caught: the docstring of
`_released_train_proven_floor` justified the fresh-bootstrap marker's
retirement on evidence that "archive-root relocation already rebinds" —
it would have merged clean in a different file and stood as
authoritative over a deleted mechanism.

## Verification

```
devtools verify --quick
15/15 gates ok (exit 0)
```

Rebase checked for silent revert: the first rebase attempt was cut
against a master predating #5085 and its diff showed 117 deletions in
`tests/unit/storage/test_blob_gc.py` — it would have reverted that PR's
protection tests. Re-cut against current master; the diff now touches no
blob-GC file.

## Timing

`devtools schema closure` reports
`operations/archive_root_relocation.py` and
`storage/sqlite/durable_change_train.py` as **IN** the derived schema
closure. Must merge before a fresh rebuild starts, never during one.

## Residuals

`_SOURCE_CONTINUITY_REFRESH_V1_FORMAT` and its
`_legacy_source_continuity_*` helpers are left in place: they sit on the
*retained* refresh path, which
`maintenance/blob_ref_liveness_reconciliation.py` still calls. Separate
blast radius, separate decision.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01DYUNBGoE652b3nfmq3CtqK

Co-authored-by: Claude Opus 5 (1M context) <[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