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): an output laya-evals cannot write is a usage error, not exit 1

Aashishcf01552030d3
diff --git a/laya/evals_cli.py b/laya/evals_cli.py
index 16b86e78..55732f3c 100644
--- a/laya/evals_cli.py
+++ b/laya/evals_cli.py
@@ -460,11 +460,24 @@ def _cmd_run(args) -> int:
" ".join("%s=%.4f" % (k, v) for k, v in sorted(metrics.items()))))
if args.json_out:
- with open(args.json_out, "w", encoding="utf-8") as handle:
- json.dump(report.to_json(), handle, ensure_ascii=False, indent=2, sort_keys=True)
+ # `_cmd_agreement` already turns a report it cannot write into `EvalError`, and for the same
+ # reason: a path that is a directory, or on a read-only volume, raises an `OSError` that is
+ # not a `FileNotFoundError`, so it walks past `main`'s usage-error handler and leaves a
+ # traceback and exit 1 -- the code this CLI reserves for "a threshold or a baseline
+ # tolerance failed" -- on a run that wrote nothing. A missing parent directory already
+ # exited 2 through that handler; the two shapes of one mistake have to agree.
+ try:
+ with open(args.json_out, "w", encoding="utf-8") as handle:
+ json.dump(report.to_json(), handle, ensure_ascii=False, indent=2, sort_keys=True)
+ except OSError as exc:
+ raise EvalError("cannot write report %r: %s" % (args.json_out, exc)) from exc
if args.markdown_out:
- with open(args.markdown_out, "w", encoding="utf-8") as handle:
- handle.write(report.to_markdown())
+ try:
+ with open(args.markdown_out, "w", encoding="utf-8") as handle:
+ handle.write(report.to_markdown())
+ except OSError as exc:
+ raise EvalError("cannot write markdown report %r: %s"
+ % (args.markdown_out, exc)) from exc
if failures:
for failure in failures:
diff --git a/tests/test_evals.py b/tests/test_evals.py
index 38c9ca54..07a225c1 100644
--- a/tests/test_evals.py
+++ b/tests/test_evals.py
@@ -2101,6 +2101,59 @@ def test_cli_run_records_the_run_identity(monkeypatch, tmp_path):
assert config["thresholds"]["baseline_tolerance"] == {"choice_accuracy": 0.02}
+def test_cli_run_an_unwritable_output_path_is_a_usage_error(monkeypatch, tmp_path, capsys):
+ """`--json`/`--markdown` to a path that cannot hold a file is the caller's mistake.
+
+ The module docstring reserves exit 1 for "a threshold or a baseline tolerance fails", and the
+ `EvalError` handler in `main` exists precisely so a broken request cannot read as a regression.
+ `_cmd_agreement` already raises `EvalError` for an unwritable report; `_cmd_run` opened both of
+ its outputs bare. Only one shape of that is caught by accident: a missing parent directory
+ raises `FileNotFoundError`, which `main` narrows to exit 2, while a path that *is* a directory
+ raises `IsADirectoryError` (PermissionError on Windows) -- an `OSError` that is not a
+ `FileNotFoundError`, so it escapes every handler and leaves a traceback and exit 1 for a run
+ that wrote nothing. Same mistake, two different codes.
+ """
+ from laya import evals_cli
+
+ real_open = open # captured before any patch below delegates to it
+ _patch_router(monkeypatch)
+ dataset = _write_dataset(tmp_path, RUN_ROWS)
+ spool = str(tmp_path / "spooled.json")
+ cases = [
+ ("a directory that does not exist", str(tmp_path / "no-such-dir" / "report.json"), None),
+ ("a path that is a directory, not a file", str(tmp_path), None),
+ # A full disk, a read-only mount, an expired quota: the errno names no path, so the only
+ # words that can say which output failed are the CLI's own. This is what the `%r` in the
+ # message buys, and `open` really is the call that raises for it.
+ ("a volume that refuses the write", spool, OSError(28, "No space left on device")),
+ ]
+
+ def make_refuser(wanted, err):
+ """`open` for real, except that one path raises. A factory, so nothing binds late."""
+ def refuse(target, *a, **k):
+ if str(target) == wanted:
+ raise err
+ return real_open(target, *a, **k)
+ return refuse
+
+ for flag in ("--json", "--markdown"):
+ for label, path, error in cases:
+ if error is not None:
+ monkeypatch.setattr("builtins.open", make_refuser(path, error))
+ capsys.readouterr()
+ assert evals_cli.main(["run", dataset, flag, path]) == 2, "%s: %s" % (flag, label)
+ err = capsys.readouterr().err
+ assert "Traceback" not in err, "%s %s leaked a traceback" % (flag, label)
+ assert "cannot write" in err, "%s %s: %s" % (flag, label, err)
+ assert path in err, "%s %s did not name the path it could not write: %s" % (
+ flag, label, err)
+ if error is not None:
+ assert "No space left on device" in err, (
+ "%s %s lost the errno the caller needs: %s" % (flag, label, err))
+ # Put `open` back: a later row must not inherit a refuser aimed at this path.
+ monkeypatch.setattr("builtins.open", real_open)
+
+
def test_cli_run_refuses_a_baseline_from_a_different_dataset(monkeypatch, tmp_path, capsys):
"""The gate that a promotion decision rests on: two different runs cannot pass as one.