mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 10:04:50 +00:00
* feat(errors): type refusals at the producer with a total recovery A resource refusal without authored text fell back to resource-kind advice: a fixed parser ceiling reported as memory told the caller to add host memory, an upstream IDA reply ceiling pointed at REA's MCP transport, and a fixed Kuna text ceiling reported as capacity was marked retryable. An ambiguous selector was reported as invalid input that pointed at the schema. Add AnalysisRefusal with three variants built from facts producers already own: fixed_ceiling (FixedAllowance, now with limit, unit and narrowing), adjustable_budget (AnalysisSettingLimit) and ambiguous_selector (new AnalysisInputError option). Each variant is attributed to the error's operation and projected under details.refusal. Presentation is a total switch over the variants, and authored text still takes precedence. Migrate the first producers. GraphQL nesting, Playwright trace input, the Kuna function text and the IDA upstream reply become fixed ceilings. jadx overloads and Kuna ambiguous names become ambiguous selectors, replacing the two Kuna-specific ambiguity texts. Seven occupied admissions declare measuredExhaustion. The rea/typed-resource-refusal lint rule rejects new untyped resource constraints, and the remaining 24 are counted in a baseline that can only shrink. * fix(errors): honor refusal recovery ownership
95 lines
3.0 KiB
TypeScript
95 lines
3.0 KiB
TypeScript
import { GraphQLError, Source, parse, visit, type DocumentNode } from "graphql";
|
|
import { AnalysisResourceConstraintError } from "../analysisErrorCore.js";
|
|
import { fixedAllowanceOptions } from "../fixedAllowanceRemediation.js";
|
|
import {
|
|
API_CONTRACT_OPERATION,
|
|
API_CONTRACT_MAX_FILE_BYTES,
|
|
type ApiContractArtifact,
|
|
} from "./apiContract.js";
|
|
import {
|
|
GRAPHQL_CONTRACT_LIMITS,
|
|
type GraphqlSource,
|
|
} from "./graphqlContract.js";
|
|
|
|
export interface GraphqlSourceDocument {
|
|
readonly artifact: ApiContractArtifact;
|
|
readonly text: string;
|
|
}
|
|
|
|
/** The library does not distinguish syntax and parser-policy errors with a stable code. */
|
|
export class GraphqlDocumentRefusal extends Error {
|
|
readonly source: GraphqlSource | null;
|
|
constructor(
|
|
cause: GraphQLError,
|
|
document: GraphqlSourceDocument,
|
|
role: GraphqlSource["role"],
|
|
) {
|
|
super("The GraphQL parser refused this document.", { cause });
|
|
const offset = cause.positions?.[0],
|
|
location = cause.locations?.[0];
|
|
this.source =
|
|
offset === undefined || location === undefined
|
|
? null
|
|
: {
|
|
role,
|
|
sha256: document.artifact.sha256,
|
|
start_utf16: offset,
|
|
end_utf16: offset,
|
|
line: location.line,
|
|
column: location.column,
|
|
};
|
|
}
|
|
}
|
|
|
|
/** Public parser allowances bound allocation; iterative AST admission bounds subsequent traversal. */
|
|
export const parseGraphqlDocument = (
|
|
document: GraphqlSourceDocument,
|
|
role: GraphqlSource["role"],
|
|
): DocumentNode => {
|
|
try {
|
|
const parsed = parse(new Source(document.text, document.artifact.path), {
|
|
maxLexicalTokens: GRAPHQL_CONTRACT_LIMITS.lexicalTokens,
|
|
maxComments: GRAPHQL_CONTRACT_LIMITS.comments,
|
|
maxSourceLength: API_CONTRACT_MAX_FILE_BYTES,
|
|
});
|
|
let depth = 0;
|
|
visit(parsed, {
|
|
enter() {
|
|
depth++;
|
|
if (depth > GRAPHQL_CONTRACT_LIMITS.astDepth)
|
|
throw new AnalysisResourceConstraintError(
|
|
API_CONTRACT_OPERATION,
|
|
"memory",
|
|
"GraphQL AST nesting exceeds the 128-level traversal allowance.",
|
|
{ max_ast_depth: GRAPHQL_CONTRACT_LIMITS.astDepth },
|
|
fixedAllowanceOptions(
|
|
`The GraphQL AST nesting allowance of ${String(GRAPHQL_CONTRACT_LIMITS.astDepth)} levels`,
|
|
"parser",
|
|
{ limit: GRAPHQL_CONTRACT_LIMITS.astDepth, unit: "levels" },
|
|
),
|
|
);
|
|
},
|
|
leave() {
|
|
depth--;
|
|
},
|
|
});
|
|
return parsed;
|
|
} catch (cause) {
|
|
if (cause instanceof GraphQLError)
|
|
throw new GraphqlDocumentRefusal(cause, document, role);
|
|
if (cause instanceof RangeError)
|
|
throw new AnalysisResourceConstraintError(
|
|
API_CONTRACT_OPERATION,
|
|
"memory",
|
|
"GraphQL parsing exceeded a runtime representation allowance.",
|
|
{ parser: "graphql-js" },
|
|
{
|
|
cause,
|
|
remediationAction:
|
|
"Use a smaller, less deeply nested complete document.",
|
|
},
|
|
);
|
|
throw cause;
|
|
}
|
|
};
|