/** * src/schema/schema-registry.ts * ============================== * P063: versioning + compatibility policy for a source's record shape. * * The DoD is "a breaking schema is detected BEFORE consumer corruption", so * this registry is placed on the ingestion path in two places, both of them * upstream of any consumer write: * * 1. `registerFromDiscovery` runs when the runner calls connector.discover(), * i.e. before the first read. A shape change that breaks the declared * policy makes the RUN fail with the field-level reasons — no envelope is * produced at all. * 2. `conformanceErrors` (src/schema/compatibility.ts) runs per envelope, so * a source that changes shape mid-stream without changing its declared * schema is caught on the offending record rather than on the next * discovery, days later. * * A source whose policy is "none" opts out of (1) — explicitly, recorded in * the registry row, never by default. */ import { SiaxError } from "../platform/errors"; import type { IngestionRepository } from "../repositories/ingestion-repository"; import { checkCompatibility } from "./compatibility"; import type { CompatibilityPolicy, CompatibilityVerdict, RecordSchema, RecordSchemaFields, } from "./record-schema"; export interface RegisterSchemaInput { source_id: string; tenant_id: string; fields: RecordSchemaFields; /** Policy governing THIS and future versions. Required — there is no default. */ compatibility: CompatibilityPolicy; registered_by?: string; } export interface RegisterSchemaResult { schema: RecordSchema; /** True when the shape was identical to the current version and no new version was created. */ unchanged: boolean; /** Present when a comparison ran (i.e. a previous version existed). */ verdict?: CompatibilityVerdict; } /** Stable comparison of two field maps, order-independent. */ function sameFields(a: RecordSchemaFields, b: RecordSchemaFields): boolean { const ka = Object.keys(a).sort(); const kb = Object.keys(b).sort(); if (ka.length !== kb.length) return false; for (let i = 0; i < ka.length; i++) { if (ka[i] !== kb[i]) return false; const fa = a[ka[i]]; const fb = b[kb[i]]; if (fa.type !== fb.type || fa.required !== fb.required) return false; } return true; } export class SchemaRegistry { constructor(private readonly repo: IngestionRepository) {} async getLatest(sourceId: string): Promise { return this.repo.getLatestSchema(sourceId); } async list(sourceId: string): Promise { return this.repo.listSchemas(sourceId); } /** * Register a shape. Throws `SiaxError("CONFLICT")` — naming every break — * when the new shape violates the previous version's declared policy. * Nothing is persisted when it throws. */ async register(input: RegisterSchemaInput): Promise { if (Object.keys(input.fields).length === 0) { throw new SiaxError("INVALID_REQUEST", "0pt.schema_registry", "a schema must declare at least one field"); } const previous = await this.repo.getLatestSchema(input.source_id); if (!previous) { const schema: RecordSchema = { source_id: input.source_id, tenant_id: input.tenant_id, schema_version: 1, fields: input.fields, compatibility: input.compatibility, registered_at: new Date().toISOString(), registered_by: input.registered_by, }; await this.repo.putSchema(schema); return { schema, unchanged: false }; } if (sameFields(previous.fields, input.fields) && previous.compatibility === input.compatibility) { return { schema: previous, unchanged: true }; } // The PREVIOUS version's policy governs whether this change is allowed — // a producer must not be able to widen its own policy retroactively by // declaring "none" on the breaking version. const verdict = checkCompatibility(previous.fields, input.fields, previous.compatibility); if (!verdict.compatible) { throw new SiaxError( "CONFLICT", "0pt.schema_registry", `schema v${previous.schema_version + 1} for source ${input.source_id} breaks the declared ` + `"${previous.compatibility}" compatibility policy: ` + verdict.breaks.map((b) => `[${b.breaks}] ${b.detail}`).join("; "), ); } const schema: RecordSchema = { source_id: input.source_id, tenant_id: input.tenant_id, schema_version: previous.schema_version + 1, fields: input.fields, compatibility: input.compatibility, registered_at: new Date().toISOString(), registered_by: input.registered_by, }; await this.repo.putSchema(schema); return { schema, unchanged: false, verdict }; } /** * Non-throwing form used by the runner's pre-flight: answers whether the * shape a connector just discovered would be accepted, without registering * it. Used to report a break as a RUN failure with a full reason list * rather than an exception message. */ async evaluate( sourceId: string, fields: RecordSchemaFields, ): Promise<{ previous?: RecordSchema; verdict?: CompatibilityVerdict; changed: boolean }> { const previous = await this.repo.getLatestSchema(sourceId); if (!previous) return { changed: true }; if (sameFields(previous.fields, fields)) return { previous, changed: false }; return { previous, verdict: checkCompatibility(previous.fields, fields, previous.compatibility), changed: true }; } }