Files
rea/src/model/ModelFormat.ts
T
2947a77507 fix(engines): keep format ceilings fixed and recover the EVMole deadline (#6088)
* fix(engines): keep format ceilings fixed and recover the EVMole deadline

Promote the owned EVMole worker wall clock to timeout_ms and name it in
recovery. Fixed CPU, byte, and address-space refusals in this theme say
the adjustable deadline does not raise them.

Refs #5910

Co-authored-by: morluto <[email protected]>

* fix(engines): recover the allowance that refused the request

Model capacity recovery follows a structured limit instead of message text,
and the shared header ceiling no longer names Safetensors. Firmware signal
errors keep captured tool output, oversized reports do not suggest an
intermittent rerun, and NativeAOT hydration budget exhaustion explains the
fixed parser allowance on the partial report.

Co-authored-by: morluto <[email protected]>

---------

Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: morluto <[email protected]>
2026-10-22 03:06:59 +08:00

104 lines
4.0 KiB
TypeScript

/** 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",
] as const;
/** Which fixed model allowance refused the request. */
export type ModelCapacityLimit = (typeof MODEL_CAPACITY_LIMITS)[number];
/** Format failures stay separate from filesystem, capacity and cancellation failures. */
export class ModelFormatFailure extends Error {
/** Set only for `kind: "capacity"`. */
readonly limit: ModelCapacityLimit | undefined;
constructor(kind: "format" | "unsupported", message: string);
constructor(kind: "capacity", message: string, limit: ModelCapacityLimit);
constructor(
readonly kind: "format" | "unsupported" | "capacity",
message: string,
limit?: ModelCapacityLimit,
) {
super(message);
this.name = "ModelFormatFailure";
if (kind === "capacity" && limit === undefined)
throw new TypeError("Model capacity failures require a limit kind");
if (kind !== "capacity" && limit !== undefined)
throw new TypeError("Only model capacity failures carry a limit kind");
this.limit = limit;
}
}
export function rejectModel(message: string): never {
throw new ModelFormatFailure("format", message);
}
// Shared header ceiling for GGUF and Safetensors. Safetensors counts declared
// JSON bytes after the eight-byte length prefix; GGUF counts the complete
// header. Retained rows and the serialized report have separate allowances.
export const MODEL_HEADER_BYTES = 100_000_000;
export const MODEL_REPORT_BYTES = 32 * 1024 * 1024;
export const MODEL_ARRAY_PREVIEW = 8;
export const MODEL_METADATA_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`,
);
}
};
/** 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 };
}
};
/** Charge representation growth before retaining rows or decoding long strings. */
export class ModelReportBudget {
#remaining = MODEL_REPORT_BYTES;
charge(bytes: number): void {
if (!Number.isSafeInteger(bytes) || bytes < 0 || bytes > this.#remaining)
throw new ModelFormatFailure(
"capacity",
"Model facts exceed the 32 MiB retained representation budget. This release has no runtime override.",
"representation-bytes",
);
this.#remaining -= bytes;
}
}