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(laya-ts): forward maxLen / headMaxLen through Router.predict

thread3dd72fa795846e
diff --git a/laya-ts/src/router.ts b/laya-ts/src/router.ts
index 68d4e2b3..7f696eae 100644
--- a/laya-ts/src/router.ts
+++ b/laya-ts/src/router.ts
@@ -604,6 +604,10 @@ export class Router extends HookRegistry {
const active = composeHooks(this.hooks, opts.hooks, opts.onPredictStart, opts.onPredictEnd);
const raiseErrors = opts.hooksRaise ?? this.hooksRaise;
const hasStart = active.some((h) => typeof h.onPredictStart === "function");
+ // The per-call token-budget overrides seed the context, so a start hook can read and rewrite
+ // them exactly as it can `states`/`questions`; the values it leaves are what reaches the agent
+ // below. Python's Router.predict builds its context the same way (router.py: `PredictContext(
+ // ..., max_len=max_len, head_max_len=head_max_len)`).
const ctx = new PredictContext({
states: [state],
questions: questions as Record<string, unknown>,
@@ -611,6 +615,8 @@ export class Router extends HookRegistry {
model: null,
agent: null,
router: this,
+ maxLen: opts.maxLen ?? null,
+ headMaxLen: opts.headMaxLen ?? null,
});
try {
// Per-call hooks apply to the whole call, including onRoute inside route().
@@ -648,6 +654,10 @@ export class Router extends HookRegistry {
const effectiveLang = opts.lang ?? (detected && detected !== "en" ? detected : null);
const agentOpts: PredictOptions = { lang: effectiveLang };
if (mc !== null) agentOpts.minConfidence = mc;
+ // Forwarded only when set, as Python does: an absent override means "the agent's own
+ // config", and `null`/`undefined` must not overwrite a deployment's configured budget.
+ if (ctx.maxLen !== null && ctx.maxLen !== undefined) agentOpts.maxLen = ctx.maxLen;
+ if (ctx.headMaxLen !== null && ctx.headMaxLen !== undefined) agentOpts.headMaxLen = ctx.headMaxLen;
markDefaultsRan(agentOpts);
const result = (await agent.systemOne(
ctx.states[0],
diff --git a/laya-ts/tests/maxlen.test.ts b/laya-ts/tests/maxlen.test.ts
index 1be38e8f..3d480f70 100644
--- a/laya-ts/tests/maxlen.test.ts
+++ b/laya-ts/tests/maxlen.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import { maxOf } from "../src/common.js";
+import { Router } from "../src/router.js";
describe("maxOf", () => {
it("returns max without spread overflow", () => {
@@ -12,3 +13,59 @@ describe("maxOf", () => {
expect(maxOf(lens, 1)).toBe(77);
});
});
+
+// ------------------------------------------------------------------ Router.predict forwarding
+//
+// `PredictOptions` documents per-call token-budget overrides and `Agent.systemOne` honours them,
+// but `Router.predict` never read them: the context was built without them and `agentOpts` carried
+// only `lang`/`minConfidence`, so the primary entry point silently used the checkpoint default. A
+// start hook's `ctx.maxLen` was dropped the same way. Python seeds `ctx.max_len` and forwards
+// `ctx.max_len`/`ctx.head_max_len` when set (`laya/router.py`), which is what these pin.
+describe("Router.predict token budgets", () => {
+ const Q = { q: { type: "noul", instructions: "?" } } as any;
+
+ function recordingRouter(seen: any[]) {
+ const stub = {
+ async systemOne(_state: unknown, _q: unknown, opts?: any) {
+ seen.push(opts ?? {});
+ return { model: "m", answers: {}, usage: { input_tokens: 1, output_tokens: 0 } };
+ },
+ };
+ return new Router({ default: "english", loader: () => stub } as any);
+ }
+
+ it("forwards maxLen and headMaxLen to the agent", async () => {
+ const seen: any[] = [];
+ const r = recordingRouter(seen);
+ await r.predict("please refund my ticket", Q, { maxLen: 64, headMaxLen: 100 });
+ expect(seen[0].maxLen).toBe(64);
+ expect(seen[0].headMaxLen).toBe(100);
+ });
+
+ it("leaves both unset when the caller did not ask, so the agent config applies", async () => {
+ const seen: any[] = [];
+ const r = recordingRouter(seen);
+ await r.predict("please refund my ticket", Q);
+ expect("maxLen" in seen[0]).toBe(false);
+ expect("headMaxLen" in seen[0]).toBe(false);
+ });
+
+ it("lets a start hook set the budget, the way it can rewrite state and questions", async () => {
+ const seen: any[] = [];
+ const r = recordingRouter(seen);
+ await r.predict("please refund my ticket", Q, {
+ onPredictStart(ctx) {
+ ctx.maxLen = 48;
+ },
+ });
+ expect(seen[0].maxLen).toBe(48);
+ });
+
+ it("forwards no budget when a start hook leaves the fields alone", async () => {
+ // An absent override must not become an explicit one just because a hook ran.
+ const seen: any[] = [];
+ const r = recordingRouter(seen);
+ await r.predict("please refund my ticket", Q, { onPredictStart() {} });
+ expect("maxLen" in seen[0]).toBe(false);
+ });
+});