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): refuse a bool hooks_timeout instead of reading it as 1.0

Aashishae132f47a75e
diff --git a/docs/hooks/errors.md b/docs/hooks/errors.md
index c84c5064..896610e8 100644
--- a/docs/hooks/errors.md
+++ b/docs/hooks/errors.md
@@ -162,11 +162,14 @@ It can be set per instance or overridden per call on every public per-call surfa
agents: `Agent.predict_batch`, `Agent.system_one`, `Agent.predict_long`, `Router.route`,
`Router.route_batch`, `Router.predict`, `Router.predict_batch`, `Router.predict_long`,
`ONNXAgent.system_one`, `ONNXAgent.predict_batch` and `ONNXAgent.predict_long`. The value must be a
-positive finite number; `0`, a negative number, `inf` and `nan` each raise `ValueError` at the point
-it is set, rather than racing on a zero-length `join` -- passed straight to the underlying thread
-`join`, `nan` would raise `ValueError` and an infinite value `OverflowError`, so neither bounds the
-wait the way a limit is meant to. `None` is how to ask for no limit, not an arbitrarily large
-number.
+positive finite number; `0`, a negative number, `inf`, `nan` and a bool each raise `ValueError` at
+the point it is set, rather than racing on a zero-length `join` -- passed straight to the underlying
+thread `join`, `nan` would raise `ValueError` and an infinite value `OverflowError`, so neither
+bounds the wait the way a limit is meant to. A bool is refused for its own reason: `float(True)` is
+`1.0`, so `hooks_timeout=True` would otherwise install a one-second deadline the caller never asked
+for, and `False` would be refused as a non-positive number rather than as a wrong type. Its message
+says `not a bool`, which is the only way a caller can tell the two refusals apart. `None` is how to
+ask for no limit, not an arbitrarily large number.
A timed hook runs on a worker thread in a copy of the caller's `contextvars` context, so a
request id or tracing span set by the caller is visible to the hook.
diff --git a/laya/hooks.py b/laya/hooks.py
index b680808c..09e10c2f 100644
--- a/laya/hooks.py
+++ b/laya/hooks.py
@@ -250,9 +250,18 @@ def validate_timeout(value: Optional[float]) -> Optional[float]:
started, so the outcome of a fast hook with such a value is a race.
Non-finite values are rejected too: ``join(nan)`` raises ``ValueError`` and
``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.
"""
if value is None:
return None
+ if isinstance(value, bool):
+ # `False` would be refused below as a non-positive number, which is the wrong reason for a
+ # wrong type; `True` was accepted as 1.0. The message names the type so a caller reading it
+ # learns what they passed, not just that it was too small. `laya/mcp/tools.py` refuses bools
+ # 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)
if not math.isfinite(timeout) or timeout <= 0:
raise ValueError("hooks_timeout must be a positive finite number or None; got %r" % (value,))
diff --git a/laya/mcp/tools.py b/laya/mcp/tools.py
index d079a58d..a757c11d 100644
--- a/laya/mcp/tools.py
+++ b/laya/mcp/tools.py
@@ -1196,9 +1196,10 @@ def _validate_hooks_timeout(value: Any) -> float | None:
# ``validate_timeout`` decides what counts as a real deadline. Wrapping
# its ValueError as a ToolError keeps a bad arg from surfacing as
# ``internal_error: ValueError`` at the MCP boundary. Bools are refused
- # here the same way _validate_batch_size refuses them -- core's
- # float(True) == 1.0 would silently turn "True" into a one-second
- # deadline, which is the kind of wrong-that-needs-to-shout.
+ # here the same way _validate_batch_size refuses them, so a wrong type
+ # reaches the caller as `invalid_hooks_timeout` instead of whichever of
+ # core's two refusals happens to fire. Core refuses a bool as well now:
+ # float(True) == 1.0 is no longer silently read as a one-second deadline.
if value is None:
return None
if isinstance(value, bool):
diff --git a/tests/test_hooks.py b/tests/test_hooks.py
index d04015db..9a634cef 100644
--- a/tests/test_hooks.py
+++ b/tests/test_hooks.py
@@ -1767,6 +1767,28 @@ check_raises("timeout/negative is rejected", ValueError, lambda: validate_timeou
check_raises("timeout/NaN is rejected", ValueError, lambda: validate_timeout(float("nan")))
check_raises("timeout/infinity is rejected", ValueError, lambda: validate_timeout(float("inf")))
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))
+
+
+# The refusal has to say *why*. `False` raises either way -- as a wrong type here, as a non-positive
+# number in the branch below -- so an arm that only checks the exception type cannot tell the two
+# apart, and a guard that simply fell through would pass it. `0` keeps the original wording.
+def _timeout_message(value):
+ try:
+ validate_timeout(value)
+ except ValueError as exc:
+ return str(exc)
+ return "<accepted>"
+
+
+check_true("timeout/a bool is refused as a bool", "not a bool" in _timeout_message(True),
+ _timeout_message(True))
+check_true("timeout/False is refused as a bool too", "not a bool" in _timeout_message(False),
+ _timeout_message(False))
+check_true("timeout/zero keeps the non-positive wording",
+ "not a bool" not in _timeout_message(0) and "positive finite" in _timeout_message(0),
+ _timeout_message(0))
check("timeout/positive passes through", validate_timeout(1.5), 1.5)
check("timeout/None means no limit", validate_timeout(None), None)
@@ -1778,6 +1800,17 @@ check_raises("timeout/NaN per call is rejected", ValueError,
check_raises("timeout/infinity per call is rejected", ValueError,
lambda: f.predict_batch(["s0"], QUESTIONS, hooks_timeout=float("inf")))
+# `float(True) == 1.0`, so `hooks_timeout=True` -- which reads like "yes, time the hooks" -- used to
+# install a one-second deadline on every hook call, and `False` was refused as a non-positive number,
+# the wrong reason for a wrong type. Both spellings now raise where they are set, per call and at
+# construction alike; an int and a numeric string are not bools and still bound the wait.
+check_raises("timeout/True per call is rejected", ValueError,
+ lambda: f.predict_batch(["s0"], QUESTIONS, hooks_timeout=True))
+check_raises("timeout/True at construction is rejected", ValueError,
+ lambda: Router(hooks_timeout=True))
+check("timeout/int still bounds", Router(hooks_timeout=1).hooks_timeout, 1.0)
+check("timeout/numeric string still bounds", Router(hooks_timeout="0.5").hooks_timeout, 0.5)
+
not_running = asyncio.new_event_loop()
try:
c = _seven()
diff --git a/tests/test_mcp.py b/tests/test_mcp.py
index 60bca110..f48ed258 100644
--- a/tests/test_mcp.py
+++ b/tests/test_mcp.py
@@ -1358,8 +1358,9 @@ def test_batch_predict():
"min_confidence" not in kwargs and "sort_by_length" not in kwargs, repr(kwargs))
# Call-level controls are validated up front like every sibling tool: a bad value is a clean
- # ToolError, not an internal_error that escaped the TypeError-only handler, and not a silent
- # 1-second hook deadline from core's float(True) == 1.0.
+ # ToolError, not an internal_error that escaped the TypeError-only handler. A bool is refused
+ # here before core sees it, so the caller hears `invalid_hooks_timeout` rather than the core
+ # refusal `laya/hooks.py::validate_timeout` raises for the same mistake.
expect_tool_error("batch/min_confidence_out_of_range",
lambda: laya_predict_batch(BATCH_REQUESTS, min_confidence=1.5, router=BatchRouter()),
"invalid_min_confidence")