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(cli): disambiguate bare question id 'questions' from wrapper object

Sarthak Pandey6e5ad0760fed
diff --git a/laya/cli.py b/laya/cli.py
index 89857b4f..8905fd14 100644
--- a/laya/cli.py
+++ b/laya/cli.py
@@ -165,7 +165,15 @@ def load_questions(path):
raise ValueError("--questions file must be a JSON object of question id -> definition")
state_key = "request"
questions = raw
- if "questions" in raw:
+ wrapper_keys = {"questions", "state_key"}
+ is_wrapped = False
+ if set(raw.keys()) <= wrapper_keys and "questions" in raw:
+ inner = raw["questions"]
+ if "state_key" in raw or not isinstance(inner, dict) or not inner:
+ is_wrapped = True
+ elif all(isinstance(v, dict) for v in inner.values()):
+ is_wrapped = True
+ if is_wrapped:
if not isinstance(raw["questions"], dict):
raise ValueError("the 'questions' field of the --questions file must be an object")
questions = raw["questions"]
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 0abd6059..bd702f63 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -471,6 +471,38 @@ check("questions: declared state_key used", stub.states[0] == {"body": "hello"},
check("questions: questions unwrapped",
sorted(stub.questions) == ["spam"], str(sorted(stub.questions or {})))
+# A bare mapping containing a question id named "questions" must not be mistaken for a wrapper
+# object: both single-question and multi-question bare mappings keep the question id intact.
+bare_questions_only = {"questions": {"type": "choice", "instructions": "Any questions?",
+ "options": ["y", "n"]}}
+bare_questions_path = write_json("bare_questions.json", bare_questions_only)
+code, out, err, stub = run_cli(["hello", "--questions", bare_questions_path],
+ router=QuestionRecorder())
+check("questions: bare mapping with 'questions' id preserved",
+ stub.questions == bare_questions_only, str(stub.questions))
+check("questions: bare mapping with 'questions' id uses default state_key",
+ stub.states[0] == {"request": "hello"}, str(stub.states[0]))
+
+bare_multi = {
+ "questions": {"type": "choice", "instructions": "Any questions?", "options": ["y", "n"]},
+ "urgency": {"type": "score", "instructions": "How urgent?"},
+}
+bare_multi_path = write_json("bare_multi.json", bare_multi)
+code, out, err, stub = run_cli(["hello", "--questions", bare_multi_path],
+ router=QuestionRecorder())
+check("questions: multi-question bare mapping with 'questions' id retains all keys",
+ stub.questions == bare_multi, str(stub.questions))
+
+# A wrapper with no state_key unwraps its inner questions.
+wrapped_no_key = {"questions": {"spam": {"type": "noul", "instructions": "Is spam?"}}}
+wrapped_no_key_path = write_json("wrapped_no_key.json", wrapped_no_key)
+code, out, err, stub = run_cli(["hello", "--questions", wrapped_no_key_path],
+ router=QuestionRecorder())
+check("questions: wrapper without state_key unwraps",
+ sorted(stub.questions) == ["spam"], str(sorted(stub.questions or {})))
+check("questions: wrapper without state_key uses default state_key",
+ stub.states[0] == {"request": "hello"}, str(stub.states[0]))
+
# The CLI validates the file's shape and defers question semantics to core, so a question set is
# handed over unchanged rather than normalised twice.
code, out, err, stub = run_cli(["hi", "--questions", write_json("odd.json", {"a": {"type": "bogus"}})],