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
+87
View File
@@ -0,0 +1,87 @@
import { describe, it, expect } from "vitest";
import { DatasetService } from "@/src/services/dataset-service";
import { DatasetIcebergMappingService } from "@/src/services/dataset-iceberg-mapping-service";
describe("DatasetIcebergMappingService", () => {
it("createMapping persists a reference into ST0RE's table registry", async () => {
const datasetService = new DatasetService();
const service = new DatasetIcebergMappingService({ datasetService });
await datasetService.registerDataset({
dataset_id: "ds_iceberg_map_1",
tenant_id: "t1",
project_id: "p1",
name: "Iceberg Mapping Dataset",
dataset_type: "raw",
});
const mapping = await service.createMapping({
dataset_iceberg_mapping_id: "dim_test_001",
tenant_id: "t1",
project_id: "p1",
dataset_id: "ds_iceberg_map_1",
iceberg_table_id: "st0re_table_abc123",
namespace: "0pt_default",
table_name: "dim_company_se",
});
expect(mapping.dataset_iceberg_mapping_id).toBe("dim_test_001");
expect(mapping.iceberg_table_id).toBe("st0re_table_abc123");
expect(mapping.mapping_status).toBe("active");
expect(mapping.created_at).toBeDefined();
});
it("createMapping rejects an unknown dataset_id", async () => {
const service = new DatasetIcebergMappingService();
await expect(
service.createMapping({
dataset_iceberg_mapping_id: "dim_test_bad",
tenant_id: "t1",
project_id: "p1",
dataset_id: "ds_does_not_exist",
iceberg_table_id: "st0re_table_xyz",
})
).rejects.toThrow("not found");
});
it("createMapping rejects a missing iceberg_table_id", async () => {
const datasetService = new DatasetService();
const service = new DatasetIcebergMappingService({ datasetService });
await datasetService.registerDataset({
dataset_id: "ds_iceberg_map_2",
tenant_id: "t1",
project_id: "p1",
name: "No Table Ref",
dataset_type: "raw",
});
await expect(
service.createMapping({
dataset_iceberg_mapping_id: "dim_test_no_ref",
tenant_id: "t1",
project_id: "p1",
dataset_id: "ds_iceberg_map_2",
iceberg_table_id: "",
})
).rejects.toThrow("iceberg_table_id is required");
});
it("getMappingForDataset returns the active mapping for a dataset", async () => {
const datasetService = new DatasetService();
const service = new DatasetIcebergMappingService({ datasetService });
await datasetService.registerDataset({
dataset_id: "ds_iceberg_map_3",
tenant_id: "t1",
project_id: "p1",
name: "Lookup Dataset",
dataset_type: "raw",
});
await service.createMapping({
dataset_iceberg_mapping_id: "dim_test_lookup",
tenant_id: "t1",
project_id: "p1",
dataset_id: "ds_iceberg_map_3",
iceberg_table_id: "st0re_table_lookup",
});
const found = await service.getMappingForDataset("ds_iceberg_map_3");
expect(found?.iceberg_table_id).toBe("st0re_table_lookup");
});
});
@@ -0,0 +1,65 @@
import {describe,it,expect} from "vitest";
describe("0pt Enterprise Data OS Expansion (PROMPT 2)",()=>{
it("iceberg_table schema has required fields",()=>{
const schema=require("../../packages/schemas/0pt.iceberg_table.schema.json");
expect(schema.required).toContain("table_id");
expect(schema.required).toContain("table_name");
expect(schema.required).toContain("storage_uri");
expect(schema.properties.table_format.enum).toContain("iceberg");
});
it("signal schema has entity and strength",()=>{
const schema=require("../../packages/schemas/0pt.signal.schema.json");
expect(schema.required).toContain("signal_id");
expect(schema.required).toContain("signal_type");
expect(schema.required).toContain("signal_strength");
expect(schema.required).toContain("entity_type");
});
it("lineage_event schema has input/output assets",()=>{
const schema=require("../../packages/schemas/0pt.lineage_event.schema.json");
expect(schema.required).toContain("input_assets");
expect(schema.required).toContain("output_assets");
expect(schema.required).toContain("producer");
});
// Updated 2026-07-12 (0pt Iceberg write-boundary pass, docs/ICEBERG_WRITE_BOUNDARY.md):
// 0pt.source_rate_policy.schema.v1's canonical fields are rate_limit_per_minute /
// crawl_budget / compliance_notes per this session's explicit task spec (the genuine
// sourceRatePolicy ownership repair, moving this out of N0D) -- superseding the
// requests_per_second/burst_limit/priority draft this test previously checked, which
// predated that spec. See src/types/source-rate-policy.ts and
// src/services/source-rate-policy-service.ts for the real, tested implementation.
it("source_rate_policy schema has rate limits",()=>{
const schema=require("../../packages/schemas/0pt.source_rate_policy.schema.json");
expect(schema.required).toContain("rate_limit_per_minute");
expect(schema.required).toContain("crawl_budget");
expect(schema.properties.access.enum).toContain("licensed");
});
it("dataset contract includes Iceberg storage_format",()=>{
const dataset={dataset_id:"ds_001",storage_format:"iceberg",quality_score:0.94,pii_level:"medium",status:"active"};
expect(dataset.storage_format).toBe("iceberg");
expect(dataset.quality_score).toBeGreaterThan(0.9);
});
it("feature_view includes offline/online store refs",()=>{
const fv={feature_view_id:"fv_001",entity:"company",offline_store:"iceberg",online_store:"redis",features:[{name:"hiring_growth_30d",type:"float"}]};
expect(fv.offline_store).toBe("iceberg");
expect(fv.online_store).toBe("redis");
expect(fv.features[0].name).toBe("hiring_growth_30d");
});
it("N0D job request for feature materialization",()=>{
const jobRequest={job_type:"0pt.feature_materialization_job",capability_id:"0pt.materialize_features",output_artifact_type:"feature_snapshot",requires_backbone:true,resource_profile:"cpu_memory_high",risk_level:"L3"};
expect(jobRequest.job_type).toBe("0pt.feature_materialization_job");
expect(jobRequest.risk_level).toBe("L3");
});
it("0pt emits AUD0 lineage event",()=>{
const lineageEvent={event_type:"0pt.lineage_emitted",app_id:"0pt",capability_id:"0pt.enrich_entities",input_assets:[{asset_type:"dataset",asset_id:"ds_raw"}],output_assets:[{asset_type:"iceberg_table",asset_id:"table_enriched"}],st0re_artifacts:["artifact_123"],quality_report_id:"qr_123"};
expect(lineageEvent.event_type).toBe("0pt.lineage_emitted");
expect(lineageEvent.output_assets[0].asset_type).toBe("iceberg_table");
});
});
+177
View File
@@ -0,0 +1,177 @@
import { describe, it, expect } from "vitest";
import { DatasetService } from "@/src/services/dataset-service";
import { DatasetIcebergMappingService } from "@/src/services/dataset-iceberg-mapping-service";
import { IcebergWriteService } from "@/src/services/iceberg-write-service";
/**
* "Write first test dataset to Iceberg through ST0RE's table registry" proof
* (task item 2). This exercises the REAL request/response shapes and control
* flow of IcebergWriteService -> St0reLakehouseClient against that client's
* deterministic MOCK implementation, the same convention this repo already
* uses for every other cross-product boundary (see
* tests/p0/cross-product-boundary.test.ts, quality-check-service.ts's own
* CL0UD/N0D/ST0RE/AUD0 usage). No ST0RE_URL was available to this pass, so
* this is NOT verified against a live, running ST0RE lakehouse API --
* src/clients/st0re-lakehouse-client.ts's module doc comment states that
* caveat explicitly and it applies here too. What IS real: the table gets
* registered, a DatasetIcebergMapping row is created referencing ST0RE's
* (mock) tableId, a snapshot gets registered against that table, and all
* four AUD0 events fire in the right order with the right payload shape.
*/
describe("IcebergWriteService", () => {
it("mapDatasetToIcebergTable registers a table in ST0RE and records the mapping", async () => {
const datasetService = new DatasetService();
const mappingService = new DatasetIcebergMappingService({ datasetService });
const service = new IcebergWriteService({ datasetService, mappingService });
await datasetService.registerDataset({
dataset_id: "ds_first_iceberg_write",
tenant_id: "t1",
project_id: "p1",
name: "First Iceberg Pilot Dataset",
dataset_type: "enriched",
});
const result = await service.mapDatasetToIcebergTable({
tenant_id: "t1",
project_id: "p1",
dataset_id: "ds_first_iceberg_write",
dataset_iceberg_mapping_id: "dim_first_write",
namespace: "0pt_pilot",
table_name: "dim_company_se_pilot",
storage_uri: "s3://siax-p0/lakehouse/0pt_pilot/dim_company_se_pilot",
catalog_ref: "0pt_pilot.dim_company_se_pilot",
actor_id: "user_123",
correlation_id: "corr_first_write",
});
expect(result.iceberg_table_id).toContain("mock_table_");
expect(result.policy_decision_id).toBeDefined();
expect(result.mapping.dataset_id).toBe("ds_first_iceberg_write");
expect(result.mapping.iceberg_table_id).toBe(result.iceberg_table_id);
expect(result.mapping.mapping_status).toBe("active");
const lookedUp = await mappingService.getMappingForDataset("ds_first_iceberg_write");
expect(lookedUp?.dataset_iceberg_mapping_id).toBe("dim_first_write");
});
it("requestIcebergWrite registers a snapshot through ST0RE for an existing mapping", async () => {
const datasetService = new DatasetService();
const mappingService = new DatasetIcebergMappingService({ datasetService });
const service = new IcebergWriteService({ datasetService, mappingService });
await datasetService.registerDataset({
dataset_id: "ds_snapshot_write",
tenant_id: "t1",
project_id: "p1",
name: "Snapshot Write Dataset",
dataset_type: "enriched",
});
const mapped = await service.mapDatasetToIcebergTable({
tenant_id: "t1",
project_id: "p1",
dataset_id: "ds_snapshot_write",
dataset_iceberg_mapping_id: "dim_snapshot_write",
namespace: "0pt_pilot",
table_name: "dim_company_se_snapshot",
storage_uri: "s3://siax-p0/lakehouse/0pt_pilot/dim_company_se_snapshot",
catalog_ref: "0pt_pilot.dim_company_se_snapshot",
actor_id: "user_123",
correlation_id: "corr_snapshot_setup",
});
const result = await service.requestIcebergWrite({
dataset_iceberg_mapping_id: mapped.mapping.dataset_iceberg_mapping_id,
actor_id: "user_123",
correlation_id: "corr_snapshot_write",
schema_version: "1",
manifest_uri: "s3://siax-p0/lakehouse/0pt_pilot/dim_company_se_snapshot/metadata/manifest-1.avro",
record_count: 1000,
size_bytes: 204800,
});
expect(result.snapshot_id).toContain("mock_snapshot_");
expect(result.iceberg_table_id).toBe(mapped.iceberg_table_id);
expect(result.n0d_job_id).toBeUndefined(); // not a heavy write
});
it("requestIcebergWrite routes heavy writes through an N0D job request, not in-process execution", async () => {
const datasetService = new DatasetService();
const mappingService = new DatasetIcebergMappingService({ datasetService });
const service = new IcebergWriteService({ datasetService, mappingService });
await datasetService.registerDataset({
dataset_id: "ds_heavy_write",
tenant_id: "t1",
project_id: "p1",
name: "Heavy Backfill Dataset",
dataset_type: "raw",
});
const mapped = await service.mapDatasetToIcebergTable({
tenant_id: "t1",
project_id: "p1",
dataset_id: "ds_heavy_write",
dataset_iceberg_mapping_id: "dim_heavy_write",
namespace: "0pt_pilot",
table_name: "fact_financials_backfill",
storage_uri: "s3://siax-p0/lakehouse/0pt_pilot/fact_financials_backfill",
catalog_ref: "0pt_pilot.fact_financials_backfill",
actor_id: "user_123",
correlation_id: "corr_heavy_setup",
});
const result = await service.requestIcebergWrite({
dataset_iceberg_mapping_id: mapped.mapping.dataset_iceberg_mapping_id,
actor_id: "user_123",
correlation_id: "corr_heavy_write",
schema_version: "1",
manifest_uri: "s3://siax-p0/lakehouse/0pt_pilot/fact_financials_backfill/metadata/manifest-backfill.avro",
heavy: true,
});
expect(result.n0d_job_id).toBeDefined();
expect(result.n0d_job_id).toContain("mock_job_");
});
it("requestIcebergWrite rejects writes against a non-active mapping", async () => {
const datasetService = new DatasetService();
const mappingService = new DatasetIcebergMappingService({ datasetService });
const service = new IcebergWriteService({ datasetService, mappingService });
await datasetService.registerDataset({
dataset_id: "ds_deprecated_mapping",
tenant_id: "t1",
project_id: "p1",
name: "Deprecated Mapping Dataset",
dataset_type: "raw",
});
await mappingService.createMapping({
dataset_iceberg_mapping_id: "dim_deprecated",
tenant_id: "t1",
project_id: "p1",
dataset_id: "ds_deprecated_mapping",
iceberg_table_id: "st0re_table_deprecated",
mapping_status: "deprecated",
});
await expect(
service.requestIcebergWrite({
dataset_iceberg_mapping_id: "dim_deprecated",
actor_id: "user_123",
correlation_id: "corr_deprecated_write",
schema_version: "1",
manifest_uri: "s3://siax-p0/lakehouse/deprecated/manifest.avro",
})
).rejects.toThrow("is not active");
});
// TODO(future session): once ST0RE_URL points at a live, reachable ST0RE
// deployment (sax3l/st0re, /v1/st0re/lakehouse/*, port 8090 per that repo's
// STATUS.md), add a live-integration test here that constructs
// IcebergWriteService with { st0reBaseUrl: process.env.ST0RE_URL } and
// asserts the same flow against the real HTTP API. Not built in this pass
// -- no reachable ST0RE endpoint was available to verify against. See
// src/clients/st0re-lakehouse-client.ts's module doc comment and
// docs/ICEBERG_WRITE_BOUNDARY.md "Verification status" section.
it.todo("mapDatasetToIcebergTable against a LIVE ST0RE deployment (ST0RE_URL) -- not live-verified in this pass");
});
+84
View File
@@ -0,0 +1,84 @@
import { describe, it, expect } from "vitest";
import { SourceService } from "@/src/services/source-service";
import { SourceRatePolicyService } from "@/src/services/source-rate-policy-service";
describe("SourceRatePolicyService", () => {
it("createSourceRatePolicy persists rate limit + crawl budget + compliance notes", async () => {
const sourceService = new SourceService();
const service = new SourceRatePolicyService({ sourceService });
await sourceService.createSource({
source_id: "src_rate_pol_1",
tenant_id: "t1",
project_id: "p1",
source_type: "web",
name: "allabolag.se",
});
const policy = await service.createSourceRatePolicy({
source_rate_policy_id: "srp_test_001",
tenant_id: "t1",
project_id: "p1",
source_id: "src_rate_pol_1",
rate_limit_per_minute: 60,
crawl_budget: 5000,
compliance_notes: "robots.txt allows /bolag/*; authorized_web tier per docs/06-data-engine.md",
domain: "allabolag.se",
robots_ok: true,
access: "authorized_web",
});
expect(policy.source_rate_policy_id).toBe("srp_test_001");
expect(policy.rate_limit_per_minute).toBe(60);
expect(policy.crawl_budget).toBe(5000);
expect(policy.compliance_notes).toContain("robots.txt");
expect(policy.status).toBe("active");
});
it("createSourceRatePolicy rejects an unknown source_id", async () => {
const service = new SourceRatePolicyService();
await expect(
service.createSourceRatePolicy({
source_rate_policy_id: "srp_test_bad",
tenant_id: "t1",
project_id: "p1",
source_id: "src_does_not_exist",
rate_limit_per_minute: 10,
crawl_budget: 100,
})
).rejects.toThrow("not found");
});
it("createSourceRatePolicy rejects a non-positive rate_limit_per_minute", async () => {
const sourceService = new SourceService();
const service = new SourceRatePolicyService({ sourceService });
await sourceService.createSource({
source_id: "src_rate_pol_2",
tenant_id: "t1",
project_id: "p1",
source_type: "api",
name: "ratsit.se",
});
await expect(
service.createSourceRatePolicy({
source_rate_policy_id: "srp_test_zero",
tenant_id: "t1",
project_id: "p1",
source_id: "src_rate_pol_2",
rate_limit_per_minute: 0,
crawl_budget: 100,
})
).rejects.toThrow("rate_limit_per_minute must be > 0");
});
it("listSourceRatePolicies filters by source_id", async () => {
const sourceService = new SourceService();
const service = new SourceRatePolicyService({ sourceService });
await sourceService.createSource({ source_id: "src_rate_pol_3", tenant_id: "t1", project_id: "p1", source_type: "web", name: "eniro.se" });
await sourceService.createSource({ source_id: "src_rate_pol_4", tenant_id: "t1", project_id: "p1", source_type: "web", name: "merinfo.se" });
await service.createSourceRatePolicy({ source_rate_policy_id: "srp_list_1", tenant_id: "t1", project_id: "p1", source_id: "src_rate_pol_3", rate_limit_per_minute: 30, crawl_budget: 1000 });
await service.createSourceRatePolicy({ source_rate_policy_id: "srp_list_2", tenant_id: "t1", project_id: "p1", source_id: "src_rate_pol_4", rate_limit_per_minute: 30, crawl_budget: 1000 });
const results = await service.listSourceRatePolicies({ source_id: "src_rate_pol_3" });
expect(results).toHaveLength(1);
expect(results[0].source_rate_policy_id).toBe("srp_list_1");
});
});