Files
reverse-engineering-anything/src/python/PythonPycProvider.ts
T
morluto b211f216d8 fix(errors): vsix and pyc limit refusals state recovery from the measured requirement (#15407)
* fix(errors): let the typed limit refusal state recovery, with the measured requirement

A resource constraint with a typed adjustable limit now takes its recovery from the shared refusal owner, which compares the measured requirement with the control's maximum, instead of producer prose. The VSIX and pyc providers drop their hand-built "raise X up to MAX" text. pyc string and key limits report the size the parser measured; VSIX reports its early-stop lower bound only when it already exceeds the control's maximum.

Refs #15325

* fix(python): report an integer key's limit without a byte requirement it did not measure

* fix(errors): keep producer-authored recovery precedence; vsix and pyc state only typed facts

The global precedence change hid multi-control advice (V8) and producer-measured requirements kept in prose (Maven/Gradle). The VSIX and pyc producers no longer author prose for a single-control limit, so the shared typed recovery applies to them without changing other producers.
2026-12-01 02:30:52 +00:00

159 lines
5.0 KiB
TypeScript

import { PYTHON_PYC_PROVIDER_IDENTITY } from "./PythonPycProviderIdentity.js";
import {
artifactFilePrimaryFacts,
closeArtifactResources,
projectStableArtifactFailure,
type StableArtifactSnapshot,
readStableArtifact,
ArtifactResourceScope,
} from "../resources/index.js";
import type { PythonPycPort } from "../ports/python/PythonPycPort.js";
import type { ExecutionOptions } from "../ports/AnalysisProvider.js";
import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js";
import {
PYTHON_PYC_READ_BUDGETS,
pythonPycBudgetLimit,
type InspectPythonPycInput,
type PythonPycLimits,
} from "../domain/python/pythonPyc.js";
import { err, ok } from "../domain/result.js";
import {
PYTHON_PYC_FORMAT,
pythonPycLimitControls,
} from "./PythonPyc.errors.js";
import {
isParseLimit,
parseLimitConstraint,
} from "../domain/parseFailureAnalysisError.js";
import { parsePythonPyc } from "./PythonPycParser.js";
import { classifyProviderFailure } from "../providers/classifyProviderFailure.js";
import { offlineArtifactFailureDialect } from "../providers/offlineArtifactFailure.js";
const OPERATION = "inspect_python_pyc";
/**
* Recovery for the caller-selected budgets that determine one parser limit.
* 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];
return selected < maximum
? `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.`;
});
return {
retryable: false as const,
...(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.`,
};
};
const failureFor = (
cause: unknown,
input: InspectPythonPycInput,
snapshot?: StableArtifactSnapshot,
) => {
const facts = artifactFilePrimaryFacts(cause);
return projectStableArtifactFailure(
cause,
classifyProviderFailure(
facts.cause,
offlineArtifactFailureDialect(facts, {
operation: OPERATION,
providerId: PYTHON_PYC_PROVIDER_IDENTITY.id,
path: input.path,
description: "pyc",
recognise: (failure) => {
if (!isParseLimit(failure, PYTHON_PYC_FORMAT)) return undefined;
const { name } = failure.failure.limit;
return parseLimitConstraint(
OPERATION,
name === "max_depth" ? "capacity" : "memory",
failure,
parserLimitRecovery(
input,
pythonPycLimitControls(name),
failure.failure.limit.required,
),
);
},
fileSize: (failure) =>
new AnalysisResourceConstraintError(
OPERATION,
"file-size",
failure.message,
{ max_file_bytes: input.max_file_bytes },
{
cause: failure,
...parserLimitRecovery(input, ["max_file_bytes"]),
},
),
}),
),
input.path,
PYTHON_PYC_PROVIDER_IDENTITY.id,
OPERATION,
snapshot,
);
};
/** Portable parser over one bounded, identity-checked regular file. */
export class PythonPycProvider implements PythonPycPort {
readonly identity = PYTHON_PYC_PROVIDER_IDENTITY;
readonly #resources = new ArtifactResourceScope();
close(): Promise<void> {
return closeArtifactResources(
() => this.#resources.close(),
PYTHON_PYC_PROVIDER_IDENTITY,
OPERATION,
);
}
async inspect(input: InspectPythonPycInput, options?: ExecutionOptions) {
let snapshot: StableArtifactSnapshot | undefined;
try {
snapshot = await this.#resources.run(() =>
readStableArtifact(
input.path,
input.max_file_bytes,
options?.signal,
undefined,
this.#resources,
),
);
return ok(
parsePythonPyc(
snapshot.bytes,
{
path: input.path,
sha256: snapshot.sha256,
bytes: snapshot.bytes.length,
},
input,
),
);
} catch (cause) {
return err(failureFor(cause, input, snapshot));
}
}
}