Skip to content

feat(integrity): tamper-evident hash chain for event audit log (#2200)#2210

Merged
vivekchand merged 1 commit into
mainfrom
auto/issue-2200-tamper-evident-hash-chain
May 28, 2026
Merged

feat(integrity): tamper-evident hash chain for event audit log (#2200)#2210
vivekchand merged 1 commit into
mainfrom
auto/issue-2200-tamper-evident-hash-chain

Conversation

@vivekchand
Copy link
Copy Markdown
Owner

Closes #2200

What

Adds a per-node SHA-256 hash chain over the immutable identity fields of every event stored in the local DuckDB store. Each event gets chain_prev_hash and chain_hash stamped atomically with the insert; a new chain_heads table tracks the current head per node.

Changes

  • clawmetry/local_store.py — new chain_prev_hash/chain_hash columns added to the events DDL and via _MIGRATIONS_V2 (safe for existing stores); chain_heads tracking table; _stamp_integrity() called inside the flush transaction so hashes land atomically; verify_integrity(node_id=None) reader method that walks the chain and returns VALID or the first broken link.
  • clawmetry/cli.pyclawmetry verify-integrity [--node-id ID] subcommand opens the store read-only and prints the result.
  • tests/test_integrity_hash_chain.py — 10 unit tests covering: genesis hash (prev_hash = "0"*64), sequential chain links, cost-backfill safety (the critical acceptance criterion), tamper detection on immutable fields, per-node scoping, and pre-chain event counting.

Key design choices

  • Hash only immutable fields (id, agent_type, node_id, agent_id, session_id, workspace_id, event_type, ts) so cost backfills writing cost_usd/token_count/model don't invalidate the chain.
  • Off by default — set CLAWMETRY_INTEGRITY=1 to enable stamping. Zero overhead when disabled; verify-integrity works on any store regardless.
  • Consistent normalization_integrity_hash applies the same field defaults as _event_to_row (agent_type → 'openclaw', agent_id → 'main') so stamp-time and verify-time hashes always agree.
  • Atomic with INSERT_stamp_integrity runs inside the same _txn as the flush, so a crash mid-batch leaves no partial stamps.

Test plan

  • python3 -m pytest tests/test_integrity_hash_chain.py -v → 10 passed
  • python3 -m pytest tests/test_local_store.py tests/test_error_signal.py tests/test_entitlements.py tests/test_integrity_hash_chain.py -q → 60 passed
  • Lint: same 188 pre-existing errors, 0 new ones introduced

Generated by Claude Code

Adds a SHA-256 per-node hash chain over the immutable identity fields of
every event (`id`, `agent_type`, `node_id`, `agent_id`, `session_id`,
`workspace_id`, `event_type`, `ts`). Cost backfills and other post-hoc
writes touch only mutable columns and do not break the chain.

- `clawmetry/local_store.py`: new `chain_prev_hash`/`chain_hash` columns
  (DDL + migration for existing stores), `chain_heads` tracking table,
  `_stamp_integrity()` called inside the flush transaction, and
  `verify_integrity()` reader method.
- `clawmetry/cli.py`: `clawmetry verify-integrity [--node-id ID]`
  subcommand that opens the store read-only and reports VALID or the
  first broken link.
- `tests/test_integrity_hash_chain.py`: 10 unit tests covering genesis
  hash, sequential links, cost-backfill safety, tamper detection, per-node
  filter, and pre-chain event counting.

Off by default. Set `CLAWMETRY_INTEGRITY=1` to enable stamping.

Closes #2200

Co-Authored-By: Claude <noreply@anthropic.com>
@vivekchand vivekchand added the persona-skip Persona-pass cron skips this PR (backend-only, infra, or manually approved) label May 28, 2026
@vivekchand vivekchand merged commit 5e59bb1 into main May 28, 2026
24 checks passed
@vivekchand vivekchand deleted the auto/issue-2200-tamper-evident-hash-chain branch May 28, 2026 06:50
vivekchand added a commit that referenced this pull request May 28, 2026
Releases #2210 to PyPI. See the CHANGELOG entry for design + cost-backfill-safety guarantee.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

persona-skip Persona-pass cron skips this PR (backend-only, infra, or manually approved)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: tamper-evident hash chain for event integrity (audit log)

2 participants