fix(recovery): restore source removed by production documentation merge
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* src/lineage/lineage-edge.ts
|
||||
* ============================
|
||||
* P067: the flattened, QUERYABLE form of an OpenLineage run event.
|
||||
*
|
||||
* LineageEmitter (src/lineage/lineage-emitter.ts) already emits spec-shaped
|
||||
* run events to AUD0, and AUD0 remains the SYSTEM OF RECORD for them. But
|
||||
* "emitted to AUD0" is not "answerable from 0pt": nothing in this repo could
|
||||
* answer "which sources fed dataset ds_x?" without asking another product.
|
||||
* These edges are 0pt's own projection — one row per (run, input, output)
|
||||
* pair — written in the same call that emits, so the question is answerable
|
||||
* locally and joins back to AUD0 by `run_id`.
|
||||
*
|
||||
* The projection is deliberately LOSSY (facets are not stored, only the
|
||||
* canonical SIAX ref) — it is an index, not a second source of truth.
|
||||
*/
|
||||
|
||||
export interface LineageEdge {
|
||||
/** Deterministic sha256 of (run_id, input, output) — a re-emitted idempotent run does not duplicate edges. */
|
||||
edge_id: string;
|
||||
tenant_id: string;
|
||||
run_id: string;
|
||||
capability_id: string;
|
||||
input_namespace: string;
|
||||
input_name: string;
|
||||
input_ref?: string;
|
||||
output_namespace: string;
|
||||
output_name: string;
|
||||
output_ref?: string;
|
||||
event_time: string;
|
||||
correlation_id?: string;
|
||||
}
|
||||
|
||||
/** Query one direction of the graph. Exactly one of the two selectors is required. */
|
||||
export interface LineageQuery {
|
||||
tenant_id: string;
|
||||
/** Everything that fed this output (upstream). */
|
||||
output_name?: string;
|
||||
/** Everything this input produced (downstream). */
|
||||
input_name?: string;
|
||||
limit?: number;
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* 0pt OpenLineage emitter.
|
||||
*
|
||||
* Durability belongs to the AUD0 client boundary, not here. The default
|
||||
* createAud0Client() persists to the local outbox when DATABASE_URL exists;
|
||||
* the raw dispatcher client (durable:false) performs the actual HTTP delivery.
|
||||
* This emitter therefore builds one canonical RunEvent and records it exactly
|
||||
* once through the injected client.
|
||||
*/
|
||||
import type { Aud0Client } from "../clients/aud0-client";
|
||||
import {
|
||||
OPT_OPENLINEAGE_PRODUCER,
|
||||
type OpenLineageDatasetRef,
|
||||
type OpenLineageEventType,
|
||||
type OpenLineageRunEvent,
|
||||
} from "./openlineage";
|
||||
|
||||
export interface LineageEmitInput {
|
||||
tenant_id: string;
|
||||
project_id?: string;
|
||||
capability_id: string;
|
||||
run_id: string;
|
||||
event_type: OpenLineageEventType;
|
||||
event_time?: string;
|
||||
inputs?: LineageDatasetInput[];
|
||||
outputs?: LineageDatasetInput[];
|
||||
correlation_id?: string;
|
||||
run_facets?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface LineageDatasetInput {
|
||||
name: string;
|
||||
ref?: string;
|
||||
namespace?: string;
|
||||
facets?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface LineageEmitResult {
|
||||
/** True only when AUD0 itself acknowledged the event synchronously. */
|
||||
emitted: boolean;
|
||||
/** True when the event is durably queued for at-least-once AUD0 delivery. */
|
||||
queued: boolean;
|
||||
run_id: string;
|
||||
outbox_id?: string;
|
||||
warning?: string;
|
||||
}
|
||||
|
||||
const APP_ID = "0pt";
|
||||
|
||||
function toDatasetRef(d: LineageDatasetInput): OpenLineageDatasetRef {
|
||||
return {
|
||||
namespace: d.namespace ?? APP_ID,
|
||||
name: d.name,
|
||||
facets: {
|
||||
...(d.ref ? { siaxResourceRef: { ref: d.ref } } : {}),
|
||||
...(d.facets ?? {}),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function safeWarning(error: unknown): string {
|
||||
return error instanceof Error ? error.name : "UnknownError";
|
||||
}
|
||||
|
||||
export class LineageEmitter {
|
||||
constructor(private readonly aud0: Aud0Client) {}
|
||||
|
||||
buildRunEvent(input: LineageEmitInput): OpenLineageRunEvent {
|
||||
return {
|
||||
eventType: input.event_type,
|
||||
eventTime: input.event_time ?? new Date().toISOString(),
|
||||
producer: OPT_OPENLINEAGE_PRODUCER,
|
||||
run: {
|
||||
runId: input.run_id,
|
||||
facets: {
|
||||
...(input.correlation_id ? { siaxCorrelation: { correlation_id: input.correlation_id } } : {}),
|
||||
...(input.project_id ? { siaxProject: { project_id: input.project_id } } : {}),
|
||||
...(input.run_facets ?? {}),
|
||||
},
|
||||
},
|
||||
job: { namespace: APP_ID, name: input.capability_id },
|
||||
inputs: (input.inputs ?? []).map(toDatasetRef),
|
||||
outputs: (input.outputs ?? []).map(toDatasetRef),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Record one run event. Never throws after domain work has completed.
|
||||
*
|
||||
* A DurableAud0Client returns an acknowledgement whose event_id is the
|
||||
* immutable `outbox:<id>` handle. A raw HTTP client returns AUD0's event id.
|
||||
* This lets callers distinguish durable queueing from live acknowledgement
|
||||
* without duplicating the outbox implementation in this layer.
|
||||
*/
|
||||
async emit(input: LineageEmitInput): Promise<LineageEmitResult> {
|
||||
const event = this.buildRunEvent(input);
|
||||
try {
|
||||
const ack = await this.aud0.ingestOpenLineageEvent(input.tenant_id, APP_ID, event);
|
||||
if (ack.event_id.startsWith("outbox:")) {
|
||||
return {
|
||||
emitted: false,
|
||||
queued: true,
|
||||
run_id: input.run_id,
|
||||
outbox_id: ack.event_id.slice("outbox:".length),
|
||||
};
|
||||
}
|
||||
return { emitted: true, queued: false, run_id: input.run_id };
|
||||
} catch (error) {
|
||||
return {
|
||||
emitted: false,
|
||||
queued: false,
|
||||
run_id: input.run_id,
|
||||
warning: `openlineage recording failed: ${safeWarning(error)}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* src/lineage/lineage-store.ts
|
||||
* =============================
|
||||
* P067 DoD: "source -> output lineage is QUERYABLE."
|
||||
*
|
||||
* LineageEmitter already emits OpenLineage run events to AUD0 and that does
|
||||
* not change — AUD0 stays the system of record. This class adds the missing
|
||||
* half: it flattens the same run event into (input, output) edges, persists
|
||||
* them, and answers both directions of the question.
|
||||
*
|
||||
* `RecordingLineageEmitter` is the composition point: it wraps the existing
|
||||
* emitter so every existing caller (QualityService, DatasetVersionService,
|
||||
* the ingestion runner) gets the local projection for free, without any of
|
||||
* them learning about a second store. Emission to AUD0 keeps its existing
|
||||
* never-throw contract; a projection write failure is likewise reported, not
|
||||
* thrown, because lineage is evidence of work that already happened.
|
||||
*/
|
||||
import crypto from "crypto";
|
||||
import type { IngestionRepository } from "../repositories/ingestion-repository";
|
||||
import type { LineageEdge, LineageQuery } from "./lineage-edge";
|
||||
import type { LineageEmitInput, LineageEmitResult, LineageEmitter } from "./lineage-emitter";
|
||||
import type { OpenLineageRunEvent } from "./openlineage";
|
||||
|
||||
/** Deterministic edge id: the same run re-emitted produces the same edges. */
|
||||
export function lineageEdgeId(runId: string, input: string, output: string): string {
|
||||
return `led_${crypto.createHash("sha256").update(`${runId}|${input}|${output}`).digest("hex").slice(0, 32)}`;
|
||||
}
|
||||
|
||||
function refOf(facets: Record<string, unknown> | undefined): string | undefined {
|
||||
const facet = facets?.siaxResourceRef as { ref?: string } | undefined;
|
||||
return typeof facet?.ref === "string" ? facet.ref : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten one OpenLineage run event into edges. A run with N inputs and M
|
||||
* outputs yields N*M edges; a run with no inputs or no outputs yields none
|
||||
* (there is no edge to draw), which is why the runner always emits both.
|
||||
*/
|
||||
export function edgesFromRunEvent(
|
||||
event: OpenLineageRunEvent,
|
||||
tenantId: string,
|
||||
correlationId?: string,
|
||||
): LineageEdge[] {
|
||||
const edges: LineageEdge[] = [];
|
||||
for (const input of event.inputs ?? []) {
|
||||
for (const output of event.outputs ?? []) {
|
||||
edges.push({
|
||||
edge_id: lineageEdgeId(event.run.runId, `${input.namespace}/${input.name}`, `${output.namespace}/${output.name}`),
|
||||
tenant_id: tenantId,
|
||||
run_id: event.run.runId,
|
||||
capability_id: event.job.name,
|
||||
input_namespace: input.namespace,
|
||||
input_name: input.name,
|
||||
input_ref: refOf(input.facets),
|
||||
output_namespace: output.namespace,
|
||||
output_name: output.name,
|
||||
output_ref: refOf(output.facets),
|
||||
event_time: event.eventTime,
|
||||
correlation_id: correlationId,
|
||||
});
|
||||
}
|
||||
}
|
||||
return edges;
|
||||
}
|
||||
|
||||
export class LineageStore {
|
||||
constructor(private readonly repo: IngestionRepository) {}
|
||||
|
||||
async record(event: OpenLineageRunEvent, tenantId: string, correlationId?: string): Promise<number> {
|
||||
const edges = edgesFromRunEvent(event, tenantId, correlationId);
|
||||
if (edges.length > 0) await this.repo.appendLineageEdges(edges);
|
||||
return edges.length;
|
||||
}
|
||||
|
||||
/** Everything that fed `outputName`, newest first. */
|
||||
async upstreamOf(tenantId: string, outputName: string, limit?: number): Promise<LineageEdge[]> {
|
||||
return this.repo.queryLineage({ tenant_id: tenantId, output_name: outputName, limit });
|
||||
}
|
||||
|
||||
/** Everything `inputName` produced, newest first. */
|
||||
async downstreamOf(tenantId: string, inputName: string, limit?: number): Promise<LineageEdge[]> {
|
||||
return this.repo.queryLineage({ tenant_id: tenantId, input_name: inputName, limit });
|
||||
}
|
||||
|
||||
async query(query: LineageQuery): Promise<LineageEdge[]> {
|
||||
return this.repo.queryLineage(query);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A LineageEmitter that also writes the local projection. Drop-in for the
|
||||
* existing emitter: same `emit` signature, same never-throw contract.
|
||||
*/
|
||||
export class RecordingLineageEmitter {
|
||||
constructor(
|
||||
private readonly inner: LineageEmitter,
|
||||
private readonly store: LineageStore,
|
||||
) {}
|
||||
|
||||
buildRunEvent(input: LineageEmitInput): OpenLineageRunEvent {
|
||||
return this.inner.buildRunEvent(input);
|
||||
}
|
||||
|
||||
async emit(input: LineageEmitInput): Promise<LineageEmitResult & { projected: number }> {
|
||||
const event = this.inner.buildRunEvent(input);
|
||||
const result = await this.inner.emit(input);
|
||||
let projected = 0;
|
||||
try {
|
||||
projected = await this.store.record(event, input.tenant_id, input.correlation_id);
|
||||
} catch (err) {
|
||||
return {
|
||||
...result,
|
||||
projected: 0,
|
||||
warning: [result.warning, `lineage projection write failed: ${err instanceof Error ? err.message : String(err)}`]
|
||||
.filter(Boolean)
|
||||
.join("; "),
|
||||
};
|
||||
}
|
||||
return { ...result, projected };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* src/lineage/openlineage.ts
|
||||
* ===========================
|
||||
* Minimal, spec-shaped OpenLineage types for 0pt's lineage emission
|
||||
* (Master-DoD §10 "Lineage: emit OpenLineage-events till AUD0:s
|
||||
* ingest-kontrakt", §16 "OpenLineage: Run/Job/Dataset ingest").
|
||||
*
|
||||
* This is a deliberate SUBSET of the OpenLineage 1.x spec
|
||||
* (https://openlineage.io/spec/2-0-2/OpenLineage.json): the fields AUD0's
|
||||
* `/v1/aud0/openlineage` ingest reads (eventType, eventTime, run.runId,
|
||||
* job.namespace/name, inputs[], outputs[], facets passed through opaquely).
|
||||
* Facets are open `Record<string, unknown>` — AUD0 stores them verbatim as
|
||||
* evidence; custom SIAX facets are namespaced `siax*`.
|
||||
*/
|
||||
|
||||
export type OpenLineageEventType = "START" | "RUNNING" | "COMPLETE" | "ABORT" | "FAIL" | "OTHER";
|
||||
|
||||
export interface OpenLineageDatasetRef {
|
||||
/** Dataset namespace, e.g. "0pt" or a source system identifier. */
|
||||
namespace: string;
|
||||
/** Dataset name, e.g. "dataset/ds_x" or "opensearch/opt-search-companies". */
|
||||
name: string;
|
||||
facets?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface OpenLineageRunEvent {
|
||||
eventType: OpenLineageEventType;
|
||||
/** ISO-8601 timestamp of the state transition. */
|
||||
eventTime: string;
|
||||
/** Producer URI identifying the emitting code base. */
|
||||
producer: string;
|
||||
/** Spec schema URL — optional; AUD0 does not require it. */
|
||||
schemaURL?: string;
|
||||
run: {
|
||||
/** Stable run id — 0pt uses deterministic ids so re-emission dedups. */
|
||||
runId: string;
|
||||
facets?: Record<string, unknown>;
|
||||
};
|
||||
job: {
|
||||
/** Job namespace — 0pt emits its app id ("0pt"). */
|
||||
namespace: string;
|
||||
/** Job name — the 0pt capability id, e.g. "0pt.dataset.version.v1". */
|
||||
name: string;
|
||||
facets?: Record<string, unknown>;
|
||||
};
|
||||
inputs?: OpenLineageDatasetRef[];
|
||||
outputs?: OpenLineageDatasetRef[];
|
||||
}
|
||||
|
||||
export const OPT_OPENLINEAGE_PRODUCER = "https://git.siax.io/sax3l/0pt";
|
||||
Reference in New Issue
Block a user