Hashing each entry against the one before it catches edits and reordering, but only a value stored somewhere the log's writers cannot reach also catches truncation and a full rewrite.
Think of a guest log chained page to page, each page naming the page before it. Tear one page out and every later page's reference breaks. The exception is when the thief also rewrites every following page by hand. In that case only someone holding a separate note of "how many pages there should be" can tell.
A hash chain makes each record commit to the hash of the one before it. That means edits, deletions and reordering break the chain, but only an anchor kept outside the log itself can also catch a truncated tail or a full rewrite by someone who recomputes every hash.
A worked example. Across five tampering attacks tried on a 200-entry log, a plain list caught 0 of 5. A hash chain caught 3 of 5 -- an edit, a deletion and a reordering -- but missed truncating the tail and a full rewrite. Only the hash-chain-plus-anchor and HMAC-chain-plus-anchor designs caught all 5, because the anchor's own length-and-hash pair cannot be recomputed by whoever controls the log's storage.
The key lines. From code/mini-accountability/accountability.py:
# from code/mini-accountability/accountability.py
def _digest ( self , prev , rec ):
data = ( prev + canon ( rec )) . encode ()
return hmac . new ( self . key , data , hashlib . sha256 ) . hexdigest () if self . key else _h ( data )
def head ( self ):
return ( len ( self . rows ), self . rows [ - 1 ][ "hash" ] if self . rows else "0" * 64 )
Read it as: head() returns the anchor's raw material: the chain's length and its latest hash. That pair only protects the log once it is stored somewhere the log's own writers cannot reach. Recomputing the whole chain from scratch is the only way to defeat it.
Hash chain, HMAC, anchor
Hash chain : each record stores the hash of the previous one.
HMAC : a keyed hash; without the key you cannot recompute it.
Anchor : the latest hash and length stored somewhere the log's writers cannot change.
Example: log.head() returning (200, "7fa3...") and stored outside the log is what let the test catch an attacker who deleted the middle entry and recomputed every hash that followed it.
AAAWgmp1bWIAAAAeanVtZGMycGEAEQAQgAAAqgA4m3EDYzJwYQAAABZcanVtYgAAAEdqdW1kYzJtYQARABCAAACqADibcQN1cm46YzJwYTowYjJhNDNhMC01NGZmLTRiNDEtODhhNS1lZmNhNWI4NGUwNWUAAAADl2p1bWIAAAApanVtZGMyYXMAEQAQgAAAqgA4m3EDYzJwYS5hc3NlcnRpb25zAAAAALxqdW1iAAAARGp1bWRjYm9yABEAE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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPZYQLLkn9GWAIGb8zKRPe8m1QCToTj3nCscAeveuV6E86i/8DLkkSEobKfc3Uny/ABlY6WdVlFqijK87jDAzNGcgu0= A hash chain makes edits visible; only an outside anchor reveals deleted tails and rewrites entry 1 record + prev hash hash = H(prev + record) entry 2 record + prev hash hash = H(prev + record) entry 3 record + prev hash hash = H(prev + record) entry 4 record + prev hash hash = H(prev + record) entry 5 record + prev hash hash = H(prev + record) anchor (outside) length + head hash Key held elsewhere (HMAC): rewriting needs the key. Anchor in another system (signed head, write-once store): catches truncation and a rewrite with a stolen key. Log who acted, what, with which arguments, the decision, the approver, model and prompt versions and the trace id. Logging is a record of accountability, not a substitute for it: someone must own the agent and the decisions. Hash-chained entries with an external anchor and the fields every record needs.