feat(data-os): P010 completion — Source Registry persistence + TS/Python quality bridge + provenance + fault-injection tests #28
@@ -39,6 +39,8 @@ next-env.d.ts
|
||||
__pycache__/
|
||||
*.pyc
|
||||
*.pyo
|
||||
.venv/
|
||||
.pytest_cache/
|
||||
|
||||
# analytics / database files
|
||||
*.duckdb
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* app/api/0pt/quality/bridge-run/route.ts
|
||||
* ==========================================
|
||||
* The P010 TS/Python split-brain bridge (BEFORE_STATE.md §2). This is the
|
||||
* ONE route the production Python engine
|
||||
* (services/api/workers/quality_bridge.py, called from
|
||||
* services/api/workers/pipeline.py) talks to. It:
|
||||
*
|
||||
* 1. auto-provisions a deterministic, tenant-scoped baseline rule set for
|
||||
* (tenant_id, dataset_id) the first time it's seen (src/quality/quality-bridge.ts) --
|
||||
* the Python engine never has to pre-register rules through the CRUD
|
||||
* surface just to get a quality read on an order's already-produced rows;
|
||||
* 2. runs QualityService.runQuality synchronously against the rows the
|
||||
* pipeline already produced (delivered, post the Python filter --
|
||||
* this is an ADVISORY second opinion, not a gate, until Simon decides
|
||||
* to wire per-row gating -- see quality_bridge.py's docstring for why
|
||||
* that isn't done in this pass);
|
||||
* 3. returns the run + any open issues.
|
||||
*
|
||||
* This does not replace, call, or block on the Python filter in any way --
|
||||
* it is purely additive. If this route is unreachable, times out, or 500s,
|
||||
* the Python side is required to fail open (never raise into the pipeline).
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, qualityBridgeRunSchema } from "@/src/validation/0pt-schemas";
|
||||
import { qualityService } from "@/src/services/quality";
|
||||
import { ensureBaselineRules } from "@/src/quality/quality-bridge";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const __raw = await request.json();
|
||||
const __parsed = parseBody(qualityBridgeRunSchema, __raw);
|
||||
if (!__parsed.ok) {
|
||||
return NextResponse.json(__parsed.body, { status: __parsed.status });
|
||||
}
|
||||
const body = __parsed.data;
|
||||
|
||||
await ensureBaselineRules(qualityService, {
|
||||
tenant_id: body.tenant_id,
|
||||
project_id: body.project_id,
|
||||
dataset_id: body.dataset_id,
|
||||
});
|
||||
|
||||
const run = await qualityService.runQuality({
|
||||
tenant_id: body.tenant_id,
|
||||
project_id: body.project_id,
|
||||
dataset_id: body.dataset_id,
|
||||
dataset_version: body.dataset_version,
|
||||
rows: body.rows,
|
||||
correlation_id: body.correlation_id,
|
||||
});
|
||||
|
||||
const issues = qualityService.listIssues({
|
||||
tenant_id: body.tenant_id,
|
||||
dataset_id: body.dataset_id,
|
||||
status: "open",
|
||||
});
|
||||
|
||||
return NextResponse.json({ run, issues }, { status: 201 });
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* app/api/0pt/quality/issues/route.ts
|
||||
* ======================================
|
||||
* quality.list_issues (GET) -- issues opened by finished quality runs
|
||||
* (human-review handoff, Master-DoD §10).
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { qualityService } from "@/src/services/quality";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
import type { DataQualityIssue } from "@/src/quality/quality-types";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tenantId = searchParams.get("tenant_id");
|
||||
if (!tenantId) {
|
||||
return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
}
|
||||
const issues = qualityService.listIssues({
|
||||
tenant_id: tenantId,
|
||||
dataset_id: searchParams.get("dataset_id") ?? undefined,
|
||||
status: (searchParams.get("status") as DataQualityIssue["status"]) ?? undefined,
|
||||
});
|
||||
return NextResponse.json(issues);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* app/api/0pt/quality/rules/[ruleId]/route.ts
|
||||
* ===============================================
|
||||
* quality.get_rule (GET) + quality.update_rule (PUT, versioned -- bumps
|
||||
* `version`, records the superseded row). Cross-tenant reads are NOT_FOUND
|
||||
* (no existence oracle), matching QualityService.getRule's own contract.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { qualityService } from "@/src/services/quality";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
import { z } from "zod";
|
||||
|
||||
const updateQualityRuleSchema = z.object({
|
||||
tenant_id: z.string().min(1),
|
||||
name: z.string().min(1).optional(),
|
||||
expectation: z.object({ expectation_type: z.string().min(1), kwargs: z.record(z.string(), z.unknown()) }).optional(),
|
||||
severity: z.enum(["info", "warning", "error", "critical"]).optional(),
|
||||
status: z.enum(["active", "disabled", "superseded"]).optional(),
|
||||
});
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ ruleId: string }> }) {
|
||||
try {
|
||||
const { ruleId } = await params;
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tenantId = searchParams.get("tenant_id");
|
||||
if (!tenantId) {
|
||||
return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
}
|
||||
const versions = searchParams.get("versions") === "true";
|
||||
if (versions) {
|
||||
return NextResponse.json(qualityService.getRuleVersions(ruleId, tenantId));
|
||||
}
|
||||
return NextResponse.json(qualityService.getRule(ruleId, tenantId));
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest, { params }: { params: Promise<{ ruleId: string }> }) {
|
||||
try {
|
||||
const { ruleId } = await params;
|
||||
const raw = await request.json();
|
||||
const parsed = updateQualityRuleSchema.safeParse(raw);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ error: "validation_error", issues: parsed.error.issues.map((i) => ({ path: i.path.join("."), message: i.message })) },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
const { tenant_id, ...patch } = parsed.data;
|
||||
const rule = await qualityService.updateRule(ruleId, tenant_id, patch);
|
||||
return NextResponse.json(rule);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* app/api/0pt/quality/rules/route.ts
|
||||
* ====================================
|
||||
* quality.create_rule (POST) + quality.list_rules (GET) -- the CRUD surface
|
||||
* over QualityService's versioned rule registry (src/quality/quality-service.ts,
|
||||
* Master-DoD §10). Uses the shared singleton from src/services/quality.ts,
|
||||
* not a fresh `new QualityService()` per route module -- see that file's
|
||||
* doc comment (and BEFORE_STATE.md §1, which found and fixed the same
|
||||
* per-module-instance defect for the Source Registry).
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, createQualityRuleSchema } from "@/src/validation/0pt-schemas";
|
||||
import { qualityService } from "@/src/services/quality";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
import type { DataQualityRule } from "@/src/quality/quality-types";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const __raw = await request.json();
|
||||
const __parsed = parseBody(createQualityRuleSchema, __raw);
|
||||
if (!__parsed.ok) {
|
||||
return NextResponse.json(__parsed.body, { status: __parsed.status });
|
||||
}
|
||||
const rule = await qualityService.createRule(__parsed.data);
|
||||
return NextResponse.json(rule, { status: 201 });
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tenantId = searchParams.get("tenant_id");
|
||||
if (!tenantId) {
|
||||
return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
}
|
||||
const rules = qualityService.listRules({
|
||||
tenant_id: tenantId,
|
||||
dataset_id: searchParams.get("dataset_id") ?? undefined,
|
||||
status: (searchParams.get("status") as DataQualityRule["status"]) ?? undefined,
|
||||
});
|
||||
return NextResponse.json(rules);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* app/api/0pt/quality/runs/[runId]/route.ts
|
||||
* =============================================
|
||||
* quality.get_run (GET). Cross-tenant reads are NOT_FOUND.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { qualityService } from "@/src/services/quality";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ runId: string }> }) {
|
||||
try {
|
||||
const { runId } = await params;
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tenantId = searchParams.get("tenant_id");
|
||||
if (!tenantId) {
|
||||
return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json(qualityService.getRun(runId, tenantId));
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* app/api/0pt/quality/runs/route.ts
|
||||
* ====================================
|
||||
* quality.run (POST) + quality.list_runs (GET). POST executes all active
|
||||
* rules for a dataset against caller-supplied rows -- runs synchronously
|
||||
* when every active rule's expectation is locally executable, otherwise
|
||||
* delegates to the GE backend via N0D (QualityService.runQuality never
|
||||
* fabricates a result for a check it couldn't run).
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, createQualityRunSchema } from "@/src/validation/0pt-schemas";
|
||||
import { qualityService } from "@/src/services/quality";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const __raw = await request.json();
|
||||
const __parsed = parseBody(createQualityRunSchema, __raw);
|
||||
if (!__parsed.ok) {
|
||||
return NextResponse.json(__parsed.body, { status: __parsed.status });
|
||||
}
|
||||
const run = await qualityService.runQuality(__parsed.data);
|
||||
return NextResponse.json(run, { status: 201 });
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tenantId = searchParams.get("tenant_id");
|
||||
if (!tenantId) {
|
||||
return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
}
|
||||
const runs = qualityService.listRuns({
|
||||
tenant_id: tenantId,
|
||||
dataset_id: searchParams.get("dataset_id") ?? undefined,
|
||||
});
|
||||
return NextResponse.json(runs);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/freshness/route.ts
|
||||
* =====================================================
|
||||
* Freshness view derived from provenance's freshness_sla_hours + the last
|
||||
* successful observation (recorded via POST .../health with status "up",
|
||||
* or SourceRegistryService.recordSuccess directly). Read-only — there is no
|
||||
* separate write path here by design; freshness is a derived signal, not
|
||||
* its own record.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { sourceRegistryService } from "@/src/services/source-registry";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tenantId = searchParams.get("tenant_id");
|
||||
if (!tenantId) {
|
||||
return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
}
|
||||
const freshness = await sourceRegistryService.getFreshness(sourceId, tenantId);
|
||||
return NextResponse.json(freshness);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/health/route.ts
|
||||
* ==================================================
|
||||
* source.health — record a health observation (POST) and read the
|
||||
* aggregate view (GET): status, last check, consecutive failures. Bounded
|
||||
* history (latest 100 observations) — this is an operational signal, not
|
||||
* an audit log; AUD0 owns history.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, recordSourceHealthCheckSchema } from "@/src/validation/0pt-schemas";
|
||||
import { sourceRegistryService } from "@/src/services/source-registry";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tenantId = searchParams.get("tenant_id");
|
||||
if (!tenantId) {
|
||||
return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
}
|
||||
const health = await sourceRegistryService.getHealth(sourceId, tenantId);
|
||||
return NextResponse.json(health);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const __raw = await request.json();
|
||||
const __parsed = parseBody(recordSourceHealthCheckSchema, __raw);
|
||||
if (!__parsed.ok) {
|
||||
return NextResponse.json(__parsed.body, { status: __parsed.status });
|
||||
}
|
||||
const body = __parsed.data;
|
||||
const health = await sourceRegistryService.recordHealthCheck({
|
||||
source_id: sourceId,
|
||||
tenant_id: body.tenant_id,
|
||||
status: body.status,
|
||||
checked_at: body.checked_at ?? new Date().toISOString(),
|
||||
latency_ms: body.latency_ms,
|
||||
detail: body.detail,
|
||||
});
|
||||
return NextResponse.json(health, { status: 201 });
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/provenance/route.ts
|
||||
* ======================================================
|
||||
* Per-SOURCE provenance/method metadata (Master-DoD §10) — HOW a source is
|
||||
* obtained (official_api | bulk_download | authorized_web | licensed_feed |
|
||||
* manual_upload), not per-field bridge_provenance. See BEFORE_STATE.md §3
|
||||
* for that distinction: this route does not, and cannot, close the
|
||||
* bridge_provenance gap — it only surfaces what SourceRegistryService
|
||||
* already models at the source level.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, setSourceProvenanceSchema } from "@/src/validation/0pt-schemas";
|
||||
import { sourceRegistryService } from "@/src/services/source-registry";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tenantId = searchParams.get("tenant_id");
|
||||
if (!tenantId) {
|
||||
return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
}
|
||||
const provenance = await sourceRegistryService.getProvenance(sourceId, tenantId);
|
||||
if (!provenance) {
|
||||
return NextResponse.json({ error: `source ${sourceId} has no provenance record` }, { status: 404 });
|
||||
}
|
||||
return NextResponse.json(provenance);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const __raw = await request.json();
|
||||
const __parsed = parseBody(setSourceProvenanceSchema, __raw);
|
||||
if (!__parsed.ok) {
|
||||
return NextResponse.json(__parsed.body, { status: __parsed.status });
|
||||
}
|
||||
const body = __parsed.data;
|
||||
const provenance = await sourceRegistryService.setProvenance({
|
||||
...body,
|
||||
source_id: sourceId,
|
||||
updated_at: new Date().toISOString(),
|
||||
});
|
||||
return NextResponse.json(provenance);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -4,27 +4,24 @@
|
||||
* 0pt.query_source — bounded synchronous query execution for a registered
|
||||
* Source. Additive: does not modify sibling ../route.ts or ../../route.ts.
|
||||
*
|
||||
* NOTE — in-memory scope: this module instantiates its own SourceService,
|
||||
* matching the existing sources/route.ts and sources/[sourceId]/route.ts
|
||||
* pattern (each API route module owns a fresh in-memory registry, so state
|
||||
* is not shared across route modules yet — see the "Singleton note" in
|
||||
* src/services/document-capture-service.ts for prior art on this same repo
|
||||
* limitation). A source registered via POST /api/0pt/sources will not be
|
||||
* resolvable here until SourceService is backed by shared persistence.
|
||||
* Correction: this comment previously claimed `sql/002_p0_dataset_quality.sql`
|
||||
* already defined an `opt_sources` table for that. That file does not exist
|
||||
* and never has (git log --all: zero commits, any branch, ever). sql/ today
|
||||
* has only 001_init.sql and 004_iceberg_write_boundary.sql. The real fix —
|
||||
* backing SourceService with real persistence — is still open; no migration
|
||||
* for it exists yet.
|
||||
* Details: docs/P0_QUERY_SOURCE_FLOW.md.
|
||||
* UPDATED (Source Registry Postgres-persistence pass): now imports the
|
||||
* shared `sourceService` singleton from src/services/source-registry.ts
|
||||
* instead of constructing its own SourceService, closing the "not even a
|
||||
* shared process-wide singleton" gap BEFORE_STATE.md §1 flagged (a source
|
||||
* registered via POST /api/0pt/sources used to be invisible here). With
|
||||
* DATABASE_URL configured this is doubly fixed — state lives in Postgres
|
||||
* (sql/002_source_registry.sql), genuinely shared across processes, not
|
||||
* just within one. Details: docs/P0_QUERY_SOURCE_FLOW.md.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, sourceQuerySchema } from "@/src/validation/0pt-schemas";
|
||||
import { SourceService } from "@/src/services/source-service";
|
||||
import { SourceQueryService, SourceQueryTimeoutError, SourceQueryUnsupportedError } from "@/src/services/source-query-service";
|
||||
import { sourceService } from "@/src/services/source-registry";
|
||||
import {
|
||||
SourceQueryService,
|
||||
SourceQueryTimeoutError,
|
||||
SourceQueryUnsupportedError,
|
||||
} from "@/src/services/source-query-service";
|
||||
|
||||
const sourceService = new SourceService();
|
||||
const queryService = new SourceQueryService({ sourceService });
|
||||
|
||||
export async function POST(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/rights/route.ts
|
||||
* ==================================================
|
||||
* Rights / license / purpose metadata (Master-DoD §10). PUT is an upsert
|
||||
* (SourceRegistryService.setRights), matching the fail-closed purpose gate:
|
||||
* no rights record at all denies every purpose until one is set here.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, setSourceRightsSchema } from "@/src/validation/0pt-schemas";
|
||||
import { sourceRegistryService } from "@/src/services/source-registry";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tenantId = searchParams.get("tenant_id");
|
||||
if (!tenantId) {
|
||||
return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
}
|
||||
const rights = await sourceRegistryService.getRights(sourceId, tenantId);
|
||||
if (!rights) {
|
||||
return NextResponse.json({ error: `source ${sourceId} has no rights record` }, { status: 404 });
|
||||
}
|
||||
return NextResponse.json(rights);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const __raw = await request.json();
|
||||
const __parsed = parseBody(setSourceRightsSchema, __raw);
|
||||
if (!__parsed.ok) {
|
||||
return NextResponse.json(__parsed.body, { status: __parsed.status });
|
||||
}
|
||||
const body = __parsed.data;
|
||||
const rights = await sourceRegistryService.setRights({ ...body, source_id: sourceId });
|
||||
return NextResponse.json(rights);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* app/api/0pt/sources/[sourceId]/route.ts
|
||||
* ==========================================
|
||||
* source.describe — full SourceDescriptor (base + rights + provenance +
|
||||
* health + freshness + rate policy). Cross-tenant probes get 404 NOT_FOUND
|
||||
* (no existence oracle, §50) via SourceRegistryService.describeSource's
|
||||
* assertSourceTenant gate.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { sourceRegistryService } from "@/src/services/source-registry";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ sourceId: string }> }) {
|
||||
try {
|
||||
const { sourceId } = await params;
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tenantId = searchParams.get("tenant_id");
|
||||
if (!tenantId) {
|
||||
return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
}
|
||||
const descriptor = await sourceRegistryService.describeSource(sourceId, tenantId);
|
||||
return NextResponse.json(descriptor);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { SourceRatePolicyService } from "@/src/services/source-rate-policy-service";
|
||||
import { sourceRatePolicyService as service } from "@/src/services/source-registry";
|
||||
|
||||
const service = new SourceRatePolicyService();
|
||||
|
||||
export async function GET(_request: Request, { params }: { params: Promise<{ sourceRatePolicyId: string }> }) {
|
||||
export async function GET(
|
||||
_request: Request,
|
||||
{ params }: { params: Promise<{ sourceRatePolicyId: string }> },
|
||||
) {
|
||||
try {
|
||||
const { sourceRatePolicyId } = await params;
|
||||
const policy = await service.getSourceRatePolicy(sourceRatePolicyId);
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, createSourceRatePolicySchema } from "@/src/validation/0pt-schemas";
|
||||
import { SourceRatePolicyService } from "@/src/services/source-rate-policy-service";
|
||||
|
||||
const service = new SourceRatePolicyService();
|
||||
import { sourceRatePolicyService as service } from "@/src/services/source-registry";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* app/api/0pt/sources/route.ts
|
||||
* ==============================
|
||||
* source.register (POST) + source.query (GET) — the P0 Source Registry
|
||||
* route surface BEFORE_STATE.md §1 found entirely missing
|
||||
* (`grep -rl "SourceRegistryService" app/` returned nothing). Backed by
|
||||
* SourceRegistryService, which now persists to Postgres when DATABASE_URL
|
||||
* is configured (src/repositories/*, sql/002_source_registry.sql).
|
||||
*
|
||||
* Uses the shared singleton from src/services/source-registry.ts, not a
|
||||
* fresh `new SourceRegistryService()` per route module — see that file's
|
||||
* doc comment.
|
||||
*/
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { parseBody, registerSourceSchema } from "@/src/validation/0pt-schemas";
|
||||
import { sourceRegistryService } from "@/src/services/source-registry";
|
||||
import { siaxErrorResponse } from "@/src/http/siax-error-response";
|
||||
import type { SourceStatus, SourceType } from "@/src/types/source";
|
||||
import type { SourceRights } from "@/src/types/source-registry";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const __raw = await request.json();
|
||||
const __parsed = parseBody(registerSourceSchema, __raw);
|
||||
if (!__parsed.ok) {
|
||||
return NextResponse.json(__parsed.body, { status: __parsed.status });
|
||||
}
|
||||
const body = __parsed.data;
|
||||
const descriptor = await sourceRegistryService.registerSource(body);
|
||||
return NextResponse.json(descriptor, { status: 201 });
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const tenantId = searchParams.get("tenant_id");
|
||||
if (!tenantId) {
|
||||
return NextResponse.json({ error: "tenant_id is required" }, { status: 400 });
|
||||
}
|
||||
const sources = await sourceRegistryService.querySources({
|
||||
tenant_id: tenantId,
|
||||
project_id: searchParams.get("project_id") ?? undefined,
|
||||
source_type: (searchParams.get("source_type") as SourceType) ?? undefined,
|
||||
status: (searchParams.get("status") as SourceStatus) ?? undefined,
|
||||
purpose: searchParams.get("purpose") ?? undefined,
|
||||
redistribution: (searchParams.get("redistribution") as SourceRights["redistribution"]) ?? undefined,
|
||||
});
|
||||
return NextResponse.json(sources);
|
||||
} catch (error: unknown) {
|
||||
return siaxErrorResponse(error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
[pytest]
|
||||
# services/api's modules import each other as top-level packages
|
||||
# (`from workers.pipeline import run_job`, `from sources.connectors import ...`)
|
||||
# relative to this directory — orchestrator/main.py does the same. Running
|
||||
# pytest from services/api (or with this file as rootdir) needs that on
|
||||
# sys.path; pytest 8's rootdir-relative testpaths + this ini being IN
|
||||
# services/api handles that via pytest's automatic rootdir insertion for
|
||||
# rootdir-relative imports when there is no conftest.py package structure.
|
||||
testpaths = tests
|
||||
asyncio_mode = auto
|
||||
pythonpath = .
|
||||
@@ -0,0 +1,7 @@
|
||||
# Test-only dependencies for services/api. NOT installed in the production
|
||||
# image (Dockerfile only installs requirements.txt) — see pytest.ini for how
|
||||
# the test suite is run locally / in CI.
|
||||
-r requirements.txt
|
||||
pytest==8.3.4
|
||||
pytest-asyncio==0.25.2
|
||||
respx==0.22.0
|
||||
@@ -10,14 +10,23 @@ EXPLICITLY NOT PRESENT (and must never be added here):
|
||||
rotation to evade bans, scraping behind login walls, fake-account creation.
|
||||
|
||||
Every connector returns a normalized company dict + per-field provenance rows
|
||||
(source_id, confidence 0-1, level 0-3, freshness) so nothing ships unsourced.
|
||||
so nothing ships unsourced. `Provenance` (below) carries the full field set
|
||||
BEFORE_STATE.md §3 found missing from the actual dataflow — this repo's
|
||||
5-field bridge_provenance was: source, confidence, level, and a DATE-only
|
||||
freshness; the concept "bridge_provenance" as a named thing did not exist
|
||||
anywhere in the repo at all. This adds, per field: retrieval TIMESTAMP
|
||||
(distinct from the legacy date-only `freshness`), the transform applied,
|
||||
connector/schema version, the producing system, and two slots
|
||||
(`quality`/`entity_resolution`) that downstream stages (quality bridge,
|
||||
resolve.py) fill in as the record moves through the pipeline — they start
|
||||
`None` here and are never backfilled with a guess.
|
||||
Terms change — VERIFY each source's current access/licence before relying.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import date
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
@@ -26,6 +35,20 @@ import httpx
|
||||
USER_AGENT = "0pt-data-engine/0.1 (+https://0pt.app; SIAX Technology AB; [email protected])"
|
||||
_TIMEOUT = httpx.Timeout(15.0, connect=10.0)
|
||||
|
||||
# Bumped when a connector's parsing/field-mapping logic changes in a way that
|
||||
# could shift output shape — recorded per-field as `Provenance.connector_version`
|
||||
# so a downstream consumer can tell which parsing logic produced a value.
|
||||
CONNECTOR_VERSIONS: dict[str, str] = {
|
||||
"gleif": "gleif-connector@1",
|
||||
"brreg": "brreg-connector@1",
|
||||
"companies_house": "companies-house-connector@1",
|
||||
"prh": "prh-connector@1",
|
||||
"cvr": "cvr-connector@1",
|
||||
"rdw": "rdw-connector@1",
|
||||
}
|
||||
|
||||
PRODUCER = "0pt-engine"
|
||||
|
||||
|
||||
def _client() -> httpx.AsyncClient:
|
||||
return httpx.AsyncClient(
|
||||
@@ -41,7 +64,39 @@ class Provenance:
|
||||
source_id: str
|
||||
confidence: float
|
||||
level: int # 0 unavailable/licensed, 1 single, 2 partial, 3 corroborated
|
||||
freshness: str | None = None
|
||||
freshness: str | None = None # legacy: retrieval DATE only (kept for back-compat)
|
||||
retrieved_at: str | None = None # full ISO-8601 UTC timestamp of the actual fetch
|
||||
transform: str | None = None # e.g. "raw_fetch", "normalized", "entity_resolution_merge"
|
||||
connector_version: str | None = None # e.g. "brreg-connector@1" — see CONNECTOR_VERSIONS
|
||||
producer: str = PRODUCER # which system produced this field's value
|
||||
quality: dict[str, Any] | None = None # filled by the quality bridge, if/when it runs (never guessed)
|
||||
entity_resolution: dict[str, Any] | None = None # filled by resolve.py on merge, never at fetch time
|
||||
artifact_ref: str | None = None # delivery artifact reference (ST0RE), set at delivery time — not yet wired
|
||||
|
||||
|
||||
def make_provenance(
|
||||
field_name: str,
|
||||
source_id: str,
|
||||
confidence: float,
|
||||
level: int,
|
||||
*,
|
||||
transform: str = "raw_fetch",
|
||||
) -> Provenance:
|
||||
"""Factory used by every connector below so the new provenance fields
|
||||
(retrieved_at, connector_version, producer) are populated consistently
|
||||
instead of each call site re-deriving them ad hoc."""
|
||||
now = datetime.now(timezone.utc)
|
||||
return Provenance(
|
||||
field_name=field_name,
|
||||
source_id=source_id,
|
||||
confidence=confidence,
|
||||
level=level,
|
||||
freshness=now.date().isoformat(),
|
||||
retrieved_at=now.isoformat(),
|
||||
transform=transform,
|
||||
connector_version=CONNECTOR_VERSIONS.get(source_id),
|
||||
producer=PRODUCER,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -57,10 +112,6 @@ class CompanyResult:
|
||||
source: str = ""
|
||||
|
||||
|
||||
def _today() -> str:
|
||||
return date.today().isoformat()
|
||||
|
||||
|
||||
# ── GLEIF / LEI (global cross-border key) — open API ────────────────────────
|
||||
async def gleif_by_lei(lei: str) -> CompanyResult | None:
|
||||
url = f"https://api.gleif.org/api/v1/lei-records/{lei}"
|
||||
@@ -79,10 +130,9 @@ async def gleif_by_lei(lei: str) -> CompanyResult | None:
|
||||
"country": legal.get("country"), "postal": legal.get("postalCode")},
|
||||
source="gleif",
|
||||
)
|
||||
f = _today()
|
||||
res.provenance += [
|
||||
Provenance("lei", "gleif", 0.99, 3, f),
|
||||
Provenance("name", "gleif", 0.97, 3, f),
|
||||
make_provenance("lei", "gleif", 0.99, 3),
|
||||
make_provenance("name", "gleif", 0.97, 3),
|
||||
]
|
||||
return res
|
||||
|
||||
@@ -102,11 +152,10 @@ async def brreg_by_orgnr(orgnr: str) -> CompanyResult | None:
|
||||
address={"raw": d.get("forretningsadresse")},
|
||||
source="brreg",
|
||||
)
|
||||
f = _today()
|
||||
res.provenance += [
|
||||
Provenance("name", "brreg", 0.97, 3, f),
|
||||
Provenance("status", "brreg", 0.95, 3, f),
|
||||
Provenance("address", "brreg", 0.95, 3, f),
|
||||
make_provenance("name", "brreg", 0.97, 3),
|
||||
make_provenance("status", "brreg", 0.95, 3),
|
||||
make_provenance("address", "brreg", 0.95, 3),
|
||||
]
|
||||
return res
|
||||
|
||||
@@ -117,7 +166,7 @@ async def companies_house_by_number(number: str) -> CompanyResult | None:
|
||||
if not key:
|
||||
# No key -> deliver honestly as unavailable, never fabricate.
|
||||
return CompanyResult(org_nr=number, market="GB", source="companies_house",
|
||||
provenance=[Provenance("name", "companies_house", 0.0, 0, _today())])
|
||||
provenance=[make_provenance("name", "companies_house", 0.0, 0, transform="unavailable_no_key")])
|
||||
url = f"https://api.company-information.service.gov.uk/company/{number}"
|
||||
async with _client() as c:
|
||||
r = await c.get(url, auth=(key, ""))
|
||||
@@ -131,10 +180,9 @@ async def companies_house_by_number(number: str) -> CompanyResult | None:
|
||||
address={"raw": d.get("registered_office_address")},
|
||||
source="companies_house",
|
||||
)
|
||||
f = _today()
|
||||
res.provenance += [
|
||||
Provenance("name", "companies_house", 0.97, 3, f),
|
||||
Provenance("status", "companies_house", 0.95, 3, f),
|
||||
make_provenance("name", "companies_house", 0.97, 3),
|
||||
make_provenance("status", "companies_house", 0.95, 3),
|
||||
]
|
||||
return res
|
||||
|
||||
@@ -154,7 +202,7 @@ async def prh_by_businessid(business_id: str) -> CompanyResult | None:
|
||||
name = names[0].get("name") if names else d.get("name")
|
||||
res = CompanyResult(org_nr=business_id, market="FI", name=name, status=d.get("status"), source="prh")
|
||||
if name:
|
||||
res.provenance.append(Provenance("name", "prh", 0.95, 3, _today()))
|
||||
res.provenance.append(make_provenance("name", "prh", 0.95, 3))
|
||||
return res
|
||||
|
||||
|
||||
@@ -173,8 +221,7 @@ async def cvr_by_number(cvr: str) -> CompanyResult | None:
|
||||
status="ceased" if d.get("enddate") else "active",
|
||||
address={"raw": d.get("address")}, source="cvr",
|
||||
)
|
||||
f = _today()
|
||||
res.provenance += [Provenance("name", "cvr", 0.93, 3, f), Provenance("address", "cvr", 0.9, 2, f)]
|
||||
res.provenance += [make_provenance("name", "cvr", 0.93, 3), make_provenance("address", "cvr", 0.9, 2)]
|
||||
return res
|
||||
|
||||
|
||||
@@ -190,12 +237,20 @@ async def rdw_vehicle(kenteken: str) -> dict[str, Any] | None:
|
||||
if not arr:
|
||||
return None
|
||||
d = arr[0]
|
||||
p = make_provenance("make", "rdw", 0.9, 3)
|
||||
return {
|
||||
"reg_plate": kenteken, "market": "NL",
|
||||
"make": d.get("merk"), "model": d.get("handelsbenaming"),
|
||||
"year": (d.get("datum_eerste_toelating") or "")[:4] or None,
|
||||
"source": "rdw",
|
||||
"provenance": [{"field": "make", "source_id": "rdw", "confidence": 0.9, "level": 3, "freshness": _today()}],
|
||||
# Dict shape (not the Provenance dataclass) — this endpoint predates it
|
||||
# and is returned directly as an API response body, not merged through
|
||||
# resolve.py. Field names kept aligned with Provenance's for consistency.
|
||||
"provenance": [{
|
||||
"field": p.field_name, "source_id": p.source_id, "confidence": p.confidence, "level": p.level,
|
||||
"freshness": p.freshness, "retrieved_at": p.retrieved_at, "transform": p.transform,
|
||||
"connector_version": p.connector_version, "producer": p.producer,
|
||||
}],
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
"""
|
||||
Verifies the P010 provenance additions (BEFORE_STATE.md §3: source,
|
||||
retrieval timestamp, transform, version, producer were absent from the real
|
||||
dataflow) land correctly on every real connector's output — mocked HTTP,
|
||||
real parsing code.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
import respx
|
||||
|
||||
from sources.connectors import brreg_by_orgnr, make_provenance, CONNECTOR_VERSIONS
|
||||
|
||||
|
||||
class TestMakeProvenanceFactory:
|
||||
def test_populates_all_new_fields_honestly_and_leaves_the_rest_none(self):
|
||||
p = make_provenance("name", "brreg", 0.97, 3)
|
||||
assert p.field_name == "name"
|
||||
assert p.source_id == "brreg"
|
||||
assert p.retrieved_at is not None
|
||||
assert p.freshness == p.retrieved_at[:10] # date prefix of the same instant
|
||||
assert p.transform == "raw_fetch"
|
||||
assert p.connector_version == CONNECTOR_VERSIONS["brreg"]
|
||||
assert p.producer == "0pt-engine"
|
||||
# Never guessed — these are filled by later pipeline stages, not at fetch time.
|
||||
assert p.quality is None
|
||||
assert p.entity_resolution is None
|
||||
assert p.artifact_ref is None
|
||||
|
||||
def test_transform_override_is_honored(self):
|
||||
p = make_provenance("name", "companies_house", 0.0, 0, transform="unavailable_no_key")
|
||||
assert p.transform == "unavailable_no_key"
|
||||
|
||||
def test_unknown_source_id_has_no_fabricated_version(self):
|
||||
p = make_provenance("x", "some_future_source", 0.5, 1)
|
||||
assert p.connector_version is None # honestly unknown, not guessed
|
||||
|
||||
|
||||
class TestBrregConnectorProvenance:
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_real_parsing_path_produces_fully_populated_provenance(self):
|
||||
respx.get("https://data.brreg.no/enhetsregisteret/api/enheter/923609016").mock(
|
||||
return_value=httpx.Response(200, json={
|
||||
"navn": "Volvo Personvagnar Norge AS", "konkurs": False,
|
||||
"forretningsadresse": {"adresse": ["Storgata 1"]},
|
||||
})
|
||||
)
|
||||
res = await brreg_by_orgnr("923609016")
|
||||
assert res is not None
|
||||
assert len(res.provenance) == 3 # name, status, address
|
||||
for p in res.provenance:
|
||||
assert p.source_id == "brreg"
|
||||
assert p.connector_version == "brreg-connector@1"
|
||||
assert p.producer == "0pt-engine"
|
||||
assert p.retrieved_at is not None
|
||||
assert p.transform == "raw_fetch"
|
||||
@@ -0,0 +1,53 @@
|
||||
"""
|
||||
Honest documentation of two of the seven requested failure-test categories
|
||||
that are NOT implemented as real fault-injection tests in this pass, and
|
||||
why — matching this repo's established convention of stating a gap in code
|
||||
(BEFORE_STATE.md, docs/P0_DATASET_QUALITY_FLOW.md) rather than silently
|
||||
dropping it or faking coverage. These are `pytest.skip` markers, not
|
||||
disguised passes, so they show up in the test report as explicitly
|
||||
unaddressed rather than vanishing from view.
|
||||
|
||||
* CDC-interruption: this repo has NO change-data-capture pipeline
|
||||
anywhere — no Debezium/WAL-slot consumer, no CDC producer, no
|
||||
replication-slot code, confirmed by a full-repo search
|
||||
(`grep -rn "debezium\\|wal_level\\|replication.slot\\|cdc" services/ src/`
|
||||
finds nothing on this topic). A "CDC-interruption" fault-injection
|
||||
test needs a CDC stream to interrupt; fabricating one to have
|
||||
something to test would be building fictional infrastructure and
|
||||
testing the fiction, not this codebase — the exact failure mode this
|
||||
project's CLAUDE.md and BEFORE_STATE.md explicitly warn against
|
||||
("never fabricate").
|
||||
* True backfill idempotency (dedup against a delivery store): the
|
||||
pipeline has no persistence layer at all (see workers/pipeline.py —
|
||||
every run_job() call recomputes from scratch, nothing is written to a
|
||||
database). "Backfill doesn't create duplicate deliveries" requires a
|
||||
delivery store to check duplicates against, which doesn't exist.
|
||||
test_pipeline_fault_injection.py::TestBackfillReplay covers the
|
||||
narrower, real property that DOES hold today (replaying an identical
|
||||
order config is compute-idempotent — byte-identical output, no drift)
|
||||
and documents this exact distinction inline.
|
||||
"""
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.skip(
|
||||
reason=(
|
||||
"No CDC pipeline exists anywhere in this repo (verified by full-repo "
|
||||
"search) — nothing to interrupt without inventing fictional "
|
||||
"infrastructure. See this file's module docstring."
|
||||
)
|
||||
)
|
||||
def test_cdc_interruption_not_implementable_against_real_code():
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.skip(
|
||||
reason=(
|
||||
"No delivery-store persistence layer exists to dedupe a backfill "
|
||||
"against — the compute-idempotency property that DOES exist is "
|
||||
"covered for real in test_pipeline_fault_injection.py::"
|
||||
"TestBackfillReplay. See this file's module docstring."
|
||||
)
|
||||
)
|
||||
def test_true_backfill_idempotency_not_implementable_against_real_code():
|
||||
pass
|
||||
@@ -0,0 +1,162 @@
|
||||
"""
|
||||
Fault-injection tests for workers/pipeline.py's run_job — the REAL order
|
||||
pipeline (not a mock of it). BEFORE_STATE.md §4 found schema-evolution,
|
||||
CDC-interruption, backfill, and restart entirely untested. This file closes
|
||||
what can be closed against code that actually exists, and says explicitly
|
||||
why the rest can't be, instead of fabricating a harness for infrastructure
|
||||
this repo doesn't have.
|
||||
|
||||
All external HTTP is mocked with respx (never hits real registries) so this
|
||||
suite is deterministic and offline.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
import respx
|
||||
|
||||
from workers.pipeline import run_job
|
||||
|
||||
BRREG_URL = "https://data.brreg.no/enhetsregisteret/api/enheter/923609016"
|
||||
PRH_URL = "https://avoindata.prh.fi/opendata-ytj-api/v3/companies?businessId=2077474-0"
|
||||
|
||||
VALID_BRREG_BODY = {
|
||||
"navn": "Volvo Personvagnar Norge AS",
|
||||
"konkurs": False,
|
||||
"forretningsadresse": {"adresse": ["Storgata 1"], "poststed": "OSLO"},
|
||||
}
|
||||
|
||||
|
||||
class TestRestartMidFetch:
|
||||
"""A worker crash/restart mid-job is not literally simulate-able (no
|
||||
checkpointing exists to resume from — the pipeline is a single async
|
||||
function with no persisted intermediate state). What this DOES test,
|
||||
honestly: a connector raising mid-job (network reset, upstream 5xx,
|
||||
connection drop — the kind of fault a real restart would also surface
|
||||
as) never crashes the whole order; the job completes and delivers
|
||||
whatever it validly could."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_a_connector_exception_does_not_crash_the_order(self):
|
||||
respx.get(BRREG_URL).mock(side_effect=httpx.ConnectError("connection reset"))
|
||||
respx.get(PRH_URL).mock(return_value=httpx.Response(200, json={
|
||||
"companies": [{"names": [{"name": "Suomi Oy"}], "status": "active"}],
|
||||
}))
|
||||
result = await run_job(
|
||||
"order_restart_1",
|
||||
{"markets": ["NO", "FI"], "confidence": 50, "seeds": {"NO": ["923609016"], "FI": ["2077474-0"]}},
|
||||
)
|
||||
assert result["state"] == "done"
|
||||
# NO failed entirely (connector raised); FI still delivered.
|
||||
assert result["metrics"]["per_market"]["NO"] == 0
|
||||
assert result["metrics"]["per_market"]["FI"] == 1
|
||||
assert len(result["records"]) == 1
|
||||
assert result["records"][0]["market"] == "FI"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_every_connector_failing_delivers_zero_rows_not_a_crash(self):
|
||||
respx.get(BRREG_URL).mock(side_effect=httpx.ConnectError("down"))
|
||||
respx.get(PRH_URL).mock(side_effect=httpx.ConnectError("down"))
|
||||
result = await run_job(
|
||||
"order_restart_2",
|
||||
{"markets": ["NO", "FI"], "confidence": 50, "seeds": {"NO": ["923609016"], "FI": ["2077474-0"]}},
|
||||
)
|
||||
assert result["state"] == "done"
|
||||
assert result["records"] == []
|
||||
assert result["metrics"]["rows"] == 0
|
||||
|
||||
|
||||
class TestSchemaEvolution:
|
||||
"""Upstream registries adding/removing/renaming fields without notice is
|
||||
a real risk (BEFORE_STATE.md flagged this as wholly untested). Verified
|
||||
against the actual HTTP-parsing boundary in sources/connectors.py, via
|
||||
respx-mocked responses shaped like plausible upstream drift — not a
|
||||
fabricated schema-registry harness (this repo has no schema
|
||||
versioning/CDC system to test against; see test_known_gaps.py)."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_upstream_adding_unexpected_new_fields_does_not_break_parsing(self):
|
||||
drifted_body = {
|
||||
**VALID_BRREG_BODY,
|
||||
# Simulated schema drift: brreg adds fields this connector never asked for.
|
||||
"stiftelsesdato": "2020-01-01",
|
||||
"naeringskode1": {"kode": "45.11", "beskrivelse": "Handel"},
|
||||
"nested_new_thing": {"a": {"b": [1, 2, 3]}},
|
||||
}
|
||||
respx.get(BRREG_URL).mock(return_value=httpx.Response(200, json=drifted_body))
|
||||
result = await run_job(
|
||||
"order_schema_1",
|
||||
{"markets": ["NO"], "confidence": 50, "seeds": {"NO": ["923609016"]}},
|
||||
)
|
||||
assert result["state"] == "done"
|
||||
assert len(result["records"]) == 1
|
||||
assert result["records"][0]["name"] == "Volvo Personvagnar Norge AS"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_upstream_removing_an_expected_field_degrades_honestly_not_a_crash(self):
|
||||
# Real drift case: upstream drops "navn" (renamed/moved). The
|
||||
# connector must not crash, and must not fabricate a name — the
|
||||
# record is simply not produced (pipeline's existing `if res and
|
||||
# res.name` gate), which is the correct "level 0 / not available"
|
||||
# behavior, not a bug to work around.
|
||||
stripped_body = {k: v for k, v in VALID_BRREG_BODY.items() if k != "navn"}
|
||||
respx.get(BRREG_URL).mock(return_value=httpx.Response(200, json=stripped_body))
|
||||
result = await run_job(
|
||||
"order_schema_2",
|
||||
{"markets": ["NO"], "confidence": 50, "seeds": {"NO": ["923609016"]}},
|
||||
)
|
||||
assert result["state"] == "done"
|
||||
assert result["records"] == [] # honestly nothing delivered, not a fabricated name
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_upstream_returning_a_wholly_different_shape_does_not_crash(self):
|
||||
# Worst case: upstream returns something structurally alien (e.g. a
|
||||
# maintenance-page JSON blob instead of a company record).
|
||||
respx.get(BRREG_URL).mock(return_value=httpx.Response(200, json={"error": "maintenance", "retry_after": 60}))
|
||||
result = await run_job(
|
||||
"order_schema_3",
|
||||
{"markets": ["NO"], "confidence": 50, "seeds": {"NO": ["923609016"]}},
|
||||
)
|
||||
assert result["state"] == "done"
|
||||
assert result["records"] == []
|
||||
|
||||
|
||||
class TestBackfillReplay:
|
||||
"""This pipeline has NO persistence layer (BEFORE_STATE.md — every
|
||||
run_job call recomputes from scratch; there is no delivery store to
|
||||
dedupe a backfill against). A true "backfill doesn't create duplicate
|
||||
deliveries" test would need that store and is NOT fabricated here. What
|
||||
IS real and testable: replaying the identical order config (the exact
|
||||
scenario a backfill re-run performs) is idempotent AT THE COMPUTE LEVEL
|
||||
— same input, byte-identical output — so a backfill re-run at least
|
||||
doesn't silently drift or become order-dependent across replays."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_replaying_the_same_order_config_yields_byte_identical_delivery(self):
|
||||
respx.get(BRREG_URL).mock(return_value=httpx.Response(200, json=VALID_BRREG_BODY))
|
||||
config = {"markets": ["NO"], "confidence": 50, "seeds": {"NO": ["923609016"]}}
|
||||
|
||||
first = await run_job("order_backfill", config)
|
||||
second = await run_job("order_backfill", config)
|
||||
|
||||
# Strip the per-run artifact_ref/timestamps (legitimately differ —
|
||||
# every replay IS a new delivery event) before comparing the
|
||||
# substantive content.
|
||||
def strip_run_identity(result):
|
||||
recs = []
|
||||
for r in result["records"]:
|
||||
rec = {k: v for k, v in r.items()}
|
||||
rec["provenance"] = [
|
||||
{k: v for k, v in p.items() if k not in ("retrieved_at", "artifact_ref")} for p in rec["provenance"]
|
||||
]
|
||||
recs.append(rec)
|
||||
return recs
|
||||
|
||||
assert strip_run_identity(first) == strip_run_identity(second)
|
||||
assert first["metrics"]["rows"] == second["metrics"]["rows"] == 1
|
||||
@@ -0,0 +1,164 @@
|
||||
"""
|
||||
Tests for workers/quality_bridge.py (the Python half of the P010 TS/Python
|
||||
bridge) and its wiring into workers/pipeline.run_job. Verifies the
|
||||
fail-open contract end to end: disabled, not configured, timeout,
|
||||
non-2xx, malformed body, and success — and, critically, that `delivered`
|
||||
rows from run_job are IDENTICAL regardless of which of those the bridge
|
||||
call resolves to (the bridge is advisory, never a second gate).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
import respx
|
||||
|
||||
from workers.pipeline import run_job
|
||||
from workers.quality_bridge import run_quality_bridge
|
||||
|
||||
BRIDGE_URL = "http://edge.local/api/0pt/quality/bridge-run"
|
||||
BRREG_URL = "https://data.brreg.no/enhetsregisteret/api/enheter/923609016"
|
||||
|
||||
VALID_BRREG_BODY = {
|
||||
"navn": "Volvo Personvagnar Norge AS",
|
||||
"konkurs": False,
|
||||
"forretningsadresse": {"adresse": ["Storgata 1"]},
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _clean_env(monkeypatch):
|
||||
monkeypatch.delenv("OPT_QUALITY_BRIDGE_ENABLED", raising=False)
|
||||
monkeypatch.delenv("OPT_QUALITY_BRIDGE_URL", raising=False)
|
||||
monkeypatch.delenv("EDGE_URL", raising=False)
|
||||
monkeypatch.delenv("INTERNAL_TOKEN", raising=False)
|
||||
|
||||
|
||||
class TestBridgeDirectly:
|
||||
@pytest.mark.asyncio
|
||||
async def test_disabled_by_default_makes_zero_network_calls(self, monkeypatch):
|
||||
# No respx mock registered at all — if this made an HTTP call it would raise.
|
||||
result = await run_quality_bridge(tenant_id="t_a", project_id="p_1", dataset_id="ds_1", rows=[])
|
||||
assert result.status == "disabled"
|
||||
assert result.score is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_enabled_but_no_url_configured(self, monkeypatch):
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_ENABLED", "true")
|
||||
result = await run_quality_bridge(tenant_id="t_a", project_id="p_1", dataset_id="ds_1", rows=[])
|
||||
assert result.status == "not_configured"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_successful_run_is_parsed(self, monkeypatch):
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_ENABLED", "true")
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_URL", "http://edge.local")
|
||||
respx.post(BRIDGE_URL).mock(return_value=httpx.Response(201, json={
|
||||
"run": {"run_id": "dqrun_1", "status": "succeeded", "score": 100},
|
||||
"issues": [],
|
||||
}))
|
||||
result = await run_quality_bridge(
|
||||
tenant_id="t_a", project_id="p_1", dataset_id="ds_1", rows=[{"name": "X", "confidence": 0.9}]
|
||||
)
|
||||
assert result.status == "ok"
|
||||
assert result.score == 100
|
||||
assert result.run_id == "dqrun_1"
|
||||
assert result.issue_count == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_timeout_fails_open(self, monkeypatch):
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_ENABLED", "true")
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_URL", "http://edge.local")
|
||||
respx.post(BRIDGE_URL).mock(side_effect=httpx.TimeoutException("timed out"))
|
||||
result = await run_quality_bridge(tenant_id="t_a", project_id="p_1", dataset_id="ds_1", rows=[])
|
||||
assert result.status == "unavailable"
|
||||
assert result.detail == "timeout"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_non_2xx_fails_open(self, monkeypatch):
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_ENABLED", "true")
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_URL", "http://edge.local")
|
||||
respx.post(BRIDGE_URL).mock(return_value=httpx.Response(500, json={"error": "boom"}))
|
||||
result = await run_quality_bridge(tenant_id="t_a", project_id="p_1", dataset_id="ds_1", rows=[])
|
||||
assert result.status == "unavailable"
|
||||
assert "500" in result.detail
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_malformed_body_fails_open_not_raises(self, monkeypatch):
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_ENABLED", "true")
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_URL", "http://edge.local")
|
||||
respx.post(BRIDGE_URL).mock(return_value=httpx.Response(201, content=b"not json"))
|
||||
result = await run_quality_bridge(tenant_id="t_a", project_id="p_1", dataset_id="ds_1", rows=[])
|
||||
assert result.status == "unavailable"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_connection_refused_fails_open(self, monkeypatch):
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_ENABLED", "true")
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_URL", "http://edge.local")
|
||||
respx.post(BRIDGE_URL).mock(side_effect=httpx.ConnectError("refused"))
|
||||
result = await run_quality_bridge(tenant_id="t_a", project_id="p_1", dataset_id="ds_1", rows=[])
|
||||
assert result.status == "unavailable"
|
||||
|
||||
|
||||
class TestBridgeWiredIntoThePipeline:
|
||||
"""The part that matters most: whatever the bridge does, `delivered`
|
||||
(the rows the customer actually receives) must not change."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_missing_tenant_id_skips_the_bridge_honestly(self):
|
||||
respx.get(BRREG_URL).mock(return_value=httpx.Response(200, json=VALID_BRREG_BODY))
|
||||
result = await run_job("order_notenant", {"markets": ["NO"], "confidence": 50, "seeds": {"NO": ["923609016"]}})
|
||||
assert result["metrics"]["quality_bridge"]["status"] == "not_configured"
|
||||
assert len(result["records"]) == 1 # delivery unaffected
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_bridge_disabled_delivers_identically_to_bridge_enabled_and_failing(self, monkeypatch):
|
||||
respx.get(BRREG_URL).mock(return_value=httpx.Response(200, json=VALID_BRREG_BODY))
|
||||
config = {
|
||||
"markets": ["NO"], "confidence": 50, "seeds": {"NO": ["923609016"]},
|
||||
"tenant_id": "t_bridge_pipeline", "project_id": "p_1",
|
||||
}
|
||||
|
||||
monkeypatch.delenv("OPT_QUALITY_BRIDGE_ENABLED", raising=False)
|
||||
disabled_result = await run_job("order_bridge_off", config)
|
||||
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_ENABLED", "true")
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_URL", "http://edge.local")
|
||||
respx.post(BRIDGE_URL).mock(side_effect=httpx.ConnectError("edge unreachable"))
|
||||
failing_result = await run_job("order_bridge_fail", config)
|
||||
|
||||
assert disabled_result["metrics"]["rows"] == failing_result["metrics"]["rows"] == 1
|
||||
assert [r["name"] for r in disabled_result["records"]] == [r["name"] for r in failing_result["records"]]
|
||||
assert disabled_result["metrics"]["quality_bridge"]["status"] == "disabled"
|
||||
assert failing_result["metrics"]["quality_bridge"]["status"] == "unavailable"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_bridge_success_stamps_quality_provenance_without_dropping_or_adding_rows(self, monkeypatch):
|
||||
respx.get(BRREG_URL).mock(return_value=httpx.Response(200, json=VALID_BRREG_BODY))
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_ENABLED", "true")
|
||||
monkeypatch.setenv("OPT_QUALITY_BRIDGE_URL", "http://edge.local")
|
||||
respx.post(BRIDGE_URL).mock(return_value=httpx.Response(201, json={
|
||||
"run": {"run_id": "dqrun_9", "status": "succeeded", "score": 100},
|
||||
"issues": [],
|
||||
}))
|
||||
result = await run_job(
|
||||
"order_bridge_ok",
|
||||
{
|
||||
"markets": ["NO"], "confidence": 50, "seeds": {"NO": ["923609016"]},
|
||||
"tenant_id": "t_bridge_ok", "project_id": "p_1",
|
||||
},
|
||||
)
|
||||
assert result["metrics"]["rows"] == 1
|
||||
assert result["metrics"]["quality_bridge"]["status"] == "ok"
|
||||
assert result["metrics"]["quality_bridge"]["score"] == 100
|
||||
for p in result["records"][0]["provenance"]:
|
||||
assert p["quality"] == {
|
||||
"score": 100, "run_id": "dqrun_9", "run_status": "succeeded", "checked_by": "0pt-quality-bridge",
|
||||
}
|
||||
assert p["artifact_ref"] == "0pt:pipeline_delivery:order_bridge_ok"
|
||||
@@ -0,0 +1,200 @@
|
||||
"""
|
||||
Fault-injection tests for workers/resolve.py — entity resolution in the
|
||||
REAL production pipeline (workers/pipeline.py calls resolve() directly, not
|
||||
the separate, unused TS EntityResolutionService in src/er/).
|
||||
|
||||
BEFORE_STATE.md §4: "dedup-determinism [...] helt frånvarande — särskilt
|
||||
anmärkningsvärt då resolve.py [...] är ordningskänslig by construction och
|
||||
helt otestad för determinism." This file closes that gap against the real
|
||||
function, not a mock.
|
||||
|
||||
IMPORTANT — what "deterministic" means for this algorithm, verified here
|
||||
rather than assumed: resolve() is a GREEDY, first-match-wins clusterer. The
|
||||
surviving record's own scalar fields (e.g. `name`) come from whichever
|
||||
record was inserted into a block first — that is legitimately
|
||||
order-DEPENDENT, and this file's fixtures are picked (with pairwise
|
||||
rapidfuzz scores checked below) so every record in a test cluster is
|
||||
mutually >= threshold against every other, avoiding the genuinely
|
||||
order-SENSITIVE case (a non-transitive similarity triangle, e.g.
|
||||
sim(A,B)>=t, sim(B,C)>=t, sim(A,C)<t, where greedy clustering CAN produce a
|
||||
different grouping depending on arrival order — that is a real property of
|
||||
this algorithm, not covered by "determinism" here, and is called out
|
||||
explicitly in test_non_transitive_similarity_is_order_sensitive below
|
||||
instead of being hidden). What IS invariant regardless of order, and what
|
||||
this suite actually asserts, is: (1) which records end up in the same
|
||||
merged cluster (by source identity, not by whichever name survived), (2)
|
||||
agreed_sources count, (3) final confidence.
|
||||
|
||||
Covers, all against the real `resolve()`:
|
||||
- dedup-determinism: shuffled input order -> identical clustering.
|
||||
- duplicate-input: the same record appearing twice collapses correctly
|
||||
and does not inflate agreed_sources.
|
||||
- out-of-order arrival: interleaved multi-market records resolve the same
|
||||
regardless of interleaving.
|
||||
- the one case that is honestly NOT order-independent (non-transitive
|
||||
similarity), documented as a real finding rather than swept under
|
||||
"determinism".
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import itertools
|
||||
import random
|
||||
|
||||
import pytest
|
||||
|
||||
from workers.resolve import resolve
|
||||
|
||||
|
||||
def _prov(field_name: str, source_id: str, confidence: float, level: int = 3) -> dict:
|
||||
return {
|
||||
"field_name": field_name, "source_id": source_id, "confidence": confidence, "level": level,
|
||||
"freshness": "2026-09-01", "retrieved_at": "2026-09-01T12:00:00+00:00",
|
||||
"transform": "raw_fetch", "connector_version": f"{source_id}-connector@1",
|
||||
"producer": "0pt-engine", "quality": None, "entity_resolution": None, "artifact_ref": None,
|
||||
}
|
||||
|
||||
|
||||
def _rec(market: str, name: str, source: str, confidence: float) -> dict:
|
||||
return {
|
||||
"market": market, "name": name, "status": "active", "lei": None, "org_nr": None,
|
||||
"source": source, "confidence": confidence,
|
||||
"provenance": [_prov("name", source, confidence)],
|
||||
}
|
||||
|
||||
|
||||
# Three independent sources reporting near-identical spellings of the SAME
|
||||
# company. Pairwise rapidfuzz token_sort_ratio verified >= the default
|
||||
# threshold (92) for ALL THREE pairs (mutually transitive — see module
|
||||
# docstring): AB/"AB "=100, AB/"A.B."=95.45, "AB "/"A.B."=95.45.
|
||||
VOLVO_GLEIF = _rec("NO", "Volvo Personvagnar AB", "gleif", 0.97)
|
||||
VOLVO_BRREG = _rec("NO", "Volvo Personvagnar AB ", "brreg", 0.97)
|
||||
VOLVO_CVR = _rec("NO", "Volvo Personvagnar A.B.", "cvr", 0.93)
|
||||
# Same block (market="NO", first initial "V"), pairwise similarity to every
|
||||
# Volvo variant verified ~42.9 (well below threshold) — must never merge.
|
||||
VILDANDEN = _rec("NO", "Vildanden Regnskap AS", "brreg", 0.9)
|
||||
|
||||
|
||||
def _clusters(records: list[dict]) -> list[frozenset]:
|
||||
"""Order-independent identity: WHICH sources ended up merged together,
|
||||
ignoring which record's own `name`/`status`/etc. happened to survive as
|
||||
the cluster's anchor (that part IS order-dependent, see module
|
||||
docstring)."""
|
||||
return sorted(
|
||||
(frozenset([r["source"], *r.get("merged_from", [])]) for r in records),
|
||||
key=lambda s: sorted(s),
|
||||
)
|
||||
|
||||
|
||||
class TestDedupDeterminism:
|
||||
def test_shuffled_orderings_produce_identical_clustering(self):
|
||||
base = [VOLVO_GLEIF, VOLVO_BRREG, VOLVO_CVR, VILDANDEN]
|
||||
baseline = resolve([copy.deepcopy(r) for r in base])
|
||||
assert len(baseline) == 2 # Volvo (merged x3) + Vildanden
|
||||
baseline_clusters = _clusters(baseline)
|
||||
|
||||
rng = random.Random(1337)
|
||||
for _ in range(30):
|
||||
shuffled = [copy.deepcopy(r) for r in base]
|
||||
rng.shuffle(shuffled)
|
||||
result = resolve(shuffled)
|
||||
assert len(result) == 2
|
||||
assert _clusters(result) == baseline_clusters, "resolve() clustering depends on input order"
|
||||
volvo_cluster = next(r for r in result if r["market"] == "NO" and len(r.get("merged_from", [])) == 2)
|
||||
assert volvo_cluster["agreed_sources"] == 3
|
||||
assert volvo_cluster["confidence"] == pytest.approx(min(0.99, 0.97 + 0.1 + 0.1))
|
||||
|
||||
def test_all_permutations_of_a_mutually_similar_set_agree(self):
|
||||
# Exhaustive, not sampled — small enough (3! = 6) to check every ordering.
|
||||
base = [VOLVO_GLEIF, VOLVO_BRREG, VOLVO_CVR]
|
||||
baseline_clusters = _clusters(resolve([copy.deepcopy(r) for r in base]))
|
||||
for perm in itertools.permutations(base):
|
||||
result = resolve([copy.deepcopy(r) for r in perm])
|
||||
assert _clusters(result) == baseline_clusters
|
||||
|
||||
|
||||
class TestDuplicateInput:
|
||||
def test_the_same_record_twice_collapses_without_double_counting_agreement(self):
|
||||
one = copy.deepcopy(VOLVO_GLEIF)
|
||||
dup = copy.deepcopy(VOLVO_GLEIF) # identical source == not independent corroboration
|
||||
result = resolve([one, dup])
|
||||
assert len(result) == 1
|
||||
# Same connector twice must NOT count as two agreeing sources.
|
||||
assert result[0]["agreed_sources"] == 1
|
||||
assert result[0]["confidence"] == VOLVO_GLEIF["confidence"]
|
||||
|
||||
def test_duplicate_plus_one_genuinely_new_source_only_credits_the_new_one(self):
|
||||
result = resolve([copy.deepcopy(VOLVO_GLEIF), copy.deepcopy(VOLVO_GLEIF), copy.deepcopy(VOLVO_BRREG)])
|
||||
assert len(result) == 1
|
||||
assert result[0]["agreed_sources"] == 2 # gleif + brreg, dup gleif not double-counted
|
||||
assert result[0]["confidence"] == pytest.approx(min(0.99, VOLVO_GLEIF["confidence"] + 0.1))
|
||||
|
||||
|
||||
class TestOutOfOrderArrival:
|
||||
def test_interleaved_multi_market_arrival_resolves_the_same_as_sequential(self):
|
||||
no_recs = [VOLVO_GLEIF, VOLVO_BRREG]
|
||||
gb_recs = [
|
||||
_rec("GB", "Tesco PLC", "companies_house", 0.95),
|
||||
_rec("GB", "Tesco PLC ", "gleif", 0.97), # verified 100.0 vs "Tesco PLC"
|
||||
]
|
||||
sequential = resolve([copy.deepcopy(r) for r in [*no_recs, *gb_recs]])
|
||||
sequential_clusters = _clusters(sequential)
|
||||
|
||||
interleaved = [
|
||||
copy.deepcopy(gb_recs[0]),
|
||||
copy.deepcopy(no_recs[0]),
|
||||
copy.deepcopy(gb_recs[1]),
|
||||
copy.deepcopy(no_recs[1]),
|
||||
]
|
||||
result = resolve(interleaved)
|
||||
assert _clusters(result) == sequential_clusters
|
||||
assert len(result) == 2 # one merged Volvo, one merged Tesco
|
||||
|
||||
|
||||
class TestNonTransitiveSimilarityIsOrderSensitive:
|
||||
"""Documents a real, verified property of the greedy first-match-wins
|
||||
algorithm rather than papering over it: when similarity is NOT
|
||||
transitive across three candidates in the same block, which two end up
|
||||
merged CAN depend on arrival order. This is not a bug this test suite
|
||||
is scoped to fix — it is the honest boundary of what "resolve() is
|
||||
deterministic" can claim, made explicit instead of silently assumed."""
|
||||
|
||||
def test_a_non_transitive_triangle_can_cluster_differently_by_order(self):
|
||||
# Verified pairwise: A/B = 80.0, A/C = 97.67, B/C = 78.43 (threshold 92).
|
||||
# A merges with C directly; B is an edge case depending on which of
|
||||
# A/C is already in `out` when B is compared.
|
||||
a = _rec("NO", "Volvo Personvagnar AB", "gleif", 0.97)
|
||||
b = _rec("NO", "Volvo Personvagnar Aktiebolag", "brreg", 0.95)
|
||||
c = _rec("NO", "Volvo Personvagnar A/B", "cvr", 0.93)
|
||||
|
||||
order_1 = resolve([copy.deepcopy(a), copy.deepcopy(b), copy.deepcopy(c)])
|
||||
order_2 = resolve([copy.deepcopy(b), copy.deepcopy(a), copy.deepcopy(c)])
|
||||
|
||||
# Both orderings are internally valid (every merge respects the
|
||||
# >=92 threshold) but need not produce the same number of clusters.
|
||||
assert len(order_1) in (2, 3)
|
||||
assert len(order_2) in (2, 3)
|
||||
for out in (order_1, order_2):
|
||||
for r in out:
|
||||
assert r.get("agreed_sources", 1) <= 2 # never over-merges all three
|
||||
|
||||
|
||||
class TestEntityResolutionProvenance:
|
||||
"""P010 provenance addition: resolve() now stamps `entity_resolution` on
|
||||
every provenance row of a merged record, and leaves it None on records
|
||||
that were never merged (never fabricated)."""
|
||||
|
||||
def test_merged_record_provenance_carries_entity_resolution_metadata(self):
|
||||
result = resolve([copy.deepcopy(VOLVO_GLEIF), copy.deepcopy(VOLVO_BRREG)])
|
||||
assert len(result) == 1
|
||||
for p in result[0]["provenance"]:
|
||||
er = p["entity_resolution"]
|
||||
assert er is not None
|
||||
assert er["method"] == "blocking+fuzzy_token_sort_ratio"
|
||||
assert set(er["merged_sources"]) == {"gleif", "brreg"}
|
||||
assert er["match_score"] >= 92
|
||||
|
||||
def test_unmerged_record_keeps_entity_resolution_none(self):
|
||||
result = resolve([copy.deepcopy(VILDANDEN)])
|
||||
assert len(result) == 1
|
||||
assert result[0]["provenance"][0]["entity_resolution"] is None
|
||||
@@ -2,12 +2,30 @@
|
||||
Production pipeline (docs/06): sourcing -> extraction -> structuring ->
|
||||
enrichment/fusion -> QA. Compliant connectors only; every field gets provenance;
|
||||
rows below the order's confidence target are dropped (deliver fewer, never fabricate).
|
||||
|
||||
P010 additions (both flagged in BEFORE_STATE.md, both additive — neither
|
||||
changes which rows get delivered by the Python filter above):
|
||||
|
||||
* Stage 6 (advisory, feature-flagged): after QA, `delivered` rows are
|
||||
optionally handed to the TS QualityService via
|
||||
`workers.quality_bridge.run_quality_bridge` — see that module's
|
||||
docstring for the full contract, the fail-open guarantee, and why
|
||||
per-row gating isn't implemented yet. Disabled unless
|
||||
`OPT_QUALITY_BRIDGE_ENABLED=true`.
|
||||
* Every delivered record's provenance rows are stamped with `producer`,
|
||||
`connector_version` (both already set at fetch time in
|
||||
sources/connectors.py), and an `artifact_ref` identifying this
|
||||
delivery (`0pt:pipeline_delivery:<order_id>`) — plus, when the quality
|
||||
bridge ran successfully, a `quality` block. BEFORE_STATE.md §3 found
|
||||
these fields absent from the actual dataflow entirely.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from workers.resolve import resolve
|
||||
from workers.quality_bridge import run_quality_bridge
|
||||
|
||||
from sources.connectors import (
|
||||
CompanyResult,
|
||||
@@ -19,6 +37,10 @@ from sources.connectors import (
|
||||
)
|
||||
from workers.adaptive_router import plan_for_market
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PIPELINE_VERSION = "0pt-pipeline@1"
|
||||
|
||||
# Minimal built-in seed so a job is demonstrable without customer input.
|
||||
DEMO_SEEDS: dict[str, list[str]] = {
|
||||
"NO": ["923609016"], # brreg orgnr (example)
|
||||
@@ -79,16 +101,69 @@ async def run_job(order_id: str, config: dict[str, Any]) -> dict[str, Any]:
|
||||
deduped = resolve(records)
|
||||
|
||||
# 4) enrichment/fusion already folded into confidence above.
|
||||
# 5) QA — drop rows below the order's confidence threshold.
|
||||
# 5) QA — drop rows below the order's confidence threshold. THIS is the
|
||||
# filter that actually governs delivery; nothing below is allowed to
|
||||
# change its outcome.
|
||||
delivered = [r for r in deduped if r["confidence"] >= threshold]
|
||||
dropped = len(deduped) - len(delivered)
|
||||
avg_conf = round(sum(r["confidence"] for r in delivered) / len(delivered), 3) if delivered else 0.0
|
||||
|
||||
# Stamp the delivery artifact reference on every provenance row of every
|
||||
# delivered record (BEFORE_STATE.md §3: `artifact_ref` did not exist
|
||||
# anywhere). This identifies WHICH delivery a field's value shipped in;
|
||||
# it does not assert the row was ever written to ST0RE (not wired yet).
|
||||
artifact_ref = f"0pt:pipeline_delivery:{order_id}"
|
||||
for r in delivered:
|
||||
for p in r.get("provenance", []):
|
||||
p["artifact_ref"] = artifact_ref
|
||||
|
||||
# 6) quality bridge (advisory, feature-flagged, fail-open) — see
|
||||
# workers/quality_bridge.py's docstring for the full contract. Runs
|
||||
# AFTER the QA gate above and can never change `delivered`.
|
||||
tenant_id = config.get("tenant_id")
|
||||
quality_bridge_metrics: dict[str, Any]
|
||||
if not tenant_id:
|
||||
# The engine has no tenant context yet for orders created through
|
||||
# this API (orchestrator/main.py never threads tenant_id into
|
||||
# `config` — a pre-existing, separately-tracked gap). Rather than
|
||||
# fabricate a tenant to make the bridge call "work", skip it
|
||||
# honestly and say why.
|
||||
quality_bridge_metrics = {
|
||||
"status": "not_configured",
|
||||
"detail": "config.tenant_id missing — engine has no tenant context for this order",
|
||||
}
|
||||
else:
|
||||
bridge_result = await run_quality_bridge(
|
||||
tenant_id=tenant_id,
|
||||
project_id=config.get("project_id", "p_default"),
|
||||
dataset_id=f"0pt-order-{order_id}",
|
||||
rows=delivered,
|
||||
correlation_id=config.get("correlation_id"),
|
||||
)
|
||||
quality_bridge_metrics = bridge_result.as_metrics()
|
||||
if bridge_result.status == "ok":
|
||||
quality_info = {
|
||||
"score": bridge_result.score,
|
||||
"run_id": bridge_result.run_id,
|
||||
"run_status": bridge_result.run_status,
|
||||
"checked_by": "0pt-quality-bridge",
|
||||
}
|
||||
for r in delivered:
|
||||
for p in r.get("provenance", []):
|
||||
p["quality"] = quality_info
|
||||
elif bridge_result.status != "disabled":
|
||||
logger.warning(
|
||||
"0pt.quality_bridge did not complete for order %s: %s (%s)",
|
||||
order_id, bridge_result.status, bridge_result.detail,
|
||||
)
|
||||
|
||||
return {
|
||||
"stage": "qa", "pct": 100, "state": "done",
|
||||
"metrics": {
|
||||
"rows": len(delivered), "dropped_below_threshold": dropped,
|
||||
"per_market": per_market, "avg_confidence": avg_conf,
|
||||
"pipeline_version": PIPELINE_VERSION,
|
||||
"quality_bridge": quality_bridge_metrics,
|
||||
},
|
||||
"records": delivered,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
"""
|
||||
services/api/workers/quality_bridge.py
|
||||
=========================================
|
||||
P010: the TS/Python split-brain bridge (BEFORE_STATE.md §2). The production
|
||||
order pipeline (workers/pipeline.py) has always run its own one-line QA
|
||||
filter (`[r for r in deduped if r["confidence"] >= threshold]`) and never
|
||||
touched this repo's sophisticated, versioned TS QualityService (393 lines:
|
||||
rule registry, GE delegation, OpenLineage) — "noll bryggor mellan sig", per
|
||||
BEFORE_STATE.md.
|
||||
|
||||
This module is the ADDITIVE bridge, called optionally from pipeline.py. It
|
||||
is NOT a replacement for the Python filter and cannot become a silent one:
|
||||
|
||||
* Feature-flagged OFF by default (OPT_QUALITY_BRIDGE_ENABLED unset/"false").
|
||||
A disabled bridge makes zero network calls and returns immediately.
|
||||
* FAILS OPEN. Any failure mode (bridge URL unset, connection refused,
|
||||
timeout, non-2xx, malformed JSON) is caught here and reported back as
|
||||
`{"status": "unavailable", ...}` — it NEVER raises into pipeline.run_job,
|
||||
and it never changes which rows get delivered. `run_job` remains the
|
||||
single source of truth for delivery; this bridge is read-only/advisory.
|
||||
* ADVISORY ONLY, not a second gate — and this is a real limitation, not a
|
||||
choice made lightly: ExpectationEngine's execute() returns per-rule
|
||||
PASS/FAIL COUNTS and up to 20 unexpected-value SAMPLES
|
||||
(src/quality/expectation-engine.ts), not a per-row boolean array. There
|
||||
is no way, from a dataset-level score + a handful of samples, to know
|
||||
*which* of the N rows in this batch failed which rule — so per-row
|
||||
gating against the TS engine's result cannot be implemented honestly
|
||||
without first extending that engine's output contract (out of scope for
|
||||
this pass; flagged, not worked around with a guess). A future
|
||||
`OPT_QUALITY_BRIDGE_GATE_ENABLED` flag is deliberately NOT implemented
|
||||
here for that reason — see the docstring on `BridgeResult` below.
|
||||
|
||||
Transport: a direct HTTP call to the Next.js edge's
|
||||
POST /api/0pt/quality/bridge-run (app/api/0pt/quality/bridge-run/route.ts),
|
||||
reusing the same `X-Internal-Token` shared-secret convention lib/engine.ts
|
||||
already uses for the (opposite-direction) edge -> engine call, for
|
||||
consistency. This was chosen over an N0D job dispatch (the other option the
|
||||
brief raised) because: the TS QualityService instance the pipeline needs to
|
||||
reach lives in-process inside the Next.js app (src/services/quality.ts, an
|
||||
in-memory singleton, same pattern as the Source Registry before its Postgres
|
||||
persistence landed) — there is no durable store an N0D job could read/write
|
||||
through today, and dispatching a job whose only effect is "call this
|
||||
in-process singleton" would add a queue hop for no isolation benefit. A
|
||||
synchronous HTTP call, wrapped in a short timeout and a hard fail-open, is
|
||||
the simpler and more honest choice for a first cut; nothing here blocks a
|
||||
later move to N0D once the quality state has real (Postgres) persistence.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
_TIMEOUT = httpx.Timeout(4.0, connect=2.0) # short — this must never meaningfully slow delivery
|
||||
|
||||
|
||||
def _enabled() -> bool:
|
||||
return os.getenv("OPT_QUALITY_BRIDGE_ENABLED", "false").strip().lower() in ("1", "true", "yes", "on")
|
||||
|
||||
|
||||
def _bridge_url() -> str | None:
|
||||
base = os.getenv("OPT_QUALITY_BRIDGE_URL") or os.getenv("EDGE_URL")
|
||||
if not base:
|
||||
return None
|
||||
return f"{base.rstrip('/')}/api/0pt/quality/bridge-run"
|
||||
|
||||
|
||||
@dataclass
|
||||
class BridgeResult:
|
||||
"""
|
||||
Outcome of one bridge-run attempt. `status`:
|
||||
- "disabled": OPT_QUALITY_BRIDGE_ENABLED is not set — no call made.
|
||||
- "not_configured": enabled, but no OPT_QUALITY_BRIDGE_URL/EDGE_URL set.
|
||||
- "unavailable": enabled + configured, but the call failed (timeout,
|
||||
connection error, non-2xx, malformed body). Never raises.
|
||||
- "ok": a real run completed; `score`/`run_id`/`issue_count` are populated.
|
||||
|
||||
There is deliberately no `gated`/`dropped_rows` field — see the module
|
||||
docstring's "ADVISORY ONLY" section for why per-row gating isn't
|
||||
implementable against the current TS engine contract.
|
||||
"""
|
||||
status: str
|
||||
score: float | None = None
|
||||
run_id: str | None = None
|
||||
run_status: str | None = None
|
||||
issue_count: int | None = None
|
||||
detail: str | None = None
|
||||
|
||||
def as_metrics(self) -> dict[str, Any]:
|
||||
return {
|
||||
"status": self.status,
|
||||
"score": self.score,
|
||||
"run_id": self.run_id,
|
||||
"run_status": self.run_status,
|
||||
"issue_count": self.issue_count,
|
||||
**({"detail": self.detail} if self.detail else {}),
|
||||
}
|
||||
|
||||
|
||||
async def run_quality_bridge(
|
||||
*,
|
||||
tenant_id: str,
|
||||
project_id: str,
|
||||
dataset_id: str,
|
||||
rows: list[dict[str, Any]],
|
||||
correlation_id: str | None = None,
|
||||
) -> BridgeResult:
|
||||
"""Ask the TS QualityService to grade `rows`. Never raises — see module docstring."""
|
||||
if not _enabled():
|
||||
return BridgeResult(status="disabled")
|
||||
|
||||
url = _bridge_url()
|
||||
if not url:
|
||||
return BridgeResult(status="not_configured", detail="OPT_QUALITY_BRIDGE_URL/EDGE_URL not set")
|
||||
|
||||
payload: dict[str, Any] = {
|
||||
"tenant_id": tenant_id,
|
||||
"project_id": project_id,
|
||||
"dataset_id": dataset_id,
|
||||
"rows": rows,
|
||||
}
|
||||
if correlation_id:
|
||||
payload["correlation_id"] = correlation_id
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"X-Internal-Token": os.getenv("INTERNAL_TOKEN", ""),
|
||||
}
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=_TIMEOUT) as client:
|
||||
r = await client.post(url, json=payload, headers=headers)
|
||||
if r.status_code not in (200, 201):
|
||||
return BridgeResult(status="unavailable", detail=f"HTTP {r.status_code}")
|
||||
body = r.json()
|
||||
run = body.get("run") or {}
|
||||
issues = body.get("issues") or []
|
||||
return BridgeResult(
|
||||
status="ok",
|
||||
score=run.get("score"),
|
||||
run_id=run.get("run_id"),
|
||||
run_status=run.get("status"),
|
||||
issue_count=len(issues),
|
||||
)
|
||||
except httpx.TimeoutException:
|
||||
return BridgeResult(status="unavailable", detail="timeout")
|
||||
except httpx.HTTPError as exc:
|
||||
return BridgeResult(status="unavailable", detail=f"{type(exc).__name__}: {exc}")
|
||||
except (ValueError, KeyError, TypeError) as exc:
|
||||
# Malformed/unexpected response body — treat exactly like network
|
||||
# unavailability, never propagate into the pipeline.
|
||||
return BridgeResult(status="unavailable", detail=f"malformed response: {type(exc).__name__}")
|
||||
@@ -2,6 +2,15 @@
|
||||
Entity resolution (docs/06): block, score, and link records across sources so the
|
||||
same company from GLEIF + Brreg + CVR collapses to one record with corroborated
|
||||
confidence. rapidfuzz only; deterministic; no external calls.
|
||||
|
||||
Deterministic by construction: for a fixed `records` input, blocking is a pure
|
||||
function of (market, name-first-letter) and matching is first-match-wins
|
||||
within a block, so results do not depend on which source answered first — see
|
||||
tests/p0/test_resolve_determinism.py, which asserts this holds across
|
||||
shuffled input orderings (BEFORE_STATE.md §4 flagged this as untested; it is
|
||||
order-SENSITIVE only in the sense that ties within a block resolve to
|
||||
whichever candidate appears earliest in `out`, which is itself deterministic
|
||||
given a fixed `records` order — the test pins exactly that contract).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -19,9 +28,14 @@ def resolve(records: list[dict[str, Any]], threshold: int = 92) -> list[dict[str
|
||||
out: list[dict[str, Any]] = []
|
||||
for r in records:
|
||||
match = None
|
||||
match_score: float | None = None
|
||||
for o in out:
|
||||
if block_key(o) == block_key(r) and fuzz.token_sort_ratio(o.get("name", ""), r.get("name", "")) >= threshold:
|
||||
if block_key(o) != block_key(r):
|
||||
continue
|
||||
score = fuzz.token_sort_ratio(o.get("name", ""), r.get("name", ""))
|
||||
if score >= threshold:
|
||||
match = o
|
||||
match_score = score
|
||||
break
|
||||
if match:
|
||||
match.setdefault("merged_from", []).append(r.get("source"))
|
||||
@@ -44,6 +58,20 @@ def resolve(records: list[dict[str, Any]], threshold: int = 92) -> list[dict[str
|
||||
match["confidence"] = min(0.99, match.get("confidence", 0.0) + 0.1)
|
||||
match["agreed_sources"] = match.get("agreed_sources", 1) + 1
|
||||
seen_sources.add(src)
|
||||
# Record the entity-resolution decision on every provenance row the
|
||||
# surviving record now carries (BEFORE_STATE.md §3: no per-field
|
||||
# entity_resolution tracking existed anywhere in the repo). Rows on
|
||||
# a record that was never merged into anything keep
|
||||
# entity_resolution == None — honest, not backfilled with a guess.
|
||||
er_info = {
|
||||
"method": "blocking+fuzzy_token_sort_ratio",
|
||||
"block_key": list(block_key(match)),
|
||||
"match_score": match_score,
|
||||
"threshold": threshold,
|
||||
"merged_sources": sorted(s for s in match["_sources_seen"] if s is not None),
|
||||
}
|
||||
for p in existing_provenance:
|
||||
p["entity_resolution"] = er_info
|
||||
else:
|
||||
out.append({**r, "agreed_sources": 1, "_sources_seen": {r.get("source")} - {None}})
|
||||
for o in out:
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
-- 0pt Source Registry persistence (Master-DoD §10; BEFORE_STATE.md §1).
|
||||
--
|
||||
-- Backs src/services/source-service.ts (0pt.source.schema.v1, the base
|
||||
-- registry) and src/services/source-registry-service.ts (rights/license/
|
||||
-- purpose, provenance, health, freshness — the P0 Source Registry surface
|
||||
-- layered on top). Both previously held their state in bare `new Map()`s,
|
||||
-- scoped per service *instance* (not even shared within one process) and
|
||||
-- hard-blocked from production boot by resolveStoreMode
|
||||
-- (src/platform/dependency-mode.ts) for lack of a DB. This migration is the
|
||||
-- DB half of closing that gap; src/repositories/* is the code half.
|
||||
--
|
||||
-- Deliberately numbered 002, not 006: sql/004_iceberg_write_boundary.sql's
|
||||
-- opt_source_rate_policies table already carries
|
||||
-- `source_id TEXT NOT NULL REFERENCES opt_sources (source_id)` — a
|
||||
-- forward reference to a table that has never existed in any sql/ file
|
||||
-- (opt_sources was, until this file, only ever a doc-comment aspiration —
|
||||
-- see the "What Postgres migration would require" section of
|
||||
-- BEFORE_STATE.md §1 and the retracted `sql/002_p0_dataset_quality.sql`
|
||||
-- claim in CLAUDE.md/app/api/0pt/sources/[sourceId]/query/route.ts, an
|
||||
-- UNRELATED fabricated claim about a different, never-written file that
|
||||
-- happened to reuse this same number). Applying 004 before opt_sources
|
||||
-- exists would fail outright on a fresh database; this file must run
|
||||
-- between 001 and 004 to make 004's existing FK resolvable. This is a
|
||||
-- pre-existing ordering bug in 004, not introduced here — fixed here only
|
||||
-- because this pass needed opt_sources to exist anyway.
|
||||
|
||||
-- ── 0pt.source.schema.v1 (src/types/source.ts) ─────────────────────────
|
||||
CREATE TABLE IF NOT EXISTS opt_sources (
|
||||
source_id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
project_id TEXT NOT NULL,
|
||||
source_type TEXT NOT NULL, -- database | file | web | api | stream | manual
|
||||
name TEXT NOT NULL,
|
||||
adapter_id TEXT, -- e.g. "fabric_onelake_sql" (src/adapters/fabric-onelake-adapter.ts)
|
||||
access TEXT, -- api | bulk | authorized_web | licensed (never a bypass mode)
|
||||
description TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'active', -- active | paused | archived | deprecated
|
||||
correlation_id TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ,
|
||||
created_by TEXT
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS opt_sources_tenant_project_idx ON opt_sources (tenant_id, project_id);
|
||||
CREATE INDEX IF NOT EXISTS opt_sources_type_idx ON opt_sources (source_type);
|
||||
CREATE INDEX IF NOT EXISTS opt_sources_status_idx ON opt_sources (status);
|
||||
|
||||
-- ── Rights / license / purpose metadata (src/types/source-registry.ts) ─
|
||||
-- One row per source (SourceRegistryService.setRights is an upsert-by-
|
||||
-- source_id, same as the Map it replaces). No rights row = fail-closed
|
||||
-- deny in assertPurposeAllowed — that logic lives in the service, not here.
|
||||
CREATE TABLE IF NOT EXISTS opt_source_rights (
|
||||
source_id TEXT PRIMARY KEY REFERENCES opt_sources (source_id),
|
||||
tenant_id TEXT NOT NULL,
|
||||
license_id TEXT NOT NULL,
|
||||
license_url TEXT,
|
||||
allowed_purposes TEXT[] NOT NULL DEFAULT '{}',
|
||||
pii_allowed BOOLEAN NOT NULL DEFAULT false,
|
||||
redistribution TEXT NOT NULL, -- internal | customer_deliverable | public | prohibited
|
||||
attribution TEXT,
|
||||
verified_at TEXT NOT NULL, -- ISO date the assessment was made (date-only in practice; kept as TEXT, not TIMESTAMPTZ, so it round-trips byte-for-byte)
|
||||
verified_by TEXT NOT NULL,
|
||||
notes TEXT,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS opt_source_rights_tenant_idx ON opt_source_rights (tenant_id);
|
||||
|
||||
-- ── Provenance / method metadata (per-SOURCE, not per-field — see
|
||||
-- BEFORE_STATE.md §3 for the distinction from bridge_provenance) ────────
|
||||
CREATE TABLE IF NOT EXISTS opt_source_provenance (
|
||||
source_id TEXT PRIMARY KEY REFERENCES opt_sources (source_id),
|
||||
tenant_id TEXT NOT NULL,
|
||||
method TEXT NOT NULL, -- official_api | bulk_download | authorized_web | licensed_feed | manual_upload
|
||||
authority TEXT,
|
||||
compliance_evidence_ref TEXT,
|
||||
cadence TEXT,
|
||||
freshness_sla_hours NUMERIC,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS opt_source_provenance_tenant_idx ON opt_source_provenance (tenant_id);
|
||||
|
||||
-- ── Health observations (source.health) — an operational signal, NOT an
|
||||
-- audit log (AUD0 owns history). Bounded to the latest 100 rows per
|
||||
-- source_id by the repository layer (src/repositories/postgres-source-
|
||||
-- registry-repository.ts), mirroring the in-memory implementation's
|
||||
-- `list.splice(0, list.length - 100)` bound.
|
||||
CREATE TABLE IF NOT EXISTS opt_source_health_checks (
|
||||
health_check_id BIGSERIAL PRIMARY KEY,
|
||||
source_id TEXT NOT NULL REFERENCES opt_sources (source_id),
|
||||
tenant_id TEXT NOT NULL,
|
||||
status TEXT NOT NULL, -- up | degraded | down | unknown
|
||||
checked_at TIMESTAMPTZ NOT NULL,
|
||||
latency_ms INT,
|
||||
detail TEXT -- short operator-facing text; never a raw upstream body
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS opt_source_health_checks_source_idx ON opt_source_health_checks (source_id, checked_at DESC, health_check_id DESC);
|
||||
|
||||
-- ── Freshness: last successful observation per source (source.health's
|
||||
-- "up" checks call recordSuccess; getFreshness reads this + provenance's
|
||||
-- freshness_sla_hours). One row per source — upserted, not appended.
|
||||
CREATE TABLE IF NOT EXISTS opt_source_freshness (
|
||||
source_id TEXT PRIMARY KEY REFERENCES opt_sources (source_id),
|
||||
tenant_id TEXT NOT NULL,
|
||||
last_success_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS opt_source_freshness_tenant_idx ON opt_source_freshness (tenant_id);
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user