Add 0pt Iceberg write-path ownership: dataset<->table mapping + source rate policy

Real, tested implementation of docs/11-lakehouse-write-plan.md's items 2-8
(the "future implementation session" that doc named): 0pt is now the
canonical writer of Iceberg tables through ST0RE's real, shipped
/v1/st0re/lakehouse/* table registry (sax3l/st0re commit 9ea0abb), never
persisting lakehouse table/catalog/snapshot state itself.

- src/types + src/services for 0pt.dataset_iceberg_mapping.schema.v1
  (dataset -> ST0RE tableId reference) and 0pt.source_rate_policy.schema.v1
  (the sourceRatePolicy ownership repair, moved out of N0D per ADR-0030).
- src/clients/st0re-lakehouse-client.ts: new client matching ST0RE's actual
  camelCase route contract, with the same mock/HTTP boundary convention as
  this repo's existing cl0ud/n0d/aud0/st0re clients. Honest caveat in its
  module doc comment: not verified against a live ST0RE instance in this
  pass (no reachable ST0RE_URL); every test exercises the deterministic mock.
- src/services/iceberg-write-service.ts: registers tables/snapshots through
  ST0RE, requests N0D jobs only for heavy writes (never executes them
  in-process), emits all four AUD0 event names
  (dataset_iceberg_table_mapped / iceberg_write_requested /
  iceberg_snapshot_created / source_rate_policy_created).
- sql/004, packages/schemas/*.json, app/api/0pt/lakehouse + sources/rate-policies
  routes, tests/p0/*.test.ts (154 passed + 1 honest it.todo for the live-ST0RE
  case, up from 142 baseline, 0 failures).
- Reconciled a real collision: an earlier untracked draft schema/test pair
  (tests/p0/enterprise-data-os-expansion.test.ts) asserted a different,
  now-superseded source_rate_policy shape; updated that one test block only.
- docs/ICEBERG_WRITE_BOUNDARY.md states the ownership boundary plainly and
  is explicit throughout about what's real-and-tested vs. contract-only.
  docs/11-lakehouse-write-plan.md gets a status-update section, not a rewrite.
- Data quality rules (quality-check-service.ts, quality-report-service.ts)
  were not touched -- confirmed still 0pt-owned.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-12 19:46:47 +02:00
co-authored by Claude Sonnet 5
parent 2545218265
commit 35b6ec63bf
20 changed files with 1695 additions and 0 deletions
+185
View File
@@ -0,0 +1,185 @@
// ST0RE lakehouse client -- 0pt's ONLY path for writing Iceberg table/
// snapshot state. 0pt must never persist lakehouse table/catalog/snapshot
// state anywhere else (see docs/ICEBERG_WRITE_BOUNDARY.md).
//
// Distinct from `./st0re-client.ts` (the generic Artifact registry client,
// snake_case request shape) because it targets a different, newer route
// family on the SAME ST0RE service: `/v1/st0re/lakehouse/*`
// (sax3l/st0re `src/api/routes/lakehouse.ts`, commit 9ea0abb). That route
// family's Fastify handlers parse camelCase JSON bodies matching ST0RE's own
// `IcebergTableInput`/`TableSnapshotInput`/`LakehouseNamespaceInput` types
// (sax3l/st0re `src/types.ts`) -- this client's request/response shapes are
// camelCase deliberately, to match ST0RE's real contract byte-for-byte
// rather than 0pt's own snake_case domain convention. The service layer
// (`src/services/dataset-iceberg-mapping-service.ts`,
// `src/services/iceberg-write-service.ts`) is what translates between 0pt's
// snake_case types and this client's camelCase wire shape.
//
// HONESTY NOTE (not live-verified): ST0RE's lakehouse routes are real,
// committed code in sax3l/st0re (verified by reading that repo's source in
// this same pass) and are documented as deployed via Coolify at port 8090
// (sax3l/st0re STATUS.md), but this pass did not confirm the deployment is
// currently reachable from this environment (no ST0RE_URL was available to
// probe). Exactly like `cl0ud-client.ts`/`aud0-client.ts`/`n0d-client.ts`/
// `st0re-client.ts` already do in this repo, this client falls back to a
// deterministic in-memory mock when no base URL is configured, so callers
// (and this pass's tests) exercise the real request/response shape and
// control flow without requiring a live ST0RE instance. Set ST0RE_URL (or
// pass a baseUrl) to point this at a real deployment; the HTTP path is
// written against the documented contract above but has not been executed
// against a running ST0RE in this pass.
export type IcebergTableFormat = "iceberg" | "delta" | "hudi";
export type LakehouseObjectStatus = "active" | "deprecated" | "archived";
export type TableSnapshotType = "append" | "overwrite" | "delete" | "replace" | "schema_change" | "compaction";
export interface St0reLakehouseNamespaceInput {
tenantId: string;
projectId: string;
name: string;
ownerProduct: string;
}
export interface St0reLakehouseNamespace extends St0reLakehouseNamespaceInput {
namespaceId: string;
createdAt: number;
}
export interface St0reIcebergTableInput {
tenantId: string;
projectId: string;
namespace: string;
tableName: string;
tableFormat: IcebergTableFormat;
storageUri: string;
/** Opaque reference into the external Iceberg catalog -- ST0RE does not resolve it. */
catalogRef: string;
datasetId?: string;
ownerProduct: string;
schemaId?: string;
partitionSpec?: readonly string[];
retentionPolicy?: string;
dataResidency?: string;
piiLevel?: "none" | "low" | "medium" | "high";
createdBy: string;
}
export interface St0reIcebergTable extends St0reIcebergTableInput {
tableId: string;
currentSnapshotId?: string;
status: LakehouseObjectStatus;
createdAt: number;
artifactId?: string;
}
export interface St0reTableSnapshotInput {
snapshotType: TableSnapshotType;
schemaVersion: string;
/** URI of the Iceberg manifest list/file for this snapshot -- 0pt/N0D writes the actual bytes; ST0RE only tracks the pointer. */
manifestUri: string;
createdByJobId?: string;
createdByCapability?: string;
recordCount?: number;
sizeBytes?: number;
aud0EventId?: string;
}
export interface St0reTableSnapshot extends St0reTableSnapshotInput {
snapshotId: string;
tableId: string;
tenantId: string;
projectId: string;
createdAt: number;
}
export interface St0reLakehouseClient {
registerNamespace(input: St0reLakehouseNamespaceInput): Promise<St0reLakehouseNamespace>;
registerTable(input: St0reIcebergTableInput): Promise<St0reIcebergTable>;
getTable(tableId: string): Promise<St0reIcebergTable>;
createSnapshot(tableId: string, input: St0reTableSnapshotInput): Promise<St0reTableSnapshot>;
}
export function createSt0reLakehouseClient(baseUrl?: string): St0reLakehouseClient {
if (!baseUrl) return new MockSt0reLakehouseClient();
return new HttpSt0reLakehouseClient(baseUrl);
}
class MockSt0reLakehouseClient implements St0reLakehouseClient {
private tables: Map<string, St0reIcebergTable> = new Map();
async registerNamespace(input: St0reLakehouseNamespaceInput): Promise<St0reLakehouseNamespace> {
return { ...input, namespaceId: `mock_ns_${Date.now()}`, createdAt: Date.now() };
}
async registerTable(input: St0reIcebergTableInput): Promise<St0reIcebergTable> {
const table: St0reIcebergTable = {
...input,
tableId: `mock_table_${Date.now()}`,
status: "active",
piiLevel: input.piiLevel ?? "none",
createdAt: Date.now(),
};
this.tables.set(table.tableId, table);
return table;
}
async getTable(tableId: string): Promise<St0reIcebergTable> {
const table = this.tables.get(tableId);
if (!table) throw new Error(`ST0RE mock: table ${tableId} not found`);
return table;
}
async createSnapshot(tableId: string, input: St0reTableSnapshotInput): Promise<St0reTableSnapshot> {
const table = this.tables.get(tableId);
const snapshotId = `mock_snapshot_${Date.now()}`;
if (table) table.currentSnapshotId = snapshotId;
return {
...input,
snapshotId,
tableId,
tenantId: table?.tenantId ?? "unknown",
projectId: table?.projectId ?? "unknown",
createdAt: Date.now(),
};
}
}
class HttpSt0reLakehouseClient implements St0reLakehouseClient {
constructor(private baseUrl: string) {}
async registerNamespace(input: St0reLakehouseNamespaceInput): Promise<St0reLakehouseNamespace> {
const res = await fetch(`${this.baseUrl}/v1/st0re/lakehouse/namespaces`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(input),
});
if (!res.ok) throw new Error(`ST0RE lakehouse namespace error: ${res.status}`);
return res.json();
}
async registerTable(input: St0reIcebergTableInput): Promise<St0reIcebergTable> {
const res = await fetch(`${this.baseUrl}/v1/st0re/lakehouse/tables`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(input),
});
if (!res.ok) throw new Error(`ST0RE lakehouse table error: ${res.status}`);
return res.json();
}
async getTable(tableId: string): Promise<St0reIcebergTable> {
const res = await fetch(`${this.baseUrl}/v1/st0re/lakehouse/tables/${tableId}`);
if (!res.ok) throw new Error(`ST0RE lakehouse get-table error: ${res.status}`);
return res.json();
}
async createSnapshot(tableId: string, input: St0reTableSnapshotInput): Promise<St0reTableSnapshot> {
const res = await fetch(`${this.baseUrl}/v1/st0re/lakehouse/tables/${tableId}/snapshots`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(input),
});
if (!res.ok) throw new Error(`ST0RE lakehouse snapshot error: ${res.status}`);
return res.json();
}
}