feat(data-os): TS/Python quality bridge + provenance fields + fault-injection tests
CI (SIAX Cloud) / security (push) Successful in 23s
Security Scan (SIAX Cloud) / security (push) Successful in 22s
CI (SIAX Cloud) / integration (push) Successful in 29s
CI (SIAX Cloud) / quality (push) Successful in 45s
CI (SIAX Cloud) / security (pull_request) Successful in 27s
CI (SIAX Cloud) / integration (pull_request) Successful in 31s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 29s
CI (SIAX Cloud) / quality (pull_request) Successful in 48s
CI (SIAX Cloud) / security (push) Successful in 23s
Security Scan (SIAX Cloud) / security (push) Successful in 22s
CI (SIAX Cloud) / integration (push) Successful in 29s
CI (SIAX Cloud) / quality (push) Successful in 45s
CI (SIAX Cloud) / security (pull_request) Successful in 27s
CI (SIAX Cloud) / integration (pull_request) Successful in 31s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 29s
CI (SIAX Cloud) / quality (pull_request) Successful in 48s
Continues P010 (branch renamed from feat/p010-source-registry-persistence):
closes the three remaining BEFORE_STATE.md gaps (§2 TS/Python split-brain,
§3 missing provenance fields, §4 untested failure modes), all additive to
the production pipeline — the Python QA filter's delivery decision is
unchanged by any of this.
TS/Python bridge (§2):
- New route surface app/api/0pt/quality/* (rules/runs/issues CRUD +
POST /api/0pt/quality/bridge-run) over the shared QualityService
singleton (src/services/quality.ts), fixing the same "no shared
instance" defect the Source Registry had.
- src/quality/quality-bridge.ts auto-provisions a deterministic,
tenant-scoped baseline rule set on first use so the Python engine never
pre-registers rules through the CRUD surface.
- services/api/workers/quality_bridge.py: the Python-side HTTP client.
Feature-flagged off by default (OPT_QUALITY_BRIDGE_ENABLED), fails open
on any error (never raises into run_job, never changes `delivered`).
Advisory only — real per-row gating against the TS engine isn't
implementable yet (ExpectationEngine returns dataset-level pass/fail
counts + samples, not a per-row boolean array); documented, not faked.
- Live-verified end-to-end: `next dev` (AUTH_ENFORCE=false) + the real
Python pipeline hitting the real brreg.no API, round-tripping through
the real bridge route, receiving a real QualityService score.
Provenance (§3): sources/connectors.py's Provenance dataclass gains
retrieved_at, transform, connector_version, producer, quality,
entity_resolution, artifact_ref (all previously absent from the real
dataflow entirely) via a make_provenance() factory used by every connector.
resolve.py stamps entity_resolution on merged records' provenance (never on
unmerged ones). pipeline.py stamps artifact_ref at delivery and quality
once the bridge succeeds.
Failure tests (§4), real fault injection against real code (services/api,
new pytest + respx suite, not vitest mocks):
- dedup-determinism + duplicate-input + out-of-order: resolve.py under
shuffled/interleaved/exhaustive-permutation input, plus an explicit,
verified test of the one case that IS legitimately order-sensitive
(non-transitive similarity in a greedy clusterer) instead of hiding it.
- schema-evolution: respx-mocked upstream responses with added/removed/
alien-shaped fields through the real brreg connector + pipeline.
- restart: connector exceptions mid-job (including one from respx itself)
never crash run_job.
- backfill: the real property that holds given no persistence layer
exists (replay is compute-idempotent) — NOT a fabricated dedup-store
test.
- CDC-interruption and true backfill/delivery-store idempotency:
explicitly documented as not implementable against this codebase
(no CDC pipeline, no delivery store exist anywhere) via skip markers
with reasons, in tests/test_known_gaps.py, rather than silently
dropped or faked.
Testing: 256/256 vitest (249 pre-existing + 7 new bridge route/service
tests) and 28/28 pytest (2 documented skips) green locally. tsc -p
tsconfig.check.json has zero new errors (14 pre-existing errors, all in
the untracked/unaudited src/fleet, src/graph, src/metrics, src/projections
paths this pass does not touch, per BEFORE_STATE.md).
No PR opened, per instructions.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
07b13bd833
commit
9740e20818
@@ -39,6 +39,8 @@ next-env.d.ts
|
||||
__pycache__/
|
||||
*.pyc
|
||||
*.pyo
|
||||
.venv/
|
||||
.pytest_cache/
|
||||
|
||||
# analytics / database files
|
||||
*.duckdb
|
||||
|
||||
@@ -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,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; hej@siax.io)"
|
||||
_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,96 @@
|
||||
/**
|
||||
* src/quality/quality-bridge.ts
|
||||
* ================================
|
||||
* The TS side of the P010 TS/Python split-brain bridge (BEFORE_STATE.md §2):
|
||||
* the production order pipeline (services/api/workers/pipeline.py) has its
|
||||
* own one-line QA filter and never touches this repo's sophisticated,
|
||||
* versioned QualityService (rule registry, GE delegation, OpenLineage). The
|
||||
* two engines had "noll bryggor mellan sig" -- no HTTP, no shared DB, no N0D
|
||||
* job.
|
||||
*
|
||||
* This module is the ADDITIVE bridge, not a replacement: it lets the Python
|
||||
* engine ask the real QualityService to grade a batch of already-produced
|
||||
* rows and get back a real score/issues, WITHOUT the Python filter's
|
||||
* behavior changing. The Python side (services/api/workers/quality_bridge.py)
|
||||
* calls POST /api/0pt/quality/bridge-run purely for an advisory read today;
|
||||
* it is feature-flagged off by default (OPT_QUALITY_BRIDGE_ENABLED) and
|
||||
* fails open (a bridge error/timeout never blocks or alters delivery) --
|
||||
* see that file's docstring for the full contract and the honest limits on
|
||||
* what "gradual activation" can mean until per-row GE results exist.
|
||||
*
|
||||
* `ensureBaselineRules` auto-provisions one deterministic, tenant-scoped
|
||||
* rule set the first time a (tenant, dataset) pair is seen, so the Python
|
||||
* engine never has to pre-register rules through the CRUD surface just to
|
||||
* get a quality read on an order's delivered rows. The baseline checks are
|
||||
* generic company-record checks (name present, confidence in [0,1]) -- real
|
||||
* GE expectations executed by the local engine, not a stub.
|
||||
*/
|
||||
import { SiaxError } from "../platform/errors";
|
||||
import type { QualityService } from "./quality-service";
|
||||
|
||||
export interface BaselineRuleTarget {
|
||||
tenant_id: string;
|
||||
project_id: string;
|
||||
dataset_id: string;
|
||||
}
|
||||
|
||||
/** Deterministic, globally-unique (tenant + dataset scoped) baseline rule ids. */
|
||||
function baselineRuleIds(target: BaselineRuleTarget): { name: string; confidence: string } {
|
||||
const scope = `${target.tenant_id}__${target.dataset_id}`;
|
||||
return {
|
||||
name: `baseline_name_notnull__${scope}`,
|
||||
confidence: `baseline_confidence_range__${scope}`,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Idempotently ensures the baseline rule set exists for (tenant_id,
|
||||
* dataset_id). Safe to call on every bridge-run -- a NOT_FOUND on getRule
|
||||
* means "create it"; any other error propagates. Never overwrites a rule a
|
||||
* caller already customized (only creates when absent).
|
||||
*/
|
||||
export async function ensureBaselineRules(
|
||||
qualityService: QualityService,
|
||||
target: BaselineRuleTarget
|
||||
): Promise<void> {
|
||||
const ids = baselineRuleIds(target);
|
||||
|
||||
await ensureRule(qualityService, ids.name, target, {
|
||||
name: "baseline: name field present (0pt-quality-bridge)",
|
||||
expectation: { expectation_type: "expect_column_values_to_not_be_null", kwargs: { column: "name" } },
|
||||
severity: "error",
|
||||
});
|
||||
|
||||
await ensureRule(qualityService, ids.confidence, target, {
|
||||
name: "baseline: confidence in [0,1] (0pt-quality-bridge)",
|
||||
expectation: {
|
||||
expectation_type: "expect_column_values_to_be_between",
|
||||
kwargs: { column: "confidence", min_value: 0, max_value: 1 },
|
||||
},
|
||||
severity: "warning",
|
||||
});
|
||||
}
|
||||
|
||||
async function ensureRule(
|
||||
qualityService: QualityService,
|
||||
ruleId: string,
|
||||
target: BaselineRuleTarget,
|
||||
rest: { name: string; expectation: { expectation_type: string; kwargs: Record<string, unknown> }; severity: "info" | "warning" | "error" | "critical" }
|
||||
): Promise<void> {
|
||||
try {
|
||||
qualityService.getRule(ruleId, target.tenant_id);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof SiaxError && error.code === "NOT_FOUND") {
|
||||
await qualityService.createRule({
|
||||
rule_id: ruleId,
|
||||
tenant_id: target.tenant_id,
|
||||
project_id: target.project_id,
|
||||
dataset_id: target.dataset_id,
|
||||
supplied_by: "0pt-quality-bridge",
|
||||
...rest,
|
||||
});
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* src/services/quality.ts
|
||||
* =========================
|
||||
* Shared singleton for the quality route group (app/api/0pt/quality/*),
|
||||
* matching src/services/source-registry.ts's precedent: constructing the
|
||||
* service once here (instead of `new QualityService()` per route module)
|
||||
* keeps rules/runs/issues registered via one route visible to another
|
||||
* within the same running process — the same "not even a shared
|
||||
* process-wide singleton" defect BEFORE_STATE.md §1 found and fixed for the
|
||||
* Source Registry would otherwise recur here.
|
||||
*
|
||||
* This is also the seam the Python engine's quality bridge
|
||||
* (services/api/workers/quality_bridge.py) calls through over HTTP —
|
||||
* see app/api/0pt/quality/bridge-run/route.ts.
|
||||
*/
|
||||
import { QualityService } from "../quality/quality-service";
|
||||
|
||||
export const qualityService = new QualityService();
|
||||
@@ -179,3 +179,49 @@ export const requestIcebergWriteSchema = z.object({
|
||||
size_bytes: z.number().int().nonnegative().optional(),
|
||||
heavy: z.boolean().optional(),
|
||||
});
|
||||
|
||||
/** POST /api/0pt/quality/rules -- mirrors CreateQualityRuleInput (src/quality/quality-types.ts). */
|
||||
export const createQualityRuleSchema = z.object({
|
||||
rule_id: z.string().min(1),
|
||||
tenant_id: z.string().min(1),
|
||||
project_id: z.string().min(1),
|
||||
dataset_id: z.string().min(1),
|
||||
name: z.string().min(1),
|
||||
expectation: z.object({
|
||||
expectation_type: z.string().min(1),
|
||||
kwargs: jsonObject,
|
||||
}),
|
||||
severity: z.enum(['info', 'warning', 'error', 'critical']).optional(),
|
||||
supplied_by: z.string().min(1).optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/0pt/quality/runs -- runs the currently-registered active rules
|
||||
* for a dataset against caller-supplied rows (QualityService.runQuality).
|
||||
*/
|
||||
export const createQualityRunSchema = z.object({
|
||||
tenant_id: z.string().min(1),
|
||||
project_id: z.string().min(1),
|
||||
dataset_id: z.string().min(1),
|
||||
dataset_version: z.number().int().optional(),
|
||||
rows: z.array(jsonObject).optional(),
|
||||
correlation_id: z.string().min(1).optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/0pt/quality/bridge-run -- the Python engine's (services/api/workers/pipeline.py)
|
||||
* bridge into the TS QualityService. Auto-provisions a baseline rule set for
|
||||
* the (tenant, dataset) pair the first time it is called, so the engine never
|
||||
* has to pre-register rules through the CRUD surface just to get an advisory
|
||||
* quality read. `rows` is required here (unlike createQualityRunSchema) --
|
||||
* the bridge always runs synchronously against real, already-produced rows;
|
||||
* it never triggers a delegated/no-rows run.
|
||||
*/
|
||||
export const qualityBridgeRunSchema = z.object({
|
||||
tenant_id: z.string().min(1),
|
||||
project_id: z.string().min(1),
|
||||
dataset_id: z.string().min(1),
|
||||
dataset_version: z.number().int().optional(),
|
||||
rows: z.array(jsonObject),
|
||||
correlation_id: z.string().min(1).optional(),
|
||||
});
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* P010: the TS/Python split-brain bridge (BEFORE_STATE.md §2 — the
|
||||
* production Python pipeline and the sophisticated TS QualityService had
|
||||
* "noll bryggor mellan sig"). Covers both the pure bridge helper
|
||||
* (src/quality/quality-bridge.ts) and the actual HTTP route
|
||||
* (app/api/0pt/quality/bridge-run/route.ts) the Python engine calls, so a
|
||||
* regression here is caught even if only the route wiring breaks.
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { NextRequest } from "next/server";
|
||||
import { QualityService } from "@/src/quality/quality-service";
|
||||
import { ensureBaselineRules } from "@/src/quality/quality-bridge";
|
||||
import { POST as bridgeRunPOST } from "@/app/api/0pt/quality/bridge-run/route";
|
||||
|
||||
function bridgeRequest(body: unknown): NextRequest {
|
||||
return new NextRequest("http://localhost/api/0pt/quality/bridge-run", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
describe("ensureBaselineRules — the auto-provisioning half of the bridge", () => {
|
||||
it("creates the baseline rule set once, and is idempotent on repeat calls", async () => {
|
||||
const q = new QualityService();
|
||||
const target = { tenant_id: "t_a", project_id: "p_1", dataset_id: "ds_order_42" };
|
||||
await ensureBaselineRules(q, target);
|
||||
const firstPass = q.listRules({ tenant_id: "t_a", dataset_id: "ds_order_42" });
|
||||
expect(firstPass).toHaveLength(2);
|
||||
expect(firstPass.every((r) => r.supplied_by === "0pt-quality-bridge")).toBe(true);
|
||||
|
||||
// Calling again must NOT throw CONFLICT or create duplicates/new versions.
|
||||
await ensureBaselineRules(q, target);
|
||||
const secondPass = q.listRules({ tenant_id: "t_a", dataset_id: "ds_order_42" });
|
||||
expect(secondPass).toHaveLength(2);
|
||||
expect(secondPass.map((r) => r.version)).toEqual([1, 1]);
|
||||
});
|
||||
|
||||
it("scopes baseline rule ids per tenant so two tenants never collide (CONFLICT)", async () => {
|
||||
const q = new QualityService();
|
||||
await ensureBaselineRules(q, { tenant_id: "t_a", project_id: "p_1", dataset_id: "ds_shared_name" });
|
||||
// Same dataset_id, different tenant — must not throw CONFLICT on the global rule_id map.
|
||||
await expect(
|
||||
ensureBaselineRules(q, { tenant_id: "t_b", project_id: "p_1", dataset_id: "ds_shared_name" })
|
||||
).resolves.toBeUndefined();
|
||||
expect(q.listRules({ tenant_id: "t_a", dataset_id: "ds_shared_name" })).toHaveLength(2);
|
||||
expect(q.listRules({ tenant_id: "t_b", dataset_id: "ds_shared_name" })).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("never overwrites a rule a caller already customized under the same id pattern", async () => {
|
||||
const q = new QualityService();
|
||||
const target = { tenant_id: "t_a", project_id: "p_1", dataset_id: "ds_x" };
|
||||
await ensureBaselineRules(q, target);
|
||||
const before = q.getRule("baseline_name_notnull__t_a__ds_x", "t_a");
|
||||
await q.updateRule("baseline_name_notnull__t_a__ds_x", "t_a", { severity: "critical" });
|
||||
await ensureBaselineRules(q, target); // must not clobber the customization
|
||||
const after = q.getRule("baseline_name_notnull__t_a__ds_x", "t_a");
|
||||
expect(after.severity).toBe("critical");
|
||||
expect(after.version).toBe(before.version + 1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/0pt/quality/bridge-run — the actual contract the Python engine calls", () => {
|
||||
it("grades delivered rows for real: a row missing `name` fails the baseline and opens an issue", async () => {
|
||||
const res = await bridgeRunPOST(
|
||||
bridgeRequest({
|
||||
tenant_id: "t_bridge",
|
||||
project_id: "p_1",
|
||||
dataset_id: "ds_order_1",
|
||||
rows: [
|
||||
{ name: "Volvo Personvagnar AB", confidence: 0.97 },
|
||||
{ name: null, confidence: 0.4 },
|
||||
],
|
||||
})
|
||||
);
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.run.status).toBe("failed");
|
||||
expect(body.run.score).toBeGreaterThan(0);
|
||||
expect(body.run.score).toBeLessThan(100);
|
||||
expect(body.issues.length).toBeGreaterThan(0);
|
||||
expect(body.issues.some((i: { rule_id: string }) => i.rule_id.startsWith("baseline_name_notnull__"))).toBe(true);
|
||||
});
|
||||
|
||||
it("a clean batch of rows succeeds with score 100 and no open issues", async () => {
|
||||
const res = await bridgeRunPOST(
|
||||
bridgeRequest({
|
||||
tenant_id: "t_bridge_clean",
|
||||
project_id: "p_1",
|
||||
dataset_id: "ds_order_2",
|
||||
rows: [
|
||||
{ name: "Ericsson AB", confidence: 0.95 },
|
||||
{ name: "Atlas Copco AB", confidence: 0.88 },
|
||||
],
|
||||
})
|
||||
);
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.run.status).toBe("succeeded");
|
||||
expect(body.run.score).toBe(100);
|
||||
expect(body.issues).toEqual([]);
|
||||
});
|
||||
|
||||
it("rejects a malformed body with a structured 400, never a 500 (Python must be able to tell apart its own bug from a network fault)", async () => {
|
||||
const res = await bridgeRunPOST(bridgeRequest({ tenant_id: "t_x" })); // missing project_id/dataset_id/rows
|
||||
expect(res.status).toBe(400);
|
||||
const body = await res.json();
|
||||
expect(body.error).toBe("validation_error");
|
||||
});
|
||||
|
||||
it("out-of-range confidence values (data drift from upstream) are graded, not crashed on", async () => {
|
||||
const res = await bridgeRunPOST(
|
||||
bridgeRequest({
|
||||
tenant_id: "t_bridge_drift",
|
||||
project_id: "p_1",
|
||||
dataset_id: "ds_order_3",
|
||||
rows: [{ name: "Weird AB", confidence: 42 }], // confidence way outside [0,1] — upstream bug simulation
|
||||
})
|
||||
);
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.run.status).toBe("failed");
|
||||
expect(body.issues.some((i: { rule_id: string }) => i.rule_id.startsWith("baseline_confidence_range__"))).toBe(
|
||||
true
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user