/** * src/clients/aud0-client.ts * =========================== * 0pt's ONLY path for recording "what happened" to AUD0, the SIAX Cloud * append-only system of record. Every mutating capability in this repo emits * a plain event (`ingestEvent`) and, where the mutation produced or * transformed something, a lineage edge (`ingestLineageEvent`) -- see * docs/ICEBERG_WRITE_BOUNDARY.md's "Event names" table for the full list this * client backs. Two endpoints: * - ingestEvent -> POST {baseUrl}/v1/aud0/events * - ingestLineageEvent -> POST {baseUrl}/v1/aud0/lineage/events * * Convention (identical to cl0ud-client.ts / n0d-client.ts / st0re-client.ts * / st0re-lakehouse-client.ts): when no base URL is configured the client is * a NO-OP -- it does not attempt any network call, it never throws, and it * never blocks a caller. AUD0 is a best-effort, out-of-band audit sink for * 0pt; a missing AUD0_URL (unit tests, local dev) must not turn every * capability call into a failed fetch to `undefined`. That matches AUD0's own * documented stance that an audit emission is a record of what already * happened, not a gate the action waits on. * * HONESTY NOTE: no AUD0_URL has been available to probe in any pass so far -- * AUD0 has no live ingestion endpoint wired for 0pt yet. A real deployment * must be pointed at via `aud0BaseUrl`/`AUD0_URL` before events emitted * through this client are durable anywhere. */ export interface Aud0Event { event_id: string; event_type: string; tenant_id: string; project_id: string; actor_id: string; app_id: string; capability_id: string; resource_type: string; resource_id: string; /** Risk level L0–L5 per the SIAX Cloud risk taxonomy. */ risk_level: string; /** * Free-form event body. Optional and `unknown`-typed so every call site in * this repo type-checks: most emit a structured `Record`, * some emit nothing at all. */ payload?: unknown; created_at: string; correlation_id?: string; } export interface Aud0LineageEvent { lineage_event_id: string; tenant_id: string; project_id: string; app_id: string; capability_id: string; source_type: string; source_id: string; target_type: string; target_id: string; /** e.g. "produced" | "derived_from" | "stored_as". */ lineage_type: string; metadata?: unknown; created_at: string; correlation_id?: string; } /** Alias so both historical import names resolve to the same shape. */ export type Aud0EventInput = Aud0Event; /** Alias so both historical import names resolve to the same shape. */ export type Aud0LineageEventInput = Aud0LineageEvent; export interface Aud0Client { ingestEvent(event: Aud0Event): Promise; ingestLineageEvent(event: Aud0LineageEvent): Promise; } export function createAud0Client(baseUrl?: string): Aud0Client { if (!baseUrl) return new NoopAud0Client(); return new HttpAud0Client(baseUrl); } /** * No AUD0_URL configured: audit emission is a no-op (see module doc comment). * Echoes the event back so callers that want the emitted record get the same * value shape they would from the HTTP client. */ class NoopAud0Client implements Aud0Client { async ingestEvent(event: Aud0Event): Promise { return event; } async ingestLineageEvent(event: Aud0LineageEvent): Promise { return event; } } class HttpAud0Client implements Aud0Client { private readonly baseUrl: string; constructor(baseUrl: string) { // Normalize so `${baseUrl}/v1/...` never produces a double slash. this.baseUrl = baseUrl.replace(/\/+$/, ""); } async ingestEvent(event: Aud0Event): Promise { return this.post("/v1/aud0/events", event); } async ingestLineageEvent(event: Aud0LineageEvent): Promise { return this.post("/v1/aud0/lineage/events", event); } private async post(path: string, body: T): Promise { const res = await fetch(`${this.baseUrl}${path}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); if (!res.ok) { throw new Error(`AUD0 ${path} error: ${res.status} ${await res.text().catch(() => "")}`); } // AUD0 is not guaranteed to echo a JSON body on ingest. Prefer the // server's canonical record when there is one, otherwise fall back to what // we sent -- never fail an already-successful POST on an empty body. return (await res.json().catch(() => body)) as T; } }