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
feat(router): verify each checkpoint with its own SHA-256 map
Aashish4b4dbdd25521
diff --git a/laya/router.py b/laya/router.pyindex 7dd999eb..f9021143 100644--- a/laya/router.py+++ b/laya/router.py@@ -207,6 +207,15 @@ class Router(HookRegistry):which is useful when standalone repositories were reviewed at different commits.Without either, huggingface_hub's normal default and existing offline cache are used.+ Artifact digests are opt-in and only ever per model: `sha256_digests={"english": {...}}`+ passes that `{path relative to the checkpoint dir: hexdigest}` map to the `Agent` that+ loads it, so a tampered or substituted weight file is refused before it is parsed. There+ is no Router-wide equivalent of `revision` because digests, unlike a commit SHA, are not+ shareable: the bundled repository ships a separate `model.safetensors` for each of+ `english`, `multilingual` and `typed-decisions`, so one flat map can only ever match one+ of them. A model listed with `None` or `{}` is loaded unverified, which also masks the+ `LAYA_SHA256_DIGESTS` environment default for that one checkpoint.+Hooks are opt-in and run at the Router level: `on_route` sees the routing decision,`on_load` / `on_evict` see model lifecycle, and `on_predict_start` / `on_predict_end`wrap the whole route+infer call. See `laya.hooks`.@@ -226,6 +235,7 @@ class Router(HookRegistry):token: Optional[str] = None,revision: Optional[str] = None,revisions: Optional[Dict[str, Optional[str]]] = None,+ sha256_digests: Optional[Dict[str, Optional[Dict[str, str]]]] = None,max_loaded: int = 2,default: str = "english",auto_task_detection: bool = False,@@ -257,6 +267,11 @@ class Router(HookRegistry):self.revisions: Dict[str, Optional[str]] = {normalise_name(k): v for k, v in (revisions or {}).items()}+ # Per checkpoint SHA-256 map. Keyed and normalised exactly like `revisions`, so a+ # misspelled model name fails here rather than leaving that checkpoint unverified.+ self.sha256_digests: Dict[str, Optional[Dict[str, str]]] = {+ normalise_name(k): v for k, v in (sha256_digests or {}).items()+ }self.max_loaded = max(1, int(max_loaded))self.default = normalise_name(default)self.auto_task_detection = bool(auto_task_detection)@@ -292,6 +307,10 @@ class Router(HookRegistry):model_revision = self.revisions.get(key, self.revision)if model_revision is not None:kwargs["revision"] = model_revision+ if key in self.sha256_digests:+ # `or {}` is deliberate: an explicit None still has to reach Agent as a map, or+ # `verify_digests` would fall back to the environment default it is masking.+ kwargs["expected_sha256"] = self.sha256_digests[key] or {}agent = Agent(repo, **kwargs)self._agents[key] = agentself._order.append(key)diff --git a/tests/test_hooks_api.py b/tests/test_hooks_api.pyindex fa750e30..804d779f 100644--- a/tests/test_hooks_api.py+++ b/tests/test_hooks_api.py@@ -68,6 +68,8 @@ for label, fn in (("Agent.__init__", Agent.__init__), ("load", load),# Router keeps lang_guess and explicit per-model revisions toocheck_param("Router.__init__", Router.__init__, "lang_guess", None)check_param("Router.__init__", Router.__init__, "revisions", None)+# ...and the per-checkpoint digests that `revisions` has always had a sibling need for+check_param("Router.__init__", Router.__init__, "sha256_digests", None)# --------------------------------------------------------------- predict surfacesfor label, fn in (("Agent.predict_batch", Agent.predict_batch),diff --git a/tests/test_revision_pinning.py b/tests/test_revision_pinning.pyindex 176e8dd4..e113a4bf 100644--- a/tests/test_revision_pinning.py+++ b/tests/test_revision_pinning.py@@ -183,5 +183,116 @@ class RouterRevisionTests(unittest.TestCase):self.assertEqual(router.loaded_revisions, {"english": "sha-english"})+class RouterDigestTests(unittest.TestCase):+ """`Router(sha256_digests=...)` is the per-checkpoint sibling of `revisions`."""++ def _capture(self):+ import laya.agent++ captured: list = []++ class FakeAgent:+ def __init__(self, repo, **kwargs):+ captured.append(kwargs)++ return patch.object(laya.agent, "Agent", FakeAgent), captured++ def test_router_normalises_digest_keys_like_revision_keys(self):+ router = Router(sha256_digests={"ml": {"w.bin": "a" * 64}, "typed": None})+ self.assertEqual(sorted(router.sha256_digests), ["multilingual", "typed-decisions"])+ self.assertEqual(Router().sha256_digests, {})++ def test_misspelled_model_fails_at_construction(self):+ with self.assertRaises(ValueError):+ Router(sha256_digests={"engligh": {"w.bin": "a" * 64}})++ def test_each_checkpoint_gets_its_own_map(self):+ capture, captured = self._capture()+ with capture:+ router = Router(sha256_digests={+ "english": {"model.safetensors": "a" * 64},+ "multilingual": {"model.safetensors": "b" * 64},+ })+ router.load("english")+ router.load("multilingual")+ self.assertEqual(captured[0]["expected_sha256"], {"model.safetensors": "a" * 64})+ self.assertEqual(captured[1]["expected_sha256"], {"model.safetensors": "b" * 64})++ def test_unlisted_checkpoint_is_left_to_the_environment(self):+ capture, captured = self._capture()+ with capture:+ Router(sha256_digests={"multilingual": {"model.safetensors": "b" * 64}}).load("english")+ self.assertNotIn("expected_sha256", captured[0])++ def test_none_entry_masks_the_environment_default_for_that_checkpoint(self):+ capture, captured = self._capture()+ with capture:+ Router(sha256_digests={"english": None}).load("english")+ # `{}` and "absent" differ inside verify_digests: only the absent one falls back to env.+ self.assertEqual(captured[0]["expected_sha256"], {})+++class RouterDigestEndToEndTests(unittest.TestCase):+ """Two checkpoints, one shared relative filename, two different digests."""++ def setUp(self):+ self.tmp = tempfile.TemporaryDirectory()+ self.dirs = {}+ self.digests = {}+ for name in ("english", "multilingual"):+ # `model.safetensors` without a valid config: a matching digest gets as far as the+ # config check, a mismatching one never leaves the digest check.+ path = os.path.join(self.tmp.name, name)+ os.makedirs(path)+ with open(os.path.join(path, "model.safetensors"), "wb") as f:+ f.write(b"weights of " + name.encode())+ self.dirs[name] = path+ self.digests[name] = hashlib.sha256(b"weights of " + name.encode()).hexdigest()++ def tearDown(self):+ self.tmp.cleanup()++ def _router(self, digests, **kw):+ return Router(models=dict(self.dirs), sha256_digests=digests, **kw)++ def _assert_past_the_digest_gate(self, router, name):+ """The config check sits just after `verify_digests`, so reaching it proves the digest passed."""+ with self.assertRaises(FileNotFoundError) as cm:+ router.load(name)+ self.assertIn("rl_agent_config.json", str(cm.exception))++ def test_a_matching_digest_lets_the_checkpoint_through(self):+ router = self._router({"english": {"model.safetensors": self.digests["english"]}})+ self._assert_past_the_digest_gate(router, "english")++ def test_one_flat_map_cannot_cover_both_checkpoints(self):+ router = self._router({"english": {"model.safetensors": self.digests["english"]},+ "multilingual": {"model.safetensors": self.digests["english"]}})+ self._assert_past_the_digest_gate(router, "english")+ with self.assertRaises(ValueError) as cm:+ router.load("multilingual")+ self.assertIn("SHA-256 mismatch", str(cm.exception))++ def test_per_checkpoint_maps_cover_both(self):+ router = self._router({"english": {"model.safetensors": self.digests["english"]},+ "multilingual": {"model.safetensors": self.digests["multilingual"]}})+ self._assert_past_the_digest_gate(router, "english")+ self._assert_past_the_digest_gate(router, "multilingual")++ def test_environment_digest_still_applies_when_no_map_is_given(self):+ env = {"LAYA_SHA256_DIGESTS": json.dumps({"model.safetensors": self.digests["english"]})}+ router = self._router({})+ with patch.dict(os.environ, env):+ self._assert_past_the_digest_gate(router, "english")+ with self.assertRaises(ValueError):+ router.load("multilingual")++ def test_explicit_none_list_skips_the_environment_digest(self):+ env = {"LAYA_SHA256_DIGESTS": json.dumps({"model.safetensors": self.digests["english"]})}+ router = self._router({"multilingual": None})+ with patch.dict(os.environ, env):+ self._assert_past_the_digest_gate(router, "multilingual")++if __name__ == "__main__":unittest.main()