CI (SIAX Cloud) / quality (push) Successful in 51s
Security Scan (SIAX Cloud) / security (push) Successful in 26s
CI (SIAX Cloud) / security (push) Successful in 28s
CI (SIAX Cloud) / integration (push) Successful in 1m1s
CI (SIAX Cloud) / integration (pull_request) Successful in 30s
CI (SIAX Cloud) / security (pull_request) Successful in 24s
CI (SIAX Cloud) / quality (pull_request) Successful in 48s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 22s
Tenant isolation (security-critical): every app/api/0pt/** route reading
tenant_id from client-controlled query/body never cross-checked it against
the token middleware.ts already verified (x-0pt-verified-tenant). Adds a
shared assertTenantMatchesVerified guard and wires it into all 20+ affected
routes. Also closes two full-leak-by-omission bugs found in this pass
(documents/capture and lakehouse/mappings GETs returned every tenant's rows
when tenant_id was simply omitted) and two "no tenant check at all" reads
(rate-policies/[id], lakehouse/mappings/[id]) reachable by ID alone.
Duplicate-delivery idempotency (correctness-critical): POST /api/orders now
honors Idempotency-Key (sql/006, replay-or-409-on-conflict, Postgres +
in-memory); the Python orchestrator's POST /orders/{id}/run — the actual
delivery trigger — is now idempotent under an asyncio lock, closing a real
concurrent-request race, not just the sequential-retry case.
Engine-duplication resolution: adaptive_router.py's docstring wrongly
invited a bypass ("see b0ts for scraping") contradicting the product's own
compliance stance; corrected, and given real (additive, not escalating)
block-signal tracking so a rate-limited/blocked source is excluded from the
plan instead of hammered. Also fixes a previously-undiscovered production
bug: the Python engine's only call into the TS quality bridge
(X-Internal-Token) was unconditionally 401'd by middleware.ts, which never
checked that credential — the advisory quality bridge was unreachable in any
environment enforcing auth.
Restore drill: executed for real against throwaway Docker Postgres
containers (not simulated). Found and fixed two real blockers along the way:
sql/004 referenced opt_datasets, which no migration ever created (chain was
never actually runnable end to end); lib/db.ts hardcoded ssl:"require",
making any non-TLS Postgres (incl. compose.dev.yaml's own local-dev
container) unreachable. After both fixes: full migration chain applies
cleanly, pg_dump/pg_restore round-trip is byte-identical (row counts +
content checksums), and real application code (lib/orders-store.ts,
SourceRegistryService's Postgres repository) reads the restored data back
correctly, including cross-tenant isolation still holding.
82 new tests (55 TS + 27 Python), all passing; full existing suite (291 TS +
55 Python) still green; typecheck clean on every changed/added file.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
179 lines
7.6 KiB
Python
179 lines
7.6 KiB
Python
"""
|
|
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,
|
|
SourceBlockedError,
|
|
brreg_by_orgnr,
|
|
companies_house_by_number,
|
|
cvr_by_number,
|
|
gleif_by_lei,
|
|
prh_by_businessid,
|
|
)
|
|
from workers.adaptive_router import default_router, 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)
|
|
"GB": ["00000006"], # companies house number (example)
|
|
}
|
|
|
|
|
|
async def _fetch(source_id: str, key: str) -> CompanyResult | None:
|
|
if source_id == "gleif":
|
|
return await gleif_by_lei(key)
|
|
if source_id == "brreg":
|
|
return await brreg_by_orgnr(key)
|
|
if source_id == "companies_house":
|
|
return await companies_house_by_number(key)
|
|
if source_id == "prh":
|
|
return await prh_by_businessid(key)
|
|
if source_id == "cvr":
|
|
return await cvr_by_number(key)
|
|
return None
|
|
|
|
|
|
async def run_job(order_id: str, config: dict[str, Any]) -> dict[str, Any]:
|
|
markets: list[str] = config.get("markets", []) or ["NO"]
|
|
threshold = float(config.get("confidence", 92)) / 100.0
|
|
# Use caller-supplied seeds when present (an explicit empty {} is honoured so a
|
|
# real job is never silently backfilled with DEMO_SEEDS), but treat an explicit
|
|
# null the same as "missing" -- `config.get("seeds") is not None` avoids a 500
|
|
# (None.get) on client-controlled `{"seeds": null}`, which the old `... or ...`
|
|
# idiom tolerated and the `"seeds" in config` form regressed.
|
|
seeds: dict[str, list[str]] = config["seeds"] if config.get("seeds") is not None else DEMO_SEEDS
|
|
|
|
records: list[dict[str, Any]] = []
|
|
per_market: dict[str, int] = {}
|
|
|
|
# 1) sourcing + 2) extraction
|
|
for market in markets:
|
|
keys = seeds.get(market, [])
|
|
got = 0
|
|
for key in keys:
|
|
# Recomputed per key (not hoisted above this loop): a source
|
|
# blocked while fetching an earlier key in this same market must
|
|
# be excluded from the plan for every key still to come in this
|
|
# run, not just retried forever with no memory (P014 -- see
|
|
# adaptive_router.py's module docstring).
|
|
plan = plan_for_market(market)
|
|
for source_id in plan:
|
|
try:
|
|
res = await _fetch(source_id, key)
|
|
except SourceBlockedError:
|
|
default_router.record_blocked(market, source_id)
|
|
res = None
|
|
except Exception:
|
|
res = None
|
|
if res and res.name:
|
|
records.append({
|
|
"market": market, "name": res.name, "status": res.status,
|
|
"lei": res.lei, "org_nr": res.org_nr, "source": res.source,
|
|
"provenance": [p.__dict__ for p in res.provenance],
|
|
"confidence": max((p.confidence for p in res.provenance), default=0.0),
|
|
})
|
|
got += 1
|
|
break # first compliant source that answers wins
|
|
per_market[market] = got
|
|
|
|
# 3) structuring — entity resolution (blocking + fuzzy match + corroboration)
|
|
deduped = resolve(records)
|
|
|
|
# 4) enrichment/fusion already folded into confidence above.
|
|
# 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,
|
|
}
|