From 3a9069f66c73f04d562c13788b756e25e8411f3b Mon Sep 17 00:00:00 2001 From: Aashish <145881415+aashish254@users.noreply.github.com> Date: Wed, 30 Sep 2026 13:20:21 +0545 Subject: [PATCH] PR: expose hooks_timeout/min_confidence/sort_by_length on laya_predict_batch Gaps found: MCP batch tool omitted 3 per-call controls that Router.predict_batch accepts. - Added hooks_timeout (float), min_confidence (float), sort_by_length (bool) as optional parameters - Forward all three through to router.predict_batch() in tools.py - Updated server wrapper to accept and pass them through - Test suite validates forwarding with BatchRouter tracking kwargs - Mutation-check: gate ensures these knobs reach Router.predict_batch --- laya/mcp/server.py | 6 +++++- laya/mcp/tools.py | 15 +++++++++++++-- tests/test_mcp.py | 34 ++++++++++++++++++++++++++++++---- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/laya/mcp/server.py b/laya/mcp/server.py index e7d362e66..f36577e4a 100644 --- a/laya/mcp/server.py +++ b/laya/mcp/server.py @@ -285,7 +285,8 @@ def laya_predict_tool( + _GUARDRAILS ), ) -def laya_predict_batch_tool(requests: list, batch_size: int = 0) -> str: +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: """Answer many typed-question requests in one batched call.""" router = _router_or_error() # batch_size=0 means "unset": MCP clients send defaults eagerly, and @@ -294,6 +295,9 @@ def laya_predict_batch_tool(requests: list, batch_size: int = 0) -> str: laya_predict_batch, requests=requests, 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, + sort_by_length=sort_by_length, router=router, ) diff --git a/laya/mcp/tools.py b/laya/mcp/tools.py index 1bf7f269b..1a643dca8 100644 --- a/laya/mcp/tools.py +++ b/laya/mcp/tools.py @@ -911,6 +911,9 @@ def _validate_batch_size(batch_size: Any) -> int | None: def laya_predict_batch( requests: Any, batch_size: Any = None, + hooks_timeout: Any = None, + min_confidence: Any = None, + sort_by_length: bool = False, *, router: Any = None, ) -> dict: @@ -934,8 +937,16 @@ def laya_predict_batch( started = time.perf_counter() try: - results = router.predict_batch(items, batch_size=size) if size is not None \ - else router.predict_batch(items) + kwargs = {} + if size is not None: + kwargs["batch_size"] = size + if hooks_timeout is not None: + kwargs["hooks_timeout"] = hooks_timeout + if min_confidence is not None: + kwargs["min_confidence"] = min_confidence + if sort_by_length: + kwargs["sort_by_length"] = True + results = router.predict_batch(items, **kwargs) except TypeError as exc: # A Router without batch support raises at the call itself; anything # else is a real bug and must surface unchanged. diff --git a/tests/test_mcp.py b/tests/test_mcp.py index d878d8c61..dcee67d40 100644 --- a/tests/test_mcp.py +++ b/tests/test_mcp.py @@ -842,8 +842,8 @@ def _answer_for(self, request): "routing": {"model": request.get("model") or "english", "repo": "fake/laya", "reason": "batch route"}} - def predict_batch(self, requests, batch_size=None): - self.predict_batch_calls.append((list(requests), batch_size)) + def predict_batch(self, requests, batch_size=None, **kwargs): + self.predict_batch_calls.append((list(requests), batch_size, kwargs)) return [self._answer_for(request) for request in requests] def route_batch(self, requests): @@ -965,7 +965,7 @@ def test_batch_predict(): out = laya_predict_batch(BATCH_REQUESTS, batch_size=8, router=router) ok("batch/predict_one_call", len(router.predict_batch_calls) == 1, repr(len(router.predict_batch_calls))) - forwarded, size = router.predict_batch_calls[0] + forwarded, size, kwargs = router.predict_batch_calls[0] ok("batch/predict_size_forwarded", size == 8, repr(size)) ok("batch/predict_items_forwarded", [item["state"] for item in forwarded] == [request["state"] for request in BATCH_REQUESTS]) @@ -1002,8 +1002,34 @@ def test_batch_predict(): # batch_size unset must not be forwarded as None (strict old stubs included). router = BatchRouter() laya_predict_batch(BATCH_REQUESTS, router=router) - ok("batch/predict_size_default", router.predict_batch_calls[0][1] is None) + _, size, _ = router.predict_batch_calls[0] + ok("batch/predict_size_default", size is None) + # Test that hooks_timeout parameter is forwarded correctly + router = BatchRouter() + out = laya_predict_batch(BATCH_REQUESTS, hooks_timeout=5.5, router=router) + _, kwargs = router.predict_batch_calls[0][1], router.predict_batch_calls[0][2] + ok("batch/hooks_timeout_forwarded", kwargs.get("hooks_timeout") == 5.5, repr(kwargs)) + + # Test that min_confidence parameter is forwarded correctly + router = BatchRouter() + out = laya_predict_batch(BATCH_REQUESTS, min_confidence=0.75, router=router) + _, kwargs = router.predict_batch_calls[0][1], router.predict_batch_calls[0][2] + ok("batch/min_confidence_forwarded", kwargs.get("min_confidence") == 0.75, repr(kwargs)) + + # Test that sort_by_length parameter is forwarded correctly + router = BatchRouter() + out = laya_predict_batch(BATCH_REQUESTS, sort_by_length=True, router=router) + _, kwargs = router.predict_batch_calls[0][1], router.predict_batch_calls[0][2] + ok("batch/sort_by_length_forwarded", kwargs.get("sort_by_length") is True, repr(kwargs)) + + # Test that defaults don't pollute kwargs + router = BatchRouter() + out = laya_predict_batch(BATCH_REQUESTS, router=router) + _, kwargs = router.predict_batch_calls[0][1], router.predict_batch_calls[0][2] + ok("batch/defaults_not_in_kwargs", "hooks_timeout" not in kwargs and + "min_confidence" not in kwargs and "sort_by_length" not in kwargs, repr(kwargs)) + expect_tool_error("batch/predict_count_mismatch", lambda: laya_predict_batch(BATCH_REQUESTS, router=ShortRouter()), "internal_error")