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.

feat(ts): per-checkpoint SHA-256 artifact verification in Router

Swatantra Yadavb48756d0e9cf
diff --git a/laya-ts/src/index.ts b/laya-ts/src/index.ts
index 38e105a3..b07f39e9 100644
--- a/laya-ts/src/index.ts
+++ b/laya-ts/src/index.ts
@@ -15,8 +15,8 @@ export type {
} from "./agent.js";
export { createNodeProvider, createWebProvider, feed, feedHead, loadNodeBundle, loadWebBundle, PINNED_REVISIONS, resolveRevision } from "./providers.js";
export type { Batch, SessionProvider, ProviderOptions, NodeBundle, WebBundle } from "./providers.js";
-export { Router, normaliseName, DEFAULT_MODELS } from "./router.js";
-export type { RoutedResult, RouteDecision, ModelName, ModelSpec } from "./router.js";
+export { Router, normaliseName, DEFAULT_MODELS, digestsFromEnv, _digestsFromEnv } from "./router.js";
+export type { RoutedResult, RouteDecision, ModelName, ModelSpec, RouterOptions, RouteOptions } from "./router.js";
export { shortlistChoice, predictShortlist, embedFnFromAgent, DEFAULT_SHORTLIST_K } from "./shortlist.js";
export type { EmbedFn, ShortlistMeta } from "./shortlist.js";
export { analyse, isEnglish, guessLatinLanguage, detectScript } from "./lang.js";
diff --git a/laya-ts/src/router.ts b/laya-ts/src/router.ts
index 33ded388..121f22dc 100644
--- a/laya-ts/src/router.ts
+++ b/laya-ts/src/router.ts
@@ -132,6 +132,10 @@ export interface RouterOptions {
revision?: string | null;
/** Per-model revision overrides, keyed by model name or alias. */
revisions?: Record<string, string | null>;
+ /** Per-model artifact SHA-256 digest maps, keyed by model name or alias. */
+ sha256Digests?: Record<string, Record<string, string> | null> | null;
+ /** Python-parity snake_case alias for `sha256Digests`. */
+ sha256_digests?: Record<string, Record<string, string> | null> | null;
hooks?: HookArg;
onPredictStart?: PredictHook;
onPredictEnd?: PredictHook;
@@ -148,6 +152,47 @@ export interface RouteOptions {
hooksRaise?: boolean;
}
+/** Turn `LAYA_SHA256_DIGESTS` into per-checkpoint digest maps when it names models. */
+export function digestsFromEnv(
+ models: Record<string, unknown>,
+): Record<string, Record<string, string> | null> {
+ if (typeof process === "undefined" || !process.env) return {};
+ const raw = (process.env["LAYA_SHA256_DIGESTS"] ?? "").trim();
+ if (!raw) return {};
+ let data: unknown;
+ try {
+ data = JSON.parse(raw);
+ } catch {
+ return {};
+ }
+ if (typeof data !== "object" || data === null || Array.isArray(data)) return {};
+ const entries = Object.entries(data);
+ if (entries.length === 0) return {};
+ const values = entries.map(([, v]) => v);
+ if (values.every((v) => typeof v === "string")) {
+ return {}; // flat: providers already applies it
+ }
+ if (!values.every((v) => typeof v === "object" && v !== null && !Array.isArray(v))) {
+ throw new Error(
+ `LAYA_SHA256_DIGESTS must be either {artifact: digest} for every checkpoint or {model: {artifact: digest}} per checkpoint; ${JSON.stringify(Object.keys(data).sort())} mixes the two or holds a value that is neither`,
+ );
+ }
+ const perModel: Record<string, Record<string, string> | null> = {};
+ for (const [k, v] of entries) {
+ perModel[normaliseName(k)] = v as Record<string, string>;
+ }
+ for (const name of Object.keys(models)) {
+ const norm = normaliseName(name);
+ if (!(norm in perModel)) {
+ perModel[norm] = {};
+ }
+ }
+ return perModel;
+}
+
+export const _digestsFromEnv = digestsFromEnv;
+export const _digests_from_env = digestsFromEnv;
+
function toSpec(spec: string | ModelSpec | [string, string | null]): ModelSpec {
if (typeof spec === "string") return { repo: spec, subfolder: null };
if (Array.isArray(spec)) {
@@ -168,6 +213,10 @@ export class Router extends HookRegistry {
token: string | null | undefined;
revision: string | null;
revisions: Partial<Record<ModelName, string | null>>;
+ sha256Digests: Record<string, Record<string, string> | null>;
+ get sha256_digests(): Record<string, Record<string, string> | null> {
+ return this.sha256Digests;
+ }
maxLoaded: number;
default: ModelName;
autoTaskDetection: boolean;
@@ -200,6 +249,17 @@ export class Router extends HookRegistry {
this.revisions = Object.fromEntries(
Object.entries(opts.revisions ?? {}).map(([name, value]) => [normaliseName(name), value]),
) as Partial<Record<ModelName, string | null>>;
+ // Per checkpoint SHA-256 map: seeded from a model-named LAYA_SHA256_DIGESTS, then
+ // overridden checkpoint by checkpoint by the argument. Keyed and normalised exactly like
+ // revisions, so a misspelled model name fails here rather than leaving that checkpoint
+ // unverified.
+ this.sha256Digests = digestsFromEnv(this.models);
+ const rawDigests = opts.sha256Digests ?? opts.sha256_digests;
+ if (rawDigests) {
+ for (const [name, val] of Object.entries(rawDigests)) {
+ this.sha256Digests[normaliseName(name)] = val;
+ }
+ }
this.maxLoaded = Math.max(1, Math.trunc(Number(opts.maxLoaded ?? opts.max_loaded ?? 2)));
this.default = normaliseName(opts.default ?? "english");
this.autoTaskDetection = Boolean(opts.autoTaskDetection ?? opts.auto_task_detection ?? false);
@@ -237,6 +297,9 @@ export class Router extends HookRegistry {
token: this.token ?? undefined,
};
if (revision) opts.revision = revision;
+ if (Object.prototype.hasOwnProperty.call(this.sha256Digests, key)) {
+ opts.expectedSha256 = this.sha256Digests[key] ?? {};
+ }
agent = await (Agent as unknown as {
load(repo: string, opts?: Record<string, unknown>): Promise<unknown>;
}).load(spec.repo, opts);
diff --git a/laya-ts/tests/revision-pinning.test.ts b/laya-ts/tests/revision-pinning.test.ts
index 367704fd..3992857a 100644
--- a/laya-ts/tests/revision-pinning.test.ts
+++ b/laya-ts/tests/revision-pinning.test.ts
@@ -8,7 +8,7 @@ import { Router } from "../src/router.js";
import { PINNED_REVISIONS, createNodeProvider, loadNodeBundle, loadWebBundle, resolveRevision } from "../src/providers.js";
const fakeProvider = () => ({
- async runEncoder(_b: any) { return { lastHidden: [[1, 0], [0, 1]] }; },
+ async runEncoder(_b: any) { return { lastHidden: [[[1, 0], [0, 1]]] }; },
async runHead(_h: any) { return { logits: [[2, 0]], act: [[3, 0]] }; },
});
@@ -185,3 +185,155 @@ describe("revision plumbing", () => {
expect(new Router().revisions).toEqual({});
});
});
+
+describe("Router digest tests", () => {
+ it("Router normalises digest keys like revision keys", () => {
+ const router = new Router({
+ sha256Digests: { ml: { "w.bin": "a".repeat(64) }, typed: null },
+ });
+ expect(Object.keys(router.sha256Digests).sort()).toEqual(["multilingual", "typed-decisions"]);
+ expect(router.sha256_digests).toBe(router.sha256Digests);
+ expect(new Router().sha256Digests).toEqual({});
+ });
+
+ it("snake_case sha256_digests alias is accepted", () => {
+ const router = new Router({
+ sha256_digests: { en: { "w.bin": "a".repeat(64) } },
+ });
+ expect(router.sha256Digests.english).toEqual({ "w.bin": "a".repeat(64) });
+ });
+
+ it("misspelled model fails at construction", () => {
+ expect(() => new Router({ sha256Digests: { engligh: { "w.bin": "a".repeat(64) } } }))
+ .toThrow(/unknown model "engligh"/);
+ });
+
+ it("each checkpoint gets its own expectedSha256 map upon load", async () => {
+ const captured: Array<{ repo: string; opts: Record<string, unknown> | undefined }> = [];
+ const spy = vi.spyOn(Agent, "load").mockImplementation(async (repo, opts) => {
+ captured.push({ repo, opts });
+ return new Agent({ provider: fakeProvider(), cfg: {} });
+ });
+ try {
+ const router = new Router({
+ sha256Digests: {
+ english: { "model.safetensors": "a".repeat(64) },
+ multilingual: { "model.safetensors": "b".repeat(64) },
+ },
+ });
+ await router.load("english");
+ await router.load("multilingual");
+ expect(captured[0].opts?.expectedSha256).toEqual({ "model.safetensors": "a".repeat(64) });
+ expect(captured[1].opts?.expectedSha256).toEqual({ "model.safetensors": "b".repeat(64) });
+ } finally {
+ spy.mockRestore();
+ }
+ });
+
+ it("unlisted checkpoint is left without expectedSha256", async () => {
+ const captured: Array<{ repo: string; opts: Record<string, unknown> | undefined }> = [];
+ const spy = vi.spyOn(Agent, "load").mockImplementation(async (repo, opts) => {
+ captured.push({ repo, opts });
+ return new Agent({ provider: fakeProvider(), cfg: {} });
+ });
+ try {
+ const router = new Router({
+ sha256Digests: { multilingual: { "model.safetensors": "b".repeat(64) } },
+ });
+ await router.load("english");
+ expect(captured[0].opts?.expectedSha256).toBeUndefined();
+ } finally {
+ spy.mockRestore();
+ }
+ });
+
+ it("none/null entry passes an empty map to mask environment defaults", async () => {
+ const captured: Array<{ repo: string; opts: Record<string, unknown> | undefined }> = [];
+ const spy = vi.spyOn(Agent, "load").mockImplementation(async (repo, opts) => {
+ captured.push({ repo, opts });
+ return new Agent({ provider: fakeProvider(), cfg: {} });
+ });
+ try {
+ const router = new Router({
+ sha256Digests: { english: null },
+ });
+ await router.load("english");
+ expect(captured[0].opts?.expectedSha256).toEqual({});
+ } finally {
+ spy.mockRestore();
+ }
+ });
+});
+
+describe("Router env digest tests", () => {
+ const origEnv = process.env["LAYA_SHA256_DIGESTS"];
+
+ afterEach(() => {
+ if (origEnv === undefined) {
+ delete process.env["LAYA_SHA256_DIGESTS"];
+ } else {
+ process.env["LAYA_SHA256_DIGESTS"] = origEnv;
+ }
+ });
+
+ it("flat map is left to underlying provider", () => {
+ process.env["LAYA_SHA256_DIGESTS"] = JSON.stringify({ "model.safetensors": "a".repeat(64) });
+ const router = new Router();
+ expect(router.sha256Digests).toEqual({});
+ });
+
+ it("nested map is split per checkpoint and unlisted models default to {}", () => {
+ const engDigests = { "model.safetensors": "a".repeat(64) };
+ const multiDigests = { "model.safetensors": "b".repeat(64) };
+ process.env["LAYA_SHA256_DIGESTS"] = JSON.stringify({
+ english: engDigests,
+ multilingual: multiDigests,
+ });
+ const router = new Router();
+ expect(Object.keys(router.sha256Digests).sort()).toEqual(["english", "multilingual", "typed-decisions"]);
+ expect(router.sha256Digests.english).toEqual(engDigests);
+ expect(router.sha256Digests.multilingual).toEqual(multiDigests);
+ expect(router.sha256Digests["typed-decisions"]).toEqual({});
+ });
+
+ it("nested map keys are normalised", () => {
+ const engDigests = { "model.safetensors": "a".repeat(64) };
+ process.env["LAYA_SHA256_DIGESTS"] = JSON.stringify({ en: engDigests });
+ const router = new Router();
+ expect(router.sha256Digests.english).toEqual(engDigests);
+ });
+
+ it("constructor argument wins for the checkpoint it names", () => {
+ const engDigests = { "model.safetensors": "a".repeat(64) };
+ const multiDigests = { "model.safetensors": "b".repeat(64) };
+ process.env["LAYA_SHA256_DIGESTS"] = JSON.stringify({
+ english: engDigests,
+ multilingual: multiDigests,
+ });
+ const router = new Router({
+ sha256Digests: { english: { "model.safetensors": "c".repeat(64) } },
+ });
+ expect(router.sha256Digests.english).toEqual({ "model.safetensors": "c".repeat(64) });
+ expect(router.sha256Digests.multilingual).toEqual(multiDigests);
+ });
+
+ it("misspelled key in environment fails at construction", () => {
+ process.env["LAYA_SHA256_DIGESTS"] = JSON.stringify({ engligh: { "w.bin": "a".repeat(64) } });
+ expect(() => new Router()).toThrow(/unknown model "engligh"/);
+ });
+
+ it("mixed artifact and model keys fail rather than guess", () => {
+ process.env["LAYA_SHA256_DIGESTS"] = JSON.stringify({
+ "model.safetensors": "a".repeat(64),
+ english: { "model.safetensors": "a".repeat(64) },
+ });
+ expect(() => new Router()).toThrow(/LAYA_SHA256_DIGESTS/);
+ });
+
+ it("unparseable environment returns empty without throwing", () => {
+ for (const val of ["", " ", "{not json", "[]", '"digests"', "{}"]) {
+ process.env["LAYA_SHA256_DIGESTS"] = val;
+ expect(new Router().sha256Digests).toEqual({});
+ }
+ });
+});