diff --git a/docs/reference/limits-and-recovery.md b/docs/reference/limits-and-recovery.md index a79e3eece8c..4dac78b7d20 100644 --- a/docs/reference/limits-and-recovery.md +++ b/docs/reference/limits-and-recovery.md @@ -131,6 +131,8 @@ their per-call budgets (`max_file_bytes`, entry, depth and decoded-byte budgets, `inspect_erlang_beam` report it too. The per-page symbol-name text allowance of `inspect_elf_unwind_ranges` is a fixed allowance (`details.refusal.kind` `fixed_ceiling`); its `narrowing` asks for a smaller page `limit` or paging with `offset`. `inspect_erlang_beam` accepts `max_table_rows` (export, import and lambda rows combined; default 65,536, at most 1,048,576). +`inspect_python_pyc` reports `details.limit` for each of its per-call budgets (`max_file_bytes`, `max_objects`, `max_depth` and `max_string_bytes`). When a pyc limit is the product of two budgets (`max_objects` and `max_depth`), the refusal lists them in `details.related_parameters` instead. + ### Native table structure budget `inspect_elf_unwind_ranges`, `inspect_macho_function_starts`, `inspect_kernel_module`, `inspect_elf_debug_link`, diff --git a/src/domain/python/pythonPyc.ts b/src/domain/python/pythonPyc.ts index cc7c43a2650..1d66958e0fb 100644 --- a/src/domain/python/pythonPyc.ts +++ b/src/domain/python/pythonPyc.ts @@ -1,4 +1,5 @@ import { z } from "zod"; +import type { AnalysisParameterLimit } from "../analysisSettings.js"; import { digestSchema } from "../digests.js"; import { absoluteLocalPathStringSchema, @@ -86,50 +87,66 @@ export const pythonPycMarshalProfile = ( ): PythonPycMarshalProfile | null => PYTHON_PYC_MARSHAL_PROFILES.get(magic) ?? null; -export const PYTHON_PYC_LIMIT_MAXIMA = { - max_file_bytes: 64 * 1024 * 1024, - max_objects: 65536, - max_depth: 256, - max_string_bytes: 8 * 1024 * 1024, +/** The per-call budgets `inspect_python_pyc` accepts, shared by its schema and refusals. */ +export const PYTHON_PYC_READ_BUDGETS = { + max_file_bytes: { + minimum: 16, + maximum: 64 * 1024 * 1024, + default_value: 16 * 1024 * 1024, + }, + max_objects: { minimum: 1, maximum: 65536, default_value: 16384 }, + max_depth: { minimum: 1, maximum: 256, default_value: 64 }, + max_string_bytes: { + minimum: 1, + maximum: 8 * 1024 * 1024, + default_value: 1024 * 1024, + }, } as const; +export type PythonPycReadBudget = keyof typeof PYTHON_PYC_READ_BUDGETS; + +/** + * The per-call control that stopped pyc inspection, named by its contract + * field; each adapter names the option that sets it. The schema owns the same + * bounds, so the reported range is the one the next call accepts. + */ +export const pythonPycBudgetLimit = ( + parameter: PythonPycReadBudget, + effectiveValue: number, +): AnalysisParameterLimit => ({ + control_kind: "parameter", + parameter, + effective_value: effectiveValue, + unit: + parameter === "max_objects" || parameter === "max_depth" + ? "count" + : "bytes", + ...PYTHON_PYC_READ_BUDGETS[parameter], + applies_at: "next_call", +}); + +const budget = (name: PythonPycReadBudget) => { + const bounds = PYTHON_PYC_READ_BUDGETS[name]; + return z + .number() + .int() + .min(bounds.minimum) + .max(bounds.maximum) + .default(bounds.default_value); +}; export const pythonPycLimitsSchema = z.strictObject({ - max_file_bytes: z - .number() - .int() - .min(16) - .max(PYTHON_PYC_LIMIT_MAXIMA.max_file_bytes) - .default(16 * 1024 * 1024) - .describe( - "Maximum selected file size in bytes; exceeding the budget fails without partial success", - ), - max_objects: z - .number() - .int() - .min(1) - .max(PYTHON_PYC_LIMIT_MAXIMA.max_objects) - .default(16384) - .describe( - "Maximum marshal objects to examine; exceeding the budget fails without partial success", - ), - max_depth: z - .number() - .int() - .min(1) - .max(PYTHON_PYC_LIMIT_MAXIMA.max_depth) - .default(64) - .describe( - "Maximum marshal nesting depth; exceeding the budget fails without partial success", - ), - max_string_bytes: z - .number() - .int() - .min(1) - .max(PYTHON_PYC_LIMIT_MAXIMA.max_string_bytes) - .default(1024 * 1024) - .describe( - "Maximum bytes retained for one marshal string or byte string; exceeding the budget fails without partial success", - ), + max_file_bytes: budget("max_file_bytes").describe( + "Maximum selected file size in bytes; exceeding the budget fails without partial success", + ), + max_objects: budget("max_objects").describe( + "Maximum marshal objects to examine; exceeding the budget fails without partial success", + ), + max_depth: budget("max_depth").describe( + "Maximum marshal nesting depth; exceeding the budget fails without partial success", + ), + max_string_bytes: budget("max_string_bytes").describe( + "Maximum bytes retained for one marshal string or byte string; exceeding the budget fails without partial success", + ), }); export type PythonPycLimits = z.infer; diff --git a/src/python/PythonPycProvider.ts b/src/python/PythonPycProvider.ts index 1da511ff023..ff2b4e47fb0 100644 --- a/src/python/PythonPycProvider.ts +++ b/src/python/PythonPycProvider.ts @@ -11,7 +11,8 @@ import type { PythonPycPort } from "../ports/python/PythonPycPort.js"; import type { ExecutionOptions } from "../ports/AnalysisProvider.js"; import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js"; import { - PYTHON_PYC_LIMIT_MAXIMA, + PYTHON_PYC_READ_BUDGETS, + pythonPycBudgetLimit, type InspectPythonPycInput, type PythonPycLimits, } from "../domain/python/pythonPyc.js"; @@ -30,20 +31,30 @@ import { offlineArtifactFailureDialect } from "../providers/offlineArtifactFailu 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. + */ const parserLimitRecovery = ( input: InspectPythonPycInput, fields: readonly (keyof PythonPycLimits)[], ) => { const actions = fields.map((field) => { const selected = input[field]; - const maximum = PYTHON_PYC_LIMIT_MAXIMA[field]; - const option = `--${field.replaceAll("_", "-")}`; + const { maximum } = PYTHON_PYC_READ_BUDGETS[field]; return selected < maximum - ? `Raise ${field} (${option}) above ${selected}, up to ${maximum}, on the next call.` - : `${field} (${option}) is at this operation's maximum of ${maximum}; select a complete artifact within that bound.`; + ? `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] } } + : {}), remediationAction: `${actions.join(" ")} Repeating the unchanged request fails the same way; waiting or adding host memory does not raise these parser limits.`, }; }; diff --git a/tests/acceptance/pythonPycCli.test.ts b/tests/acceptance/pythonPycCli.test.ts index 9567c381b92..07162d7a202 100644 --- a/tests/acceptance/pythonPycCli.test.ts +++ b/tests/acceptance/pythonPycCli.test.ts @@ -702,13 +702,43 @@ cliTest( expect.soft(result.json, flag).toMatchObject({ code: "resource_constraint", retryable: false, - details: { resource, reported_limits: { boundary, maximum } }, + details: { + resource, + reported_limits: { boundary, maximum }, + limit: { parameter: field, effective_value: maximum, cli_flag: flag }, + }, remediation: { - action: expect.stringContaining( - `Raise ${field} (${flag}) above ${maximum}`, - ), + action: expect.stringContaining(`Raise ${field} above ${maximum}`), }, }); } }, ); + +cliTest( + "adds the CLI flag to a pyc budget refusal's structured limit", + async ({ cli }) => { + const directory = await createTestTempDirectory("rea-pyc-cli-limit-"); + const path = join(directory, "sample.pyc"); + await writeFile(path, pythonPycFixture("py311", { nested: true })); + const refused = await cli.run({ + arguments: ["inspect-python-pyc", path, "--max-objects", "1", "--json"], + }); + expect(refused.exitCode).toBe(1); + expect(refused.json).toMatchObject({ + code: "resource_constraint", + details: { + limit: { + control_kind: "parameter", + parameter: "max_objects", + effective_value: 1, + cli_flag: "--max-objects", + }, + }, + }); + // The service names contract fields; only the CLI spells flags. + expect( + JSON.stringify((refused.json as { remediation: unknown }).remediation), + ).not.toContain("--"); + }, +); diff --git a/tests/boundary/filesystem/pythonPyc.test.ts b/tests/boundary/filesystem/pythonPyc.test.ts index 4991061662c..1c2621244b0 100644 --- a/tests/boundary/filesystem/pythonPyc.test.ts +++ b/tests/boundary/filesystem/pythonPyc.test.ts @@ -29,11 +29,26 @@ describe("Python pyc stable filesystem boundary", () => { const bytes = pythonPycFixture("py311", { nested: true }); await writeFile(path, bytes); const service = createPythonPycService(); - for (const [field, maximum, resource] of [ - ["max_file_bytes", 16, "file-size"], - ["max_objects", 1, "memory"], - ["max_depth", 1, "capacity"], - ["max_string_bytes", 2, "memory"], + // Expected ranges are the published schema bounds, written out literally. + const ranges = { + max_file_bytes: { + minimum: 16, + maximum: 67108864, + default_value: 16777216, + }, + max_objects: { minimum: 1, maximum: 65536, default_value: 16384 }, + max_depth: { minimum: 1, maximum: 256, default_value: 64 }, + max_string_bytes: { + minimum: 1, + maximum: 8388608, + default_value: 1048576, + }, + } as const; + for (const [field, maximum, resource, unit] of [ + ["max_file_bytes", 16, "file-size", "bytes"], + ["max_objects", 1, "memory", "count"], + ["max_depth", 1, "capacity", "count"], + ["max_string_bytes", 2, "memory", "bytes"], ] as const) { const refused = await service.inspect({ path, [field]: maximum }); if (refused.ok) throw new Error(`Expected ${field} refusal`); @@ -46,13 +61,23 @@ describe("Python pyc stable filesystem boundary", () => { field === "max_file_bytes" ? { max_file_bytes: maximum } : { boundary: field, maximum }, + limit: { + control_kind: "parameter", + parameter: field, + effective_value: maximum, + unit, + ...ranges[field], + applies_at: "next_call", + }, }, remediation: { - action: expect.stringContaining( - `${field} (--${field.replaceAll("_", "-")})`, - ), + action: expect.stringContaining(`Raise ${field} above`), }, }); + // Contract fields only; the CLI adds its own flag spelling. + expect( + JSON.stringify(projectAnalysisError(refused.error).remediation), + ).not.toContain("--"); } const control = await service.inspect({ path }); if (!control.ok) throw control.error; diff --git a/tests/boundary/filesystem/pythonPycNestedLimit.test.ts b/tests/boundary/filesystem/pythonPycNestedLimit.test.ts new file mode 100644 index 00000000000..c6f3344eb4d --- /dev/null +++ b/tests/boundary/filesystem/pythonPycNestedLimit.test.ts @@ -0,0 +1,52 @@ +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { describe, expect, it, vi } from "vitest"; +import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js"; +import { pythonPycFixture } from "../../../src/python/PythonPyc.fixture.js"; +import { pythonPycLimit } from "../../../src/python/PythonPyc.errors.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; + +// The nested-code allowances are max_objects * max_depth, and no marshal +// fixture can spend them: each code object costs dozens of objects, so the +// object and depth budgets refuse first. The parser is therefore replaced +// here so the provider's real classification and projection run on the +// refusal it would raise. +const parser = vi.hoisted(() => ({ limit: undefined as unknown })); +vi.mock("../../../src/python/PythonPycParser.js", () => ({ + parsePythonPyc: () => { + throw parser.limit; + }, +})); + +const { createPythonPycService } = + await import("../../../src/composition/pythonPyc.js"); + +describe("pyc nested-code limits", () => { + it.each(["nested_code_steps", "nested_code_references"] as const)( + "reports %s as a product of two controls, not one range", + async (name) => { + parser.limit = pythonPycLimit( + name, + 4096, + "Nested code walk is too large.", + ); + const root = await createTestTempDirectory("rea-pyc-nested-limit-"); + const path = join(root, "sample.pyc"); + await writeFile(path, pythonPycFixture("py311")); + const refused = await createPythonPycService().inspect({ + path, + max_objects: 64, + max_depth: 64, + }); + if (refused.ok) throw new Error(`Expected ${name} refusal`); + const { details, remediation } = projectAnalysisError(refused.error); + expect(details).toMatchObject({ + resource: "memory", + related_parameters: ["max_objects", "max_depth"], + }); + expect(details).not.toHaveProperty("limit"); + expect(remediation?.action).toContain("Raise max_objects above 64"); + expect(remediation?.action).toContain("Raise max_depth above 64"); + }, + ); +});