Spec v0.1 & CI
Normative spec/, JSON schemas, conformance fixtures, Cargo & pnpm foundations.
Auditstream is an open-source audit trail engine in Rust. Adapters attach the actor as a
trailing sqlcommenter tag, generated triggers record
before and after in
auditstream.changes, and the CLI exports on demand with a lossless
xid8 cursor.
1UPDATE invoice
2SET status = $1
3WHERE id = $2
4/*auditstream='eyJ0eXBlIjoidXNlciIsImlkIjoidXNyXzQyIn0'*/1-- Row-level UPDATE trigger (WHEN OLD.* IS DISTINCT FROM NEW.*)
2-- actor_source = 'comment' (unpadded base64url, IDs only)
3actor : { "type": "user", "id": "usr_42" }
4changed : [ "status" ] -- "open" -> "paid"1INSERT INTO auditstream.changes
2 (xact_id, source, table_name, op, changed, actor)
3VALUES (
4 pg_current_xact_id(), 'db', 'invoice', 'UPDATE',
5 ARRAY['status'], '{"type":"user","id":"usr_42"}'
6);$auditstream export --format jsonl --out changes.jsonl
# window: xact_id >= cursor AND xact_id < pg_snapshot_xmin()
# exported 1,428 rows (JSON Lines) · ack after flush*/ or inject SQL. IDs only — no names, emails, or IPs leak into pg_stat_activity.
Application code never writes audit rows itself. audit.as() awaits your
writes inside AsyncLocalStorage.run() and appends an unpadded base64url
sqlcommenter tag — supporting humans, API keys, services, and
AI agents acting onBehalfOf a user.
Because comments stay on the statement, standalone Prisma 7 queries need no explicit transaction and never pin connections on RDS Proxy. Malformed payloads never abort your write.
1import { PrismaClient } from "@prisma/client";
2import { audit } from "@auditstream/node";
3import { auditstreamPlugin } from "@auditstream/prisma";
4
5const db = new PrismaClient({
6 comments: [auditstreamPlugin()],
7});
8
9await audit.as(
10 {
11 type: "ai_agent",
12 id: "billing-agent",
13 onBehalfOf: { type: "user", id: "usr_42" },
14 tenant: "org_acme",
15 },
16 async () => {
17 await db.invoice.update({
18 where: { id: "inv_901" },
19 data: { status: "paid" },
20 });
21 }
22);1import { drizzle } from "drizzle-orm/node-postgres";
2import { eq } from "drizzle-orm";
3import { audit } from "@auditstream/node";
4import { wrapPgPool } from "@auditstream/drizzle";
5import { invoice } from "./schema";
6
7// Drizzle 0.45 wrapper (or .comment() in Drizzle 1.x)
8const db = drizzle(wrapPgPool(pool));
9
10await audit.as(
11 { type: "user", id: "usr_42", tenant: "org_acme" },
12 async () => {
13 await db.update(invoice)
14 .set({ status: "paid" })
15 .where(eq(invoice.id, "inv_901"));
16 }
17);1import { audit } from "@auditstream/node";
2
3// AsyncLocalStorage.run() — works in Node, Bun, Deno,
4// and Cloudflare Workers (nodejs_als)
5await audit.as(
6 { type: "api_key", id: "key_prod_09" },
7 async () => {
8 // Database writes are captured by PG triggers.
9 // App-only events go through auditstream.log_event:
10 await audit.log("invoice.exported", {
11 target: { table: "invoice", id: "inv_901" },
12 data: { format: "jsonl" },
13 });
14 }
15); auditstream.changes is partitioned by month on occurred_at,
owned by a dedicated NOLOGIN role, and written exclusively through
SECURITY DEFINER triggers. Your application role receives
SELECT only — its INSERT, UPDATE,
and DELETE tamper attempts are rejected by Postgres itself.
INSERT and DELETE fire once per statement via transition tables (REFERENCING NEW/OLD TABLE);
UPDATE stays row-level with WHEN (OLD.* IS DISTINCT FROM NEW.*) so no-op and
excluded-column writes record nothing.
1-- App role has SELECT only (INSERT/UPDATE/DELETE denied)
2SELECT xact_id, occurred_at, actor, changed,
3 before->>'status' AS prev,
4 after->>'status' AS next
5FROM auditstream.changes
6WHERE table_name = 'invoice'
7 AND record_id = '{"id":"inv_901"}'::jsonb
8ORDER BY xact_id DESC, id DESC;1{
2 "schema_version": 1,
3 "event_id": "019ab7c1-442e-7a10-9c2a-81f02b9c1140",
4 "xact_id": "48291044",
5 "occurred_at": "2026-11-24T04:11:09.184Z",
6 "source": "db", "table": "public.invoice", "op": "UPDATE",
7 "actor_source": "comment",
8 "actor": {
9 "type": "ai_agent", "id": "billing-agent",
10 "onBehalfOf": { "type": "user", "id": "usr_42" }
11 },
12 "changed": ["status"],
13 "before": { "status": "open" },
14 "after": { "status": "paid" }
15}
Logical replication needs an always-on consumer — a stopped slot retains WAL and can fill your disk.
Auditstream exports pull-based batches on demand using
xact_id >= cursor AND xact_id < pg_snapshot_xmin(),
acknowledging the cursor only after the sink flushes.
Normative RFC 2119 spec with language-neutral conformance fixtures in Rust & TS.
Statement-level INSERT/DELETE (3× faster bulk), row-level UPDATE, TRUNCATE trigger.
Hexagonal Cargo workspace (unsafe_code = forbid). Runs on demand or cron.
JSON Lines & CSV in v0.1; Webhooks, S3, SIEM, and MCP (rmcp) later.
Every claim below was benchmarked directly on PostgreSQL 16.15 (pgbench, 4 clients)
and verified across local Postgres 15–19, PgBouncer 1.26, Neon (PG 18.7), and Supabase Supavisor (PG 17.12).
Read the full Evidence log →
| Operational requirement | Auditstream · Comment (default) | Auditstream · set_config (fallback) | Logical Replication (pgoutput) |
|---|---|---|---|
| Database setup | Any PG 15–19 (free tiers too) | Any PG 15–19 | wal_level=logical · RDS reboot |
| Before & after values | OLD / NEW in trigger | OLD / NEW in trigger | REPLICA IDENTITY FULL per table |
| 24/7 consumer daemon | None — export on demand | None — export on demand | Required (slot can fill disk) |
| Connection poolers | PgBouncer · Neon · Supavisor | Pins session in RDS Proxy | Needs explicit tx per write |
| Worst-case single-row UPDATE | 6,630 tps (−40% vs 11,105) | 5,825 tps (−48% vs 11,105) | Lower write cost · higher WAL |
| Known limits (honest) | No Aurora DSQL · RDS Proxy untested | Use under Prisma Accelerate | Deferred behind EventSource |
Single-row updates are the worst case; bulk inserts run 3× faster with statement-level
transition tables (155–180 ms vs 541–550 ms for 10,000 rows).
Tested through PgBouncer 1.26, Neon pooler (HTTP & WebSocket), and Supabase Supavisor
with more clients than server connections: 9,436 set_config rows and
9,312 actor-less rows interleaved with zero crossover.
The contract in spec/ gates both repositories. v0.1 ships triggers,
auditstream.changes, JSONL/CSV export, and TypeScript adapters;
S3/Webhooks/SIEM, MCP (rmcp), and optional WAL replication via
pg_logical_emit_message follow behind the same port traits.
Normative spec/, JSON schemas, conformance fixtures, Cargo & pnpm foundations.
Domain types, port traits, base64url transport codec passing shared fixtures in Rust & TS.
auditstream-pg, JSONL/CSV sinks, Prisma 7 plugin, Drizzle 0.45 & 1.x wrappers.
init, verify, history, export + Next.js & Hono example apps.
npm, Homebrew, shell installer, ghcr.io. Later: S3, SIEM, MCP & WAL EventSource.
Writes the monthly-partitioned auditstream.changes table and triggers for Prisma, Drizzle, or plain SQL.
$auditstream init --format prisma --tables invoice,users
# wrote partitioned auditstream.changes + triggers
# statement-level INSERT/DELETE · row-level UPDATEESM-only packages for Node 22.12+, Bun, Deno, and Cloudflare Workers.
$pnpm add @auditstream/node @auditstream/prisma
Query history from the CLI or export JSON Lines / CSV using the commit-safe xid8 cursor.
$auditstream history --table invoice --limit 10
auditstream export --format jsonl --out audit.jsonl
# xact_id window exported · acked after sink flush