Live-verify Iceberg write path against ST0RE + close field/contract gaps

Re-runs this session's task spec against 0pt's actual repo state, which
turned out to already have a substantial, tested implementation from a
prior pass (35b6ec6): dataset<->Iceberg-table mapping, source rate policy,
and an ST0RE lakehouse client that was honestly marked "not live-verified".

This pass found api.st0re.siax.io IS reachable from this environment and
used it -- first via curl to safely discover the real contract, then
through 0pt's own updated code -- to drive a real namespace -> table ->
snapshot write against production ST0RE (task item 3's "write ONE first
test dataset" proof). That surfaced and fixed three real bugs the
mock-only pass could not have caught:

- ST0RE requires X-Tenant-Id/X-Actor-Id headers on every lakehouse route;
  the previous HTTP client sent none. Fixed via a threaded
  St0reRequestContext.
- IcebergTableInput.datasetId is a foreign key into ST0RE's OWN dataset
  table, not a free-form string -- sending 0pt's dataset_id 400s live with
  a Postgres FK violation. Fixed by no longer sending it; 0pt's
  dataset<->table linkage lives entirely in DatasetIcebergMapping.
- ST0RE enforces a real unique constraint on lakehouse namespaces
  (tenant_id, project_id, name) with no upsert route. Fixed with a
  client-side lookup-or-create (listNamespaces + findOrRegisterNamespace).

Also closed a field-level gap against the task's literal schema spec:
0pt.dataset_iceberg_mapping.schema.v1 now carries namespace_id (a real
reference to ST0RE's LakehouseNamespace.namespaceId, not guessed) and
mapped_at, both additive alongside the existing namespace/created_at
fields (sql/005, JSON schema, types, service).

tests/p0/iceberg-write-service.realinfra.test.ts replaces the prior
it.todo with a real SEAM-pattern live test: attempts the real HTTP call,
skips honestly if ST0RE is unreachable, never fabricates success. It ran
successfully against production ST0RE this session (real namespace/table/
snapshot UUIDs logged in docs/ICEBERG_WRITE_BOUNDARY.md).

Two ST0RE-side cross-tenant read bugs were found and flagged (not fixed
here -- out of scope for this repo): GET .../namespaces ignores its own
tenant header and query filters; GET .../tables/:id doesn't check the
caller's tenant against the table's owner.

npm test: 155 passed, 0 failed, 0 todo (up from 154 passed + 1 todo).
npx tsc --noEmit: 1 pre-existing, unrelated error in
quality-report-service.ts (present before this pass, part of the wider
uncommitted P0 data-platform build already in this repo) -- not touched.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-13 00:18:10 +02:00
co-authored by Claude Sonnet 5
parent 35b6ec63bf
commit bda21e87eb
11 changed files with 446 additions and 86 deletions
@@ -12,9 +12,11 @@ interface DatasetIcebergMappingRecord {
project_id: string;
dataset_id: string;
iceberg_table_id: string;
namespace_id: string | null;
namespace: string | null;
table_name: string | null;
mapping_status: string;
mapped_at: string;
mapped_by: string | null;
correlation_id: string | null;
created_at: string;
@@ -67,9 +69,11 @@ export class DatasetIcebergMappingService {
project_id: input.project_id,
dataset_id: input.dataset_id,
iceberg_table_id: input.iceberg_table_id,
namespace_id: input.namespace_id ?? null,
namespace: input.namespace ?? null,
table_name: input.table_name ?? null,
mapping_status: input.mapping_status ?? "active",
mapped_at: now,
mapped_by: input.mapped_by ?? null,
correlation_id: input.correlation_id ?? null,
created_at: now,
@@ -93,6 +97,7 @@ export class DatasetIcebergMappingService {
payload: {
dataset_id: input.dataset_id,
iceberg_table_id: input.iceberg_table_id,
namespace_id: input.namespace_id,
namespace: input.namespace,
table_name: input.table_name,
},
@@ -152,9 +157,11 @@ export class DatasetIcebergMappingService {
project_id: r.project_id,
dataset_id: r.dataset_id,
iceberg_table_id: r.iceberg_table_id,
namespace_id: r.namespace_id ?? undefined,
namespace: r.namespace ?? undefined,
table_name: r.table_name ?? undefined,
mapping_status: r.mapping_status as DatasetIcebergMappingStatus,
mapped_at: r.mapped_at,
mapped_by: r.mapped_by ?? undefined,
correlation_id: r.correlation_id ?? undefined,
created_at: r.created_at,
+72 -23
View File
@@ -59,6 +59,7 @@ export interface MapDatasetToIcebergTableInput {
export interface MapDatasetToIcebergTableResult {
mapping: DatasetIcebergMapping;
iceberg_table_id: string;
namespace_id: string;
policy_decision_id: string;
}
@@ -126,20 +127,36 @@ export class IcebergWriteService {
if (policy.decision === "deny") throw new Error(`Policy denied: ${policy.reason}`);
if (policy.decision === "approval_required") throw new Error(`Policy requires approval: ${policy.reason}`);
const ctx = { tenantId: input.tenant_id, actorId: input.actor_id };
// Register (or reuse) the namespace THROUGH ST0RE's namespace registry
// first, so the mapping can carry a real `namespace_id` (ST0RE's
// LakehouseNamespace.namespaceId) rather than only the bare namespace
// name string. See findOrRegisterNamespace's doc comment for why this is
// a lookup-or-create, not a bare create.
const namespace = await this.findOrRegisterNamespace(input.tenant_id, input.project_id, input.namespace, ctx);
// Register the table THROUGH ST0RE's table registry -- 0pt does not
// create or track lakehouse table state on its own.
const table = await this.st0reLakehouse.registerTable({
tenantId: input.tenant_id,
projectId: input.project_id,
namespace: input.namespace,
tableName: input.table_name,
tableFormat: input.table_format ?? "iceberg", // ADR-0030: Iceberg is canonical; Delta/Hudi accepted only for bridge/import scenarios, never chosen by default here.
storageUri: input.storage_uri,
catalogRef: input.catalog_ref,
datasetId: input.dataset_id,
ownerProduct: "0pt",
createdBy: input.actor_id,
});
// create or track lakehouse table state on its own. NOTE: `datasetId` is
// deliberately NOT sent -- it is a foreign key into ST0RE's OWN
// dataset/artifact table (live-verified: sending 0pt's `dataset_id` here
// 400s with a Postgres FK violation, since 0pt datasets are never rows in
// ST0RE's dataset table). 0pt's dataset<->table linkage is carried
// entirely by the DatasetIcebergMapping row created below.
const table = await this.st0reLakehouse.registerTable(
{
tenantId: input.tenant_id,
projectId: input.project_id,
namespace: input.namespace,
tableName: input.table_name,
tableFormat: input.table_format ?? "iceberg", // ADR-0030: Iceberg is canonical; Delta/Hudi accepted only for bridge/import scenarios, never chosen by default here.
storageUri: input.storage_uri,
catalogRef: input.catalog_ref,
ownerProduct: "0pt",
createdBy: input.actor_id,
},
ctx
);
const mapping = await this.mappingService.createMapping({
dataset_iceberg_mapping_id: input.dataset_iceberg_mapping_id,
@@ -147,6 +164,7 @@ export class IcebergWriteService {
project_id: input.project_id,
dataset_id: input.dataset_id,
iceberg_table_id: table.tableId,
namespace_id: namespace.namespaceId,
namespace: input.namespace,
table_name: input.table_name,
mapping_status: "active",
@@ -154,7 +172,7 @@ export class IcebergWriteService {
correlation_id: input.correlation_id,
});
return { mapping, iceberg_table_id: table.tableId, policy_decision_id: policy.decision_id };
return { mapping, iceberg_table_id: table.tableId, namespace_id: namespace.namespaceId, policy_decision_id: policy.decision_id };
}
async requestIcebergWrite(input: RequestIcebergWriteInput): Promise<RequestIcebergWriteResult> {
@@ -222,15 +240,19 @@ export class IcebergWriteService {
// Register the snapshot THROUGH ST0RE's table registry -- the one and
// only place 0pt persists that a write landed.
const snapshot = await this.st0reLakehouse.createSnapshot(mapping.iceberg_table_id, {
snapshotType: input.snapshot_type ?? "append",
schemaVersion: input.schema_version,
manifestUri: input.manifest_uri,
createdByJobId: n0dJobId,
createdByCapability: "0pt.write_iceberg_table",
recordCount: input.record_count,
sizeBytes: input.size_bytes,
});
const snapshot = await this.st0reLakehouse.createSnapshot(
mapping.iceberg_table_id,
{
snapshotType: input.snapshot_type ?? "append",
schemaVersion: input.schema_version,
manifestUri: input.manifest_uri,
createdByJobId: n0dJobId,
createdByCapability: "0pt.write_iceberg_table",
recordCount: input.record_count,
sizeBytes: input.size_bytes,
},
{ tenantId: mapping.tenant_id, actorId: input.actor_id }
);
// 0pt.iceberg_snapshot_created -- fired once the write lands and ST0RE
// has registered a new snapshot, carrying table ref + snapshot id + row
@@ -281,4 +303,31 @@ export class IcebergWriteService {
n0d_job_id: n0dJobId,
};
}
/**
* Looks up an existing ST0RE lakehouse namespace by (tenantId, projectId,
* name) before creating one. ST0RE has no upsert route and enforces a real
* unique constraint on that triple (live-verified 2026-07-13: a second
* `registerNamespace` call for the same triple 400s with
* `duplicate key value violates unique constraint
* "st0re_lakehouse_namespaces_tenant_id_project_id_name_key"`), so a bare
* "always create" call is not idempotent and would break every
* `mapDatasetToIcebergTable` call after the first for a given namespace.
*
* ST0RE's list route does not reliably scope or filter results (see the
* `listNamespaces` doc comment in st0re-lakehouse-client.ts, live-verified
* finding #4) so the match is done here, client-side, against the full
* returned list.
*/
private async findOrRegisterNamespace(
tenantId: string,
projectId: string,
name: string,
ctx: { tenantId: string; actorId: string }
) {
const existing = await this.st0reLakehouse.listNamespaces(ctx);
const match = existing.find((n) => n.tenantId === tenantId && n.projectId === projectId && n.name === name);
if (match) return match;
return this.st0reLakehouse.registerNamespace({ tenantId, projectId, name, ownerProduct: "0pt" }, ctx);
}
}