Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(ts): clear stale low_confidence marker at min_confidence 0.0 (#910)

Swatantra Yadav5804f83cf2ab
diff --git a/laya-ts/src/common.ts b/laya-ts/src/common.ts
index 6a099b76..8ab30cbb 100644
--- a/laya-ts/src/common.ts
+++ b/laya-ts/src/common.ts
@@ -333,16 +333,21 @@ export function resolveMinConfidence(
*
* Reads `answer_confidence` (the calibrated max(p) confidence, invariant to label count k),
* falling back to `confidence` if `answer_confidence` is absent.
- * The raw answer and confidence stay intact; `low_confidence: true` is added.
+ * The raw answer and confidence stay intact; `low_confidence: true` is added when the answer
+ * falls below the threshold, and removed if a previously-flagged answer now clears it (e.g.
+ * when a result dict is reused or re-evaluated with a different threshold).
*
* Supports both a scalar number in [0.0, 1.0] and a per-bucket mapping of thresholds.
+ *
+ * Exactly 0.0 runs this same loop rather than short-circuiting out of it. Nothing falls below
+ * it, so it marks no answer -- and the pass is what clears a marker an earlier, stricter threshold
+ * left on a reused payload (#910).
*/
export function flagLowConfidence(
results: Array<Record<string, unknown>> | Record<string, unknown>,
minConfidence: MinConfidence,
): void {
const isMap = typeof minConfidence === "object" && minConfidence !== null;
- if (!isMap && minConfidence === 0.0) return;
const list = Array.isArray(results) ? results : [results];
for (const res of list) {
const answers = res && typeof res === "object" ? (res as Record<string, unknown>).answers : null;
@@ -354,11 +359,15 @@ export function flagLowConfidence(
if (conf === undefined || conf === null) {
conf = ansObj.confidence;
}
- if (typeof conf === "number" && !Number.isNaN(conf)) {
- const thr = isMap ? resolveMinConfidence(ansObj, minConfidence) : minConfidence;
- if (conf < thr) {
- ansObj.low_confidence = true;
- }
+ if (typeof conf !== "number" || !Number.isFinite(conf) || conf < 0 || conf > 1) {
+ delete ansObj.low_confidence;
+ continue;
+ }
+ const thr = isMap ? resolveMinConfidence(ansObj, minConfidence) : minConfidence;
+ if (conf < thr) {
+ ansObj.low_confidence = true;
+ } else {
+ delete ansObj.low_confidence;
}
}
}
diff --git a/laya-ts/tests/confidence-gating.test.ts b/laya-ts/tests/confidence-gating.test.ts
index 52161f64..a56d7426 100644
--- a/laya-ts/tests/confidence-gating.test.ts
+++ b/laya-ts/tests/confidence-gating.test.ts
@@ -132,7 +132,7 @@ describe("optionBucket & resolveMinConfidence", () => {
});
describe("flagLowConfidence", () => {
- it("is a no-op when minConfidence is 0.0", () => {
+ it("does not flag clean answers when minConfidence is 0.0", () => {
const sample = [
{
answers: {
@@ -144,6 +144,70 @@ describe("flagLowConfidence", () => {
expect(sample[0].answers.q1).not.toHaveProperty("low_confidence");
});
+ it("clears stale low_confidence flag when re-evaluated with a more permissive threshold (#910)", () => {
+ const sample = [
+ {
+ answers: {
+ intent: { choice: "refund", answer_confidence: 0.8 },
+ },
+ },
+ ];
+ const ans = sample[0].answers.intent as any;
+
+ flagLowConfidence(sample as any, 0.9);
+ expect(ans.low_confidence).toBe(true);
+
+ flagLowConfidence(sample as any, 0.5);
+ expect(ans).not.toHaveProperty("low_confidence");
+ });
+
+ it("clears stale low_confidence flag when re-evaluated at minConfidence 0.0 (#910)", () => {
+ const sample = [
+ {
+ answers: {
+ intent: { choice: "refund", answer_confidence: 0.8 },
+ },
+ },
+ ];
+ const ans = sample[0].answers.intent as any;
+
+ flagLowConfidence(sample as any, 0.9);
+ expect(ans.low_confidence).toBe(true);
+
+ // 0.0 is the most permissive threshold: it marks no answer and clears any earlier marker
+ flagLowConfidence(sample as any, 0.0);
+ expect(ans).not.toHaveProperty("low_confidence");
+ });
+
+ it("clears stale low_confidence flag when confidence is missing, non-finite, or out of [0, 1] range", () => {
+ const sample = [
+ {
+ answers: {
+ q_missing: { choice: "a", low_confidence: true },
+ q_nan: { choice: "b", answer_confidence: NaN, low_confidence: true },
+ q_inf: { choice: "c", answer_confidence: Infinity, low_confidence: true },
+ q_neg: { choice: "d", answer_confidence: -0.1, low_confidence: true },
+ q_over: { choice: "e", answer_confidence: 1.5, low_confidence: true },
+ },
+ },
+ ];
+ flagLowConfidence(sample as any, 0.5);
+ for (const key of ["q_missing", "q_nan", "q_inf", "q_neg", "q_over"]) {
+ expect((sample[0].answers as any)[key]).not.toHaveProperty("low_confidence");
+ }
+
+ // Out-of-range confidence is also not flagged when minConfidence is 0.0
+ const sampleNeg = [
+ {
+ answers: {
+ q_neg: { choice: "d", answer_confidence: -0.1 },
+ },
+ },
+ ];
+ flagLowConfidence(sampleNeg as any, 0.0);
+ expect((sampleNeg[0].answers as any).q_neg).not.toHaveProperty("low_confidence");
+ });
+
it("flags answers where answer_confidence falls below threshold", () => {
const sample = [
{