fix(json): preserve runtime limits across JSON file readers (#1350)

* fix(cli): preserve runtime JSON string-limit diagnostics

* test(json): check actionable oversized Evidence recovery

* fix(json): decode large UTF-8 inputs by character length

* test(json): reject truncated UTF-8 after large input decode
This commit is contained in:
Ysopwnable
2026-10-09 19:39:35 +00:00
committed by GitHub
parent 750fb65650
commit 9c4a68bc19
8 changed files with 232 additions and 81 deletions
+69
View File
@@ -0,0 +1,69 @@
import { constants } from "node:buffer";
import { open } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "vitest";
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",
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.
await file.truncate(constants.MAX_STRING_LENGTH + 1);
} finally {
await file.close();
}
const limits = {
input_path: input,
input_bytes: constants.MAX_STRING_LENGTH + 1,
max_string_code_units: constants.MAX_STRING_LENGTH,
};
// Run readers sequentially in owned child processes, releasing each large
// read buffer when its process exits instead of retaining it in Vitest.
const result = await cli.run({
arguments: ["trace-application-feature", input, "--json"],
});
expect(result.json).toMatchObject({
code: "resource_constraint",
category: "resource_constraint",
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,
},
});
expect(result.exitCode).toBe(1);
expect(result.stderr).toBe("");
const evidence = await processes.run(process.execPath, [
"--input-type=module",
"-e",
`import { readJsonFile } from "./dist/application/JsonFiles.js";
import { projectAnalysisError } from "./dist/domain/analysisErrorProjection.js";
const result = await readJsonFile(process.argv[1]);
console.log(JSON.stringify(result.ok ? result : projectAnalysisError(result.error)));`,
input,
]);
expect(JSON.parse(evidence.stdout)).toMatchObject({
code: "resource_constraint",
details: {
operation: "read_evidence_file",
resource: "memory",
reported_limits: limits,
},
});
expect(evidence.stderr).toBe("");
expect(evidence.exitCode).toBe(0);
},
);
@@ -1,6 +1,7 @@
import { execFile } from "node:child_process";
import { constants as bufferConstants } from "node:buffer";
import {
appendFile,
chmod,
open,
symlink,
@@ -119,8 +120,7 @@ describe("CLI JSON input", () => {
},
},
remediation: {
action:
"Split the JSON into smaller inputs or rerun its producer on a smaller subset, then retry.",
action: expect.stringContaining("re-analyze a smaller selection"),
},
},
});
@@ -137,6 +137,13 @@ describe("CLI JSON input", () => {
if (typeof multibyteResult.value !== "string")
throw new Error("Expected parsed JSON string");
expect(multibyteResult.value.length).toBe(characterCount);
// Large-input streaming must flush and reject an incomplete UTF-8 suffix.
await appendFile(path, Buffer.from([0xc3]));
expect(await parseCliJsonInput(path, "test-input")).toMatchObject({
ok: false,
error: { input_path: path, input_reason: "invalid-json" },
});
}, 20_000);
it("distinguishes malformed inline text from files and preserves bracket-prefixed paths", async () => {