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(hooks): accept sequences in hooks_installed and enforce in API contract

Swatantra Yadavc45ac0cb70e2
diff --git a/laya/hooks.py b/laya/hooks.py
index f2e3696e..bea7a60c 100644
--- a/laya/hooks.py
+++ b/laya/hooks.py
@@ -390,8 +390,11 @@ class HookRegistry:
with agent.hooks_installed(tracer):
agent.system_one(state, questions)
+
+ Each argument is one hook or a sequence of them, matching `add_hook` and the
+ `hooks=` parameter, so `hooks_installed([tracer, meter])` works too.
"""
- added = normalise_hooks(list(hooks))
+ added = normalise_hooks([hook for arg in hooks for hook in _as_sequence(arg)])
self._extend_hooks(added)
try:
yield self
diff --git a/tests/test_hooks.py b/tests/test_hooks.py
index 7a9b6fb6..97d7c0a3 100644
--- a/tests/test_hooks.py
+++ b/tests/test_hooks.py
@@ -429,6 +429,21 @@ log.clear()
f.predict_batch(["s0"], QUESTIONS)
check("hooks_installed/add_hook inside: still fires after the block", log, ["added-inside"])
+# sequences are flattened, matching add_hook and the hooks= parameter
+log = []
+f = make_fake()
+with f.hooks_installed([Tag(log, "x"), Tag(log, "y")]):
+ f.predict_batch(["s0"], QUESTIONS)
+f.predict_batch(["s0"], QUESTIONS)
+check("hooks_installed/list of hooks fires during the block", log, ["x", "y"])
+check("hooks_installed/list of hooks removed after the block", len(f.hooks), 0)
+
+log = []
+f = make_fake()
+with f.hooks_installed((Tag(log, "a"),), Tag(log, "b")):
+ f.predict_batch(["s0"], QUESTIONS)
+check("hooks_installed/tuple arg and varargs mix in order", log, ["a", "b"])
+
# and the same hook passed twice is the same case with no pre-install at all
log = []
f = make_fake()
diff --git a/tests/test_hooks_api.py b/tests/test_hooks_api.py
index 2dc747d6..54f75589 100644
--- a/tests/test_hooks_api.py
+++ b/tests/test_hooks_api.py
@@ -222,6 +222,21 @@ for label, cls in (("Agent", Agent), ("Router", Router), ("ONNXAgent", ONNXAgent
for method in ("add_hook", "remove_hook", "hooks_installed"):
check_true("%s/%s exists" % (label, method), callable(getattr(cls, method, None)))
+
+class _ApiProbeHook(BaseHook):
+ pass
+
+
+for label, cls in (("Agent", Agent), ("Router", Router), ("ONNXAgent", ONNXAgent)):
+ reg = cls.__new__(cls)
+ reg.hooks = ()
+ h1, h2 = _ApiProbeHook(), _ApiProbeHook()
+ with reg.hooks_installed([h1, h2]):
+ check("%s/hooks_installed accepts a list" % label, tuple(reg.hooks), (h1, h2))
+ check("%s/hooks_installed restores hooks after list block" % label, tuple(reg.hooks), ())
+ with reg.hooks_installed((h1,), h2):
+ check("%s/hooks_installed accepts mixed sequence and vararg" % label, tuple(reg.hooks), (h1, h2))
+
# The LangChain runnables batch: laya.integrations.langchain's own suite checks what
# batch() returns, so these lines pin only the caller-visible shape. A rename, or losing
# the keyword-only return_exceptions, would break LCEL and LangGraph map-reduce silently.