Skip to content

fix(ts): reject a null choice label, as Python does (#508 parity) - #710

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
Divit-aggarwal:fix/ts-reject-null-choice-label
Sep 29, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
Divit-aggarwal:fix/ts-reject-null-choice-label

Conversation

@Divit-aggarwal

Copy link
Copy Markdown
Contributor

What

checkQuestion in laya-ts now rejects a null or undefined label in a choice question's criteria list (including a hole in a sparse list), with an error that names the question and the index, as Python does since #508.

Why

A list-form label becomes the answer key. A null label comes back as the key "null", which a client cannot tell apart from a real "null" label, and criteria[answer.choice] never finds it:

checkQuestion("q", { type: "choice", instructions: "which?", criteria: ["billing", null] });
// before: accepted; the answer key for the second option is "null"
// after:  question "q": choice label 1 is null; a label is rendered as option text and used as the
//         answer key, so it must be a string, number or bool

The nested-label message now says "string, number or bool" rather than "… or null", matching Python. This reverses one assertion from #551, which accepted null before #508 landed.

How it was verified

cd laya-ts
npx tsc -p tsconfig.json --noEmit   # exit 0
npm test -- --run                   # 23 files, all tests passed
npm run test:package                # package test passed
cd ..
ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120   # All checks passed
python -m compileall -q laya/ tests/                                   # exit 0

The two Python commands are the CI gates; no Python was changed. The new test fails on main and passes with this change.

Scope

  • TypeScript only: one check in checkQuestion and its test
  • A null choice label now throws; dict criteria and "" are unchanged
  • Parity fix only. No prediction-accuracy claim is made

Related: #508 (the Python change). #496's TS port is stacked on this branch.

Written with Claude Code; I've reviewed the change and re-run the verification myself.

🤖 Generated with Claude Code

…parity)

A choice question given its criteria as a list uses each label as an object key
and as the answer key. Python rejects a null label (NandhaKishorM#508): its answer key comes
back as the string "null", which a client cannot tell apart from a real "null"
label, and `criteria[answer.choice]` never finds it. The TypeScript checkQuestion
accepted null and undefined (and a hole in a sparse list, which forEach skipped),
so the same undecodable answer key reached the caller.

Reject a null, undefined or missing label with a message naming the question and
the index, and say "string, number or bool" in the nested-label message, as
Python now does. Dict criteria and the empty string are unchanged.

This is a parity fix; it makes no prediction-accuracy claim.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@Bruce-Yii Bruce-Yii left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the Python side on main (laya/agent.py:653-668) against this diff. The layer and the moment line up — checkQuestion runs immediately before toInternal, the same relationship _check_question has to _to_internal — and the hole handling is worth having on its own: on main, ["a", , "b"] passes checkQuestion because forEach skips the hole, and then toInternal dies with TypeError: Iterator value undefined is not an entry object, naming neither the question nor the label. Two things before merge.

The message drops the clause that explains the rejection. Python raises:

question 'q': choice label 1 is null; a label is rendered as option text and used as the answer key, so it must be a string, number or bool -- a null label renders as the text "None" while its answer key is "null"

This stops at bool. That -- clause is the only part telling a caller why a label which used to be accepted is now an error, and the test matches just the prefix, so nothing catches the omission.

The mechanism in the description does not occur in this runtime. Python's render_options renders with str(k), so a null label's option text is "None" while the JSON key is "null" — the two disagree, which is what makes the answer undecodable. Here toInternal uses Object.fromEntries, which applies ToPropertyKey, so the key is already "null"; renderOptions reads Object.entries, so the text is "null"; and choice comes from Object.keys(q.crit), also "null". I measured ["billing", null] on main: crit = {"billing":null,"null":null}, options = ["billing","null"]. Nothing disagrees internally.

The argument that does hold is the next sentence in your own description: the string "null" and JSON null produce the same key, so a client cannot tell them apart, and after this change only the string can — which makes label-to-key injective. I'd lead with that, and keep the behaviour change explicit, because a null label that round-trips fine today stops being accepted.

Two notes, not blocking. This is stacked on #711, and git merge-tree of the two heads produces exactly #711's tree, so whichever order they are reviewed or merged in, one of them is a no-op — worth deciding which single PR carries this. And moving the nested-label wording from "string, number or null" to "string, number or bool" moves the TS toward main, which already says bool; that part is right.

@NandhaKishorM
NandhaKishorM merged commit 5a67043 into NandhaKishorM:main Sep 29, 2026
20 of 21 checks passed
@NandhaKishorM

Copy link
Copy Markdown
Owner

Passed checks and merged. Thank you @Divit-aggarwal! It ships in 0.3.22.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants