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): a question file or batch file that cannot be read is a usage error naming the file

Aashishb68fd1b7b89e
diff --git a/laya/cli.py b/laya/cli.py
index 24121be6..d5794be0 100644
--- a/laya/cli.py
+++ b/laya/cli.py
@@ -164,6 +164,17 @@ def load_questions(path):
raw = json.load(handle)
except FileNotFoundError:
raise ValueError("no such --questions file: %s" % path)
+ # The same three refusals `laya/evidence.py::_read_json_object` makes: a bare JSONDecodeError or
+ # UnicodeDecodeError says what is wrong with the text but not which file, and a too-deep document
+ # surfaces as a RecursionError that the caller's `except (OSError, RuntimeError, ...)` misreads as
+ # a dependency/download problem. Prefix each with the path so it is a usage error about this file.
+ except json.JSONDecodeError as error:
+ raise ValueError("--questions file %s is not valid JSON: %s" % (path, error)) from error
+ except UnicodeDecodeError as error:
+ raise ValueError("--questions file %s is not UTF-8 text: %s" % (path, error)) from error
+ except RecursionError as error:
+ raise ValueError("--questions file %s is JSON nested deeper than it can be read: %s"
+ % (path, error)) from error
if not isinstance(raw, dict):
raise ValueError("--questions file must be a JSON object of question id -> definition")
state_key = "request"
@@ -483,7 +494,7 @@ def main(argv=None):
return 2
try:
lines = read_batch_lines(args.batch)
- except OSError as error:
+ except (OSError, UnicodeDecodeError) as error:
print("laya: could not read %s (%s)." % (args.batch, error), file=sys.stderr)
return 2
if not lines:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 1e5a75b0..a5454140 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -384,6 +384,17 @@ code, out, err, stub = run_batch_cli(["--batch", os.path.join(tmp, "missing.txt"
check("batch missing file: exit 2 with a named failure",
code == 2 and "could not read" in err, "code %r err %r" % (code, err))
+# read_batch_lines opens FILE with encoding="utf-8", so a non-UTF-8 file fails inside the read
+# with a UnicodeDecodeError -- a ValueError, not an OSError -- which the old `except OSError`
+# let escape as a traceback with exit 1, naming no file. It is a usage error about this file.
+bad_bytes = os.path.join(tmp, "latin1.txt")
+with open(bad_bytes, "wb") as handle:
+ handle.write(b"caf\xe9 ticket\nsecond line\n")
+code, out, err, stub = run_batch_cli(["--batch", bad_bytes])
+check("batch non-UTF-8 file: exit 2 naming the file, not a traceback",
+ code == 2 and "could not read" in err and bad_bytes in err,
+ "code %r err %r" % (code, err))
+
code, out, err, stub = run_batch_cli(["--batch", path, "inline text"])
check("batch: text and --batch are mutually exclusive",
code == 2 and "not both" in err, "code %r err %r" % (code, err))
@@ -669,6 +680,36 @@ code, out, err, stub = run_cli(["hi", "--questions", os.path.join(_TMP, "missing
check("questions: missing file is a handled error",
code == 2 and "no such --questions file" in err, "code %r, err %r" % (code, err))
+
+def write_raw(name, data):
+ path = os.path.join(_TMP, name)
+ with open(path, "wb") as handle:
+ handle.write(data)
+ return path
+
+
+# A file that is not valid JSON, not UTF-8, or nested past what json.load can read used to leak
+# out of load_questions as a bare JSONDecodeError / UnicodeDecodeError / RecursionError: the first
+# two named the fault but not the file, and RecursionError was swallowed by run()'s
+# `except (ImportError, OSError, RuntimeError)` and reported as a checkpoint/download problem --
+# a false diagnosis for a corrupt question file. Each is now a path-naming ValueError (exit 2).
+not_json_path = write_raw("not_json.json", b"{ this is not json }")
+code, out, err, stub = run_cli(["hi", "--questions", not_json_path], router=QuestionRecorder())
+check("questions: invalid JSON is exit 2 naming the file",
+ code == 2 and "not valid JSON" in err and not_json_path in err, "code %r err %r" % (code, err))
+
+non_utf8_path = write_raw("latin1.json", b'{"\xe9": {"type": "noul"}}')
+code, out, err, stub = run_cli(["hi", "--questions", non_utf8_path], router=QuestionRecorder())
+check("questions: non-UTF-8 file is exit 2 naming the file",
+ code == 2 and "not UTF-8 text" in err and non_utf8_path in err, "code %r err %r" % (code, err))
+
+# Depth past the C scanner's limit raises RecursionError, not a decode/parse error.
+deep_path = write_raw("deep.json", b"[" * 100000 + b"]" * 100000)
+code, out, err, stub = run_cli(["hi", "--questions", deep_path], router=QuestionRecorder())
+check("questions: too-deep JSON is exit 2, not a download-error message",
+ code == 2 and "nested deeper" in err and "checkpoint" not in err,
+ "code %r err %r" % (code, err))
+
code, out, err, stub = run_cli(["hi", "--questions", INTENTS_PATH, "--preset", "triage"],
router=QuestionRecorder())
check("questions: two sources refuse each other",