Skip to content

Persist byte-exact canonical audit bundles - #173

Merged
kiwidream merged 11 commits into
2.x.xfrom
persist-canonical-audit-bundles
Aug 20, 2026
Merged

kiwidream merged 11 commits into
2.x.xfrom
persist-canonical-audit-bundles

Conversation

@kiwidream

@kiwidream kiwidream commented Aug 20, 2026 •

Copy link
Copy Markdown
Member

Summary

  • persist the exact canonical audit-bundle bytes already verified at publication time as deterministic gzip-9 files, while keeping the existing uncompressed SHA-256 as the public artifact identity
  • serve persisted canonical bytes through the byte-exact public artifact path, with the current legacy response retained when a replica has replayed the database row before the file arrives
  • add an idempotent, resumable, verification-first historical backfill with literal-byte reuse, canonicalizer fallback, loud mismatch handling, and a fully read-only dry-run mode
  • document rollout and replica behavior, and promote the audit-bundle byte-exactness test from an expected failure to a permanent regression guarantee

Storage decision

Measured representative canonical bundles compressed from 7.97 MB to 696 KB and from 35.85 MB to 3.159 MB with gzip-9. Because zstd level 3 was only 7.8% smaller on the larger sample, this keeps the existing gzip-based store format and retains all canonical bundles. Estimated centralized growth is 5.6–9.2 GB per month.

No PostgreSQL schema, dependency, or hash-definition changes are included.

Verification

  • Docker Ruff checks for changed Python files
  • Docker Python 3.12 compilation and focused canonical-artifact tests
  • 391 focused audit-artifact, ledger, backfill, public artifact, and API contract tests
  • complete public API/read-service/contract suite
  • PostgreSQL ledger, migration, revert, process, and A1 integration gates

Fixes #158


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

kiwidream and others added 7 commits August 20, 2026 12:34
Rows written before the immutable canonical artifact API advertise an
audit_bundle_sha256 with nothing canonical behind it. Add a standalone,
idempotent, resumable backfill that walks qbit_pool_audit_bundles in
primary-key order and recovers the exact advertised bytes for each row:

* a pre-v2 literal external body is promoted verbatim, never parsed and
  re-emitted, because those bytes are the serialization the digest was
  computed over;
* a compact/v2 external body is reconstructed through the artifact store,
  with an absent on-disk share segment reloaded from the append-only
  ledger, canonicalized with the store's segment encoder, and checked
  against the segment digest the bundle embeds;
* an inline JSONB body is canonicalized with the repository's existing
  canonicalizer.

Every candidate is verified against the advertised digest before anything
reaches the filesystem, and the ledger's pre-publication preflight
(writer-lease refresh plus row identity confirmation) runs immediately
before each write. A row that fails verification is reported on stderr
with its block, digest, and reason, left untouched, and turned into a
nonzero exit status once the requested range has been processed; the
advertised digest is never rewritten.

The phase-2 canonical artifact API and the ledger preflight are bound by
name in two small adapters that report what they resolved in the run
summary, so reconciling a final signature difference is a local edit.

Co-Authored-By: Claude Opus 5 <[email protected]>
The public digest of an audit publication is the SHA-256 of the canonical
JSON bundle, but only a compact pointer body reached the disk. Answering
for that digest meant reconstructing the bundle from share segments and
trusting the reconstruction, and a segment that had since been pruned
made the identity unanswerable.

Persist the exact bytes instead. `prepare_external_audit_body` already
holds the canonical bytes it just digest-checked, so it publishes them as
a first-class artifact kind before deriving the compact body -- reusing
the bytes rather than reserializing, and running after the writer-lease
preflight so the copy stays behind the same fence as every other audit
side effect.

The store gains the artifact kind and its APIs: a content-addressed path,
a writer that verifies the uncompressed digest before publication and
again after reopening the published file, and a reader that separates a
clean miss from damage so a caller can log the loss before falling back.
Compression is stdlib gzip level 9 at mtime=0, which is reproducible, so
republishing identical bytes is a durable no-op and a conflicting
artifact fails loudly.

For the historical backfill, the resolvers now accept a missing-range
callback and a helper reconstructs a bundle from an already published
body. Recovered ranges are canonicalized and checked against the digest
the body itself commits to, then the assembled bundle against the
advertised digest, so database fallback bytes are never trusted on their
own. Pre-v2 bodies that already are the canonical bundle are taken
verbatim. A ledger preflight refreshes the writer lease and confirms the
block/digest row identity immediately before each backfill publication.

Canonical bundles are classified as their own kind, counted in metrics,
and never touched by retention.

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

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

@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: 3c11e8a1fb

ℹ️ 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: 5c1bdb2bf6

ℹ️ 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/audit_artifacts.py Resolved
@kiwidream
kiwidream merged commit dd502a5 into 2.x.x Aug 20, 2026
11 checks passed
RobertClarke added a commit that referenced this pull request Oct 6, 2026
…2026-08-24 have no canonical sidecar

The guide said 2.x wrote a sidecar beside every v2 body. It writes one beside
each new body only since #173 was deployed. On union, the bodies from before
2026-08-24, about 9,500 of them, have none, so most legacy v2 rows reach the
parts-digest check.

Co-Authored-By: Claude Opus 5.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.

1 participant