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): an interactive session returns the exit code its requests earned

Aashish158242a0d9da
diff --git a/docs/cli-mcp.md b/docs/cli-mcp.md
index 8e5cb45c..ed6dbcca 100644
--- a/docs/cli-mcp.md
+++ b/docs/cli-mcp.md
@@ -120,8 +120,10 @@ writing a script.
The CLI handles invalid values and common dependency, download, and runtime failures at the
application boundary. It prints a diagnostic to stderr and returns exit code `2` instead of
-showing an unhandled traceback. If a first-use checkpoint download fails, check dependency
-installation, Hub access, and the selected device before retrying.
+showing an unhandled traceback. A handled failure ends the request, not the session: the prompt comes
+back for the next line, and the session exits `2` when it ends, so a piped session reports the
+code a failing request earned. If a first-use checkpoint download
+fails, check dependency installation, Hub access, and the selected device before retrying.
## 2. Built-in MCP stdio server
diff --git a/laya/cli.py b/laya/cli.py
index 50b21daa..4b31d5c7 100644
--- a/laya/cli.py
+++ b/laya/cli.py
@@ -429,6 +429,7 @@ def run_batch(lines, args, router=None):
def interactive(args):
print("Laya interactive mode. Type a request and press Enter; Ctrl-D or 'quit' to exit.")
router = make_router(args)
+ code = 0
while True:
try:
text = input("laya> ").strip()
@@ -437,8 +438,11 @@ def interactive(args):
break
if not text or text.lower() in ("quit", "exit"):
break
- run(text, args, router)
- return 0
+ # A handled failure stays non-fatal to the session, but the process cannot then report
+ # success for a request it knows did not run: `laya < requests.txt` exited 0 with every
+ # answer missing and only stderr to say so.
+ code = run(text, args, router) or code
+ return code
def main(argv=None):
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 817b0197..8f7f1c58 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -1,4 +1,5 @@
"""CLI tests. No model weights are loaded: the Router is stubbed throughout."""
+import builtins
import io
import os
import sys
@@ -83,6 +84,71 @@ check("error: exit code 2", code == 2, "got %r" % code)
check("error: names the failure", "could not run Laya" in err, err)
check("error: points at the fix", "Hugging Face hub" in err, err)
+# --------------------------------------------------------------------- interactive session
+# The page promises exit code 2 for a handled failure, and the interactive loop has to keep it:
+# `laya < requests.txt` is a script, so the session owes the code its requests earned.
+def run_session(lines, argv, router=None):
+ """Drive the interactive loop with `lines` as what gets typed, then Ctrl-D."""
+ stub = router or StubRouter()
+ original_router, original_input = cli.make_router, builtins.input
+ typed = iter(list(lines) + [None])
+
+ def fake_input(_prompt=""):
+ item = next(typed)
+ if item is None:
+ raise EOFError
+ return item
+
+ cli.make_router = lambda args: stub
+ out, err = io.StringIO(), io.StringIO()
+ try:
+ builtins.input = fake_input
+ with redirect_stdout(out), redirect_stderr(err):
+ code = cli.main(argv)
+ finally:
+ cli.make_router = original_router
+ builtins.input = original_input
+ return code, out.getvalue(), err.getvalue(), stub
+
+
+class AlwaysBrokenRouter:
+ """Fails every request but counts them, so a session cut short by one is visible."""
+
+ def __init__(self):
+ self.count = 0
+
+ def route(self, state, **kwargs):
+ self.count += 1
+ raise OSError("connection failed")
+
+
+class SecondRequestFails(AlwaysBrokenRouter):
+ def route(self, state, **kwargs):
+ if self.count:
+ raise OSError("connection failed")
+ self.count += 1
+ return StubDecision()
+
+
+broken = AlwaysBrokenRouter()
+code, out, err, stub = run_session(["first request", "second request"], [], router=broken)
+check("interactive: a handled failure exits 2, not 0", code == 2, "got %r" % code)
+check("interactive: a handled failure keeps the session up",
+ broken.count == 2 and "could not run Laya" in err, "%d calls, err %r" % (broken.count, err))
+
+code, out, err, stub = run_session(["first request", "second request"], [])
+check("interactive: a clean session exits 0", code == 0, "got %r" % code)
+check("interactive: a clean session routes both lines",
+ [c[0] for c in stub.route_calls] == [{"text": "first request"}, {"text": "second request"}],
+ str(stub.route_calls))
+
+mixed = SecondRequestFails()
+code, out, err, stub = run_session(["ok", "bad"], [], router=mixed)
+check("interactive: one failed request is enough for exit 2", code == 2, "got %r" % code)
+
+code, out, err, stub = run_session(["quit"], [], router=AlwaysBrokenRouter())
+check("interactive: leaving the prompt before a request exits 0", code == 0, "got %r" % code)
+
# --------------------------------------------------------------------- explicit flags
code, out, err, stub = run_cli(["--model", "english", "charged twice"])
check("flags: --model forwarded", stub.route_calls[0][1]["model"] == "english")