Files
0pt/src/clients/aud0-client.ts
T
Claude (SIAX platform session)andClaude Opus 5 bcf285f057
CI (SIAX Cloud) / quality (push) Successful in 38s
CI (SIAX Cloud) / security (push) Successful in 17s
merge: unify GitHub main into Gitea main (0pt)
Merges github/main (6 commits: 17-bug audit fix, CI quality gate,
.env.example + .gitignore fix, missing platform clients + registry
services, docs accuracy overhaul) into gitea/main (8 commits: Iceberg
write boundary + live ST0RE verification, lakehouse mapping routes,
Waves 1-4 foundational hardening).

16 conflicts. Both sides had independently written the four
src/clients/*.ts and the two registry services, so those are combined
rather than picked:

- st0re-client.ts: GitHub's endpoint wins on evidence. POST goes to
  /v1/st0re/artifacts/register, not /v1/st0re/artifacts -- verified in
  sax3l/st0re src/api/routes/artifacts.ts, where the bare path is a
  GET-only list route. Gitea's URL normalisation, error-body text and
  mock kept.
- n0d-client.ts: field is `status`, not `state`, with N0D's real 13-state
  union -- verified in sax3l/n0d packages/schemas/n0d.job.schema.json,
  which says explicitly not to add "succeeded". Gitea's mock echo,
  URL normalisation and error-body text kept.
- cl0ud-client.ts: Gitea's fail-closed HTTP normalisation kept (a 200
  with no `decision` reads as "deny", not as a silent allow). GitHub's
  PolicyDecision/PolicyDecisionValue naming kept; PolicyResult retained
  as an alias.
- aud0-client.ts: permissive union of both input types (optional
  payload/metadata/correlation_id), GitHub's echo return, Gitea's URL
  normalisation and error-body text, plus a JSON-parse fallback so a
  successful POST with an empty body does not throw.
- dataset-service.ts / source-service.ts: GitHub's record/DTO split and
  event naming (0pt.source_created / 0pt.create_source, which the merged
  docs already reference) plus Gitea's name validation, description /
  created_by fields and actor_id derivation. List filters are the union.
- lakehouse mapping routes: GitHub's shared lakehouse-mapping-registry
  singletons (without which every write 400'd "mapping not found") AND
  Gitea's Zod parseBody validation. Both, not either.
- src/types/source.ts, src/types/dataset.ts: unions of both vocabularies;
  GitHub's `access` legal-sourcing field kept.
- .env.example: GitHub's audited inventory plus Gitea's Zitadel /
  AUTH_ENFORCE block.
- CLAUDE.md, docs/ICEBERG_WRITE_BOUNDARY.md: GitHub's corrections kept,
  Gitea's live-verification kept and marked as superseding the
  "would 400 outright" finding (Gitea had already added the required
  X-Tenant-Id/X-Actor-Id headers).
- pnpm-lock.yaml: regenerated with pnpm 9.15.9, not merged.

Also corrects CLAUDE.md's claim that the banned services/scrapers stack
was "since removed" -- the removal PR is open, not merged, and this
branch inherits that code from Gitea main. Nothing here resurrects,
extends or re-wires any of it; services/scrapers/ is byte-identical to
Gitea main.

Verified on the merged tree: typecheck (src/ + tests/) clean;
17 test files, 121 passed, 1 skipped (the realinfra test skipping
honestly on ST0RE HTTP 401), 0 failures. Gitea main baseline was the
same 121/1; GitHub main baseline could not even `pnpm install`
("packages field missing or empty") -- Gitea's pnpm-workspace.yaml fix
resolves that. Whole-repo `typecheck:app` reports 9 errors, an identical
set to Gitea main's baseline, so it is advisory in both CI workflows
rather than deleted. Lint: 16 problems, identical to Gitea baseline.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-07-28 12:38:01 +02:00

127 lines
4.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 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<string, unknown>`,
* 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<Aud0Event>;
ingestLineageEvent(event: Aud0LineageEvent): Promise<Aud0LineageEvent>;
}
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<Aud0Event> {
return event;
}
async ingestLineageEvent(event: Aud0LineageEvent): Promise<Aud0LineageEvent> {
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<Aud0Event> {
return this.post("/v1/aud0/events", event);
}
async ingestLineageEvent(event: Aud0LineageEvent): Promise<Aud0LineageEvent> {
return this.post("/v1/aud0/lineage/events", event);
}
private async post<T>(path: string, body: T): Promise<T> {
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;
}
}