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(tools): fail closed on a non-object remote decision response

Aashish0410017684f6
diff --git a/laya/integrations/tools.py b/laya/integrations/tools.py
index bc30df78..7f4305bd 100644
--- a/laya/integrations/tools.py
+++ b/laya/integrations/tools.py
@@ -347,7 +347,12 @@ class LayaToolSelector:
hooks_timeout=self.hooks_timeout,
)
- answers_dict = result.get("answers")
+ # A remote laya-serve body is arbitrary JSON: a gateway or mis-pointed URL can answer
+ # with a top-level array, string or number, where `.get` would raise AttributeError and
+ # defeat the fail-closed branch below -- including the fallback_tool path meant to absorb
+ # exactly this malformed response. Read `answers` only from an object; anything else is
+ # the same "missing or malformed decision response" the guard already handles.
+ answers_dict = result.get("answers") if isinstance(result, dict) else None
if not isinstance(answers_dict, dict) or "tool_select" not in answers_dict:
if self.fallback_tool is not None:
fb_name, _ = _extract_tool_metadata(self.fallback_tool, -1)
diff --git a/tests/test_tools.py b/tests/test_tools.py
index dbdb1cf1..3cc7ef1f 100644
--- a/tests/test_tools.py
+++ b/tests/test_tools.py
@@ -353,6 +353,73 @@ finally:
laya.integrations.tools.urllib.request.build_opener = original_build_opener
+# --------------------------------------------------------------- 8b. Non-Object Remote Response
+# A remote body is arbitrary JSON: a gateway or a mis-pointed URL can answer with a top-level
+# array/string/number/null. select() must fail closed (RuntimeError, or the fallback tool) like a
+# malformed object does, not leak AttributeError from `result.get`.
+NON_OBJECT_BODIES = ['[]', '[{"answers": {"tool_select": {"choice": "tool_0"}}}]',
+ '"unauthorized"', 'null', '123', 'true']
+
+
+class _BodyResponse:
+ def __init__(self, raw):
+ self._raw = raw.encode("utf-8")
+
+ def read(self):
+ return self._raw
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ pass
+
+
+class _BodyHandler:
+ def __init__(self, raw):
+ self._raw = raw
+
+ def open(self, req, timeout=10.0):
+ return _BodyResponse(self._raw)
+
+
+_original_build_opener_8b = laya.integrations.tools.urllib.request.build_opener
+nonobj_fallback = lambda *a, **k: "general_llm_fallback"
+try:
+ for _raw in NON_OBJECT_BODIES:
+ laya.integrations.tools.urllib.request.build_opener = (
+ lambda *a, _r=_raw: _BodyHandler(_r))
+ # no fallback: must raise RuntimeError, never AttributeError
+ _sel = LayaToolSelector(tools=tools_list, base_url="http://localhost:8000")
+ try:
+ _sel.select("Query")
+ FAIL.append(f"nonobj/raises_{_raw}: expected RuntimeError")
+ except RuntimeError as _e:
+ check_true(f"nonobj/runtimeerror_{_raw}",
+ "Malformed or missing decision response" in str(_e))
+ except AttributeError as _e:
+ FAIL.append(f"nonobj/attriberror_{_raw}: leaked AttributeError ({_e})")
+ # with fallback: must route to the fallback tool, never raise
+ _sel_fb = LayaToolSelector(tools=tools_list, base_url="http://localhost:8000",
+ fallback_tool=nonobj_fallback)
+ try:
+ _dec = _sel_fb.select("Query")
+ check(f"nonobj/fallback_{_raw}", _dec.is_fallback, True)
+ except AttributeError as _e:
+ FAIL.append(f"nonobj/fallback_{_raw}: leaked AttributeError ({_e})")
+finally:
+ laya.integrations.tools.urllib.request.build_opener = _original_build_opener_8b
+
+# A well-formed object response must still route normally through the same harness.
+laya.integrations.tools.urllib.request.build_opener = lambda *a: _BodyHandler(
+ json.dumps(make_mock_response("tool_0", confidence=0.93)))
+try:
+ _ok = LayaToolSelector(tools=tools_list, base_url="http://localhost:8000").select("Query")
+ check("nonobj/object_still_routes", _ok.tool_name, "web_search")
+finally:
+ laya.integrations.tools.urllib.request.build_opener = _original_build_opener_8b
+
+
# --------------------------------------------------------------- 9. Controls Signatures Check
params = inspect.signature(LayaToolSelector.__init__).parameters
for ctrl in PREDICT_CONTROLS: