feat(analytics): BI-P025/BI-P026 — quality providers that fail on empty, freshness that refuses to be silent
CI (SIAX Cloud) / integration (push) Successful in 43s
CI (SIAX Cloud) / security (push) Successful in 23s
CI (SIAX Cloud) / quality (push) Successful in 57s
Security Scan (SIAX Cloud) / security (push) Successful in 21s
CI (SIAX Cloud) / security (pull_request) Successful in 23s
CI (SIAX Cloud) / integration (pull_request) Successful in 40s
CI (SIAX Cloud) / quality (pull_request) Successful in 55s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 25s

Both prompts are about a FALSE GREEN, so both are tested by constructing the
false green and requiring a failure.

BI-P025. Three provider kinds, and the interface makes the difference visible
instead of implying parity: `providerKind` is "executing" or "ingesting". The
native provider runs checks; the dbt and Great Expectations adapters INGEST
results produced elsewhere (n0d#38 owns dbt execution against BI-P022, and GE
is Python — nothing here runs it). Calling all three "providers" while only
one executes is how "we have Great Expectations coverage" ends up in a status
report.

THE FALSE GREEN, proven against a live ClickHouse 24.8.14.39: a `not_null`
check over an EMPTY table returns violations=0, evaluated=0. The raw counts
are asserted first — the engine really does say zero-over-zero — and then the
provider is required to call it a FAILURE, not a pass. Every rule compiles to
a VIOLATION expression rather than a "rows that are fine" count, so the
distinction cannot be lost at the runner layer.

Same treatment for the ingesting providers: a MISSING dbt result is a failure,
and dbt's `error`, `warn` and `skipped` are each asserted not to be passes.
`skipped` in particular is the exact shape of l0re's 28 silently-skipped
Postgres tests.

The gate: zero verdicts QUARANTINES. "No rules ran" and "everything passed"
produce the same green in most implementations, which is how a quality gate
stops gating without anyone noticing — api0's `trivy --exit-code 0` in another
costume. A later passing run does not auto-release either; release takes a
principal and a reason, or a flapping rule would quarantine and un-quarantine
a dataset between two reads of the same dashboard.

BI-P025's DoD is the READ side: a quarantined projection is REFUSED, not
annotated. An annotation on a dashboard nobody reads is not a quarantine.

Live counts, all exact, from evidence/bi-p025/native-quality-live.json:
not_null 5/118, range 3, distribution 4, unique 11. The unique figure is 11
rather than the 6 the first version of the test asserted — the five
empty-string customers are duplicates of each other, so not_null and unique
overlap on the same defect. The engine was right and the fixture arithmetic
was wrong; the breakdown is written into the test rather than the number
quietly adjusted.

Tenant isolation re-proven on this path specifically, because a quality run
uses an ADMINISTRATIVE credential: with 618 rows in the base table, t_alpha
evaluates 118 and t_beta 500, and t_beta's blocking failure quarantines t_beta
alone. Without the forced predicate one tenant's dirty rows would quarantine
another tenant's projection.

BI-P026. Five states, and the DoD's operative word is "silently". So the gate
sits at the boundary every result crosses: a result with NO freshness envelope
is unreleasable — not defaulted to FRESH, not defaulted to UNKNOWN — because
"we forgot to attach it" and "the data is current" are indistinguishable to a
reader, and any default makes forgetting the silent path. A non-current state
without an explicit acknowledgement is refused, so a consumer that ignores the
envelope cannot render stale numbers as current.

FAILED outranks age: a pipeline that just failed may still hold
recently-timestamped data from the previous run, and reporting that as FRESH
is how a broken pipeline stays invisible for a day. UNKNOWN is never collapsed
into FRESH.

54 unit tests, 11 live tests. typecheck + eslint clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Claude Opus 5
2026-09-07 02:40:35 +02:00
parent 65cce86144
commit 6cb00add57
7 changed files with 1583 additions and 0 deletions
@@ -0,0 +1,127 @@
/**
* src/analytics/quality/clickhouse-quality-runner.ts
* ===================================================
* BI-P025 — the ClickHouse `QualityQueryRunner`.
*
* One statement per rule, and it counts BOTH numbers in a single pass:
*
* SELECT countIf(<violation expression>) AS violations, count() AS evaluated
* FROM <db>.<table> WHERE tenant_id = {tenant:String} AND dataset_id = …
*
* `evaluated` is not decoration. It is what makes an EMPTY dataset
* distinguishable from a CLEAN one — the false green
* `NativeQualityProvider` refuses. Counting only violations would make the two
* cases identical at this layer, and no amount of care upstream could recover
* the difference.
*
* The BI-P011 forced tenant predicate applies here exactly as it does to a
* metric query: `tenant_id = {tenant:String}` is condition[0], the value is
* bound, and a quality run therefore cannot read another tenant's rows even
* though it runs on an administrative credential.
*
* `unique` gets its own statement shape, because a duplicate is a property of
* a GROUP, not of a row, and pretending otherwise would make the rule count
* the wrong thing.
*/
import { SiaxError } from '../../platform/errors';
import { assertSafeIdentifier, FORCED_TENANT_PREDICATE } from '../engine/query-compiler';
import type { QualityQueryRunner } from './quality-provider';
export interface ClickHouseQualityRunnerOptions {
baseUrl: string;
basicAuth?: string;
database: string;
/** dataset_id -> physical table. An unlisted dataset is refused, not guessed. */
tables: Record<string, string>;
timeoutMs?: number;
/** Ceiling for a quality scan; a check must not become the expensive query. */
maxRowsToRead?: number;
}
/** Marker the provider emits for the `unique` rule. */
const UNIQUE_MARKER = /^__unique__\(([A-Za-z_][A-Za-z0-9_]{0,62})\)$/;
export class ClickHouseQualityRunner implements QualityQueryRunner {
readonly engine = 'clickhouse';
constructor(private readonly opts: ClickHouseQualityRunnerOptions) {
assertSafeIdentifier(opts.database, 'database');
for (const table of Object.values(opts.tables)) assertSafeIdentifier(table, 'table');
}
async count(args: {
tenant_id: string;
dataset_id: string;
violationExpression: string;
params: Record<string, string>;
}): Promise<{ violations: number; evaluated: number }> {
if (!args.tenant_id?.trim()) {
throw new SiaxError(
'UNAUTHENTICATED',
'0pt.analytics.quality',
'a quality run requires a verified tenant',
);
}
const table = this.opts.tables[args.dataset_id];
if (!table) {
throw new SiaxError(
'NOT_FOUND',
'0pt.analytics.quality',
`dataset "${args.dataset_id}" has no physical table registered for quality runs`,
);
}
const params: Record<string, string> = {
...args.params,
param_tenant: args.tenant_id,
param_dataset: args.dataset_id,
};
const where = `${FORCED_TENANT_PREDICATE} AND dataset_id = {dataset:String}`;
const unique = UNIQUE_MARKER.exec(args.violationExpression.trim());
const sql = unique
? // A duplicate is a property of a GROUP. Count the rows that belong to a
// key appearing more than once, and the total rows, in one pass.
`SELECT sum(multiIf(c > 1, c, 0)) AS violations, sum(c) AS evaluated FROM ` +
`(SELECT ${assertSafeIdentifier(unique[1], 'field')} AS k, count() AS c ` +
`FROM ${this.opts.database}.${table} WHERE ${where} GROUP BY k) FORMAT JSON`
: `SELECT countIf(${args.violationExpression}) AS violations, count() AS evaluated ` +
`FROM ${this.opts.database}.${table} WHERE ${where} FORMAT JSON`;
const url = new URL(this.opts.baseUrl.replace(/\/+$/, ''));
url.searchParams.set('query', sql);
for (const [k, v] of Object.entries(params)) url.searchParams.set(k, v);
url.searchParams.set('max_rows_to_read', String(this.opts.maxRowsToRead ?? 5_000_000_000));
url.searchParams.set('read_overflow_mode', 'throw');
url.searchParams.set('max_execution_time', '60');
url.searchParams.set('timeout_overflow_mode', 'throw');
const headers: Record<string, string> = {};
if (this.opts.basicAuth) {
headers.Authorization = `Basic ${Buffer.from(this.opts.basicAuth).toString('base64')}`;
}
let res: Response;
try {
res = await fetch(url.toString(), {
method: 'POST',
headers,
body: '',
signal: AbortSignal.timeout(this.opts.timeoutMs ?? 60_000),
});
} catch (cause) {
throw new SiaxError('DEPENDENCY_UNAVAILABLE', 'clickhouse', 'clickhouse unreachable', { cause });
}
const text = await res.text();
if (!res.ok) {
const code = /Code:\s*(\d+)/.exec(text)?.[1] ?? String(res.status);
throw new SiaxError('PERMANENT_FAILURE', 'clickhouse', `quality query failed (CH code ${code})`);
}
const row = (
JSON.parse(text) as { data: Array<{ violations: string | number; evaluated: string | number }> }
).data[0];
if (!row) {
throw new SiaxError('PERMANENT_FAILURE', 'clickhouse', 'quality query returned no counts');
}
return { violations: Number(row.violations ?? 0), evaluated: Number(row.evaluated ?? 0) };
}
}
+196
View File
@@ -0,0 +1,196 @@
/**
* src/analytics/quality/freshness.ts
* ===================================
* BI-P026 — DATA FRESHNESS.
*
* Five states, exactly as BI-P026 names them, and the DoD is a NEGATIVE:
*
* "Stale data is never SILENTLY presented as current."
*
* Two words carry it. "Silently" means the failure mode is not "we served old
* data" — it is "we served old data and the reader could not tell". And
* "never" means the enforcement has to sit at the boundary every result
* crosses, not in each dashboard's rendering code.
*
* So `assertFreshnessDisclosed` refuses to release a result that carries no
* freshness envelope at all. A result with no envelope is not FRESH and not
* UNKNOWN — it is unreleasable, because "we forgot to attach it" and "the data
* is current" are indistinguishable to a reader, and a default of FRESH would
* make forgetting the silent path.
*
* UNKNOWN is likewise never treated as FRESH. It is its own state precisely
* because "we could not determine the age" is the honest thing to display, and
* collapsing it into FRESH is the classic version of this bug.
*/
import { SiaxError } from '../../platform/errors';
/** BI-P026's states, verbatim and in severity order. */
export const FRESHNESS_STATES = ['FRESH', 'DELAYED', 'STALE', 'FAILED', 'UNKNOWN'] as const;
export type FreshnessState = (typeof FRESHNESS_STATES)[number];
/** States a reader must be shown explicitly rather than served as current. */
export const NON_CURRENT_STATES: readonly FreshnessState[] = ['DELAYED', 'STALE', 'FAILED', 'UNKNOWN'];
export interface FreshnessPolicy {
/** How often the upstream is expected to land data. */
expectedIntervalMs: number;
/**
* Multiples of the expected interval. Past `delayedAfter` the data is
* DELAYED; past `staleAfter` it is STALE. Defaults are deliberately tight:
* a dashboard that is two cycles behind is already misleading someone.
*/
delayedAfterIntervals?: number;
staleAfterIntervals?: number;
}
export interface FreshnessInput {
/** When the newest successfully loaded data is timestamped. */
lastSuccessfulLoadAt?: string;
/** Outcome of the most recent load attempt. */
lastRunStatus?: 'succeeded' | 'failed' | 'running' | 'never_run';
lastRunAt?: string;
policy: FreshnessPolicy;
now: number;
}
export interface FreshnessEnvelope {
state: FreshnessState;
/** Age of the newest data, in ms. Absent when unknown. */
age_ms?: number;
last_successful_load_at?: string;
last_run_at?: string;
expected_interval_ms: number;
/** Plain-language line a UI can render verbatim. */
disclosure: string;
computed_at: string;
}
export function computeFreshness(input: FreshnessInput): FreshnessEnvelope {
const { policy, now } = input;
const delayedAfter = policy.expectedIntervalMs * (policy.delayedAfterIntervals ?? 1.5);
const staleAfter = policy.expectedIntervalMs * (policy.staleAfterIntervals ?? 3);
const computed_at = new Date(now).toISOString();
const base = {
last_successful_load_at: input.lastSuccessfulLoadAt,
last_run_at: input.lastRunAt,
expected_interval_ms: policy.expectedIntervalMs,
computed_at,
};
// FAILED outranks age: a pipeline that just failed may still have
// recently-timestamped data from the run before, and reporting that as FRESH
// is precisely how a broken pipeline stays invisible for a day.
if (input.lastRunStatus === 'failed') {
const age = ageOf(input.lastSuccessfulLoadAt, now);
return {
...base,
state: 'FAILED',
age_ms: age,
disclosure: `The most recent load FAILED. Showing data from ${input.lastSuccessfulLoadAt ?? 'an unknown time'}.`,
};
}
if (!input.lastSuccessfulLoadAt || input.lastRunStatus === 'never_run') {
return {
...base,
state: 'UNKNOWN',
disclosure: 'The age of this data could not be determined. Do not read it as current.',
};
}
const age = ageOf(input.lastSuccessfulLoadAt, now);
if (age === undefined) {
return { ...base, state: 'UNKNOWN', disclosure: 'The age of this data could not be determined.' };
}
if (age > staleAfter) {
return {
...base,
state: 'STALE',
age_ms: age,
disclosure: `This data is ${describeAge(age)} old, past the ${describeAge(staleAfter)} staleness threshold.`,
};
}
if (age > delayedAfter) {
return {
...base,
state: 'DELAYED',
age_ms: age,
disclosure: `This data is ${describeAge(age)} old and behind its ${describeAge(policy.expectedIntervalMs)} schedule.`,
};
}
return {
...base,
state: 'FRESH',
age_ms: age,
disclosure: `Current as of ${input.lastSuccessfulLoadAt}.`,
};
}
function ageOf(iso: string | undefined, now: number): number | undefined {
if (!iso) return undefined;
const t = Date.parse(iso);
if (Number.isNaN(t)) return undefined;
return Math.max(0, now - t);
}
function describeAge(ms: number): string {
const s = Math.round(ms / 1000);
if (s < 90) return `${s}s`;
const m = Math.round(s / 60);
if (m < 90) return `${m}m`;
const h = Math.round(m / 60);
if (h < 48) return `${h}h`;
return `${Math.round(h / 24)}d`;
}
/** Any result leaving the analytics boundary. */
export interface DisclosableResult {
freshness?: FreshnessEnvelope;
/**
* Set by a caller that has SEEN a non-current state and chosen to proceed.
* Its presence is what makes serving non-current data not-silent.
*/
acknowledged_non_current?: boolean;
}
/**
* BI-P026's DoD, as a gate.
*
* Three refusals, in order of how easy they are to get wrong:
* 1. no envelope at all — unreleasable. Not defaulted to FRESH.
* 2. a non-current state with no acknowledgement — refused, so a consumer
* that ignores the envelope cannot render stale numbers as current.
* 3. an acknowledgement with no envelope — refused, because acknowledging
* something you were never shown is not disclosure.
*/
export function assertFreshnessDisclosed(result: DisclosableResult, context: string): FreshnessEnvelope {
if (!result.freshness) {
throw new SiaxError(
'PERMANENT_FAILURE',
'0pt.analytics.freshness',
`${context} carries no freshness envelope — BI-P026: data with unknown age must never be released ` +
`as though it were current`,
);
}
const state = result.freshness.state;
if (!(FRESHNESS_STATES as readonly string[]).includes(state)) {
throw new SiaxError(
'PERMANENT_FAILURE',
'0pt.analytics.freshness',
`${context} has an unknown freshness state`,
);
}
if (NON_CURRENT_STATES.includes(state) && !result.acknowledged_non_current) {
throw new SiaxError(
'CONFLICT',
'0pt.analytics.freshness',
`${context} is ${state} and the consumer has not acknowledged it: ${result.freshness.disclosure}`,
);
}
return result.freshness;
}
/** True when the state may be presented without a caveat. Only one state may. */
export function isPresentableAsCurrent(state: FreshnessState): boolean {
return state === 'FRESH';
}
+418
View File
@@ -0,0 +1,418 @@
/**
* src/analytics/quality/quality-provider.ts
* ==========================================
* BI-P025 — DATA QUALITY PROVIDER.
*
* One interface, three provider kinds, and an honest split between them:
*
* NATIVE — this module compiles the rule into a counting query and runs it
* through an injected runner. It EXECUTES.
* DBT — dbt runs its own tests elsewhere (n0d#38 owns execution against
* the BI-P022 contract). This module INGESTS the results. It does
* not run dbt, and `providerKind` says so rather than implying
* otherwise.
* GREAT_EXPECTATIONS — same posture. GE is a Python library; nothing here
* executes it. The adapter maps a GE validation result onto the
* canonical verdict so a GE suite counts toward the same gate.
*
* Calling all three "providers" while only one executes would be the kind of
* overstatement that turns into "we have Great Expectations coverage" in a
* status report. `SiaxDataQualityProvider.providerKind` is `"executing"` or
* `"ingesting"`, and the gate records which produced each verdict.
*
* ── BI-P025's DoD ─────────────────────────────────────────────────────────
* "Failed critical quality gate can QUARANTINE serving projection."
*
* `quarantine.ts` is that, and the important half is what happens on the READ
* side: a quarantined projection is REFUSED, not annotated. An annotation on a
* dashboard nobody reads is not a quarantine.
*/
import { SiaxError } from '../../platform/errors';
import type { QualityRuleKind, QualitySeverity } from '../domain/entities';
/** BI-P025's rule list, verbatim, mapped onto BI-P002's persisted kinds. */
export const QUALITY_RULES = [
'not_null',
'unique',
'range',
'referential',
'freshness',
'schema',
'distribution',
'custom',
] as const;
export type QualityRule = (typeof QUALITY_RULES)[number];
/** BI-P002 persists `accepted_values` and `row_count`; BI-P025 names `distribution` and `range`. */
export const RULE_KIND_ALIASES: Record<QualityRule, QualityRuleKind> = {
not_null: 'not_null',
unique: 'unique',
range: 'range',
referential: 'referential',
freshness: 'freshness',
schema: 'custom',
distribution: 'accepted_values',
custom: 'custom',
};
export interface QualityRuleSpec {
rule_id: string;
tenant_id: string;
dataset_id: string;
rule: QualityRule;
/** Column the rule applies to; absent for dataset-level rules. */
field?: string;
severity: QualitySeverity;
params: QualityRuleParams;
description?: string;
}
export interface QualityRuleParams {
/** range */
min?: number;
max?: number;
/** distribution / accepted values */
accepted_values?: string[];
/** distribution: the share of rows that must fall in accepted_values */
min_share?: number;
/** referential */
references_dataset?: string;
references_field?: string;
/** freshness: the maximum acceptable age of the newest row */
max_age_seconds?: number;
timestamp_field?: string;
/** schema: the columns and types that must exist */
expected_columns?: Array<{ name: string; type: string }>;
/** unique / not_null take no parameters */
/** custom: an operator-authored counting query, reviewed like code */
custom_violation_sql?: string;
}
export interface QualityVerdict {
rule_id: string;
dataset_id: string;
tenant_id: string;
rule: QualityRule;
severity: QualitySeverity;
passed: boolean;
violation_count: number;
evaluated_rows: number;
/** Which provider produced this, and whether it EXECUTED or merely ingested. */
produced_by: string;
provider_kind: 'executing' | 'ingesting';
evaluated_at: string;
/** Short operator-facing note. Never raw engine output. */
detail?: string;
}
export interface SiaxDataQualityProvider {
readonly provider: string;
readonly providerKind: 'executing' | 'ingesting';
/** Rules this provider can actually evaluate. */
supports(rule: QualityRule): boolean;
evaluate(spec: QualityRuleSpec): Promise<QualityVerdict>;
}
/**
* Runs a counting query and returns two numbers. The only engine coupling in
* this module, so a DuckDB or Trino runner drops in unchanged.
*/
export interface QualityQueryRunner {
readonly engine: string;
/**
* Must apply the caller's tenant scoping. The runner is handed the tenant
* explicitly rather than trusting the SQL to carry it.
*/
count(args: {
tenant_id: string;
dataset_id: string;
violationExpression: string;
/** Bound parameters. Values NEVER appear in the expression text. */
params: Record<string, string>;
}): Promise<{ violations: number; evaluated: number }>;
}
const IDENT_RE = /^[A-Za-z_][A-Za-z0-9_]{0,62}$/;
function assertField(field: string | undefined, rule: QualityRule): string {
if (!field || !IDENT_RE.test(field)) {
throw new SiaxError(
'INVALID_REQUEST',
'0pt.analytics.quality',
`rule "${rule}" requires a valid field name`,
);
}
return field;
}
/**
* The provider that actually runs checks.
*
* Every rule compiles to a VIOLATION EXPRESSION — a boolean over one row — and
* the runner counts rows where it is true. That shape is chosen so a rule can
* never return "how many rows are fine", which is the number that looks
* healthy when a table is empty.
*/
export class NativeQualityProvider implements SiaxDataQualityProvider {
readonly provider = 'native';
readonly providerKind = 'executing' as const;
constructor(
private readonly runner: QualityQueryRunner,
private readonly now: () => number = Date.now,
) {}
supports(rule: QualityRule): boolean {
// `schema` and `referential` need catalog/second-table access the counting
// runner does not have; they are honestly unsupported here rather than
// silently passing.
return !['schema', 'referential'].includes(rule);
}
async evaluate(spec: QualityRuleSpec): Promise<QualityVerdict> {
if (!this.supports(spec.rule)) {
throw new SiaxError(
'CAPABILITY_UNAVAILABLE',
'0pt.analytics.quality',
`the native provider cannot evaluate "${spec.rule}" — use a dbt test or a Great Expectations suite`,
);
}
const { expression, params } = this.compile(spec);
const { violations, evaluated } = await this.runner.count({
tenant_id: spec.tenant_id,
dataset_id: spec.dataset_id,
violationExpression: expression,
params,
});
// An EMPTY dataset is not a passing dataset. A `not_null` check over zero
// rows finds zero violations and would report success while the pipeline
// is silently broken — the single most common false green in data quality.
const emptyIsFailure = spec.rule !== 'custom';
const passed = evaluated === 0 && emptyIsFailure ? false : violations === 0;
return {
rule_id: spec.rule_id,
dataset_id: spec.dataset_id,
tenant_id: spec.tenant_id,
rule: spec.rule,
severity: spec.severity,
passed,
violation_count: violations,
evaluated_rows: evaluated,
produced_by: `${this.provider}:${this.runner.engine}`,
provider_kind: this.providerKind,
evaluated_at: new Date(this.now()).toISOString(),
detail:
evaluated === 0 && emptyIsFailure
? 'dataset is EMPTY — reported as a failure, not as zero violations'
: undefined,
};
}
private compile(spec: QualityRuleSpec): { expression: string; params: Record<string, string> } {
const p = spec.params;
switch (spec.rule) {
case 'not_null': {
const f = assertField(spec.field, spec.rule);
return { expression: `${f} IS NULL OR ${f} = ''`, params: {} };
}
case 'unique': {
const f = assertField(spec.field, spec.rule);
// Counted over a grouped subquery by the runner; the expression names
// the key so the runner can build it.
return { expression: `__unique__(${f})`, params: {} };
}
case 'range': {
const f = assertField(spec.field, spec.rule);
if (p.min === undefined && p.max === undefined) {
throw new SiaxError(
'INVALID_REQUEST',
'0pt.analytics.quality',
'a range rule needs a min or a max',
);
}
const parts: string[] = [];
const params: Record<string, string> = {};
if (p.min !== undefined) {
parts.push(`${f} < {qmin:Float64}`);
params.param_qmin = String(p.min);
}
if (p.max !== undefined) {
parts.push(`${f} > {qmax:Float64}`);
params.param_qmax = String(p.max);
}
return { expression: parts.join(' OR '), params };
}
case 'freshness': {
const f = assertField(p.timestamp_field ?? spec.field, spec.rule);
if (!p.max_age_seconds || p.max_age_seconds <= 0) {
throw new SiaxError(
'INVALID_REQUEST',
'0pt.analytics.quality',
'a freshness rule needs max_age_seconds',
);
}
return {
expression: `${f} < now() - toIntervalSecond({qage:UInt64})`,
params: { param_qage: String(Math.floor(p.max_age_seconds)) },
};
}
case 'distribution': {
const f = assertField(spec.field, spec.rule);
if (!p.accepted_values?.length) {
throw new SiaxError(
'INVALID_REQUEST',
'0pt.analytics.quality',
'a distribution rule needs accepted_values',
);
}
const params: Record<string, string> = {};
const bound = p.accepted_values.map((v, i) => {
params[`param_qv${i}`] = v;
return `{qv${i}:String}`;
});
return { expression: `${f} NOT IN (${bound.join(', ')})`, params };
}
case 'custom': {
if (!p.custom_violation_sql?.trim()) {
throw new SiaxError(
'INVALID_REQUEST',
'0pt.analytics.quality',
'a custom rule needs custom_violation_sql',
);
}
// Deliberately NOT parsed or guarded here. A custom rule is
// operator-authored, reviewed like code, and stored in the rule table
// — not caller input. It never reaches this function from a request
// body; `QualityRuleSpec` rows are written by an admin path.
return { expression: p.custom_violation_sql, params: {} };
}
default:
throw new SiaxError('INVALID_REQUEST', '0pt.analytics.quality', `unsupported rule "${spec.rule}"`);
}
}
}
/**
* Ingests dbt test results. Does NOT run dbt — n0d#38 owns execution against
* the BI-P022 contract, and pretending otherwise here would misreport where
* the work happens.
*/
export interface DbtTestResult {
unique_id: string;
status: 'pass' | 'fail' | 'error' | 'warn' | 'skipped';
failures: number;
/** dbt does not report rows evaluated; absent, not zero. */
rows_evaluated?: number;
message?: string;
}
export class DbtQualityProvider implements SiaxDataQualityProvider {
readonly provider = 'dbt';
readonly providerKind = 'ingesting' as const;
constructor(
private readonly results: Map<string, DbtTestResult>,
private readonly now: () => number = Date.now,
) {}
supports(): boolean {
return true;
}
async evaluate(spec: QualityRuleSpec): Promise<QualityVerdict> {
const result = this.results.get(spec.rule_id);
if (!result) {
// A missing result is UNKNOWN, and unknown is a FAILURE for a blocking
// rule. Treating "no result" as "passed" is how a quality gate stops
// gating the moment the test stops running.
return {
rule_id: spec.rule_id,
dataset_id: spec.dataset_id,
tenant_id: spec.tenant_id,
rule: spec.rule,
severity: spec.severity,
passed: false,
violation_count: 0,
evaluated_rows: 0,
produced_by: this.provider,
provider_kind: this.providerKind,
evaluated_at: new Date(this.now()).toISOString(),
detail: 'no dbt result for this rule — reported as a failure, not as a pass',
};
}
return {
rule_id: spec.rule_id,
dataset_id: spec.dataset_id,
tenant_id: spec.tenant_id,
rule: spec.rule,
severity: spec.severity,
// `skipped` and `error` are NOT passes. dbt's `skipped` in particular is
// the shape of l0re's silently-skipped Postgres tests.
passed: result.status === 'pass',
violation_count: result.failures,
evaluated_rows: result.rows_evaluated ?? 0,
produced_by: `dbt:${result.unique_id}`,
provider_kind: this.providerKind,
evaluated_at: new Date(this.now()).toISOString(),
detail: result.status !== 'pass' ? `dbt status ${result.status}` : undefined,
};
}
}
/** Ingests a Great Expectations validation result. Does NOT run GE. */
export interface GreatExpectationsResult {
expectation_suite_name: string;
success: boolean;
results: Array<{
expectation_config: { expectation_type: string };
success: boolean;
result?: { unexpected_count?: number; element_count?: number };
}>;
}
export class GreatExpectationsQualityProvider implements SiaxDataQualityProvider {
readonly provider = 'great_expectations';
readonly providerKind = 'ingesting' as const;
constructor(
private readonly suites: Map<string, GreatExpectationsResult>,
private readonly now: () => number = Date.now,
) {}
supports(): boolean {
return true;
}
async evaluate(spec: QualityRuleSpec): Promise<QualityVerdict> {
const suite = this.suites.get(spec.rule_id);
const base = {
rule_id: spec.rule_id,
dataset_id: spec.dataset_id,
tenant_id: spec.tenant_id,
rule: spec.rule,
severity: spec.severity,
produced_by: this.provider,
provider_kind: this.providerKind,
evaluated_at: new Date(this.now()).toISOString(),
};
if (!suite) {
return {
...base,
passed: false,
violation_count: 0,
evaluated_rows: 0,
detail: 'no GE result — reported as a failure',
};
}
const unexpected = suite.results.reduce((a, r) => a + (r.result?.unexpected_count ?? 0), 0);
const elements = suite.results.reduce((a, r) => Math.max(a, r.result?.element_count ?? 0), 0);
return {
...base,
passed: suite.success,
violation_count: unexpected,
evaluated_rows: elements,
produced_by: `great_expectations:${suite.expectation_suite_name}`,
detail: suite.success ? undefined : 'GE suite failed',
};
}
}
+188
View File
@@ -0,0 +1,188 @@
/**
* src/analytics/quality/quarantine.ts
* ====================================
* BI-P025's DoD: "Failed critical quality gate can QUARANTINE serving
* projection."
*
* The word that matters is on the READ side. A quarantine that annotates a
* result is a label; a quarantine that REFUSES the read is a quarantine. This
* module does the second, and `assertProjectionReadable` is the single place
* every serving read passes through.
*
* Severity semantics, stated because they are the whole gate:
* `block` — a failure quarantines the projection. Reads are refused.
* `warn` — a failure is recorded and surfaced, and reads continue.
* A rule with no severity does not exist: `QualityRuleSpec.severity` is
* required, so "we forgot to mark it critical" cannot silently become `warn`.
*/
import { SiaxError } from '../../platform/errors';
import type { QualityVerdict } from './quality-provider';
export interface QuarantineRecord {
tenant_id: string;
dataset_id: string;
quarantined: boolean;
/** Rule ids whose blocking failure caused it. */
caused_by: string[];
since?: string;
released_at?: string;
released_by?: string;
/** Operator-facing summary. */
reason?: string;
}
export interface QuarantineStore {
get(tenantId: string, datasetId: string): Promise<QuarantineRecord | undefined>;
put(record: QuarantineRecord): Promise<void>;
}
export class InMemoryQuarantineStore implements QuarantineStore {
private readonly rows = new Map<string, QuarantineRecord>();
private key(t: string, d: string): string {
return `${t}::${d}`;
}
async get(t: string, d: string): Promise<QuarantineRecord | undefined> {
return this.rows.get(this.key(t, d));
}
async put(r: QuarantineRecord): Promise<void> {
this.rows.set(this.key(r.tenant_id, r.dataset_id), r);
}
}
export interface GateOutcome {
dataset_id: string;
tenant_id: string;
passed: boolean;
quarantined: boolean;
blocking_failures: QualityVerdict[];
warnings: QualityVerdict[];
evaluated: number;
}
export class QualityGate {
constructor(
private readonly store: QuarantineStore,
private readonly now: () => number = Date.now,
) {}
/**
* Apply a run's verdicts.
*
* A gate over ZERO verdicts does not pass. "No rules ran" and "every rule
* passed" produce the same green in most implementations, and that is how a
* quality gate stops gating without anyone noticing — the same shape as
* l0re's 28 silently-skipped tests and api0's `trivy --exit-code 0`.
*/
async apply(tenantId: string, datasetId: string, verdicts: QualityVerdict[]): Promise<GateOutcome> {
if (verdicts.length === 0) {
const record: QuarantineRecord = {
tenant_id: tenantId,
dataset_id: datasetId,
quarantined: true,
caused_by: [],
since: new Date(this.now()).toISOString(),
reason: 'no quality rules were evaluated — an ungated dataset is quarantined, not trusted',
};
await this.store.put(record);
return {
dataset_id: datasetId,
tenant_id: tenantId,
passed: false,
quarantined: true,
blocking_failures: [],
warnings: [],
evaluated: 0,
};
}
for (const v of verdicts) {
if (v.tenant_id !== tenantId || v.dataset_id !== datasetId) {
// A verdict from another tenant's run must never influence this gate.
throw new SiaxError(
'AUTHORIZATION_DENIED',
'0pt.analytics.quality',
'a quality verdict from a different tenant or dataset was supplied to this gate',
);
}
}
const blocking = verdicts.filter((v) => !v.passed && v.severity === 'block');
const warnings = verdicts.filter((v) => !v.passed && v.severity === 'warn');
if (blocking.length > 0) {
await this.store.put({
tenant_id: tenantId,
dataset_id: datasetId,
quarantined: true,
caused_by: blocking.map((v) => v.rule_id),
since: new Date(this.now()).toISOString(),
reason: `${blocking.length} blocking quality rule(s) failed`,
});
} else {
// Passing does NOT auto-release: releasing is an act with an operator
// behind it (`release()`), because a flapping rule would otherwise let a
// dataset quarantine and un-quarantine itself between reads.
const existing = await this.store.get(tenantId, datasetId);
if (!existing || !existing.quarantined) {
await this.store.put({
tenant_id: tenantId,
dataset_id: datasetId,
quarantined: false,
caused_by: [],
});
}
}
return {
dataset_id: datasetId,
tenant_id: tenantId,
passed: blocking.length === 0,
quarantined: blocking.length > 0 || (await this.isQuarantined(tenantId, datasetId)),
blocking_failures: blocking,
warnings,
evaluated: verdicts.length,
};
}
async isQuarantined(tenantId: string, datasetId: string): Promise<boolean> {
return (await this.store.get(tenantId, datasetId))?.quarantined ?? false;
}
/** Release requires an operator and a reason. It is not automatic. */
async release(tenantId: string, datasetId: string, by: string, reason: string): Promise<QuarantineRecord> {
if (!by?.trim() || !reason?.trim()) {
throw new SiaxError(
'INVALID_REQUEST',
'0pt.analytics.quality',
'releasing a quarantine requires a principal and a reason',
);
}
const record: QuarantineRecord = {
tenant_id: tenantId,
dataset_id: datasetId,
quarantined: false,
caused_by: [],
released_at: new Date(this.now()).toISOString(),
released_by: by,
reason,
};
await this.store.put(record);
return record;
}
/**
* The read-side gate. Every serving projection read passes through here.
* Refuses, rather than annotating — an annotation on a dashboard nobody
* reads is not a quarantine.
*/
async assertProjectionReadable(tenantId: string, datasetId: string): Promise<void> {
const record = await this.store.get(tenantId, datasetId);
if (record?.quarantined) {
throw new SiaxError(
'DEPENDENCY_UNAVAILABLE',
'0pt.analytics.quality',
`serving projection for "${datasetId}" is QUARANTINED: ${record.reason ?? 'failed quality gate'}`,
);
}
}
}