Skip to content

fix(storage): permit same-raw revision CAS supersede - #3194

Merged
Sinity merged 2 commits into
masterfrom
fix/storage/revision-cas-same-raw-supersede
Jul 20, 2026
Merged

Sinity merged 2 commits into
masterfrom
fix/storage/revision-cas-same-raw-supersede

Conversation

@Sinity

@Sinity Sinity commented Jul 20, 2026

Copy link
Copy Markdown
Owner

Summary

record_revision_application_sync now permits a raw-revision replay to
supersede its own previous accepted head when the incoming receipt is
derived from the same accepted_raw_id — a re-derivation from identical
raw evidence, not a genuine conflict. Every raise in the function now
carries diagnostic context (logical source key, existing vs incoming raw
ids/session ids/frontiers, content-hash prefixes).

This unblocks resume #4 of operation ab5bad1f: resume #3 failed fast
after the #3191 parser fix because ~466 already-committed sessions'
raw evidence now reparses to a different content hash (and in some cases
a different session_id after identity-law fixes), and the CAS was
treating that as an unauthorized conflict.

Problem

polylogue/storage/sqlite/archive_tiers/revision_application.py's
equal-frontier CAS branch rejected any incoming receipt whose derived
semantics (accepted_content_hash, session_id) differed from the
existing raw_revision_heads row at the same frontier, unless a
FullSnapshotFoldAuthorization was present — even when the receipt's
accepted_raw_id was identical to the existing head's. The raw blob
is content-addressed and unchanged after a parser fix; only the
derivation (parsed content, and possibly the session_id it resolves
to) changed. Blocking same-raw re-derivation contradicts the repo's
derived-tier doctrine ("derived content re-derives from source",
docs/architecture.md) and means every parser or identity-law
improvement poisons already-committed archives on the next rebuild.

While reproducing this at fixture scale, a second, earlier gate with
the identical failure mode turned up: raw_revision_applications'
immutable-ledger check uses decision_id as its natural key, and
decision_id deliberately excludes accepted_content_hash from its
hash payload. So a receipt that is identical to an existing ledger row
in every decision-defining field except accepted_content_hash (the
pure "same raw, reparsed to a different hash" case) hit "conflicting
raw revision application receipt" before ever reaching the heads
CAS — same root cause, one call earlier.

Solution

Both in polylogue/storage/sqlite/archive_tiers/revision_application.py:

  • Ledger check (raw_revision_applications): when an existing row
    found by decision_id differs from the incoming receipt only in
    accepted_content_hash (the only field decision_id's hash doesn't
    cover), UPDATE the row's accepted_content_hash in place instead of
    raising. Any other field actually differing is unreachable without a
    decision_id hash collision and still raises defensively.
  • Heads CAS (raw_revision_heads, equal-frontier branch): added a
    same_raw_supersede = receipt.accepted_raw_id == existing_head[1]
    exemption alongside the existing fold-authorization exemption. Falls
    through to the same UPSERT that fold-authorized folds already use.
    Cross-raw conflicts (different accepted_raw_id, same frontier,
    differing semantics, no fold authorization) are unaffected and still
    reject — that guard protects real divergence. Older-frontier and
    incomparable-frontier-kind rejections are unaffected.
  • Error context: all six raises in the function now include
    logical_source_key, existing vs incoming accepted_raw_id/
    session_id/frontier, and content-hash prefixes. A 10-hour rebuild
    died on a context-free RuntimeError twice this weekend.

Async twin audit: grepped the repo for a second CAS implementation
(record_revision_application without _sync, an async_sqlite tree,
etc.) — none exists. record_revision_application_sync is the sole
writer of raw_revision_heads across every call site
(storage/sqlite/archive_tiers/archive.py, storage/repair.py).

Verification

  • devtools test tests/unit/storage/test_revision_application.py — 12
    passed. Extended (not duplicated) with: same-raw content-hash
    supersede, same-raw session_id supersede (identity-law fix case),
    cross-raw conflict without fold authorization (asserts the message
    names both accepted_raw_ids), idempotent same-everything
    re-application, and a frontier-mismatch case split out of the old
    parametrized test whose other two cases this PR intentionally
    changes the expected outcome of.
  • devtools test tests/unit/sources/test_live_batch_support.py — 63
    passed, 5 failed. Reproduced the identical 5 failures on unmodified
    master (08cd41b34) via a throwaway git worktree, confirming they
    are pre-existing and unrelated to this change (removed the worktree
    after comparison).
  • devtools test tests/unit/storage/test_repair.py tests/unit/storage/test_raw_revision_authority.py tests/unit/storage/test_duplicate_raw_identity_repair.py — 95
    passed (broader affected-area check for the touched CAS surface).
  • devtools verify --quick — all steps green (ruff format/check, mypy
    --strict, render all, topology/layering/closure-matrix/manifests/
    ci-workflows/doc-commands/docs-coverage/test-infra-currency/
    test-clock-hygiene/pytest-timeout-overrides/degrade-loudly).

Not run: devtools verify --all (full non-integration suite) — the
targeted + affected-area runs above cover the touched surface; the
per-PR CI test job runs post-merge on master per repo convention.

Ref ab5bad1f resume #4

Sinity added 2 commits July 20, 2026 07:12
Problem: record_revision_application_sync's equal-frontier CAS branch
rejected any incoming receipt whose derived semantics (content hash,
session_id) differed from the existing raw_revision_heads row, even
when the receipt was derived from the exact same accepted_raw_id. A
parser or identity-law fix reparsing already-committed raw evidence
into a new content hash therefore hit "raw revision CAS rejected a
conflicting accepted head" instead of superseding the head. Same
symptom one gate earlier: the immutable raw_revision_applications
ledger's natural key (decision_id) deliberately excludes
accepted_content_hash, so a pure content-hash change for an otherwise
identical decision hit "conflicting raw revision application receipt"
before ever reaching the heads CAS. Both blocked re-derivation from
identical evidence, which contradicts the derived-tier doctrine
(derived content re-derives from source) and made every parser
improvement poison already-committed archives.

Solution: in polylogue/storage/sqlite/archive_tiers/revision_application.py,
permit both same-raw paths to supersede instead of rejecting:
- the ledger check now updates the existing row's accepted_content_hash
  in place when every other decision-defining field already matched
  (i.e. only content hash differs for the same decision_id);
- the heads CAS equal-frontier branch now allows the update when
  receipt.accepted_raw_id equals the existing head's accepted_raw_id,
  alongside the existing fold-authorization exemption. Cross-raw
  conflicts (differing accepted_raw_id, same frontier, no fold
  authorization) are unaffected and still reject.
- every raise in the function now carries logical_source_key, existing
  vs incoming raw ids/session ids/frontiers, and content-hash prefixes,
  so a rebuild failure is diagnosable without a debugger.

Audited for a second CAS implementation (async twin): none exists --
record_revision_application_sync is the sole writer of
raw_revision_heads across all call sites (archive.py, repair.py).

Tests: extended tests/unit/storage/test_revision_application.py with
same-raw content-hash supersede, same-raw session_id supersede (identity
fix), cross-raw conflict without fold authorization (asserts both raw
ids appear in the message), idempotent same-everything re-application,
and split the frontier-mismatch rejections into their own case.

Verification: devtools test tests/unit/storage/test_revision_application.py
(12 passed); devtools test tests/unit/sources/test_live_batch_support.py
(5 pre-existing failures reproduced identically on master 08cd41b via
a throwaway baseline worktree, unrelated to this change; 63 passed).

Ref ab5bad1f resume #4
Problem: devtools verify --quick reported mypy --strict failing on
tests/unit/storage/test_revision_application.py:174 -- `receipt.accepted_raw_id`
is typed str | None, so `in message` was an unsupported operand check.

Solution: assert not-None before the membership check, matching the
existing dataclass typing instead of loosening it.

Verification: devtools verify --quick (mypy ok); devtools test
tests/unit/storage/test_revision_application.py (12 passed).
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 14 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: fb246fdc-8b6b-4724-a8d1-ec75b6cc49be

📥 Commits

Reviewing files that changed from the base of the PR and between d3d4fc5 and a4aac9c.

📒 Files selected for processing (2)
  • polylogue/storage/sqlite/archive_tiers/revision_application.py
  • tests/unit/storage/test_revision_application.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/storage/revision-cas-same-raw-supersede

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 d6667d1 into master Jul 20, 2026
3 checks passed
@Sinity
Sinity deleted the fix/storage/revision-cas-same-raw-supersede branch July 20, 2026 05:17

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

ℹ️ 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 on lines +216 to +219
conn.execute(
"UPDATE raw_revision_applications SET accepted_content_hash = ? WHERE decision_id = ?",
(receipt.accepted_content_hash, receipt.decision_id),
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Refresh receipt timestamps when re-deriving the accepted hash

When the same raw evidence reparses to a new content hash, this updates the existing application row's hash but leaves its old decided_at_ms; the subsequent head upsert records the new decided_at_ms. The raw-retention path in raw_retention._active_index_raw_authority requires head.decided_at_ms = application.decided_at_ms before treating a superseded receipt as deletion authority, so after precisely this re-derivation flow none of the updated superseded receipts match the head and their raw blobs remain ineligible for collection indefinitely. Refresh the receipt cohort's timestamp along with the hash, or otherwise preserve the timestamp equality used by retention.

Useful? React with 👍 / 👎.

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