Repository navigation
Verify the audit log against its hash chain - #78
Open
WiktorB2004 wants to merge 12 commits into
Open
WiktorB2004 wants to merge 12 commits into
WiktorB2004 wants to merge 12 commits into
Conversation
WiktorB2004
force-pushed
the
add-integrity-verify
branch
from
November 27, 2026 00:43
972c174 to
a095ff8
Compare
4 of 6 tasks
With integrity, a scrub changes rows written without integrity but does not stamp them, so activity_rows alone cannot be compared with the stamps. The record now also carries stamped_activity_rows (and stamped_transaction_rows for scrub_actor). Nothing changes without integrity.
A pure class, with no SQLAlchemy import, that checks the blocks of the chain and the rows of each partition, rebuilt with the leaf functions of the sealer, and reports every failure and note. Rows are judged per incarnation, and ack blocks turn matching failures into ACKNOWLEDGED notes.
verify() reads the chain, the catalog and then each partition in short READ ONLY transactions, keyset-paged by (xid, id), and feeds the verifier core. acknowledge() appends an ack block under the sealer's lock. Both stay private.
The partition comes from the rows and the retire bounds are fixed dates, so no test depends on the clock. A chunk size that divides a partition's rows ends with one empty read.
A stamp must name a transaction no older than its row and below the snapshot's xmax; anything else is SCRUB_COUNT_MISMATCH instead of being skipped. Records and stamps from before the genesis block are no receipts. Scrubs at or above the horizon are noted as UNSEALED_TAIL and counted by a later run. A retired partition explains a missing record only when it is in the stream of the records' severity, which verify now takes as system_severity.
A row whose xid is at or above the xmax of the snapshot that read it cannot have been written yet; it was a note in the unsealed tail. The reader reads xmax after each chunk, so rows of transactions that started during the run stay in the tail.
verify only reads, so one run gains nothing from excluding another, and a session lock does not survive a pooler in transaction mode. It now runs on one connection, so a pool of one is enough.
The horizon was read before the chain, so a seal that committed in between looked like a rewind. Now the catalog comes first, then the blocks, streamed in seq chunks, then the horizon and xmax, then the rows. A partition dropped after the catalog read, as drop_expired does after its retire block, makes the run start again once; dropped again, it is reported as absent. A partition whose read fails leaves no trace in the verifier.
An ack waived every failure of its kind in its scope, so a later change in the same scope was waived too. acknowledge now runs verify first and stores each failure it waives: the row for a failure about a row, the rebuilt root and row count for a segment, every field for the rest. A failure that is new or changed since is reported again. A scope of only table is refused, and so is an ack that would waive nothing.
The async forms run verify and acknowledge on one AsyncConnection through run_sync, as aseal does. Tested on asyncpg and psycopg, with a partition dropped mid-run.
WiktorB2004
force-pushed
the
add-integrity-verify
branch
from
November 27, 2026 02:37
a095ff8 to
139e4bc
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
verify()andacknowledge()for the hash chain, with async formsaverify()andaacknowledge(). Builds on #76, so merge that first.verify(engine, tables)checks every block of the chain and every row it seals, and returns aVerificationReportwith a status (VALID,VALID_WITH_NOTESorFAILED), the failures and the notes. It collects every failure instead of stopping at the first one.xmax, then each partition in shortREAD ONLYtransactions of keyset chunks on(xid, id). It never holds one snapshot over the whole log. If a partition is dropped during the run, asdrop_expireddoes after its retire block, the run starts again once. If it is dropped again, it is reported as absent.xidis at or above the snapshot'sxmaxis reported asXID_REWIND.audit.scrubbedrecords. A stamp must name a transaction no older than its row and belowxmax. Records and stamps from before the genesis block count as no receipt. Scrubs still running at the horizon get a note and are counted by a later run. A retired partition explains a missing record only when it is in the stream of the records' severity (system_severity).audit.scrubbedrecord now also counts the rows it stamped (stamped_activity_rows, andstamped_transaction_rowsforscrub_actor). Rows written before integrity are changed but not stamped, so the existing counts could not be compared with the stamps on their own. This only happens with integrity on.acknowledge()runsverifyfirst, then appends anackblock, hashed and locked like the other blocks. The block pins each failure it waives as it was seen: the row for a row failure, the root and row count for a segment. A failure that is new or has changed since is reported again. A scope of onlytableis refused, and so is an ack that would waive nothing.Closes #74
Test plan
uv run pytestpasses locally, including the PostgreSQL tests intests/dbAUDIT_TEST_PG_IMAGE=postgres:14) as well as the default imageruff check,ruff format --checkandmypypassCHANGELOG.mdUnreleasedbullet if callers should notice (N/A: private for now)