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.

test(mcp): cover the per-call controls at the tool layer and over the wire

Aashishbe4934dc9333
diff --git a/tests/test_mcp.py b/tests/test_mcp.py
index a674ed53..7fec0a43 100644
--- a/tests/test_mcp.py
+++ b/tests/test_mcp.py
@@ -31,16 +31,20 @@ from laya.mcp.tools import ( # noqa: E402
PRESETS,
PRESET_ALIASES,
ToolError,
+ _overrides,
get_available_presets,
laya_predict,
laya_preset,
laya_route,
laya_shortlist,
laya_status,
+ validate_budget,
+ validate_lang,
validate_model,
validate_preset,
validate_questions,
validate_state,
+ validate_task,
)
PASS, FAIL = [], []
@@ -658,6 +662,386 @@ def test_shortlist():
and out["routing"]["reason"] == "explicit model", repr(out["routing"]))
+# --- per-call controls: task / lang / max_len / head_max_len -------------------
+
+class ControlRouter:
+ """A router that records which keywords the tools handed to core.
+
+ `route` and `predict` both take **kwargs on purpose: what these checks read is which keywords
+ arrive, and an absent one has to be distinguishable from a `None` one -- which is exactly what
+ `_overrides` promises.
+ """
+
+ def __init__(self, routed="english"):
+ self._agents = {routed: ShortlistAgent()}
+ self.routed = routed
+ self.predict_calls = []
+ self.predict_questions = []
+ self.route_calls = []
+
+ def route(self, state, questions, **kwargs):
+ self.route_calls.append(kwargs)
+ return {"model": self.routed, "repo": "fake/repo",
+ "reason": "unit-test route %s" % sorted(kwargs)}
+
+ def predict(self, state, questions, **kwargs):
+ self.predict_calls.append(kwargs)
+ self.predict_questions.append(questions)
+ return {"answers": {"department": {"choice": "billing", "confidence": 0.94}},
+ "routing": {"model": kwargs.get("model", self.routed), "repo": "fake/repo",
+ "reason": "unit-test predict %s" % sorted(kwargs)}}
+
+ def load(self, name):
+ return ShortlistAgent()
+
+
+class ControlAgent:
+ """An ``agent=`` injection: it answers, and it remembers being asked."""
+
+ device = "cpu"
+
+ def __init__(self):
+ self.calls = []
+
+ def predict(self, state, questions, **kwargs):
+ self.calls.append(kwargs)
+ return {"answers": {"department": {"choice": "billing", "confidence": 0.94}}}
+
+
+def test_controls_validation():
+ """Each control is checked the way core checks it, and refused in this layer's words."""
+ from laya.router import DEFAULT_MODELS, _ALIASES
+
+ ok("task/none_is_unset", validate_task(None) is None)
+ for name in sorted(DEFAULT_MODELS):
+ ok("task/canonical_%s" % name, validate_task(name) == name)
+ for alias in sorted(_ALIASES):
+ # Accepted because core accepts it, and forwarded as the caller wrote it: `Router._route`
+ # normalises the task itself, and its `reason` quotes the spelling that was asked for, so
+ # canonicalising here would rewrite the explanation the caller reads back.
+ ok("task/alias_%s" % alias, validate_task(alias) == alias, repr(validate_task(alias)))
+ # What the sweep really protects: every name core's registry holds is accepted here, so this
+ # layer can never be narrower than `router.predict(task=...)` the way a copied list was.
+ from laya.router import normalise_name
+
+ for alias in sorted(_ALIASES):
+ ok("task/resolves_%s" % alias, normalise_name(validate_task(alias)) == _ALIASES[alias],
+ "%s -> %s" % (alias, normalise_name(validate_task(alias))))
+ # The underscore form is what the CLI and the question ids use, and `Router._route` remaps it
+ # before it looks anything up. The tool remaps the same way but forwards the caller's own
+ # spelling, so `routing.reason` still reads as the request that was made.
+ for spelling in ("typed_decisions", "TYPED_DECISIONS", " typed_decisions ", "typed-decisions"):
+ got = validate_task(spelling)
+ ok("task/workflow_%r" % spelling, got == spelling, repr(got))
+ for bad in ("nope", "", " ", "english-ish", 5, [], {}, True):
+ expect_tool_error("task/rejected_%r" % (bad,), lambda b=bad: validate_task(b), "invalid_task")
+ try:
+ validate_task("nope")
+ message = ""
+ except ToolError as exc:
+ message = exc.message
+ ok("task/error_lists_checkpoints", all(n in message for n in sorted(DEFAULT_MODELS)),
+ repr(message))
+
+ ok("lang/none_is_unset", validate_lang(None) is None)
+ # Verbatim, including the codes that mean nothing to core: a blank lang falls through to
+ # detection rather than failing, and whether a code names a language is core's question.
+ for code in ("de", "en-US", "pt", "en_US.UTF-8", "", " ", "DE"):
+ ok("lang/verbatim_%r" % code, validate_lang(code) == code)
+ for bad in (5, ["de"], {"lang": "de"}, True):
+ expect_tool_error("lang/rejected_%r" % (bad,), lambda b=bad: validate_lang(b), "invalid_lang")
+
+ ok("budget/none_is_unset", validate_budget(None, "max_len") is None)
+ for value in (1, 8, 512, 8192):
+ ok("budget/ok_%d" % value, validate_budget(value, "max_len") == value)
+ # The code carries the argument's own name, so a client that set one wrongly learns which.
+ for bad in (0, -1, 1.5, 384.0, "384", True, [], {}, " "):
+ for arg in ("max_len", "head_max_len"):
+ expect_tool_error("budget/rejected_%s_%r" % (arg, bad),
+ lambda b=bad, a=arg: validate_budget(b, a), "invalid_%s" % arg)
+ # 384.0 is a float and core slices with it (`ids[:max_len]`), which is a TypeError in the middle
+ # of a forward pass. JSON writes both 384 and 384.0, so this is a real input, not a corner.
+ expect_tool_error("budget/float_is_rejected", lambda: validate_budget(384.0, "max_len"),
+ "invalid_max_len")
+
+ ok("overrides/all_unset", _overrides(None, None, None, None) == {})
+ ok("overrides/drops_unset",
+ _overrides(None, "de", None, 384) == {"lang": "de", "head_max_len": 384},
+ repr(_overrides(None, "de", None, 384)))
+ ok("overrides/keeps_falsy_lang", _overrides(None, "", None, None) == {"lang": ""},
+ repr(_overrides(None, "", None, None)))
+ ok("overrides/names_in_order",
+ list(_overrides("t", "l", 1, 2)) == ["task", "lang", "max_len", "head_max_len"])
+
+
+def test_controls_predict():
+ """What `laya_predict` forwards, and what it refuses to forward."""
+ # The baseline these checks protect: a call that sets no control reaches core exactly as it did
+ # before the controls existed -- no keywords at all.
+ router = ControlRouter()
+ laya_predict(STATE, QUESTIONS, router=router)
+ ok("predict/no_controls_call", router.predict_calls == [{}], repr(router.predict_calls))
+ laya_predict(STATE, QUESTIONS, model="english", router=router)
+ ok("predict/pinned_model_only_kwarg", router.predict_calls[-1] == {"model": "english"},
+ repr(router.predict_calls[-1]))
+
+ router = ControlRouter()
+ out = laya_predict(STATE, QUESTIONS, task="typed_decisions", lang="de",
+ max_len=1024, head_max_len=512, router=router)
+ ok("predict/all_controls_forwarded",
+ router.predict_calls == [{"task": "typed_decisions", "lang": "de",
+ "max_len": 1024, "head_max_len": 512}],
+ repr(router.predict_calls))
+ ok("predict/answers_untouched", out["answers"]["department"]["choice"] == "billing")
+
+ router = ControlRouter()
+ laya_predict(STATE, QUESTIONS, lang="de", router=router)
+ ok("predict/lang_only", router.predict_calls == [{"lang": "de"}], repr(router.predict_calls))
+ router = ControlRouter()
+ laya_predict(STATE, QUESTIONS, head_max_len=384, router=router)
+ ok("predict/budget_only", router.predict_calls == [{"head_max_len": 384}],
+ repr(router.predict_calls))
+
+ # A pinned checkpoint plus a task is two answers to one question: `Router._route` checks model
+ # first and never reads the task, and `Agent.system_one` does not accept it at all.
+ for model in ("english", "laya", "ML"):
+ router = ControlRouter()
+ expect_tool_error("predict/model_plus_task_%s" % model,
+ lambda m=model: laya_predict(STATE, QUESTIONS, model=m,
+ task="typed_decisions", router=router),
+ "invalid_task")
+ ok("predict/model_plus_task_no_call_%s" % model, router.predict_calls == [],
+ repr(router.predict_calls))
+
+ # Everything is checked before core is called, so a bad value costs no forward pass.
+ router = ControlRouter()
+ expect_tool_error("predict/bad_budget_before_core",
+ lambda: laya_predict(STATE, QUESTIONS, max_len=0, router=router),
+ "invalid_max_len")
+ ok("predict/bad_budget_no_call", router.predict_calls == [], repr(router.predict_calls))
+ router = ControlRouter()
+ expect_tool_error("predict/bad_task_before_core",
+ lambda: laya_predict(STATE, QUESTIONS, task="nope", router=router),
+ "invalid_task")
+ ok("predict/bad_task_no_call", router.predict_calls == [], repr(router.predict_calls))
+
+ # The agent branch: budget and lang reach a checkpoint directly, task cannot mean anything.
+ agent = ControlAgent()
+ laya_predict(STATE, QUESTIONS, model="english", agent=agent)
+ ok("predict/agent_no_controls_call", agent.calls == [{}], repr(agent.calls))
+ agent = ControlAgent()
+ out = laya_predict(STATE, QUESTIONS, model="english", lang="de", max_len=1024,
+ head_max_len=512, agent=agent)
+ ok("predict/agent_budget_forwarded",
+ agent.calls == [{"lang": "de", "max_len": 1024, "head_max_len": 512}], repr(agent.calls))
+ ok("predict/agent_routing_recorded", out["routing"]["model"] == "english", repr(out["routing"]))
+ agent = ControlAgent()
+ expect_tool_error("predict/agent_task_refused",
+ lambda a=agent: laya_predict(STATE, QUESTIONS, model="english",
+ task="typed_decisions", agent=a),
+ "invalid_task")
+ ok("predict/agent_task_no_call", agent.calls == [], repr(agent.calls))
+
+ # A router whose predict() accepts only state and questions still answers a plain call.
+ class LegacyRouter:
+ _agents = {"english": FakeAgent()}
+
+ def predict(self, state, questions):
+ return {"answers": {"department": {"choice": "other", "confidence": 0.6}}}
+
+ out = laya_predict(STATE, QUESTIONS, router=LegacyRouter())
+ ok("predict/legacy_router_untouched", out["answers"]["department"]["choice"] == "other")
+
+
+def test_controls_route():
+ """`laya_route` takes the routing overrides, so a route can explain a pinned predict."""
+ router = ControlRouter(routed="multilingual")
+ laya_route(STATE, QUESTIONS, router=router)
+ ok("route/no_overrides_call", router.route_calls == [{}], repr(router.route_calls))
+
+ router = ControlRouter(routed="multilingual")
+ out = laya_route(STATE, QUESTIONS, task="typed_decisions", lang="de", router=router)
+ ok("route/overrides_forwarded",
+ router.route_calls == [{"task": "typed_decisions", "lang": "de"}], repr(router.route_calls))
+ ok("route/decision_shape", set(out) == {"model", "repo", "reason"}, repr(out))
+
+ # `auto` is this layer's sentinel and core has no such name: it means "do not pin", so it is
+ # dropped rather than forwarded as a checkpoint that does not exist.
+ for unset in (None, "auto", "AUTO", " auto "):
+ router = ControlRouter()
+ laya_route(STATE, QUESTIONS, model=unset, router=router)
+ ok("route/auto_is_absent_%r" % (unset,), router.route_calls == [{}], repr(router.route_calls))
+ router = ControlRouter()
+ laya_route(STATE, QUESTIONS, model="en", router=router)
+ ok("route/model_canonical", router.route_calls == [{"model": "english"}],
+ repr(router.route_calls))
+ router = ControlRouter()
+ expect_tool_error("route/model_plus_task",
+ lambda: laya_route(STATE, QUESTIONS, model="english",
+ task="typed_decisions", router=router),
+ "invalid_task")
+ ok("route/refused_before_core", router.route_calls == [], repr(router.route_calls))
+ for bad in ("nope", 5, []):
+ router = ControlRouter()
+ expect_tool_error("route/bad_task_%r" % (bad,),
+ lambda b=bad: laya_route(STATE, QUESTIONS, task=b, router=router),
+ "invalid_task")
+ ok("route/bad_task_no_call_%r" % (bad,), router.route_calls == [], repr(router.route_calls))
+ router = ControlRouter()
+ expect_tool_error("route/bad_lang",
+ lambda: laya_route(STATE, QUESTIONS, lang=["de"], router=router),
+ "invalid_lang")
+ ok("route/bad_lang_no_call", router.route_calls == [], repr(router.route_calls))
+ # No budget on a route: there is no forward pass to size, so the parameter does not exist.
+ try:
+ laya_route(STATE, QUESTIONS, max_len=512, router=ControlRouter())
+ ok("route/no_budget_param", False, "accepted max_len")
+ except TypeError:
+ ok("route/no_budget_param", True)
+
+
+def test_controls_shortlist():
+ """The budget reaches the answering pass; the task reaches only the route that chose it."""
+ small = {"dept": {"type": "choice", "instructions": "pick",
+ "criteria": {"a": "A", "b": "B", "c": "C", "d": "D", "e": "E"}}}
+
+ router = ControlRouter(routed="multilingual")
+ laya_shortlist(STATE, small, k=2, router=router, embed_fn=_tie_embed)
+ ok("shortlist/no_controls_route", router.route_calls == [{}], repr(router.route_calls))
+ ok("shortlist/no_controls_predict", router.predict_calls == [{"model": "multilingual"}],
+ repr(router.predict_calls))
+
+ router = ControlRouter(routed="multilingual")
+ out = laya_shortlist(STATE, small, k=2, task="typed_decisions", lang="de",
+ max_len=1024, head_max_len=384, router=router, embed_fn=_tie_embed)
+ ok("shortlist/route_sees_task",
+ router.route_calls == [{"task": "typed_decisions", "lang": "de"}], repr(router.route_calls))
+ ok("shortlist/predict_sees_budget",
+ router.predict_calls == [{"model": "multilingual", "lang": "de",
+ "max_len": 1024, "head_max_len": 384}],
+ repr(router.predict_calls))
+ ok("shortlist/shortlist_still_ran", out["shortlist"]["dept"]["k"] == 2
+ and len(out["shortlist"]["dept"]["labels"]) == 2, repr(out["shortlist"]["dept"]))
+
+ router = ControlRouter(routed="multilingual")
+ laya_shortlist(STATE, small, k=2, model="english", lang="de", head_max_len=384,
+ router=router, embed_fn=_tie_embed)
+ ok("shortlist/pinned_skips_route", router.route_calls == [], repr(router.route_calls))
+ ok("shortlist/pinned_budget",
+ router.predict_calls == [{"model": "english", "lang": "de", "head_max_len": 384}],
+ repr(router.predict_calls))
+
+ router = ControlRouter()
+ expect_tool_error("shortlist/pinned_plus_task",
+ lambda: laya_shortlist(STATE, small, k=2, model="english",
+ task="typed_decisions", router=router,
+ embed_fn=_tie_embed),
+ "invalid_task")
+ ok("shortlist/pinned_plus_task_no_call", router.predict_calls == [], repr(router.predict_calls))
+
+ agent = ControlAgent()
+ laya_shortlist(STATE, small, k=2, model="english", agent=agent, lang="de",
+ head_max_len=384, embed_fn=_tie_embed)
+ ok("shortlist/agent_budget", agent.calls == [{"lang": "de", "head_max_len": 384}],
+ repr(agent.calls))
+ agent = ControlAgent()
+ expect_tool_error("shortlist/agent_task_refused",
+ lambda a=agent: laya_shortlist(STATE, small, k=2, model="english",
+ task="typed", agent=a, embed_fn=_tie_embed),
+ "invalid_task")
+ ok("shortlist/agent_task_no_call", agent.calls == [], repr(agent.calls))
+
+ # A bad budget is refused before the embedding pass, which is the expensive half.
+ calls = []
+
+ def counting_embed(texts):
+ calls.append(len(texts))
+ return _tie_embed(texts)
+
+ router = ControlRouter()
+ expect_tool_error("shortlist/bad_budget_before_embedding",
+ lambda: laya_shortlist(STATE, small, k=2, head_max_len="384", router=router,
+ embed_fn=counting_embed),
+ "invalid_head_max_len")
+ ok("shortlist/bad_budget_no_embedding", calls == [], repr(calls))
+
+
+def test_controls_preset():
+ """The preset fixes the questions, not the route or the budget."""
+ def builder(attr):
+ return {"probe": {"type": "noul", "instructions": "Does the `body` need a human?"}}
+
+ router = ControlRouter()
+ laya_preset("guard", STATE, router=router, preset_builder=builder)
+ ok("preset/no_controls_call", router.predict_calls == [{}], repr(router.predict_calls))
+
+ router = ControlRouter()
+ out = laya_preset("guard", STATE, task="typed_decisions", lang="de", max_len=1024,
+ head_max_len=384, router=router, preset_builder=builder)
+ ok("preset/controls_forwarded",
+ router.predict_calls == [{"task": "typed_decisions", "lang": "de",
+ "max_len": 1024, "head_max_len": 384}],
+ repr(router.predict_calls))
+ # The questions the preset builder produced are what core was asked, alongside the controls --
+ # the preset supplies the questions, the caller supplies how they get answered.
+ ok("preset/questions_reached_core",
+ list(router.predict_questions[-1]) == ["probe"], repr(router.predict_questions[-1]))
+ ok("preset/result_shape", set(out) >= {"answers", "routing", "latency_ms"}, repr(sorted(out)))
+
+ for args, code in (({"max_len": -1}, "invalid_max_len"),
+ ({"task": "nope"}, "invalid_task"),
+ ({"lang": 7}, "invalid_lang"),
+ ({"head_max_len": "512"}, "invalid_head_max_len")):
+ router = ControlRouter()
+ expect_tool_error("preset/rejected_%r" % (args,),
+ lambda a=args: laya_preset("guard", STATE, router=router,
+ preset_builder=builder, **a),
+ code)
+ ok("preset/rejected_no_call_%r" % (args,), router.predict_calls == [],
+ repr(router.predict_calls))
+
+
+def test_controls_signature_and_schema():
+ """The keywords a client can see, and that none of them became required.
+
+ A new required argument would break every prompt already in the wild; a Python keyword with no
+ schema entry would be a control nobody can send. Both directions are checked.
+ """
+ import inspect
+
+ controls = ["task", "lang", "max_len", "head_max_len"]
+ for fn, want in ((laya_predict, controls), (laya_shortlist, controls),
+ (laya_preset, controls), (laya_route, ["model", "task", "lang"])):
+ params = inspect.signature(fn).parameters
+ for name in want:
+ ok("signature/%s_has_%s" % (fn.__name__, name), name in params, repr(sorted(params)))
+ ok("signature/%s_%s_default_none" % (fn.__name__, name),
+ params[name].default is None, repr(params[name].default))
+ ok("signature/%s_%s_keyword_only" % (fn.__name__, name),
+ params[name].kind is inspect.Parameter.KEYWORD_ONLY)
+ ok("signature/predict_positionals_kept",
+ list(inspect.signature(laya_predict).parameters)[:3] == ["state", "questions", "model"],
+ repr(list(inspect.signature(laya_predict).parameters)))
+
+ by_name = {t.name: t for t in asyncio.run(mcp_server.list_tools())}
+ for name, want in (("laya_predict", controls), ("laya_shortlist", controls),
+ ("laya_preset", controls), ("laya_route", ["model", "task", "lang"])):
+ tool = by_name[name]
+ schema = tool.input_schema if hasattr(tool, "input_schema") else tool.inputSchema
+ props = schema.get("properties", {})
+ required = schema.get("required", [])
+ for arg in want:
+ ok("schema/%s_exposes_%s" % (name, arg), arg in props, repr(sorted(props)))
+ spec = props.get(arg, {})
+ ok("schema/%s_%s_nullable" % (name, arg),
+ {"type": "null"} in (spec.get("anyOf") or []), repr(spec))
+ ok("schema/%s_%s_default_none" % (name, arg),
+ spec.get("default", "missing") is None, repr(spec))
+ ok("schema/%s_%s_not_required" % (name, arg), arg not in required, repr(required))
+ ok("schema/%s_desc_documents_%s" % (name, arg), arg in tool.description.lower())
+ ok("schema/%s_required_unchanged" % name,
+ required == (["preset", "state"] if name == "laya_preset" else ["state", "questions"]),
+ repr(required))
def test_timeout_removed():
# The per-call timeout was removed: a ThreadPoolExecutor shutdown waits for
# the work anyway, and MCP clients apply their own request timeout. The tool
@@ -719,6 +1103,12 @@ test_model_forwarding()
test_shape()
test_question_forwarding()
test_shortlist()
+test_controls_validation()
+test_controls_predict()
+test_controls_route()
+test_controls_shortlist()
+test_controls_preset()
+test_controls_signature_and_schema()
test_timeout_removed()
test_models_from_env()
test_server_registration()
diff --git a/tests/test_mcp_local_e2e.py b/tests/test_mcp_local_e2e.py
index 6ccca90e..39bad939 100644
--- a/tests/test_mcp_local_e2e.py
+++ b/tests/test_mcp_local_e2e.py
@@ -242,6 +242,120 @@ def main():
ok("e2e/preset_jailbreak_value", 0.0 <= jailbreak.get("noul", -1) <= 1.0, repr(jailbreak))
ok("e2e/preset_jailbreak_detected", jailbreak.get("noul", 0) > 0.5,
"explicit injection prompt -> expect jailbreak flagged: %r" % jailbreak)
+
+ # --- per-call controls over the wire -------------------------------------------
+ # The schema an MCP client actually reads, not the one the module builds.
+ by_name = {t["name"]: t for t in tools.get("tools", [])}
+ for tname, args in (("laya_predict", ["task", "lang", "max_len", "head_max_len"]),
+ ("laya_shortlist", ["task", "lang", "max_len", "head_max_len"]),
+ ("laya_preset", ["task", "lang", "max_len", "head_max_len"]),
+ ("laya_route", ["model", "task", "lang"])):
+ props = (by_name[tname].get("inputSchema") or {}).get("properties") or {}
+ for arg in args:
+ ok("e2e/schema_%s_%s" % (tname, arg), arg in props, repr(sorted(props)))
+ ok("e2e/schema_%s_%s_optional" % (tname, arg),
+ arg not in ((by_name[tname].get("inputSchema") or {}).get("required") or []),
+ repr(by_name[tname].get("inputSchema")))
+
+ # A two-option choice is nowhere near the head budget, so the controls must not move
+ # the answer: this is the "setting them changes routing/sizing, not this decision"
+ # check, and the one that would fail if a budget were applied to the wrong thing.
+ baseline = json.loads(client.request(
+ "tools/call", {"name": "laya_predict", "arguments": ticket})["content"][0]["text"])
+ sized = dict(ticket)
+ sized.update({"max_len": 1024, "head_max_len": 384})
+ result = client.request("tools/call", {"name": "laya_predict", "arguments": sized})
+ payload = json.loads(result["content"][0]["text"])
+ ok("e2e/controls_predict_not_error", not result.get("isError"), repr(payload)[:300])
+ ok("e2e/controls_budget_leaves_small_choice_alone",
+ (payload.get("answers") or {}).get("department", {}).get("choice")
+ == (baseline.get("answers") or {}).get("department", {}).get("choice"),
+ "sized=%r baseline=%r" % ((payload.get("answers") or {}).get("department"),
+ (baseline.get("answers") or {}).get("department")))
+
+ # lang is a routing override, so the checkpoint it names is the answer's own routing.
+ for code, expect in (("en", "english"), ("de", "multilingual")):
+ argued = dict(ticket)
+ argued["lang"] = code
+ payload = json.loads(client.request(
+ "tools/call", {"name": "laya_predict", "arguments": argued})["content"][0]["text"])
+ ok("e2e/controls_lang_%s_routes_%s" % (code, expect),
+ payload.get("routing", {}).get("model") == expect, repr(payload.get("routing")))
+ ok("e2e/controls_lang_%s_reason" % code,
+ "explicit lang" in (payload.get("routing", {}).get("reason") or ""),
+ repr(payload.get("routing", {}).get("reason")))
+
+ payload = json.loads(client.request(
+ "tools/call",
+ {"name": "laya_route", "arguments": {"state": ticket["state"],
+ "questions": ticket["questions"],
+ "lang": "de"}})["content"][0]["text"])
+ ok("e2e/controls_route_lang", payload.get("model") == "multilingual", repr(payload))
+
+ # A bad value comes back as this layer's code, not internal_error and not a crash:
+ # that distinction is the whole reason the validation sits in the tool layer.
+ # Two shapes are worth separating. A value the schema can carry but the tool refuses
+ # (`max_len: 0`) comes back as this layer's JSON payload; a value the schema cannot
+ # carry at all (`lang: ["de"]`, or an integer written as `"384"`, which the schema
+ # coerces to 384 before the tool ever sees it) is settled by the protocol layer, and
+ # the only promise is that no forward pass happens.
+ def call_predict(**extra):
+ call = dict(ticket)
+ call.update(extra)
+ res = client.request("tools/call", {"name": "laya_predict", "arguments": call})
+ text = res["content"][0]["text"]
+ # A protocol-layer refusal renders the offending value inside its prose (an empty
+ # dict for `head_max_len: {}`), so the first `{` is not always the start of a
+ # complete JSON document. raw_decode reads the object that is there and ignores
+ # the sentence around it; this check only asks whether a forward pass happened.
+ start = text.find("{")
+ payload = {}
+ if start >= 0:
+ try:
+ payload = json.JSONDecoder().raw_decode(text, start)[0]
+ except ValueError:
+ payload = {}
+ return res, payload
+
+ for args, want in (({"max_len": 0}, "invalid_max_len"),
+ ({"max_len": -1}, "invalid_max_len"),
+ ({"head_max_len": 0}, "invalid_head_max_len"),
+ ({"task": "nope"}, "invalid_task"),
+ ({"model": "english", "task": "typed_decisions"}, "invalid_task")):
+ res, payload = call_predict(**args)
+ ok("e2e/controls_error_%s" % sorted(args), res.get("isError") is True,
+ repr(res.get("isError")))
+ ok("e2e/controls_error_code_%s" % sorted(args), payload.get("error") == want,
+ repr(payload))
+ # A refused *name* says what the names are; a refused *combination* explains the
+ # combination. Both are this layer's words, not internal_error.
+ if args == {"task": "nope"}:
+ ok("e2e/controls_error_lists_options",
+ "typed-decisions" in (payload.get("message") or ""), repr(payload))
+
+ # A blank lang is not an error: it resolves to no usable hint and falls through to
+ # detection, exactly as core documents it. The tool must not turn it into a refusal.
+ res, payload = call_predict(lang="")
+ ok("e2e/controls_blank_lang_answers", not res.get("isError")
+ and "answers" in payload, repr(payload)[:200])
+
+ # An integer written as a string is coerced by the schema before the tool sees it, so
+ # the tool's own type check is a backstop for in-process callers, not the wire path.
+ # Recorded here because it is the difference between the two sets of checks.
+ res, sized_string = call_predict(head_max_len="384")
+ ok("e2e/controls_budget_string_coerced", not res.get("isError")
+ and "answers" in sized_string, repr(sized_string)[:200])
+ ok("e2e/controls_budget_string_matches_int",
+ sized_string.get("answers", {}).get("department", {}).get("choice")
+ == payload.get("answers", {}).get("department", {}).get("choice"),
+ repr([sized_string.get("answers"), payload.get("answers")])[:200])
+
+ for args in ({"lang": ["de"]}, {"max_len": "many"}, {"max_len": [1]},
+ {"head_max_len": {}}):
+ res, payload = call_predict(**args)
+ ok("e2e/controls_schema_rejects_%s" % sorted(args), res.get("isError") is True
+ or payload.get("error") in ("invalid_max_len", "invalid_head_max_len"),
+ repr(payload)[:200])
finally:
client.close()