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(confidence): a JSON integer wider than a double is not a confidence, in every reader
Aashish5d5d163d3cef
diff --git a/laya/confidence.py b/laya/confidence.pyindex a2b92644..ef890cbb 100644--- a/laya/confidence.py+++ b/laya/confidence.py@@ -7,6 +7,24 @@ import mathfrom typing import Any, Dict, List, Optional+def _finite(value: Any) -> bool:+ """Whether `value` is a real number that `float()` can read -- the test every check here means.++ `math.isfinite` is written for a `float`, and a Python `int` may be wider than one: JSON's+ grammar has no double limit, so a 400-digit confidence arrives as an `int` and `math.isfinite`+ raises `OverflowError: int too large to convert to float` instead of answering "not finite".+ `laya/_eval_policy.py::_finite_number` answers it this way already, and this module reads the+ same exception as "does not read as a number" in `jev_confidence`. A `bool` is refused here so+ no caller below has to repeat it.+ """+ if isinstance(value, bool) or not isinstance(value, (int, float)):+ return False+ try:+ return math.isfinite(value)+ except OverflowError:+ return False++def answer_confidence_value(answer: Dict[str, Any]) -> Optional[float]:"""The `answer_confidence` of `answer`, or None if it did not report a usable one.@@ -30,7 +48,7 @@ def answer_confidence_value(answer: Dict[str, Any]) -> Optional[float]:A `bool` is not a confidence and a NaN is not a decision, so both give None rather than 0.0."""conf = answer.get("answer_confidence")- if isinstance(conf, (int, float)) and not isinstance(conf, bool) and math.isfinite(conf):+ if _finite(conf):return float(conf)return None@@ -104,13 +122,13 @@ def _gate_confidence(answer: Dict[str, Any]) -> Optional[float]:if conf is not None:return conffallback = answer.get("confidence")- if isinstance(fallback, (int, float)) and not isinstance(fallback, bool) and math.isfinite(fallback):+ if _finite(fallback):return float(fallback)return Nonedef _check_one_threshold(v: Any) -> float:- if isinstance(v, bool) or not isinstance(v, (int, float)) or not math.isfinite(v) or v < 0.0 or v > 1.0:+ if not _finite(v) or v < 0.0 or v > 1.0:raise ValueError("min_confidence must be a float in [0.0, 1.0], got %r" % (v,))return float(v)diff --git a/tests/test_confidence.py b/tests/test_confidence.pyindex a08821c5..afe10b9b 100644--- a/tests/test_confidence.py+++ b/tests/test_confidence.py@@ -127,7 +127,13 @@ from laya.confidence import check_min_confidence, flag_low_confidence # noqa: Efor valid in (0.0, 0.5, 1.0, 0, 1, 0.85):check("min_confidence/valid %.2f" % valid, check_min_confidence(valid), float(valid))-for invalid in (True, False, -0.01, 1.01, -1.0, 2.0, float("nan"), float("inf"), float("-inf"), "0.5", None, [0.5]):+for invalid in (True, False, -0.01, 1.01, -1.0, 2.0, float("nan"), float("inf"), float("-inf"), "0.5", None, [0.5],+ # A JSON integer wider than a double is the third way a client's threshold is not a+ # number: `math.isfinite` raises `OverflowError` rather than answering "not finite",+ # so this walked out of `check_min_confidence` -- and out of `validate_min_confidence`,+ # whose docstring says it "only wraps its ValueError as a clean invalid_min_confidence+ # the client can read" -- as an interpreter error instead of the refusal.+ 10 ** 400, -(10 ** 400)):try:check_min_confidence(invalid)FAIL.append("min_confidence/should reject %r" % (invalid,))@@ -245,6 +251,11 @@ odd = [{"answers": {# None is not None is also False), so nothing else pinned this down."q_nan_entropy": {"type": "choice", "confidence": float("nan")},"q_bool_entropy": {"type": "choice", "confidence": True},+ # Both readers above end in `math.isfinite`, and a 400-digit integer is what JSON's unbounded+ # grammar hands them: the gate crashed instead of reporting `unevaluated`, from the same field+ # it is defined against and from the entropy fallback that already leaked a NaN.+ "q_overflow": {"type": "choice", "answer_confidence": 10 ** 400},+ "q_overflow_entropy": {"type": "choice", "confidence": 10 ** 400},"q_fine": {"type": "choice", "answer_confidence": 0.99},}}]apply_confidence_gate(odd, 0.80)diff --git a/tests/test_mcp.py b/tests/test_mcp.pyindex 65282847..3af9e914 100644--- a/tests/test_mcp.py+++ b/tests/test_mcp.py@@ -2563,7 +2563,13 @@ def test_min_confidence_control():for value in (0, 0.0, 0.5, 1, 1.0, 0.94):ok("minconf/ok_%r" % (value,), validate_min_confidence(value) == float(value),repr(validate_min_confidence(value)))- for bad in (-0.1, 1.1, 2, True, False, [], {}, "0.5", float("nan"), float("inf")):+ for bad in (-0.1, 1.1, 2, True, False, [], {}, "0.5", float("nan"), float("inf"),+ # "only re-labels the refusal" holds only while core's refusal reaches this layer as+ # the exception it catches. A 400-digit JSON integer -- which this server's clients+ # can send, since JSON has no double limit -- raised `OverflowError` out of+ # `math.isfinite`, past `except (TypeError, ValueError)`, so `min_confidence` was the+ # one field a client could crash the tool with instead of being refused.+ 10 ** 400, -(10 ** 400)):expect_tool_error("minconf/rejected_%r" % (bad,),lambda b=bad: validate_min_confidence(b), "invalid_min_confidence")