fix(model): project the enforced parser limit and cause (#12399)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 15:24:26 +00:00
1 parent 2773327843
commit bd4339ade9
5 files changed
+201 -131

No files matched your search

+63 -76
View File
@@ -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<Record<string, number>> => {
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. */