Files
reverse-engineering-anything/src/application/JsonInputFileRefusal.test.ts
T
CyberSproutandVincent Koc 6c7dffc59b fix(json): classify JSON input heap headroom and string-ceiling refusals (#14949)
* fix(json): classify JSON input heap headroom and string-ceiling refusals

Mark live V8 heap-headroom refusals in JsonInputFile as measuredExhaustion, bind single-string runtime and serialization ceilings in JsonInputFile and JsonAuthenticationHeadroom to registered representation allowances, and remove all 4 entries from untyped-budget-refusal-baseline.json (Refs #14234).

* test(json): inject runtime probes instead of mocking node:v8

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-11-30 15:55:37 +08:00

140 lines
4.7 KiB
TypeScript

import { constants } from "node:buffer";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { getHeapStatistics } from "node:v8";
import { describe, expect, it } from "vitest";
import { readJsonInputFile } from "./JsonInputFile.js";
import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
describe("JsonInputFile resource constraint classification", () => {
it("marks streaming container-assembly heap exhaustion as measuredExhaustion without fixed_ceiling refusal", async () => {
const root = await createTestTempDirectory("rea-json-input-headroom-");
const path = join(root, "doc.json");
await writeFile(path, JSON.stringify({ ok: true }));
const result = await readJsonInputFile(
path,
"read_evidence_file",
undefined,
undefined,
false,
{
getHeapStatistics: () => ({
...getHeapStatistics(),
total_available_size: 0,
}),
},
);
if (result.ok || !(result.error instanceof AnalysisResourceConstraintError))
throw new Error("Expected low-headroom refusal");
expect(result.error.recovery).toMatchObject({
measuredExhaustion: true,
});
const projected = projectAnalysisError(result.error);
expect(projected).toMatchObject({
code: "resource_constraint",
details: {
operation: "read_evidence_file",
resource: "memory",
reported_limits: {
input_path: path,
available_heap_bytes: 0,
},
},
remediation: {
action: expect.stringContaining("re-analyze a smaller selection"),
},
});
expect(projected.details).not.toHaveProperty("refusal");
});
it("marks long-scalar assembly heap exhaustion as measuredExhaustion", async () => {
const root = await createTestTempDirectory("rea-json-scalar-headroom-");
const path = join(root, "scalar.json");
await writeFile(path, JSON.stringify("x".repeat(70_000)));
let calls = 0;
const result = await readJsonInputFile(
path,
"trace_application_feature",
undefined,
undefined,
false,
{
getHeapStatistics: () => {
calls += 1;
const actual = getHeapStatistics();
return {
...actual,
// Force streaming mode on the first check and exhaust string-assembly headroom next.
total_available_size: calls <= 2 ? 0 : actual.total_available_size,
};
},
},
);
if (result.ok || !(result.error instanceof AnalysisResourceConstraintError))
throw new Error("Expected scalar-assembly headroom refusal");
expect(result.error.recovery).toMatchObject({
measuredExhaustion: true,
});
expect(result.error.reportedLimits).toMatchObject({
input_path: path,
string_code_units: expect.any(Number),
string_assembly_headroom_bytes: expect.any(Number),
});
expect(projectAnalysisError(result.error).details).not.toHaveProperty(
"refusal",
);
});
it("projects a fixed_ceiling refusal when a streamed JSON scalar exceeds constants.MAX_STRING_LENGTH", async () => {
const root = await createTestTempDirectory("rea-json-string-ceiling-");
const path = join(root, "oversized.json");
await writeFile(path, JSON.stringify({ key: "value_exceeding_probe" }));
let calls = 0;
const result = await readJsonInputFile(
path,
"read_evidence_file",
undefined,
undefined,
false,
{
getHeapStatistics: () => {
calls += 1;
const actual = getHeapStatistics();
return {
...actual,
// Route through the streaming pipeline on nativeParseFits, then keep headroom available.
total_available_size: calls === 1 ? 0 : actual.total_available_size,
};
},
maxStringCodeUnits: 8,
},
);
if (result.ok) throw new Error("Expected runtime string-length refusal");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "resource_constraint",
retryable: false,
details: {
operation: "read_evidence_file",
resource: "memory",
reported_limits: {
input_path: path,
max_string_code_units: constants.MAX_STRING_LENGTH,
},
refusal: {
kind: "fixed_ceiling",
operation: "read_evidence_file",
allowance: "The JavaScript runtime single-string length limit",
allowance_kind: "parser",
limit: constants.MAX_STRING_LENGTH,
unit: "code_units",
},
},
});
});
});