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.

Merge pull request #572 from aashish254/feat-router-digests

Nandakishor7febda4c9b91
diff --cc laya/router.py
index fe54b99b,58772c00..a2b9ec97
--- a/laya/router.py
+++ b/laya/router.py
@@@ -176,74 -174,47 +176,115 @@@ def _english_from_code(value: Any) -> O
return primary in _ENGLISH_SUBTAGS
+class _ScanLong:
+ """Last start hook of `Router.predict_long`: score the routed state with the agent's
+ `predict_long` rather than leave `predict` to call `system_one` on one window of it.
+
+ It is appended after every other start hook, so a caller's hook has already had its say: one
+ that answered (`ctx.skip(...)`) or rewrote the state/questions wins, and only what is left is
+ scanned. The `lang` rule is `predict`'s -- an explicit `lang=`, else the language routing
+ detected -- repeated here because `predict` computes it locally for its own call.
+ """
+
+ def __init__(self, window, stride, aggregate, batch_size, lang):
+ self.window = window
+ self.stride = stride
+ self.aggregate = aggregate
+ self.batch_size = batch_size
+ self.lang = lang
+
+ def on_predict_start(self, ctx):
+ if ctx.results is not None:
+ return
+ if not hasattr(ctx.agent, "predict_long"):
+ raise TypeError(
+ "%s has no predict_long, so a state longer than its window cannot be scanned; "
+ "the PyTorch Agent implements it, and an ONNX or hand-attached agent needs it too"
+ % type(ctx.agent).__name__
+ )
+ lang = self.lang
+ if lang is None:
+ lang = (ctx.decision.get("detection") or {}).get("language")
+ ctx.results = [ctx.agent.predict_long(
+ ctx.states[0], ctx.questions, window=self.window, stride=self.stride,
+ aggregate=self.aggregate, batch_size=self.batch_size, lang=lang)]
+
+
+# Checkpoint options a Router will not accept through `agent_kwargs`: the ones it sets for itself
+# on every `Agent(...)` it builds, plus the hook family, which has its own home in `Router(hooks=)`
+# and would otherwise fire from two registries at once.
+_ROUTER_OWNED_AGENT_ARGS = frozenset({
+ "model_id_or_path", "device", "token", "subfolder", "revision",
+ "hooks", "on_predict_start", "on_predict_end", "hooks_raise", "hooks_concurrent",
+ "hooks_timeout",
+})
+
+
+def check_agent_kwargs(agent_kwargs: Dict[str, Any]) -> None:
+ """Reject `agent_kwargs` names a Router cannot honour, before any checkpoint is loaded.
+
+ The accepted names are read out of `Agent.__init__`'s signature rather than written down here:
+ the whole point of `agent_kwargs` is that this file stops keeping its own list of checkpoint
+ options, so a hard-coded copy of them would go stale in exactly the way this argument removes.
+ The import is only reached when a caller passes the argument, which is also the moment torch is
+ about to be loaded anyway.
+ """
+ from .agent import Agent
+
+ accepted = set(inspect.signature(Agent.__init__).parameters) - {"self"}
+ reserved = sorted(set(agent_kwargs) & _ROUTER_OWNED_AGENT_ARGS)
+ if reserved:
+ raise ValueError(
+ "Router sets %s itself; pass them to Router(...) instead of in agent_kwargs"
+ % (", ".join(repr(n) for n in reserved),))
+ unknown = sorted(set(agent_kwargs) - accepted)
+ if unknown:
+ raise ValueError(
+ "Agent accepts none of %s; it accepts %s"
+ % (", ".join(repr(n) for n in unknown), sorted(accepted - _ROUTER_OWNED_AGENT_ARGS)))
+
+
+ def _digests_from_env(models: Dict[str, Any]) -> Dict[str, Optional[Dict[str, str]]]:
+ """Turn `LAYA_SHA256_DIGESTS` into per-checkpoint digest maps when it names models.
+
+ The variable has two shapes, and the value types say which. A flat
+ `{artifact: digest}` map is `laya.revisions.verify_digests`'s own reading -- the same
+ files checked on every checkpoint the process loads -- and is returned as `{}` here so
+ that path stays untouched. A nested `{model: {artifact: digest}}` map pins each
+ checkpoint with its own files, which is what a server holding several resident needs:
+ the bundled repository ships a separate `model.safetensors` per checkpoint, so one flat
+ map can only ever match one of them and refuses the rest at startup.
+
+ A checkpoint the nested map does not name is returned as `{}`, meaning deliberately
+ unpinned: `Agent` falls back to the environment when given `None`, and reading a
+ model-keyed map as an artifact map fails with `cannot verify 'english': no such file`.
+ Keys are normalised exactly as `Router(sha256_digests=...)` normalises them, so `en`
+ names `english` and a name core does not know raises here rather than quietly leaving
+ that checkpoint unverified. Unset, empty or unparseable input returns `{}`:
+ `laya.revisions` reports a malformed value in its own words.
+ """
+ raw = os.environ.get("LAYA_SHA256_DIGESTS", "").strip()
+ if not raw:
+ return {}
+ try:
+ data = json.loads(raw)
+ except ValueError:
+ return {}
+ if not isinstance(data, dict) or not data:
+ return {}
+ values = list(data.values())
+ if all(isinstance(v, str) for v in values):
+ return {} # flat: laya.revisions already applies it
+ if not all(isinstance(v, dict) for v in values):
+ raise ValueError("LAYA_SHA256_DIGESTS must be either {artifact: digest} for every "
+ "checkpoint or {model: {artifact: digest}} per checkpoint; %s mixes "
+ "the two or holds a value that is neither" % sorted(data))
+ per_model = {normalise_name(k): v for k, v in data.items()}
+ for name in models:
+ per_model.setdefault(normalise_name(name), {})
+ return per_model
+
+
class Router(HookRegistry):
"""Lazily loads Laya checkpoints and sends each request to the right one.
@@@ -277,18 -248,22 +318,34 @@@
which is useful when standalone repositories were reviewed at different commits.
Without either, huggingface_hub's normal default and existing offline cache are used.
+ Anything else `laya.Agent` accepts is reachable through `agent_kwargs`, which is merged into
+ every checkpoint the Router builds:
+
+ Router(agent_kwargs={"lang_temperatures": {"de": {"temperature": [1.0, 1.4, 2.0]}}})
+ Router(agent_kwargs={"expected_sha256": {"model.safetensors": "a3f1..."}})
+ Router(agent_kwargs={"fast": True})
+
+ The names the Router sets for itself -- `model_id_or_path`, `device`, `token`, `subfolder`,
+ `revision` and the hook arguments -- are refused here rather than silently shadowed, and the
+ remaining names are checked against `Agent.__init__` at construction, so a misspelled option
+ fails on the `Router(...)` line instead of on the first request.
+
+ Artifact digests are opt-in and always 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.
+
+ The same split is available to a process configured only by environment: when
+ `LAYA_SHA256_DIGESTS` holds a model-keyed map (`{"english": {...}, "multilingual": {...}}`)
+ this seeds it per checkpoint, so a server that keeps several resident can pin each with its
+ own digests instead of refusing to start on the second one. A flat `LAYA_SHA256_DIGESTS`
+ keeps its existing meaning, applied by `laya.revisions` to every checkpoint the process
+ loads, which is right for a single-checkpoint one. An argument entry wins over the
+ environment for the model it names.
+
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`.
@@@ -309,7 -283,7 +366,8 @@@
token: Optional[str] = None,
revision: Optional[str] = None,
revisions: Optional[Dict[str, Optional[str]]] = None,
+ agent_kwargs: Optional[Dict[str, Any]] = None,
+ sha256_digests: Optional[Dict[str, Optional[Dict[str, str]]]] = None,
max_loaded: int = 2,
default: str = "english",
auto_task_detection: bool = False,
@@@ -341,11 -315,14 +399,19 @@@
self.revisions: Dict[str, Optional[str]] = {
normalise_name(k): v for k, v in (revisions or {}).items()
}
+ # Options forwarded to every `Agent` this Router builds, checked now so a bad name fails on
+ # this line rather than the first request that happens to load a checkpoint.
+ self.agent_kwargs: Dict[str, Any] = dict(agent_kwargs or {})
+ if self.agent_kwargs:
+ check_agent_kwargs(self.agent_kwargs)
+ # Per checkpoint SHA-256 map: seeded from a model-named `LAYA_SHA256_DIGESTS`, then
+ # overridden checkpoint by checkpoint by the argument. 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]]] = _digests_from_env(self.models)
+ self.sha256_digests.update({
+ 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)
@@@ -381,8 -358,10 +447,12 @@@
model_revision = self.revisions.get(key, self.revision)
if model_revision is not None:
kwargs["revision"] = model_revision
+ # Safe to merge last: the names this method just set are refused in `agent_kwargs`.
+ kwargs.update(self.agent_kwargs)
+ 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] = agent
self._order.append(key)
diff --cc tests/test_hooks_api.py
index 4e8a38ab,804d779f..ffaeac02
--- a/tests/test_hooks_api.py
+++ b/tests/test_hooks_api.py
@@@ -68,8 -68,8 +68,10 @@@ for label, fn in (("Agent.__init__", Ag
# Router keeps lang_guess and explicit per-model revisions too
check_param("Router.__init__", Router.__init__, "lang_guess", None)
check_param("Router.__init__", Router.__init__, "revisions", None)
+# ...and the remaining `Agent` options, forwarded to every checkpoint it builds
+check_param("Router.__init__", Router.__init__, "agent_kwargs", 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 surfaces
for label, fn in (("Agent.predict_batch", Agent.predict_batch),
diff --cc tests/test_revision_pinning.py
index a0c1198f,b2aa6ce6..48c53607
--- a/tests/test_revision_pinning.py
+++ b/tests/test_revision_pinning.py
@@@ -183,122 -183,229 +183,346 @@@ class RouterRevisionTests(unittest.Test
self.assertEqual(router.loaded_revisions, {"english": "sha-english"})
+class RouterAgentKwargsTests(unittest.TestCase):
+ """`agent_kwargs` is how a Router user reaches the rest of `Agent`'s constructor.
+
+ Before it, `Router.load` built every checkpoint with exactly four arguments, so
+ `lang_temperatures`, `expected_sha256`, `fast` and `compile` could only be set by giving up the
+ Router and hand-building an `Agent` -- which also left the per-language grouping in
+ `Router.predict_batch` unable to fire for any agent the Router owned.
+ """
+
+ TABLE = {"de": {"temperature": [2.0, 2.0, 2.0], "temperature_by_options": {"choice:2": 3.0}}}
+
+ @staticmethod
+ def _fake_agent():
+ """(module holding Agent, the fake, the list each build is appended to).
+
+ `check_agent_kwargs` reads the option names out of whatever `laya.agent.Agent` currently
+ is, so the stand-in has to carry the real signature or these tests would be refused before
+ they ever reached a build.
+ """
+ import inspect
+
+ import laya.agent
+
+ captured = []
+
+ class FakeAgent:
+ def __init__(self, repo, **kwargs):
+ captured.append((repo, kwargs))
+
+ __init__.__signature__ = inspect.signature(Agent.__init__)
+
+ return laya.agent, FakeAgent, captured
+
+ def test_agent_kwargs_reach_the_agent_build(self):
+ module, fake, captured = self._fake_agent()
+ with patch.object(module, "Agent", fake):
+ Router(agent_kwargs={"lang_temperatures": self.TABLE}).load("english")
+ self.assertEqual(captured[0][1]["lang_temperatures"], self.TABLE)
+
+ def test_applied_to_every_checkpoint_the_router_builds(self):
+ module, fake, captured = self._fake_agent()
+ with patch.object(module, "Agent", fake):
+ router = Router(agent_kwargs={"expected_sha256": {"model.safetensors": "0" * 64}})
+ router.load("english")
+ router.load("multi")
+ self.assertEqual(len(captured), 2)
+ for _repo, kwargs in captured:
+ self.assertEqual(kwargs["expected_sha256"], {"model.safetensors": "0" * 64})
+
+ def test_passing_nothing_leaves_the_build_exactly_as_it_was(self):
+ module, fake, captured = self._fake_agent()
+ with patch.object(module, "Agent", fake):
+ Router().load("english")
+ Router(revision="sha").load("english")
+ self.assertEqual(sorted(captured[0][1]), ["device", "subfolder", "token"])
+ self.assertEqual(sorted(captured[1][1]), ["device", "revision", "subfolder", "token"])
+
+ def test_a_router_value_is_never_shadowed_by_an_agent_kwarg(self):
+ module, fake, captured = self._fake_agent()
+ with patch.object(module, "Agent", fake):
+ Router(device="cuda", revisions={"english": "sha"},
+ agent_kwargs={"lang_temperatures": self.TABLE}).load("english")
+ self.assertEqual(captured[0][1]["device"], "cuda")
+ self.assertEqual(captured[0][1]["revision"], "sha")
+
+ def test_router_owned_names_are_refused(self):
+ owned = ("model_id_or_path", "device", "token", "subfolder", "revision", "hooks",
+ "on_predict_start", "on_predict_end", "hooks_raise", "hooks_concurrent",
+ "hooks_timeout")
+ for name in owned:
+ with self.assertRaises(ValueError) as ctx:
+ Router(agent_kwargs={name: "x"})
+ self.assertIn(name, str(ctx.exception))
+ self.assertIn("Router(...)", str(ctx.exception))
+
+ def test_refusal_happens_at_construction_not_at_the_first_load(self):
+ module, fake, captured = self._fake_agent()
+ with patch.object(module, "Agent", fake):
+ with self.assertRaises(ValueError):
+ Router(agent_kwargs={"device": "cpu"}).load("english")
+ self.assertEqual(captured, [])
+
+ def test_unknown_option_is_refused_with_the_names_that_do_exist(self):
+ with self.assertRaises(ValueError) as ctx:
+ Router(agent_kwargs={"lang_tempertaures": self.TABLE})
+ message = str(ctx.exception)
+ self.assertIn("lang_tempertaures", message)
+ # The typo is refused, and the accepted list carries the spelling the caller meant.
+ self.assertIn("lang_temperatures", message)
+
+ def test_accepted_names_are_read_from_agent_rather_than_copied_here(self):
+ """The anti-drift check: this file must not grow its own list of checkpoint options."""
+ import inspect
+
+ from laya.router import _ROUTER_OWNED_AGENT_ARGS, check_agent_kwargs
+
+ owned = set(_ROUTER_OWNED_AGENT_ARGS)
+ accepted = set(inspect.signature(Agent.__init__).parameters) - {"self"} - owned
+ self.assertTrue(accepted, "expected some Agent options to be reachable")
+ for name in ("fast", "compile", "expected_sha256", "lang_temperatures"):
+ self.assertIn(name, accepted)
+ check_agent_kwargs({name: None for name in accepted})
+
+ def test_the_callers_dict_is_copied_not_aliased(self):
+ module, fake, captured = self._fake_agent()
+ options = {"lang_temperatures": self.TABLE}
+ with patch.object(module, "Agent", fake):
+ router = Router(agent_kwargs=options)
+ options["compile"] = True
+ router.load("english")
+ self.assertNotIn("compile", captured[0][1])
+
+ def test_default_is_an_empty_build(self):
+ self.assertEqual(Router().agent_kwargs, {})
+ self.assertEqual(Router(agent_kwargs={}).agent_kwargs, {})
+
+
+ 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")
+
+ def test_nested_environment_pins_both_checkpoints(self):
+ """The shape a server with two resident checkpoints has to write.
+
+ Before this, `LAYA_SHA256_DIGESTS` was read only by `laya.revisions`, which treats every
+ key as a path -- so a model-named map failed with `cannot verify 'english': no such file`
+ on the *first* checkpoint and the flat map refused the second one.
+ """
+ with self._env({"english": {"model.safetensors": self.digests["english"]},
+ "multilingual": {"model.safetensors": self.digests["multilingual"]}}):
+ router = self._router({})
+ self._assert_past_the_digest_gate(router, "english")
+ self._assert_past_the_digest_gate(router, "multilingual")
+
+ def test_nested_environment_fails_only_the_checkpoint_it_calls_out(self):
+ with self._env({"english": {"model.safetensors": self.digests["english"]},
+ "multilingual": {"model.safetensors": "0" * 64}}):
+ router = self._router({})
+ 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_nested_environment_leaves_an_unnamed_checkpoint_unpinned(self):
+ # `multilingual` is a resident model that the map does not name. It has to be loaded
+ # unverified, not handed the model-named map as if "english" were one of its files.
+ with self._env({"english": {"model.safetensors": self.digests["english"]}}):
+ router = self._router({})
+ self._assert_past_the_digest_gate(router, "english")
+ self._assert_past_the_digest_gate(router, "multilingual")
+
+ def _env(self, per_model):
+ return patch.dict(os.environ, {"LAYA_SHA256_DIGESTS": json.dumps(per_model)})
+
+
+ class RouterEnvDigestTests(unittest.TestCase):
+ """How `Router` reads the two shapes of `LAYA_SHA256_DIGESTS`.
+
+ `serve.build_router()` builds its Router from the environment and nothing else, so the
+ environment is the only channel a container, a compose stack or a `services.laya-serve`
+ host has. These pin down which shape `Router` takes over and which it leaves to
+ `laya.revisions`, without weights: the `Agent` builds are captured, not run.
+ """
+
+ ENGLISH = {"model.safetensors": "a" * 64}
+ MULTI = {"model.safetensors": "b" * 64}
+
+ 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 _env(self, value):
+ return patch.dict(os.environ, {"LAYA_SHA256_DIGESTS": value})
+
+ def test_flat_map_is_left_to_revisions(self):
+ with self._env(json.dumps({"model.safetensors": "a" * 64})):
+ router = Router()
+ self.assertEqual(router.sha256_digests, {})
+ capture, captured = self._capture()
+ with capture:
+ router.load("english")
+ self.assertNotIn("expected_sha256", captured[0])
+
+ def test_nested_map_is_split_per_checkpoint(self):
+ with self._env(json.dumps({"english": self.ENGLISH, "multilingual": self.MULTI})):
+ capture, captured = self._capture()
+ with capture:
+ router = Router()
+ self.assertEqual(sorted(router.sha256_digests),
+ ["english", "multilingual", "typed-decisions"])
+ for name in ("english", "multilingual", "typed-decisions"):
+ router.load(name)
+ self.assertEqual(captured[0]["expected_sha256"], self.ENGLISH)
+ self.assertEqual(captured[1]["expected_sha256"], self.MULTI)
+ # Named by neither the map nor the caller: explicitly unpinned, so `Agent` does not
+ # fall back to an environment value keyed by model names.
+ self.assertEqual(captured[2]["expected_sha256"], {})
+
+ def test_nested_map_keys_are_normalised(self):
+ with self._env(json.dumps({"en": self.ENGLISH})):
+ self.assertEqual(Router().sha256_digests["english"], self.ENGLISH)
+
+ def test_argument_wins_for_the_checkpoint_it_names(self):
+ with self._env(json.dumps({"english": self.ENGLISH, "multilingual": self.MULTI})):
+ router = Router(sha256_digests={"english": {"model.safetensors": "c" * 64}})
+ self.assertEqual(router.sha256_digests["english"], {"model.safetensors": "c" * 64})
+ self.assertEqual(router.sha256_digests["multilingual"], self.MULTI)
+
+ def test_misspelled_key_in_the_environment_fails_at_construction(self):
+ with self._env(json.dumps({"engligh": self.ENGLISH})):
+ with self.assertRaises(ValueError):
+ Router()
+
+ def test_mixed_artifact_and_model_keys_fail_rather_than_guess(self):
+ env = json.dumps({"model.safetensors": "a" * 64, "english": self.ENGLISH})
+ with self._env(env):
+ with self.assertRaises(ValueError) as cm:
+ Router()
+ self.assertIn("LAYA_SHA256_DIGESTS", str(cm.exception))
+
+ def test_unparseable_environment_is_not_this_layers_error(self):
+ # `laya.revisions.verify_digests` owns the message a malformed value gets; a Router
+ # that repeated it would be a second copy of a rule it does not enforce.
+ for value in ("", " ", "{not json", "[]", '"digests"', "{}"):
+ with self._env(value):
+ self.assertEqual(Router().sha256_digests, {}, value)
+
+
if __name__ == "__main__":
unittest.main()