Skip to content

fix(hooks): refuse a bool hooks_timeout instead of reading it as 1.0 - #1203

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:fix-hooks-api-timeout
Oct 17, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:fix-hooks-api-timeout

Conversation

@aashish254

@aashish254 aashish254 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

What

laya/hooks.py::validate_timeout refuses a bool.

  • hooks_timeout=True used to answer 1.0. It now raises
    ValueError: hooks_timeout must be a positive finite number or None, not a bool; got True
    at the point it is set — per call and at construction alike.
  • hooks_timeout=False used to raise ... positive finite number or None; got False, i.e. it was
    refused as a number that is too small rather than as a wrong type. It now raises the bool refusal,
    and the not a bool in the message is the only thing that tells a caller which of the two they hit.
  • 0, a negative number, inf, nan, an int and a numeric string all behave exactly as before.

Three surfaces describe or depend on that guard, so they move with it:

  • docs/hooks/errors.md — the paragraph listing what is refused at the point the timeout is set gains
    the bool and the reason it is refused for its own reason.
  • laya/mcp/tools.py — the comment above _validate_hooks_timeout said bools are refused there
    because core's float(True) == 1.0 would silently turn True into a one-second deadline. That
    asymmetry was the bug; the comment now says the boundary refuses first so the caller hears
    invalid_hooks_timeout, and core refuses too.
  • tests/test_mcp.py — the note above the batch-control arms made the same claim; corrected the same way.

Nine arms in tests/test_hooks.py (the timeout/* block): both bools rejected, the message says
not a bool for both, 0 keeps the non-positive wording, True rejected per call on
predict_batch and at Router construction, and an int / numeric string still bound the wait.

Python-only. A bool has no counterpart in the Java, Go or TypeScript ports — they are typed at the
parameter, so hooks_timeout=true is a compile error there — so no fixture, oracle or parity surface moves.

Why

bool is a subclass of int, and float(True) is 1.0, so hooks_timeout=True — which reads as
"yes, please time the hooks" — was read as "one second". Every hook call in that
request then got a one-second deadline the caller never asked for, and a hook that takes longer than
that turns a correct answer into a LayaHookTimeoutError (or a silently skipped hook, depending on
hooks_raise). The value is also inherited by every call on the instance when set at construction,
so the mistake is not local to one request.

Measured at the base of this PR (4ac226d5, upstream main, release 0.4.8) in a clean worktree:

validate_timeout(True)  -> 1.0                       # the defect: accepted, and installs 1.0
validate_timeout(False) -> ValueError: hooks_timeout must be a positive finite number or None; got False
validate_timeout(0)     -> ValueError: ... or None; got 0

Measured on this branch:

validate_timeout(True)  -> ValueError: hooks_timeout must be a positive finite number or None, not a bool; got True
validate_timeout(False) -> ValueError: ... not a bool; got False
validate_timeout(0)     -> ValueError: hooks_timeout must be a positive finite number or None; got 0   # unchanged
validate_timeout(1)     -> 1.0        validate_timeout('0.5') -> 0.5                                   # unchanged
Router(hooks_timeout=True) -> ValueError: ... not a bool; got True

The MCP boundary already refused bools (laya/mcp/tools.py::_validate_hooks_timeout, and
_validate_batch_size refuses them the same way), so this closes a gap the repo had already
agreed is a bug — just one layer down, where the non-MCP callers are. Verified unchanged after the
guard lands: _validate_hooks_timeout(True) still raises ToolError with the boundary's own text,
so no MCP caller sees a different error than before.

On this branch's history. #1203 previously carried a TypeScript option_order commit built on the
fork's main, which sits at release 0.4.6 and is 187 commits behind upstream — so the PR showed a
change upstream had already merged as e894d167, and none of the change its title promised. Per the
review on this PR, the branch is now a fresh branch off upstream main: HEAD^ is 4ac226d5 release: 0.4.8, git merge-base --is-ancestor refs/remotes/upstream/main HEAD holds, and the diff is the five
files above (+57 / −10, 1 commit). Nothing from the old head was kept — that change is upstream already.
The branch name fix-hooks-api-timeout predates that rebuild; the content is the guard above.

Still unfixed at the base. git show 4ac226d5:laya/hooks.py has no bool branch in
validate_timeout, and the probe above is run against that commit in a clean worktree. The only
bool-refusing guard in the tree is the MCP boundary, which is why merged #857 and #1118 are related but
not this: they validate at the boundary, this fixes the layer every non-MCP caller goes through.

How it was verified

PY=/Users/aashish/.subproto/laya-venv/bin/python   # 3.11.15, numpy 1.26.4, offline
TREE=/Users/aashish/github contribution/GIT/laya-1203
CTRL=/Users/aashish/github contribution/GIT/laya-ctrl-4323   # detached at 4ac226d5, the clean control
export HF_HUB_OFFLINE=1 USE_TF=0 USE_TORCH=1 TOKENIZERS_PARALLELISM=false NO_COLOR=1

# 1. the guard, control then branch, each with PYTHONPATH set to the named tree (tables in "Why")
PYTHONPATH=$CTRL  $PY -B -c 'from laya.hooks import validate_timeout; ...'   # -> 1.0 at the base
PYTHONPATH=$TREE  $PY -B -c 'from laya.hooks import validate_timeout; ...'   # -> the refusal
# 2. the suites
PYTHONPATH=$TREE $PY -B tests/test_hooks.py
PYTHONPATH=$TREE $PY -B tests/test_hooks_api.py
PYTHONPATH=$TREE $PY -B tests/test_mcp.py
# 3. the repo's own gate list, replayed against each tree
LAYA_TREE=$TREE  $PY -B "…/laya-bench/ci_gate.py"    # scripts/test_suites.py, 78 invocations
LAYA_TREE=$CTRL  $PY -B "…/laya-bench/ci_gate.py"
# 4. lint, compile, fixtures, docs
ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120
ruff check tests/test_hooks.py tests/test_mcp.py --select=E9,F63,F7,F82,F401,F811 --line-length=120
python -m compileall -q laya/ tests/
$PY -B laya-java/scripts/gen_fixtures.py --check     # both trees
zensical build --strict --clean                       # the CI `docs (strict build)` job

What they showed:

check control 4ac226d5 (0.4.8, no change) this branch
tests/test_hooks.py 285 passed, 0 failed 294 passed, 0 failed (+9 arms)
tests/test_hooks_api.py (untouched; it executes errors.md's hooks_timeout surface list) 598 passed, 0 failed 598 passed, 0 failed
ci_gate (scripts/test_suites.py) invocations 78 78
ci_gate pytest lane 3 failed, 473 passed, 3 skipped 3 failed, 473 passed, 3 skipped — identical counts
ci_gate invocations that failed the same two: test_langchain_real_mode.py (no langchain-core) and the pytest lane's three test_compile.py inductor cases (no CUDA) the same two, the same three test ids — both red at 4ac226d5 for want of the extra and a GPU
per-invocation result diff, ctrl vs branch, paths normalised — exactly one of 78 suites changed: tests/test_hooks.py 285 → 294 passed, 0 failed both. All other 77 identical.
tests/test_mcp.py (comment-only edits here) SKIP: mcp extra not installed SKIP: mcp extra not installed (same line, so the gate's answer is unchanged)
ruff (both commands), compileall — all pass, rc 0
gen_fixtures.py --check rc 1: lang_detect.json, python_json.json stale rc 1, the same two files, output identical to the control except for the tree path in one pre-existing RuntimeWarning
zensical build --strict --clean — No issues found, 1.68 s; the built site/hooks/errors/index.html contains the rendered not a bool sentence, read back rather than assumed
secrets scan of git diff — no key/token/credential-shaped line, no .env, no private key

Mutation ledger (laya-bench/mutate_hooks_bool.py, run against tests/test_hooks.py; laya/hooks.py
restored byte-for-byte after each mutant, sha256 74387e07bba7f1a1… verified equal at the end; baseline
green at 294/0. Re-run after the rebase onto 4ac226d5 — same 5/5, same restore hash, so the edited
region of laya/hooks.py did not move between the two bases):

mutant expected got died at
m1 bool branch deleted KILL KILL, 5 arms timeout/True is rejected, …/a bool is refused as a bool, …/False is refused as a bool too, …/True per call is rejected, …/True at construction is rejected
m2 message drops not a bool KILL KILL, 2 arms the two message arms — a type-only check cannot tell the refusals apart
m3 branch raises TypeError KILL KILL via abort tests/test_hooks.py:1779 in _timeout_message: a wrong type escapes the except ValueError and the suite dies loudly rather than silently
m4 type(value) is bool instead of isinstance SURVIVE SURVIVE nothing can subclass bool in CPython, so this is the same guard written differently — reported as an equivalence, not a missing test
m5 branch moved below the non-positive check KILL KILL, 1 arm timeout/False is refused as a bool too — False is then refused as a number that is too small, so the ordering is pinned, not incidental

5 mutants, 5 as predicted, 0 mismatched.

Deconfliction by hunk range (gh pr diff, +-side ranges), because other open PRs share these files:

Checklist

  • Focused on one change (split unrelated work into another PR)
  • Rebased on the latest main
  • Tests pass locally
  • Docs or examples updated when the public API changed

Fixes #

@NandhaKishorM

Copy link
Copy Markdown
Owner

Not merged, because the branch does not carry the change this PR describes.

The description is about the hooks timeout rejection message, and fix-hooks-api-timeout holds the option_order port instead: laya-ts/src/agent.ts, laya-ts/src/common.ts, laya-ts/src/shortlist.ts and laya-ts/tests/option-order.test.ts, +515 / -27. That is byte-for-byte the diff on #1196, which I merged in 0.4.7.

So nothing here is lost, but nothing here is the timeout message either. If you still want that change, the quickest path is a fresh branch off origin/main, since laya-ts/src/agent.ts has moved under it twice since this branch was cut (#1195 and #1196).

One request for the next one: the checklist says "tests pass before opening" and "no PR overlap". Both were checked against a tree that holds someone else's change, so the boxes could not have been ticked from this branch. A git log --oneline origin/main..HEAD before opening would have shown it.

@aashish254 aashish254 changed the title fix(hooks): say "not a bool" so the two refusals are told apart feat(ts): honour a question's option_order in encoding and decoding Oct 15, 2026
`float(True) == 1.0`, so `hooks_timeout=True` -- which reads as "yes, time the
hooks" -- silently installed a one-second deadline on every hook call. `False`
raised, but as a non-positive number: the wrong reason for a wrong type. Core
now refuses a bool where it is set, with a message that says `not a bool` so
the two refusals stay distinguishable; a numeric int or string still bounds.

`laya/mcp/tools.py` already refused bools at the MCP boundary, so its comment
and the matching `tests/test_mcp.py` note are corrected to say the guard now
also exists underneath them. `docs/hooks/errors.md` gains the bool to the list
of values refused at the point the timeout is set.

Python-only: a bool has no counterpart in the typed ports, so no Java/Go/TS
fixture or parity surface moves.
@aashish254
aashish254 force-pushed the fix-hooks-api-timeout branch from 5d04039 to ae132f4 Compare October 15, 2026 11:10
@aashish254 aashish254 changed the title feat(ts): honour a question's option_order in encoding and decoding fix(hooks): refuse a bool hooks_timeout instead of reading it as 1.0 Oct 15, 2026
@NandhaKishorM
NandhaKishorM merged commit d13bd42 into NandhaKishorM:main Oct 17, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants