From 04d861b92057b9f2717d6b753012708eda1b878a Mon Sep 17 00:00:00 2001 From: Simon Axelsson Date: Mon, 7 Sep 2026 18:07:56 +0200 Subject: [PATCH] fix(er): require human review for score-only company matches --- app/api/0pt/internal/er/candidates/route.ts | 27 ++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/app/api/0pt/internal/er/candidates/route.ts b/app/api/0pt/internal/er/candidates/route.ts index 8152aba..f6a55ea 100644 --- a/app/api/0pt/internal/er/candidates/route.ts +++ b/app/api/0pt/internal/er/candidates/route.ts @@ -4,6 +4,7 @@ import { verifyInternalServiceToken } from "@/src/auth/internal-token"; import { EntityResolutionService, type ErCandidateRequest, + type ErCandidateResult, type ErRecord, } from "@/src/er/entity-resolution-service"; import { siaxErrorResponse } from "@/src/http/siax-error-response"; @@ -136,12 +137,36 @@ function parseRequest(raw: unknown): ErCandidateRequest { }; } +/** + * Company/Work boundary: fuzzy similarity is candidate evidence, never identity truth. + * + * The generic ER engine supports score-based `match` for domains that explicitly + * choose that policy. This internal company endpoint is stricter: only a product + * hard rule backed by stable identifiers may emit `match`. Any score-only match + * is downgraded to `pending_review`, matching the review-gated Python candidate + * layer used by Work/CRM/FIT0 and preventing two ER paths from disagreeing about + * whether fuzzy similarity is sufficient to link an organization. + */ +export function enforceCompanyReviewBoundary(result: ErCandidateResult): ErCandidateResult { + const pairs = result.pairs.map((pair) => + pair.decided_by === "score" && pair.status === "match" + ? { ...pair, status: "pending_review" as const } + : pair, + ); + return { + ...result, + pairs, + review_queue: pairs.filter((pair) => pair.status === "pending_review"), + }; +} + export async function POST(request: NextRequest) { const denied = assertInternal(request); if (denied) return denied; try { - return NextResponse.json(er.generateCandidates(parseRequest(await request.json()))); + const result = er.generateCandidates(parseRequest(await request.json())); + return NextResponse.json(enforceCompanyReviewBoundary(result)); } catch (error: unknown) { return siaxErrorResponse(error); }