/** * src/types/ingestion.ts * ======================= * P062: THE common ingestion envelope. Every record that enters 0pt through * a connector — batch page, stream message, or change event — is normalized * into exactly this shape before anything downstream (quality gate, entity * resolution, dataset write, lineage) sees it. Nothing downstream is allowed * to special-case "this came from a file" vs "this came from a change feed"; * that difference is expressed by `mode` + `operation` and nothing else. * * HONEST SCOPE NOTE. `mode: "cdc"` and the delete/update operations are part * of the CONTRACT, and the runner handles them (a delete envelope carries no * payload and is never quality-gated as if it had one). This repo does NOT * ship a CDC *source* — there is no Debezium consumer, no replication-slot * reader, no WAL tailer here, exactly as services/api/tests/test_known_gaps.py * has stated since P010. What changed with P062 is that a CDC connector can * now be added without redesigning the pipeline, and that the checkpoint it * would resume from is durable. A CDC connector itself is still MISSING. * * `checkpoint` on a ReadResult is what makes resume real: the connector hands * back the cursor that means "everything up to and including these envelopes * has been produced", the runner commits it durably AFTER the envelopes are * accepted, and a restart passes it straight back into read(). Committing it * before acceptance would create silent gaps — that ordering is the whole * point and is asserted in tests/p0/ingestion-runner.test.ts. */ /** How data arrives from the source. */ export type IngestionMode = "batch" | "stream" | "cdc"; /** * What the record means. Batch/stream sources normally emit "upsert" * (they carry current state); a change feed distinguishes the three CDC * operations. "delete" envelopes carry `payload: null`. */ export type EnvelopeOperation = "upsert" | "insert" | "update" | "delete"; /** Per-envelope provenance — which code, which version, which run produced it. */ export interface EnvelopeProvenance { connector_id: string; connector_version: string; /** The ingestion run that produced this envelope. Joins to lineage + checkpoints. */ run_id: string; /** Opaque connector cursor at which this record was produced. */ cursor?: string; } /** One normalized record. `payload` is null only for `operation: "delete"`. */ export interface IngestionEnvelope { envelope_id: string; source_id: string; tenant_id: string; project_id: string; mode: IngestionMode; operation: EnvelopeOperation; /** Partition/stream within the source (a table, a topic, a file). */ partition_key: string; /** Stable business key within the partition — the unit of idempotency (P068). */ record_key: string; /** Schema version the payload conforms to (P063). */ schema_version: number; /** Monotonic per-(source, partition) sequence assigned by the runner. */ sequence: number; /** When the change happened upstream, when known. */ occurred_at?: string; /** When 0pt normalized it. */ ingested_at: string; payload: Record | null; provenance: EnvelopeProvenance; } /** * The durable resume point for one (source, partition). Committed only after * the envelopes it covers have been accepted downstream. */ export interface IngestionCheckpoint { source_id: string; tenant_id: string; partition_key: string; /** Opaque connector cursor. `undefined` means "never read". */ cursor?: string; /** Highest envelope sequence committed for this partition. */ sequence: number; records_committed: number; committed_at: string; } /** * P064: an envelope the declared quality policy sent to quarantine instead of * downstream. Quarantine is DURABLE and inspectable — a dropped record that * only exists in a log line is a silently lost record. */ export interface QuarantineRecord { quarantine_id: string; source_id: string; tenant_id: string; run_id: string; record_key: string; /** One entry per failed expectation. */ violations: Array<{ expectation_type: string; detail: string }>; envelope: IngestionEnvelope; quarantined_at: string; } /** P068: what the idempotency ledger decided about one delivered record. */ export type LedgerVerdict = "new" | "changed" | "duplicate"; export interface LedgerDecision { record_key: string; verdict: LedgerVerdict; }