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(router): share concurrent TypeScript model loads

dakjdakdff09084cd31c
diff --git a/laya-ts/src/router.ts b/laya-ts/src/router.ts
index 6153103e..87fccffb 100644
--- a/laya-ts/src/router.ts
+++ b/laya-ts/src/router.ts
@@ -161,6 +161,7 @@ export class Router extends HookRegistry {
loader: AgentLoader | null;
_agents: Map<string, unknown> = new Map();
_order: string[] = []; // least-recently-used first
+ private readonly _loading = new Map<string, Promise<unknown>>();
constructor(opts: RouterOptions = {}) {
super();
@@ -197,39 +198,50 @@ export class Router extends HookRegistry {
this._touch(key);
return this._agents.get(key);
}
- let agent: unknown;
- if (this.loader) {
- agent = await this.loader(key, this.models[key]);
- } else {
- const { Agent } = await import("./agent.js");
- const spec = this.models[key];
- agent = await (Agent as unknown as {
- load(repo: string, opts?: Record<string, unknown>): Promise<unknown>;
- }).load(spec.repo, {
- subfolder: spec.subfolder,
- device: this.device ?? undefined,
- token: this.token ?? undefined,
- });
- }
- this._agents.set(key, agent);
- this._order.push(key);
- const evicted = this._evict();
- // Lifecycle hooks fire after the maps settle, so a hook can safely call the Router.
- for (const victim of evicted) {
+ const loading = this._loading.get(key);
+ if (loading) return loading;
+ // Start in a microtask so even a synchronous loader sees its in-flight entry.
+ const pending = Promise.resolve().then(async () => {
+ let agent: unknown;
+ if (this.loader) {
+ agent = await this.loader(key, this.models[key]);
+ } else {
+ const { Agent } = await import("./agent.js");
+ const spec = this.models[key];
+ agent = await (Agent as unknown as {
+ load(repo: string, opts?: Record<string, unknown>): Promise<unknown>;
+ }).load(spec.repo, {
+ subfolder: spec.subfolder,
+ device: this.device ?? undefined,
+ token: this.token ?? undefined,
+ });
+ }
+ this._agents.set(key, agent);
+ this._order.push(key);
+ const evicted = this._evict();
+ // Lifecycle hooks fire after the maps settle, so a hook can safely call the Router.
+ for (const victim of evicted) {
+ dispatch(
+ composeHooks(this.hooks),
+ "onEvict",
+ new PredictContext({ states: [], questions: {}, model: victim, router: this }),
+ { raiseErrors: this.hooksRaise },
+ );
+ }
dispatch(
composeHooks(this.hooks),
- "onEvict",
- new PredictContext({ states: [], questions: {}, model: victim, router: this }),
+ "onLoad",
+ new PredictContext({ states: [], questions: {}, model: key, agent, router: this }),
{ raiseErrors: this.hooksRaise },
);
+ return agent;
+ });
+ this._loading.set(key, pending);
+ try {
+ return await pending;
+ } finally {
+ this._loading.delete(key);
}
- dispatch(
- composeHooks(this.hooks),
- "onLoad",
- new PredictContext({ states: [], questions: {}, model: key, agent, router: this }),
- { raiseErrors: this.hooksRaise },
- );
- return agent;
}
_touch(key: string): void {
diff --git a/laya-ts/tests/router.test.ts b/laya-ts/tests/router.test.ts
index 929c08ed..3ea49d6f 100644
--- a/laya-ts/tests/router.test.ts
+++ b/laya-ts/tests/router.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
import { Router, normaliseName } from "../src/router.js";
describe("router", () => {
it("normalises aliases", () => { expect(normaliseName("en")).toBe("english"); });
@@ -15,4 +15,39 @@ describe("router", () => {
const r = new Router();
expect(r.route("text", {}, { langGuess: () => "ro" }).model).toBe("multilingual");
});
+ it("shares concurrent loads and emits lifecycle hooks once per cached model", async () => {
+ const events: string[] = [];
+ const loader = vi.fn(async (name: string) => ({ name }));
+ const r = new Router({
+ maxLoaded: 1,
+ loader,
+ hooks: [{
+ onLoad: (ctx) => events.push(`load:${ctx.model}`),
+ onEvict: (ctx) => events.push(`evict:${ctx.model}`),
+ }],
+ });
+
+ const [first, second] = await Promise.all([r.load("en"), r.load("english")]);
+ expect(first).toBe(second);
+ expect(loader).toHaveBeenCalledTimes(1);
+ expect(r.loaded).toEqual(["english"]);
+ expect(r._agents.size).toBe(1);
+ await r.load("multilingual");
+ expect(events).toEqual(["load:english", "evict:english", "load:multilingual"]);
+ expect(r.loaded).toEqual(["multilingual"]);
+ });
+ it("clears an unsuccessful in-flight load so the model can be retried", async () => {
+ const agent = { name: "english" };
+ const loader = vi.fn()
+ .mockRejectedValueOnce(new Error("load failed"))
+ .mockResolvedValue(agent);
+ const r = new Router({ loader });
+
+ const results = await Promise.allSettled([r.load("en"), r.load("english")]);
+ expect(results.map((result) => result.status)).toEqual(["rejected", "rejected"]);
+ expect(loader).toHaveBeenCalledTimes(1);
+ expect(await r.load("english")).toBe(agent);
+ expect(loader).toHaveBeenCalledTimes(2);
+ expect(r.loaded).toEqual(["english"]);
+ });
});