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(integrations): a delegation choice that names no agent is refused, not indexed

Aashishdd3e15b794b7
diff --git a/laya/integrations/agno.py b/laya/integrations/agno.py
index 1d8238d2..7c292811 100644
--- a/laya/integrations/agno.py
+++ b/laya/integrations/agno.py
@@ -584,6 +584,16 @@ class LayaAgnoRouter:
elif chosen_key in criteria:
chosen_idx = list(criteria.keys()).index(chosen_key)
+ # `chosen_key` is remote JSON and `agents` is the caller's list, so a two-agent team can be
+ # told to route to `agent_9`: the parsed number indexes no agent and `agents[chosen_idx]`
+ # below raised a bare IndexError out of the routing decision, while `agent_-1` silently
+ # wrapped to the LAST agent. `criteria` is built from exactly these agents, so an index it
+ # does not name is a candidate that does not exist -- the same "unknown decision" that
+ # `tools.py:493` refuses by name rather than raising.
+ if not 0 <= chosen_idx < len(agents):
+ raise _malformed(where, "decision %r names agent %d, which is not in the %d-agent team"
+ % (chosen_key, chosen_idx, len(agents)))
+
# Confidence gating (same logic as LayaCrewRouter).
if self.confidence_threshold > 0.0 and conf < self.confidence_threshold:
if self.fallback_agent_index is not None:
diff --git a/laya/integrations/crewai.py b/laya/integrations/crewai.py
index a8077f3c..cef77ab4 100644
--- a/laya/integrations/crewai.py
+++ b/laya/integrations/crewai.py
@@ -402,6 +402,16 @@ class LayaCrewRouter:
elif chosen_key in criteria:
chosen_idx = list(criteria.keys()).index(chosen_key)
+ # `chosen_key` is remote JSON and `agents` is the caller's list, so a two-agent crew can be
+ # told to delegate to `agent_9`: the parsed number indexes no agent and `agents[chosen_idx]`
+ # below raised a bare IndexError out of the routing decision, while `agent_-1` silently
+ # wrapped to the LAST agent. `criteria` is built from exactly these agents, so an index it
+ # does not name is a candidate that does not exist -- the same "unknown decision" that
+ # `tools.py:493` refuses by name rather than raising.
+ if not 0 <= chosen_idx < len(agents):
+ raise _malformed(where, "decision %r names agent %d, which is not in the %d-agent crew"
+ % (chosen_key, chosen_idx, len(agents)))
+
# Confidence gating
if self.confidence_threshold > 0.0 and conf < self.confidence_threshold:
if self.fallback_agent_index is not None:
diff --git a/laya/integrations/llamaindex.py b/laya/integrations/llamaindex.py
index 9feffe45..af1e5fa0 100644
--- a/laya/integrations/llamaindex.py
+++ b/laya/integrations/llamaindex.py
@@ -392,6 +392,16 @@ class LayaSingleSelector(BaseSelector):
elif chosen_key in criteria:
chosen_idx = list(criteria.keys()).index(chosen_key)
+ # `chosen_key` is remote JSON and `choices` is the caller's list, so two choices can be
+ # told to select `choice_9`: the parsed number indexes no choice and `choices[chosen_idx]`
+ # below raised a bare IndexError out of the selection, while `choice_-1` silently wrapped to
+ # the LAST one. `criteria` is built from exactly these choices, so an index it does not name
+ # is a candidate that does not exist -- `LayaQueryRouter.route` already treats a name outside
+ # `self.query_engines` that way, and `tools.py:493` refuses it by name.
+ if not 0 <= chosen_idx < len(choices):
+ raise _malformed(where, "decision %r names choice %d, which is not among the %d choices"
+ % (chosen_key, chosen_idx, len(choices)))
+
# Confidence gating
if self.confidence_threshold > 0.0 and conf < self.confidence_threshold:
if self.fallback_index is not None:
diff --git a/tests/test_agno.py b/tests/test_agno.py
index 6b812f43..cf7b0586 100644
--- a/tests/test_agno.py
+++ b/tests/test_agno.py
@@ -891,6 +891,37 @@ for _body, _label in _AGNO_NESTED_ROUTE:
except Exception as exc:
check_true("nested/%s/route is RuntimeError" % _label, False, type(exc).__name__)
+# A `choice` may name an index the team does not have. `chosen_key` is remote JSON and `agents` is
+# the caller's list, and `agent_N` was parsed straight into `agents[N]`: `agent_99` raised a bare
+# IndexError out of the routing decision and `agent_-1` wrapped to the LAST agent -- a route no
+# caller asked for and nothing in the decision names. `tools.py:493` answers an unknown decision by
+# name instead of raising, so the index range is checked the same way here.
+_AGNO_CHOICE_INDEX = ["agent_5", "agent_99", "agent_-1", "agent_-99"]
+for _choice in _AGNO_CHOICE_INDEX:
+ with patch("urllib.request.build_opener") as _m:
+ _m.return_value.open.return_value = DummyHTTPResponse(
+ {"answers": {"delegation": {"choice": _choice, "answer_confidence": 0.95}}})
+ try:
+ _d = LayaAgnoRouter(base_url="http://localhost:8000").route(
+ "Write a Python function to reverse a string", agents)
+ check_true("choice-index/%s/route raises" % _choice, False,
+ "no error raised (routed to index %r)" % (_d.agent_index,))
+ except RuntimeError as exc:
+ check_true("choice-index/%s/route malformed" % _choice,
+ "Malformed decision response" in str(exc))
+ check_true("choice-index/%s/route names the choice" % _choice, _choice in str(exc))
+ check_true("choice-index/%s/route names the team size" % _choice, "5-agent" in str(exc))
+ except Exception as exc:
+ check_true("choice-index/%s/route is RuntimeError" % _choice, False, type(exc).__name__)
+
+# The last real index still routes: the guard is a range check, not a rejection of the highest one.
+with patch("urllib.request.build_opener") as _m:
+ _m.return_value.open.return_value = DummyHTTPResponse(
+ {"answers": {"delegation": {"choice": "agent_4", "answer_confidence": 0.95}}})
+ check("choice-index/last agent still routes",
+ LayaAgnoRouter(base_url="http://localhost:8000").route(
+ "Write a Python function to reverse a string", agents).agent_index, 4)
+
_AGNO_NESTED_SCREEN = [
({"answers": []}, "answers-list"),
({"answers": {"harm_severity": "nope"}}, "answer-str"),
diff --git a/tests/test_crewai.py b/tests/test_crewai.py
index c8b2a3a2..176b7014 100644
--- a/tests/test_crewai.py
+++ b/tests/test_crewai.py
@@ -908,6 +908,40 @@ for _body, _label in _CREWAI_NESTED_ROUTE:
except Exception as exc:
check_true("nested/%s/route is RuntimeError" % _label, False, type(exc).__name__)
+# A `choice` may name an index the crew does not have. `chosen_key` is remote JSON and `agents` is
+# the caller's list, and the code parsed `agent_N` straight into `agents[N]`: `agent_9` raised a
+# bare IndexError out of the routing decision, and `agent_-1` wrapped to the LAST agent -- a route
+# no caller asked for and nothing in the decision names. `tools.py:493` answers an unknown decision
+# by name instead of raising, so the index range is checked the same way here.
+_CREWAI_CHOICE_INDEX = [
+ "agent_4", # one past the end of the four-agent crew
+ "agent_9", # far past the end
+ "agent_-1", # would wrap to the last agent
+ "agent_-99", # far before the start
+]
+for _choice in _CREWAI_CHOICE_INDEX:
+ with patch("urllib.request.build_opener") as _m:
+ _m.return_value.open.return_value = DummyHTTPResponse(
+ {"answers": {"delegation": {"choice": _choice, "answer_confidence": 0.95}}})
+ try:
+ _d = LayaCrewRouter(base_url="http://localhost:8000").route("Analyze codebase", agents)
+ check_true("choice-index/%s/route raises" % _choice, False,
+ "no error raised (routed to index %r)" % (_d.agent_index,))
+ except RuntimeError as exc:
+ check_true("choice-index/%s/route malformed" % _choice,
+ "Malformed decision response" in str(exc))
+ check_true("choice-index/%s/route names the choice" % _choice, _choice in str(exc))
+ check_true("choice-index/%s/route names the crew size" % _choice, "4-agent" in str(exc))
+ except Exception as exc:
+ check_true("choice-index/%s/route is RuntimeError" % _choice, False, type(exc).__name__)
+
+# The last real index still routes: the guard is a range check, not a rejection of the highest one.
+with patch("urllib.request.build_opener") as _m:
+ _m.return_value.open.return_value = DummyHTTPResponse(
+ {"answers": {"delegation": {"choice": "agent_3", "answer_confidence": 0.95}}})
+ check("choice-index/last agent still routes",
+ LayaCrewRouter(base_url="http://localhost:8000").route("Analyze codebase", agents).agent_index, 3)
+
_CREWAI_NESTED_SCREEN = [
({"answers": []}, "answers-list"),
({"answers": {"harm_severity": "nope"}}, "answer-str"),
diff --git a/tests/test_llamaindex.py b/tests/test_llamaindex.py
index ffbefd51..b7d30afe 100644
--- a/tests/test_llamaindex.py
+++ b/tests/test_llamaindex.py
@@ -919,6 +919,37 @@ for _body, _surface, _label in _LLAMA_NESTED:
except Exception as exc:
check_true("nested/%s is RuntimeError" % _label, False, type(exc).__name__)
+# A `choice` may name an index the caller's list does not have. `chosen_key` is remote JSON and
+# `choices` is the caller's, and `choice_N` was parsed straight into `choices[N]`: `choice_9` raised
+# a bare IndexError out of the selection and `choice_-1` wrapped to the LAST tool -- a selection no
+# caller asked for and nothing in the decision names. `LayaQueryRouter.route` in this module already
+# treats a name outside its engines as no match, so the single selector now checks the range too.
+_LLAMA_CHOICE_INDEX = ["choice_4", "choice_9", "choice_-1", "choice_-99"]
+for _choice in _LLAMA_CHOICE_INDEX:
+ with patch("urllib.request.build_opener") as _m:
+ _m.return_value.open.return_value = DummyHTTPResponse(
+ {"answers": {"selector": {"choice": _choice, "answer_confidence": 0.95}}})
+ try:
+ _r = LayaSingleSelector(base_url="http://localhost:8000").select(
+ tools, "Query via remote server")
+ check_true("choice-index/%s/select raises" % _choice, False,
+ "no error raised (selected index %r)" % (_r.selections[0].index,))
+ except RuntimeError as exc:
+ check_true("choice-index/%s/select malformed" % _choice,
+ "Malformed decision response" in str(exc))
+ check_true("choice-index/%s/select names the choice" % _choice, _choice in str(exc))
+ check_true("choice-index/%s/select names the count" % _choice, "4 choices" in str(exc))
+ except Exception as exc:
+ check_true("choice-index/%s/select is RuntimeError" % _choice, False, type(exc).__name__)
+
+# The last real index still selects: the guard is a range check, not a rejection of the highest one.
+with patch("urllib.request.build_opener") as _m:
+ _m.return_value.open.return_value = DummyHTTPResponse(
+ {"answers": {"selector": {"choice": "choice_3", "answer_confidence": 0.95}}})
+ check("choice-index/last choice still selects",
+ LayaSingleSelector(base_url="http://localhost:8000").select(
+ tools, "Query via remote server").selections[0].index, 3)
+
print(f"FAIL: {len(FAIL)}")
for f in FAIL:
print(f" - {f}")