13  Per-query search audit

Record a tamper-evident audit row for every search: what was queried, with what model, what came back, and when. The substrate signs each record, stores it tenant-scoped, and publishes it to a trigger topic — so you do not hand-roll an audit schema, a signature scheme, and a stream integration in every project.

This is the primitive every audited-ML deployment in a regulated setting (finance, healthcare, legal, and the like) needs to answer “show me exactly what this model returned for this query, and prove the record hasn’t been altered.”

13.1 Key points

  • Lineage is capped. query_lineage JSON may not exceed 8 KiB (override with JAMMI_AUDIT_MAX_LINEAGE_BYTES). Store image hashes and row IDs, not raw payloads — compliance posture is structural, not advisory.
  • top_k_result_ids and retrieval_scores must be the same length. This is checked when you construct the record.
  • The table is reserved. _jammi_search_audit is created implicitly on the first log; you cannot create or directly INSERT into it (that would bypass signing). Read it freely via SQL.
  • Tenant isolation is automatic. A record logged under tenant A is invisible to tenant B, through both the typed API and raw SQL.

The program is cookbook/recipes/search_audit/example.py; its steps run here in order.

13.2 The audit master key

The engine signs every audit record with a key it derives per tenant from JAMMI_AUDIT_MASTER_KEY, and refuses to sign without one. This program sets a fixed key when none is set; in production the key comes from your secret manager, never from code:

export JAMMI_AUDIT_MASTER_KEY=$(python -c "import secrets; print(secrets.token_hex(32))")
import os
import tempfile
import uuid

import jammi

if not os.environ.get("JAMMI_AUDIT_MASTER_KEY"):
    os.environ["JAMMI_AUDIT_MASTER_KEY"] = "00" * 32

TENANT = "01906c83-d4c8-7e10-9c4f-3b6f7c5a8e9a"

db = jammi.connect(f"file://{tempfile.mkdtemp()}")
db.set_tenant(TENANT)

13.4 Fetch it back and verify it

fetch_by_query_id returns the typed record, and verify re-derives the tenant’s key and checks the signature, raising when the record was altered. fetch_recent lists the newest records.

fetched = db.audit.fetch_by_query_id(query_id)
assert fetched is not None, "record should be retrievable"
assert fetched.tenant_id == TENANT
assert fetched.signature, "record must carry a signature"
fetched.verify()
print(f"verified audit record for query {fetched.query_id}")

assert len(db.audit.fetch_recent(limit=10)) == 1
verified audit record for query 14ddeda1-0ebd-4820-904a-238f87e55a0d

13.5 Query it with SQL

The records live in the reserved _jammi_search_audit table. SQL reads it freely, under the same tenant scope; nothing writes it except audit.log, which is what keeps every row signed.

table = db.sql('SELECT model_id, model_version FROM mutable.public."_jammi_search_audit"')
assert table.num_rows == 1, "SQL view returns the tenant's audit rows"
print("model_id via SQL:", table.column("model_id")[0].as_py())
model_id via SQL: openai/clip-vit-base-patch32

13.6 Stream it

Every logged record is also published on the jammi.audit.search.v1 topic, for an alerting, analytics or warehouse subscriber. from_offset=0 replays the topic’s backing table and returns once it has caught up.

delivered = db.subscribe_collect("jammi.audit.search.v1", from_offset=0)
assert delivered.num_rows >= 1, "subscriber receives the audit payload"
print("audit topic delivered", delivered.num_rows, "row(s)")
audit topic delivered 1 row(s)
db.close()