Skip to content

armgit / laya

Public Imported from GitHub
0

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.

fix(calibrate): refuse a payload whose shape is not one this code can read

Aashish6510d4204315
diff --git a/laya/calibrate.py b/laya/calibrate.py
index cd437755..3906a5fe 100644
--- a/laya/calibrate.py
+++ b/laya/calibrate.py
@@ -398,13 +398,36 @@ def apply_calibration_payload(obj, payload: Dict[str, Any]) -> None:
and still loads, so an older file never becomes a hard failure. Each value is passed
through `clamp_temperature`, so a non-numeric or out-of-range entry cannot crash a later
forward the way an unclamped zero used to.
+
+ The payload's *shape* is checked before any of that, and refused with a `ValueError`
+ naming the field: the values may be junk the clamp forgives, but a temperature that is
+ not a list of three, a version that is not an integer, or a bucket map that is not an
+ object is a file this code cannot read -- JSON gives `int`, `str`, `list` and `dict`
+ for the three mistakes below just as happily as it gives the right shapes, and each
+ used to fail with a raw `TypeError`/`AttributeError` from `len()`/`dict()`, or worse,
+ to load: a `{"a": 1, "b": 2, "c": 3}` or `"abc"` has `len` 3 and used to pass the
+ length check and install its *keys* as temperatures.
"""
+ if not isinstance(payload, dict):
+ raise ValueError("calibration JSON must be an object, got %s" % type(payload).__name__)
version = payload.get("version", 1)
if version is None:
version = 1
+ if isinstance(version, bool) or not isinstance(version, (int, float, str)):
+ raise ValueError("calibration JSON version must be an integer, got %r" % (version,))
+ try:
+ version = int(version)
+ except (TypeError, ValueError, OverflowError) as exc: # Overflow: JSON's 1e999 is inf
+ raise ValueError(
+ "calibration JSON version must be an integer, got %r" % (version,)) from exc
temps = payload.get("temperature")
- if temps is None or len(temps) != N_QTYPES:
+ if not isinstance(temps, (list, tuple)) or len(temps) != N_QTYPES:
raise ValueError("calibration JSON must contain temperature: [3 floats]")
- if int(version) >= CALIBRATION_VERSION:
+ by_options = payload.get("temperature_by_options") or {}
+ if not isinstance(by_options, dict):
+ raise ValueError(
+ "calibration JSON temperature_by_options must be an object of bucket -> float, "
+ "got %s" % type(by_options).__name__)
+ if version >= CALIBRATION_VERSION:
_warn_if_identity_mismatch(obj, payload)
- _install_temperatures(obj, temps, payload.get("temperature_by_options") or {})
+ _install_temperatures(obj, temps, by_options)
diff --git a/tests/test_calibrate.py b/tests/test_calibrate.py
index 7fe6a717..e4adb12e 100644
--- a/tests/test_calibrate.py
+++ b/tests/test_calibrate.py
@@ -351,6 +351,50 @@ check(
)
+# --------------------------------------------------------------- payload shape
+# The values above are tolerated and clamped; the *shape* is not negotiable, and the
+# check is the line above the tolerance: "[3 floats]" means a list of three, not
+# anything with a length of three. JSON hands back int, str, list and dict for the
+# mistakes below just as happily as the right shapes -- a dict or a string of length 3
+# used to pass the length check and install its *keys* as temperatures, and a scalar
+# temperature or bucket map crashed with a raw TypeError instead of the ValueError
+# the function's own message promises.
+
+
+def _refuses(name, payload, fragment=None):
+ try:
+ apply_calibration_payload(stub, payload)
+ except ValueError as exc:
+ check_true(name, fragment is None or fragment in str(exc), str(exc))
+ return
+ except BaseException as exc: # noqa: BLE001 -- the old TypeError/AttributeError
+ FAIL.append("%s: raised %r, want ValueError" % (name, exc))
+ return
+ FAIL.append("%s: accepted %r" % (name, payload))
+
+
+_refuses("shape/non-object payload", ["temperature", [1.0, 1.0, 1.0]], "must be an object")
+_refuses("shape/scalar temperature", {"temperature": 5}, "[3 floats]")
+_refuses("shape/string temperature", {"temperature": "abc"}, "[3 floats]")
+_refuses("shape/dict temperature", {"temperature": {"a": 1, "b": 2, "c": 3}}, "[3 floats]")
+_refuses("shape/wrong-length temperature", {"temperature": [1.0, 1.0]}, "[3 floats]")
+_refuses("shape/string version", {"temperature": [1.0] * 3, "version": "x"},
+ "version must be an integer")
+_refuses("shape/bool version", {"temperature": [1.0] * 3, "version": True},
+ "version must be an integer")
+_refuses("shape/object version", {"temperature": [1.0] * 3, "version": {"v": 2}},
+ "version must be an integer")
+_refuses("shape/scalar by_options",
+ {"temperature": [1.0] * 3, "temperature_by_options": 5}, "temperature_by_options")
+_refuses("shape/list by_options",
+ {"temperature": [1.0] * 3, "temperature_by_options": [["choice:2", 1.5]]},
+ "temperature_by_options")
+# and a numeric string version still parses, as it did before the check existed
+version_ok = type("Stub", (), {})()
+apply_calibration_payload(version_ok, {"temperature": [1.1, 1.2, 1.3], "version": "1"})
+check("shape/numeric string version still loads", version_ok.temperature, [1.1, 1.2, 1.3])
+
+
# --------------------------------------------------------------- constructor wiring (no Hub download)
init_src = inspect.getsource(Agent.__init__)
check_true("init/calibration kwarg", "calibration: Optional[str] = None" in init_src)