BI-P060/P061/P103/P104/P105 — dashboard model + versioning, BI SLO, benchmark harness, materialization #36

Merged
admin merged 6 commits from feat/bi-p060-p061-p103-p105-dashboards-slo-materialization into main 2026-09-07 02:53:38 +00:00
3 changed files with 1003 additions and 0 deletions
Showing only changes of commit ed6e62809d - Show all commits
File diff suppressed because it is too large Load Diff
+209
View File
@@ -0,0 +1,209 @@
/**
* src/analytics/slo/usage-events.ts
* ==================================
* BI-P103's measurement path: 0pt emits the AUD0 BI usage events (aud0#34,
* BI-P076) that the SLO evaluator then reads.
*
* aud0 says it plainly in docs/BI_ANALYTICS_USAGE.md: "Zero real producers
* emit these seven types today." An SLO computed from a second, private
* telemetry path would be a number nobody else can audit, so 0pt becomes a
* producer instead of inventing one.
*
* THE PAYLOAD IS AN ALLOWLIST, BUILT FIELD BY NAMED FIELD. AUD0's ingest
* refuses result-bearing payloads with a 400 and its schemas are `.strict()`,
* so a leak here is a 400 rather than a breach — but a producer that has to be
* refused is a producer that is broken. `biUsageEventsForExecution` therefore
* never spreads an execution record; it names each field, and
* tests/analytics/bi-slo.test.ts feeds it a record carrying SQL text, an error
* message, a tenant id and a result sample, then asserts none of it appears in
* the output.
*
* WHAT IS DELIBERATELY NOT SENT: `compiled_query_hash` goes out as
* `query_hash` (aud0 requires a 64-hex digest and nothing else), the statement
* never leaves 0pt at all, `error_code` travels but `error_message` does not,
* and `row_count` travels but no row does.
*/
/** The seven event types, named exactly as aud0 registers them. */
export const AUD0_BI_EVENT_TYPES = [
'aud0.analytics_query_started',
'aud0.analytics_query_completed',
'aud0.analytics_query_failed',
'aud0.dashboard_viewed',
'aud0.report_generated',
'aud0.dataset_exported',
'aud0.metric_queried',
] as const;
export type Aud0BiEventType = (typeof AUD0_BI_EVENT_TYPES)[number];
/** aud0's closed surface list (BI_SURFACES). */
export const BI_SURFACES = ['dashboard', 'report', 'explore', 'api', 'scheduled', 'notebook'] as const;
export type BiSurface = (typeof BI_SURFACES)[number];
export interface Aud0BiEvent {
event_type: Aud0BiEventType;
/** Bounded, allowlisted metadata. Never a result, a statement or a message. */
payload: Record<string, string | number | boolean | string[]>;
/** Occurrence time, used by the SLO window. */
occurred_at: string;
}
/**
* The 0pt-side view of one execution. Intentionally a SUPERSET of what may be
* emitted — it carries the fields 0pt genuinely has, including the ones that
* must never leave — so that the mapping below is a real filter rather than a
* rename.
*/
export interface ExecutionTelemetry {
query_id: string;
/** 64-hex; anything else is dropped rather than reshaped. */
compiled_query_hash?: string;
/** NEVER emitted. Present here because 0pt has it. */
compiled_sql?: string;
/** NEVER emitted. */
error_message?: string;
error_code?: string;
/** NEVER emitted — a consumer must not be able to name a tenant, BI-P016. */
tenant_id?: string;
dataset_id?: string;
engine?: string;
surface?: BiSurface;
state: 'succeeded' | 'failed' | 'cancelled' | 'running';
started_at: string;
finished_at?: string;
duration_ms?: number;
row_count?: number;
bytes_scanned?: number;
cache_hit?: boolean;
filter_keys?: string[];
/** NEVER emitted: filter VALUES are facts about a subject. */
filter_values?: Record<string, string[]>;
/** NEVER emitted. */
result_sample?: unknown;
metric_name?: string;
dimension_keys?: string[];
granularity?: string;
dashboard_id?: string;
widget_count?: number;
}
const SHA256 = /^[0-9a-f]{64}$/;
const MAX_KEY_NAMES = 50;
const MAX_SHORT_TEXT = 120;
function shortText(v: string | undefined): string | undefined {
if (!v) return undefined;
return v.slice(0, MAX_SHORT_TEXT);
}
function keyNames(v: string[] | undefined): string[] | undefined {
if (!v || v.length === 0) return undefined;
return v.slice(0, MAX_KEY_NAMES).map((k) => k.slice(0, MAX_SHORT_TEXT));
}
function put(
payload: Record<string, string | number | boolean | string[]>,
key: string,
value: string | number | boolean | string[] | undefined,
): void {
if (value === undefined) return;
payload[key] = value;
}
/**
* Map one execution to its AUD0 usage events.
*
* A running execution emits only `started`: a query that has not finished is
* neither a success nor a failure, and the SLO evaluator counts it as
* unresolved rather than quietly as one or the other.
*/
export function biUsageEventsForExecution(t: ExecutionTelemetry): Aud0BiEvent[] {
const base: Record<string, string | number | boolean | string[]> = {};
put(base, 'query_id', t.query_id);
if (t.compiled_query_hash && SHA256.test(t.compiled_query_hash)) {
put(base, 'query_hash', t.compiled_query_hash);
}
put(base, 'dataset_id', t.dataset_id);
put(base, 'engine', shortText(t.engine));
put(base, 'surface', t.surface);
put(base, 'filter_keys', keyNames(t.filter_keys));
const events: Aud0BiEvent[] = [
{ event_type: 'aud0.analytics_query_started', payload: { ...base }, occurred_at: t.started_at },
];
if (t.state === 'succeeded') {
const payload = { ...base };
put(payload, 'duration_ms', t.duration_ms);
put(payload, 'row_count', t.row_count);
put(payload, 'bytes_scanned', t.bytes_scanned);
put(payload, 'cache_hit', t.cache_hit);
events.push({
event_type: 'aud0.analytics_query_completed',
payload,
occurred_at: t.finished_at ?? t.started_at,
});
} else if (t.state === 'failed' || t.state === 'cancelled') {
const payload = { ...base };
put(payload, 'duration_ms', t.duration_ms);
// A CODE. `error_message` is never mapped, at any length.
put(
payload,
'error_code',
shortText(t.error_code ?? (t.state === 'cancelled' ? 'CANCELLED' : undefined)),
);
events.push({
event_type: 'aud0.analytics_query_failed',
payload,
occurred_at: t.finished_at ?? t.started_at,
});
}
if (t.metric_name) {
const payload: Record<string, string | number | boolean | string[]> = {};
put(payload, 'metric_name', shortText(t.metric_name));
put(payload, 'dataset_id', t.dataset_id);
put(payload, 'surface', t.surface);
put(payload, 'engine', shortText(t.engine));
put(payload, 'dimension_keys', keyNames(t.dimension_keys));
put(payload, 'granularity', shortText(t.granularity));
put(payload, 'row_count', t.row_count);
put(payload, 'duration_ms', t.duration_ms);
events.push({
event_type: 'aud0.metric_queried',
payload,
occurred_at: t.finished_at ?? t.started_at,
});
}
if (t.dashboard_id) {
const payload: Record<string, string | number | boolean | string[]> = {};
put(payload, 'dashboard_id', t.dashboard_id);
put(payload, 'widget_count', t.widget_count);
put(payload, 'duration_ms', t.duration_ms);
put(payload, 'surface', t.surface ?? 'dashboard');
events.push({ event_type: 'aud0.dashboard_viewed', payload, occurred_at: t.started_at });
}
return events;
}
/** Every payload key this producer can ever emit. Pinned by test. */
export const EMITTABLE_PAYLOAD_KEYS = [
'query_id',
'query_hash',
'dataset_id',
'engine',
'surface',
'filter_keys',
'duration_ms',
'row_count',
'bytes_scanned',
'cache_hit',
'error_code',
'metric_name',
'dimension_keys',
'granularity',
'dashboard_id',
'widget_count',
] as const;
+293
View File
@@ -0,0 +1,293 @@
/**
* tests/analytics/bi-slo.test.ts
* ===============================
* BI-P103 — BI SLO, and the AUD0 usage events it is measured from.
*
* Two failure modes are attacked directly, because both look like a healthy
* dashboard:
*
* 1. AN SLO REPORT OVER SILENCE. Zero events must not read as 100%
* availability. Every objective in an empty window must come back
* `insufficient_data`, and `fully_measured` must be false.
* 2. A PRODUCER THAT LEAKS. The emitter is handed an execution record
* carrying SQL text, a database error message quoting a personnummer,
* filter VALUES, a tenant id and a result sample — everything AUD0's
* BI-P076 ingest exists to refuse — and none of it may appear in any
* emitted payload. The test greps the serialized events, and first
* proves the grep finds those strings in the INPUT so a clean result
* cannot be a broken detector.
*/
import { describe, expect, it } from 'vitest';
import {
BI_SLO_CATALOGUE,
evaluateBiSlos,
percentile,
summariseSloReport,
type SloResult,
} from '@/src/analytics/slo/bi-slo';
import {
biUsageEventsForExecution,
EMITTABLE_PAYLOAD_KEYS,
type Aud0BiEvent,
type ExecutionTelemetry,
} from '@/src/analytics/slo/usage-events';
const FROM = new Date('2026-09-07T00:00:00.000Z');
const TO = new Date('2026-09-07T23:59:59.000Z');
function queryEvents(args: {
n: number;
surface: 'dashboard' | 'explore';
outcome: 'completed' | 'failed' | 'running';
durationMs: (i: number) => number;
cacheHit?: boolean;
idPrefix?: string;
}): Aud0BiEvent[] {
const out: Aud0BiEvent[] = [];
for (let i = 0; i < args.n; i += 1) {
const t: ExecutionTelemetry = {
query_id: `${args.idPrefix ?? args.surface}_${i}`,
surface: args.surface,
state: args.outcome === 'completed' ? 'succeeded' : args.outcome === 'failed' ? 'failed' : 'running',
started_at: new Date(FROM.getTime() + i * 1000).toISOString(),
finished_at: new Date(FROM.getTime() + i * 1000 + args.durationMs(i)).toISOString(),
duration_ms: args.durationMs(i),
...(args.cacheHit !== undefined ? { cache_hit: args.cacheHit } : {}),
...(args.outcome === 'failed' ? { error_code: 'TIMEOUT' } : {}),
};
out.push(...biUsageEventsForExecution(t));
}
return out;
}
function byId(results: SloResult[], id: string): SloResult {
const r = results.find((x) => x.objective_id === id);
if (!r) throw new Error(`no result for ${id}`);
return r;
}
describe('BI-P103 — an SLO over silence is not a met SLO', () => {
it('an empty window makes every measurable objective insufficient_data, never 100%', () => {
const report = evaluateBiSlos({ events: [], from: FROM, to: TO });
expect(report.fully_measured).toBe(false);
for (const r of report.results) {
expect(['insufficient_data', 'not_measurable_here']).toContain(r.status);
expect(r.status).not.toBe('met');
}
const summary = summariseSloReport(report);
expect(summary.met).toBe(0);
expect(summary.measured_fraction).toBe(0);
});
it('a started query that never finished is unresolved, not a success', () => {
const events = queryEvents({ n: 200, surface: 'dashboard', outcome: 'running', durationMs: () => 100 });
const report = evaluateBiSlos({ events, from: FROM, to: TO });
expect(report.unresolved_queries).toBe(200);
expect(byId(report.results, 'dash.availability').status).toBe('insufficient_data');
expect(byId(report.results, 'dash.availability').observed).toBeUndefined();
});
it('a percentile below min_samples is reported WITH its sample size and not judged', () => {
const events = queryEvents({
n: 12,
surface: 'explore',
outcome: 'completed',
durationMs: (i) => 1000 + i * 10,
});
const p99 = byId(evaluateBiSlos({ events, from: FROM, to: TO }).results, 'explore.p99');
expect(p99.status).toBe('insufficient_data');
expect(p99.low_confidence).toBe(true);
expect(p99.sample_size).toBe(12);
expect(p99.observed).toBeDefined(); // reported…
expect(p99.detail).toMatch(/not a percentile/); // …with the caveat attached
});
it('freshness and delivery are declared not_measurable_here rather than proxied', () => {
const report = evaluateBiSlos({ events: [], from: FROM, to: TO });
const freshness = byId(report.results, 'dash.freshness');
const delivery = byId(report.results, 'report.delivery_sla');
expect(freshness.status).toBe('not_measurable_here');
expect(freshness.detail).toMatch(/analytics_freshness_watermark/);
expect(delivery.status).toBe('not_measurable_here');
expect(delivery.detail).toMatch(/n0tify_delivery_receipt/);
});
});
describe('BI-P103 — real measurement from real events', () => {
it('computes availability, error rate and both dashboard latencies', () => {
const events = [
...queryEvents({
n: 180,
surface: 'dashboard',
outcome: 'completed',
cacheHit: false,
durationMs: (i) => 500 + i * 10,
idPrefix: 'cold',
}),
...queryEvents({
n: 120,
surface: 'dashboard',
outcome: 'completed',
cacheHit: true,
durationMs: () => 120,
idPrefix: 'warm',
}),
...queryEvents({
n: 3,
surface: 'dashboard',
outcome: 'failed',
durationMs: () => 900,
idPrefix: 'bad',
}),
];
const { results } = evaluateBiSlos({ events, from: FROM, to: TO });
const availability = byId(results, 'dash.availability');
expect(availability.sample_size).toBe(303);
expect(availability.observed).toBeCloseTo(300 / 303, 6);
expect(availability.status).toBe('breached'); // 0.9901 < 0.995 target
const errorRate = byId(results, 'dash.error_rate');
expect(errorRate.observed).toBeCloseTo(3 / 303, 6);
const cold = byId(results, 'dash.p95_first_query');
expect(cold.sample_size).toBe(183); // 180 cold + 3 failures, which had no cache hit
expect(cold.status).toBe('met');
const cached = byId(results, 'dash.p95_cached');
expect(cached.sample_size).toBe(120);
expect(cached.observed).toBe(120);
expect(cached.status).toBe('met');
});
it('a breach is reported as a breach', () => {
const events = queryEvents({
n: 60,
surface: 'dashboard',
outcome: 'completed',
cacheHit: false,
durationMs: () => 9000,
});
expect(
byId(evaluateBiSlos({ events, from: FROM, to: TO }).results, 'dash.p95_first_query'),
).toMatchObject({ status: 'breached', observed: 9000 });
});
it('events outside the window do not count', () => {
const events = queryEvents({ n: 200, surface: 'dashboard', outcome: 'completed', durationMs: () => 100 });
const report = evaluateBiSlos({
events,
from: new Date('2026-09-08T00:00:00.000Z'),
to: new Date('2026-09-08T23:59:59.000Z'),
});
expect(byId(report.results, 'dash.availability').status).toBe('insufficient_data');
});
it('percentile is nearest-rank — it never invents a value nobody experienced', () => {
const sample = [10, 20, 30, 40];
expect(percentile(sample, 0.95)).toBe(40);
expect(percentile(sample, 0.5)).toBe(20);
expect(percentile([], 0.95)).toBeUndefined();
});
it('the catalogue covers all three workload classes and every field BI-P103 names', () => {
const kinds = new Set(BI_SLO_CATALOGUE.map((o) => `${o.workload}:${o.kind}`));
for (const expected of [
'interactive_dashboard:availability',
'interactive_dashboard:latency_p95_first',
'interactive_dashboard:latency_p95_cached',
'interactive_dashboard:error_rate',
'interactive_dashboard:freshness',
'exploration:latency_p95',
'exploration:latency_p99',
'exploration:max_result_rows',
'exploration:timeout',
'report:completion_sla',
'report:delivery_sla',
]) {
expect(kinds.has(expected), expected).toBe(true);
}
});
});
describe('BI-P103 — the producer cannot leak a result into AUD0', () => {
const hostile: ExecutionTelemetry = {
query_id: 'q_1',
compiled_query_hash: 'f'.repeat(64),
compiled_sql: "SELECT * FROM customers WHERE personnummer = '19800101-1234'",
error_message: 'duplicate key value violates unique constraint (personnummer)=(19800101-1234)',
error_code: 'CONFLICT',
tenant_id: 't_alpha',
dataset_id: 'ds_sales',
engine: 'clickhouse',
surface: 'dashboard',
state: 'failed',
started_at: '2026-09-07T10:00:00.000Z',
finished_at: '2026-09-07T10:00:02.000Z',
duration_ms: 2000,
filter_keys: ['region'],
filter_values: { region: ['Gotland'] },
result_sample: [{ customer: 'Anna Andersson', revenue: 42 }],
metric_name: 'revenue',
dashboard_id: 'dash_sales',
widget_count: 4,
};
it('emits none of the forbidden material', () => {
const events = biUsageEventsForExecution(hostile);
const serialized = JSON.stringify(events);
// The detector must be able to find these strings — they are in the input.
const input = JSON.stringify(hostile);
for (const secret of ['19800101-1234', 'Anna Andersson', 'Gotland', 't_alpha', 'SELECT *']) {
expect(input, `detector check for ${secret}`).toContain(secret);
}
for (const secret of [
'19800101-1234',
'Anna Andersson',
'Gotland',
't_alpha',
'SELECT *',
'duplicate key',
]) {
expect(serialized, secret).not.toContain(secret);
}
});
it('every emitted key is on the allowlist AUD0 accepts', () => {
const events = biUsageEventsForExecution(hostile);
for (const e of events) {
for (const key of Object.keys(e.payload)) {
expect(EMITTABLE_PAYLOAD_KEYS, `${e.event_type}.${key}`).toContain(key);
}
}
});
it('a query_hash that is not a sha256 digest is dropped, not reshaped', () => {
const events = biUsageEventsForExecution({ ...hostile, compiled_query_hash: 'SELECT 1' });
for (const e of events) expect(e.payload.query_hash).toBeUndefined();
});
it('a running query emits only "started" — it is neither a success nor a failure', () => {
const events = biUsageEventsForExecution({
...hostile,
state: 'running',
metric_name: undefined,
dashboard_id: undefined,
});
expect(events.map((e) => e.event_type)).toEqual(['aud0.analytics_query_started']);
});
it('a cancelled query is reported as failed with a CODE', () => {
const events = biUsageEventsForExecution({
...hostile,
state: 'cancelled',
error_code: undefined,
metric_name: undefined,
dashboard_id: undefined,
});
const failed = events.find((e) => e.event_type === 'aud0.analytics_query_failed');
expect(failed?.payload.error_code).toBe('CANCELLED');
expect(failed?.payload.error_message).toBeUndefined();
});
});