Files
reverse-engineering-anything/src/domain/javascript/javascriptSemanticResourceLimits.ts
T
morluto cb4e5cfbff feat(analysis): make resource budgets recoverable across CLI and MCP (#9706)
* refactor(analysis): centralize resource budgets and recovery facts

* feat(analysis): expose ASAR and Ghidra runtime admission controls

* feat(artifacts): expose reader admission and preserve limited recognition

* feat(javascript): expose recoverable semantic precision budgets

* feat(web): add recoverable source-map budgets and compact retained views

* feat(web): make historical capture decoding budgets recoverable

* fix(desktop): enforce selected view policies across capture and export

* feat(runtime): make Frida and LLDB retention budgets selectable

* feat(native): expose diagnostic budgets and preserve exhaustion causes

* feat(managed): expose recoverable snapshot and representation budgets

* feat(metadata): make Go and Rust inspection budgets recoverable

* feat(wasm): expose representation and producer admissions

* feat(evm): expose per-call worker resource budgets

* feat(android): make resource and string-pool omissions recoverable

* feat(producers): expose command and recorded-capture budgets

* docs(analysis): record recovery contracts and integrated validation

* fix(docs): link calibration oracle from published verification pages

* fix(docs): restore desktop task anchors and receipt links

* test(analysis): align fixtures with selectable budget contracts

* fix(analysis): remove source-location projector dependency cycle

* test(analysis): refresh budget recovery and verifier regressions

* test(analysis): preserve format and publication recovery expectations

* test(windows): align conformance with owned runtime admission

* fix(desktop): preserve selected alias kinds in exported state

* test(analysis): align provider fixtures with recoverable budgets

* test(desktop): validate emitted alias metadata structurally

* fix(ghidra): classify bounded Windows endpoint refusals

* test(cli): assert selectable artifact admission recovery

* fix(cli): preserve capture provenance in environment builds
2026-11-10 05:11:20 -05:00

116 lines
5.1 KiB
TypeScript

import type {
JavaScriptSemanticResourceLimit,
JavaScriptSemanticValue,
} from "./javascriptSemanticValueTypes.js";
import {
JAVASCRIPT_SEMANTIC_PRECISION_DEFAULTS,
type JavaScriptSemanticPrecision,
} from "./javascriptSemanticPrecision.js";
const resourceControls = {
"primitive-candidates": ["primitive_candidates", "items", "alternatives"],
"primitive-bytes": ["primitive_bytes", "bytes", "estimated JSON bytes"],
"expression-depth": ["expression_depth", "depth", "nested expressions"],
"getter-expansion": ["getter_expansions", "items", "scheduled states"],
"alias-selections": [
"alias_selections",
"items",
"additional selector states",
],
} as const;
/** One owner maps exhaustion classes to controls and effective coverage facts. */
export const semanticResourceLimitFacts = (
resourceLimit: JavaScriptSemanticResourceLimit,
precision: JavaScriptSemanticPrecision = JAVASCRIPT_SEMANTIC_PRECISION_DEFAULTS,
) => {
const [control, unit, displayUnit] = resourceControls[resourceLimit];
return {
control,
unit,
displayUnit,
value: precision[control],
defaultValue: JAVASCRIPT_SEMANTIC_PRECISION_DEFAULTS[control],
};
};
/** Exact caller control and effective value at the evaluator boundary. */
export const semanticResourceLimitReason = (
resourceLimit: JavaScriptSemanticResourceLimit,
precision: JavaScriptSemanticPrecision = JAVASCRIPT_SEMANTIC_PRECISION_DEFAULTS,
): string => {
const { control, displayUnit, value, defaultValue } =
semanticResourceLimitFacts(resourceLimit, precision);
const prefixes = {
"primitive-candidates": `Primitive candidate budget exceeded (maximum ${String(precision.primitive_candidates)} alternatives).`,
"primitive-bytes": `Primitive string-byte budget exceeded (maximum ${String(precision.primitive_bytes)} estimated JSON bytes).`,
"expression-depth": `Semantic expression depth budget exceeded (maximum ${String(precision.expression_depth)} nested expressions).`,
"getter-expansion": `Getter expansion was conservatively bounded (maximum ${String(precision.getter_expansions)} scheduled states or a growing receiver cycle).`,
"alias-selections": `Alias selection expansion was conservatively bounded (maximum ${String(precision.alias_selections)} additional selector states); selected paths remain unknown.`,
};
return `${prefixes[resourceLimit]} Semantic ${resourceLimit} budget reached: semantic_precision.${control}=${String(value)} ${displayUnit} (default ${String(defaultValue)}). Increase semantic_precision.${control} on the next analysis call (analyze_javascript_application or analyze_web_bundle), or pass --semantic-precision JSON on the CLI; no REA restart is required. Worker memory, transfer and graph-retention limits remain independent. Cyclic or unsupported evaluations remain unknown regardless of budget.`;
};
/** Check UTF-16 length against worst-case JSON escaping without flattening. */
const jsonStringBytesFromLength = (codeUnits: number): number =>
codeUnits * 6 + 2;
/** Bound string payload before JSON key serialization or candidate creation. */
export const exceedsSemanticPrimitiveStringByteBudget = (
strings: Iterable<string>,
precision: JavaScriptSemanticPrecision = JAVASCRIPT_SEMANTIC_PRECISION_DEFAULTS,
): boolean => {
let total = 0;
for (const value of strings) {
total += jsonStringBytesFromLength(value.length);
if (total > precision.primitive_bytes) return true;
}
return false;
};
/** Bound all outputs of a primitive addition before concatenating any. */
export const exceedsSemanticPrimitiveAdditionByteBudget = (
left: readonly (string | number | boolean | null)[],
right: readonly (string | number | boolean | null)[],
precision: JavaScriptSemanticPrecision = JAVASCRIPT_SEMANTIC_PRECISION_DEFAULTS,
): boolean => {
let total = 0;
for (const leftValue of left) {
for (const rightValue of right) {
if (typeof leftValue !== "string" && typeof rightValue !== "string")
continue;
const length = String(leftValue).length + String(rightValue).length;
total += jsonStringBytesFromLength(length);
if (total > precision.primitive_bytes) return true;
}
}
return false;
};
/** Bound template interpolation outputs before creating concatenated strings. */
export const exceedsSemanticTemplateByteBudget = (
prefixes: readonly string[],
suffixes: readonly string[],
precision: JavaScriptSemanticPrecision = JAVASCRIPT_SEMANTIC_PRECISION_DEFAULTS,
): boolean => {
let total = 0;
for (const prefix of prefixes) {
for (const suffix of suffixes) {
total += jsonStringBytesFromLength(prefix.length + suffix.length);
if (total > precision.primitive_bytes) return true;
}
}
return false;
};
/** Make an explicit unknown value tagged with its effective semantic bound. */
export const semanticResourceLimitUnknown = (
resourceLimit: JavaScriptSemanticResourceLimit,
precision: JavaScriptSemanticPrecision = JAVASCRIPT_SEMANTIC_PRECISION_DEFAULTS,
): JavaScriptSemanticValue => ({
status: "unknown",
reason: semanticResourceLimitReason(resourceLimit, precision),
resourceLimit,
});