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(mcp): run Agent directly for model=auto and report null model

umran66640f397d759b5
diff --git a/laya/mcp/tools.py b/laya/mcp/tools.py
index 810f5edb..87efd2d5 100644
--- a/laya/mcp/tools.py
+++ b/laya/mcp/tools.py
@@ -132,11 +132,18 @@ def laya_predict(
state_d = validate_state(state)
questions_d = validate_questions(questions)
model_name = validate_model(model)
+ auto_without_router = False
def _run() -> Any:
if model_name == "auto":
if router is None:
- raise ToolError("models_not_ready", "Router is not loaded (auto mode)")
+ if agent is None:
+ raise ToolError("models_not_ready", "Router is not loaded (auto mode)")
+ # An Agent answers but does not route: run it directly and report
+ # a null model below, instead of echoing 'auto' (#444).
+ nonlocal auto_without_router
+ auto_without_router = True
+ return agent.predict(state_d, questions_d)
return router.predict(state_d, questions_d)
if agent is not None:
return agent.predict(state_d, questions_d)
@@ -153,12 +160,26 @@ def laya_predict(
# Router.predict and Agent.system_one both return the system_one payload,
# which always carries an "answers" object (empty for empty questions).
answers = _normalize_answers(result["answers"])
- routing = result.get("routing") or {"model": model_name, "repo": None, "reason": "explicit model"}
+ if auto_without_router:
+ routing = {"model": None, "repo": None, "reason": "auto routing without router"}
+ else:
+ routing = result.get("routing")
+ if not routing:
+ # Agent.predict() returns the system_one payload, which carries no
+ # 'routing' key because the Agent itself does no routing. 'auto' is a
+ # routing directive, not a checkpoint name, so it must never be echoed
+ # back as the answering model (#444).
+ if model_name == "auto":
+ routing = {"model": None, "repo": None, "reason": "auto routing without router"}
+ else:
+ routing = {"model": model_name, "repo": None, "reason": "explicit model"}
# Real device of the checkpoint that answered: Agent.device reflects a
# silent GPU -> CPU fallback. Omitted when it cannot be read, rather than
- # guessed.
+ # guessed. With no routing decision there is no checkpoint to read it from.
device = None
- if model_name != "auto" and agent is not None:
+ if auto_without_router:
+ pass
+ elif model_name != "auto" and agent is not None:
device = agent_device(agent)
else:
model_used = routing.get("model")
diff --git a/tests/test_mcp.py b/tests/test_mcp.py
index 4d3afab5..f48bcabd 100644
--- a/tests/test_mcp.py
+++ b/tests/test_mcp.py
@@ -153,6 +153,28 @@ class FakeAgent:
device = "cpu"
+class FakeAgentDirect:
+ """An Agent used for model='auto' with no Router present (#444).
+
+ Agent.predict / system_one return the system_one payload but carry no
+ 'routing' key, because the Agent itself does no routing.
+ """
+
+ device = "cpu"
+
+ def predict(self, state, questions, **kwargs):
+ answers = {}
+ for name, spec in questions.items():
+ if spec["type"] == "choice":
+ answers[name] = {"choice": "billing", "confidence": 0.94, "probs": {"billing": 0.94}}
+ elif spec["type"] == "score":
+ answers[name] = {"score": 1.84, "confidence": 0.8, "distribution": [0.1, 0.3, 0.6]}
+ else:
+ answers[name] = {"noul": 0.892, "confidence": 0.89}
+ # Note: no "routing" key -- an Agent does not route.
+ return {"answers": answers}
+
+
class FakeRouter:
_agents = {"english": FakeAgent()}
@@ -175,6 +197,15 @@ class FakeRouter:
return D()
+class FakeRouterNoRouting(FakeRouter):
+ """A router whose predict returns an Agent-shaped payload (no 'routing' key)."""
+
+ def predict(self, state, questions, **kwargs):
+ result = super().predict(state, questions, **kwargs)
+ result.pop("routing", None)
+ return result
+
+
def test_shape():
out = laya_predict(STATE, QUESTIONS, model="auto", router=FakeRouter())
ok("shape/predict_keys", set(out) >= {"answers", "routing", "latency_ms"})
@@ -225,6 +256,30 @@ def test_shape():
lambda: laya_predict(STATE, {}, model="auto", router=FakeRouter()),
"invalid_questions")
+ # #444: model='auto' with an Agent and no Router runs the Agent directly and
+ # reports a null model -- 'auto' is a routing directive, not a checkpoint name.
+ out = laya_predict(STATE, QUESTIONS, model="auto", agent=FakeAgentDirect())
+ ok("shape/predict_auto_agent_model_null",
+ out["routing"] == {"model": None, "repo": None, "reason": "auto routing without router"},
+ repr(out.get("routing")))
+ ok("shape/predict_auto_agent_answers", out["answers"]["department"]["choice"] == "billing")
+ ok("shape/predict_auto_agent_no_device", "device" not in out, repr(set(out)))
+ # auto with neither Router nor Agent still errors, as before.
+ expect_tool_error("shape/predict_auto_needs_router_or_agent",
+ lambda: laya_predict(STATE, QUESTIONS, model="auto"),
+ "models_not_ready")
+ # a Router payload without a 'routing' key hits the same null-model fallback.
+ out = laya_predict(STATE, QUESTIONS, model="auto", router=FakeRouterNoRouting())
+ ok("shape/predict_auto_fallback_model_null",
+ out["routing"] == {"model": None, "repo": None, "reason": "auto routing without router"},
+ repr(out.get("routing")))
+ ok("shape/predict_auto_fallback_answers", out["answers"]["department"]["choice"] == "billing")
+ # an explicit checkpoint still names itself in the same fallback.
+ out = laya_predict(STATE, QUESTIONS, model="english", router=FakeRouterNoRouting())
+ ok("shape/predict_explicit_fallback_names_checkpoint",
+ out["routing"] == {"model": "english", "repo": None, "reason": "explicit model"},
+ repr(out.get("routing")))
+
def test_question_forwarding():
questions = {