Wire Format & Spec
Actor JSON schema, unpadded base64url SQL comment transport, strict validation rules, and language-neutral conformance fixtures.
Any language, ORM, or SQL client that emits this wire format works natively with Auditstream’s PostgreSQL triggers and CLI.
Actor & context schema
{
"type": "ai_agent",
"id": "billing-agent",
"onBehalfOf": { "type": "user", "id": "usr_42" },
"tenant": "org_acme"
}typeMUST beuser,api_key,ai_agent,service, orsystem.idMUST be a non-empty string.onBehalfOfMAY hold one nested actor object, strictly one level deep (for delegated actions).tenantMAY be a non-empty string.- IDs only by default. Avoid names, emails, or IPs since comments appear in PostgreSQL logs;
contextsupportsrequestId,source, and opt-inip/userAgent. - 512-byte cap. Serialized actor plus context JSON MUST NOT exceed 512 UTF-8 bytes (≤ 683 base64url chars).
SQL transport format
-- Per statement: trailing sqlcommenter tag (default)
UPDATE invoice SET status = $1 WHERE id = $2
/*auditstream='eyJ0eXBlIjoidXNlciIsImlkIjoidXNyXzQyIn0'*/
-- Per transaction: set_config fallback
SELECT set_config(
'auditstream.actor',
'{"type":"user","id":"usr_42"}',
true
);Transport guarantees
- Injection-proof encoding: Unpadded base64url (
A-Za-z0-9_-) can never contain*/or break out of a SQL comment. - Trailing position: Must be the final comment in the statement; may share a comment with other
sqlcommenterkeys (such astraceparent). - No named prepared statements: Per-actor comments create unbounded cached statements in
pgorpsycopg; disable statement naming or useset_config. - Spoofing resistance: Extraction trims trailing whitespace/semicolons and reads only the final comment, ignoring comment-like text inside string literals.
Validation rules
Triggers (resolve_actor()), exports (event.schema.json), and SDKs enforce identical strict validation:
- Actor & context:
typeandonBehalfOf.typemust match the 5-value enum;id,tenant, and knowncontextfields (requestId,source,ip,userAgent) must be non-empty strings;onBehalfOfdepth ≤ 1. - Application events:
auditstream.log_event()requirestargetanddatato be JSON objects orNULL(elseSQLSTATE 22023) and rejects reserved DML names after trim/uppercase. - JWT fallback: Invalid
request.jwt.claimsJSON or invalid non-nullsubyieldsactor_source = 'invalid'; valid objects withoutsub(orsub: null) yieldNULL. - Fail-open capture: Table
CHECKconstraints enforcesource,actor_source, andschema_version; malformed actors never abort writes (actor = NULL,actor_source = 'invalid').
Spec & conformance
The normative specification (v0.2.0, independently SemVer-versioned) and shared conformance fixtures live in spec/:
- Normative Specification (
spec-v0.2.0) —actor.md,transport.md,history-table.md,export.md, JSON schemas, and fixtures. - Implementation Compliance Matrix (
spec/compliance.md) — per-SDK support and verified conformance coverage.