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(evals): a gate policy too deep to parse is a usage error, not a crash

Aashish702595cb66ad
diff --git a/laya/_eval_policy.py b/laya/_eval_policy.py
index 0ce5c736..1abc282e 100644
--- a/laya/_eval_policy.py
+++ b/laya/_eval_policy.py
@@ -22,11 +22,19 @@ def _finite_number(value: Any) -> bool:
def load_policy(path: str) -> Dict[str, Any]:
- """Read and validate a small, versioned JSON gate before a checkpoint is loaded."""
+ """Read and validate a small, versioned JSON gate before a checkpoint is loaded.
+
+ A `RecursionError` is one of the ways a document cannot be read: `json` reports a nesting depth
+ it will not walk as a stack overflow, not as a `ValueError`. Leaving it out here made a policy
+ too deep to parse escape `laya-evals`' usage-error handler as a traceback with exit 1 -- the
+ code the CLI keeps for "a threshold or a tolerance failed" -- which `docs/evals.md` says may
+ only be exit 2, before a checkpoint loads. `_load_report` in `laya/evals_cli.py` already reads
+ a caller's JSON this way.
+ """
try:
with open(path, "r", encoding="utf-8") as handle:
policy = json.load(handle)
- except (OSError, UnicodeError, json.JSONDecodeError) as exc:
+ except (OSError, UnicodeError, json.JSONDecodeError, RecursionError) as exc:
raise evals.EvalError("invalid gate policy %r: %s" % (path, exc)) from exc
if not isinstance(policy, dict) or set(policy) != {"version", "rules"}:
raise evals.EvalError("gate policy needs exactly 'version' and 'rules'")
diff --git a/tests/test_evals.py b/tests/test_evals.py
index c67bc906..dc536e1d 100644
--- a/tests/test_evals.py
+++ b/tests/test_evals.py
@@ -3454,7 +3454,7 @@ def test_cli_relative_slice_rule_requires_baseline_before_loading(monkeypatch, t
assert not built
-def test_cli_rejects_invalid_policy_json_and_version(monkeypatch, tmp_path):
+def test_cli_rejects_invalid_policy_json_and_version(monkeypatch, tmp_path, capsys):
from laya import evals_cli
built = _patch_router(monkeypatch)
@@ -3462,9 +3462,16 @@ def test_cli_rejects_invalid_policy_json_and_version(monkeypatch, tmp_path):
path = tmp_path / "gates.json"
for content in ("{not json", json.dumps({"version": 2, "rules": [
{"slice": {"language": "zh"}, "metric": "choice_accuracy",
- "min_count": 1, "min": 0.5}]})):
+ "min_count": 1, "min": 0.5}]}),
+ # Unclosable depth is a document `json` refuses to read, and it says so with a
+ # `RecursionError` rather than a `ValueError`. It is the caller's file, so `docs/evals.md`
+ # ("Invalid policy syntax exits 2 before a checkpoint loads") belongs to it too.
+ "[" * 100000 + "]" * 100000):
path.write_text(content, encoding="utf-8")
assert evals_cli.main(["run", dataset, "--gate-policy", str(path)]) == 2
+ err = capsys.readouterr().err
+ assert "Traceback" not in err, "exit 2 with a traceback is the shape this refuses"
+ assert err.startswith("laya-evals: "), "a usage error, named as one: %r" % err
assert not built