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:
Ysopwnable
2026-10-10 04:53:09 +08:00
committed by GitHub
parent 3728c8267b
commit 6ce3288f3b
10 changed files with 555 additions and 197 deletions
+13 -12
View File
@@ -3,7 +3,6 @@ import {
lstat,
mkdtemp,
open,
readFile,
realpath,
rename,
rm,
@@ -18,6 +17,8 @@ import {
} from "../domain/analysisErrorCore.js";
import { err, ok, type Result } from "../domain/result.js";
import { parseUtf8Json } from "./Utf8JsonInput.js";
import { readRegularFile } from "./RegularFileRead.js";
import { NonRegularFileReadError } from "../filesystem/RegularFile.js";
/** Request control and its owning operation for an interruptible atomic write. */
export interface TextWriteCancellation {
@@ -36,13 +37,7 @@ export const readJsonFile = async (
> => {
const requestedPath = resolve(path);
try {
const canonicalPath = await realpath(requestedPath);
const stats = await lstat(canonicalPath);
if (!stats.isFile())
return err(
new EvidenceFileError("read", "not-file", { path: requestedPath }),
);
const encoded = await readFile(canonicalPath);
const encoded = await readRegularFile(requestedPath);
const decoded = parseUtf8Json(encoded, "read_evidence_file", requestedPath);
if (!decoded.ok) {
return err(
@@ -56,10 +51,16 @@ export const readJsonFile = async (
} catch (cause: unknown) {
if (cause instanceof AnalysisResourceConstraintError) return err(cause);
return err(
new EvidenceFileError("read", missingOrIo(cause), {
cause,
path: requestedPath,
}),
new EvidenceFileError(
"read",
cause instanceof NonRegularFileReadError
? "not-file"
: missingOrIo(cause),
{
cause,
path: requestedPath,
},
),
);
}
};
+18 -5
View File
@@ -1,16 +1,29 @@
import type { Stats } from "node:fs";
import type { FileHandle } from "node:fs/promises";
import { openRegularFile } from "../filesystem/RegularFile.js";
/** Admit a regular file without waiting for a pipe, then read its verified handle. */
export const readRegularFile = async (
/** Admit one regular-file handle, retain it through the consumer, and always close it. */
export const withRegularFile = async <Value>(
path: string,
read: (handle: FileHandle, stats: Stats) => Promise<Value>,
signal?: AbortSignal,
): Promise<Buffer> => {
): Promise<Value> => {
const handle = await openRegularFile(path, { symlinks: "follow", signal });
try {
const bytes = await handle.readFile({ signal });
const stats = await handle.stat();
signal?.throwIfAborted();
return bytes;
const value = await read(handle, stats);
signal?.throwIfAborted();
return value;
} finally {
await handle.close();
}
};
/** Admit a regular file without waiting for a pipe, then read its verified handle. */
export const readRegularFile = (
path: string,
signal?: AbortSignal,
): Promise<Buffer> =>
withRegularFile(path, (handle) => handle.readFile({ signal }), signal);
+5 -2
View File
@@ -3,6 +3,10 @@ import { constants } from "node:buffer";
import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js";
import { safeParseJson, type SafeJsonParseResult } from "../domain/safeJson.js";
/** Recovery that preserves complete, authenticated Evidence workflow inputs. */
export const JSON_INPUT_RESOURCE_REMEDIATION =
"Provide a smaller valid JSON value. For Evidence workflows, re-analyze a smaller selection of the original target and use its complete Evidence; splitting JSON text or trimming Evidence fields does not produce valid workflow input.";
/** Decode JSON bytes without confusing a runtime string limit with malformed input. */
export const parseUtf8Json = (
bytes: Uint8Array,
@@ -29,8 +33,7 @@ export const parseUtf8Json = (
},
{
...(cause === undefined ? {} : { cause }),
remediationAction:
"Provide a smaller JSON document. For Evidence-based workflows, re-analyze a smaller selection of the original target and use its Evidence; splitting JSON text alone does not produce a valid workflow input.",
remediationAction: JSON_INPUT_RESOURCE_REMEDIATION,
},
);
let text: string;