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): review follow-ups on helpers PR

nnlgsakibba4904a5ea83
diff --git a/laya-ts/src/common.ts b/laya-ts/src/common.ts
index 7a3df97b..f1e716e4 100644
--- a/laya-ts/src/common.ts
+++ b/laya-ts/src/common.ts
@@ -76,7 +76,9 @@ export function buildQuestionPrefix(tok: TokenizerLike, q: InternalQ,
per = new Map();
prefixCache.set(tok as object, per);
}
- const key = JSON.stringify([q.t, q.ins, q.crit, q.labels ?? null, maxLen, headMaxLen, optionOrder ?? null]);
+ // Key on what actually builds the prefix: raw criteria lie (JSON.stringify
+ // drops undefined values and throws on BigInt), rendered options don't.
+ const key = JSON.stringify([q.t, q.ins, renderOptions(q), maxLen, headMaxLen, optionOrder ?? null]);
const hit = per.get(key);
if (hit) return hit;
const built = buildQuestionPrefixUncached(tok, q, maxLen, headMaxLen, optionOrder);
diff --git a/laya-ts/src/dx.ts b/laya-ts/src/dx.ts
index 6508f47d..c82cac8f 100644
--- a/laya-ts/src/dx.ts
+++ b/laya-ts/src/dx.ts
@@ -41,13 +41,11 @@ export function topK(probs: Record<string, number>, k: number): string[] {
.map(([key]) => key);
}
-/** Confidence gate (default 0.8). Reads answer_confidence (max(p), comparable
- * across question types) when present, falling back to entropy confidence. */
+/** Confidence gate (default 0.8) on answer_confidence (max(p)) alone, so the
+ * gate uses one scale across question types instead of mixing in entropy. */
export function isConfident(
- answer: Pick<SystemAnswer, "confidence"> & { answer_confidence?: unknown },
+ answer: Pick<SystemAnswer, "answer_confidence">,
thresh = 0.8,
): boolean {
- const ac = (answer as { answer_confidence?: unknown }).answer_confidence;
- const v = typeof ac === "number" && Number.isFinite(ac) ? ac : answer.confidence;
- return (v ?? 0) >= thresh;
+ return (answer.answer_confidence ?? 0) >= thresh;
}
diff --git a/laya-ts/src/hooks.ts b/laya-ts/src/hooks.ts
index cbd198a2..47cdc45f 100644
--- a/laya-ts/src/hooks.ts
+++ b/laya-ts/src/hooks.ts
@@ -242,6 +242,7 @@ export function composeHooks(
}
const EMPTY_HOOKS: Hook[] = [];
+Object.freeze(EMPTY_HOOKS);
/**
* Base class giving a runtime-mutable hook list.
diff --git a/laya-ts/tests/dx.test.ts b/laya-ts/tests/dx.test.ts
index e9550216..76455098 100644
--- a/laya-ts/tests/dx.test.ts
+++ b/laya-ts/tests/dx.test.ts
@@ -24,12 +24,13 @@ describe("result helpers", () => {
expect(best({ type: "choice", choice: "b", probabilities: { a: 0.2, b: 0.8 } } as any)).toBe("b");
expect(best({ type: "score", probabilities: { "0": 0.1, "1": 0.7, "2": 0.2 } } as any)).toBe("1");
expect(topK({ a: 0.2, b: 0.8, c: 0.5 }, 2)).toEqual(["b", "c"]);
- expect(isConfident({ confidence: 0.9 } as any, 0.8)).toBe(true);
- expect(isConfident({ confidence: 0.5 } as any, 0.8)).toBe(false);
+ expect(isConfident({ answer_confidence: 0.9 } as any, 0.8)).toBe(true);
+ expect(isConfident({ answer_confidence: 0.5 } as any, 0.8)).toBe(false);
});
- it("prefers answer_confidence over entropy confidence", () => {
+ it("gates on answer_confidence alone, ignoring entropy confidence", () => {
expect(isConfident({ confidence: 0.5, answer_confidence: 0.9 } as any, 0.8)).toBe(true);
expect(isConfident({ confidence: 0.9, answer_confidence: 0.5 } as any, 0.8)).toBe(false);
+ expect(isConfident({ confidence: 0.9 } as any, 0.8)).toBe(false);
});
});
diff --git a/laya-ts/tests/prefix-cache.test.ts b/laya-ts/tests/prefix-cache.test.ts
index 503fa475..b0980123 100644
--- a/laya-ts/tests/prefix-cache.test.ts
+++ b/laya-ts/tests/prefix-cache.test.ts
@@ -11,4 +11,19 @@ describe("prefix cache", () => {
expect(p2).toBe(p1);
expect(p2.ids).toEqual(p1.ids);
});
+ it("does not merge undefined descriptions into missing keys", () => {
+ const tok = defaultTokenizer();
+ const withUndef: any = { t: "choice", ins: "What?", crit: { a: undefined, b: "x" } };
+ const without: any = { t: "choice", ins: "What?", crit: { b: "x" } };
+ const p1 = buildQuestionPrefix(tok, withUndef, 512, 192);
+ const p2 = buildQuestionPrefix(tok, without, 512, 192);
+ expect(p2).not.toBe(p1);
+ expect(p1.markers.length).toBe(2);
+ expect(p2.markers.length).toBe(1);
+ });
+ it("tolerates BigInt descriptions", () => {
+ const tok = defaultTokenizer();
+ const q: any = { t: "choice", ins: "What?", crit: { a: 1n } };
+ expect(() => buildQuestionPrefix(tok, q, 512, 192)).not.toThrow();
+ });
});