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(router): run predict hooks when route() or load() fails
Bruce-Yiib7114f3bfe1c
diff --git a/laya/router.py b/laya/router.pyindex 7dd999eb..09b3196b 100644--- a/laya/router.py+++ b/laya/router.py@@ -575,19 +575,32 @@ class Router(HookRegistry):raise_errors = self.hooks_raise if hooks_raise is None else bool(hooks_raise)timeout = self.hooks_timeout if hooks_timeout is None else validate_timeout(hooks_timeout)- # Per-call hooks apply to the whole call, including on_route inside route().- decision = self.route(state, questions, model=model, task=task, lang=lang,- lang_guess=lang_guess, hooks=hooks, hooks_raise=hooks_raise,- hooks_timeout=hooks_timeout)- agent = self.load(decision["model"])- effective_lang = lang- if effective_lang is None and decision.get("detection") and decision["detection"].get("language"):- effective_lang = decision["detection"]["language"]-- ctx = PredictContext(states=[state], questions=questions, decision=dict(decision),- model=decision["model"], agent=agent, router=self,- max_len=max_len, head_max_len=head_max_len)+ ctx = PredictContext(+ states=[state],+ questions=questions,+ decision=None,+ model=None,+ agent=None,+ router=self,+ max_len=max_len,+ head_max_len=head_max_len,+ )try:+ # Per-call hooks apply to the whole call, including on_route inside route().+ decision = self.route(state, questions, model=model, task=task, lang=lang,+ lang_guess=lang_guess, hooks=hooks, hooks_raise=hooks_raise,+ hooks_timeout=hooks_timeout)+ ctx.decision = dict(decision)+ ctx.model = decision["model"]+ agent = self.load(decision["model"])+ ctx.agent = agent+ effective_lang = lang+ if effective_lang is None and decision.get("detection") and decision["detection"].get("language"):+ effective_lang = decision["detection"]["language"]++ # Keep the existing success-path timing contract: elapsed_ms starts here, after+ # route/load and immediately before the prediction start hook.+ ctx.started_at = time.perf_counter()dispatch(active, "on_predict_start", ctx, raise_errors=raise_errors, lock=self._hooks_lock, timeout=timeout)if ctx.results is None:# Pass token-budget overrides only when set, so any Agent-like object that doesdiff --git a/tests/test_hooks.py b/tests/test_hooks.pyindex 755e69d1..880fa225 100644--- a/tests/test_hooks.py+++ b/tests/test_hooks.py@@ -532,6 +532,51 @@ check("router/predict start sees the decision", predict_seen["decision"]["model"check("router/predict end sees results", len(predict_seen["results"]), 1)check("router/predict keeps routing", out["routing"]["model"], "english")++class PredictSuccessTrace:+ def __init__(self):+ self.events = []+ self.contexts = []++ def on_predict_start(self, ctx):+ self.events.append("start")+ self.contexts.append(ctx)++ def on_predict_end(self, ctx):+ self.events.append("end")+ self.contexts.append(ctx)+++success_trace = PredictSuccessTrace()+r = Router(hooks=[success_trace])+r.attach("english", FakeAgent())+success_out = r.predict("hello", QUESTIONS)+check("router/predict success lifecycle", success_trace.events, ["start", "end"])+check_true("router/predict success shares one context", success_trace.contexts[0] is success_trace.contexts[1])+check("router/predict success shares one run_id", success_trace.contexts[0].run_id,+ success_trace.contexts[1].run_id)+check("router/predict success keeps the normal result", success_out["routing"]["model"], "english")+++class TimedAgent:+ def system_one(self, state, questions, **kwargs):+ return {"model": "timed", "answers": {}, "usage": {"input_tokens": 0, "output_tokens": 0}}+++class TimedRouter(Router):+ def load(self, model):+ time.sleep(0.001)+ self.load_finished = time.perf_counter()+ return TimedAgent()+++timed_trace = PredictSuccessTrace()+timed_router = TimedRouter(hooks=[timed_trace])+timed_router.predict("hello", QUESTIONS)+timed_ctx = timed_trace.contexts[0]+check_true("router/success elapsed starts after load", timed_ctx.started_at >= timed_router.load_finished)++cached = [{"model": "cached", "answers": {}, "usage": {"input_tokens": 0, "output_tokens": 0}}]r = Router()r.attach("english", FakeAgent())@@ -555,6 +600,68 @@ r.predict("hello", QUESTIONS, hooks=[pcr])check("router/per-call hooks apply to on_route", len(pcr.decisions), 1)+class PredictFailureTrace:+ def __init__(self):+ self.events = []+ self.contexts = {}++ def on_predict_start(self, ctx):+ self.events.append("start")++ def on_error(self, ctx):+ self.events.append("error")+ self.contexts["error"] = ctx++ def on_predict_end(self, ctx):+ self.events.append("end")+ self.contexts["end"] = ctx+++class RaiseOnRoute(PredictFailureTrace):+ def on_route(self, ctx):+ raise RuntimeError("route failed")+++route_trace = RaiseOnRoute()+route_error = None+try:+ Router(hooks=[route_trace]).predict("hello", QUESTIONS)+except RuntimeError as exc:+ route_error = exc+check_true("router/route failure propagates", isinstance(route_error, RuntimeError))+check("router/route failure lifecycle", route_trace.events, ["error", "end"])+check_true("router/route failure has no start", "start" not in route_trace.events)+route_ctx = route_trace.contexts.get("error")+check_true("router/route failure records the original error",+ getattr(route_ctx, "error", None) is route_error)+check("router/route failure has no decision", getattr(route_ctx, "decision", "missing"), None)+check("router/route failure has no model", getattr(route_ctx, "model", "missing"), None)+check("router/route failure has no agent", getattr(route_ctx, "agent", "missing"), None)+++class LoadFailRouter(Router):+ def load(self, model):+ raise RuntimeError("load failed")+++load_trace = PredictFailureTrace()+load_error = None+try:+ LoadFailRouter(hooks=[load_trace]).predict("hello", QUESTIONS)+except RuntimeError as exc:+ load_error = exc+check_true("router/load failure propagates", isinstance(load_error, RuntimeError))+check("router/load failure lifecycle", load_trace.events, ["error", "end"])+check_true("router/load failure has no start", "start" not in load_trace.events)+load_ctx = load_trace.contexts.get("error")+check_true("router/load failure records the original error",+ getattr(load_ctx, "error", None) is load_error)+check("router/load failure retains the routed decision",+ (getattr(load_ctx, "decision", None) or {}).get("model"), "english")+check("router/load failure exposes the routed model", getattr(load_ctx, "model", "missing"), "english")+check("router/load failure has no agent", getattr(load_ctx, "agent", "missing"), None)++# --------------------------------------------------------------- Router.predict_batch# `predict_batch` promises each result is what `predict` returns for that request, so Router-level# predict hooks must run per request there too. It used to skip them: a redaction hook never ran,