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.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
fix(serve): the early refusal must report a length it measured, not cap + 1
Adeshina Lasisi510ab7ecd9f0
diff --git a/laya/serve.py b/laya/serve.pyindex 7a7fafb0..1ec74655 100644--- a/laya/serve.py+++ b/laya/serve.py@@ -205,11 +205,17 @@ def _resolve_port() -> int:_STATE_PROBE_VALUES = 64-def _provably_over_state_limit(state: Any, cap: int) -> bool:- """Whether `state` **provably** serializes past `cap`, without serializing it.+def _state_length_lower_bound_over(state: Any, cap: int) -> int:+ """A measured lower bound on the serialized length, if it already exceeds `cap`; else 0.- Returns True only when that is certain, and False for "not proven" -- never the other way round,- so a False sends the state on to the exact `json.dumps` measurement and the verdict is unchanged.+ Non-zero only when exceeding `cap` is certain, and 0 for "not proven" -- never the other way+ round, so a 0 sends the state on to the exact `json.dumps` measurement and no verdict changes.++ The value is returned rather than a bool so the 413 can quote a number something measured. It+ understates the true length (see below), so the message is a true statement about the state --+ unlike reporting `cap + 1`, which would answer "state too large (50001 > 50000 chars)" for a+ 60 012-character state. That is the same fabricated-count flaw this gate replaces, and an+ earlier revision of this branch reintroduced it.The bound: the raw lengths of the string values are a *lower* bound on the JSON length. Escapingmaps each character to one or more characters and so never shortens; keys, separators, brackets@@ -233,14 +239,14 @@ def _provably_over_state_limit(state: Any, cap: int) -> bool:if isinstance(item, str):total += len(item)if total > cap:- return True+ return totalelif isinstance(item, dict):# `.values()` and `extend` stay at C level; the keys are ignored, which keeps this a# lower bound. Iterating items in Python here made a 2000-key state 1.66x slower.stack.extend(item.values())elif isinstance(item, (list, tuple)):stack.extend(item)- return False+ return 0def _state_length(state: Any) -> int:@@ -274,11 +280,12 @@ def _state_length(state: Any) -> int:if isinstance(state, str):return len(state) # `serialize_state` returns a string state unchanged- if _provably_over_state_limit(state, MAX_STATE_CHARS):- # Already over on a lower bound, so serializing the rest cannot change the verdict. The- # number reported is the bound rather than the true length, which is why the message says- # "over" instead of quoting a total it did not measure.- return MAX_STATE_CHARS + 1+ over = _state_length_lower_bound_over(state, MAX_STATE_CHARS)+ if over:+ # Already past the cap on a lower bound, so serializing the rest cannot change the verdict.+ # The bound is what gets reported: it is a measured number and it understates, so the 413+ # never claims a length larger than the state really has.+ return overtry:return len(json.dumps(state, ensure_ascii=False))except (TypeError, ValueError, RecursionError):diff --git a/tests/test_serve.py b/tests/test_serve.pyindex 69aba017..89b582fd 100644--- a/tests/test_serve.py+++ b/tests/test_serve.py@@ -1150,6 +1150,21 @@ def test_the_probe_verdict_always_matches_the_exact_measurement():assert probed == exact+def test_the_early_refusal_reports_a_number_it_measured():+ """The 413 must not invent a count. Reporting `cap + 1` would answer "50001 > 50000" for a+ 60 012-character state -- the same fabricated count this gate was written to remove.+ """+ import laya.serve as serve_mod+ cap = serve_mod.MAX_STATE_CHARS+ for state in ({"body": "x" * 60000}, {"body": "y" * (2 * 1024 * 1024)}, {"a": ["z" * 80000]}):+ exact = len(json.dumps(state, ensure_ascii=False))+ reported = serve_mod._state_length(state)+ assert reported > cap, reported+ assert reported != cap + 1, "the refusal reported cap+1, a number nothing measured"+ assert reported <= exact, \+ "reported %d for a state of %d characters -- a 413 must never overstate" % (reported, exact)++def test_the_probe_is_bounded_and_falls_through_rather_than_guessing():"""The probe examines a constant number of values, so it can never become the expensive step.@@ -1159,5 +1174,5 @@ def test_the_probe_is_bounded_and_falls_through_rather_than_guessing():import laya.serve as serve_modassert serve_mod._STATE_PROBE_VALUES <= 256wide = {("k%06d" % i): "x" for i in range(200000)}- assert serve_mod._provably_over_state_limit(wide, serve_mod.MAX_STATE_CHARS) is False+ assert serve_mod._state_length_lower_bound_over(wide, serve_mod.MAX_STATE_CHARS) == 0assert serve_mod._state_length(wide) == len(json.dumps(wide, ensure_ascii=False))