CI (SIAX Cloud) / integration (push) Has been skipped
masterplan-lock / check (pull_request) Successful in 35s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 55s
CI (SIAX Cloud) / security (pull_request) Successful in 1m10s
CI (SIAX Cloud) / integration (pull_request) Successful in 1m46s
CI (SIAX Cloud) / quality (pull_request) Successful in 1m50s
masterplan-lock / check (push) Successful in 2m27s
CI (SIAX Cloud) / security (push) Successful in 3m26s
Security Scan (SIAX Cloud) / security (push) Successful in 3m26s
CI (SIAX Cloud) / quality (push) Successful in 4m50s
0PT_DECISION_ER_SHADOW borjar med siffra och avvisas av Coolifys env-validering (422, bevisat 2026-09-23) — gaten nadde aldrig prod-containern. Nytt namn: OPT_DECISION_ER_SHADOW (accepterad, satt i Coolify prod). Det gamla 0PT_- namnet accepteras fortfarande for backcompat.
133 lines
5.1 KiB
TypeScript
133 lines
5.1 KiB
TypeScript
/**
|
|
* `@siax/0pt-decision` — 0pt ER via siax-decision (:4600).
|
|
*
|
|
* Kaskad: hard rules + weighted-scoring (D0) → decision.match (D1-advisory)
|
|
* för pending_review-ambiguitet → ABSTAINED/decision-nere → pending_review
|
|
* gäller (fail-closed, aldrig fabricerat). 0pt behåller provenance-semantik.
|
|
*/
|
|
|
|
const DECISION_URL = process.env.DECISION_API_URL ?? "http://127.0.0.1:4600";
|
|
const DECISION_TOKEN = process.env.DECISION_TOKEN ?? "";
|
|
const DECISION_TIMEOUT_MS = Number(process.env.DECISION_TIMEOUT_MS ?? 2000);
|
|
|
|
export interface DecisionMatch {
|
|
decided: boolean;
|
|
label?: "match" | "pending_review" | "non_match";
|
|
confidence?: number;
|
|
model?: string;
|
|
latencyMs?: number;
|
|
}
|
|
|
|
/** decision.match för ER-pair (semantic tie-break vid gränsfall). */
|
|
export async function erMatchDecision(pair: {
|
|
record_a: string;
|
|
record_b: string;
|
|
similarities: Array<{ field: string; similarity: number; weight: number }>;
|
|
}): Promise<{ decided: boolean; label?: string; confidence?: number; model?: string; latencyMs?: number }> {
|
|
try {
|
|
const res = await fetch(`${DECISION_URL}/decision/match`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json", Authorization: `Bearer ${DECISION_TOKEN}` },
|
|
body: JSON.stringify({
|
|
decision: "0pt.er.pair_match",
|
|
left: { record_id: pair.record_a },
|
|
right: [{ record_id: pair.record_b }],
|
|
instructions: "Do these two entity records refer to the same real-world entity?",
|
|
minConfidence: 0.7,
|
|
context: { field_similarities: pair.similarities },
|
|
}),
|
|
signal: AbortSignal.timeout(DECISION_TIMEOUT_MS),
|
|
});
|
|
if (!res.ok) return { decided: false };
|
|
const out = (await res.json()) as { data?: { status?: string; choice?: string; confidence?: number; model?: string; latencyMs?: number } };
|
|
const d = out.data;
|
|
if (!d || d.status !== "DECIDED") return { decided: false, confidence: d?.confidence };
|
|
const label = String(d.choice ?? "pending_review");
|
|
return {
|
|
decided: true,
|
|
label: label === "0" ? "non_match" : label === "1" ? "match" : (label as "match" | "pending_review" | "non_match"),
|
|
confidence: d.confidence,
|
|
model: d.model,
|
|
latencyMs: d.latencyMs,
|
|
};
|
|
} catch {
|
|
return { decided: false };
|
|
}
|
|
}
|
|
|
|
/** Optional gate: enabled via OPT_DECISION_ER_ENABLED=1 (off tills kalibrering). */
|
|
export function erDecisionEnabled(): boolean {
|
|
const v = process.env.OPT_DECISION_ER_ENABLED;
|
|
return v === "1" || v === "true";
|
|
}
|
|
|
|
// --- Shadow-eval (Jev-everywhere): auto-match-kvalitet mäts, påverkar ALDRIG ---
|
|
export interface ErAutoMatchShadowInput {
|
|
tenant_id: string;
|
|
/** Målentiteten som auto-matchats av D0-tröskeln. */
|
|
target: { record_id: string; fields?: Record<string, unknown> };
|
|
/** Kandidaterna (tie-set) som D0 matchade mot målet. */
|
|
candidates: Array<{ record_id: string; fields?: Record<string, unknown> }>;
|
|
/** D0-score som triggade auto-matchen (skickas som kontext till beslutsplanet). */
|
|
score?: number;
|
|
}
|
|
|
|
export interface ErShadowResult {
|
|
decided: boolean;
|
|
choice?: string;
|
|
confidence?: number;
|
|
model?: string;
|
|
latencyMs?: number;
|
|
}
|
|
|
|
/** Shadow-gate: OPT_DECISION_ER_SHADOW=1 (OFF by default — fire-and-forget).
|
|
* Coolify avvisar env-nycklar som borjar med siffra ("0PT_..."), sa gaten
|
|
* heter OPT_DECISION_ER_SHADOW; det gamla 0PT_-namnet accepteras for
|
|
* bakatkompatibilitet (t.ex. systemd/docker-miljor utan Coolify). */
|
|
export function erAutoMatchShadowEnabled(): boolean {
|
|
return process.env.OPT_DECISION_ER_SHADOW === "1" || process.env["0PT_DECISION_ER_SHADOW"] === "1";
|
|
}
|
|
|
|
/**
|
|
* Shadow-eval av D0 auto-match: POST /decision/match (decisionId
|
|
* `0pt.er.auto_match`). D0-tröskeln förblir auktoritet — svaret används endast
|
|
* för kalibrering (decision-planen loggar inbyggt → AUD0). Fail-closed:
|
|
* gate-off, nätverksfel, timeout eller ABSTAINED → null / decided:false,
|
|
* kastar ALDRIG.
|
|
*/
|
|
export async function erAutoMatchShadow(
|
|
input: ErAutoMatchShadowInput,
|
|
): Promise<ErShadowResult | null> {
|
|
if (!erAutoMatchShadowEnabled()) return null;
|
|
try {
|
|
const res = await fetch(`${DECISION_URL}/decision/match`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json", Authorization: `Bearer ${DECISION_TOKEN}` },
|
|
body: JSON.stringify({
|
|
decision: "0pt.er.auto_match",
|
|
left: input.target,
|
|
right: input.candidates,
|
|
instructions: "Which candidate is the same entity as the target?",
|
|
minConfidence: 0.7,
|
|
context: { tenant_id: input.tenant_id, d0_score: input.score },
|
|
}),
|
|
signal: AbortSignal.timeout(DECISION_TIMEOUT_MS),
|
|
});
|
|
if (!res.ok) return null;
|
|
const out = (await res.json()) as {
|
|
data?: { status?: string; choice?: string; confidence?: number; model?: string; latencyMs?: number };
|
|
};
|
|
const d = out.data;
|
|
if (!d || d.status !== "DECIDED") return { decided: false, confidence: d?.confidence };
|
|
return {
|
|
decided: true,
|
|
choice: String(d.choice ?? ""),
|
|
confidence: d.confidence,
|
|
model: d.model,
|
|
latencyMs: d.latencyMs,
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|