Repository navigation
fix(mcp): send an out-of-range batch control to the validator instead of dropping it - #1118
Conversation
… of dropping it laya_predict_batch_tool collapsed every `hooks_timeout < 0` to None and every `min_confidence < 0` to None before calling the tools layer, so an invalid value became "unset": the client got a clean response and no way to tell the deadline or the abstention gate it asked for had been dropped. laya_predict_batch owns validation for both -- `_validate_hooks_timeout` and `validate_min_confidence` raise a ToolError naming the value -- and the above-range side already reached it (1.5 is refused), so only the below-range side was silently swallowed. hooks_timeout now uses the sibling tool's spelling, `or None`, which folds the eager-client 0 into "unset" and leaves anything else alone. min_confidence uses None as its unset marker, like every other tool in the file, and is forwarded verbatim. -1 was never a valid threshold (the range is [0, 1]) and the tools layer already refuses it, so the declaration default was the only thing reading it as "unset". Verified: tests/test_mcp.py gains test_batch_tool_control_forwarding(), which fails 4 checks without this change and passes with it (968 passed, 0 failed); tests/test_mcp_remote.py 53 passed; ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120 All checks passed. Refs NandhaKishorM#707
There was a problem hiding this comment.
🟡 Changes recommended
Boolean timeout handling remains silently dropped, and the required public API contract test is missing.
2 open findings
What changed in this PR
Fixes MCP batch controls so invalid negative values reach downstream validation.
Changes:
- Uses
Noneas the unsetmin_confidencedefault. - Adds forwarding and rejection regression tests.
| File | Description |
|---|---|
laya/mcp/server.py |
Updates batch-control forwarding. |
tests/test_mcp.py |
Tests defaults, valid values, and negative values. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| ) | ||
| def laya_predict_batch_tool(requests: list, batch_size: int = 0, hooks_timeout: float = 0, | ||
| min_confidence: float = -1, sort_by_length: bool = False) -> str: | ||
| min_confidence: float | None = None, |
There was a problem hiding this comment.
You're right — AGENTS.md requires tests/test_hooks_api.py to move with a public API change, and I changed the advertised schema without touching it. Fixed in 32d262b.
tests/test_hooks_api.py now pins the tool's controls and their defaults:
check_param("laya_predict_batch_tool", _mcp_batch_tool, "batch_size", 0)
check_param("laya_predict_batch_tool", _mcp_batch_tool, "hooks_timeout", 0)
check_param("laya_predict_batch_tool", _mcp_batch_tool, "min_confidence", None)
check_param("laya_predict_batch_tool", _mcp_batch_tool, "sort_by_length", False)The import is guarded (except ImportError: _mcp_batch_tool = None) so a core install without the mcp extra still runs the suite, matching how tests/test_mcp.py skips.
I also ran the second gate named in AGENTS.md, which I had skipped: python -m compileall -q laya/ tests/ is clean, and tests/test_hooks_api.py reports 551 passed, 0 failed.
| batch_size=batch_size or None, | ||
| hooks_timeout=hooks_timeout if hooks_timeout > 0 else None, | ||
| min_confidence=min_confidence if min_confidence >= 0 else None, | ||
| hooks_timeout=hooks_timeout or None, |
There was a problem hiding this comment.
Correct, and it is the same class of bug this PR is about, so it should not ship in a fix that claims to stop swallowing invalid values. Fixed in 32d262b by folding only a numeric 0:
if not isinstance(hooks_timeout, bool) and hooks_timeout == 0:
hooks_timeout = NoneThe isinstance check is the whole point: False == 0 is True, so an equality test alone folds a boolean too, and _validate_hooks_timeout refuses booleans by contract ("would silently turn True into a one-second deadline"). hooks_timeout is now annotated float | None, since None is what it folds to.
Both booleans are covered in the test, forwarding and rejection:
PASS batch_tool/hooks_timeout_True_reaches_tools True
PASS batch_tool/hooks_timeout_False_reaches_tools False
PASS batch_tool/hooks_timeout_false_rejected_downstream got 'invalid_hooks_timeout' want 'invalid_hooks_timeout'
laya_route_batch_tool has the identical or None on its hooks_timeout, so it has the same hole. I left it alone to keep this PR to one change — happy to take it as a follow-up if you'd rather it land here.
…eep booleans out of "unset" Review follow-up. `or None` also collapsed `False`, because `False == 0`, so a boolean still disappeared instead of reaching `_validate_hooks_timeout`, whose contract is to refuse booleans. Fold only a numeric 0 now, and cover both booleans in the forwarding and rejection checks. `hooks_timeout` is annotated `float | None` because None is the unset marker it folds to. Per AGENTS.md the public API contract suite has to move with a signature change, so tests/test_hooks_api.py pins `laya_predict_batch_tool`'s controls and their defaults (batch_size 0, hooks_timeout 0, min_confidence None, sort_by_length False). The import is guarded: the mcp extra is optional and a core install must still run this suite. Verified: tests/test_mcp.py 971 passed / 0 failed; tests/test_hooks_api.py 551 passed / 0 failed; tests/test_mcp_remote.py 53 passed; ruff and python -m compileall -q laya/ tests/ both clean.
|
Merged. Measured the thing the PR is about, on the merged tree: Before this, The Pinning the two defaults in |

What
laya_predict_batch_toolsends an out-of-rangehooks_timeoutormin_confidenceto the tools layer instead of collapsing it toNonefirst.min_confidencealso switches its declaration default from-1toNone, so-1is no longer a second meaning for "unset".Why
A client that sends
min_confidence=-0.5asked for a gate; it got "no gate" and an error-free response. The same forhooks_timeout=-0.5, which silently removed the hook deadline. Neither is detectable from the response.laya_predict_batchowns validation for both and answers with aToolErrornaming the value, and the above-range side already reached it —min_confidence=1.5is refused today. Only the below-range side was swallowed, so the two halves of the same rule behaved differently.-1was never a valid threshold (the documented range is[0, 1]), and_validate_hooks_timeout(0)already refuses0— which is why0stays "unset" and is the one value that must still be folded. Theisinstanceguard is deliberate:False == 0, so an equality test alone would fold a boolean too, and that validator refuses booleans by contract.How it was verified
tests/test_mcp.pygainstest_batch_tool_control_forwarding(), which records what the server tool hands the tools layer (the router and the wrapper are stubbed, so no checkpoint is loaded):It also pins that unset stays unset (
hooks_timeout=0, and both declaration defaults), that in-range values are forwarded unchanged (1.5,0.75), that both booleans reach the validator, and that the tools layer then refuses each of them.tests/test_hooks_api.pypins the tool's controls and their defaults, since this changes its advertised schema:The import there is guarded, so a core install without the
mcpextra still runs the suite.Docs need no change:
docs/cli-mcp.mdlists both controls as optional and pins no default.Checklist
main(branched from6880462,release: 0.4.2)tests/test_hooks_api.pyupdated per AGENTS.md; docs pin no default, so no docs editRefs #707 — this is the MCP-layer item from that thread's follow-up comments, not the
TypeError/ValueErrorcontract question, which is untouched.Known sibling with the same hole, left out to keep this focused:
laya_route_batch_toolforwardshooks_timeoutwith the identicalor None, so it also foldsFalse. Happy to take it as a follow-up.