diff --git a/src/python/PythonPyc.errors.ts b/src/python/PythonPyc.errors.ts index ace8b42979..9ae854f4f3 100644 --- a/src/python/PythonPyc.errors.ts +++ b/src/python/PythonPyc.errors.ts @@ -17,11 +17,18 @@ const LIMIT_CONTROLS = { marshal_key_bytes: ["max_file_bytes"], } as const satisfies Record; +/** A parser allowance exhausted; `required` is the size the parser measured. */ export const pythonPycLimit = ( name: keyof typeof LIMIT_CONTROLS, maximum: number, detail: string, -) => decodeLimit(PYTHON_PYC_FORMAT, detail, { name, maximum }); + required?: number, +) => + decodeLimit(PYTHON_PYC_FORMAT, detail, { + name, + maximum, + ...(required === undefined ? {} : { required }), + }); export const pythonPycLimitControls = ( boundary: string, diff --git a/src/python/PythonPycParser.test.ts b/src/python/PythonPycParser.test.ts index 49b056e92b..83e39ec343 100644 --- a/src/python/PythonPycParser.test.ts +++ b/src/python/PythonPycParser.test.ts @@ -1476,7 +1476,10 @@ it("preflights expanded shared code-name keys before building their hex table", expect.objectContaining({ failure: expect.objectContaining({ kind: "limit", - limit: { name: "max_string_bytes", maximum: 4096 }, + limit: expect.objectContaining({ + name: "max_string_bytes", + maximum: 4096, + }), }), message: expect.stringContaining("max_string_bytes 4096"), }), diff --git a/src/python/PythonPycParser.ts b/src/python/PythonPycParser.ts index 6cb2cea0f8..2df2883943 100644 --- a/src/python/PythonPycParser.ts +++ b/src/python/PythonPycParser.ts @@ -359,6 +359,7 @@ class PycMarshalReader extends MarshalReader { "max_string_bytes", max_string_bytes, `Marshal string of ${exceeded.amount} bytes exceeds max_string_bytes ${max_string_bytes}.`, + exceeded.amount, ); default: // Long digits; this reader sets no sequence or digit-count bound. @@ -366,6 +367,7 @@ class PycMarshalReader extends MarshalReader { "max_string_bytes", max_string_bytes, `Marshal long of ${exceeded.amount} digit bytes exceeds max_string_bytes ${max_string_bytes}.`, + exceeded.amount, ); } } @@ -1129,8 +1131,10 @@ class PycMarshalReader extends MarshalReader { if (mode === "hash") return value.value ? "i1" : "i0"; return value.value ? "B1" : "B0"; case "int": + // A bit count bounds the decimal key only approximately, so it is + // not reported as a byte requirement. if (value.bits > this.input.max_string_bytes * 3) - this.#keyTooLarge(value.bits); + this.#keyTooLarge(value.bits, false); return this.#boundedJoin("i", [value.value.toString()], ""); case "float": if (Number.isNaN(value.value)) return this.#identity(value); @@ -1424,11 +1428,14 @@ class PycMarshalReader extends MarshalReader { this.work.keyBytes += size; } - #keyTooLarge(size: number): never { + #keyTooLarge(size: number, measuredBytes = true): never { throw pythonPycLimit( "max_string_bytes", this.input.max_string_bytes, - `Marshal key of ${size} bytes exceeds max_string_bytes ${this.input.max_string_bytes}.`, + measuredBytes + ? `Marshal key of ${size} bytes exceeds max_string_bytes ${this.input.max_string_bytes}.` + : `Marshal integer key of ${size} bits exceeds max_string_bytes ${this.input.max_string_bytes}.`, + measuredBytes ? size : undefined, ); } diff --git a/src/python/PythonPycProvider.ts b/src/python/PythonPycProvider.ts index ff2b4e47fb..8f8881240e 100644 --- a/src/python/PythonPycProvider.ts +++ b/src/python/PythonPycProvider.ts @@ -33,13 +33,25 @@ const OPERATION = "inspect_python_pyc"; /** * Recovery for the caller-selected budgets that determine one parser limit. - * A limit owned by one control reports it as `limit`; a product of several - * controls names each as a related parameter, since no single range applies. + * A limit owned by one control is typed, with the requirement the parser + * measured, so the shared refusal owner states whether raising it can help. + * A product of several controls names each as a related parameter, since no + * single range applies. */ const parserLimitRecovery = ( input: InspectPythonPycInput, fields: readonly (keyof PythonPycLimits)[], + required?: number | bigint, ) => { + const [only] = fields; + if (fields.length === 1 && only !== undefined) + return { + retryable: false as const, + limit: { + ...pythonPycBudgetLimit(only, input[only]), + ...(typeof required === "number" ? { required_value: required } : {}), + }, + }; const actions = fields.map((field) => { const selected = input[field]; const { maximum } = PYTHON_PYC_READ_BUDGETS[field]; @@ -47,14 +59,9 @@ const parserLimitRecovery = ( ? `Raise ${field} above ${String(selected)}, up to ${String(maximum)}, on the next call.` : `${field} is at this operation's maximum of ${String(maximum)}; select a complete artifact within that bound.`; }); - const [only] = fields; return { retryable: false as const, - ...(fields.length === 1 && only !== undefined - ? { limit: pythonPycBudgetLimit(only, input[only]) } - : fields.length > 1 - ? { related: { parameters: [...fields] } } - : {}), + ...(fields.length > 1 ? { related: { parameters: [...fields] } } : {}), remediationAction: `${actions.join(" ")} Repeating the unchanged request fails the same way; waiting or adding host memory does not raise these parser limits.`, }; }; @@ -81,7 +88,11 @@ const failureFor = ( OPERATION, name === "max_depth" ? "capacity" : "memory", failure, - parserLimitRecovery(input, pythonPycLimitControls(name)), + parserLimitRecovery( + input, + pythonPycLimitControls(name), + failure.failure.limit.required, + ), ); }, fileSize: (failure) => diff --git a/src/vsix/VsixPackageProvider.ts b/src/vsix/VsixPackageProvider.ts index 35bcfb355d..fa9e186bf9 100644 --- a/src/vsix/VsixPackageProvider.ts +++ b/src/vsix/VsixPackageProvider.ts @@ -41,13 +41,24 @@ import { parseVsixPackage } from "./VsixPackageParser.js"; const OPERATION = "inspect_vsix_package"; -/** The refusal options for an exhausted per-call budget. */ -const budgetRecovery = (parameter: VsixPackageBudget, selected: number) => ({ - limit: vsixPackageBudgetLimit(parameter, selected), - remediationAction: - selected >= VSIX_PACKAGE_BUDGETS[parameter].maximum - ? `${parameter} is at this operation's maximum of ${String(VSIX_PACKAGE_BUDGETS[parameter].maximum)}; select a complete artifact within that bound.` - : `Raise ${parameter} above ${String(selected)}, up to ${String(VSIX_PACKAGE_BUDGETS[parameter].maximum)}, or select a smaller artifact. The budget applies on the next call; repeating the unchanged request does not raise it.`, +/** + * The typed limit of an exhausted per-call budget, so the shared refusal + * owner states the recovery. An early-stop read measures only a lower bound + * of the requirement, so it is reported as `required_value` only when even + * that bound exceeds the control's maximum: raising the budget cannot help. + */ +const budgetRecovery = ( + parameter: VsixPackageBudget, + selected: number, + required?: number | bigint, +) => ({ + limit: { + ...vsixPackageBudgetLimit(parameter, selected), + ...(typeof required === "number" && + required > VSIX_PACKAGE_BUDGETS[parameter].maximum + ? { required_value: required } + : {}), + }, }); const vsixFileSizeFailure = ( @@ -104,13 +115,15 @@ const vsixParseLimit = ( { path: input.path, retryable: false, - // Early-stop parse limits (such as ZIP entry enumeration and streaming - // manifest reads) only know a lower bound, which parseLimitConstraint - // preserves in details.reported_limits; omit required_value unless the - // complete total admitting the whole request is known. + // parseLimitConstraint preserves the measured bound in + // details.reported_limits; budgetRecovery decides what it establishes. ...(parameter === undefined ? {} - : budgetRecovery(parameter, cause.failure.limit.maximum)), + : budgetRecovery( + parameter, + cause.failure.limit.maximum, + cause.failure.limit.required, + )), }, ); }; diff --git a/tests/acceptance/pythonPycCli.test.ts b/tests/acceptance/pythonPycCli.test.ts index 07162d7a20..32b32768b4 100644 --- a/tests/acceptance/pythonPycCli.test.ts +++ b/tests/acceptance/pythonPycCli.test.ts @@ -708,7 +708,7 @@ cliTest( limit: { parameter: field, effective_value: maximum, cli_flag: flag }, }, remediation: { - action: expect.stringContaining(`Raise ${field} above ${maximum}`), + action: expect.stringContaining(`Pass a larger ${flag}`), }, }); } @@ -736,9 +736,9 @@ cliTest( }, }, }); - // The service names contract fields; only the CLI spells flags. + // The CLI spells the control as its flag. expect( - JSON.stringify((refused.json as { remediation: unknown }).remediation), - ).not.toContain("--"); + (refused.json as { remediation: { action: string } }).remediation.action, + ).toContain("Pass a larger --max-objects"); }, ); diff --git a/tests/boundary/filesystem/pythonPyc.test.ts b/tests/boundary/filesystem/pythonPyc.test.ts index 1c2621244b..2050759faf 100644 --- a/tests/boundary/filesystem/pythonPyc.test.ts +++ b/tests/boundary/filesystem/pythonPyc.test.ts @@ -71,7 +71,8 @@ describe("Python pyc stable filesystem boundary", () => { }, }, remediation: { - action: expect.stringContaining(`Raise ${field} above`), + // The shared refusal owner states the recovery. + action: expect.stringContaining(`Pass a larger ${field}`), }, }); // Contract fields only; the CLI adds its own flag spelling.