From bd4339ade98a00f124f79ffa20c8fb2cab290eb7 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:24:26 -0500 Subject: [PATCH] fix(model): project the enforced parser limit and cause (#12399) --- .../resource-constraint-constructor.json | 2 +- src/model/ModelArtifactProvider.ts | 24 +-- src/model/ModelFormat.ts | 139 ++++++++---------- tests/boundary/model/modelArtifact.test.ts | 78 ++++++---- .../stableArtifactConsumers.test.ts | 89 ++++++++++- 5 files changed, 201 insertions(+), 131 deletions(-) diff --git a/scripts/verify/baselines/resource-constraint-constructor.json b/scripts/verify/baselines/resource-constraint-constructor.json index fc1414595f..5b1c865ee9 100644 --- a/scripts/verify/baselines/resource-constraint-constructor.json +++ b/scripts/verify/baselines/resource-constraint-constructor.json @@ -102,7 +102,7 @@ "src/kuna/KunaReferenceQueries.ts": 1, "src/kuna/KunaSessionClient.ts": 1, "src/main/StdioStartupInput.ts": 1, - "src/model/ModelArtifactProvider.ts": 3, + "src/model/ModelArtifactProvider.ts": 2, "src/native/AppleDispatchMetadata.ts": 1, "src/native/LldbCallTracer.ts": 1, "src/native/NativeCommandAnalysisError.ts": 1, diff --git a/src/model/ModelArtifactProvider.ts b/src/model/ModelArtifactProvider.ts index caaafb6976..c4b063e338 100644 --- a/src/model/ModelArtifactProvider.ts +++ b/src/model/ModelArtifactProvider.ts @@ -41,13 +41,10 @@ import { GgufHeader } from "./GgufHeader.js"; import { readSafetensorsHeader } from "./SafetensorsHeader.js"; import { modelCapacityFailure, - modelCapacityLimitOf, + modelCapacityConstraint, MODEL_REPORT_BYTES, - modelCapacityRemediation, - modelCapacityReportedLimits, } from "./ModelFormat.js"; import { jsonValueByteLength } from "../domain/jsonValueByteLength.js"; -import { ParseFailureError } from "../domain/parseFailure.js"; import { parseFailureAnalysisError } from "../domain/parseFailureAnalysisError.js"; import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { @@ -110,10 +107,7 @@ export class ModelArtifactProvider implements ModelArtifactPort { ); } catch (cause) { const { cause: primary, origin } = artifactFilePrimaryFacts(cause); - const capacity = - primary instanceof ParseFailureError - ? modelCapacityLimitOf(primary) - : undefined; + const capacityError = modelCapacityConstraint(OPERATION, primary); const formatError = parseFailureAnalysisError( OPERATION, input.path, @@ -122,19 +116,7 @@ export class ModelArtifactProvider implements ModelArtifactPort { let error: AnalysisError; if (origin === "owned-or-classified" && primary instanceof AnalysisError) error = primary; - else if (primary instanceof ParseFailureError && capacity !== undefined) - // A model allowance names its own recovery. - error = new AnalysisResourceConstraintError( - OPERATION, - "memory", - primary.message, - modelCapacityReportedLimits(capacity), - { - cause, - retryable: false, - remediationAction: modelCapacityRemediation(capacity), - }, - ); + else if (capacityError !== undefined) error = capacityError; else if (formatError !== undefined) error = formatError; else if ( origin === "owned-or-classified" && diff --git a/src/model/ModelFormat.ts b/src/model/ModelFormat.ts index ea4225916b..7eae5ff483 100644 --- a/src/model/ModelFormat.ts +++ b/src/model/ModelFormat.ts @@ -1,22 +1,14 @@ import { Budget } from "../domain/budget.js"; import { + decodeLimit, malformedInput, - ParseFailureError, + type ParseFailureError, unsupportedInput, } from "../domain/parseFailure.js"; - -/** Producing constraint for a model capacity refusal. Recovery uses this, not message text. */ -export const MODEL_CAPACITY_LIMITS = [ - "header-bytes", - "report-bytes", - "representation-bytes", - "exact-offset", - "metadata-nesting", - "json-nesting", -] as const; - -/** Which fixed model allowance refused the request. */ -export type ModelCapacityLimit = (typeof MODEL_CAPACITY_LIMITS)[number]; +import { + isParseLimit, + parseLimitConstraint, +} from "../domain/parseFailureAnalysisError.js"; /** The ParseFailure format of GGUF and Safetensors model headers. */ export const MODEL_FORMAT = "model"; @@ -26,25 +18,11 @@ export const modelCapacityFailure = ( limit: ModelCapacityLimit, detail: string, ): ParseFailureError<"limit"> => - new ParseFailureError({ - kind: "limit", - format: MODEL_FORMAT, - detail, - limit: { - name: limit, - maximum: Object.values(modelCapacityReportedLimits(limit))[0] ?? 0, - }, + decodeLimit(MODEL_FORMAT, detail, { + name: limit, + maximum: modelCapacityLimits[limit].maximum, }); -/** The model allowance a failure names, or undefined for any other failure. */ -export const modelCapacityLimitOf = ( - cause: ParseFailureError, -): ModelCapacityLimit | undefined => { - const { failure } = cause; - if (failure.kind !== "limit") return undefined; - return MODEL_CAPACITY_LIMITS.find((limit) => limit === failure.limit.name); -}; - export function rejectModel(message: string): never { throw malformedInput(MODEL_FORMAT, message); } @@ -68,52 +46,61 @@ export const MODEL_JSON_NESTING = 32; const fixedModelLimit = (allowance: string): string => `${allowance}. This release has no runtime override. Additional host memory does not raise it. Repeating the unchanged request fails the same way.`; -/** Recovery for the allowance that actually refused the model. Host memory does not raise it. */ -export const modelCapacityRemediation = (limit: ModelCapacityLimit): string => { - switch (limit) { - case "header-bytes": - return fixedModelLimit("The model header allowance is 100,000,000 bytes"); - case "report-bytes": - return fixedModelLimit( - `The retained model report allowance is ${String(MODEL_REPORT_BYTES)} bytes`, - ); - case "representation-bytes": - return fixedModelLimit( - `The retained model representation allowance is ${String(MODEL_REPORT_BYTES)} bytes`, - ); - case "exact-offset": - return fixedModelLimit( - `Exact host offset precision stops at ${String(Number.MAX_SAFE_INTEGER)}`, - ); - case "metadata-nesting": - return fixedModelLimit( - `GGUF metadata nesting is limited to ${String(MODEL_METADATA_NESTING)} array frames`, - ); - case "json-nesting": - return fixedModelLimit( - `Safetensors JSON nesting is limited to ${String(MODEL_JSON_NESTING)} levels`, - ); - } -}; +const modelCapacityLimits = { + "header-bytes": { + maximum: MODEL_HEADER_BYTES, + legacyMaximumKey: "maximum_header_bytes", + allowance: (maximum: number) => + `The model header allowance is ${maximum.toLocaleString("en-US")} bytes`, + }, + "report-bytes": { + maximum: MODEL_REPORT_BYTES, + legacyMaximumKey: "maximum_report_bytes", + allowance: (maximum: number) => + `The retained model report allowance is ${String(maximum)} bytes`, + }, + "representation-bytes": { + maximum: MODEL_REPORT_BYTES, + legacyMaximumKey: "maximum_representation_bytes", + allowance: (maximum: number) => + `The retained model representation allowance is ${String(maximum)} bytes`, + }, + "exact-offset": { + maximum: Number.MAX_SAFE_INTEGER, + legacyMaximumKey: "maximum_exact_offset", + allowance: (maximum: number) => + `Exact host offset precision stops at ${String(maximum)}`, + }, + "metadata-nesting": { + maximum: MODEL_METADATA_NESTING, + legacyMaximumKey: "maximum_array_frames", + allowance: (maximum: number) => + `GGUF metadata nesting is limited to ${String(maximum)} array frames`, + }, + "json-nesting": { + maximum: MODEL_JSON_NESTING, + legacyMaximumKey: "maximum_json_depth", + allowance: (maximum: number) => + `Safetensors JSON nesting is limited to ${String(maximum)} levels`, + }, +} as const; -/** Limits that belong to the producing constraint, without unrelated ceilings. */ -export const modelCapacityReportedLimits = ( - limit: ModelCapacityLimit, -): Readonly> => { - switch (limit) { - case "header-bytes": - return { maximum_header_bytes: MODEL_HEADER_BYTES }; - case "report-bytes": - return { maximum_report_bytes: MODEL_REPORT_BYTES }; - case "representation-bytes": - return { maximum_representation_bytes: MODEL_REPORT_BYTES }; - case "exact-offset": - return { maximum_exact_offset: Number.MAX_SAFE_INTEGER }; - case "metadata-nesting": - return { maximum_array_frames: MODEL_METADATA_NESTING }; - case "json-nesting": - return { maximum_json_depth: MODEL_JSON_NESTING }; - } +type ModelCapacityLimit = keyof typeof modelCapacityLimits; + +const isModelCapacityLimit = (name: string): name is ModelCapacityLimit => + Object.hasOwn(modelCapacityLimits, name); + +/** The model parser supplies the enforced maximum and the original cause. */ +export const modelCapacityConstraint = (operation: string, cause: unknown) => { + if (!isParseLimit(cause, MODEL_FORMAT)) return undefined; + const { name, maximum } = cause.failure.limit; + if (!isModelCapacityLimit(name)) return undefined; + const { allowance, legacyMaximumKey } = modelCapacityLimits[name]; + return parseLimitConstraint(operation, "memory", cause, { + legacyMaximumKey, + retryable: false, + remediationAction: fixedModelLimit(allowance(maximum)), + }); }; /** Charge representation growth before retaining rows or decoding long strings. */ diff --git a/tests/boundary/model/modelArtifact.test.ts b/tests/boundary/model/modelArtifact.test.ts index 77b260571e..69a78e46db 100644 --- a/tests/boundary/model/modelArtifact.test.ts +++ b/tests/boundary/model/modelArtifact.test.ts @@ -9,8 +9,8 @@ import { MODEL_JSON_NESTING, MODEL_METADATA_NESTING, MODEL_REPORT_BYTES, - modelCapacityRemediation, - modelCapacityReportedLimits, + modelCapacityConstraint, + modelCapacityFailure, } from "../../../src/model/ModelFormat.js"; import { readSafetensorsHeader } from "../../../src/model/SafetensorsHeader.js"; import { ParseFailureError } from "../../../src/domain/parseFailure.js"; @@ -946,36 +946,50 @@ it.each([ }, ); -it("chooses model recovery from the producing limit", () => { - const header = modelCapacityRemediation("header-bytes"); - expect(header).toContain("100,000,000 bytes"); - expect(header).not.toContain("Safetensors"); - expect(header).not.toContain(String(MODEL_REPORT_BYTES)); - const report = modelCapacityRemediation("report-bytes"); - expect(report).toContain(`report allowance is ${String(MODEL_REPORT_BYTES)}`); - expect(report).not.toContain("representation"); - const representation = modelCapacityRemediation("representation-bytes"); - expect(representation).toContain("representation allowance"); - expect(representation).not.toContain("report allowance"); - const offset = modelCapacityRemediation("exact-offset"); - expect(offset).toContain(String(Number.MAX_SAFE_INTEGER)); - expect(offset).not.toContain(String(MODEL_REPORT_BYTES)); - const nesting = modelCapacityRemediation("metadata-nesting"); - expect(nesting).toContain(`${String(MODEL_METADATA_NESTING)} array frames`); - expect(nesting).not.toContain(String(MODEL_REPORT_BYTES)); - const jsonNesting = modelCapacityRemediation("json-nesting"); - expect(jsonNesting).toContain(`${String(MODEL_JSON_NESTING)} levels`); - expect(jsonNesting).not.toContain(String(MODEL_REPORT_BYTES)); - expect(modelCapacityReportedLimits("report-bytes")).toEqual({ - maximum_report_bytes: MODEL_REPORT_BYTES, - }); - expect(modelCapacityReportedLimits("exact-offset")).toEqual({ - maximum_exact_offset: Number.MAX_SAFE_INTEGER, - }); - expect(modelCapacityReportedLimits("json-nesting")).toEqual({ - maximum_json_depth: MODEL_JSON_NESTING, - }); -}); +it.each([ + [ + "header-bytes", + { maximum_header_bytes: 100_000_000 }, + "header allowance is 100,000,000 bytes", + ], + [ + "report-bytes", + { maximum_report_bytes: MODEL_REPORT_BYTES }, + `report allowance is ${String(MODEL_REPORT_BYTES)} bytes`, + ], + [ + "representation-bytes", + { maximum_representation_bytes: MODEL_REPORT_BYTES }, + `representation allowance is ${String(MODEL_REPORT_BYTES)} bytes`, + ], + [ + "exact-offset", + { maximum_exact_offset: Number.MAX_SAFE_INTEGER }, + `offset precision stops at ${String(Number.MAX_SAFE_INTEGER)}`, + ], + [ + "metadata-nesting", + { maximum_array_frames: MODEL_METADATA_NESTING }, + `GGUF metadata nesting is limited to ${String(MODEL_METADATA_NESTING)} array frames`, + ], + [ + "json-nesting", + { maximum_json_depth: MODEL_JSON_NESTING }, + `Safetensors JSON nesting is limited to ${String(MODEL_JSON_NESTING)} levels`, + ], +] as const)( + "projects legacy fields and recovery for %s", + (limit, reportedLimits, remediation) => { + const cause = modelCapacityFailure(limit, "Model allowance exhausted"); + const error = modelCapacityConstraint("inspect_model_artifact", cause); + expect(error).toMatchObject({ + reason: "Model allowance exhausted", + remediationAction: expect.stringContaining(remediation), + cause, + }); + expect(error?.reportedLimits).toEqual(reportedLimits); + }, +); it("refuses an oversized declared header before reading or expanding it", async () => { const bytes = Buffer.alloc(9); diff --git a/tests/process-global/stableArtifactConsumers.test.ts b/tests/process-global/stableArtifactConsumers.test.ts index 3c99afd7f2..aebe47dc94 100644 --- a/tests/process-global/stableArtifactConsumers.test.ts +++ b/tests/process-global/stableArtifactConsumers.test.ts @@ -80,6 +80,7 @@ import { } from "../../src/domain/analysisErrorBase.js"; import { createEvidence, parseEvidence } from "../../src/domain/evidence.js"; import { projectAnalysisError } from "../../src/domain/analysisErrorProjection.js"; +import { ParseFailureError } from "../../src/domain/parseFailure.js"; import { AnalysisCancelledError, AnalysisInputError, @@ -151,7 +152,7 @@ import { inspectWindowsShortcutInputSchema } from "../../src/domain/native/windo import { inspectNativeExportsInputSchema } from "../../src/domain/native/nativeExports.js"; import { inspectErlangBeamInputSchema } from "../../src/domain/erlang/beam.js"; import { ModelArtifactProvider } from "../../src/model/ModelArtifactProvider.js"; -import { ggufFixture } from "../fixtures/model/model.js"; +import { ggufFixture, safetensorsFixture } from "../fixtures/model/model.js"; import { inspectPeResourcesInputSchema } from "../../src/domain/native/peResources.js"; import { peResourceFixture } from "../../src/domain/pe/peResources.fixture.js"; import { createServer } from "../../src/server/createServer.js"; @@ -697,6 +698,92 @@ it("preserves a model permission failure when cancellation arrives during close" } }); +it("keeps the model parser cause through failed close and retry", async () => { + const path = await selectedImage(); + const fixture = (depth: number) => { + const nested = "[".repeat(depth) + "0" + "]".repeat(depth); + return safetensorsFixture( + `{"weight":{"dtype":"F32","shape":[1],"data_offsets":[0,4],"note":${nested}}}`, + Buffer.alloc(4), + ).bytes; + }; + const provider = new ModelArtifactProvider(); + try { + fault.blockNew = false; + await writeFile(path, fixture(1)); + const valid = await provider.inspect({ path, hash: "header" }); + expect(valid).toMatchObject({ + ok: true, + value: { tensors: [{ name: "weight" }] }, + }); + + fault.blockNew = true; + await writeFile(path, fixture(33)); + const result = await provider.inspect({ path, hash: "header" }); + if (result.ok) throw new Error("Expected a model depth refusal"); + const { error } = result; + const maximum = { maximum_json_depth: 32 }; + expect(projectAnalysisError(error)).toMatchObject({ + code: "cleanup_incomplete", + details: { reported_limits: maximum }, + }); + expect(error.cleanupPrimary?.cause).toBeInstanceOf(ParseFailureError); + expect(error.cleanupPrimary?.cause).toMatchObject({ + failure: { + format: "model", + kind: "limit", + limit: { name: "json-nesting", maximum: 32 }, + }, + }); + const retained = fault.handles.at(-1); + expect(retained?.fd).toBeGreaterThanOrEqual(0); + fault.blocked.clear(); + await provider.close(); + expect(retained?.fd).toBe(-1); + expect(projectAnalysisError(error)).toMatchObject({ + code: "resource_constraint", + details: { reported_limits: maximum }, + }); + expect(resolvedAnalysisError(error).cause).toBe( + error.cleanupPrimary?.cause, + ); + } finally { + fault.blocked.clear(); + await provider.close(); + } +}); + +it.each(["model", "foreign-fixture"])( + "projects only a model-owned parser limit with its enforced maximum (%s)", + async (format) => { + const path = await selectedImage(); + const injected = new ParseFailureError({ + format, + kind: "limit", + detail: "fixture-owned parser limit", + limit: { name: "json-nesting", maximum: 17 }, + }); + fault.blockNew = false; + fault.laterReadFailure = { position: 0, cause: injected }; + const provider = new ModelArtifactProvider(); + try { + const result = await provider.inspect({ path, hash: "header" }); + if (result.ok) throw new Error("Expected a read refusal"); + expect(result.error.cause).toBe(injected); + const projected = projectAnalysisError(result.error); + if (format === "model") + expect(projected).toMatchObject({ + code: "resource_constraint", + details: { reported_limits: { maximum_json_depth: 17 } }, + remediation: { action: expect.stringContaining("17 levels") }, + }); + else expect(projected.code).toBe("execution_failure"); + } finally { + await provider.close(); + } + }, +); + it("retains earlier GGUF rows and physical failure while retrying failed descriptor cleanup", async () => { const path = await selectedImage(); const seed = ggufFixture();