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(ts): name the hook and event in hook timeout errors

Nithilan Kumaran60682bc237f6
diff --git a/laya-ts/src/hooks.ts b/laya-ts/src/hooks.ts
index 412c9f78..58e55fe5 100644
--- a/laya-ts/src/hooks.ts
+++ b/laya-ts/src/hooks.ts
@@ -334,31 +334,74 @@ function reportHookFailure(hook: Hook, event: HookEvent, err: unknown): void {
* `dispatchAsync` waits and applies `raiseErrors` to it.
*/
-/** Run a promise with a timeout, returning void if it completes or throwing on timeout. */
+function isThenable(value: unknown): value is PromiseLike<unknown> {
+ return typeof (value as PromiseLike<unknown> | null | undefined)?.then === "function";
+}
+
+function trimTrailingZeros(decimal: string): string {
+ const dot = decimal.indexOf(".");
+ if (dot < 0) return decimal;
+ let end = decimal.length;
+ while (end > 0 && decimal.charAt(end - 1) === "0") end--;
+ if (end > 0 && decimal.charAt(end - 1) === ".") end--;
+ return decimal.slice(0, end);
+}
+
+/** Seconds the way CPython prints `"%g"`: `exceeded 0.05s` and `exceeded 1s`, not `1.0s`. */
+function formatTimeoutSeconds(timeoutMs: number): string {
+ const value = timeoutMs / 1000;
+ if (!Number.isFinite(value)) return String(value);
+ const scientific = value.toExponential(5);
+ const marker = scientific.indexOf("e");
+ if (marker < 0) return scientific;
+ const exponent = Number(scientific.slice(marker + 1));
+ if (exponent < -4 || exponent >= 6) {
+ const sign = exponent < 0 ? "-" : "+";
+ return (
+ trimTrailingZeros(scientific.slice(0, marker)) +
+ `e${sign}${String(Math.abs(exponent)).padStart(2, "0")}`
+ );
+ }
+ return trimTrailingZeros(value.toFixed(5 - exponent));
+}
+
+/** JS has no `TimeoutError` class; Python raises that builtin, so the name is set to match. */
+function hookTimeoutError(operationName: string, timeoutMs: number): Error {
+ const err = new Error(`laya: hook ${operationName} exceeded ${formatTimeoutSeconds(timeoutMs)}s`);
+ err.name = "TimeoutError";
+ return err;
+}
+
+/**
+ * Await one hook result. The return value is discarded: nothing reads it.
+ *
+ * A non-thenable has already finished, so a configured timeout starts no timer. A thenable is
+ * raced against `timeoutMs`, and the timer is cleared whether the hook settles or the deadline
+ * wins. The deadline matches Python: `TimeoutError`, `laya: hook <Class.event> exceeded <N>s`.
+ */
async function runWithTimeout(
- promise: unknown,
+ result: unknown,
timeoutMs: number | null,
operationName: string,
): Promise<void> {
if (timeoutMs === null || timeoutMs === undefined) {
- await promise;
+ await result;
return;
}
-
- const abortController = new AbortController();
- const timeoutId = setTimeout(() => abortController.abort(), timeoutMs);
-
+ if (!isThenable(result)) return;
+
+ let timer: ReturnType<typeof setTimeout> | undefined;
try {
await Promise.race([
- Promise.resolve(promise),
+ result,
new Promise<never>((_, reject) => {
- abortController.signal.addEventListener("abort", () => {
- reject(new Error(`hook ${operationName} timed out after ${timeoutMs}ms`));
- });
+ timer = setTimeout(() => {
+ reject(hookTimeoutError(operationName, timeoutMs));
+ }, timeoutMs);
}),
]);
} finally {
- clearTimeout(timeoutId);
+ if (timer !== undefined) clearTimeout(timer);
}
}
@@ -368,15 +411,13 @@ export function dispatch(
ctx: PredictContext,
opts: { raiseErrors?: boolean; timeoutMs?: number | null; hook?: Hook } = {},
): void {
- const timeoutMs = opts.timeoutMs ?? null;
- const operationName = `${((opts.hook as object | undefined)?.constructor?.name ?? "hook")}.`;
const raiseErrors = opts.raiseErrors ?? true;
for (const hook of hooks) {
const method = hook?.[event];
if (typeof method !== "function") continue;
try {
const result: unknown = method.call(hook, ctx);
- if (typeof (result as PromiseLike<unknown> | null)?.then === "function") {
+ if (isThenable(result)) {
Promise.resolve(result).catch((err) => reportHookFailure(hook, event, err));
}
} catch (err) {
@@ -395,11 +436,11 @@ export async function dispatchAsync(
opts: { raiseErrors?: boolean; timeoutMs?: number | null; hook?: Hook } = {},
): Promise<void> {
const timeoutMs = opts.timeoutMs ?? null;
- const operationName = `${((opts.hook as object | undefined)?.constructor?.name ?? "hook")}.`;
const raiseErrors = opts.raiseErrors ?? true;
for (const hook of hooks) {
const method = hook?.[event];
if (typeof method !== "function") continue;
+ const operationName = `${(hook as object)?.constructor?.name ?? "hook"}.${event}`;
try {
await runWithTimeout(method.call(hook, ctx), timeoutMs, operationName);
} catch (err) {
diff --git a/laya-ts/src/router.ts b/laya-ts/src/router.ts
index a5cd0e6a..8ecc0c01 100644
--- a/laya-ts/src/router.ts
+++ b/laya-ts/src/router.ts
@@ -157,7 +157,8 @@ export interface RouteOptions {
/**
* Per-call options for `Router.predictBatch` / `predictMany` / `routeBatch`.
* `hooks`, `onPredictStart` and `onPredictEnd` are composed the same way as on `predict`:
- * installed hooks first, then this call. `null`, `undefined` and `[]` add nothing.
+ * `setDefaultHooks` defaults, then installed hooks, then this call's hooks.
+ * `null`, `undefined` and `[]` add nothing.
* `hooksRaise` overrides the Router's policy for this call, including `onRoute`.
* The start and end callables apply to each request's predict lifecycle, not to routing.
*/
@@ -748,7 +749,8 @@ export class Router extends HookRegistry {
* carrying `decision`; `onPredictStart` may rewrite a request or `ctx.skip()` it, and
* `onPredictEnd` runs once per started request even when the batch fails.
* `opts.hooks`, `opts.onPredictStart` and `opts.onPredictEnd` are composed the way `predict`
- * composes them: installed hooks first, then this call. `null` and `[]` add nothing. `hooks`
+ * composes them: `setDefaultHooks` defaults, then installed hooks, then this call's hooks.
+ * `null` and `[]` add nothing. `hooks`
* also run `onRoute`. `opts.hooksRaise` overrides the Router's policy for this call, the way
* `predict` uses `opts.hooksRaise ?? this.hooksRaise`.
*/
diff --git a/laya-ts/tests/hooks.test.ts b/laya-ts/tests/hooks.test.ts
index 7105a063..399af981 100644
--- a/laya-ts/tests/hooks.test.ts
+++ b/laya-ts/tests/hooks.test.ts
@@ -527,3 +527,128 @@ describe("BaseHook and process-wide default hooks (py #276 parity)", () => {
}
});
});
+
+describe("hook timeout", () => {
+ class SlowHook {
+ async onPredictStart(): Promise<void> {
+ await new Promise((resolve) => setTimeout(resolve, 10_000));
+ }
+ }
+
+ /** Settle `pending` without an unhandled rejection if the call fails. */
+ function capture(pending: Promise<unknown>): Promise<unknown> {
+ return pending.then(
+ (value) => value,
+ (err: unknown) => err,
+ );
+ }
+
+ it("names the hook and the event when a hook times out", async () => {
+ vi.useFakeTimers();
+ try {
+ const pending = makeAgent([], { hooks: [new SlowHook()], hooksTimeout: 0.05 }).systemOne("hi", QUESTIONS);
+ const settled = capture(pending);
+ await vi.advanceTimersByTimeAsync(50);
+ const err = await settled;
+ expect(err).toBeInstanceOf(Error);
+ expect((err as Error).name).toBe("TimeoutError");
+ expect((err as Error).message).toBe("laya: hook SlowHook.onPredictStart exceeded 0.05s");
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it("clears the timer when an async hook finishes before the deadline", async () => {
+ vi.useFakeTimers();
+ const armed = vi.spyOn(globalThis, "setTimeout");
+ const cleared = vi.spyOn(globalThis, "clearTimeout");
+ try {
+ class FastHook {
+ async onPredictStart(): Promise<void> {
+ await Promise.resolve();
+ }
+ }
+ const agent = makeAgent([], { hooks: [new FastHook()], hooksTimeout: 1 });
+ await agent.systemOne("hi", QUESTIONS);
+ expect(armed).toHaveBeenCalled();
+ for (const id of armed.mock.results.map((result) => result.value)) {
+ expect(cleared).toHaveBeenCalledWith(id);
+ }
+ expect(vi.getTimerCount()).toBe(0);
+ } finally {
+ armed.mockRestore();
+ cleared.mockRestore();
+ vi.useRealTimers();
+ }
+ });
+
+ it("does not start a timer for a synchronous hook", async () => {
+ vi.useFakeTimers();
+ const armed = vi.spyOn(globalThis, "setTimeout");
+ try {
+ class SyncHook {
+ onPredictStart(): void {}
+ }
+ const agent = makeAgent([], { hooks: [new SyncHook()], hooksTimeout: 0.05 });
+ await agent.systemOne("hi", QUESTIONS);
+ expect(armed).not.toHaveBeenCalled();
+ } finally {
+ armed.mockRestore();
+ vi.useRealTimers();
+ }
+ });
+
+ it("hooksRaise false reports the timeout and continues", async () => {
+ vi.useFakeTimers();
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
+ try {
+ const pending = makeAgent([], {
+ hooks: [new SlowHook()],
+ hooksTimeout: 0.05,
+ hooksRaise: false,
+ }).systemOne("hi", QUESTIONS);
+ await vi.advanceTimersByTimeAsync(50);
+ const r = await pending;
+ expect(r.answers).toHaveProperty("q");
+ expect(warn).toHaveBeenCalledWith(
+ expect.stringContaining("laya: hook SlowHook.onPredictStart exceeded 0.05s"),
+ );
+ } finally {
+ warn.mockRestore();
+ vi.useRealTimers();
+ }
+ });
+
+ it("a per-call hooksTimeout overrides the agent default", async () => {
+ vi.useFakeTimers();
+ try {
+ class BoundedHook {
+ async onPredictStart(): Promise<void> {
+ await new Promise((resolve) => setTimeout(resolve, 200));
+ }
+ }
+
+ const shortCall = capture(
+ makeAgent([], { hooks: [new BoundedHook()], hooksTimeout: 5 }).systemOne("hi", QUESTIONS, {
+ hooksTimeout: 0.05,
+ }),
+ );
+ await vi.advanceTimersByTimeAsync(50);
+ const err = await shortCall;
+ expect(err).toBeInstanceOf(Error);
+ expect((err as Error).name).toBe("TimeoutError");
+ expect((err as Error).message).toBe("laya: hook BoundedHook.onPredictStart exceeded 0.05s");
+
+ const longCall = makeAgent([], { hooks: [new BoundedHook()], hooksTimeout: 0.05 }).systemOne(
+ "hi",
+ QUESTIONS,
+ { hooksTimeout: 5 },
+ );
+ await vi.advanceTimersByTimeAsync(200);
+ const r = await longCall;
+ expect(r.answers).toHaveProperty("q");
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+});