Architecture
@xtandard/audit is one package with two planes, a stream runner, and a pluggable storage floor.
flowchart LR
subgraph host["Your app process"]
A[app code] -- "record()" --> C[core]
R[buffered recorder] --> C
P[admin panel / customer portal] --> C
S[stream runner] --> C
end
C --> DB[(storage: KV + event log)]
S -- "batches + signed checkpoints" --> SINK[SIEM / S3 / webhooks]
The two planes
Section titled “The two planes”| Record plane | Control plane | |
|---|---|---|
| Surface | record(), recordMany(), createRecorder() |
query, export, verify, checkpoints, erasure, retention, catalog |
| Traffic | hot, machine-driven | rare, human-driven |
| Guards | validation, typed action schemas, event.record hook |
auth, authorization, portal scoping, before hooks, self-audit |
| Failure stance | throw, never drop silently (ADR 0002) | fail loudly to the operator |
Anatomy of a record
Section titled “Anatomy of a record”- Resolve — tenant and actor from the input, falling back to the ambient request context
(
@xtandard/audit/context); context merged under explicit keys. - Validate — structure (valibot), then the action’s Standard Schema for
metadata(ADR 0012); withstrictActions, unknown actions fail. - Transform — redact / mask / hash / omit, so the hash covers exactly what is stored.
- Gate —
beforehooks get oneevent.recordper tenant batch (quotas, policy). - Chain — under a per-tenant lock: read the head, assign contiguous
seqs, compute the salted section digests and the chain hash linking each event to the previous one (ADR 0004). - Append — native event-log drivers insert atomically and reject a stale head (
ChainConflictError→ re-chain and retry); KV drivers claim the head with compare-and-swap first (ADR 0005). - After —
events.recordedhooks, theonRecordtap, and an automatic checkpoint when one is due.
Storage
Section titled “Storage”One contract, two layers: the four-method KV contract (control data: catalog, chain heads, checkpoints,
stream cursors) and the optional native EventLogStorage capability (events with real indexes). Drivers
without it get the core’s generic KV-index fallback. See Storage.
Streams
Section titled “Streams”A stream is a sink plus a position in each tenant’s sequence (ADR 0008).
The runner reads eventsBySeq(tenant, cursor), calls the sink, and advances only on success; retention
never prunes past the slowest cursor. See Streaming.
Deployment shapes
Section titled “Deployment shapes”- Embedded (default) — panel + stream runner inside the host app;
record()calls the core directly. - Split — the app records into shared storage; a separate process runs the panel/streams
(
xtandard-audit serve) against the same database. - Standalone — the Docker image serves the panel; apps ingest over HTTP
(
POST /api/tenants/:tenant/events).
Scale posture
Section titled “Scale posture”A library for the enormous middle: thousands to millions of events a month per app, from a process you already run. Native drivers query with indexes and keyset pagination; the KV fallback lists index keys per query (fine for small tenants with retention). Hashing uses the runtime’s synchronous SHA-256 when there is one (Bun, Node) — about a quarter of a millisecond per event end to end on a laptop. If you need a dedicated analytics store over billions of events, stream into it.