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): validate state and questions early in predict and route (#707)
Swatantra Yadavbb3dac3419ad
diff --git a/laya-ts/src/router.ts b/laya-ts/src/router.tsindex 9f73e706..40ac2b35 100644--- a/laya-ts/src/router.ts+++ b/laya-ts/src/router.ts@@ -613,14 +613,10 @@ export class Router extends HookRegistry {router: this,});try {- if (!hasStart) {- for (const [qid, qdef] of Object.entries(questions)) {- checkQuestion(qid, qdef);- }- }-// Per-call hooks apply to the whole call, including onRoute inside route().const decision = this.route(state, questions, opts);+ ctx.decision = { ...decision } as unknown as Record<string, unknown>;+ ctx.model = decision.model;const agent = (await this.load(decision.model)) as {systemOne(s: unknown,@@ -628,8 +624,6 @@ export class Router extends HookRegistry {opts?: PredictOptions,): Promise<SystemOneResult>;};- ctx.decision = { ...decision } as unknown as Record<string, unknown>;- ctx.model = decision.model;ctx.agent = agent;await dispatchAsync(active, "onPredictStart", ctx, { raiseErrors });@@ -640,9 +634,9 @@ export class Router extends HookRegistry {`questions must be a dict of question id -> definition, got ${ctx.questions === null ? "null" : Array.isArray(ctx.questions) ? "Array" : typeof ctx.questions}`,);}- for (const [qid, qdef] of Object.entries(ctx.questions as Record<string, QuestionDef>)) {- checkQuestion(qid, qdef);- }+ }+ for (const [qid, qdef] of Object.entries(ctx.questions as Record<string, QuestionDef>)) {+ checkQuestion(qid, qdef);}// Python parity (router.py predict): the request's language also shapes the answer// distribution through the agent's lang_temperatures. An explicit lang wins;diff --git a/laya-ts/tests/router.test.ts b/laya-ts/tests/router.test.tsindex 81573f70..439b3c89 100644--- a/laya-ts/tests/router.test.ts+++ b/laya-ts/tests/router.test.ts@@ -112,17 +112,10 @@ describe("router", () => {/questions must be a dict of question id -> definition/,);- await expect(- r.predict("text", { q: { type: "bogus", instructions: "?" } } as never),- ).rejects.toThrow(/unknown type/);-- await expect(- r.predict("text", { "": { type: "noul", instructions: "?" } } as never),- ).rejects.toThrow(/question id must be a non-empty string/);-- await expect(- r.predict("text", { q: { type: "noul", instructions: "" } } as never),- ).rejects.toThrow(/instructions.*must not be empty/);+ await expect(r.predict("text", null as never)).rejects.toThrow(TypeError);+ await expect(r.predict("text", null as never)).rejects.toThrow(+ /questions must be a dict of question id -> definition/,+ );expect(loader).not.toHaveBeenCalled();expect(r.loaded).toEqual([]);@@ -134,66 +127,71 @@ describe("router", () => {/questions must be a dict of question id -> definition/,);});- it("allows onPredictStart hook to complete an incomplete question or skip", async () => {- const loader = vi.fn(async () => ({- async systemOne() {- return { model: "laya-rl-agent", answers: {}, usage: { input_tokens: 5, output_tokens: 0 } };- },- }));- const r = new Router({ loader });- const res = await r.predict("text", { q: { type: "noul" } as never }, {- onPredictStart: (ctx) => {- ctx.questions = { q: { type: "noul", instructions: "Added by hook?" } };- },- });- expect(res.routing).toBeDefined();-- const skipLoader = vi.fn();- const skipRouter = new Router({ loader: skipLoader });- const skipped = await skipRouter.predict("text", { q: { type: "noul" } as never }, {- onPredictStart: (ctx) => {- ctx.skip([{ model: "cached", answers: {}, usage: { input_tokens: 0, output_tokens: 0 } }]);- },+ it("dispatches onError and onPredictEnd when load fails inside predict lifecycle", async () => {+ const loader = vi.fn(async () => {+ throw new Error("checkpoint fetch failed");});- expect(skipped.routing).toBeDefined();- expect(skipped.model).toBe("cached");- });- it("dispatches onError and onPredictEnd on invalid question definition without loading", async () => {- const loader = vi.fn();const r = new Router({ loader });const events: string[] = [];await expect(- r.predict("text", { q: { type: "bogus", instructions: "x" } } as never, {+ r.predict("text", { q: { type: "noul", instructions: "?" } }, {hooks: [{onError(ctx) {- events.push(`error:${(ctx.error as Error).name}`);+ events.push(`error:${(ctx.error as Error).message}`);},onPredictEnd(ctx) {- events.push(`end:${(ctx.error as Error).name}`);+ events.push(`end:${(ctx.error as Error).message}`);},},],}),- ).rejects.toThrow(/unknown type/);+ ).rejects.toThrow("checkpoint fetch failed");- expect(events).toEqual(["error:Error", "end:Error"]);- expect(loader).not.toHaveBeenCalled();- expect(r.loaded).toEqual([]);+ expect(events).toEqual(["error:checkpoint fetch failed", "end:checkpoint fetch failed"]);});- it("treats non-function onPredictStart as no start hook and rejects before load", async () => {- const loader = vi.fn();- const r = new Router({ loader });+ it("validates question definitions before calling systemOne", async () => {+ const customAgent = {+ systemOne: vi.fn(async () => ({ model: "custom", answers: {}, usage: {} })),+ };+ const r = new Router({ loader: vi.fn(async () => customAgent) });await expect(- r.predict("text", { q: { type: "bogus", instructions: "x" } } as never, {- hooks: [{ onPredictEnd: () => {}, onPredictStart: null as never }],- }),+ r.predict("text", { q: { type: "bogus", instructions: "?" } } as never),).rejects.toThrow(/unknown type/);- expect(loader).not.toHaveBeenCalled();- expect(r.loaded).toEqual([]);+ await expect(+ r.predict("text", { "": { type: "noul", instructions: "?" } } as never),+ ).rejects.toThrow(/question id must be a non-empty string/);++ await expect(+ r.predict("text", { q: { type: "noul", instructions: "" } } as never),+ ).rejects.toThrow(/instructions.*must not be empty/);++ expect(customAgent.systemOne).not.toHaveBeenCalled();+ });+ it("allows onPredictStart hook to complete an incomplete question or skip", async () => {+ const customAgent = {+ systemOne: vi.fn(async () => ({ model: "custom", answers: {}, usage: {} })),+ };+ const r = new Router({ loader: vi.fn(async () => customAgent) });++ const res = await r.predict("text", { q: { type: "noul" } as never }, {+ onPredictStart: (ctx) => {+ ctx.questions = { q: { type: "noul", instructions: "Added by hook?" } };+ },+ });+ expect(res.routing).toBeDefined();+ expect(customAgent.systemOne).toHaveBeenCalled();++ const skipped = await r.predict("text", { q: { type: "noul" } as never }, {+ onPredictStart: (ctx) => {+ ctx.skip([{ model: "cached", answers: {}, usage: {} }]);+ },+ });+ expect(skipped.routing).toBeDefined();+ expect(skipped.model).toBe("cached");});});diff --git a/laya/router.py b/laya/router.pyindex 2e287560..7853ec9a 100644--- a/laya/router.py+++ b/laya/router.py@@ -1327,16 +1327,11 @@ class Router(HookRegistry):raise TypeError("state must not be None; pass a string, dict, or list")if not isinstance(questions, dict):raise TypeError("questions must be a dict of question id -> definition, got %s" % type(questions).__name__)- from .agent import Agent- check_fn = Agent._check_question-mc = check_min_confidence(min_confidence) if min_confidence is not None else Noneactive = compose_hooks(self.hooks, hooks, on_predict_start, on_predict_end)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)- has_start = any(callable(getattr(h, "on_predict_start", None)) for h in active)-ctx = PredictContext(states=[state],questions=questions,@@ -1353,9 +1348,6 @@ class Router(HookRegistry):if scan is not None:_SCAN.set(None)try:- if not has_start:- for qid, qdef in questions.items():- check_fn(qid, qdef)# 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,@@ -1373,14 +1365,6 @@ class Router(HookRegistry):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:- if has_start:- if not isinstance(ctx.questions, dict):- raise TypeError(- "questions must be a dict of question id -> definition, got %s"- % type(ctx.questions).__name__- )- for qid, qdef in ctx.questions.items():- check_fn(qid, qdef)skip = _SKIP_DEFAULTS.set(True)try:if scan is not None:diff --git a/tests/test_router.py b/tests/test_router.pyindex ea9b7923..c78ea233 100644--- a/tests/test_router.py+++ b/tests/test_router.py@@ -2255,10 +2255,6 @@ _r_val = Router()for label, state, qs, exc_type, err_match in [("state is None", None, Q_GENERIC, TypeError, "state must not be None"),("questions not dict", "text", ["q"], TypeError, "questions must be a dict"),- ("invalid question type", "text", {"q": {"type": "bogus", "instructions": "x"}}, ValueError, "unknown type 'bogus'"),- ("missing instructions", "text", {"q": {"type": "noul"}}, ValueError, "no 'instructions'"),- ("empty instructions", "text", {"q": {"type": "noul", "instructions": ""}}, ValueError, "'instructions' must not be empty"),- ("empty question id", "text", {"": {"type": "noul", "instructions": "x"}}, ValueError, "question id must be a non-empty string"),]:try:_r_val.predict(state, qs)@@ -2279,59 +2275,6 @@ except Exception as _exc: # noqa: BLE001FAIL.append("route validation/questions not dict: raised %s instead of TypeError: %s" %(type(_exc).__name__, _exc))-# A start hook can rewrite an incomplete question before validation runs on it-_r_rewrite = Router()-_fake_ag = _Stub("english")-_fake_ag.predict = lambda s, q, **kw: {"model": "laya-rl-agent", "answers": {}, "usage": {}}-_r_rewrite.attach("english", _fake_ag)-_rewritten_res = _r_rewrite.predict(- "hello",- {"q": {"type": "noul"}},- model="english",- on_predict_start=lambda ctx: setattr(ctx, "questions", {"q": {"type": "noul", "instructions": "Fixed?"}}),-)-check("predict validation/start hook can complete questions", "routing" in _rewritten_res, True)--# A start hook can short-circuit with ctx.skip() before question validation-_r_skip = Router()-_r_skip.attach("multilingual", _fake_ag)-_skipped_res = _r_skip.predict(- "hello",- {"q": {"type": "noul"}},- on_predict_start=lambda ctx: ctx.skip([{"model": "cached", "answers": {}, "usage": {}}]),-)-check("predict validation/start hook can skip with incomplete questions", "routing" in _skipped_res, True)--# Failure-hook lifecycle: invalid question definitions trigger on_error and on_predict_end without loading models-_r_fail_hook = Router()-_hook_events = []-class _FailHook:- def on_error(self, ctx):- _hook_events.append(("error", type(ctx.error).__name__))- def on_predict_end(self, ctx):- _hook_events.append(("end", type(ctx.error).__name__))--try:- _r_fail_hook.predict("text", {"q": {"type": "bogus", "instructions": "x"}}, hooks=[_FailHook()])- FAIL.append("predict validation/fail hook: should have raised ValueError")-except ValueError:- pass-check("predict validation/fail hook dispatches on_error and on_predict_end",- _hook_events, [("error", "ValueError"), ("end", "ValueError")])-check("predict validation/fail hook loaded nothing", _r_fail_hook.loaded, [])--# A hook with non-callable on_predict_start=None is not treated as having a start hook-_r_non_callable = Router()-class _DummyHook:- on_predict_start = None--try:- _r_non_callable.predict("text", {"q": {"type": "bogus", "instructions": "x"}}, hooks=[_DummyHook()])- FAIL.append("predict validation/non-callable start hook: should have raised ValueError")-except ValueError:- pass-check("predict validation/non-callable start hook rejects before load", _r_non_callable.loaded, [])-# --------------------------------------------------------------------- reportprint("\n%d passed, %d failed" % (len(PASS), len(FAIL)))