Skip to content

feat(serve): forward the per-call controls /v1/systemone already forwards on /v1/systemone/batch - #774

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:feat-serve-batch-per-call-controls
Oct 1, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:feat-serve-batch-per-call-controls

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

What

/v1/systemone/batch now reads and forwards the same per-call controls
/v1/systemone already forwards (max_len, head_max_len, task,
lang, lang_guess, min_confidence), plus the two batch-only knobs
Router.predict_batch takes (batch_size, sort_by_length). It also
honours the same BODY_REFUSALS policy, so a batch body cannot silently
shorten the operator's own hook deadline.

Concretely:

  • laya/serve.py grows BATCH_BODY_CALL_CONTROLS and
    BATCH_BODY_ITEM_CONTROLS, so Router.predict_batch's split
    (call-level kwargs vs. per-request keys it lifts off each item) is
    declared rather than implicit.
  • The batch handler reads the same six controls the single endpoint
    reads, using the same validators
    (_validate_budget_param, _validate_language_param,
    _validate_min_confidence), and adds _validate_batch_size_param
    and _validate_sort_by_length_param for the two batch-only knobs.
  • Every control is forwarded only when the caller sent it, matching
    _systemone_inner's rule that an absent argument means "inherit
    what the Router was built with".
  • _refuse_body_refusals now guards the batch path too, which matters
    specifically because predict_batch does take hooks_timeout --
    without the refusal a caller could hand itself a shorter deadline on
    which the deployment's hooks execute.
  • The predict() fallback (an attached Router without predict_batch)
    gets the same kwargs so a min_confidence on a batch body still
    engages the abstention gate per state instead of silently
    disappearing on the older-Router path.

Why

Router.predict_batch reads two kinds of control: call-level keyword
arguments that apply to the whole batch (chunking, padding, abstention)
and per-request keys it lifts off each item of requests (checkpoint,
task, language, token budget). The HTTP batch endpoint at laya/serve.py
was synthesizing [{"state": s, "questions": questions, "model": model} for s in states] and calling router.predict_batch(reqs) bare. A
client that sent min_confidence, max_len, task, lang, lang_guess
or head_max_len at the top level -- which is what #724 taught
/v1/systemone to accept -- got a 200 as though the control had been
honoured and none of it applied. batch_size and sort_by_length,
which predict_batch takes as call kwargs and predict does not, were
not read at all.

The refusal gap is more than cosmetic. predict_batch accepts
hooks_timeout as a call argument, and today the batch endpoint drops
it silently rather than refusing it, so a caller asking for a shorter
hook deadline on a batch body got the same non-answer the pre-#724
single endpoint gave for the same key. That is exactly the failure mode
#724 documented and closed on /v1/systemone, left open here.

The concrete use case is a caller that wants a batch of abstention-gated
decisions -- min_confidence: 0.9 on a heterogeneous set of states --
without paying for N single-predict HTTP round-trips. Right now that
caller either sends N singles (defeating the batch endpoint) or sends a
batch and gets back an unhedged answer they had asked to be hedged.

How it was verified

$ python -m pytest tests/test_serve.py -q
180 passed in 3.37s

$ python tests/test_hooks_api.py
365 passed, 0 failed
all hook API tests passed

$ python tests/test_router.py
552 passed, 0 failed
all routing tests passed

$ python tests/test_cli.py
135 passed, 0 failed
all CLI tests passed

$ python tests/test_mcp.py
769 passed, 0 failed
all mcp tests passed

$ ruff check laya/serve.py tests/test_serve.py --select=E9,F63,F7,F82,F401,F811 --line-length=120
All checks passed!

$ python -m compileall -q laya/ tests/
(silent)

tests/test_serve.py was at 131/131 on upstream/main; this PR adds
49 tests. Two of them are pins, not behaviour:

  • test_batch_call_controls_are_exactly_predict_batch_kwargs_or_refusal
    compares BATCH_BODY_CALL_CONTROLS ∪ {hooks_timeout} against
    inspect.signature(Router.predict_batch).parameters - {self, requests}.
    A new call-level kwarg on predict_batch cannot be silently ignored
    on the HTTP path; it has to be placed on one side of the
    forwarded/refused line first.
  • test_batch_controls_cover_the_single_endpoints_set_plus_batch_only
    pins BATCH_BODY_ITEM_CONTROLS ∪ BATCH_BODY_CALL_CONTROLS against
    (BODY_CONTROLS ∪ {batch_size, sort_by_length}) - {model}, so a
    control the single endpoint forwards cannot drift out of the batch
    path without a named test failure.

Mutation check

7 mutants, each a faithful single-line break to laya/serve.py, each
producing a NAMED FAIL on the restored tree (harness in
/tmp/mutate_serve_batch_controls.py; every mutant resets the tree and
clears the serve.*.pyc cache before running so a stale bytecode file
cannot hide a survivor).

# Mutation Named FAIL
M1 Drop _refuse_body_refusals(body) on the batch handler test_batch_hook_control_is_refused_not_dropped[hooks_raise], [hooks_timeout], + 3 more (5 failed)
M2 Always pass batch_size=None to predict_batch test_batch_strict_predict_batch_router_answers_a_quiet_body (1 failed)
M3 Never forward **item_overrides into the request dicts test_each_batch_item_control_reaches_every_request[max_len], [task], + 3 more (5 failed)
M4 Falsy-check on min_confidence (if min_confidence:) test_batch_min_confidence_zero_is_still_a_threshold (1 failed)
M5 Forward sort_by_length even when False test_batch_sort_by_length_false_is_not_forwarded (1 failed)
M6 predict() fallback ignores kwargs test_batch_predict_fallback_forwards_every_control (1 failed)
M7 batch_size validator uses val < 0 instead of val < 1 test_bad_control_on_batch_is_refused_before_inference[batch_size-0] (1 failed)

StrictBatchRouter (predict_batch takes only requests) is the witness
behind M2. The endpoint's "send kwargs only when the caller asked" rule
is what keeps an attached Router that predates #766 answering on a
quiet body -- M2 breaks that guarantee and the strict-router test names
it.

Design notes

  • Item-level vs call-level is not a stylistic split. Router.predict_batch
    reads max_len / head_max_len / task / lang / lang_guess off
    each item of requests so different requests in a batch can carry
    different budgets -- the endpoint's HTTP body has one control set,
    so every synthesized item receives the same override. min_confidence,
    batch_size and sort_by_length apply to the call as a whole and
    go on the kwargs.
  • _validate_batch_size_param rejects a bool explicitly. bool is
    an int subclass, so isinstance(val, int) would let True
    through; the MCP tool at laya/mcp/tools.py:_validate_batch_size
    refuses the same shape, so the two surfaces agree on what "typo"
    looks like.
  • sort_by_length=False is accepted (a False value is not a
    422) but not forwarded. predict_batch's own default is False, and
    forwarding False explicitly to an agent whose predict_batch
    predates the knob (feat(agent): group batch inputs by encoded length #294) would silently disappear -- which is the
    same reason the MCP tool makes the same choice at
    laya/mcp/tools.py:947-948.
  • The predict() fallback path uses a "no kwargs when body is quiet"
    shape (elif predict_kwargs: … then else: … predict(s, questions, model=model)) so an attached FakeRouter-shape Router whose
    predict(self, state, questions, model=None) takes nothing else
    still answers on a bare batch body.

Not in this PR

  • docs/http-api.md still says "no batch endpoint" at line 194. That
    is a pre-existing lie about the shipped HTTP surface -- serve.py has
    had /v1/systemone/batch since before this PR -- and it is unrelated
    to the controls gap closed here. The follow-up belongs in a docs PR
    that adds the batch table and extends the existing
    test_http_api_page_documents_exactly_the_forwarded_controls pin to
    read both tables.
  • _has_lone_surrogate is still not applied on the batch body. The
    same "unpaired surrogate escape becomes a 500 rather than a 400" bug
    that laya/serve.py:716 fixes on the single path is present on the
    batch path. That is a different bug class (HTTP status mapping, not
    control forwarding) and belongs in its own PR.

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

    (No new documented API changed; the batch endpoint's controls
    surface is being brought to parity with the already-documented single
    endpoint. Batch-endpoint documentation is called out above as
    out-of-scope.)

Fixes #

…ards on /v1/systemone/batch

`Router.predict_batch` reads six controls off each request item
(max_len, head_max_len, task, lang, lang_guess, model) and three as call
kwargs (batch_size, min_confidence, sort_by_length). The batch endpoint
at laya/serve.py:851+ was synthesizing {state, questions, model} and
calling predict_batch bare, silently dropping every per-call control the
single /v1/systemone handler already honours (NandhaKishorM#724) plus the two
batch-only knobs.

Split BATCH_BODY_CALL_CONTROLS / BATCH_BODY_ITEM_CONTROLS pin the same
two-side contract against Router.predict_batch's signature and against
the single endpoint's BODY_CONTROLS, so a new core kwarg cannot be
ignored on the HTTP batch path. Refusal policy for hooks and the two
hook-execution knobs is now shared between the endpoints: predict_batch
*does* take hooks_timeout, so a batch body without the refusal would
silently shorten the operator's own hook deadline.

The predict() fallback path (an attached Router without predict_batch)
gets the same treatment so a caller that sends min_confidence on a
batch body still sees the abstention gate apply per state.
@NandhaKishorM
NandhaKishorM merged commit 43b1a41 into NandhaKishorM:main Oct 1, 2026
24 of 25 checks passed
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged for 0.3.23.

Verified on the integrated branch, not on the PR in isolation: every suite in the ci.yml test lane plus the pytest lane green, compileall clean, and a decision diff against main on the real english and multilingual checkpoints (72 answers over choice, score and noul, short and long states, budgeted and predict_long) showing zero decision flips and a max probability delta of 0.000000.

Thank you.

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