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(hooks): reject a non-numeric or overflowing hooks_timeout as a ValueError

Aashish5114f4263a8c
diff --git a/laya/hooks.py b/laya/hooks.py
index 81715f62..c200d361 100644
--- a/laya/hooks.py
+++ b/laya/hooks.py
@@ -258,6 +258,8 @@ def validate_timeout(value: Optional[float]) -> Optional[float]:
``join(inf)`` raises ``OverflowError`` instead of waiting.
A bool is rejected as well: ``float(True)`` is ``1.0``, so ``hooks_timeout=True`` would
otherwise install a one-second deadline on every hook call that the caller never asked for.
+ A value ``float()`` cannot read -- a list/object, or a JSON integer too large for a float -- is
+ rejected with the same ``ValueError`` rather than leaking ``TypeError``/``OverflowError``.
"""
if value is None:
return None
@@ -268,7 +270,16 @@ def validate_timeout(value: Optional[float]) -> Optional[float]:
# first, so an MCP caller still hears `invalid_hooks_timeout` rather than this ValueError.
raise ValueError(
"hooks_timeout must be a positive finite number or None, not a bool; got %r" % (value,))
- timeout = float(value)
+ try:
+ timeout = float(value)
+ except (TypeError, OverflowError):
+ # A list/object is not a number and a JSON integer larger than a float overflows `float()`;
+ # both used to escape as a raw TypeError/OverflowError from the coercion below. `OverflowError`
+ # in particular is a valid-JSON number that reached `laya/mcp/tools.py::_validate_hooks_timeout`,
+ # which catches only (TypeError, ValueError), so it surfaced as `internal_error`. Refuse both
+ # shapes here with the same ValueError as every other bad timeout.
+ raise ValueError(
+ "hooks_timeout must be a positive finite number or None; got %r" % (value,)) from None
if not math.isfinite(timeout) or timeout <= 0:
raise ValueError("hooks_timeout must be a positive finite number or None; got %r" % (value,))
return timeout
diff --git a/tests/test_hooks.py b/tests/test_hooks.py
index 2fb33f7a..1ac99ebf 100644
--- a/tests/test_hooks.py
+++ b/tests/test_hooks.py
@@ -1771,6 +1771,13 @@ check_raises("timeout/infinity is rejected", ValueError, lambda: validate_timeou
check_raises("timeout/-infinity is rejected", ValueError, lambda: validate_timeout(float("-inf")))
check_raises("timeout/True is rejected", ValueError, lambda: validate_timeout(True))
check_raises("timeout/False is rejected", ValueError, lambda: validate_timeout(False))
+# A shape `float()` cannot read is still a bad timeout: a list/object used to leak TypeError and a
+# JSON integer too large for a float leaked OverflowError. Both must raise the same ValueError, and
+# the OverflowError case is what let a giant JSON number reach `laya/mcp/tools.py` (which catches
+# only TypeError/ValueError) and surface as `internal_error`.
+check_raises("timeout/list is rejected", ValueError, lambda: validate_timeout([10]))
+check_raises("timeout/dict is rejected", ValueError, lambda: validate_timeout({"a": 1}))
+check_raises("timeout/oversized int is rejected", ValueError, lambda: validate_timeout(10 ** 1000))
# The refusal has to say *why*. `False` raises either way -- as a wrong type here, as a non-positive
diff --git a/tests/test_mcp.py b/tests/test_mcp.py
index c5e4f6a5..65282847 100644
--- a/tests/test_mcp.py
+++ b/tests/test_mcp.py
@@ -1368,6 +1368,16 @@ def test_batch_predict():
expect_tool_error("batch/hooks_timeout_bool_rejected",
lambda: laya_predict_batch(BATCH_REQUESTS, hooks_timeout=True, router=BatchRouter()),
"invalid_hooks_timeout")
+ # A list and a JSON integer too large for a float are also bad deadlines. `_validate_hooks_timeout`
+ # catches only (TypeError, ValueError), so before core refused them as a ValueError the giant int
+ # escaped as an `OverflowError` and surfaced as `internal_error` -- the failure this handler exists
+ # to prevent. Both must now come back as `invalid_hooks_timeout`.
+ expect_tool_error("batch/hooks_timeout_list_rejected",
+ lambda: laya_predict_batch(BATCH_REQUESTS, hooks_timeout=[10], router=BatchRouter()),
+ "invalid_hooks_timeout")
+ expect_tool_error("batch/hooks_timeout_oversized_int_rejected",
+ lambda: laya_predict_batch(BATCH_REQUESTS, hooks_timeout=10 ** 1000, router=BatchRouter()),
+ "invalid_hooks_timeout")
# min_confidence=0.0 is a real gate (abstain over nothing) and must still be forwarded.
router = BatchRouter()
laya_predict_batch(BATCH_REQUESTS, min_confidence=0.0, router=router)