fix(cli): stream JSON inputs beyond the whole-document string limit (#1360)
Stream large CLI JSON files through a strict UTF-8 decoder and standard JSON parser using the same admitted regular-file handle. Preserve native parsing for small inputs, typed malformed-input/resource/access-denied errors, cancellation, and complete Evidence. Verified with focused public CLI regressions, a 768 MB whitespace-padded valid request, and the Windows native decoder boundary. Individual strings and the assembled object graph remain subject to native runtime limits. All 27 final-head checks passed. Refs #1331
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { constants } from "node:buffer";
|
||||
import { open } from "node:fs/promises";
|
||||
import { mkdir, open, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { expect } from "vitest";
|
||||
@@ -8,14 +8,14 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
|
||||
cliTest(
|
||||
"preserves the runtime string limit through CLI and Evidence file inputs",
|
||||
"rejects malformed oversized CLI input and preserves Evidence decoder resource failures",
|
||||
async ({ cli, processes }) => {
|
||||
const root = await createTestTempDirectory("rea-json-string-limit-");
|
||||
const input = join(root, "oversized.json");
|
||||
const file = await open(input, "wx");
|
||||
try {
|
||||
// Sparse zero bytes are valid UTF-8. Decoded length exceeds the runtime
|
||||
// string limit before JSON syntax can be inspected; no fixture is retained.
|
||||
// Sparse zero bytes are valid UTF-8. The Evidence reader reaches the
|
||||
// decoded-length limit; the streamed CLI rejects their invalid JSON syntax.
|
||||
await file.truncate(constants.MAX_STRING_LENGTH + 1);
|
||||
} finally {
|
||||
await file.close();
|
||||
@@ -31,17 +31,9 @@ cliTest(
|
||||
arguments: ["trace-application-feature", input, "--json"],
|
||||
});
|
||||
expect(result.json).toMatchObject({
|
||||
code: "resource_constraint",
|
||||
category: "resource_constraint",
|
||||
code: "invalid_request",
|
||||
input_path: input,
|
||||
input_reason: "too-large",
|
||||
retryable: false,
|
||||
remediation: { action: expect.stringContaining("re-analyze") },
|
||||
details: {
|
||||
operation: "trace-application-feature",
|
||||
resource: "memory",
|
||||
reported_limits: limits,
|
||||
},
|
||||
input_reason: "invalid-json",
|
||||
});
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toBe("");
|
||||
@@ -67,3 +59,101 @@ cliTest(
|
||||
expect(evidence.exitCode).toBe(0);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"traces valid Evidence beyond the whole-document string limit",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-json-streamed-evidence-");
|
||||
const source = join(root, "application");
|
||||
await mkdir(source);
|
||||
await writeFile(
|
||||
join(source, "package.json"),
|
||||
JSON.stringify({
|
||||
name: "streamed-evidence",
|
||||
type: "module",
|
||||
main: "entry.mjs",
|
||||
}),
|
||||
);
|
||||
await writeFile(
|
||||
join(source, "entry.mjs"),
|
||||
'export const observed = "source-owned";',
|
||||
);
|
||||
const application = await cli.run({
|
||||
arguments: [
|
||||
"analyze-javascript-application",
|
||||
source,
|
||||
"--artifact-format",
|
||||
"directory",
|
||||
"--json",
|
||||
],
|
||||
});
|
||||
expect(application.exitCode).toBe(0);
|
||||
const request = JSON.stringify({
|
||||
application: application.json,
|
||||
seed: { kind: "module", value: "entry.mjs", match: "contains" },
|
||||
direction: "incoming",
|
||||
});
|
||||
const input = join(root, "trace.json");
|
||||
await writeFile(input, request);
|
||||
const ordinary = await cli.run({
|
||||
arguments: ["trace-application-feature", input, "--json"],
|
||||
});
|
||||
expect(ordinary.exitCode).toBe(0);
|
||||
const file = await open(input, "a");
|
||||
try {
|
||||
const whitespace = Buffer.alloc(1024 * 1024, 0x20);
|
||||
let remaining =
|
||||
constants.MAX_STRING_LENGTH + 1 - Buffer.byteLength(request);
|
||||
while (remaining > 0) {
|
||||
const length = Math.min(remaining, whitespace.length);
|
||||
await file.writeFile(whitespace.subarray(0, length));
|
||||
remaining -= length;
|
||||
}
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
const streamed = await cli.run({
|
||||
arguments: ["trace-application-feature", input, "--json"],
|
||||
});
|
||||
expect(streamed.exitCode).toBe(0);
|
||||
expect(streamed.json).toEqual(ordinary.json);
|
||||
expect(streamed.stderr).toBe("");
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"returns a typed string assembly constraint under a bounded heap",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-json-string-headroom-");
|
||||
const input = join(root, "long-string.json");
|
||||
const file = await open(input, "wx");
|
||||
try {
|
||||
await file.writeFile('"');
|
||||
const chunk = Buffer.alloc(1024 * 1024, 0x78);
|
||||
let remaining = constants.MAX_STRING_LENGTH + 1;
|
||||
while (remaining > 0) {
|
||||
const length = Math.min(remaining, chunk.length);
|
||||
await file.writeFile(chunk.subarray(0, length));
|
||||
remaining -= length;
|
||||
}
|
||||
await file.writeFile('"');
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
const result = await cli.run({
|
||||
arguments: ["trace-application-feature", input, "--json"],
|
||||
environment: { NODE_OPTIONS: "--max-old-space-size=768" },
|
||||
});
|
||||
expect(result.exitCode, result.stderr).toBe(1);
|
||||
expect(result.stderr).toBe("");
|
||||
expect(result.json).toMatchObject({
|
||||
code: "resource_constraint",
|
||||
input_path: input,
|
||||
input_reason: "too-large",
|
||||
details: { resource: "memory" },
|
||||
remediation: {
|
||||
action: expect.stringContaining("re-analyze a smaller selection"),
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user