Files
reverse-engineering-anything/src/domain/javascript/javascriptAnalysisResourceControls.test.ts
T
morluto 2430b081c9 refactor(providers): bind provider refusals' per-call controls instead of naming CLI flags (#11455)
* refactor(native): bind producer_limits and max_input_bytes as the per-call controls of pwntools and Apple dispatch refusals

producerLimitControl in src/domain/producerResourceLimits.ts names one producer budget as producer_limits.<field>; the shared provider outcome classifier now carries a resource signal's limit. Pwntools file-size, CPU and output refusals and the Apple dispatch input refusal bind their control, and their remediation no longer names CLI flags.

* fix(native): report the selected output allowance as the omitted file_bytes default

* fix(native): bind producer_limits.diagnostic_bytes as the pwntools command output-limit control

* refactor(rust): bind producer_limits as the per-call control of Rust input and report refusals (#11458)

* refactor(rust): bind producer_limits as the per-call control of Rust input and report refusals

* test(rust): cover the complete-report refusal's producer_limits control

* refactor(browser): bind caller-selectable browser deadlines as per-call controls instead of naming CLI flags (#11466)

* refactor(rust): bind producer_limits as the per-call control of Rust input and report refusals

* test(rust): cover the complete-report refusal's producer_limits control

* refactor(browser): bind caller-selectable browser deadlines as per-call controls instead of naming CLI flags

deadlineParameterLimit in src/domain/analysisResourceLimits.ts owns the millisecond deadline control that nativeDeadlineRecovery and the browser recoveries share. CDP command and discovery, web bundle per-script and total, network capture and source-map decode deadlines now carry it; browserDeadlineIncrease no longer takes CLI flag text.

* fix(browser): record a per-script deadline's parameter control in partial web bundle results

* refactor(providers): bind Wabt, SQLite, EVMole, Apktool and Go controls instead of naming CLI commands (#11472)

* refactor(providers): bind Wabt, SQLite, EVMole and Apktool deadline and byte controls instead of naming CLI commands

These remediation strings named rea commands and flags through template text the cli-option guard did not count. Wabt byte refusals and output limits bind max_input_bytes, max_tool_bytes or max_output_bytes while a larger value is selectable; SQLite worker_timeout_ms, EVMole timeout_ms and Apktool decode_timeout_ms bind through deadlineParameterLimit; the shared provider outcome classifier now carries an output limit's control.

* refactor(go): bind Go analysis budgets as per-call controls instead of naming CLI flags

* fix(providers): keep the typed primary when Wabt, EVMole or pwntools command cleanup fails

* ci(guards): count template-built CLI option and command text outside the CLI layer (#11478)

* refactor(providers): bind Wabt, SQLite, EVMole and Apktool deadline and byte controls instead of naming CLI commands

These remediation strings named rea commands and flags through template text the cli-option guard did not count. Wabt byte refusals and output limits bind max_input_bytes, max_tool_bytes or max_output_bytes while a larger value is selectable; SQLite worker_timeout_ms, EVMole timeout_ms and Apktool decode_timeout_ms bind through deadlineParameterLimit; the shared provider outcome classifier now carries an output limit's control.

* refactor(go): bind Go analysis budgets as per-call controls instead of naming CLI flags

* fix(providers): keep the typed primary when Wabt, EVMole or pwntools command cleanup fails

* ci(guards): count template-built CLI option and command text outside the CLI layer

The cli-option-text-outside-cli pattern now also matches "(CLI ...", "on the CLI", "or use rea ..." and backticked `rea <command>` references, so remediation built from a template such as --${name} or rea inspect-... <path> is counted. Doctor.ts is exempt: setup and doctor run only through the CLI. The baseline records the newly counted pre-existing sites (22 -> 45 across 21 files).

* ci(guards): extract the CLI text matcher, exempt host-only commands with written reasons, and test each spelling

* ci(guards): exempt a host-only command and its own arguments in any matched CLI phrase

* fix(native): bind producer_limits.output_bytes for oversized pwndbg debugger replies through one owner

* fix(cli): name an Evidence target source failure's control by the source option that sets it

* feat(cli): render the flags of result-recorded controls in the full-output envelope (#11507)

* feat(cli): render the flags of result-recorded controls in the full-output envelope

* fix(cli): collect result controls only for a full-output envelope, in its requested format, past undefined values

* ci(guards): end a host-only command's arguments before the next CLI command

* refactor(reverse): record the output budget that stopped a one-shot command as a parameter control (#11528)

* fix(cli): read result controls only at positions the output schema declares as recorded controls

* refactor(providers): record the controls that bounded Rust compiler paths and kernel module text instead of naming CLI options (#11532)

* refactor(rust): record the control that bounded compiler-path coverage instead of naming CLI options

* refactor(native): record the controls that omitted kernel module .modinfo text instead of naming CLI options (#11534)

* fix(rust): describe compiler coverage from the retained result after report trimming

* refactor(providers): record managed representation and JavaScript semantic ceiling controls instead of naming CLI options (#11537)

* refactor(managed): record the representation budget on a resource-limit issue instead of naming its CLI option

* refactor(javascript): record the semantic retention ceilings that stopped a graph instead of naming CLI options (#11540)

* fix(cli): name a composed command's source options for the controls its result records

* fix(cli): read result controls from the delivered Evidence when a summary wraps it

* fix(rust): keep the report-byte control recorded when trimming omits every compiler path

When the complete-report allowance left room for no compiler path, rustc_commit became null and its coverage.limit was lost, so --full-output could not name the option that raises the allowance. The report now records rustc_commit_omission { reason, limit } outside the dropped record, and the summary view carries it.

* refactor(flutter): record the string-pool budgets that left a projection partial instead of naming the CLI option (#11541)

* refactor(flutter): record the string-pool budgets that left a projection partial instead of naming the CLI option

* test(flutter): assert the complete set of recorded string-pool budgets

* feat(cli): name the options of controls a failure's partial Evidence records (#11557)

meta.cli_controls was collected only from a successful result. A failure whose partial observation is Evidence of the command's own operation is now read the same way, at the positions the output schema declares as recorded controls; Evidence of another operation or a retained reference is not read. When --evidence_output publishes the partial, the delivery records it before replacing it.

* refactor(javascript): record the evaluator budgets that limited sources instead of naming the CLI option (#11564)

Per-value unknown reasons and the Electron wrapper limitation named --semantic-precision. The static examination now records the semantic_precision controls that limited a source (examination.controls), and analyze_javascript_application's semantic_graph.coverage.controls records every evaluator budget that limited any source (examination and semantic IR coverage), beside the retention ceilings. The CLI renders --semantic-precision from those records in meta.cli_controls; the reason texts name only the contract field.
2026-11-16 07:12:16 +08:00

244 lines
9.2 KiB
TypeScript

import { expect, it } from "vitest";
import { AnalysisResourceConstraintError } from "../analysisErrorCore.js";
import { projectAnalysisError } from "../analysisErrorProjection.js";
import {
JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS,
JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION,
JAVASCRIPT_PARENT_RESULT_BUDGET_REMEDIATION,
JAVASCRIPT_SEMANTIC_ITEM_MAXIMUM,
JAVASCRIPT_WORKER_HEAP_MIB_MAXIMUM,
JAVASCRIPT_WORKER_HEAP_MIB_MINIMUM,
javaScriptAnalysisResourceControlsSchema,
javaScriptWorkerHeapExhaustionRecovery,
javaScriptSemanticCeilingCoverage,
javaScriptWorkerHeapRemediation,
} from "./javascriptAnalysisResourceControls.js";
it("keeps omitted operational choices absent and validates explicit semantic budgets", () => {
expect(javaScriptAnalysisResourceControlsSchema.parse({})).toEqual({});
expect(
javaScriptAnalysisResourceControlsSchema.parse({
max_semantic_nodes: 750_000,
max_semantic_nodes_per_file: 700_000,
max_semantic_unknowns: 500_000,
max_semantic_unknowns_per_file: 400_000,
}),
).toEqual({
max_semantic_nodes: 750_000,
max_semantic_nodes_per_file: 700_000,
max_semantic_unknowns: 500_000,
max_semantic_unknowns_per_file: 400_000,
});
for (const value of [0, -1, 1.5, 1_000_001, Infinity, NaN]) {
for (const key of [
"max_semantic_nodes",
"max_semantic_nodes_per_file",
"max_semantic_unknowns",
"max_semantic_unknowns_per_file",
])
expect(
javaScriptAnalysisResourceControlsSchema.safeParse({ [key]: value })
.success,
).toBe(false);
}
});
it("names max_heap_mb, its range, and the budgets it does not raise", () => {
const heapMb = JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.heapMb;
const error = new AnalysisResourceConstraintError(
"analyze_javascript_application",
"memory",
`Owned JavaScript worker exhausted its ${String(heapMb)} MiB V8 old-space heap (max_heap_mb) while analyzing app.js during static`,
{ worker_old_space_mb: heapMb },
{ remediationAction: javaScriptWorkerHeapRemediation(heapMb) },
);
const projected = projectAnalysisError(error);
expect(projected.message).toContain(`max_heap_mb`);
expect(projected.message).toContain(`${String(heapMb)} MiB`);
expect(projected.remediation.action).toContain("max_heap_mb");
expect(projected.remediation.action).toContain(
String(JAVASCRIPT_WORKER_HEAP_MIB_MINIMUM),
);
expect(projected.remediation.action).toContain(
String(JAVASCRIPT_WORKER_HEAP_MIB_MAXIMUM),
);
expect(projected.remediation.action).not.toContain("--");
expect(projected.remediation.action).toContain("next analysis");
expect(projected.remediation.action).toContain("new worker");
expect(projected.remediation.action).toContain("parent result budget");
expect(projected.remediation.action).toContain("transfer");
expect(projected.remediation.action).not.toMatch(/available host memory/u);
const maximum = projectAnalysisError(
new AnalysisResourceConstraintError(
"analyze_javascript_application",
"memory",
"heap",
null,
{
remediationAction: javaScriptWorkerHeapRemediation(
JAVASCRIPT_WORKER_HEAP_MIB_MAXIMUM,
),
},
),
);
expect(maximum.remediation.action).toContain("Do not increase it further");
});
it("names the parent heap control for the parent result budget", () => {
expect(JAVASCRIPT_PARENT_RESULT_BUDGET_REMEDIATION).toContain(
"parent result budget",
);
expect(JAVASCRIPT_PARENT_RESULT_BUDGET_REMEDIATION).toContain("NODE_OPTIONS");
expect(JAVASCRIPT_PARENT_RESULT_BUDGET_REMEDIATION).toContain(
"--max-old-space-size=<MiB>",
);
expect(JAVASCRIPT_PARENT_RESULT_BUDGET_REMEDIATION).toContain("max_heap_mb");
expect(JAVASCRIPT_PARENT_RESULT_BUDGET_REMEDIATION).not.toContain(
"no runtime override",
);
});
it("names the parent heap control for checkpoint decode", () => {
expect(JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION).toContain(
"parent process heap",
);
expect(JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION).toContain(
"NODE_OPTIONS",
);
expect(JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION).toContain(
"--max-old-space-size=<MiB>",
);
expect(JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION).toContain(
"admit this checkpoint",
);
expect(JAVASCRIPT_PARENT_CHECKPOINT_HEAP_REMEDIATION).toContain(
"Increasing max_heap_mb does not raise it",
);
});
it("attributes V8 heap exhaustion to max_heap_mb only when that flag is enforced", () => {
const node = javaScriptWorkerHeapExhaustionRecovery({
heapFlagsEnforced: true,
heapMb: 1024,
path: "app.js",
phase: "static",
});
expect(node.reason).toContain(
"exhausted its 1024 MiB V8 old-space heap (max_heap_mb)",
);
expect(node.remediationAction).toContain("max_heap_mb");
expect(node.remediationAction).not.toContain("--");
const bun = javaScriptWorkerHeapExhaustionRecovery({
heapFlagsEnforced: false,
heapMb: 1024,
path: "app.js",
phase: "static",
});
expect(bun.reason).toContain("Bun ignores the V8 old-space flag");
expect(bun.reason).not.toContain(
"exhausted its 1024 MiB V8 old-space heap (max_heap_mb)",
);
expect(bun.remediationAction).toContain("Bun ignores V8 heap flags");
expect(bun.remediationAction).not.toContain("Increase max_heap_mb");
});
it("names the four semantic arguments on ceiling coverage", () => {
const both = { total: true, perFile: true };
const nodes = javaScriptSemanticCeilingCoverage("nodes", 50, 30, both);
const unknowns = javaScriptSemanticCeilingCoverage("unknowns", 3, 2, both);
const perFileOnly = javaScriptSemanticCeilingCoverage("nodes", 50, 30, {
total: false,
perFile: true,
});
const totalOnly = javaScriptSemanticCeilingCoverage("unknowns", 3, 2, {
total: true,
perFile: false,
});
expect(nodes.limits).toEqual(perFileOnly.limits);
expect(nodes.limits).toEqual(
expect.arrayContaining([
{ name: "semantic_graph_node_ceiling", value: 50, unit: "items" },
{ name: "semantic_graph_file_node_ceiling", value: 30, unit: "items" },
{ name: "max_semantic_nodes", value: 50, unit: "items" },
{ name: "max_semantic_nodes_per_file", value: 30, unit: "items" },
]),
);
expect(unknowns.limits).toEqual(totalOnly.limits);
expect(unknowns.limits).toEqual(
expect.arrayContaining([
{ name: "semantic_graph_unknown_ceiling", value: 3, unit: "items" },
{ name: "semantic_graph_file_unknown_ceiling", value: 2, unit: "items" },
{ name: "max_semantic_unknowns", value: 3, unit: "items" },
{ name: "max_semantic_unknowns_per_file", value: 2, unit: "items" },
]),
);
expect(nodes.limitation).toContain("1 to");
expect(nodes.limitation).toContain(String(JAVASCRIPT_SEMANTIC_ITEM_MAXIMUM));
// The producer records controls by contract field; adapters name options.
expect(nodes.limitation).not.toContain("--");
expect(nodes.limitation).toContain("coverage.controls records them");
expect(nodes.controls).toEqual([
{
control_kind: "parameter",
parameter: "max_semantic_nodes",
effective_value: 50,
unit: "count",
minimum: 1,
maximum: JAVASCRIPT_SEMANTIC_ITEM_MAXIMUM,
default_value: JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.semanticNodes,
applies_at: "next_call",
},
expect.objectContaining({
parameter: "max_semantic_nodes_per_file",
effective_value: 30,
default_value: JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.semanticNodesPerFile,
}),
]);
expect(nodes.limitation).toContain(
"Set max_semantic_nodes and max_semantic_nodes_per_file",
);
expect(nodes.limitation).toContain("Raising them does not raise");
expect(nodes.limitation).toContain("next analysis");
expect(unknowns.controls.map(({ parameter }) => parameter)).toEqual([
"max_semantic_unknowns",
"max_semantic_unknowns_per_file",
]);
expect(unknowns.limitation).toContain("max_semantic_nodes");
expect(perFileOnly.limitation).toContain("max_semantic_nodes_per_file is 30");
expect(perFileOnly.limitation).toContain(
"Set max_semantic_nodes_per_file on the next",
);
expect(perFileOnly.controls.map(({ parameter }) => parameter)).toEqual([
"max_semantic_nodes_per_file",
]);
expect(perFileOnly.limitation).toContain("coverage.controls records it");
expect(perFileOnly.limitation).toContain("Raising it does not raise");
expect(perFileOnly.limitation).not.toContain("max_semantic_nodes is 50");
expect(totalOnly.limitation).toContain("max_semantic_unknowns is 3");
expect(totalOnly.limitation).toContain(
"Set max_semantic_unknowns on the next",
);
expect(totalOnly.controls.map(({ parameter }) => parameter)).toEqual([
"max_semantic_unknowns",
]);
expect(totalOnly.limitation).not.toContain(
"max_semantic_unknowns_per_file is 2",
);
const capped = javaScriptSemanticCeilingCoverage(
"nodes",
JAVASCRIPT_SEMANTIC_ITEM_MAXIMUM,
30,
{ total: true, perFile: false },
);
expect(capped.limitation).toContain("Do not increase it further");
expect(capped.limitation).not.toContain("max_semantic_nodes_per_file is 30");
expect(() =>
javaScriptSemanticCeilingCoverage("nodes", 50, 30, {
total: false,
perFile: false,
}),
).toThrow("ceiling that stopped retention");
});