fix(er): require human review for score-only company matches

This commit is contained in:
2026-09-07 18:07:56 +02:00
parent 1c1d14a3eb
commit 04d861b920
+26 -1
View File
@@ -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);
}