2026-10-11 07:22:50 +08:00
import { z } from "zod" ;
import type { JsonValue } from "../jsonValue.js" ;
import type { ApplicationCoverage } from "./javascriptApplicationEvidenceSchemas.js" ;
/** Shared operational defaults for the isolated source-analysis boundary. */
export const JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS = {
heapMb : 1024 ,
timeoutMs : 300_000 ,
2026-10-12 09:28:59 +08:00
semanticNodes : 100_000 ,
semanticNodesPerFile : 20_000 ,
2026-10-11 07:22:50 +08:00
} as const ;
2026-10-12 09:28:59 +08:00
/** Maximum caller-selected node capacity; heap and transfer admission still apply. */
export const JAVASCRIPT_SEMANTIC_NODE_MAXIMUM = 1 _000_000 ;
2026-10-11 07:22:50 +08:00
/** Project observed execution limits without inventing unmeasured values. */
export const javaScriptAnalysisCoverageLimits = (
limits : Readonly < Record < string , JsonValue >> | null ,
) : ApplicationCoverage [ "limits" ] => {
const result : ApplicationCoverage [ "limits" ] = [];
for ( const key of [
"worker_heap_limit_bytes" ,
2026-10-11 07:20:00 -04:00
"worker_process_memory_limit_bytes" ,
2026-10-11 07:22:50 +08:00
"worker_timeout_ms" ,
"parent_result_budget_bytes" ,
2026-10-11 11:43:31 -07:00
"parent_available_result_budget_bytes" ,
2026-10-11 07:22:50 +08:00
"semantic_result_budget_bytes" ,
"transfer_record_bytes" ,
"transfer_file_bytes" ,
]) {
const value = limits ? .[ key ];
if ( typeof value !== "number" || ! Number . isSafeInteger ( value ) || value < 0 )
continue ;
result . push ({
name : `javascript_analysis_ ${ key } ` ,
value ,
unit : key.endsWith ( "_ms" ) ? "milliseconds" : "bytes" ,
});
}
return result ;
};
/** Optional operational choices; omission keeps them out of Evidence parameters. */
export const javaScriptAnalysisResourceControlsSchema = z . strictObject ({
2026-10-12 09:28:59 +08:00
max_semantic_nodes : z
. number ()
. int ()
. min ( 1 )
. max ( JAVASCRIPT_SEMANTIC_NODE_MAXIMUM )
. optional ()
. describe (
"Maximum retained semantic nodes across the application; defaults to 100000. Worker heap and parent result admission remain enforced." ,
),
max_semantic_nodes_per_file : z
. number ()
. int ()
. min ( 1 )
. max ( JAVASCRIPT_SEMANTIC_NODE_MAXIMUM )
. optional ()
. describe (
"Maximum retained semantic nodes per source file; defaults to 20000, additionally bounded by max_semantic_nodes and memory/transfer limits." ,
),
2026-10-11 07:22:50 +08:00
max_heap_mb : z
. number ()
. int ()
. min ( 128 )
. max ( 16 _384 )
. optional ()
. describe (
2026-10-11 07:20:00 -04:00
"Maximum V8 old-space heap in MiB for the isolated JavaScript source analyzer; defaults to 1024. This does not change the CLI/MCP process heap. When REA runs on Bun, which ignores V8 heap flags, Windows instead limits the analyzer's committed memory to this budget plus a 512 MiB runtime reserve; other Bun hosts enforce no limit." ,
2026-10-11 07:22:50 +08:00
),
analysis_timeout_ms : z
. number ()
. int ()
. min ( 1 )
. max ( 2 _147_483_647 )
. optional ()
. describe (
"Maximum startup, analysis and transfer time per JavaScript source, in milliseconds; defaults to 300000. On timeout, return collected partial Evidence and stop the analysis." ,
),
});