71 lines
2.9 KiB
TypeScript
71 lines
2.9 KiB
TypeScript
/**
|
|
* 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";
|
|
import { assertTenantMatchesVerified } from "@/src/http/tenant-guard";
|
|
|
|
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;
|
|
// Tenant-scoped write (auto-provisions baseline rules + stores a run):
|
|
// the internal service principal must stay inside its server-configured
|
|
// tenant allowlist, never a tenant named by the caller.
|
|
const guard = assertTenantMatchesVerified(request, body.tenant_id);
|
|
if (guard) return guard;
|
|
|
|
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);
|
|
}
|
|
}
|