@plasius/gpu-model-runtime 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,8 +7,21 @@ This repository is the dedicated package boundary defined by ADR 0094.
7
7
 
8
8
  The runtime owns orchestration only. Canonical model documents, diagnostics, and
9
9
  renderer-specific data remain owned by the corresponding `@plasius/gpu-model-*`
10
- packages and are carried through generic type parameters until the core package
11
- is released.
10
+ packages. Published `@plasius/gpu-model-core@0.6.0` supplies source, provenance,
11
+ diagnostic-mode and canonical document types; runtime `ModelSource` adds the
12
+ optional package-resource descriptor. Existing explicit generic model types
13
+ remain supported. `ModelAdapter`, `AdapterLoadResult` and `ModelLoadResult` now
14
+ have `GpuModelDocument` as their optional type default. `loadModel<T>()` remains
15
+ explicitly generic; an unannotated call still returns an unknown model.
16
+
17
+ Core imports in runtime source are type-only, so importing this runtime does not
18
+ execute core validators or eagerly load concrete adapters. Adapters own canonical
19
+ validation and strict/tolerant/forensic policy through the core factories and
20
+ evaluator. Runtime returns the resulting document and diagnostic ledger without
21
+ rewriting them. Its `ModelAdapter` is an acquisition/worker port, distinct from
22
+ core's inspect/load/export/validate conversion interface: bind a format adapter's
23
+ factory to the provided resource resolver and map its result in that port;
24
+ do not cast a format module directly to a runtime adapter.
12
25
 
13
26
  ## Runtime behavior
14
27
 
@@ -25,23 +38,28 @@ is released.
25
38
  statuses, abort signals, optional byte ranges, response content types, and
26
39
  SHA-256 integrity checks.
27
40
  - `MemoryModelCache` keys loaded output by content hash, adapter format, and
28
- stable adapter options and acquisition byte ceiling. Call `runtime.invalidate()` to clear all entries or
29
- entries for one content hash.
41
+ stable adapter options, acquisition byte ceiling, diagnostic mode and actual
42
+ execution path. Call `runtime.invalidate()` to clear all entries or entries
43
+ for one content hash.
30
44
  - Worker execution is opt-in through a `WorkerDispatcher`; consumers that do
31
45
  not provide one stay on the main execution path.
32
46
 
33
47
  Example:
34
48
 
35
49
  ```ts
50
+ import { AdapterRegistry, ModelRuntime, type GpuModelDocument } from "@plasius/gpu-model-runtime";
51
+
36
52
  const registry = new AdapterRegistry().register({
37
53
  formatId: "gltf",
38
54
  extensions: ["gltf", "glb"],
39
55
  sniff: ({ bytes }) => bytes[0] === 0x67,
40
- load: () => import("@plasius/gpu-model-gltf"),
56
+ // A consumer-owned bridge implementing this runtime's ModelAdapter port.
57
+ // Its factory binds the format adapter to input.resourceResolver.
58
+ load: () => import("./model-adapters/gltf.js"),
41
59
  });
42
60
 
43
61
  const runtime = new ModelRuntime({ registry });
44
- const result = await runtime.loadModel({
62
+ const result = await runtime.loadModel<GpuModelDocument>({
45
63
  kind: "blob-storage-url",
46
64
  url: "https://storage.example.invalid/models/scene.glb?<signed-url>",
47
65
  });
@@ -50,6 +68,22 @@ result.canonicalModel;
50
68
  result.rendererReady;
51
69
  ```
52
70
 
71
+ A tolerant or forensic cache entry cannot satisfy a strict request. Mandatory
72
+ worker support is checked before cache lookup; main and worker results have
73
+ separate keys, while `auto` and `always` can share a result when both select the
74
+ worker. Adapters and dispatchers injected by the consumer are trusted execution
75
+ ports; they must honor signals, enforce their own parsing/decoding budgets, and
76
+ return core-validated documents. This package does not supply a Worker script or
77
+ serialize resolver functions across threads. A dispatcher binds its own RPC and
78
+ resource-resolution transport.
79
+
80
+ New cache keys intentionally cold-start existing external caches. This is a
81
+ patch-compatible policy fix with additive canonical type defaults and one exact
82
+ published dependency (core 0.6.0, previously absent); no concrete adapter is added.
83
+ The new dependency has no runtime dependencies and requires the already-supported
84
+ Node 24. Installed package and strict ESM/CJS declaration checks verify the
85
+ published boundary. Do not use local workspace links.
86
+
53
87
  ## Bounded source acquisition
54
88
 
55
89
  Every entrypoint and related resource has a default ceiling of 64 MiB
package/dist/index.cjs CHANGED
@@ -472,7 +472,23 @@ var ModelRuntime = class {
472
472
  fileName: input.fileName,
473
473
  sourceKind: source.kind
474
474
  }, options.formatId);
475
- const cacheKey = createModelCacheKey(input.contentHash, adapter.formatId, { adapterOptions: options.adapterOptions ?? null, maxBytes: byteLimit(options.fetchPolicy?.maxBytes) });
475
+ const workerMode = options.worker?.mode ?? "auto";
476
+ const canUseWorker = workerMode !== "never" && Boolean(this.workerDispatcher) && Boolean(adapter.supportsWorker);
477
+ if (workerMode === "always" && !canUseWorker) {
478
+ throw new Error(`Adapter ${adapter.formatId} does not support the requested worker execution`);
479
+ }
480
+ const context = {
481
+ signal: options.signal ?? new AbortController().signal,
482
+ mode: options.mode ?? "strict",
483
+ execution: canUseWorker ? "worker" : "main",
484
+ adapterOptions: options.adapterOptions ?? null
485
+ };
486
+ const cacheKey = createModelCacheKey(input.contentHash, adapter.formatId, {
487
+ adapterOptions: context.adapterOptions,
488
+ maxBytes: byteLimit(options.fetchPolicy?.maxBytes),
489
+ mode: context.mode,
490
+ execution: context.execution
491
+ });
476
492
  const useCache = options.useCache ?? true;
477
493
  if (useCache) {
478
494
  const cached = await this.cache.get(cacheKey);
@@ -480,13 +496,7 @@ var ModelRuntime = class {
480
496
  return { ...cached, fromCache: true };
481
497
  }
482
498
  }
483
- const context = {
484
- signal: options.signal ?? new AbortController().signal,
485
- mode: options.mode ?? "strict",
486
- execution: "main",
487
- adapterOptions: options.adapterOptions ?? null
488
- };
489
- const adapterResult = await this.loadWithExecution(adapter, input, context, options);
499
+ const adapterResult = await this.loadWithExecution(adapter, input, context);
490
500
  const result = {
491
501
  ...adapterResult,
492
502
  contentHash: input.contentHash,
@@ -501,13 +511,8 @@ var ModelRuntime = class {
501
511
  }
502
512
  return result;
503
513
  }
504
- async loadWithExecution(adapter, input, context, options) {
505
- const workerMode = options.worker?.mode ?? "auto";
506
- const canUseWorker = workerMode !== "never" && Boolean(this.workerDispatcher) && Boolean(adapter.supportsWorker);
507
- if (workerMode === "always" && !canUseWorker) {
508
- throw new Error(`Adapter ${adapter.formatId} does not support the requested worker execution`);
509
- }
510
- if (canUseWorker && this.workerDispatcher) {
514
+ async loadWithExecution(adapter, input, context) {
515
+ if (context.execution === "worker" && this.workerDispatcher) {
511
516
  const workerRequest = {
512
517
  formatId: adapter.formatId,
513
518
  input,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/cache.ts","../src/types.ts","../src/bytes.ts","../src/fetch.ts","../src/runtime.ts","../src/model-residency.ts"],"sourcesContent":["export * from \"./cache.js\";\nexport * from \"./fetch.js\";\nexport * from \"./runtime.js\";\nexport * from \"./types.js\";\n\n/** Package identity and rollout metadata for the runtime boundary. */\nexport const packageName = \"@plasius/gpu-model-runtime\" as const;\n\nexport const packageBootstrap = Object.freeze({\n packageName,\n featureFlag: \"gpu.model.conversion.enabled\",\n status: \"runtime\",\n} as const);\n\nexport * from \"./model-residency.js\";\n\nexport { ModelResourceTooLargeError } from \"./bytes.js\";\n","import type { ModelCache } from \"./types.js\";\n\nexport class MemoryModelCache implements ModelCache {\n private readonly entries = new Map<string, unknown>();\n\n async get<T>(key: string): Promise<T | undefined> {\n return this.entries.get(key) as T | undefined;\n }\n\n async set<T>(key: string, value: T): Promise<void> {\n this.entries.set(key, value);\n }\n\n async delete(key: string): Promise<boolean> {\n return this.entries.delete(key);\n }\n\n async clear(): Promise<void> {\n this.entries.clear();\n }\n}\n\nfunction stableValue(value: unknown): unknown {\n if (Array.isArray(value)) {\n return value.map(stableValue);\n }\n\n if (value !== null && typeof value === \"object\") {\n return Object.fromEntries(\n Object.entries(value)\n .sort(([left], [right]) => left.localeCompare(right))\n .map(([key, entry]) => [key, stableValue(entry)]),\n );\n }\n\n return value;\n}\n\nexport function createModelCacheKey(\n contentHash: string,\n formatId: string,\n adapterOptions: unknown,\n): string {\n return `${formatId}:${contentHash}:${JSON.stringify(stableValue(adapterOptions ?? null))}`;\n}\n","/** Default ceiling per entrypoint or related resource: 64 MiB. */\nexport const DEFAULT_MODEL_MAX_BYTES = 64 * 1024 * 1024;\n\nexport type ModelSourceKind =\n | \"file-path\"\n | \"url\"\n | \"blob-storage-url\"\n | \"blob\"\n | \"array-buffer\"\n | \"uint8-array\"\n | \"stream\";\n\nexport type ModelProvenance = Readonly<{\n sourceId?: string;\n sourceSystem?: string;\n acquiredAt?: string;\n licenseHint?: string;\n}>;\n\nexport type ModelResourceInput = Readonly<{\n bytes: ArrayBuffer | Blob | Uint8Array;\n contentType?: string;\n integrity?: string;\n}>;\n\nexport type ModelPackage = Readonly<{\n entrypoint?: string;\n baseUrl?: string;\n resources?: Readonly<Record<string, ModelResourceInput>>;\n}>;\n\nexport type ModelSource =\n | Readonly<{\n kind: \"file-path\";\n path: string;\n mimeTypeHint?: string;\n fileNameHint?: string;\n package?: ModelPackage;\n provenance?: ModelProvenance;\n }>\n | Readonly<{\n kind: \"url\";\n url: string;\n headers?: Readonly<Record<string, string>>;\n mimeTypeHint?: string;\n package?: ModelPackage;\n provenance?: ModelProvenance;\n }>\n | Readonly<{\n kind: \"blob-storage-url\";\n url: string;\n credentialMode?: \"anonymous\" | \"signed\" | \"runtime-resolved\";\n mimeTypeHint?: string;\n package?: ModelPackage;\n provenance?: ModelProvenance;\n }>\n | Readonly<{\n kind: \"blob\";\n blob: Blob;\n fileNameHint?: string;\n mimeTypeHint?: string;\n package?: ModelPackage;\n provenance?: ModelProvenance;\n }>\n | Readonly<{\n kind: \"array-buffer\";\n bytes: ArrayBuffer;\n fileNameHint?: string;\n mimeTypeHint?: string;\n package?: ModelPackage;\n provenance?: ModelProvenance;\n }>\n | Readonly<{\n kind: \"uint8-array\";\n bytes: Uint8Array;\n fileNameHint?: string;\n mimeTypeHint?: string;\n package?: ModelPackage;\n provenance?: ModelProvenance;\n }>\n | Readonly<{\n kind: \"stream\";\n stream: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array>;\n fileNameHint?: string;\n mimeTypeHint?: string;\n byteLengthHint?: number;\n package?: ModelPackage;\n provenance?: ModelProvenance;\n }>;\n\nexport type ResourceReference = Readonly<{\n path: string;\n contentType?: string;\n integrity?: string;\n}>;\n\nexport type ResolvedResource = Readonly<{\n path: string;\n bytes: Uint8Array;\n contentType?: string;\n contentHash: string;\n rangeSupported: boolean;\n}>;\n\nexport interface ResourceResolver {\n resolve(reference: string | ResourceReference, options?: ResolveResourceOptions): Promise<ResolvedResource>;\n}\n\nexport type ResolveResourceOptions = Readonly<{\n /** Tightens the inherited acquisition ceiling for this resource. */\n maxBytes?: number;\n signal?: AbortSignal;\n range?: Readonly<{ start: number; end?: number }>;\n}>;\n\nexport type FetchPolicy = Readonly<{\n /** Maximum bytes per entrypoint/resource, including non-HTTP sources. Default 64 MiB. */\n maxBytes?: number;\n maxAttempts?: number;\n baseDelayMs?: number;\n maxDelayMs?: number;\n timeoutMs?: number;\n}>;\n\nexport type RuntimeFetch = (input: string | URL, init?: RequestInit) => Promise<Response>;\n/** Providers must enforce maxBytes during I/O; runtime also checks returned bytes. */\nexport type ReadFile = (path: string, signal?: AbortSignal, options?: Readonly<{ maxBytes: number }>) => Promise<Uint8Array>;\nexport type HashBytes = (bytes: Uint8Array) => Promise<string>;\nexport type Sleep = (milliseconds: number, signal?: AbortSignal) => Promise<void>;\nexport type BlobStorageUrlResolver = (baseUrl: string, reference: string) => string | Promise<string>;\n\nexport type RuntimeDependencies = Readonly<{\n fetch?: RuntimeFetch;\n readFile?: ReadFile;\n hashBytes?: HashBytes;\n sleep?: Sleep;\n blobStorageUrlResolver?: BlobStorageUrlResolver;\n}>;\n\nexport type ResolvedModelSource = Readonly<{\n source: ModelSource;\n bytes: Uint8Array;\n contentType?: string;\n fileName?: string;\n contentHash: string;\n rangeSupported: boolean;\n resourceResolver: ResourceResolver;\n}>;\n\nexport type SniffInput = Readonly<{\n bytes: Uint8Array;\n contentType?: string;\n fileName?: string;\n sourceKind: ModelSourceKind;\n}>;\n\nexport type AdapterLoadContext = Readonly<{\n signal: AbortSignal;\n mode: \"strict\" | \"tolerant\" | \"forensic\";\n execution: \"main\" | \"worker\";\n adapterOptions: unknown;\n}>;\n\nexport type AdapterLoadResult<TCanonicalModel, TRendererReady = unknown> = Readonly<{\n canonicalModel: TCanonicalModel;\n rendererReady?: TRendererReady;\n diagnostics?: readonly unknown[];\n}>;\n\nexport interface ModelAdapter<TCanonicalModel, TRendererReady = unknown> {\n readonly formatId: string;\n readonly supportsWorker?: boolean;\n sniff(input: SniffInput): number | boolean;\n load(\n input: ResolvedModelSource,\n context: AdapterLoadContext,\n ): Promise<AdapterLoadResult<TCanonicalModel, TRendererReady>>;\n}\n\nexport type AdapterModule<TCanonicalModel, TRendererReady = unknown> =\n | ModelAdapter<TCanonicalModel, TRendererReady>\n | Readonly<{ default: ModelAdapter<TCanonicalModel, TRendererReady> }>;\n\nexport type AdapterRegistration<TCanonicalModel = unknown, TRendererReady = unknown> = Readonly<{\n formatId: string;\n extensions?: readonly string[];\n mimeTypes?: readonly string[];\n sniff?: (input: SniffInput) => number | boolean;\n load: () => Promise<AdapterModule<TCanonicalModel, TRendererReady>>;\n}>;\n\nexport type WorkerLoadRequest = Readonly<{\n formatId: string;\n input: ResolvedModelSource;\n context: AdapterLoadContext;\n}>;\n\nexport interface WorkerDispatcher {\n dispatch<TCanonicalModel, TRendererReady = unknown>(\n request: WorkerLoadRequest,\n ): Promise<AdapterLoadResult<TCanonicalModel, TRendererReady>>;\n}\n\nexport type ModelCache = Readonly<{\n get<T>(key: string): Promise<T | undefined>;\n set<T>(key: string, value: T): Promise<void>;\n delete(key: string): Promise<boolean>;\n clear(): Promise<void>;\n}>;\n\nexport type LoadModelOptions = Readonly<{\n signal?: AbortSignal;\n mode?: \"strict\" | \"tolerant\" | \"forensic\";\n adapterOptions?: unknown;\n formatId?: string;\n fetchPolicy?: FetchPolicy;\n integrity?: string;\n useCache?: boolean;\n worker?: Readonly<{\n mode?: \"auto\" | \"never\" | \"always\";\n }>;\n}>;\n\nexport type ModelLoadResult<TCanonicalModel, TRendererReady = unknown> = Readonly<{\n canonicalModel: TCanonicalModel;\n rendererReady?: TRendererReady;\n diagnostics?: readonly unknown[];\n contentHash: string;\n cacheKey: string;\n fromCache: boolean;\n contentType?: string;\n sourceKind: ModelSourceKind;\n}>;\n","import { DEFAULT_MODEL_MAX_BYTES } from \"./types.js\";\n\n// The intrinsic getter identifies the actual typed-array element kind across\n// realms, without trusting a producer's constructor or Symbol.toStringTag.\nconst typedArrayKind = Object.getOwnPropertyDescriptor(\n Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag,\n)?.get;\n\n/** Non-retryable acquisition failure; never contains a source URI or provider error. */\nexport class ModelResourceTooLargeError extends Error {\n readonly code = \"MODEL_RESOURCE_TOO_LARGE\";\n constructor() {\n super(\"Model resource exceeds maxBytes\");\n this.name = \"ModelResourceTooLargeError\";\n }\n}\n\nexport function byteLimit(value = DEFAULT_MODEL_MAX_BYTES): number {\n if (!Number.isSafeInteger(value) || value <= 0) throw new RangeError(\"maxBytes must be a positive safe integer\");\n return value;\n}\n\nexport function checkSize(size: number, maxBytes: number): void {\n if (size > maxBytes) throw new ModelResourceTooLargeError();\n}\n\nexport function checkAbort(signal?: AbortSignal): void {\n if (signal?.aborted) throw new DOMException(\"The model load was aborted\", \"AbortError\");\n}\n\nexport function acquisitionController(signal?: AbortSignal, timeoutMs = 30_000, parent?: AbortSignal) {\n if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2_147_483_647) {\n throw new RangeError(\"timeoutMs must be a positive timer-safe integer\");\n }\n const controller = new AbortController();\n const abort = () => controller.abort();\n signal?.addEventListener(\"abort\", abort, { once: true });\n parent?.addEventListener(\"abort\", abort, { once: true });\n if (signal?.aborted || parent?.aborted) controller.abort();\n const timer = setTimeout(abort, timeoutMs);\n return {\n signal: controller.signal,\n dispose() {\n clearTimeout(timer);\n signal?.removeEventListener(\"abort\", abort);\n parent?.removeEventListener(\"abort\", abort);\n },\n };\n}\n\n/** Stop waiting even when an injected producer ignores cancellation. */\nexport async function abortable<T>(work: Promise<T>, signal: AbortSignal): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n const abort = () => {\n signal.removeEventListener(\"abort\", abort);\n reject(new DOMException(\"The model load was aborted\", \"AbortError\"));\n };\n signal.addEventListener(\"abort\", abort, { once: true });\n if (signal.aborted) abort();\n work.then(\n (value) => { signal.removeEventListener(\"abort\", abort); resolve(value); },\n (error: unknown) => { signal.removeEventListener(\"abort\", abort); reject(error); },\n );\n });\n}\n\n/** Invoke cleanup without allowing an uncooperative producer to stall failure. */\nexport function cleanup(work: () => unknown): void {\n try { void Promise.resolve(work()).catch(() => {}); } catch { /* Preserve the original failure. */ }\n}\n\nexport async function readBoundedStream(\n source: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array>,\n maxBytes: number,\n signal: AbortSignal,\n): Promise<Uint8Array> {\n // Geometric storage bounds both retained bytes and metadata for tiny/empty chunks.\n let bytes = new Uint8Array(0);\n let length = 0;\n let chunks = 0;\n const reader = \"getReader\" in source ? source.getReader() : undefined;\n const iterator = reader ? undefined : (source as AsyncIterable<Uint8Array>)[Symbol.asyncIterator]();\n try {\n while (true) {\n checkAbort(signal);\n if (++chunks % 256 === 0) await abortable(new Promise<void>((resolve) => setTimeout(resolve, 0)), signal);\n const next = await abortable(reader ? reader.read() : iterator!.next(), signal);\n checkAbort(signal);\n if (next.done) break;\n if (!ArrayBuffer.isView(next.value) || typedArrayKind?.call(next.value) !== \"Uint8Array\") throw new TypeError(\"Model stream chunks must be Uint8Array\");\n const chunk = next.value;\n if (chunk.byteLength > maxBytes - length) throw new ModelResourceTooLargeError();\n const required = length + chunk.byteLength;\n if (required > bytes.length) {\n const grown = new Uint8Array(Math.min(maxBytes, Math.max(required, bytes.length * 2, 4096)));\n grown.set(bytes.subarray(0, length));\n bytes = grown;\n }\n bytes.set(chunk, length);\n length = required;\n }\n return bytes.length === length ? bytes : bytes.slice(0, length);\n } catch (error) {\n if (reader) cleanup(() => reader.cancel());\n else cleanup(() => iterator?.return?.());\n throw error;\n } finally {\n reader?.releaseLock();\n }\n}\n\nexport async function boundedBytes(input: ArrayBuffer | Blob | Uint8Array, maxBytes: number, signal: AbortSignal): Promise<Uint8Array> {\n checkAbort(signal);\n checkSize(input instanceof Blob ? input.size : input.byteLength, maxBytes);\n if (input instanceof Blob) return readBoundedStream(input.stream(), maxBytes, signal);\n return new Uint8Array(input);\n}\n","import { abortable, acquisitionController, byteLimit, checkAbort, checkSize, cleanup, ModelResourceTooLargeError, readBoundedStream } from \"./bytes.js\";\nimport { DEFAULT_MODEL_MAX_BYTES } from \"./types.js\";\nimport type {\n FetchPolicy,\n HashBytes,\n RuntimeFetch,\n Sleep,\n ResolveResourceOptions,\n} from \"./types.js\";\n\nconst DEFAULT_POLICY: Required<FetchPolicy> = {\n maxBytes: DEFAULT_MODEL_MAX_BYTES,\n maxAttempts: 3,\n baseDelayMs: 100,\n maxDelayMs: 1_000,\n timeoutMs: 30_000,\n};\n\nconst RETRYABLE_STATUSES = new Set([408, 425, 429, 500, 502, 503, 504]);\n\nclass NonRetryableResourceError extends Error {}\n\nexport type FetchedResource = Readonly<{\n bytes: Uint8Array;\n contentType?: string;\n rangeSupported: boolean;\n}>;\n\nexport function defaultSleep(milliseconds: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n if (signal?.aborted) {\n reject(createAbortError());\n return;\n }\n\n const timer = setTimeout(resolve, milliseconds);\n signal?.addEventListener(\"abort\", () => {\n clearTimeout(timer);\n reject(createAbortError());\n }, { once: true });\n });\n}\n\nexport async function sha256Hex(bytes: Uint8Array): Promise<string> {\n if (!globalThis.crypto?.subtle) {\n throw new Error(\"SHA-256 integrity checks require Web Crypto support or an injected hashBytes dependency\");\n }\n\n const digest = await globalThis.crypto.subtle.digest(\"SHA-256\", bytes as BufferSource);\n return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nexport async function fetchResource(\n url: string,\n options: Readonly<{\n fetch: RuntimeFetch;\n hashBytes: HashBytes;\n sleep: Sleep;\n headers?: Readonly<Record<string, string>>;\n integrity?: string;\n policy?: FetchPolicy;\n resolveOptions?: ResolveResourceOptions;\n }>,\n): Promise<FetchedResource> {\n const policy = { ...DEFAULT_POLICY, ...options.policy };\n const maxBytes = Math.min(byteLimit(policy.maxBytes), byteLimit(options.resolveOptions?.maxBytes ?? policy.maxBytes));\n const headers = new Headers(options.headers);\n if (options.resolveOptions?.range) {\n const { start, end } = options.resolveOptions.range;\n headers.set(\"Range\", `bytes=${start}-${end ?? \"\"}`);\n }\n\n let lastError: unknown;\n for (let attempt = 1; attempt <= policy.maxAttempts; attempt += 1) {\n const controller = acquisitionController(options.resolveOptions?.signal, policy.timeoutMs);\n\n try {\n checkAbort(controller.signal);\n const response = await abortable(options.fetch(url, {\n headers,\n signal: controller.signal,\n }).then((response) => {\n if (controller.signal.aborted) {\n cleanup(() => response.body?.cancel());\n checkAbort(controller.signal);\n }\n return response;\n }), controller.signal);\n\n if (!response.ok) {\n cleanup(() => response.body?.cancel());\n if (!RETRYABLE_STATUSES.has(response.status)) {\n throw new NonRetryableResourceError(`Model resource request failed with HTTP ${response.status}`);\n }\n if (attempt === policy.maxAttempts) {\n throw new NonRetryableResourceError(`Model resource request failed with HTTP ${response.status}`);\n }\n lastError = new Error(`Retryable model resource response: HTTP ${response.status}`);\n await waitForRetry(options.sleep, attempt, policy, controller.signal);\n continue;\n }\n\n let bytes: Uint8Array;\n try {\n const declared = response.headers.get(\"content-length\");\n if (declared !== null && /^\\d+$/u.test(declared)) checkSize(Number(declared), maxBytes);\n bytes = response.body ? await readBoundedStream(response.body, maxBytes, controller.signal) : new Uint8Array(0);\n checkAbort(controller.signal);\n } catch (error) {\n if (!response.body?.locked) cleanup(() => response.body?.cancel());\n throw error;\n }\n await abortable(verifyIntegrity(bytes, options.integrity, options.hashBytes), controller.signal);\n return {\n bytes,\n contentType: response.headers.get(\"content-type\") ?? undefined,\n rangeSupported: response.status === 206 || response.headers.get(\"accept-ranges\")?.toLowerCase() === \"bytes\",\n };\n } catch (error) {\n if (isAbortError(error) || options.resolveOptions?.signal?.aborted) {\n throw createAbortError();\n }\n if (error instanceof NonRetryableResourceError || error instanceof ModelResourceTooLargeError) {\n throw error;\n }\n lastError = error;\n if (attempt === policy.maxAttempts) {\n throw error;\n }\n await waitForRetry(options.sleep, attempt, policy, controller.signal);\n } finally {\n controller.dispose();\n }\n }\n\n throw lastError instanceof Error ? lastError : new Error(\"Model resource request failed\");\n}\n\nexport async function verifyIntegrity(bytes: Uint8Array, integrity: string | undefined, hashBytes: HashBytes): Promise<void> {\n if (!integrity) {\n return;\n }\n\n const expected = integrity.replace(/^sha256-/iu, \"\");\n if (/^[0-9a-f]{64}$/iu.test(expected)) {\n const actualHex = await hashBytes(bytes);\n if (expected.toLowerCase() === actualHex.toLowerCase()) return;\n } else if (/^[A-Za-z0-9+/]{43}=$/u.test(expected)) {\n const actualBase64 = bytesToBase64(await digestBytes(bytes, hashBytes));\n if (expected === actualBase64) return;\n }\n throw new NonRetryableResourceError(\"Model resource integrity check failed\");\n}\n\nasync function digestBytes(bytes: Uint8Array, hashBytes: HashBytes): Promise<Uint8Array> {\n if (globalThis.crypto?.subtle) {\n return new Uint8Array(await globalThis.crypto.subtle.digest(\"SHA-256\", bytes as BufferSource));\n }\n\n const hex = await hashBytes(bytes);\n if (!/^[0-9a-f]{64}$/iu.test(hex)) {\n throw new Error(\"A base64 integrity check requires Web Crypto or a byte-level hash implementation\");\n }\n return Uint8Array.from(hex.match(/.{2}/gu) ?? [], (pair) => Number.parseInt(pair, 16));\n}\n\nfunction bytesToBase64(bytes: Uint8Array): string {\n let binary = \"\";\n for (const byte of bytes) {\n binary += String.fromCharCode(byte);\n }\n return btoa(binary);\n}\n\nasync function waitForRetry(sleep: Sleep, attempt: number, policy: Required<FetchPolicy>, signal: AbortSignal): Promise<void> {\n const delay = Math.min(policy.maxDelayMs, policy.baseDelayMs * 2 ** (attempt - 1));\n await abortable(sleep(delay, signal), signal);\n}\n\nfunction createAbortError(): Error {\n return new DOMException(\"The model load was aborted\", \"AbortError\");\n}\n\nfunction isAbortError(error: unknown): boolean {\n return error instanceof DOMException && error.name === \"AbortError\";\n}\n","import { abortable, acquisitionController, boundedBytes, byteLimit, checkAbort, checkSize, readBoundedStream } from \"./bytes.js\";\nimport { MemoryModelCache, createModelCacheKey } from \"./cache.js\";\nimport { defaultSleep, fetchResource, sha256Hex, verifyIntegrity } from \"./fetch.js\";\nimport type {\n AdapterLoadResult,\n AdapterModule,\n AdapterRegistration,\n HashBytes,\n LoadModelOptions,\n ModelAdapter,\n ModelCache,\n ModelLoadResult,\n ModelPackage,\n ModelResourceInput,\n ModelSource,\n ModelSourceKind,\n ReadFile,\n ResolveResourceOptions,\n ResolvedModelSource,\n ResolvedResource,\n ResourceReference,\n ResourceResolver,\n RuntimeDependencies,\n RuntimeFetch,\n Sleep,\n SniffInput,\n WorkerDispatcher,\n WorkerLoadRequest,\n} from \"./types.js\";\n\nexport class AdapterRegistry {\n private readonly registrations = new Map<string, AdapterRegistration>();\n\n register<TCanonicalModel, TRendererReady>(registration: AdapterRegistration<TCanonicalModel, TRendererReady>): this {\n if (this.registrations.has(registration.formatId)) {\n throw new Error(`An adapter is already registered for format ${registration.formatId}`);\n }\n this.registrations.set(registration.formatId, registration as AdapterRegistration);\n return this;\n }\n\n async resolve<TCanonicalModel = unknown, TRendererReady = unknown>(\n input: SniffInput,\n formatId?: string,\n ): Promise<ModelAdapter<TCanonicalModel, TRendererReady>> {\n if (formatId) {\n const registration = this.registrations.get(formatId);\n if (!registration) {\n throw new Error(`No model adapter is registered for format ${formatId}`);\n }\n return unwrapAdapter(await registration.load()) as ModelAdapter<TCanonicalModel, TRendererReady>;\n }\n\n const candidates = [...this.registrations.values()]\n .map((registration) => ({ registration, score: scoreRegistration(registration, input) }))\n .filter(({ score }) => score > 0)\n .sort((left, right) => right.score - left.score);\n\n const selected = candidates[0]?.registration;\n if (!selected) {\n throw new Error(\"No model adapter matched the source content, MIME type, or extension\");\n }\n\n return unwrapAdapter(await selected.load()) as ModelAdapter<TCanonicalModel, TRendererReady>;\n }\n}\n\nexport class PackageResourceResolver implements ResourceResolver {\n private readonly parentSignal?: AbortSignal;\n private readonly memoryResources: Readonly<Record<string, ModelResourceInput>>;\n private readonly baseUrl?: string;\n private readonly sourceKind: ModelSourceKind;\n private readonly fetch: RuntimeFetch;\n private readonly hashBytes: HashBytes;\n private readonly sleep: Sleep;\n private readonly fetchPolicy: LoadModelOptions[\"fetchPolicy\"];\n private readonly headers?: Readonly<Record<string, string>>;\n private readonly blobStorageUrlResolver?: RuntimeDependencies[\"blobStorageUrlResolver\"];\n\n public constructor(options: Readonly<{\n sourceKind: ModelSourceKind;\n signal?: AbortSignal;\n baseUrl?: string;\n package?: ModelPackage;\n fetch: RuntimeFetch;\n hashBytes: HashBytes;\n sleep: Sleep;\n fetchPolicy?: LoadModelOptions[\"fetchPolicy\"];\n headers?: Readonly<Record<string, string>>;\n blobStorageUrlResolver?: RuntimeDependencies[\"blobStorageUrlResolver\"];\n }>) {\n this.parentSignal = options.signal;\n this.sourceKind = options.sourceKind;\n this.baseUrl = options.package?.baseUrl ?? options.baseUrl;\n this.memoryResources = options.package?.resources ?? {};\n this.fetch = options.fetch;\n this.hashBytes = options.hashBytes;\n this.sleep = options.sleep;\n this.fetchPolicy = { ...options.fetchPolicy };\n this.headers = options.headers;\n this.blobStorageUrlResolver = options.blobStorageUrlResolver;\n }\n\n async resolve(reference: string | ResourceReference, options: ResolveResourceOptions = {}): Promise<ResolvedResource> {\n const inheritedLimit = byteLimit(this.fetchPolicy?.maxBytes);\n const maxBytes = Math.min(inheritedLimit, byteLimit(options.maxBytes ?? inheritedLimit));\n const controller = acquisitionController(options.signal, this.fetchPolicy?.timeoutMs, this.parentSignal);\n try {\n checkAbort(controller.signal);\n const requested = typeof reference === \"string\" ? { path: reference } : reference;\n const memoryKey = normalizePackagePath(requested.path);\n const memoryResource = this.memoryResources[memoryKey] ?? this.memoryResources[`./${memoryKey}`];\n if (memoryResource) {\n const bytes = await boundedBytes(memoryResource.bytes, maxBytes, controller.signal);\n await abortable(verifyIntegrity(bytes, requested.integrity ?? memoryResource.integrity, this.hashBytes), controller.signal);\n return {\n path: memoryKey,\n bytes,\n contentType: requested.contentType ?? memoryResource.contentType,\n contentHash: await abortable(this.hashBytes(bytes), controller.signal),\n rangeSupported: false,\n };\n }\n\n const url = await abortable(this.resolveUrl(requested.path), controller.signal);\n checkAbort(controller.signal);\n const result = await fetchResource(url, {\n fetch: this.fetch,\n hashBytes: this.hashBytes,\n sleep: this.sleep,\n headers: this.headers,\n integrity: requested.integrity,\n policy: this.fetchPolicy,\n resolveOptions: { ...options, maxBytes, signal: controller.signal },\n });\n return {\n path: memoryKey,\n bytes: result.bytes,\n contentType: requested.contentType ?? result.contentType,\n contentHash: await abortable(this.hashBytes(result.bytes), controller.signal),\n rangeSupported: result.rangeSupported,\n };\n } finally {\n controller.dispose();\n }\n }\n\n private async resolveUrl(reference: string): Promise<string> {\n if (/^[a-z][a-z\\d+.-]*:/iu.test(reference)) {\n return ensureHttpUrl(reference);\n }\n if (!this.baseUrl) {\n throw new Error(`Cannot resolve relative model resource ${reference} without a package base URL or memory resource`);\n }\n if (this.sourceKind === \"blob-storage-url\" && this.blobStorageUrlResolver) {\n return this.blobStorageUrlResolver(this.baseUrl, reference);\n }\n return ensureHttpUrl(new URL(reference, this.baseUrl).href);\n }\n}\n\nexport class ModelRuntime {\n private readonly registry: AdapterRegistry;\n private readonly cache: ModelCache;\n private readonly fetch: RuntimeFetch;\n private readonly readFile?: ReadFile;\n private readonly hashBytes: HashBytes;\n private readonly sleep: Sleep;\n private readonly workerDispatcher?: WorkerDispatcher;\n private readonly blobStorageUrlResolver?: RuntimeDependencies[\"blobStorageUrlResolver\"];\n private readonly cacheKeys = new Set<string>();\n\n public constructor(options: Readonly<{\n registry?: AdapterRegistry;\n cache?: ModelCache;\n dependencies?: RuntimeDependencies;\n workerDispatcher?: WorkerDispatcher;\n }> = {}) {\n this.registry = options.registry ?? new AdapterRegistry();\n this.cache = options.cache ?? new MemoryModelCache();\n this.fetch = options.dependencies?.fetch ?? defaultFetch;\n this.readFile = options.dependencies?.readFile;\n this.hashBytes = options.dependencies?.hashBytes ?? sha256Hex;\n this.sleep = options.dependencies?.sleep ?? defaultSleep;\n this.workerDispatcher = options.workerDispatcher;\n this.blobStorageUrlResolver = options.dependencies?.blobStorageUrlResolver;\n }\n\n public get adapters(): AdapterRegistry {\n return this.registry;\n }\n\n public async invalidate(contentHash?: string): Promise<void> {\n if (!contentHash) {\n await this.cache.clear();\n this.cacheKeys.clear();\n return;\n }\n\n for (const key of this.cacheKeys) {\n if (key.includes(`:${contentHash}:`)) {\n await this.cache.delete(key);\n this.cacheKeys.delete(key);\n }\n }\n }\n\n public async loadModel<TCanonicalModel, TRendererReady = unknown>(\n source: ModelSource,\n options: LoadModelOptions = {},\n ): Promise<ModelLoadResult<TCanonicalModel, TRendererReady>> {\n const input = await this.resolveSource(source, options);\n const adapter = await this.registry.resolve<TCanonicalModel, TRendererReady>({\n bytes: input.bytes,\n contentType: input.contentType,\n fileName: input.fileName,\n sourceKind: source.kind,\n }, options.formatId);\n const cacheKey = createModelCacheKey(input.contentHash, adapter.formatId, { adapterOptions: options.adapterOptions ?? null, maxBytes: byteLimit(options.fetchPolicy?.maxBytes) });\n const useCache = options.useCache ?? true;\n if (useCache) {\n const cached = await this.cache.get<ModelLoadResult<TCanonicalModel, TRendererReady>>(cacheKey);\n if (cached) {\n return { ...cached, fromCache: true };\n }\n }\n\n const context = {\n signal: options.signal ?? new AbortController().signal,\n mode: options.mode ?? \"strict\",\n execution: \"main\" as const,\n adapterOptions: options.adapterOptions ?? null,\n };\n const adapterResult = await this.loadWithExecution<TCanonicalModel, TRendererReady>(adapter, input, context, options);\n const result: ModelLoadResult<TCanonicalModel, TRendererReady> = {\n ...adapterResult,\n contentHash: input.contentHash,\n cacheKey,\n fromCache: false,\n contentType: input.contentType,\n sourceKind: source.kind,\n };\n if (useCache) {\n await this.cache.set(cacheKey, result);\n this.cacheKeys.add(cacheKey);\n }\n return result;\n }\n\n private async loadWithExecution<TCanonicalModel, TRendererReady>(\n adapter: ModelAdapter<TCanonicalModel, TRendererReady>,\n input: ResolvedModelSource,\n context: Parameters<ModelAdapter<TCanonicalModel, TRendererReady>[\"load\"]>[1],\n options: LoadModelOptions,\n ): Promise<AdapterLoadResult<TCanonicalModel, TRendererReady>> {\n const workerMode = options.worker?.mode ?? \"auto\";\n const canUseWorker = workerMode !== \"never\" && Boolean(this.workerDispatcher) && Boolean(adapter.supportsWorker);\n if (workerMode === \"always\" && !canUseWorker) {\n throw new Error(`Adapter ${adapter.formatId} does not support the requested worker execution`);\n }\n if (canUseWorker && this.workerDispatcher) {\n const workerRequest: WorkerLoadRequest = {\n formatId: adapter.formatId,\n input,\n context: { ...context, execution: \"worker\" },\n };\n return this.workerDispatcher.dispatch<TCanonicalModel, TRendererReady>(workerRequest);\n }\n return adapter.load(input, context);\n }\n\n private async resolveSource(source: ModelSource, options: LoadModelOptions): Promise<ResolvedModelSource> {\n const maxBytes = byteLimit(options.fetchPolicy?.maxBytes);\n const controller = acquisitionController(options.signal, options.fetchPolicy?.timeoutMs);\n try {\n checkAbort(controller.signal);\n const fileName = sourceFileName(source);\n const baseUrl = \"url\" in source ? source.url : undefined;\n const headers = \"headers\" in source ? source.headers : undefined;\n let bytes: Uint8Array;\n let contentType: string | undefined = \"mimeTypeHint\" in source ? source.mimeTypeHint : undefined;\n let rangeSupported = false;\n\n if (source.kind === \"file-path\") {\n if (!this.readFile) {\n throw new Error(\"file-path sources require a readFile dependency\");\n }\n bytes = await abortable(this.readFile(source.path, controller.signal, { maxBytes }), controller.signal);\n checkSize(bytes.byteLength, maxBytes);\n } else if (source.kind === \"url\" || source.kind === \"blob-storage-url\") {\n const result = await fetchResource(ensureHttpUrl(source.url), {\n fetch: this.fetch,\n hashBytes: this.hashBytes,\n sleep: this.sleep,\n headers,\n integrity: options.integrity,\n policy: options.fetchPolicy,\n resolveOptions: { signal: controller.signal },\n });\n bytes = result.bytes;\n contentType ??= result.contentType;\n rangeSupported = result.rangeSupported;\n } else {\n bytes = await sourceToBytes(source, maxBytes, controller.signal);\n }\n\n checkAbort(controller.signal);\n const contentHash = await abortable(this.hashBytes(bytes), controller.signal);\n const resourceResolver = new PackageResourceResolver({\n sourceKind: source.kind,\n signal: options.signal,\n baseUrl,\n package: source.package,\n fetch: this.fetch,\n hashBytes: this.hashBytes,\n sleep: this.sleep,\n fetchPolicy: options.fetchPolicy,\n headers,\n blobStorageUrlResolver: this.blobStorageUrlResolver,\n });\n return {\n source,\n bytes,\n contentType,\n fileName,\n contentHash,\n rangeSupported,\n resourceResolver,\n };\n } finally {\n controller.dispose();\n }\n }\n}\n\nfunction scoreRegistration(registration: AdapterRegistration, input: SniffInput): number {\n const extension = input.fileName?.split(\".\").pop()?.toLowerCase();\n const extensionScore = extension && registration.extensions?.some((item) => item.replace(/^\\./u, \"\").toLowerCase() === extension) ? 2 : 0;\n const mimeScore = input.contentType && registration.mimeTypes?.some((item) => item.toLowerCase() === input.contentType?.toLowerCase()) ? 3 : 0;\n const sniffScore = registration.sniff?.(input) ?? 0;\n const normalizedSniffScore = sniffScore === true ? 1 : sniffScore === false ? 0 : sniffScore;\n return extensionScore + mimeScore + normalizedSniffScore;\n}\n\nfunction unwrapAdapter<TCanonicalModel, TRendererReady>(module: AdapterModule<TCanonicalModel, TRendererReady>): ModelAdapter<TCanonicalModel, TRendererReady> {\n return \"default\" in module ? module.default : module;\n}\n\nfunction normalizePackagePath(path: string): string {\n if (path.includes(\"\\0\")) {\n throw new Error(\"Model resource paths cannot contain null bytes\");\n }\n const segments: string[] = [];\n for (const segment of path.replaceAll(\"\\\\\", \"/\").split(\"/\")) {\n if (!segment || segment === \".\") continue;\n if (segment === \"..\") {\n if (segments.length === 0) {\n throw new Error(`Model resource path escapes its package: ${path}`);\n }\n segments.pop();\n continue;\n }\n segments.push(segment);\n }\n return segments.join(\"/\");\n}\n\nasync function sourceToBytes(source: Exclude<ModelSource, { kind: \"file-path\" | \"url\" | \"blob-storage-url\" }>, maxBytes: number, signal: AbortSignal): Promise<Uint8Array> {\n if (source.kind === \"blob\") return boundedBytes(source.blob, maxBytes, signal);\n if (source.kind === \"array-buffer\" || source.kind === \"uint8-array\") return boundedBytes(source.bytes, maxBytes, signal);\n if (source.byteLengthHint !== undefined) checkSize(source.byteLengthHint, maxBytes);\n return readBoundedStream(source.stream, maxBytes, signal);\n}\n\nconst defaultFetch: RuntimeFetch = (input, init) => {\n if (!globalThis.fetch) {\n throw new Error(\"URL sources require fetch or an injected fetch dependency\");\n }\n return globalThis.fetch(input, init);\n};\n\nfunction sourceFileName(source: ModelSource): string | undefined {\n if (\"fileNameHint\" in source && source.fileNameHint) {\n return source.fileNameHint;\n }\n if (source.kind === \"file-path\") {\n return source.path.split(/[\\\\/]/u).pop();\n }\n if (source.kind === \"url\" || source.kind === \"blob-storage-url\") {\n try {\n return new URL(source.url).pathname.split(\"/\").pop() || undefined;\n } catch {\n return undefined;\n }\n }\n return undefined;\n}\n\nfunction ensureHttpUrl(value: string): string {\n const url = new URL(value);\n if (url.protocol !== \"http:\" && url.protocol !== \"https:\") {\n throw new Error(`Unsupported model resource URL scheme: ${url.protocol}`);\n }\n return url.href;\n}\n","import type {\n ModelAssetRef,\n ModelLodLevel,\n} from \"@plasius/asset-contracts\";\n\nconst SHA256_PATTERN = /^[a-f0-9]{64}$/u;\nconst PARTITION_ID_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/u;\n\n/** Hard CPU, GPU, and concurrent-load limits for one residency manager. */\nexport interface ModelResidencyBudget {\n readonly maxCpuBytes: number;\n readonly maxGpuBytes: number;\n readonly maxInFlightLoads: number;\n}\n\n/** Adapter request for one immutable model LOD or partition. */\nexport interface ModelLoadRequest {\n readonly assetRef: ModelAssetRef;\n readonly lod: ModelLodLevel;\n readonly partitionId?: string;\n readonly priority: number;\n readonly signal: AbortSignal;\n}\n\n/** Resource returned by a format-specific loader/upload adapter. */\nexport interface ModelLoadedResource<Resource> {\n readonly resource: Resource;\n readonly contentHash: string;\n readonly cpuBytes: number;\n readonly gpuBytes: number;\n readonly dispose: () => void | Promise<void>;\n}\n\n/** Options for acquiring a shared model resource. */\nexport interface AcquireModelOptions {\n readonly assetRef: ModelAssetRef;\n readonly lod: ModelLodLevel;\n readonly partitionId?: string;\n readonly priority: number;\n readonly estimatedCpuBytes: number;\n readonly estimatedGpuBytes: number;\n readonly signal?: AbortSignal;\n readonly pinned?: boolean;\n}\n\n/** Idempotent reference to one shared resident model resource. */\nexport interface ModelResidencyLease<Resource> {\n readonly key: string;\n readonly assetRef: ModelAssetRef;\n readonly lod: ModelLodLevel;\n readonly partitionId?: string;\n readonly resource: Resource;\n release(): void;\n setPriority(priority: number): void;\n setPinned(pinned: boolean): void;\n}\n\n/** Construction boundary for adapter-driven model loading. */\nexport interface ModelResidencyManagerOptions<Resource> {\n readonly budget: ModelResidencyBudget;\n readonly load: (\n request: ModelLoadRequest,\n ) => Promise<ModelLoadedResource<Resource>>;\n readonly loadTimeoutMs?: number;\n}\n\nexport type ModelResidencyErrorCode =\n | \"acquire-aborted\"\n | \"budget-exceeded\"\n | \"content-hash-mismatch\"\n | \"invalid-load-result\"\n | \"load-timeout\"\n | \"manager-closed\";\n\n/** Stable operational error returned by residency-specific failure paths. */\nexport class ModelResidencyError extends Error {\n readonly code: ModelResidencyErrorCode;\n\n constructor(code: ModelResidencyErrorCode, message: string) {\n super(message);\n this.name = \"ModelResidencyError\";\n this.code = code;\n }\n}\n\nexport interface ModelResidencyEntrySnapshot {\n readonly key: string;\n readonly state: \"queued\" | \"loading\" | \"resident\";\n readonly priority: number;\n readonly references: number;\n readonly pinned: boolean;\n readonly cpuBytes: number;\n readonly gpuBytes: number;\n}\n\n/** Immutable accounting and lifetime metrics for diagnostics. */\nexport interface ModelResidencySnapshot {\n readonly maxCpuBytes: number;\n readonly maxGpuBytes: number;\n readonly maxInFlightLoads: number;\n readonly cpuBytes: number;\n readonly gpuBytes: number;\n readonly reservedCpuBytes: number;\n readonly reservedGpuBytes: number;\n readonly residentCount: number;\n readonly pendingCount: number;\n readonly referenceCount: number;\n readonly pinnedCount: number;\n readonly inFlightLoads: number;\n readonly queuedLoads: number;\n readonly acquireCount: number;\n readonly cacheHits: number;\n readonly deduplicatedAcquires: number;\n readonly loadsStarted: number;\n readonly loadsCompleted: number;\n readonly loadsFailed: number;\n readonly loadsAborted: number;\n readonly loadsTimedOut: number;\n readonly evictions: number;\n readonly disposals: number;\n readonly disposalFailures: number;\n readonly residentKeys: readonly string[];\n readonly entries: readonly ModelResidencyEntrySnapshot[];\n}\n\ninterface AcquisitionWaiter<Resource> {\n readonly id: number;\n priority: number;\n pinned: boolean;\n readonly signal?: AbortSignal;\n readonly resolve: (lease: ModelResidencyLease<Resource>) => void;\n readonly reject: (reason: unknown) => void;\n onAbort?: () => void;\n}\n\ninterface LeaseRecord {\n readonly id: number;\n active: boolean;\n priority: number;\n pinned: boolean;\n}\n\ninterface ResidencyEntry<Resource> {\n readonly key: string;\n readonly assetRef: ModelAssetRef;\n readonly lod: ModelLodLevel;\n readonly partitionId?: string;\n readonly estimatedCpuBytes: number;\n readonly estimatedGpuBytes: number;\n readonly queueOrder: number;\n state: \"queued\" | \"loading\" | \"resident\";\n priority: number;\n retainedPriority: number;\n lastUsed: number;\n abandoned: boolean;\n loadSlotReleased: boolean;\n waiters: Map<number, AcquisitionWaiter<Resource>>;\n leases: Map<number, LeaseRecord>;\n controller?: AbortController;\n timeout?: ReturnType<typeof setTimeout>;\n loaded?: ModelLoadedResource<Resource>;\n loadTask?: Promise<void>;\n}\n\ninterface MutableMetrics {\n acquireCount: number;\n cacheHits: number;\n deduplicatedAcquires: number;\n loadsStarted: number;\n loadsCompleted: number;\n loadsFailed: number;\n loadsAborted: number;\n loadsTimedOut: number;\n evictions: number;\n disposals: number;\n disposalFailures: number;\n}\n\nfunction requireSafeNonNegativeInteger(value: number, name: string): void {\n if (!Number.isSafeInteger(value) || value < 0) {\n throw new RangeError(`${name} must be a non-negative safe integer`);\n }\n}\n\nfunction requirePositiveSafeInteger(value: number, name: string): void {\n if (!Number.isSafeInteger(value) || value <= 0) {\n throw new RangeError(`${name} must be a positive safe integer`);\n }\n}\n\nfunction requirePriority(priority: number): void {\n if (!Number.isFinite(priority) || priority < 0) {\n throw new RangeError(\"priority must be a non-negative finite number\");\n }\n}\n\nfunction requireLod(lod: ModelLodLevel): void {\n if (!Number.isInteger(lod) || lod < 0 || lod > 3) {\n throw new RangeError(\"lod must be an integer from 0 to 3\");\n }\n}\n\n/** Build the canonical content-hash, LOD, and optional partition cache key. */\nexport function createModelResidencyKey(\n assetRef: ModelAssetRef,\n lod: ModelLodLevel,\n partitionId?: string,\n): string {\n if (!SHA256_PATTERN.test(assetRef.contentHash)) {\n throw new Error(\"ModelAssetRef.contentHash must be a lowercase SHA-256 value\");\n }\n requireLod(lod);\n if (partitionId !== undefined && !PARTITION_ID_PATTERN.test(partitionId)) {\n throw new Error(\"partitionId must be a bounded runtime token\");\n }\n return `${assetRef.contentHash}:lod${lod}:${partitionId ?? \"whole\"}`;\n}\n\nfunction closedError(): ModelResidencyError {\n return new ModelResidencyError(\n \"manager-closed\",\n \"The model residency manager is closed\",\n );\n}\n\nfunction abortedError(): ModelResidencyError {\n return new ModelResidencyError(\n \"acquire-aborted\",\n \"The model residency acquisition was aborted\",\n );\n}\n\nfunction signalIsAborted(signal?: AbortSignal): boolean {\n return signal?.aborted === true;\n}\n\n/**\n * Content-addressed model cache with bounded work, reference-counted leases,\n * deterministic eviction, and exact-once adapter disposal.\n */\nexport class ModelResidencyManager<Resource = unknown> {\n readonly #budget: ModelResidencyBudget;\n readonly #load: (\n request: ModelLoadRequest,\n ) => Promise<ModelLoadedResource<Resource>>;\n readonly #loadTimeoutMs?: number;\n readonly #entries = new Map<string, ResidencyEntry<Resource>>();\n readonly #loadTasks = new Set<Promise<void>>();\n readonly #disposalTasks = new Set<Promise<void>>();\n readonly #metrics: MutableMetrics = {\n acquireCount: 0,\n cacheHits: 0,\n deduplicatedAcquires: 0,\n loadsStarted: 0,\n loadsCompleted: 0,\n loadsFailed: 0,\n loadsAborted: 0,\n loadsTimedOut: 0,\n evictions: 0,\n disposals: 0,\n disposalFailures: 0,\n };\n #nextIdentity = 1;\n #clock = 1;\n #inFlightLoads = 0;\n #closed = false;\n #admissionTail: Promise<void> = Promise.resolve();\n #shutdownPromise?: Promise<void>;\n\n constructor(options: ModelResidencyManagerOptions<Resource>) {\n requirePositiveSafeInteger(\n options.budget.maxCpuBytes,\n \"budget.maxCpuBytes\",\n );\n requirePositiveSafeInteger(\n options.budget.maxGpuBytes,\n \"budget.maxGpuBytes\",\n );\n requirePositiveSafeInteger(\n options.budget.maxInFlightLoads,\n \"budget.maxInFlightLoads\",\n );\n if (options.loadTimeoutMs !== undefined) {\n requirePositiveSafeInteger(options.loadTimeoutMs, \"loadTimeoutMs\");\n }\n this.#budget = Object.freeze({ ...options.budget });\n this.#load = options.load;\n this.#loadTimeoutMs = options.loadTimeoutMs;\n }\n\n /** Acquire one shared resource lease, loading it at most once per cache key. */\n acquire(options: AcquireModelOptions): Promise<ModelResidencyLease<Resource>> {\n try {\n if (this.#closed) throw closedError();\n requirePriority(options.priority);\n requireSafeNonNegativeInteger(\n options.estimatedCpuBytes,\n \"estimatedCpuBytes\",\n );\n requireSafeNonNegativeInteger(\n options.estimatedGpuBytes,\n \"estimatedGpuBytes\",\n );\n const key = createModelResidencyKey(\n options.assetRef,\n options.lod,\n options.partitionId,\n );\n if (signalIsAborted(options.signal)) throw abortedError();\n this.#metrics.acquireCount += 1;\n\n const existing = this.#entries.get(key);\n if (existing !== undefined) return this.#acquireExisting(existing, options);\n return this.#scheduleAdmission(key, options);\n } catch (error) {\n return Promise.reject(error);\n }\n }\n\n #acquireExisting(\n entry: ResidencyEntry<Resource>,\n options: AcquireModelOptions,\n ): Promise<ModelResidencyLease<Resource>> {\n this.#metrics.deduplicatedAcquires += 1;\n if (entry.state === \"resident\") {\n this.#metrics.cacheHits += 1;\n return Promise.resolve(\n this.#createLease(\n entry,\n options.priority,\n options.pinned ?? false,\n ),\n );\n }\n return this.#addWaiter(entry, options);\n }\n\n #scheduleAdmission(\n key: string,\n options: AcquireModelOptions,\n ): Promise<ModelResidencyLease<Resource>> {\n let resolveAcquisition!: (lease: ModelResidencyLease<Resource>) => void;\n let rejectAcquisition!: (reason: unknown) => void;\n const acquisition = new Promise<ModelResidencyLease<Resource>>(\n (resolve, reject) => {\n resolveAcquisition = resolve;\n rejectAcquisition = reject;\n },\n );\n const admission = this.#admissionTail.then(async () => {\n if (this.#closed) throw closedError();\n if (signalIsAborted(options.signal)) throw abortedError();\n const existing = this.#entries.get(key);\n if (existing !== undefined) {\n void this.#acquireExisting(existing, options).then(\n resolveAcquisition,\n rejectAcquisition,\n );\n return;\n }\n await this.#ensureCapacityForReservation(\n options.estimatedCpuBytes,\n options.estimatedGpuBytes,\n );\n if (this.#closed) throw closedError();\n if (signalIsAborted(options.signal)) throw abortedError();\n const entry: ResidencyEntry<Resource> = {\n key,\n assetRef: options.assetRef,\n lod: options.lod,\n ...(options.partitionId === undefined\n ? {}\n : { partitionId: options.partitionId }),\n estimatedCpuBytes: options.estimatedCpuBytes,\n estimatedGpuBytes: options.estimatedGpuBytes,\n queueOrder: this.#nextIdentity++,\n state: \"queued\",\n priority: options.priority,\n retainedPriority: options.priority,\n lastUsed: this.#clock++,\n abandoned: false,\n loadSlotReleased: true,\n waiters: new Map(),\n leases: new Map(),\n };\n this.#entries.set(key, entry);\n void this.#addWaiter(entry, options).then(\n resolveAcquisition,\n rejectAcquisition,\n );\n this.#pumpQueue();\n });\n this.#admissionTail = admission.catch(() => undefined);\n void admission.catch(rejectAcquisition);\n return acquisition;\n }\n\n /** Dispose every currently unreferenced, unpinned resident cache entry. */\n async evictUnused(): Promise<void> {\n const tasks: Promise<void>[] = [];\n for (const entry of this.#evictionCandidates()) {\n tasks.push(this.#evict(entry));\n }\n await Promise.all(tasks);\n }\n\n /** Wait until queued/loading work and its resulting disposal have settled. */\n async waitForIdle(): Promise<void> {\n await this.#admissionTail;\n do {\n this.#pumpQueue();\n const tasks = [...this.#loadTasks];\n if (tasks.length > 0) {\n await Promise.allSettled(tasks);\n }\n } while (this.#loadTasks.size > 0 || this.#queuedCount() > 0);\n await Promise.allSettled([...this.#disposalTasks]);\n }\n\n /** Abort pending work and dispose every resident or late-completing resource. */\n shutdown(): Promise<void> {\n if (this.#shutdownPromise !== undefined) return this.#shutdownPromise;\n this.#closed = true;\n const error = closedError();\n const disposalTasks: Promise<void>[] = [];\n for (const entry of [...this.#entries.values()]) {\n if (entry.state === \"resident\") {\n this.#entries.delete(entry.key);\n disposalTasks.push(this.#disposeEntry(entry));\n continue;\n }\n this.#rejectWaiters(entry, error);\n if (entry.state === \"queued\") {\n this.#entries.delete(entry.key);\n } else {\n this.#abandonLoadingEntry(entry, error, false);\n }\n }\n this.#shutdownPromise = (async () => {\n await Promise.allSettled(disposalTasks);\n await this.waitForIdle();\n await Promise.allSettled([...this.#disposalTasks]);\n })();\n return this.#shutdownPromise;\n }\n\n /** Return immutable byte, queue, cache, and lifetime metrics. */\n snapshot(): ModelResidencySnapshot {\n let cpuBytes = 0;\n let gpuBytes = 0;\n let reservedCpuBytes = 0;\n let reservedGpuBytes = 0;\n let referenceCount = 0;\n let pinnedCount = 0;\n let residentCount = 0;\n let pendingCount = 0;\n const entries: ModelResidencyEntrySnapshot[] = [];\n\n for (const entry of this.#entries.values()) {\n const pinned = [...entry.leases.values()].some(\n (lease) => lease.active && lease.pinned,\n );\n const references = [...entry.leases.values()].filter(\n (lease) => lease.active,\n ).length;\n referenceCount += references;\n if (pinned) pinnedCount += 1;\n if (entry.state === \"resident\") {\n residentCount += 1;\n cpuBytes += entry.loaded!.cpuBytes;\n gpuBytes += entry.loaded!.gpuBytes;\n } else {\n pendingCount += 1;\n reservedCpuBytes += entry.estimatedCpuBytes;\n reservedGpuBytes += entry.estimatedGpuBytes;\n }\n entries.push(\n Object.freeze({\n key: entry.key,\n state: entry.state,\n priority: entry.priority,\n references,\n pinned,\n cpuBytes:\n entry.state === \"resident\"\n ? entry.loaded!.cpuBytes\n : entry.estimatedCpuBytes,\n gpuBytes:\n entry.state === \"resident\"\n ? entry.loaded!.gpuBytes\n : entry.estimatedGpuBytes,\n }),\n );\n }\n entries.sort((left, right) => left.key.localeCompare(right.key));\n return Object.freeze({\n ...this.#budget,\n cpuBytes,\n gpuBytes,\n reservedCpuBytes,\n reservedGpuBytes,\n residentCount,\n pendingCount,\n referenceCount,\n pinnedCount,\n inFlightLoads: this.#inFlightLoads,\n queuedLoads: this.#queuedCount(),\n ...this.#metrics,\n residentKeys: Object.freeze(\n entries\n .filter((entry) => entry.state === \"resident\")\n .map((entry) => entry.key),\n ),\n entries: Object.freeze(entries),\n });\n }\n\n #addWaiter(\n entry: ResidencyEntry<Resource>,\n options: AcquireModelOptions,\n ): Promise<ModelResidencyLease<Resource>> {\n const id = this.#nextIdentity++;\n let resolvePromise!: (lease: ModelResidencyLease<Resource>) => void;\n let rejectPromise!: (reason: unknown) => void;\n const promise = new Promise<ModelResidencyLease<Resource>>(\n (resolve, reject) => {\n resolvePromise = resolve;\n rejectPromise = reject;\n },\n );\n const waiter: AcquisitionWaiter<Resource> = {\n id,\n priority: options.priority,\n pinned: options.pinned ?? false,\n ...(options.signal === undefined ? {} : { signal: options.signal }),\n resolve: resolvePromise,\n reject: rejectPromise,\n };\n if (options.signal !== undefined) {\n waiter.onAbort = () => this.#abortWaiter(entry, waiter);\n options.signal.addEventListener(\"abort\", waiter.onAbort, { once: true });\n }\n entry.waiters.set(id, waiter);\n entry.retainedPriority = Math.max(entry.retainedPriority, options.priority);\n this.#recalculatePriority(entry);\n return promise;\n }\n\n #abortWaiter(\n entry: ResidencyEntry<Resource>,\n waiter: AcquisitionWaiter<Resource>,\n ): void {\n entry.waiters.delete(waiter.id);\n this.#detachWaiterSignal(waiter);\n waiter.reject(abortedError());\n this.#recalculatePriority(entry);\n if (entry.waiters.size > 0) return;\n if (entry.state === \"queued\") {\n this.#entries.delete(entry.key);\n this.#pumpQueue();\n return;\n }\n this.#abandonLoadingEntry(entry, abortedError(), true);\n }\n\n #createLease(\n entry: ResidencyEntry<Resource>,\n priority: number,\n pinned: boolean,\n ): ModelResidencyLease<Resource> {\n const id = this.#nextIdentity++;\n const record: LeaseRecord = {\n id,\n active: true,\n priority,\n pinned,\n };\n entry.leases.set(id, record);\n entry.retainedPriority = priority;\n entry.lastUsed = this.#clock++;\n this.#recalculatePriority(entry);\n const lease = {\n key: entry.key,\n assetRef: entry.assetRef,\n lod: entry.lod,\n ...(entry.partitionId === undefined\n ? {}\n : { partitionId: entry.partitionId }),\n resource: entry.loaded!.resource,\n release: () => {\n if (!record.active) return;\n record.active = false;\n entry.leases.delete(id);\n entry.lastUsed = this.#clock++;\n this.#recalculatePriority(entry);\n },\n setPriority: (nextPriority: number) => {\n if (!record.active || this.#entries.get(entry.key) !== entry) return;\n requirePriority(nextPriority);\n record.priority = nextPriority;\n entry.retainedPriority = nextPriority;\n entry.lastUsed = this.#clock++;\n this.#recalculatePriority(entry);\n },\n setPinned: (nextPinned: boolean) => {\n if (!record.active || this.#entries.get(entry.key) !== entry) return;\n record.pinned = nextPinned;\n },\n } satisfies ModelResidencyLease<Resource>;\n return Object.freeze(lease);\n }\n\n #recalculatePriority(entry: ResidencyEntry<Resource>): void {\n let priority = entry.retainedPriority;\n for (const waiter of entry.waiters.values()) {\n priority = Math.max(priority, waiter.priority);\n }\n for (const lease of entry.leases.values()) {\n priority = Math.max(priority, lease.priority);\n }\n entry.priority = priority;\n }\n\n #pumpQueue(): void {\n if (this.#closed) return;\n while (this.#inFlightLoads < this.#budget.maxInFlightLoads) {\n const candidate = [...this.#entries.values()]\n .filter(\n (entry) =>\n entry.state === \"queued\" &&\n !entry.abandoned &&\n entry.waiters.size > 0,\n )\n .sort(\n (left, right) =>\n right.priority - left.priority ||\n left.queueOrder - right.queueOrder,\n )[0];\n if (candidate === undefined) return;\n this.#startLoad(candidate);\n }\n }\n\n #startLoad(entry: ResidencyEntry<Resource>): void {\n entry.state = \"loading\";\n entry.loadSlotReleased = false;\n entry.controller = new AbortController();\n this.#inFlightLoads += 1;\n this.#metrics.loadsStarted += 1;\n if (this.#loadTimeoutMs !== undefined) {\n entry.timeout = setTimeout(() => {\n this.#metrics.loadsTimedOut += 1;\n this.#abandonLoadingEntry(\n entry,\n new ModelResidencyError(\n \"load-timeout\",\n `Model load exceeded ${this.#loadTimeoutMs} ms`,\n ),\n true,\n );\n }, this.#loadTimeoutMs);\n }\n\n let adapterPromise: Promise<ModelLoadedResource<Resource>>;\n try {\n adapterPromise = this.#load({\n assetRef: entry.assetRef,\n lod: entry.lod,\n ...(entry.partitionId === undefined\n ? {}\n : { partitionId: entry.partitionId }),\n priority: entry.priority,\n signal: entry.controller.signal,\n });\n } catch (error) {\n adapterPromise = Promise.reject(error);\n }\n const task = Promise.resolve(adapterPromise)\n .then((result) => this.#completeLoad(entry, result))\n .catch((error: unknown) => this.#failLoad(entry, error))\n .finally(() => {\n if (entry.timeout !== undefined) clearTimeout(entry.timeout);\n entry.timeout = undefined;\n entry.controller = undefined;\n this.#releaseLoadSlot(entry);\n entry.loadTask = undefined;\n });\n entry.loadTask = task;\n this.#loadTasks.add(task);\n }\n\n async #completeLoad(\n entry: ResidencyEntry<Resource>,\n result: ModelLoadedResource<Resource>,\n ): Promise<void> {\n if (entry.abandoned) {\n await this.#disposeLoaded(result);\n return;\n }\n\n try {\n this.#validateLoadedResource(entry, result);\n const evictions = this.#ensureCapacityForLoadedResult(entry, result);\n await Promise.all(evictions);\n } catch (error) {\n await this.#disposeLoaded(result);\n this.#entries.delete(entry.key);\n this.#rejectWaiters(entry, error);\n this.#metrics.loadsFailed += 1;\n return;\n }\n\n entry.state = \"resident\";\n entry.loaded = result;\n entry.lastUsed = this.#clock++;\n this.#metrics.loadsCompleted += 1;\n const waiters = [...entry.waiters.values()];\n entry.waiters.clear();\n for (const waiter of waiters) {\n this.#detachWaiterSignal(waiter);\n waiter.resolve(this.#createLease(entry, waiter.priority, waiter.pinned));\n }\n }\n\n #failLoad(entry: ResidencyEntry<Resource>, error: unknown): void {\n if (entry.abandoned) return;\n this.#entries.delete(entry.key);\n this.#metrics.loadsFailed += 1;\n this.#rejectWaiters(entry, error);\n }\n\n #abandonLoadingEntry(\n entry: ResidencyEntry<Resource>,\n error: ModelResidencyError,\n rejectWaiters: boolean,\n ): void {\n entry.abandoned = true;\n this.#metrics.loadsAborted += 1;\n if (entry.timeout !== undefined) clearTimeout(entry.timeout);\n entry.timeout = undefined;\n this.#entries.delete(entry.key);\n if (rejectWaiters) this.#rejectWaiters(entry, error);\n entry.controller?.abort(error);\n this.#releaseLoadSlot(entry);\n }\n\n #releaseLoadSlot(entry: ResidencyEntry<Resource>): void {\n if (entry.loadSlotReleased) return;\n entry.loadSlotReleased = true;\n this.#inFlightLoads -= 1;\n this.#loadTasks.delete(entry.loadTask!);\n this.#pumpQueue();\n }\n\n #rejectWaiters(entry: ResidencyEntry<Resource>, error: unknown): void {\n const waiters = [...entry.waiters.values()];\n entry.waiters.clear();\n for (const waiter of waiters) {\n this.#detachWaiterSignal(waiter);\n waiter.reject(error);\n }\n this.#recalculatePriority(entry);\n }\n\n #detachWaiterSignal(waiter: AcquisitionWaiter<Resource>): void {\n if (waiter.signal !== undefined && waiter.onAbort !== undefined) {\n waiter.signal.removeEventListener(\"abort\", waiter.onAbort);\n }\n waiter.onAbort = undefined;\n }\n\n #validateLoadedResource(\n entry: ResidencyEntry<Resource>,\n result: ModelLoadedResource<Resource>,\n ): void {\n if (result === null || typeof result !== \"object\") {\n throw new ModelResidencyError(\n \"invalid-load-result\",\n \"The model loader returned an invalid resource\",\n );\n }\n if (typeof result.dispose !== \"function\") {\n throw new ModelResidencyError(\n \"invalid-load-result\",\n \"The model loader must return a disposer\",\n );\n }\n requireSafeNonNegativeInteger(result.cpuBytes, \"loaded.cpuBytes\");\n requireSafeNonNegativeInteger(result.gpuBytes, \"loaded.gpuBytes\");\n if (result.contentHash !== entry.assetRef.contentHash) {\n throw new ModelResidencyError(\n \"content-hash-mismatch\",\n \"The loaded model content hash does not match its canonical reference\",\n );\n }\n }\n\n async #ensureCapacityForReservation(\n cpuBytes: number,\n gpuBytes: number,\n ): Promise<void> {\n if (\n cpuBytes > this.#budget.maxCpuBytes ||\n gpuBytes > this.#budget.maxGpuBytes\n ) {\n throw this.#budgetError();\n }\n let usage = this.#totalAccountedBytes();\n for (const candidate of this.#evictionCandidates()) {\n if (\n usage.cpu + cpuBytes <= this.#budget.maxCpuBytes &&\n usage.gpu + gpuBytes <= this.#budget.maxGpuBytes\n ) {\n return;\n }\n await this.#evict(candidate);\n usage = this.#totalAccountedBytes();\n }\n if (\n usage.cpu + cpuBytes > this.#budget.maxCpuBytes ||\n usage.gpu + gpuBytes > this.#budget.maxGpuBytes\n ) {\n throw this.#budgetError();\n }\n }\n\n #ensureCapacityForLoadedResult(\n loadingEntry: ResidencyEntry<Resource>,\n result: ModelLoadedResource<Resource>,\n ): Promise<void>[] {\n const usage = this.#totalAccountedBytes();\n usage.cpu += result.cpuBytes - loadingEntry.estimatedCpuBytes;\n usage.gpu += result.gpuBytes - loadingEntry.estimatedGpuBytes;\n const disposals: Promise<void>[] = [];\n for (const candidate of this.#evictionCandidates()) {\n if (\n usage.cpu <= this.#budget.maxCpuBytes &&\n usage.gpu <= this.#budget.maxGpuBytes\n ) {\n return disposals;\n }\n usage.cpu -= candidate.loaded!.cpuBytes;\n usage.gpu -= candidate.loaded!.gpuBytes;\n disposals.push(this.#evict(candidate));\n }\n if (\n usage.cpu > this.#budget.maxCpuBytes ||\n usage.gpu > this.#budget.maxGpuBytes\n ) {\n throw this.#budgetError();\n }\n return disposals;\n }\n\n #budgetError(): ModelResidencyError {\n return new ModelResidencyError(\n \"budget-exceeded\",\n \"Pinned, referenced, or pending models leave insufficient residency budget\",\n );\n }\n\n #totalAccountedBytes(): { cpu: number; gpu: number } {\n let cpu = 0;\n let gpu = 0;\n for (const entry of this.#entries.values()) {\n if (entry.state === \"resident\") {\n cpu += entry.loaded!.cpuBytes;\n gpu += entry.loaded!.gpuBytes;\n } else {\n cpu += entry.estimatedCpuBytes;\n gpu += entry.estimatedGpuBytes;\n }\n }\n return { cpu, gpu };\n }\n\n #evictionCandidates(): ResidencyEntry<Resource>[] {\n return [...this.#entries.values()]\n .filter(\n (entry) =>\n entry.state === \"resident\" &&\n entry.leases.size === 0,\n )\n .sort(\n (left, right) =>\n left.priority - right.priority ||\n left.lastUsed - right.lastUsed,\n );\n }\n\n #evict(entry: ResidencyEntry<Resource>): Promise<void> {\n this.#entries.delete(entry.key);\n this.#metrics.evictions += 1;\n return this.#disposeEntry(entry);\n }\n\n #disposeEntry(entry: ResidencyEntry<Resource>): Promise<void> {\n const result = entry.loaded!;\n entry.loaded = undefined;\n return this.#disposeLoaded(result);\n }\n\n #disposeLoaded(result: ModelLoadedResource<Resource>): Promise<void> {\n this.#metrics.disposals += 1;\n let disposal: Promise<void>;\n try {\n disposal = Promise.resolve(result.dispose());\n } catch (error) {\n disposal = Promise.reject(error);\n }\n const tracked = disposal\n .catch(() => {\n this.#metrics.disposalFailures += 1;\n })\n .finally(() => {\n this.#disposalTasks.delete(tracked);\n });\n this.#disposalTasks.add(tracked);\n return tracked;\n }\n\n #queuedCount(): number {\n let count = 0;\n for (const entry of this.#entries.values()) {\n if (entry.state === \"queued\") count += 1;\n }\n return count;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,mBAAN,MAA6C;AAAA,EACjC,UAAU,oBAAI,IAAqB;AAAA,EAEpD,MAAM,IAAO,KAAqC;AAChD,WAAO,KAAK,QAAQ,IAAI,GAAG;AAAA,EAC7B;AAAA,EAEA,MAAM,IAAO,KAAa,OAAyB;AACjD,SAAK,QAAQ,IAAI,KAAK,KAAK;AAAA,EAC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;AACV,CAAU;","names":["response","module"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/cache.ts","../src/types.ts","../src/bytes.ts","../src/fetch.ts","../src/runtime.ts","../src/model-residency.ts"],"sourcesContent":["export * from \"./cache.js\";\nexport * from \"./fetch.js\";\nexport * from \"./runtime.js\";\nexport * from \"./types.js\";\n\n/** Package identity and rollout metadata for the runtime boundary. */\nexport const packageName = \"@plasius/gpu-model-runtime\" as const;\n\nexport const packageBootstrap = Object.freeze({\n packageName,\n featureFlag: \"gpu.model.conversion.enabled\",\n status: \"runtime\",\n} as const);\n\nexport * from \"./model-residency.js\";\n\nexport { ModelResourceTooLargeError } from \"./bytes.js\";\n","import type { ModelCache } from \"./types.js\";\n\nexport class MemoryModelCache implements ModelCache {\n private readonly entries = new Map<string, unknown>();\n\n async get<T>(key: string): Promise<T | undefined> {\n return this.entries.get(key) as T | undefined;\n }\n\n async set<T>(key: string, value: T): Promise<void> {\n this.entries.set(key, value);\n }\n\n async delete(key: string): Promise<boolean> {\n return this.entries.delete(key);\n }\n\n async clear(): Promise<void> {\n this.entries.clear();\n }\n}\n\nfunction stableValue(value: unknown): unknown {\n if (Array.isArray(value)) {\n return value.map(stableValue);\n }\n\n if (value !== null && typeof value === \"object\") {\n return Object.fromEntries(\n Object.entries(value)\n .sort(([left], [right]) => left.localeCompare(right))\n .map(([key, entry]) => [key, stableValue(entry)]),\n );\n }\n\n return value;\n}\n\nexport function createModelCacheKey(\n contentHash: string,\n formatId: string,\n adapterOptions: unknown,\n): string {\n return `${formatId}:${contentHash}:${JSON.stringify(stableValue(adapterOptions ?? null))}`;\n}\n","import type {\n GpuModelDiagnosticMode, GpuModelDocument,\n ModelSource as CoreModelSource, ModelProvenance as CoreModelProvenance,\n} from \"@plasius/gpu-model-core\";\n\nexport type { GpuModelDiagnosticMode, GpuModelDocument, GpuModelConversionDiagnostic } from \"@plasius/gpu-model-core\";\n\n/** Default ceiling per entrypoint or related resource: 64 MiB. */\nexport const DEFAULT_MODEL_MAX_BYTES = 64 * 1024 * 1024;\n\n/** Canonical source and policy contracts stay owned by the published core. */\nexport type ModelSourceKind = CoreModelSource[\"kind\"];\nexport type ModelProvenance = CoreModelProvenance;\n\nexport type ModelResourceInput = Readonly<{\n bytes: ArrayBuffer | Blob | Uint8Array;\n contentType?: string;\n integrity?: string;\n}>;\n\nexport type ModelPackage = Readonly<{\n entrypoint?: string;\n baseUrl?: string;\n resources?: Readonly<Record<string, ModelResourceInput>>;\n}>;\n\n/** Runtime acquisition extends canonical IO with package-resource resolution. */\nexport type ModelSource = CoreModelSource & Readonly<{ package?: ModelPackage }>;\n\nexport type ResourceReference = Readonly<{\n path: string;\n contentType?: string;\n integrity?: string;\n}>;\n\nexport type ResolvedResource = Readonly<{\n path: string;\n bytes: Uint8Array;\n contentType?: string;\n contentHash: string;\n rangeSupported: boolean;\n}>;\n\nexport interface ResourceResolver {\n resolve(reference: string | ResourceReference, options?: ResolveResourceOptions): Promise<ResolvedResource>;\n}\n\nexport type ResolveResourceOptions = Readonly<{\n /** Tightens the inherited acquisition ceiling for this resource. */\n maxBytes?: number;\n signal?: AbortSignal;\n range?: Readonly<{ start: number; end?: number }>;\n}>;\n\nexport type FetchPolicy = Readonly<{\n /** Maximum bytes per entrypoint/resource, including non-HTTP sources. Default 64 MiB. */\n maxBytes?: number;\n maxAttempts?: number;\n baseDelayMs?: number;\n maxDelayMs?: number;\n timeoutMs?: number;\n}>;\n\nexport type RuntimeFetch = (input: string | URL, init?: RequestInit) => Promise<Response>;\n/** Providers must enforce maxBytes during I/O; runtime also checks returned bytes. */\nexport type ReadFile = (path: string, signal?: AbortSignal, options?: Readonly<{ maxBytes: number }>) => Promise<Uint8Array>;\nexport type HashBytes = (bytes: Uint8Array) => Promise<string>;\nexport type Sleep = (milliseconds: number, signal?: AbortSignal) => Promise<void>;\nexport type BlobStorageUrlResolver = (baseUrl: string, reference: string) => string | Promise<string>;\n\nexport type RuntimeDependencies = Readonly<{\n fetch?: RuntimeFetch;\n readFile?: ReadFile;\n hashBytes?: HashBytes;\n sleep?: Sleep;\n blobStorageUrlResolver?: BlobStorageUrlResolver;\n}>;\n\nexport type ResolvedModelSource = Readonly<{\n source: ModelSource;\n bytes: Uint8Array;\n contentType?: string;\n fileName?: string;\n contentHash: string;\n rangeSupported: boolean;\n resourceResolver: ResourceResolver;\n}>;\n\nexport type SniffInput = Readonly<{\n bytes: Uint8Array;\n contentType?: string;\n fileName?: string;\n sourceKind: ModelSourceKind;\n}>;\n\nexport type AdapterLoadContext = Readonly<{\n signal: AbortSignal;\n mode: GpuModelDiagnosticMode;\n execution: \"main\" | \"worker\";\n adapterOptions: unknown;\n}>;\n\nexport type AdapterLoadResult<TCanonicalModel = GpuModelDocument, TRendererReady = unknown> = Readonly<{\n canonicalModel: TCanonicalModel;\n rendererReady?: TRendererReady;\n diagnostics?: readonly unknown[];\n}>;\n\nexport interface ModelAdapter<TCanonicalModel = GpuModelDocument, TRendererReady = unknown> {\n readonly formatId: string;\n readonly supportsWorker?: boolean;\n sniff(input: SniffInput): number | boolean;\n load(\n input: ResolvedModelSource,\n context: AdapterLoadContext,\n ): Promise<AdapterLoadResult<TCanonicalModel, TRendererReady>>;\n}\n\nexport type AdapterModule<TCanonicalModel, TRendererReady = unknown> =\n | ModelAdapter<TCanonicalModel, TRendererReady>\n | Readonly<{ default: ModelAdapter<TCanonicalModel, TRendererReady> }>;\n\nexport type AdapterRegistration<TCanonicalModel = unknown, TRendererReady = unknown> = Readonly<{\n formatId: string;\n extensions?: readonly string[];\n mimeTypes?: readonly string[];\n sniff?: (input: SniffInput) => number | boolean;\n load: () => Promise<AdapterModule<TCanonicalModel, TRendererReady>>;\n}>;\n\nexport type WorkerLoadRequest = Readonly<{\n formatId: string;\n input: ResolvedModelSource;\n context: AdapterLoadContext;\n}>;\n\nexport interface WorkerDispatcher {\n dispatch<TCanonicalModel, TRendererReady = unknown>(\n request: WorkerLoadRequest,\n ): Promise<AdapterLoadResult<TCanonicalModel, TRendererReady>>;\n}\n\nexport type ModelCache = Readonly<{\n get<T>(key: string): Promise<T | undefined>;\n set<T>(key: string, value: T): Promise<void>;\n delete(key: string): Promise<boolean>;\n clear(): Promise<void>;\n}>;\n\nexport type LoadModelOptions = Readonly<{\n signal?: AbortSignal;\n mode?: GpuModelDiagnosticMode;\n adapterOptions?: unknown;\n formatId?: string;\n fetchPolicy?: FetchPolicy;\n integrity?: string;\n useCache?: boolean;\n worker?: Readonly<{\n mode?: \"auto\" | \"never\" | \"always\";\n }>;\n}>;\n\nexport type ModelLoadResult<TCanonicalModel = GpuModelDocument, TRendererReady = unknown> = Readonly<{\n canonicalModel: TCanonicalModel;\n rendererReady?: TRendererReady;\n diagnostics?: readonly unknown[];\n contentHash: string;\n cacheKey: string;\n fromCache: boolean;\n contentType?: string;\n sourceKind: ModelSourceKind;\n}>;\n","import { DEFAULT_MODEL_MAX_BYTES } from \"./types.js\";\n\n// The intrinsic getter identifies the actual typed-array element kind across\n// realms, without trusting a producer's constructor or Symbol.toStringTag.\nconst typedArrayKind = Object.getOwnPropertyDescriptor(\n Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag,\n)?.get;\n\n/** Non-retryable acquisition failure; never contains a source URI or provider error. */\nexport class ModelResourceTooLargeError extends Error {\n readonly code = \"MODEL_RESOURCE_TOO_LARGE\";\n constructor() {\n super(\"Model resource exceeds maxBytes\");\n this.name = \"ModelResourceTooLargeError\";\n }\n}\n\nexport function byteLimit(value = DEFAULT_MODEL_MAX_BYTES): number {\n if (!Number.isSafeInteger(value) || value <= 0) throw new RangeError(\"maxBytes must be a positive safe integer\");\n return value;\n}\n\nexport function checkSize(size: number, maxBytes: number): void {\n if (size > maxBytes) throw new ModelResourceTooLargeError();\n}\n\nexport function checkAbort(signal?: AbortSignal): void {\n if (signal?.aborted) throw new DOMException(\"The model load was aborted\", \"AbortError\");\n}\n\nexport function acquisitionController(signal?: AbortSignal, timeoutMs = 30_000, parent?: AbortSignal) {\n if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2_147_483_647) {\n throw new RangeError(\"timeoutMs must be a positive timer-safe integer\");\n }\n const controller = new AbortController();\n const abort = () => controller.abort();\n signal?.addEventListener(\"abort\", abort, { once: true });\n parent?.addEventListener(\"abort\", abort, { once: true });\n if (signal?.aborted || parent?.aborted) controller.abort();\n const timer = setTimeout(abort, timeoutMs);\n return {\n signal: controller.signal,\n dispose() {\n clearTimeout(timer);\n signal?.removeEventListener(\"abort\", abort);\n parent?.removeEventListener(\"abort\", abort);\n },\n };\n}\n\n/** Stop waiting even when an injected producer ignores cancellation. */\nexport async function abortable<T>(work: Promise<T>, signal: AbortSignal): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n const abort = () => {\n signal.removeEventListener(\"abort\", abort);\n reject(new DOMException(\"The model load was aborted\", \"AbortError\"));\n };\n signal.addEventListener(\"abort\", abort, { once: true });\n if (signal.aborted) abort();\n work.then(\n (value) => { signal.removeEventListener(\"abort\", abort); resolve(value); },\n (error: unknown) => { signal.removeEventListener(\"abort\", abort); reject(error); },\n );\n });\n}\n\n/** Invoke cleanup without allowing an uncooperative producer to stall failure. */\nexport function cleanup(work: () => unknown): void {\n try { void Promise.resolve(work()).catch(() => {}); } catch { /* Preserve the original failure. */ }\n}\n\nexport async function readBoundedStream(\n source: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array>,\n maxBytes: number,\n signal: AbortSignal,\n): Promise<Uint8Array> {\n // Geometric storage bounds both retained bytes and metadata for tiny/empty chunks.\n let bytes = new Uint8Array(0);\n let length = 0;\n let chunks = 0;\n const reader = \"getReader\" in source ? source.getReader() : undefined;\n const iterator = reader ? undefined : (source as AsyncIterable<Uint8Array>)[Symbol.asyncIterator]();\n try {\n while (true) {\n checkAbort(signal);\n if (++chunks % 256 === 0) await abortable(new Promise<void>((resolve) => setTimeout(resolve, 0)), signal);\n const next = await abortable(reader ? reader.read() : iterator!.next(), signal);\n checkAbort(signal);\n if (next.done) break;\n if (!ArrayBuffer.isView(next.value) || typedArrayKind?.call(next.value) !== \"Uint8Array\") throw new TypeError(\"Model stream chunks must be Uint8Array\");\n const chunk = next.value;\n if (chunk.byteLength > maxBytes - length) throw new ModelResourceTooLargeError();\n const required = length + chunk.byteLength;\n if (required > bytes.length) {\n const grown = new Uint8Array(Math.min(maxBytes, Math.max(required, bytes.length * 2, 4096)));\n grown.set(bytes.subarray(0, length));\n bytes = grown;\n }\n bytes.set(chunk, length);\n length = required;\n }\n return bytes.length === length ? bytes : bytes.slice(0, length);\n } catch (error) {\n if (reader) cleanup(() => reader.cancel());\n else cleanup(() => iterator?.return?.());\n throw error;\n } finally {\n reader?.releaseLock();\n }\n}\n\nexport async function boundedBytes(input: ArrayBuffer | Blob | Uint8Array, maxBytes: number, signal: AbortSignal): Promise<Uint8Array> {\n checkAbort(signal);\n checkSize(input instanceof Blob ? input.size : input.byteLength, maxBytes);\n if (input instanceof Blob) return readBoundedStream(input.stream(), maxBytes, signal);\n return new Uint8Array(input);\n}\n","import { abortable, acquisitionController, byteLimit, checkAbort, checkSize, cleanup, ModelResourceTooLargeError, readBoundedStream } from \"./bytes.js\";\nimport { DEFAULT_MODEL_MAX_BYTES } from \"./types.js\";\nimport type {\n FetchPolicy,\n HashBytes,\n RuntimeFetch,\n Sleep,\n ResolveResourceOptions,\n} from \"./types.js\";\n\nconst DEFAULT_POLICY: Required<FetchPolicy> = {\n maxBytes: DEFAULT_MODEL_MAX_BYTES,\n maxAttempts: 3,\n baseDelayMs: 100,\n maxDelayMs: 1_000,\n timeoutMs: 30_000,\n};\n\nconst RETRYABLE_STATUSES = new Set([408, 425, 429, 500, 502, 503, 504]);\n\nclass NonRetryableResourceError extends Error {}\n\nexport type FetchedResource = Readonly<{\n bytes: Uint8Array;\n contentType?: string;\n rangeSupported: boolean;\n}>;\n\nexport function defaultSleep(milliseconds: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n if (signal?.aborted) {\n reject(createAbortError());\n return;\n }\n\n const timer = setTimeout(resolve, milliseconds);\n signal?.addEventListener(\"abort\", () => {\n clearTimeout(timer);\n reject(createAbortError());\n }, { once: true });\n });\n}\n\nexport async function sha256Hex(bytes: Uint8Array): Promise<string> {\n if (!globalThis.crypto?.subtle) {\n throw new Error(\"SHA-256 integrity checks require Web Crypto support or an injected hashBytes dependency\");\n }\n\n const digest = await globalThis.crypto.subtle.digest(\"SHA-256\", bytes as BufferSource);\n return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nexport async function fetchResource(\n url: string,\n options: Readonly<{\n fetch: RuntimeFetch;\n hashBytes: HashBytes;\n sleep: Sleep;\n headers?: Readonly<Record<string, string>>;\n integrity?: string;\n policy?: FetchPolicy;\n resolveOptions?: ResolveResourceOptions;\n }>,\n): Promise<FetchedResource> {\n const policy = { ...DEFAULT_POLICY, ...options.policy };\n const maxBytes = Math.min(byteLimit(policy.maxBytes), byteLimit(options.resolveOptions?.maxBytes ?? policy.maxBytes));\n const headers = new Headers(options.headers);\n if (options.resolveOptions?.range) {\n const { start, end } = options.resolveOptions.range;\n headers.set(\"Range\", `bytes=${start}-${end ?? \"\"}`);\n }\n\n let lastError: unknown;\n for (let attempt = 1; attempt <= policy.maxAttempts; attempt += 1) {\n const controller = acquisitionController(options.resolveOptions?.signal, policy.timeoutMs);\n\n try {\n checkAbort(controller.signal);\n const response = await abortable(options.fetch(url, {\n headers,\n signal: controller.signal,\n }).then((response) => {\n if (controller.signal.aborted) {\n cleanup(() => response.body?.cancel());\n checkAbort(controller.signal);\n }\n return response;\n }), controller.signal);\n\n if (!response.ok) {\n cleanup(() => response.body?.cancel());\n if (!RETRYABLE_STATUSES.has(response.status)) {\n throw new NonRetryableResourceError(`Model resource request failed with HTTP ${response.status}`);\n }\n if (attempt === policy.maxAttempts) {\n throw new NonRetryableResourceError(`Model resource request failed with HTTP ${response.status}`);\n }\n lastError = new Error(`Retryable model resource response: HTTP ${response.status}`);\n await waitForRetry(options.sleep, attempt, policy, controller.signal);\n continue;\n }\n\n let bytes: Uint8Array;\n try {\n const declared = response.headers.get(\"content-length\");\n if (declared !== null && /^\\d+$/u.test(declared)) checkSize(Number(declared), maxBytes);\n bytes = response.body ? await readBoundedStream(response.body, maxBytes, controller.signal) : new Uint8Array(0);\n checkAbort(controller.signal);\n } catch (error) {\n if (!response.body?.locked) cleanup(() => response.body?.cancel());\n throw error;\n }\n await abortable(verifyIntegrity(bytes, options.integrity, options.hashBytes), controller.signal);\n return {\n bytes,\n contentType: response.headers.get(\"content-type\") ?? undefined,\n rangeSupported: response.status === 206 || response.headers.get(\"accept-ranges\")?.toLowerCase() === \"bytes\",\n };\n } catch (error) {\n if (isAbortError(error) || options.resolveOptions?.signal?.aborted) {\n throw createAbortError();\n }\n if (error instanceof NonRetryableResourceError || error instanceof ModelResourceTooLargeError) {\n throw error;\n }\n lastError = error;\n if (attempt === policy.maxAttempts) {\n throw error;\n }\n await waitForRetry(options.sleep, attempt, policy, controller.signal);\n } finally {\n controller.dispose();\n }\n }\n\n throw lastError instanceof Error ? lastError : new Error(\"Model resource request failed\");\n}\n\nexport async function verifyIntegrity(bytes: Uint8Array, integrity: string | undefined, hashBytes: HashBytes): Promise<void> {\n if (!integrity) {\n return;\n }\n\n const expected = integrity.replace(/^sha256-/iu, \"\");\n if (/^[0-9a-f]{64}$/iu.test(expected)) {\n const actualHex = await hashBytes(bytes);\n if (expected.toLowerCase() === actualHex.toLowerCase()) return;\n } else if (/^[A-Za-z0-9+/]{43}=$/u.test(expected)) {\n const actualBase64 = bytesToBase64(await digestBytes(bytes, hashBytes));\n if (expected === actualBase64) return;\n }\n throw new NonRetryableResourceError(\"Model resource integrity check failed\");\n}\n\nasync function digestBytes(bytes: Uint8Array, hashBytes: HashBytes): Promise<Uint8Array> {\n if (globalThis.crypto?.subtle) {\n return new Uint8Array(await globalThis.crypto.subtle.digest(\"SHA-256\", bytes as BufferSource));\n }\n\n const hex = await hashBytes(bytes);\n if (!/^[0-9a-f]{64}$/iu.test(hex)) {\n throw new Error(\"A base64 integrity check requires Web Crypto or a byte-level hash implementation\");\n }\n return Uint8Array.from(hex.match(/.{2}/gu) ?? [], (pair) => Number.parseInt(pair, 16));\n}\n\nfunction bytesToBase64(bytes: Uint8Array): string {\n let binary = \"\";\n for (const byte of bytes) {\n binary += String.fromCharCode(byte);\n }\n return btoa(binary);\n}\n\nasync function waitForRetry(sleep: Sleep, attempt: number, policy: Required<FetchPolicy>, signal: AbortSignal): Promise<void> {\n const delay = Math.min(policy.maxDelayMs, policy.baseDelayMs * 2 ** (attempt - 1));\n await abortable(sleep(delay, signal), signal);\n}\n\nfunction createAbortError(): Error {\n return new DOMException(\"The model load was aborted\", \"AbortError\");\n}\n\nfunction isAbortError(error: unknown): boolean {\n return error instanceof DOMException && error.name === \"AbortError\";\n}\n","import { abortable, acquisitionController, boundedBytes, byteLimit, checkAbort, checkSize, readBoundedStream } from \"./bytes.js\";\nimport { MemoryModelCache, createModelCacheKey } from \"./cache.js\";\nimport { defaultSleep, fetchResource, sha256Hex, verifyIntegrity } from \"./fetch.js\";\nimport type {\n AdapterLoadResult,\n AdapterModule,\n AdapterRegistration,\n HashBytes,\n LoadModelOptions,\n ModelAdapter,\n ModelCache,\n ModelLoadResult,\n ModelPackage,\n ModelResourceInput,\n ModelSource,\n ModelSourceKind,\n ReadFile,\n ResolveResourceOptions,\n ResolvedModelSource,\n ResolvedResource,\n ResourceReference,\n ResourceResolver,\n RuntimeDependencies,\n RuntimeFetch,\n Sleep,\n SniffInput,\n WorkerDispatcher,\n WorkerLoadRequest,\n} from \"./types.js\";\n\nexport class AdapterRegistry {\n private readonly registrations = new Map<string, AdapterRegistration>();\n\n register<TCanonicalModel, TRendererReady>(registration: AdapterRegistration<TCanonicalModel, TRendererReady>): this {\n if (this.registrations.has(registration.formatId)) {\n throw new Error(`An adapter is already registered for format ${registration.formatId}`);\n }\n this.registrations.set(registration.formatId, registration as AdapterRegistration);\n return this;\n }\n\n async resolve<TCanonicalModel = unknown, TRendererReady = unknown>(\n input: SniffInput,\n formatId?: string,\n ): Promise<ModelAdapter<TCanonicalModel, TRendererReady>> {\n if (formatId) {\n const registration = this.registrations.get(formatId);\n if (!registration) {\n throw new Error(`No model adapter is registered for format ${formatId}`);\n }\n return unwrapAdapter(await registration.load()) as ModelAdapter<TCanonicalModel, TRendererReady>;\n }\n\n const candidates = [...this.registrations.values()]\n .map((registration) => ({ registration, score: scoreRegistration(registration, input) }))\n .filter(({ score }) => score > 0)\n .sort((left, right) => right.score - left.score);\n\n const selected = candidates[0]?.registration;\n if (!selected) {\n throw new Error(\"No model adapter matched the source content, MIME type, or extension\");\n }\n\n return unwrapAdapter(await selected.load()) as ModelAdapter<TCanonicalModel, TRendererReady>;\n }\n}\n\nexport class PackageResourceResolver implements ResourceResolver {\n private readonly parentSignal?: AbortSignal;\n private readonly memoryResources: Readonly<Record<string, ModelResourceInput>>;\n private readonly baseUrl?: string;\n private readonly sourceKind: ModelSourceKind;\n private readonly fetch: RuntimeFetch;\n private readonly hashBytes: HashBytes;\n private readonly sleep: Sleep;\n private readonly fetchPolicy: LoadModelOptions[\"fetchPolicy\"];\n private readonly headers?: Readonly<Record<string, string>>;\n private readonly blobStorageUrlResolver?: RuntimeDependencies[\"blobStorageUrlResolver\"];\n\n public constructor(options: Readonly<{\n sourceKind: ModelSourceKind;\n signal?: AbortSignal;\n baseUrl?: string;\n package?: ModelPackage;\n fetch: RuntimeFetch;\n hashBytes: HashBytes;\n sleep: Sleep;\n fetchPolicy?: LoadModelOptions[\"fetchPolicy\"];\n headers?: Readonly<Record<string, string>>;\n blobStorageUrlResolver?: RuntimeDependencies[\"blobStorageUrlResolver\"];\n }>) {\n this.parentSignal = options.signal;\n this.sourceKind = options.sourceKind;\n this.baseUrl = options.package?.baseUrl ?? options.baseUrl;\n this.memoryResources = options.package?.resources ?? {};\n this.fetch = options.fetch;\n this.hashBytes = options.hashBytes;\n this.sleep = options.sleep;\n this.fetchPolicy = { ...options.fetchPolicy };\n this.headers = options.headers;\n this.blobStorageUrlResolver = options.blobStorageUrlResolver;\n }\n\n async resolve(reference: string | ResourceReference, options: ResolveResourceOptions = {}): Promise<ResolvedResource> {\n const inheritedLimit = byteLimit(this.fetchPolicy?.maxBytes);\n const maxBytes = Math.min(inheritedLimit, byteLimit(options.maxBytes ?? inheritedLimit));\n const controller = acquisitionController(options.signal, this.fetchPolicy?.timeoutMs, this.parentSignal);\n try {\n checkAbort(controller.signal);\n const requested = typeof reference === \"string\" ? { path: reference } : reference;\n const memoryKey = normalizePackagePath(requested.path);\n const memoryResource = this.memoryResources[memoryKey] ?? this.memoryResources[`./${memoryKey}`];\n if (memoryResource) {\n const bytes = await boundedBytes(memoryResource.bytes, maxBytes, controller.signal);\n await abortable(verifyIntegrity(bytes, requested.integrity ?? memoryResource.integrity, this.hashBytes), controller.signal);\n return {\n path: memoryKey,\n bytes,\n contentType: requested.contentType ?? memoryResource.contentType,\n contentHash: await abortable(this.hashBytes(bytes), controller.signal),\n rangeSupported: false,\n };\n }\n\n const url = await abortable(this.resolveUrl(requested.path), controller.signal);\n checkAbort(controller.signal);\n const result = await fetchResource(url, {\n fetch: this.fetch,\n hashBytes: this.hashBytes,\n sleep: this.sleep,\n headers: this.headers,\n integrity: requested.integrity,\n policy: this.fetchPolicy,\n resolveOptions: { ...options, maxBytes, signal: controller.signal },\n });\n return {\n path: memoryKey,\n bytes: result.bytes,\n contentType: requested.contentType ?? result.contentType,\n contentHash: await abortable(this.hashBytes(result.bytes), controller.signal),\n rangeSupported: result.rangeSupported,\n };\n } finally {\n controller.dispose();\n }\n }\n\n private async resolveUrl(reference: string): Promise<string> {\n if (/^[a-z][a-z\\d+.-]*:/iu.test(reference)) {\n return ensureHttpUrl(reference);\n }\n if (!this.baseUrl) {\n throw new Error(`Cannot resolve relative model resource ${reference} without a package base URL or memory resource`);\n }\n if (this.sourceKind === \"blob-storage-url\" && this.blobStorageUrlResolver) {\n return this.blobStorageUrlResolver(this.baseUrl, reference);\n }\n return ensureHttpUrl(new URL(reference, this.baseUrl).href);\n }\n}\n\nexport class ModelRuntime {\n private readonly registry: AdapterRegistry;\n private readonly cache: ModelCache;\n private readonly fetch: RuntimeFetch;\n private readonly readFile?: ReadFile;\n private readonly hashBytes: HashBytes;\n private readonly sleep: Sleep;\n private readonly workerDispatcher?: WorkerDispatcher;\n private readonly blobStorageUrlResolver?: RuntimeDependencies[\"blobStorageUrlResolver\"];\n private readonly cacheKeys = new Set<string>();\n\n public constructor(options: Readonly<{\n registry?: AdapterRegistry;\n cache?: ModelCache;\n dependencies?: RuntimeDependencies;\n workerDispatcher?: WorkerDispatcher;\n }> = {}) {\n this.registry = options.registry ?? new AdapterRegistry();\n this.cache = options.cache ?? new MemoryModelCache();\n this.fetch = options.dependencies?.fetch ?? defaultFetch;\n this.readFile = options.dependencies?.readFile;\n this.hashBytes = options.dependencies?.hashBytes ?? sha256Hex;\n this.sleep = options.dependencies?.sleep ?? defaultSleep;\n this.workerDispatcher = options.workerDispatcher;\n this.blobStorageUrlResolver = options.dependencies?.blobStorageUrlResolver;\n }\n\n public get adapters(): AdapterRegistry {\n return this.registry;\n }\n\n public async invalidate(contentHash?: string): Promise<void> {\n if (!contentHash) {\n await this.cache.clear();\n this.cacheKeys.clear();\n return;\n }\n\n for (const key of this.cacheKeys) {\n if (key.includes(`:${contentHash}:`)) {\n await this.cache.delete(key);\n this.cacheKeys.delete(key);\n }\n }\n }\n\n public async loadModel<TCanonicalModel, TRendererReady = unknown>(\n source: ModelSource,\n options: LoadModelOptions = {},\n ): Promise<ModelLoadResult<TCanonicalModel, TRendererReady>> {\n const input = await this.resolveSource(source, options);\n const adapter = await this.registry.resolve<TCanonicalModel, TRendererReady>({\n bytes: input.bytes,\n contentType: input.contentType,\n fileName: input.fileName,\n sourceKind: source.kind,\n }, options.formatId);\n const workerMode = options.worker?.mode ?? \"auto\";\n const canUseWorker = workerMode !== \"never\" && Boolean(this.workerDispatcher) && Boolean(adapter.supportsWorker);\n if (workerMode === \"always\" && !canUseWorker) {\n throw new Error(`Adapter ${adapter.formatId} does not support the requested worker execution`);\n }\n const context = {\n signal: options.signal ?? new AbortController().signal,\n mode: options.mode ?? \"strict\",\n execution: canUseWorker ? \"worker\" as const : \"main\" as const,\n adapterOptions: options.adapterOptions ?? null,\n };\n const cacheKey = createModelCacheKey(input.contentHash, adapter.formatId, {\n adapterOptions: context.adapterOptions,\n maxBytes: byteLimit(options.fetchPolicy?.maxBytes),\n mode: context.mode,\n execution: context.execution,\n });\n const useCache = options.useCache ?? true;\n if (useCache) {\n const cached = await this.cache.get<ModelLoadResult<TCanonicalModel, TRendererReady>>(cacheKey);\n if (cached) {\n return { ...cached, fromCache: true };\n }\n }\n\n const adapterResult = await this.loadWithExecution<TCanonicalModel, TRendererReady>(adapter, input, context);\n const result: ModelLoadResult<TCanonicalModel, TRendererReady> = {\n ...adapterResult,\n contentHash: input.contentHash,\n cacheKey,\n fromCache: false,\n contentType: input.contentType,\n sourceKind: source.kind,\n };\n if (useCache) {\n await this.cache.set(cacheKey, result);\n this.cacheKeys.add(cacheKey);\n }\n return result;\n }\n\n private async loadWithExecution<TCanonicalModel, TRendererReady>(\n adapter: ModelAdapter<TCanonicalModel, TRendererReady>,\n input: ResolvedModelSource,\n context: Parameters<ModelAdapter<TCanonicalModel, TRendererReady>[\"load\"]>[1],\n ): Promise<AdapterLoadResult<TCanonicalModel, TRendererReady>> {\n if (context.execution === \"worker\" && this.workerDispatcher) {\n const workerRequest: WorkerLoadRequest = {\n formatId: adapter.formatId,\n input,\n context: { ...context, execution: \"worker\" },\n };\n return this.workerDispatcher.dispatch<TCanonicalModel, TRendererReady>(workerRequest);\n }\n return adapter.load(input, context);\n }\n\n private async resolveSource(source: ModelSource, options: LoadModelOptions): Promise<ResolvedModelSource> {\n const maxBytes = byteLimit(options.fetchPolicy?.maxBytes);\n const controller = acquisitionController(options.signal, options.fetchPolicy?.timeoutMs);\n try {\n checkAbort(controller.signal);\n const fileName = sourceFileName(source);\n const baseUrl = \"url\" in source ? source.url : undefined;\n const headers = \"headers\" in source ? source.headers : undefined;\n let bytes: Uint8Array;\n let contentType: string | undefined = \"mimeTypeHint\" in source ? source.mimeTypeHint : undefined;\n let rangeSupported = false;\n\n if (source.kind === \"file-path\") {\n if (!this.readFile) {\n throw new Error(\"file-path sources require a readFile dependency\");\n }\n bytes = await abortable(this.readFile(source.path, controller.signal, { maxBytes }), controller.signal);\n checkSize(bytes.byteLength, maxBytes);\n } else if (source.kind === \"url\" || source.kind === \"blob-storage-url\") {\n const result = await fetchResource(ensureHttpUrl(source.url), {\n fetch: this.fetch,\n hashBytes: this.hashBytes,\n sleep: this.sleep,\n headers,\n integrity: options.integrity,\n policy: options.fetchPolicy,\n resolveOptions: { signal: controller.signal },\n });\n bytes = result.bytes;\n contentType ??= result.contentType;\n rangeSupported = result.rangeSupported;\n } else {\n bytes = await sourceToBytes(source, maxBytes, controller.signal);\n }\n\n checkAbort(controller.signal);\n const contentHash = await abortable(this.hashBytes(bytes), controller.signal);\n const resourceResolver = new PackageResourceResolver({\n sourceKind: source.kind,\n signal: options.signal,\n baseUrl,\n package: source.package,\n fetch: this.fetch,\n hashBytes: this.hashBytes,\n sleep: this.sleep,\n fetchPolicy: options.fetchPolicy,\n headers,\n blobStorageUrlResolver: this.blobStorageUrlResolver,\n });\n return {\n source,\n bytes,\n contentType,\n fileName,\n contentHash,\n rangeSupported,\n resourceResolver,\n };\n } finally {\n controller.dispose();\n }\n }\n}\n\nfunction scoreRegistration(registration: AdapterRegistration, input: SniffInput): number {\n const extension = input.fileName?.split(\".\").pop()?.toLowerCase();\n const extensionScore = extension && registration.extensions?.some((item) => item.replace(/^\\./u, \"\").toLowerCase() === extension) ? 2 : 0;\n const mimeScore = input.contentType && registration.mimeTypes?.some((item) => item.toLowerCase() === input.contentType?.toLowerCase()) ? 3 : 0;\n const sniffScore = registration.sniff?.(input) ?? 0;\n const normalizedSniffScore = sniffScore === true ? 1 : sniffScore === false ? 0 : sniffScore;\n return extensionScore + mimeScore + normalizedSniffScore;\n}\n\nfunction unwrapAdapter<TCanonicalModel, TRendererReady>(module: AdapterModule<TCanonicalModel, TRendererReady>): ModelAdapter<TCanonicalModel, TRendererReady> {\n return \"default\" in module ? module.default : module;\n}\n\nfunction normalizePackagePath(path: string): string {\n if (path.includes(\"\\0\")) {\n throw new Error(\"Model resource paths cannot contain null bytes\");\n }\n const segments: string[] = [];\n for (const segment of path.replaceAll(\"\\\\\", \"/\").split(\"/\")) {\n if (!segment || segment === \".\") continue;\n if (segment === \"..\") {\n if (segments.length === 0) {\n throw new Error(`Model resource path escapes its package: ${path}`);\n }\n segments.pop();\n continue;\n }\n segments.push(segment);\n }\n return segments.join(\"/\");\n}\n\nasync function sourceToBytes(source: Exclude<ModelSource, { kind: \"file-path\" | \"url\" | \"blob-storage-url\" }>, maxBytes: number, signal: AbortSignal): Promise<Uint8Array> {\n if (source.kind === \"blob\") return boundedBytes(source.blob, maxBytes, signal);\n if (source.kind === \"array-buffer\" || source.kind === \"uint8-array\") return boundedBytes(source.bytes, maxBytes, signal);\n if (source.byteLengthHint !== undefined) checkSize(source.byteLengthHint, maxBytes);\n return readBoundedStream(source.stream, maxBytes, signal);\n}\n\nconst defaultFetch: RuntimeFetch = (input, init) => {\n if (!globalThis.fetch) {\n throw new Error(\"URL sources require fetch or an injected fetch dependency\");\n }\n return globalThis.fetch(input, init);\n};\n\nfunction sourceFileName(source: ModelSource): string | undefined {\n if (\"fileNameHint\" in source && source.fileNameHint) {\n return source.fileNameHint;\n }\n if (source.kind === \"file-path\") {\n return source.path.split(/[\\\\/]/u).pop();\n }\n if (source.kind === \"url\" || source.kind === \"blob-storage-url\") {\n try {\n return new URL(source.url).pathname.split(\"/\").pop() || undefined;\n } catch {\n return undefined;\n }\n }\n return undefined;\n}\n\nfunction ensureHttpUrl(value: string): string {\n const url = new URL(value);\n if (url.protocol !== \"http:\" && url.protocol !== \"https:\") {\n throw new Error(`Unsupported model resource URL scheme: ${url.protocol}`);\n }\n return url.href;\n}\n","import type {\n ModelAssetRef,\n ModelLodLevel,\n} from \"@plasius/asset-contracts\";\n\nconst SHA256_PATTERN = /^[a-f0-9]{64}$/u;\nconst PARTITION_ID_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/u;\n\n/** Hard CPU, GPU, and concurrent-load limits for one residency manager. */\nexport interface ModelResidencyBudget {\n readonly maxCpuBytes: number;\n readonly maxGpuBytes: number;\n readonly maxInFlightLoads: number;\n}\n\n/** Adapter request for one immutable model LOD or partition. */\nexport interface ModelLoadRequest {\n readonly assetRef: ModelAssetRef;\n readonly lod: ModelLodLevel;\n readonly partitionId?: string;\n readonly priority: number;\n readonly signal: AbortSignal;\n}\n\n/** Resource returned by a format-specific loader/upload adapter. */\nexport interface ModelLoadedResource<Resource> {\n readonly resource: Resource;\n readonly contentHash: string;\n readonly cpuBytes: number;\n readonly gpuBytes: number;\n readonly dispose: () => void | Promise<void>;\n}\n\n/** Options for acquiring a shared model resource. */\nexport interface AcquireModelOptions {\n readonly assetRef: ModelAssetRef;\n readonly lod: ModelLodLevel;\n readonly partitionId?: string;\n readonly priority: number;\n readonly estimatedCpuBytes: number;\n readonly estimatedGpuBytes: number;\n readonly signal?: AbortSignal;\n readonly pinned?: boolean;\n}\n\n/** Idempotent reference to one shared resident model resource. */\nexport interface ModelResidencyLease<Resource> {\n readonly key: string;\n readonly assetRef: ModelAssetRef;\n readonly lod: ModelLodLevel;\n readonly partitionId?: string;\n readonly resource: Resource;\n release(): void;\n setPriority(priority: number): void;\n setPinned(pinned: boolean): void;\n}\n\n/** Construction boundary for adapter-driven model loading. */\nexport interface ModelResidencyManagerOptions<Resource> {\n readonly budget: ModelResidencyBudget;\n readonly load: (\n request: ModelLoadRequest,\n ) => Promise<ModelLoadedResource<Resource>>;\n readonly loadTimeoutMs?: number;\n}\n\nexport type ModelResidencyErrorCode =\n | \"acquire-aborted\"\n | \"budget-exceeded\"\n | \"content-hash-mismatch\"\n | \"invalid-load-result\"\n | \"load-timeout\"\n | \"manager-closed\";\n\n/** Stable operational error returned by residency-specific failure paths. */\nexport class ModelResidencyError extends Error {\n readonly code: ModelResidencyErrorCode;\n\n constructor(code: ModelResidencyErrorCode, message: string) {\n super(message);\n this.name = \"ModelResidencyError\";\n this.code = code;\n }\n}\n\nexport interface ModelResidencyEntrySnapshot {\n readonly key: string;\n readonly state: \"queued\" | \"loading\" | \"resident\";\n readonly priority: number;\n readonly references: number;\n readonly pinned: boolean;\n readonly cpuBytes: number;\n readonly gpuBytes: number;\n}\n\n/** Immutable accounting and lifetime metrics for diagnostics. */\nexport interface ModelResidencySnapshot {\n readonly maxCpuBytes: number;\n readonly maxGpuBytes: number;\n readonly maxInFlightLoads: number;\n readonly cpuBytes: number;\n readonly gpuBytes: number;\n readonly reservedCpuBytes: number;\n readonly reservedGpuBytes: number;\n readonly residentCount: number;\n readonly pendingCount: number;\n readonly referenceCount: number;\n readonly pinnedCount: number;\n readonly inFlightLoads: number;\n readonly queuedLoads: number;\n readonly acquireCount: number;\n readonly cacheHits: number;\n readonly deduplicatedAcquires: number;\n readonly loadsStarted: number;\n readonly loadsCompleted: number;\n readonly loadsFailed: number;\n readonly loadsAborted: number;\n readonly loadsTimedOut: number;\n readonly evictions: number;\n readonly disposals: number;\n readonly disposalFailures: number;\n readonly residentKeys: readonly string[];\n readonly entries: readonly ModelResidencyEntrySnapshot[];\n}\n\ninterface AcquisitionWaiter<Resource> {\n readonly id: number;\n priority: number;\n pinned: boolean;\n readonly signal?: AbortSignal;\n readonly resolve: (lease: ModelResidencyLease<Resource>) => void;\n readonly reject: (reason: unknown) => void;\n onAbort?: () => void;\n}\n\ninterface LeaseRecord {\n readonly id: number;\n active: boolean;\n priority: number;\n pinned: boolean;\n}\n\ninterface ResidencyEntry<Resource> {\n readonly key: string;\n readonly assetRef: ModelAssetRef;\n readonly lod: ModelLodLevel;\n readonly partitionId?: string;\n readonly estimatedCpuBytes: number;\n readonly estimatedGpuBytes: number;\n readonly queueOrder: number;\n state: \"queued\" | \"loading\" | \"resident\";\n priority: number;\n retainedPriority: number;\n lastUsed: number;\n abandoned: boolean;\n loadSlotReleased: boolean;\n waiters: Map<number, AcquisitionWaiter<Resource>>;\n leases: Map<number, LeaseRecord>;\n controller?: AbortController;\n timeout?: ReturnType<typeof setTimeout>;\n loaded?: ModelLoadedResource<Resource>;\n loadTask?: Promise<void>;\n}\n\ninterface MutableMetrics {\n acquireCount: number;\n cacheHits: number;\n deduplicatedAcquires: number;\n loadsStarted: number;\n loadsCompleted: number;\n loadsFailed: number;\n loadsAborted: number;\n loadsTimedOut: number;\n evictions: number;\n disposals: number;\n disposalFailures: number;\n}\n\nfunction requireSafeNonNegativeInteger(value: number, name: string): void {\n if (!Number.isSafeInteger(value) || value < 0) {\n throw new RangeError(`${name} must be a non-negative safe integer`);\n }\n}\n\nfunction requirePositiveSafeInteger(value: number, name: string): void {\n if (!Number.isSafeInteger(value) || value <= 0) {\n throw new RangeError(`${name} must be a positive safe integer`);\n }\n}\n\nfunction requirePriority(priority: number): void {\n if (!Number.isFinite(priority) || priority < 0) {\n throw new RangeError(\"priority must be a non-negative finite number\");\n }\n}\n\nfunction requireLod(lod: ModelLodLevel): void {\n if (!Number.isInteger(lod) || lod < 0 || lod > 3) {\n throw new RangeError(\"lod must be an integer from 0 to 3\");\n }\n}\n\n/** Build the canonical content-hash, LOD, and optional partition cache key. */\nexport function createModelResidencyKey(\n assetRef: ModelAssetRef,\n lod: ModelLodLevel,\n partitionId?: string,\n): string {\n if (!SHA256_PATTERN.test(assetRef.contentHash)) {\n throw new Error(\"ModelAssetRef.contentHash must be a lowercase SHA-256 value\");\n }\n requireLod(lod);\n if (partitionId !== undefined && !PARTITION_ID_PATTERN.test(partitionId)) {\n throw new Error(\"partitionId must be a bounded runtime token\");\n }\n return `${assetRef.contentHash}:lod${lod}:${partitionId ?? \"whole\"}`;\n}\n\nfunction closedError(): ModelResidencyError {\n return new ModelResidencyError(\n \"manager-closed\",\n \"The model residency manager is closed\",\n );\n}\n\nfunction abortedError(): ModelResidencyError {\n return new ModelResidencyError(\n \"acquire-aborted\",\n \"The model residency acquisition was aborted\",\n );\n}\n\nfunction signalIsAborted(signal?: AbortSignal): boolean {\n return signal?.aborted === true;\n}\n\n/**\n * Content-addressed model cache with bounded work, reference-counted leases,\n * deterministic eviction, and exact-once adapter disposal.\n */\nexport class ModelResidencyManager<Resource = unknown> {\n readonly #budget: ModelResidencyBudget;\n readonly #load: (\n request: ModelLoadRequest,\n ) => Promise<ModelLoadedResource<Resource>>;\n readonly #loadTimeoutMs?: number;\n readonly #entries = new Map<string, ResidencyEntry<Resource>>();\n readonly #loadTasks = new Set<Promise<void>>();\n readonly #disposalTasks = new Set<Promise<void>>();\n readonly #metrics: MutableMetrics = {\n acquireCount: 0,\n cacheHits: 0,\n deduplicatedAcquires: 0,\n loadsStarted: 0,\n loadsCompleted: 0,\n loadsFailed: 0,\n loadsAborted: 0,\n loadsTimedOut: 0,\n evictions: 0,\n disposals: 0,\n disposalFailures: 0,\n };\n #nextIdentity = 1;\n #clock = 1;\n #inFlightLoads = 0;\n #closed = false;\n #admissionTail: Promise<void> = Promise.resolve();\n #shutdownPromise?: Promise<void>;\n\n constructor(options: ModelResidencyManagerOptions<Resource>) {\n requirePositiveSafeInteger(\n options.budget.maxCpuBytes,\n \"budget.maxCpuBytes\",\n );\n requirePositiveSafeInteger(\n options.budget.maxGpuBytes,\n \"budget.maxGpuBytes\",\n );\n requirePositiveSafeInteger(\n options.budget.maxInFlightLoads,\n \"budget.maxInFlightLoads\",\n );\n if (options.loadTimeoutMs !== undefined) {\n requirePositiveSafeInteger(options.loadTimeoutMs, \"loadTimeoutMs\");\n }\n this.#budget = Object.freeze({ ...options.budget });\n this.#load = options.load;\n this.#loadTimeoutMs = options.loadTimeoutMs;\n }\n\n /** Acquire one shared resource lease, loading it at most once per cache key. */\n acquire(options: AcquireModelOptions): Promise<ModelResidencyLease<Resource>> {\n try {\n if (this.#closed) throw closedError();\n requirePriority(options.priority);\n requireSafeNonNegativeInteger(\n options.estimatedCpuBytes,\n \"estimatedCpuBytes\",\n );\n requireSafeNonNegativeInteger(\n options.estimatedGpuBytes,\n \"estimatedGpuBytes\",\n );\n const key = createModelResidencyKey(\n options.assetRef,\n options.lod,\n options.partitionId,\n );\n if (signalIsAborted(options.signal)) throw abortedError();\n this.#metrics.acquireCount += 1;\n\n const existing = this.#entries.get(key);\n if (existing !== undefined) return this.#acquireExisting(existing, options);\n return this.#scheduleAdmission(key, options);\n } catch (error) {\n return Promise.reject(error);\n }\n }\n\n #acquireExisting(\n entry: ResidencyEntry<Resource>,\n options: AcquireModelOptions,\n ): Promise<ModelResidencyLease<Resource>> {\n this.#metrics.deduplicatedAcquires += 1;\n if (entry.state === \"resident\") {\n this.#metrics.cacheHits += 1;\n return Promise.resolve(\n this.#createLease(\n entry,\n options.priority,\n options.pinned ?? false,\n ),\n );\n }\n return this.#addWaiter(entry, options);\n }\n\n #scheduleAdmission(\n key: string,\n options: AcquireModelOptions,\n ): Promise<ModelResidencyLease<Resource>> {\n let resolveAcquisition!: (lease: ModelResidencyLease<Resource>) => void;\n let rejectAcquisition!: (reason: unknown) => void;\n const acquisition = new Promise<ModelResidencyLease<Resource>>(\n (resolve, reject) => {\n resolveAcquisition = resolve;\n rejectAcquisition = reject;\n },\n );\n const admission = this.#admissionTail.then(async () => {\n if (this.#closed) throw closedError();\n if (signalIsAborted(options.signal)) throw abortedError();\n const existing = this.#entries.get(key);\n if (existing !== undefined) {\n void this.#acquireExisting(existing, options).then(\n resolveAcquisition,\n rejectAcquisition,\n );\n return;\n }\n await this.#ensureCapacityForReservation(\n options.estimatedCpuBytes,\n options.estimatedGpuBytes,\n );\n if (this.#closed) throw closedError();\n if (signalIsAborted(options.signal)) throw abortedError();\n const entry: ResidencyEntry<Resource> = {\n key,\n assetRef: options.assetRef,\n lod: options.lod,\n ...(options.partitionId === undefined\n ? {}\n : { partitionId: options.partitionId }),\n estimatedCpuBytes: options.estimatedCpuBytes,\n estimatedGpuBytes: options.estimatedGpuBytes,\n queueOrder: this.#nextIdentity++,\n state: \"queued\",\n priority: options.priority,\n retainedPriority: options.priority,\n lastUsed: this.#clock++,\n abandoned: false,\n loadSlotReleased: true,\n waiters: new Map(),\n leases: new Map(),\n };\n this.#entries.set(key, entry);\n void this.#addWaiter(entry, options).then(\n resolveAcquisition,\n rejectAcquisition,\n );\n this.#pumpQueue();\n });\n this.#admissionTail = admission.catch(() => undefined);\n void admission.catch(rejectAcquisition);\n return acquisition;\n }\n\n /** Dispose every currently unreferenced, unpinned resident cache entry. */\n async evictUnused(): Promise<void> {\n const tasks: Promise<void>[] = [];\n for (const entry of this.#evictionCandidates()) {\n tasks.push(this.#evict(entry));\n }\n await Promise.all(tasks);\n }\n\n /** Wait until queued/loading work and its resulting disposal have settled. */\n async waitForIdle(): Promise<void> {\n await this.#admissionTail;\n do {\n this.#pumpQueue();\n const tasks = [...this.#loadTasks];\n if (tasks.length > 0) {\n await Promise.allSettled(tasks);\n }\n } while (this.#loadTasks.size > 0 || this.#queuedCount() > 0);\n await Promise.allSettled([...this.#disposalTasks]);\n }\n\n /** Abort pending work and dispose every resident or late-completing resource. */\n shutdown(): Promise<void> {\n if (this.#shutdownPromise !== undefined) return this.#shutdownPromise;\n this.#closed = true;\n const error = closedError();\n const disposalTasks: Promise<void>[] = [];\n for (const entry of [...this.#entries.values()]) {\n if (entry.state === \"resident\") {\n this.#entries.delete(entry.key);\n disposalTasks.push(this.#disposeEntry(entry));\n continue;\n }\n this.#rejectWaiters(entry, error);\n if (entry.state === \"queued\") {\n this.#entries.delete(entry.key);\n } else {\n this.#abandonLoadingEntry(entry, error, false);\n }\n }\n this.#shutdownPromise = (async () => {\n await Promise.allSettled(disposalTasks);\n await this.waitForIdle();\n await Promise.allSettled([...this.#disposalTasks]);\n })();\n return this.#shutdownPromise;\n }\n\n /** Return immutable byte, queue, cache, and lifetime metrics. */\n snapshot(): ModelResidencySnapshot {\n let cpuBytes = 0;\n let gpuBytes = 0;\n let reservedCpuBytes = 0;\n let reservedGpuBytes = 0;\n let referenceCount = 0;\n let pinnedCount = 0;\n let residentCount = 0;\n let pendingCount = 0;\n const entries: ModelResidencyEntrySnapshot[] = [];\n\n for (const entry of this.#entries.values()) {\n const pinned = [...entry.leases.values()].some(\n (lease) => lease.active && lease.pinned,\n );\n const references = [...entry.leases.values()].filter(\n (lease) => lease.active,\n ).length;\n referenceCount += references;\n if (pinned) pinnedCount += 1;\n if (entry.state === \"resident\") {\n residentCount += 1;\n cpuBytes += entry.loaded!.cpuBytes;\n gpuBytes += entry.loaded!.gpuBytes;\n } else {\n pendingCount += 1;\n reservedCpuBytes += entry.estimatedCpuBytes;\n reservedGpuBytes += entry.estimatedGpuBytes;\n }\n entries.push(\n Object.freeze({\n key: entry.key,\n state: entry.state,\n priority: entry.priority,\n references,\n pinned,\n cpuBytes:\n entry.state === \"resident\"\n ? entry.loaded!.cpuBytes\n : entry.estimatedCpuBytes,\n gpuBytes:\n entry.state === \"resident\"\n ? entry.loaded!.gpuBytes\n : entry.estimatedGpuBytes,\n }),\n );\n }\n entries.sort((left, right) => left.key.localeCompare(right.key));\n return Object.freeze({\n ...this.#budget,\n cpuBytes,\n gpuBytes,\n reservedCpuBytes,\n reservedGpuBytes,\n residentCount,\n pendingCount,\n referenceCount,\n pinnedCount,\n inFlightLoads: this.#inFlightLoads,\n queuedLoads: this.#queuedCount(),\n ...this.#metrics,\n residentKeys: Object.freeze(\n entries\n .filter((entry) => entry.state === \"resident\")\n .map((entry) => entry.key),\n ),\n entries: Object.freeze(entries),\n });\n }\n\n #addWaiter(\n entry: ResidencyEntry<Resource>,\n options: AcquireModelOptions,\n ): Promise<ModelResidencyLease<Resource>> {\n const id = this.#nextIdentity++;\n let resolvePromise!: (lease: ModelResidencyLease<Resource>) => void;\n let rejectPromise!: (reason: unknown) => void;\n const promise = new Promise<ModelResidencyLease<Resource>>(\n (resolve, reject) => {\n resolvePromise = resolve;\n rejectPromise = reject;\n },\n );\n const waiter: AcquisitionWaiter<Resource> = {\n id,\n priority: options.priority,\n pinned: options.pinned ?? false,\n ...(options.signal === undefined ? {} : { signal: options.signal }),\n resolve: resolvePromise,\n reject: rejectPromise,\n };\n if (options.signal !== undefined) {\n waiter.onAbort = () => this.#abortWaiter(entry, waiter);\n options.signal.addEventListener(\"abort\", waiter.onAbort, { once: true });\n }\n entry.waiters.set(id, waiter);\n entry.retainedPriority = Math.max(entry.retainedPriority, options.priority);\n this.#recalculatePriority(entry);\n return promise;\n }\n\n #abortWaiter(\n entry: ResidencyEntry<Resource>,\n waiter: AcquisitionWaiter<Resource>,\n ): void {\n entry.waiters.delete(waiter.id);\n this.#detachWaiterSignal(waiter);\n waiter.reject(abortedError());\n this.#recalculatePriority(entry);\n if (entry.waiters.size > 0) return;\n if (entry.state === \"queued\") {\n this.#entries.delete(entry.key);\n this.#pumpQueue();\n return;\n }\n this.#abandonLoadingEntry(entry, abortedError(), true);\n }\n\n #createLease(\n entry: ResidencyEntry<Resource>,\n priority: number,\n pinned: boolean,\n ): ModelResidencyLease<Resource> {\n const id = this.#nextIdentity++;\n const record: LeaseRecord = {\n id,\n active: true,\n priority,\n pinned,\n };\n entry.leases.set(id, record);\n entry.retainedPriority = priority;\n entry.lastUsed = this.#clock++;\n this.#recalculatePriority(entry);\n const lease = {\n key: entry.key,\n assetRef: entry.assetRef,\n lod: entry.lod,\n ...(entry.partitionId === undefined\n ? {}\n : { partitionId: entry.partitionId }),\n resource: entry.loaded!.resource,\n release: () => {\n if (!record.active) return;\n record.active = false;\n entry.leases.delete(id);\n entry.lastUsed = this.#clock++;\n this.#recalculatePriority(entry);\n },\n setPriority: (nextPriority: number) => {\n if (!record.active || this.#entries.get(entry.key) !== entry) return;\n requirePriority(nextPriority);\n record.priority = nextPriority;\n entry.retainedPriority = nextPriority;\n entry.lastUsed = this.#clock++;\n this.#recalculatePriority(entry);\n },\n setPinned: (nextPinned: boolean) => {\n if (!record.active || this.#entries.get(entry.key) !== entry) return;\n record.pinned = nextPinned;\n },\n } satisfies ModelResidencyLease<Resource>;\n return Object.freeze(lease);\n }\n\n #recalculatePriority(entry: ResidencyEntry<Resource>): void {\n let priority = entry.retainedPriority;\n for (const waiter of entry.waiters.values()) {\n priority = Math.max(priority, waiter.priority);\n }\n for (const lease of entry.leases.values()) {\n priority = Math.max(priority, lease.priority);\n }\n entry.priority = priority;\n }\n\n #pumpQueue(): void {\n if (this.#closed) return;\n while (this.#inFlightLoads < this.#budget.maxInFlightLoads) {\n const candidate = [...this.#entries.values()]\n .filter(\n (entry) =>\n entry.state === \"queued\" &&\n !entry.abandoned &&\n entry.waiters.size > 0,\n )\n .sort(\n (left, right) =>\n right.priority - left.priority ||\n left.queueOrder - right.queueOrder,\n )[0];\n if (candidate === undefined) return;\n this.#startLoad(candidate);\n }\n }\n\n #startLoad(entry: ResidencyEntry<Resource>): void {\n entry.state = \"loading\";\n entry.loadSlotReleased = false;\n entry.controller = new AbortController();\n this.#inFlightLoads += 1;\n this.#metrics.loadsStarted += 1;\n if (this.#loadTimeoutMs !== undefined) {\n entry.timeout = setTimeout(() => {\n this.#metrics.loadsTimedOut += 1;\n this.#abandonLoadingEntry(\n entry,\n new ModelResidencyError(\n \"load-timeout\",\n `Model load exceeded ${this.#loadTimeoutMs} ms`,\n ),\n true,\n );\n }, this.#loadTimeoutMs);\n }\n\n let adapterPromise: Promise<ModelLoadedResource<Resource>>;\n try {\n adapterPromise = this.#load({\n assetRef: entry.assetRef,\n lod: entry.lod,\n ...(entry.partitionId === undefined\n ? {}\n : { partitionId: entry.partitionId }),\n priority: entry.priority,\n signal: entry.controller.signal,\n });\n } catch (error) {\n adapterPromise = Promise.reject(error);\n }\n const task = Promise.resolve(adapterPromise)\n .then((result) => this.#completeLoad(entry, result))\n .catch((error: unknown) => this.#failLoad(entry, error))\n .finally(() => {\n if (entry.timeout !== undefined) clearTimeout(entry.timeout);\n entry.timeout = undefined;\n entry.controller = undefined;\n this.#releaseLoadSlot(entry);\n entry.loadTask = undefined;\n });\n entry.loadTask = task;\n this.#loadTasks.add(task);\n }\n\n async #completeLoad(\n entry: ResidencyEntry<Resource>,\n result: ModelLoadedResource<Resource>,\n ): Promise<void> {\n if (entry.abandoned) {\n await this.#disposeLoaded(result);\n return;\n }\n\n try {\n this.#validateLoadedResource(entry, result);\n const evictions = this.#ensureCapacityForLoadedResult(entry, result);\n await Promise.all(evictions);\n } catch (error) {\n await this.#disposeLoaded(result);\n this.#entries.delete(entry.key);\n this.#rejectWaiters(entry, error);\n this.#metrics.loadsFailed += 1;\n return;\n }\n\n entry.state = \"resident\";\n entry.loaded = result;\n entry.lastUsed = this.#clock++;\n this.#metrics.loadsCompleted += 1;\n const waiters = [...entry.waiters.values()];\n entry.waiters.clear();\n for (const waiter of waiters) {\n this.#detachWaiterSignal(waiter);\n waiter.resolve(this.#createLease(entry, waiter.priority, waiter.pinned));\n }\n }\n\n #failLoad(entry: ResidencyEntry<Resource>, error: unknown): void {\n if (entry.abandoned) return;\n this.#entries.delete(entry.key);\n this.#metrics.loadsFailed += 1;\n this.#rejectWaiters(entry, error);\n }\n\n #abandonLoadingEntry(\n entry: ResidencyEntry<Resource>,\n error: ModelResidencyError,\n rejectWaiters: boolean,\n ): void {\n entry.abandoned = true;\n this.#metrics.loadsAborted += 1;\n if (entry.timeout !== undefined) clearTimeout(entry.timeout);\n entry.timeout = undefined;\n this.#entries.delete(entry.key);\n if (rejectWaiters) this.#rejectWaiters(entry, error);\n entry.controller?.abort(error);\n this.#releaseLoadSlot(entry);\n }\n\n #releaseLoadSlot(entry: ResidencyEntry<Resource>): void {\n if (entry.loadSlotReleased) return;\n entry.loadSlotReleased = true;\n this.#inFlightLoads -= 1;\n this.#loadTasks.delete(entry.loadTask!);\n this.#pumpQueue();\n }\n\n #rejectWaiters(entry: ResidencyEntry<Resource>, error: unknown): void {\n const waiters = [...entry.waiters.values()];\n entry.waiters.clear();\n for (const waiter of waiters) {\n this.#detachWaiterSignal(waiter);\n waiter.reject(error);\n }\n this.#recalculatePriority(entry);\n }\n\n #detachWaiterSignal(waiter: AcquisitionWaiter<Resource>): void {\n if (waiter.signal !== undefined && waiter.onAbort !== undefined) {\n waiter.signal.removeEventListener(\"abort\", waiter.onAbort);\n }\n waiter.onAbort = undefined;\n }\n\n #validateLoadedResource(\n entry: ResidencyEntry<Resource>,\n result: ModelLoadedResource<Resource>,\n ): void {\n if (result === null || typeof result !== \"object\") {\n throw new ModelResidencyError(\n \"invalid-load-result\",\n \"The model loader returned an invalid resource\",\n );\n }\n if (typeof result.dispose !== \"function\") {\n throw new ModelResidencyError(\n \"invalid-load-result\",\n \"The model loader must return a disposer\",\n );\n }\n requireSafeNonNegativeInteger(result.cpuBytes, \"loaded.cpuBytes\");\n requireSafeNonNegativeInteger(result.gpuBytes, \"loaded.gpuBytes\");\n if (result.contentHash !== entry.assetRef.contentHash) {\n throw new ModelResidencyError(\n \"content-hash-mismatch\",\n \"The loaded model content hash does not match its canonical reference\",\n );\n }\n }\n\n async #ensureCapacityForReservation(\n cpuBytes: number,\n gpuBytes: number,\n ): Promise<void> {\n if (\n cpuBytes > this.#budget.maxCpuBytes ||\n gpuBytes > this.#budget.maxGpuBytes\n ) {\n throw this.#budgetError();\n }\n let usage = this.#totalAccountedBytes();\n for (const candidate of this.#evictionCandidates()) {\n if (\n usage.cpu + cpuBytes <= this.#budget.maxCpuBytes &&\n usage.gpu + gpuBytes <= this.#budget.maxGpuBytes\n ) {\n return;\n }\n await this.#evict(candidate);\n usage = this.#totalAccountedBytes();\n }\n if (\n usage.cpu + cpuBytes > this.#budget.maxCpuBytes ||\n usage.gpu + gpuBytes > this.#budget.maxGpuBytes\n ) {\n throw this.#budgetError();\n }\n }\n\n #ensureCapacityForLoadedResult(\n loadingEntry: ResidencyEntry<Resource>,\n result: ModelLoadedResource<Resource>,\n ): Promise<void>[] {\n const usage = this.#totalAccountedBytes();\n usage.cpu += result.cpuBytes - loadingEntry.estimatedCpuBytes;\n usage.gpu += result.gpuBytes - loadingEntry.estimatedGpuBytes;\n const disposals: Promise<void>[] = [];\n for (const candidate of this.#evictionCandidates()) {\n if (\n usage.cpu <= this.#budget.maxCpuBytes &&\n usage.gpu <= this.#budget.maxGpuBytes\n ) {\n return disposals;\n }\n usage.cpu -= candidate.loaded!.cpuBytes;\n usage.gpu -= candidate.loaded!.gpuBytes;\n disposals.push(this.#evict(candidate));\n }\n if (\n usage.cpu > this.#budget.maxCpuBytes ||\n usage.gpu > this.#budget.maxGpuBytes\n ) {\n throw this.#budgetError();\n }\n return disposals;\n }\n\n #budgetError(): ModelResidencyError {\n return new ModelResidencyError(\n \"budget-exceeded\",\n \"Pinned, referenced, or pending models leave insufficient residency budget\",\n );\n }\n\n #totalAccountedBytes(): { cpu: number; gpu: number } {\n let cpu = 0;\n let gpu = 0;\n for (const entry of this.#entries.values()) {\n if (entry.state === \"resident\") {\n cpu += entry.loaded!.cpuBytes;\n gpu += entry.loaded!.gpuBytes;\n } else {\n cpu += entry.estimatedCpuBytes;\n gpu += entry.estimatedGpuBytes;\n }\n }\n return { cpu, gpu };\n }\n\n #evictionCandidates(): ResidencyEntry<Resource>[] {\n return [...this.#entries.values()]\n .filter(\n (entry) =>\n entry.state === \"resident\" &&\n entry.leases.size === 0,\n )\n .sort(\n (left, right) =>\n left.priority - right.priority ||\n left.lastUsed - right.lastUsed,\n );\n }\n\n #evict(entry: ResidencyEntry<Resource>): Promise<void> {\n this.#entries.delete(entry.key);\n this.#metrics.evictions += 1;\n return this.#disposeEntry(entry);\n }\n\n #disposeEntry(entry: ResidencyEntry<Resource>): Promise<void> {\n const result = entry.loaded!;\n entry.loaded = undefined;\n return this.#disposeLoaded(result);\n }\n\n #disposeLoaded(result: ModelLoadedResource<Resource>): Promise<void> {\n this.#metrics.disposals += 1;\n let disposal: Promise<void>;\n try {\n disposal = Promise.resolve(result.dispose());\n } catch (error) {\n disposal = Promise.reject(error);\n }\n const tracked = disposal\n .catch(() => {\n this.#metrics.disposalFailures += 1;\n })\n .finally(() => {\n this.#disposalTasks.delete(tracked);\n });\n this.#disposalTasks.add(tracked);\n return tracked;\n }\n\n #queuedCount(): number {\n let count = 0;\n for (const entry of this.#entries.values()) {\n if (entry.state === \"queued\") count += 1;\n }\n return count;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,mBAAN,MAA6C;AAAA,EACjC,UAAU,oBAAI,IAAqB;AAAA,EAEpD,MAAM,IAAO,KAAqC;AAChD,WAAO,KAAK,QAAQ,IAAI,GAAG;AAAA,EAC7B;AAAA,EAEA,MAAM,IAAO,KAAa,OAAyB;AACjD,SAAK,QAAQ,IAAI,KAAK,KAAK;AAAA,EAC7B;AAAA,EAEA,MAAM,OAAO,KAA+B;AAC1C,WAAO,KAAK,QAAQ,OAAO,GAAG;AAAA,EAChC;AAAA,EAEA,MAAM,QAAuB;AAC3B,SAAK,QAAQ,MAAM;AAAA,EACrB;AACF;AAEA,SAAS,YAAY,OAAyB;AAC5C,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,WAAO,MAAM,IAAI,WAAW;AAAA,EAC9B;AAEA,MAAI,UAAU,QAAQ,OAAO,UAAU,UAAU;AAC/C,WAAO,OAAO;AAAA,MACZ,OAAO,QAAQ,KAAK,EACjB,KAAK,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,MAAM,KAAK,cAAc,KAAK,CAAC,EACnD,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC;AAAA,IACpD;AAAA,EACF;AAEA,SAAO;AACT;AAEO,SAAS,oBACd,aACA,UACA,gBACQ;AACR,SAAO,GAAG,QAAQ,IAAI,WAAW,IAAI,KAAK,UAAU,YAAY,kBAAkB,IAAI,CAAC,CAAC;AAC1F;;;ACpCO,IAAM,0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