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 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_lineageJSON may not exceed 8 KiB (override withJAMMI_AUDIT_MAX_LINEAGE_BYTES). Store image hashes and row IDs, not raw payloads — compliance posture is structural, not advisory. top_k_result_idsandretrieval_scoresmust be the same length. This is checked when you construct the record.- The table is reserved.
_jammi_search_auditis created implicitly on the first log; you cannot create or directlyINSERTinto 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))")13.3 Record a search
A PerQueryAudit says what was queried, with which model, and what came back. query_lineage holds hashes and ids, never raw payloads: it is capped at 8 KiB. audit.log adds the session’s tenant, signs the record, stores it and publishes it.
query_id = str(uuid.uuid4())
record = jammi.PerQueryAudit(
query_id=query_id,
model_id="openai/clip-vit-base-patch32",
model_version="3aa649a",
query_lineage={
"image_hashes": ["sha256:9f86d0...", "sha256:2c26b4..."],
"user_id": "12345",
"crop": [120, 80, 480, 360],
},
top_k_result_ids=["doc-0017", "doc-0042"],
retrieval_scores=[0.92, 0.88],
)
db.audit.log([record])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)) == 1verified 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()