fix(trace): report the enforced JSON parser limit (#12335)

fix(browser): report Playwright trace parser limits
This commit is contained in:
morluto authored and GitHub committed 2026-11-19 12:57:34 +00:00
1 parent 77be47fbf9
commit 7d5e9be81c
7 files changed
+87 -14

No files matched your search

+3 -1
View File
@@ -112,7 +112,9 @@ retryable capacity error. Retained queries do not require another import.
Resource policies admit a 256-MiB archive, 32 MiB of decoded streams, a
conservative 32-MiB retained-facts representation, 4-MiB metadata reads and
4-MiB physical lines. JSON parsing additionally checks heap headroom, a
131,072-node record allowance and the shared 64-level capture nesting budget.
131,072-node record allowance and a fixed 64-level trace nesting allowance.
JSON parser refusals report the exhausted limit name and maximum in
`details.reported_limits`.
These are resource policies, not Playwright format limits. Paging or field
selection does not increase parsing allowances; choose a smaller complete
recorder chunk when admission fails.
@@ -37,7 +37,7 @@
"src/browser/history/playwrightTrace/TraceFailure.ts": 1,
"src/browser/history/playwrightTrace/TraceInventory.ts": 1,
"src/browser/history/playwrightTrace/TraceLines.ts": 2,
"src/browser/history/playwrightTrace/TraceStream.ts": 2,
"src/browser/history/playwrightTrace/TraceStream.ts": 1,
"src/cli/cliJsonInput.ts": 2,
"src/cli/cliResultOutput.ts": 2,
"src/cli/environmentCommands.ts": 1,
+17
View File
@@ -1,6 +1,8 @@
import { expect, it, onTestFinished } from "vitest";
import { createTestProcessRegistry } from "../../../tests/support/process/processFixture.js";
import { selectedWebNetworkCaptureBudgets } from "../../domain/webNetworkCapture.js";
import { withCaptureBudgets } from "./CaptureBudgets.js";
import { parseCaptureJson } from "./CaptureJson.js";
it("decodes every JSON string escape as JSON.parse does", () => {
@@ -9,6 +11,21 @@ it("decodes every JSON string escape as JSON.parse does", () => {
expect(parseCaptureJson(text, "HAR")).toEqual(JSON.parse(text));
});
it("keeps trace nesting independent of the selected network capture depth", () => {
const text = "[[true]]";
const budgets = selectedWebNetworkCaptureBudgets({ max_nesting_depth: 1 });
withCaptureBudgets(budgets, () => {
expect(() => parseCaptureJson(text, "HAR")).toThrow(
expect.objectContaining({
failure: expect.objectContaining({
limit: { name: "nesting-depth", maximum: 1 },
}),
}),
);
expect(parseCaptureJson(text, "Playwright trace")).toEqual([[true]]);
});
});
it("retains escape-heavy strings within a heap near their decoded size", async () => {
const processes = createTestProcessRegistry();
onTestFinished(() => processes.shutdown());
+8 -3
View File
@@ -4,6 +4,7 @@ import { visit } from "jsonc-parser";
import { LosslessNumber } from "lossless-json";
import { CaptureFormatError } from "./CaptureFormatError.js";
import { decodeStrictUtf8 } from "../../domain/strictUtf8.js";
import { WEB_NETWORK_CAPTURE_LIMITS } from "../../domain/webNetworkCapture.js";
type Frame =
| {
@@ -51,6 +52,10 @@ export const parseCaptureJson = (
): unknown => {
const frames: Frame[] = [];
const admitNode = captureNodeAdmission(maximumNodes);
const fixedDepth = format === "Playwright trace";
const maximumDepth = fixedDepth
? WEB_NETWORK_CAPTURE_LIMITS.depth
: captureBudgets().depth;
let result: unknown;
const valuePointer = (): string => {
const parent = frames.at(-1);
@@ -66,10 +71,10 @@ export const parseCaptureJson = (
// container add no level, so empty and scalar-bearing containers agree.
const admitContainer = (): void => {
admitNode();
if (frames.length >= captureBudgets().depth)
if (frames.length >= maximumDepth)
throw new CaptureFormatError(
{ name: "nesting-depth", maximum: captureBudgets().depth },
`${format} exceeds max_nesting_depth (${String(captureBudgets().depth)} levels).`,
{ name: "nesting-depth", maximum: maximumDepth },
`${format} exceeds ${fixedDepth ? "its fixed JSON nesting allowance" : "max_nesting_depth"} (${String(maximumDepth)} levels).`,
valuePointer(),
);
};
@@ -391,6 +391,11 @@ it("bounds JSON node expansion even when many tokens fit inside a small physical
expect(result.error._tag).toBe("AnalysisResourceConstraintError");
expect(projectAnalysisError(result.error).details).toMatchObject({
refusal: { limit: 131072, unit: "parser nodes" },
reported_limits: {
boundary: "parser-nodes",
maximum: 131072,
parser_nodes: 131072,
},
});
expect(resources.hasPending).toBe(false);
} finally {
@@ -10,6 +10,10 @@ import {
AnalysisResourceConstraintError,
AnalysisUnsupportedTargetError,
} from "../../../domain/analysisErrorCore.js";
import {
isParseLimit,
parseLimitConstraint,
} from "../../../domain/parseFailureAnalysisError.js";
import {
PLAYWRIGHT_TRACE_OPERATION,
PLAYWRIGHT_TRACE_LIMITS,
@@ -18,7 +22,11 @@ import {
type TraceEvent,
} from "../../../domain/playwrightTrace/playwrightTrace.js";
import { captureObject } from "../CaptureRedaction.js";
import { CAPTURE_LIMITS, CaptureFormatError } from "../CaptureFormatError.js";
import {
CAPTURE_FORMAT,
CAPTURE_LIMITS,
CaptureFormatError,
} from "../CaptureFormatError.js";
import { parseCaptureJson } from "../CaptureJson.js";
import { TraceInventory, traceIdentity } from "./TraceInventory.js";
import { traceLines, type TraceLineBudget } from "./TraceLines.js";
@@ -142,18 +150,18 @@ class TraceStreamRead {
raw = readRecord(line.bytes);
} catch (cause) {
if (!(cause instanceof CaptureFormatError)) throw cause;
const { limit } = cause;
if (limit !== undefined) {
// The parser names the allowance it exhausted: node count or depth.
const { name, maximum } = limit;
if (isParseLimit(cause, CAPTURE_FORMAT)) {
const { name, maximum } = cause.failure.limit;
if (name !== "nesting-depth" && name !== "parser-nodes") throw cause;
const nesting = name === "nesting-depth";
throw new AnalysisResourceConstraintError(
throw parseLimitConstraint(
PLAYWRIGHT_TRACE_OPERATION,
"memory",
cause.message,
{ [nesting ? "nesting_levels" : "parser_nodes"]: maximum },
cause,
{
cause,
reportedLimitContext: {
[nesting ? "nesting_levels" : "parser_nodes"]: maximum,
},
...fixedAllowanceOptions(
nesting
? "The Playwright trace record nesting allowance"
@@ -17,6 +17,42 @@ import { evidenceBundleSchema } from "../../../src/domain/evidenceBundle.js";
type McpCall = Awaited<ReturnType<typeof connectLocalToolsMcp>>["call"];
cliTest(
"reports the fixed trace nesting limit through CLI and MCP",
async ({ cli }) => {
const trace =
'{"type":"context-options","version":10}\n' +
`{"type":"console","args":${"[".repeat(65)}${"]".repeat(65)}}\n`;
const fixture = await playwrightTraceFixture({ trace, network: "" });
const expected = {
code: "resource_constraint",
retryable: false,
details: {
reason: expect.stringContaining("fixed JSON nesting allowance"),
reported_limits: {
boundary: "nesting-depth",
maximum: 64,
nesting_levels: 64,
},
refusal: { kind: "fixed_ceiling", limit: 64, unit: "nesting levels" },
},
};
const result = await cli.run({
arguments: ["inspect-playwright-trace", fixture.path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(1);
expect(result.json).toMatchObject(expected);
const { call } = await connectLocalToolsMcp({
REA_MCP_TOOL_FAMILIES: "application",
});
const response = await call("inspect_playwright_trace", {
path: fixture.path,
});
expect(response.isError).toBe(true);
expect(parseMcpToolError(response).error).toMatchObject(expected);
},
);
cliTest(
"preserves original ZIP paths across CLI/MCP, resource lookup and exact member filters",
async ({ cli }) => {