feat(data-os): P0 capability layer — source registry, search, quality, dataset versions, metadata, analytics, features, ER

- Source Registry (§10): rights/license/purpose metadata, provenance,
  freshness SLA, health checks, describe/query; purpose gate FAILS CLOSED
  (no rights record => deny); cross-tenant probes => NOT_FOUND.
- Search (§10/§42): OpenSearch REST adapter (no SDK) + SearchService with
  MANDATORY tenant + purpose/rights filtering, versioned index definitions,
  bulk, generation-per-rebuild (opt-{name}--g{n}) with atomic alias cutover,
  rollback, failed-rebuild generation discard. OpenSearch never canonical.
- Quality (§10): versioned rule registry (GE-JSON interchange), local
  engine for the GE core subset (never silently passes unsupported types),
  runs/results/issues/score/trend, whole-run delegation to the GE backend
  via N0D (0pt.quality.ge_run) - never fabricated results.
- Dataset (§10/§5/§53): immutable numbered versions holding canonical
  st0re:artifact refs only, idempotent createVersion, export registers the
  ST0RE artifact + AUD0 evidence + OpenLineage.
- Metadata (§10): OpenMetadata PUT /api/v1/tables projection adapter +
  projectAll rebuild path; projection never canonical.
- Analytics (§10): ClickHouse HTTP adapter, closed metric-query DSL with
  FORCED tenant predicate + readonly/max_execution_time/max_result_rows,
  TTL retention. Zero connection to b0ts/quarantined pipelines.
- Features (§10): versioned feature-view registry, real Feast
  get-online-features adapter, materialize/get-historical as N0D delegation.
- ER (§10): generic candidate API with deterministic blocking + transparent
  scoring + per-field explanations, product hard-rule hook (force both
  directions), review-band handoff, Splink batch via N0D contract.

93 new unit/contract tests; suite 248 passed / 1 skipped.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Simon Axelsson
2026-08-31 16:49:32 +02:00
co-authored by Claude Fable 5
parent da58176735
commit b46807da75
19 changed files with 4038 additions and 0 deletions
+293
View File
@@ -0,0 +1,293 @@
/**
* src/services/dataset-version-service.ts
* ========================================
* P0 Dataset capability extensions (Master-DoD §10 Dataset:
* dataset.create/version/query/export) layered ON TOP of DatasetService (the
* owner of 0pt.dataset.schema.v1 registry entries — deliberately untouched:
* riv inte fungerande kod).
*
* dataset.version -> createVersion: an immutable, monotonically numbered
* version whose artifacts are CANONICAL ST0RE REFS
* (`st0re:artifact:art_x` — Master-DoD §5); 0pt never
* stores bytes here, only references.
* dataset.query -> listVersions/getVersion (tenant-gated).
* dataset.export -> exportDataset: registers the export artifact in ST0RE
* (real client), records AUD0 evidence + OpenLineage,
* returns the canonical `st0re:artifact:...` ref.
*
* Idempotency (§53): createVersion accepts an idempotency_key; the same key
* returns the SAME version instead of minting a duplicate.
*/
import crypto from "crypto";
import { SiaxError } from "../platform/errors";
import { formatResourceRef, isResourceRef } from "../platform/resource-ref";
import { createAud0Client, type Aud0Client } from "../clients/aud0-client";
import { createSt0reClient, type St0reClient } from "../clients/st0re-client";
import { LineageEmitter } from "../lineage/lineage-emitter";
import { DatasetService } from "./dataset-service";
export interface DatasetVersion {
dataset_id: string;
version: number;
tenant_id: string;
project_id: string;
/** Canonical ST0RE artifact refs (st0re:artifact:...) making up this version. */
artifact_refs: string[];
/** Schema version of the dataset content at this version. */
schema_version: string;
/** Row/record count when known. */
record_count?: number;
description?: string;
created_at: string;
created_by: string;
correlation_id?: string;
}
export interface CreateDatasetVersionInput {
dataset_id: string;
tenant_id: string;
project_id: string;
artifact_refs: string[];
schema_version: string;
record_count?: number;
description?: string;
created_by: string;
correlation_id?: string;
idempotency_key?: string;
}
export interface DatasetExport {
export_id: string;
dataset_id: string;
version: number;
tenant_id: string;
/** Canonical ref of the registered export artifact. */
artifact_ref: string;
format: "parquet" | "csv" | "json";
created_at: string;
}
export class DatasetVersionService {
private readonly versions = new Map<string, DatasetVersion[]>();
private readonly idempotency = new Map<string, DatasetVersion>();
private readonly aud0: Aud0Client;
private readonly st0re: St0reClient;
private readonly lineage: LineageEmitter;
readonly datasets: DatasetService;
constructor(opts?: { aud0BaseUrl?: string; st0reBaseUrl?: string; datasetService?: DatasetService }) {
this.aud0 = createAud0Client(opts?.aud0BaseUrl);
this.st0re = createSt0reClient(opts?.st0reBaseUrl);
this.lineage = new LineageEmitter(this.aud0);
this.datasets = opts?.datasetService ?? new DatasetService(opts?.aud0BaseUrl);
}
// -----------------------------------------------------------------------
// dataset.version
// -----------------------------------------------------------------------
async createVersion(input: CreateDatasetVersionInput): Promise<DatasetVersion> {
await this.assertDatasetTenant(input.dataset_id, input.tenant_id);
if (input.idempotency_key) {
const existing = this.idempotency.get(`${input.tenant_id}:${input.idempotency_key}`);
if (existing) return existing;
}
if (input.artifact_refs.length === 0) {
throw new SiaxError("INVALID_REQUEST", "0pt.dataset", "a dataset version needs at least one artifact ref");
}
for (const ref of input.artifact_refs) {
if (!isResourceRef(ref, "st0re", "artifact")) {
throw new SiaxError(
"INVALID_REQUEST",
"0pt.dataset",
`artifact ref "${ref}" is not a canonical st0re:artifact:... reference (Master-DoD §5)`
);
}
}
const list = this.versions.get(input.dataset_id) ?? [];
const version: DatasetVersion = {
dataset_id: input.dataset_id,
version: (list[list.length - 1]?.version ?? 0) + 1,
tenant_id: input.tenant_id,
project_id: input.project_id,
artifact_refs: [...input.artifact_refs],
schema_version: input.schema_version,
record_count: input.record_count,
description: input.description,
created_at: new Date().toISOString(),
created_by: input.created_by,
correlation_id: input.correlation_id,
};
list.push(version);
this.versions.set(input.dataset_id, list);
if (input.idempotency_key) {
this.idempotency.set(`${input.tenant_id}:${input.idempotency_key}`, version);
}
const versionRef = formatResourceRef({
product: "0pt",
resourceType: "dataset",
resourceId: input.dataset_id,
version: String(version.version),
});
await this.aud0.ingestEvent({
event_id: `evt_0pt_dataset_version_${input.dataset_id}_v${version.version}`,
event_type: "0pt.dataset_version_created",
tenant_id: input.tenant_id,
project_id: input.project_id,
actor_id: input.created_by,
app_id: "0pt",
capability_id: "0pt.dataset.version.v1",
resource_type: "dataset_version",
resource_id: `${input.dataset_id}@${version.version}`,
risk_level: "L2",
result: "success",
input_refs: version.artifact_refs,
output_refs: [versionRef],
payload: { schema_version: input.schema_version, record_count: input.record_count },
created_at: version.created_at,
correlation_id: input.correlation_id,
idempotency_key: input.idempotency_key,
});
await this.lineage.emit({
tenant_id: input.tenant_id,
project_id: input.project_id,
capability_id: "0pt.dataset.version.v1",
run_id: `dataset-version:${input.dataset_id}:v${version.version}`,
event_type: "COMPLETE",
inputs: version.artifact_refs.map((ref) => ({ name: ref, ref })),
outputs: [{ name: `dataset/${input.dataset_id}`, ref: versionRef }],
correlation_id: input.correlation_id,
});
return version;
}
// -----------------------------------------------------------------------
// dataset.query
// -----------------------------------------------------------------------
async listVersions(datasetId: string, tenantId: string): Promise<DatasetVersion[]> {
await this.assertDatasetTenant(datasetId, tenantId);
return [...(this.versions.get(datasetId) ?? [])];
}
async getVersion(datasetId: string, tenantId: string, version: number): Promise<DatasetVersion> {
const list = await this.listVersions(datasetId, tenantId);
const found = list.find((v) => v.version === version);
if (!found) {
throw new SiaxError("NOT_FOUND", "0pt.dataset", `dataset ${datasetId} has no version ${version}`);
}
return found;
}
// -----------------------------------------------------------------------
// dataset.export
// -----------------------------------------------------------------------
async exportDataset(input: {
dataset_id: string;
tenant_id: string;
project_id: string;
version: number;
format: DatasetExport["format"];
/** Where the export bytes were written (by the exporting job). */
object_location: { provider: string; bucket: string; key: string; region?: string };
size_bytes: number;
checksum_sha256: string;
actor_id: string;
correlation_id?: string;
}): Promise<DatasetExport> {
const version = await this.getVersion(input.dataset_id, input.tenant_id, input.version);
const exportId = `dsexp_${crypto.randomUUID()}`;
const artifact = await this.st0re.createArtifact({
tenant_id: input.tenant_id,
project_id: input.project_id,
artifact_type: "dataset_export",
app_id: "0pt",
produced_by: "0pt.dataset.export.v1",
object_location: input.object_location,
content_type:
input.format === "parquet"
? "application/vnd.apache.parquet"
: input.format === "csv"
? "text/csv"
: "application/json",
size_bytes: input.size_bytes,
checksum_sha256: input.checksum_sha256,
pii_level: "none",
retention_policy: "standard",
data_residency: "eu",
risk_level: "L2",
correlation_id: input.correlation_id,
created_by: input.actor_id,
idempotency_key: `dsexp:${input.dataset_id}:v${input.version}:${input.format}:${input.checksum_sha256}`,
});
const artifactRef = formatResourceRef({
product: "st0re",
resourceType: "artifact",
resourceId: artifact.artifact_id,
});
const record: DatasetExport = {
export_id: exportId,
dataset_id: input.dataset_id,
version: input.version,
tenant_id: input.tenant_id,
artifact_ref: artifactRef,
format: input.format,
created_at: new Date().toISOString(),
};
const versionRef = formatResourceRef({
product: "0pt",
resourceType: "dataset",
resourceId: input.dataset_id,
version: String(version.version),
});
await this.aud0.ingestEvent({
event_id: `evt_0pt_dataset_export_${exportId}`,
event_type: "0pt.dataset_exported",
tenant_id: input.tenant_id,
project_id: input.project_id,
actor_id: input.actor_id,
app_id: "0pt",
capability_id: "0pt.dataset.export.v1",
resource_type: "dataset_export",
resource_id: exportId,
risk_level: "L3",
result: "success",
input_refs: [versionRef],
output_refs: [artifactRef],
payload: { format: input.format, size_bytes: input.size_bytes },
created_at: record.created_at,
correlation_id: input.correlation_id,
});
await this.lineage.emit({
tenant_id: input.tenant_id,
project_id: input.project_id,
capability_id: "0pt.dataset.export.v1",
run_id: `dataset-export:${exportId}`,
event_type: "COMPLETE",
inputs: [{ name: `dataset/${input.dataset_id}`, ref: versionRef }],
outputs: [{ name: artifactRef, ref: artifactRef }],
correlation_id: input.correlation_id,
});
return record;
}
// -----------------------------------------------------------------------
private async assertDatasetTenant(datasetId: string, tenantId: string): Promise<void> {
if (!tenantId) throw new SiaxError("INVALID_REQUEST", "0pt.dataset", "tenant_id is required");
let dataset;
try {
dataset = await this.datasets.getDataset(datasetId);
} catch {
throw new SiaxError("NOT_FOUND", "0pt.dataset", `dataset ${datasetId} not found`);
}
if (dataset.tenant_id !== tenantId) {
throw new SiaxError("NOT_FOUND", "0pt.dataset", `dataset ${datasetId} not found`);
}
}
}