Files
reverse-engineering-anything/src/domain/apiContract/GraphqlDocuments.ts
T
morluto 98d83d0787 feat(trace)!: give Playwright trace import per-call trace_limits (#14628)
* feat(trace)!: give Playwright trace import per-call trace_limits

inspect_playwright_trace refused large ordinary traces at a fixed archive
allowance, a fixed record nesting allowance and four authored-only
budgets (decoded streams, retained facts, metadata reads, line bytes).
It now accepts trace_limits (CLI --trace-limits JSON) with today's values
as defaults, and each refusal reports details.limit. The record parser
node allowance is derived from facts_bytes and recovers through it;
nesting_depth is capped where a retained record still fits the Evidence
JSON depth. The capture JSON parser takes the trace's depth from the
caller instead of a format special case.

The source-location view's page refusal now reports view.max_bytes in
details.limit. GraphQL AST nesting is registered as a representation
bound, and GraphQL host-limit, web-bundle host-capacity and the trace
decoder's occupied slot declare measured exhaustion.

BREAKING CHANGE: PlaywrightTracePort.inspect takes the resolved limits,
and trace archive and nesting refusals report details.limit instead of a
fixed_ceiling refusal.

* fix(trace): project trace records at the selected depth and record trace_limits

The HAR projection of a trace record read the network capture's depth
(64 by default), so a record admitted under a raised
trace_limits.nesting_depth was dropped as malformed. Projection now runs
under the selected trace depth. Evidence parameters record the caller's
trace_limits selection as given, in complete and partial results.
2026-11-25 22:09:23 -05:00

98 lines
3.2 KiB
TypeScript

import { GraphQLError, Source, parse, visit, type DocumentNode } from "graphql";
import { AnalysisResourceConstraintError } from "../analysisErrorCore.js";
import { fixedAllowanceOptions } from "../fixedAllowanceRemediation.js";
import { isHostLimitFailure } from "../parseFailure.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 (isHostLimitFailure(cause))
throw new AnalysisResourceConstraintError(
API_CONTRACT_OPERATION,
"memory",
"GraphQL parsing exceeded a runtime representation allowance.",
{ parser: "graphql-js" },
{
cause,
// The host ran out of stack or heap; no REA control was reached.
measuredExhaustion: true,
remediationAction:
"Use a smaller, less deeply nested complete document.",
},
);
throw cause;
}
};