Bound audit-emit capability.
AuditEmitter closes over the pool-level Db, its registered
listeners, and the optional AuditLogConfig. Built by the
consumer's audit_factory callback on CreateAppBackendOptions —
create_app_backend invokes the factory once with its constructed
{db, log} and lands the result on AppDeps.audit. Consumers reach
for await deps.audit.emit(ctx, input); the emitter picks where the row
is written (see "Where a row is written").
These methods cover every fan-out shape the auth domain needs
(add_listener and listener_count manage the listener chain):
emit(ctx, input) — the request-scoped emit. A success row inside a
request transaction is written in it and the promise rejects when the
INSERT fails; every other row is a fire-and-forget pool write whose
in-flight promise goes on ctx.pending_effects, and the returned promise
resolves at once.emit_role_grant_target(ctx, auth, input) — wrapper that lifts the
actor_id / account_id / ip boilerplate every role-grant-shape audit
site repeated. Delegates to emit.emit_pool(input) — awaitable pool write for code paths without a
request transaction (maintenance scripts, a post-commit thunk). Writes,
then notifies.notify(event) — fan out an already-written audit row (e.g. rows
returned by query_accept_offer that were inserted in-transaction by
the query layer, or the offer-expiry rows auth/cleanup.ts writes in
its sweep transaction). Runs every registered listener; per-listener
throws are isolated.drain_inflight() — test-binary barrier: await every in-flight emit
write, on an emitter built with track_inflight: true.is_tracking_inflight() — whether that barrier can wait on anything, so a
test binary built without tracking is caught rather than racing.
Where a row is written
The routing reads two things: the row's outcome and whether ctx.db is a
transaction (Db.in_transaction).
- A success row emitted inside a request transaction (an action with
side_effects, a route with transaction: true) is written in that
transaction: the change and the row that reports it commit together or
not at all. The INSERT is awaited, and a failed one rejects emit, so the
handler throws and its transaction rolls back — a committed change is
never left unaudited. - A failure row is written on the pool, outside the request's
transaction, so it survives the rollback that usually follows it — the
forensic record of a refused attempt.
- A success row emitted with no request transaction open (a
GET, or a
route that runs transaction: false and opens its own around just its
writes, like /login and /password, which emit once that transaction
has committed) is written on the pool too — there is no transaction to
join.
A pool write is fail-open: a failed one is counted
(get_audit_write_failures) and logged as one error line carrying the
whole row, secret metadata masked (to_dropped_audit_row), and the request
goes on. Its checkout waits the backend's audit bound
(FUZ_DB_AUDIT_CHECKOUT_TIMEOUT_MS, 30 s by default — create_app_backend
hands the factory its pool Db through Db.with_checkout_timeout), longer
than a request's, so a pool saturated enough to shed requests with a 503
does not also drop the failure rows recording them.
This is the Rust spine's split: fuz_auth's route_audit_emit writes an
action's success row in the request transaction (emit_in_tx) and every
failure row on the pool (emit), and its REST handlers (/login,
/password, signup, bootstrap) emit on the pool. Two rows differ in
routing between the twins, visible on the wire only when the audit INSERT
fails: the TS /logout runs in a route transaction, so its success row is
written in it (Rust writes it on the pool), and db_admin_row_delete is
written on the pool after the commit on both (http/db_routes.ts, through
emit_pool).
When listeners hear about a row
- A failure row is announced as soon as it is written. It records an
attempt, which happened whether or not the request's transaction commits.
- A success row is announced only after the request's transaction
commits (after the handler returns, on a route without one), and never
when the handler throws: the fan-out is queued on
ctx.post_commit_effects. A success row reports a state change, and the
listeners act on it — the revocation listeners close connections, the
audit stream broadcasts it. Announced before the commit, a close could run
while the revoked credential still reads as valid, and a rolled-back
revocation would close connections anyway.
Listeners are a documented registration seam — create_app_server
registers additional listeners via add_listener after the backend is
built (the factory-managed audit-log SSE, the revocation guard) before the
first request
runs, and removes each through the remover add_listener returns when its
assembly fails or the server closes. Consumers can also register
listeners directly on the emitter they return from audit_factory for
setups that don't pass through create_app_server.