/** * src/quality/quality-policy.ts * ============================== * P064: the DECLARED enforcement policy — what happens to a record that fails * its quality rules — and the gate that applies it on the ingestion path. * * WHAT EXISTED BEFORE (and is reused, not rebuilt): QualityService already * owns the rule registry, runs, results, issues and scores, and * ExpectationEngine already executes the Great Expectations core subset. What * neither of them had was a CONSEQUENCE: a failing rule produced an ISSUE for * a human, and the data flowed on regardless. That is the gap this closes. * This module deliberately does NOT re-implement rule storage or execution — * it composes ExpectationEngine and adds the decision. * * Three declared actions, and nothing implicit: * - `block` — the whole page fails; the checkpoint is NOT advanced, so * the run stops on the bad data instead of stepping over it. * - `quarantine` — the failing envelopes are persisted to durable quarantine * and excluded from the output; clean envelopes continue and * the checkpoint advances past all of them. * - `warn` — everything continues; violations are counted and reported. * * There is no default. A source with no policy gets `block` from the runner * only if a policy was supplied; with no policy at all the runner skips the * gate and SAYS SO in the run report (`quality_policy: "none_declared"`) — * an undeclared policy must never look like a passed one. */ import { ExpectationEngine } from "./expectation-engine"; import type { QualityExpectation } from "./quality-types"; import type { IngestionEnvelope } from "../types/ingestion"; export type QualityEnforcementAction = "block" | "quarantine" | "warn"; export interface QualityEnforcementPolicy { source_id: string; tenant_id: string; /** Rules applied per record. Expectations are evaluated against a one-row table. */ expectations: QualityExpectation[]; on_violation: QualityEnforcementAction; } export interface EnvelopeViolation { expectation_type: string; detail: string; } export interface QualityGateResult { /** Envelopes that may proceed downstream. */ accepted: IngestionEnvelope[]; /** Envelopes routed to quarantine (only under `quarantine`). */ quarantined: Array<{ envelope: IngestionEnvelope; violations: EnvelopeViolation[] }>; /** Every violation seen, including under `warn` where nothing was withheld. */ violations: Array<{ record_key: string; violations: EnvelopeViolation[] }>; /** True when `block` fired: the caller must NOT advance the checkpoint. */ blocked: boolean; action: QualityEnforcementAction; } export class QualityGate { constructor(private readonly engine: ExpectationEngine = new ExpectationEngine()) {} /** * Evaluate one page of envelopes against the policy. * * Delete envelopes (payload null) are passed through untouched: a CDC delete * carries no columns to check, and failing it against a column expectation * would quarantine the very deletions a source is asking us to honour. */ evaluate(envelopes: IngestionEnvelope[], policy: QualityEnforcementPolicy): QualityGateResult { const accepted: IngestionEnvelope[] = []; const quarantined: QualityGateResult["quarantined"] = []; const allViolations: QualityGateResult["violations"] = []; for (const envelope of envelopes) { if (envelope.payload === null) { accepted.push(envelope); continue; } const violations = this.violationsFor(envelope.payload, policy.expectations); if (violations.length === 0) { accepted.push(envelope); continue; } allViolations.push({ record_key: envelope.record_key, violations }); if (policy.on_violation === "quarantine") { quarantined.push({ envelope, violations }); } else if (policy.on_violation === "warn") { accepted.push(envelope); } // `block`: neither accepted nor quarantined — the caller stops. } const blocked = policy.on_violation === "block" && allViolations.length > 0; return { accepted: blocked ? [] : accepted, quarantined, violations: allViolations, blocked, action: policy.on_violation, }; } private violationsFor( payload: Record, expectations: QualityExpectation[], ): EnvelopeViolation[] { const violations: EnvelopeViolation[] = []; for (const expectation of expectations) { if (!this.engine.supports(expectation.expectation_type)) { // An expectation this engine cannot execute is a VIOLATION of the // policy's executability, not a silent pass. The record is treated as // failing so an unrunnable rule can never look like a satisfied one. violations.push({ expectation_type: expectation.expectation_type, detail: `expectation is not executable by the local engine; delegate it to the GE backend before declaring it in an enforcement policy`, }); continue; } const outcome = this.engine.execute(expectation, [payload]); if (!outcome.success) { violations.push({ expectation_type: expectation.expectation_type, detail: `failed on column "${String(expectation.kwargs.column ?? "")}" (${outcome.unexpected_count}/${outcome.evaluated_count} unexpected)`, }); } } return violations; } }