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): reject a null choice label, as Python does (#508 parity)

divit aggarwal738c04aa335e
diff --git a/laya-ts/src/agent.ts b/laya-ts/src/agent.ts
index 6a8c8566..9d44168b 100644
--- a/laya-ts/src/agent.ts
+++ b/laya-ts/src/agent.ts
@@ -160,14 +160,24 @@ export function checkQuestion(qid: string, qdef: unknown): void {
throw new Error(`question ${qidStr(qid)}: a choice question needs at least one criterion`);
}
if (Array.isArray(crit)) {
- crit.forEach((label: unknown, i) => {
- if (typeof label !== "object" || label === null) return;
+ // An indexed loop, not forEach: a hole in a sparse list is an undefined label too.
+ for (let i = 0; i < crit.length; i++) {
+ const label: unknown = crit[i];
+ if (label === null || label === undefined) {
+ // A null label becomes the answer key "null", which a client cannot tell apart from
+ // the string "null", and `criteria[choice]` never finds it. Python rejects it (#508).
+ throw new Error(
+ `question ${qidStr(qid)}: choice label ${i} is null; a label is rendered as option text and used ` +
+ `as the answer key, so it must be a string, number or bool`,
+ );
+ }
+ if (typeof label !== "object") continue;
throw new Error(
`question ${qidStr(qid)}: choice label ${i} is a ${Array.isArray(label) ? "list" : "dict"}; a label is ` +
`rendered as option text and used as the answer key, so it must be a scalar (a string, number or ` +
- `null), got ${JSON.stringify(label)}`,
+ `bool), got ${JSON.stringify(label)}`,
);
- });
+ }
}
} else if (t === "score") {
if (!Array.isArray(crit)) {
diff --git a/laya-ts/tests/audit-regressions.test.ts b/laya-ts/tests/audit-regressions.test.ts
index 2b6b1f44..b01c68ec 100644
--- a/laya-ts/tests/audit-regressions.test.ts
+++ b/laya-ts/tests/audit-regressions.test.ts
@@ -86,10 +86,18 @@ describe("audit regressions", () => {
expect(check(["yes", { k: 1 }])).toThrow("choice label 1 is a dict");
expect(check([["y", ["z"]], "no"])).toThrow("choice label 0 is a list");
expect(check(["a", "b", [1]])).toThrow('question "q": choice label 2 is a list');
- expect(check(["a", 1, true, null, 2.5])).not.toThrow();
+ expect(check(["a", 1, true, "", 2.5])).not.toThrow();
expect(() => checkQuestion("q", { type: "choice", instructions: "?", criteria: { a: "x", b: { d: 1 } } })).not.toThrow();
});
+ it("rejects a null choice label, as Python does (#508)", () => {
+ const check = (criteria: unknown[]) => () => checkQuestion("q", { type: "choice", instructions: "?", criteria });
+ expect(check(["billing", null])).toThrow('question "q": choice label 1 is null');
+ expect(check([undefined, "a"])).toThrow("choice label 0 is null");
+ expect(check(["a", , "b"])).toThrow("choice label 1 is null");
+ expect(() => checkQuestion("q", { type: "choice", instructions: "?", criteria: { a: null, b: null } })).not.toThrow();
+ });
+
it("rejects malformed provider output", async () => {
const agent = new Agent({ provider: {
async runEncoder(_batch: any) { return { lastHidden: [[[0, 0]]] }; },