import { z } from "zod"; import type { AnalysisControlLimit, AnalysisParameterLimit, AnalysisInvocationSettingLimit, AnalysisLimit, AnalysisSettingId, AnalysisSettingLimit, } from "./analysisSettings.js"; import { DEFAULT_ASAR_MAX_HEADER_BYTES, MAXIMUM_ASAR_MAX_HEADER_BYTES, DEFAULT_ASAR_MAX_LISTING_BYTES, MAXIMUM_ASAR_MAX_LISTING_BYTES, DEFAULT_GHIDRA_VALUE_FLOW_MAX_OPERATIONS, DEFAULT_GHIDRA_VALUE_FLOW_MAX_INPUTS_PER_OPERATION, DEFAULT_GHIDRA_VALUE_FLOW_MAX_EDGES, MAXIMUM_GHIDRA_VALUE_FLOW_RECORDS, MAXIMUM_GHIDRA_DECOMPILE_PAYLOAD_MIB, DEFAULT_GHIDRA_STARTUP_TIMEOUT_MS, DEFAULT_HOPPER_STARTUP_TIMEOUT_MS, MAX_NODE_TIMER_DELAY_MS, MAX_GHIDRA_DECOMPILE_TIMEOUT_SECONDS, DEFAULT_REGEX_MATCHING_DEADLINE_MS, DEFAULT_REGEX_WORKER_HEAP_REQUEST_MIB, MINIMUM_REGEX_WORKER_HEAP_REQUEST_MIB, MAXIMUM_REGEX_WORKER_HEAP_REQUEST_MIB, DEFAULT_CAPTURE_DECODER_OLD_GENERATION_MIB, CAPTURE_DECODER_SEMI_SPACE_MIB, MAXIMUM_CAPTURE_DECODER_OLD_GENERATION_MIB, DEFAULT_CAPTURE_PACKET_JSON_EXPANSION_BYTES, MINIMUM_CAPTURE_PACKET_JSON_EXPANSION_BYTES, MAXIMUM_CAPTURE_PACKET_JSON_EXPANSION_BYTES, DEFAULT_JADX_OPERATION_TIMEOUT_MS, DEFAULT_NATIVE_MATCHING_REPRESENTATION_BYTES, MAXIMUM_NATIVE_MATCHING_REPRESENTATION_BYTES, DEFAULT_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES, MAXIMUM_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES, DEFAULT_BRIDGE_MAX_REQUEST_BYTES, MINIMUM_BRIDGE_MAX_REQUEST_BYTES, MAXIMUM_BRIDGE_MAX_REQUEST_BYTES, DEFAULT_BRIDGE_MAX_REPLY_BYTES, MINIMUM_BRIDGE_MAX_REPLY_BYTES, MAXIMUM_BRIDGE_MAX_REPLY_BYTES, DEFAULT_DEBUGGER_COMMAND_DEADLINE_MS, MINIMUM_DEBUGGER_COMMAND_DEADLINE_MS, DEFAULT_DEBUGGER_MAX_COMMAND_OUTPUT_BYTES, MINIMUM_DEBUGGER_MAX_COMMAND_OUTPUT_BYTES, MAXIMUM_DEBUGGER_MAX_COMMAND_OUTPUT_BYTES, DEFAULT_HOPPER_CALLER_SCAN_BUDGET_MS, MINIMUM_HOPPER_CALLER_SCAN_BUDGET_MS, DEFAULT_JEB_MCP_TIMEOUT_MS, DEFAULT_KUNA_FUNCTION_TIMEOUT_MS, DEFAULT_KUNA_PROJECT_TIMEOUT_MS, DEFAULT_PWNTOOLS_MAX_INPUT_BYTES, MAXIMUM_PWNTOOLS_MAX_INPUT_BYTES, DEFAULT_WINDOWS_CHANGE_TIME_DRIFT_MS, MAXIMUM_WINDOWS_CHANGE_TIME_DRIFT_MS, DEFAULT_WINDOWS_CHANGE_TIME_SETTLE_POLL_MS, MAXIMUM_WINDOWS_CHANGE_TIME_SETTLE_POLL_MS, DEFAULT_WINDOWS_CHANGE_TIME_SETTLE_MAX_POLLS, MAXIMUM_WINDOWS_CHANGE_TIME_SETTLE_MAX_POLLS, } from "./analysisResourceLimits.js"; import { JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS, JAVASCRIPT_WORKER_HEAP_MIB_MINIMUM, JAVASCRIPT_WORKER_HEAP_MIB_MAXIMUM, } from "./javascript/javascriptAnalysisResourceControls.js"; import { DEFAULT_WASM_COMMAND_TIMEOUT_MS } from "./wasm/wasmArtifact.js"; interface SettingDefinition { readonly environment?: string; readonly defaultValue: number | null; readonly minimum: number; readonly maximum: number; readonly unit: "bytes" | "ms" | "seconds" | "MiB" | "count"; readonly appliesAt: "next_operation" | "next_worker" | "next_startup"; readonly updateMode: "runtime" | "restart"; readonly provider: string; readonly relatedProviders?: readonly string[]; readonly operations: readonly string[]; readonly parameter?: string; } const timer = { minimum: 1, maximum: MAX_NODE_TIMER_DELAY_MS, unit: "ms", appliesAt: "next_operation", } as const; /** Operations that invoke Ghidra's decompiler directly or through a workflow. */ const ghidraDecompileOperations = [ "procedure_pseudo_code", "analyze_function", "inspect_native_api", "decompile_all", "trace_native_values", ] as const; // These operations can use the shared owned-file baseline on Windows, depending // on their input and provider. Other workflows may also use it; this is not a // complete reader inventory. const windowsChangeTimeOperations = [ "open_binary", "export_web_scripts", "capture_process_scenario", "compare_process_captures", "inspect_analysis_view", "compare_bundles", "import_evidence_bundle", "inspect_android_package", "search_android_classes", "inspect_android_class", "inspect_android_method", "trace_android_references", "inventory_artifact", "inspect_artifact", "extract_artifact", "inspect_binary_layout", "inspect_recorded_crash", ] as const; /** Every Hopper and Ghidra bridge operation; their replies share one line reader. */ const hopperAndGhidraBridgeOperations = [ "open_binary", "address_name", "comment", "current_address", "current_procedure", "current_document", "goto_address", "inline_comment", "list_bookmarks", "list_documents", "list_names", "list_procedures", "list_segments", "list_strings", "next_address", "prev_address", "procedure_address", "procedure_assembly", "procedure_callees", "procedure_callers", "procedure_info", "read_function_instructions", "read_bytes", "address_to_file_offset", "procedure_references", "procedure_pseudo_code", "resolve_containing_procedure", "search_procedures", "search_strings", "set_address_name", "set_addresses_names", "set_bookmark", "set_comment", "set_inline_comment", "unset_bookmark", "xrefs", "analyze_function", "binary_overview", "inspect_native_load_image", "search_bytes", "annotate_native_function", "inspect_native_data_type", "inspect_native_instruction", "resolve_native_call_targets", ] as const; /** Metadata aggregates existing owner definitions; no environment mutation or engine discovery. */ export const ANALYSIS_SETTINGS = { "ghidra.decompile_max_payload_mib": { updateMode: "runtime", environment: "REA_GHIDRA_DECOMPILE_MAX_PAYLOAD_MIB", defaultValue: null, minimum: 1, maximum: MAXIMUM_GHIDRA_DECOMPILE_PAYLOAD_MIB, unit: "MiB", appliesAt: "next_operation", provider: "ghidra", operations: ghidraDecompileOperations, }, "asar.max_listing_bytes": { updateMode: "runtime", environment: "REA_ASAR_MAX_LISTING_BYTES", defaultValue: DEFAULT_ASAR_MAX_LISTING_BYTES, minimum: 1, maximum: MAXIMUM_ASAR_MAX_LISTING_BYTES, unit: "bytes", appliesAt: "next_operation", provider: "rea-artifact-graph", relatedProviders: [ "rea-javascript-application", "rea-cdp-electron", "rea-v8-inspector", ], operations: [ "open_binary", "inspect_artifact", "inventory_artifact", "extract_artifact", "analyze_javascript_application", "inspect_electron_page", "observe_javascript_runtime", ], }, "ghidra.value_flow_max_operations": { updateMode: "runtime", environment: "REA_GHIDRA_VALUE_FLOW_MAX_OPERATIONS", defaultValue: DEFAULT_GHIDRA_VALUE_FLOW_MAX_OPERATIONS, minimum: 1, maximum: MAXIMUM_GHIDRA_VALUE_FLOW_RECORDS, unit: "count", appliesAt: "next_operation", provider: "ghidra", operations: [ "analyze_function", "inspect_native_api", "trace_native_values", ], }, "ghidra.value_flow_max_inputs_per_operation": { updateMode: "runtime", environment: "REA_GHIDRA_VALUE_FLOW_MAX_INPUTS_PER_OPERATION", defaultValue: DEFAULT_GHIDRA_VALUE_FLOW_MAX_INPUTS_PER_OPERATION, minimum: 1, maximum: MAXIMUM_GHIDRA_VALUE_FLOW_RECORDS, unit: "count", appliesAt: "next_operation", provider: "ghidra", operations: [ "analyze_function", "inspect_native_api", "trace_native_values", ], }, "ghidra.value_flow_max_edges": { updateMode: "runtime", environment: "REA_GHIDRA_VALUE_FLOW_MAX_EDGES", defaultValue: DEFAULT_GHIDRA_VALUE_FLOW_MAX_EDGES, minimum: 1, maximum: MAXIMUM_GHIDRA_VALUE_FLOW_RECORDS, unit: "count", appliesAt: "next_operation", provider: "ghidra", operations: [ "analyze_function", "inspect_native_api", "trace_native_values", ], }, "asar.max_header_bytes": { updateMode: "runtime", environment: "REA_ASAR_MAX_HEADER_BYTES", defaultValue: DEFAULT_ASAR_MAX_HEADER_BYTES, minimum: 1, maximum: MAXIMUM_ASAR_MAX_HEADER_BYTES, unit: "bytes", appliesAt: "next_operation", provider: "rea-artifact-graph", relatedProviders: [ "rea-javascript-application", "rea-cdp-electron", "rea-v8-inspector", ], operations: [ "open_binary", "inspect_artifact", "inventory_artifact", "extract_artifact", "analyze_javascript_application", "inspect_electron_page", "observe_javascript_runtime", ], }, "ghidra.startup_timeout_ms": { updateMode: "runtime", ...timer, environment: "REA_GHIDRA_STARTUP_TIMEOUT_MS", defaultValue: DEFAULT_GHIDRA_STARTUP_TIMEOUT_MS, appliesAt: "next_startup", provider: "ghidra", operations: [ "open_binary", "binary_overview", "list_documents", "list_procedures", "search_procedures", "list_strings", "search_strings", "procedure_pseudo_code", "procedure_assembly", "analyze_function", "decompile_all", "trace_native_values", ], }, "hopper.startup_timeout_ms": { updateMode: "runtime", ...timer, environment: "REA_HOPPER_STARTUP_TIMEOUT_MS", defaultValue: DEFAULT_HOPPER_STARTUP_TIMEOUT_MS, appliesAt: "next_startup", provider: "hopper", operations: [ "open_binary", "binary_overview", "list_documents", "list_procedures", "search_procedures", "list_strings", "search_strings", "procedure_pseudo_code", "procedure_assembly", "analyze_function", "decompile_all", "trace_native_values", ], }, "ghidra.decompile_timeout_seconds": { updateMode: "runtime", environment: "REA_GHIDRA_DECOMPILE_TIMEOUT_SECONDS", defaultValue: null, minimum: 1, maximum: MAX_GHIDRA_DECOMPILE_TIMEOUT_SECONDS, unit: "seconds", appliesAt: "next_operation", provider: "ghidra", operations: ghidraDecompileOperations, }, "jadx.operation_timeout_ms": { updateMode: "runtime", ...timer, environment: "REA_JADX_OPERATION_TIMEOUT_MS", defaultValue: DEFAULT_JADX_OPERATION_TIMEOUT_MS, provider: "jadx", operations: [ "inspect_android_package", "search_android_classes", "inspect_android_class", "inspect_android_method", "trace_android_references", ], }, "regex.matching_deadline_ms": { updateMode: "runtime", ...timer, environment: "REA_REGEX_MATCHING_DEADLINE_MS", defaultValue: DEFAULT_REGEX_MATCHING_DEADLINE_MS, maximum: MAX_NODE_TIMER_DELAY_MS, provider: "regex", relatedProviders: ["ida", "hopper", "kuna"], operations: ["search_strings", "search_procedures"], }, "regex.worker_heap_request_mib": { updateMode: "runtime", environment: "REA_REGEX_WORKER_HEAP_REQUEST_MIB", defaultValue: DEFAULT_REGEX_WORKER_HEAP_REQUEST_MIB, minimum: MINIMUM_REGEX_WORKER_HEAP_REQUEST_MIB, maximum: MAXIMUM_REGEX_WORKER_HEAP_REQUEST_MIB, unit: "MiB", appliesAt: "next_worker", provider: "regex", relatedProviders: ["ida", "hopper", "kuna"], operations: ["search_strings", "search_procedures"], }, "capture.decoder_old_generation_mib": { updateMode: "runtime", environment: "REA_CAPTURE_DECODER_OLD_GENERATION_MIB", defaultValue: DEFAULT_CAPTURE_DECODER_OLD_GENERATION_MIB, minimum: CAPTURE_DECODER_SEMI_SPACE_MIB, maximum: MAXIMUM_CAPTURE_DECODER_OLD_GENERATION_MIB, unit: "MiB", appliesAt: "next_worker", provider: "capture", operations: ["inspect_web_network_capture"], }, "capture.packet_json_expansion_bytes": { updateMode: "runtime", environment: "REA_CAPTURE_PACKET_JSON_EXPANSION_BYTES", defaultValue: DEFAULT_CAPTURE_PACKET_JSON_EXPANSION_BYTES, minimum: MINIMUM_CAPTURE_PACKET_JSON_EXPANSION_BYTES, maximum: MAXIMUM_CAPTURE_PACKET_JSON_EXPANSION_BYTES, unit: "bytes", appliesAt: "next_operation", provider: "tshark-capture", operations: ["inspect_packet_capture"], }, "javascript.analysis_timeout_ms": { updateMode: "runtime", ...timer, defaultValue: JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.timeoutMs, provider: "rea-javascript-application", operations: ["analyze_javascript_application"], parameter: "analysis_timeout_ms", }, "javascript.max_heap_mb": { updateMode: "runtime", defaultValue: JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.heapMb, minimum: JAVASCRIPT_WORKER_HEAP_MIB_MINIMUM, maximum: JAVASCRIPT_WORKER_HEAP_MIB_MAXIMUM, unit: "MiB", appliesAt: "next_worker", provider: "rea-javascript-application", operations: ["analyze_javascript_application"], parameter: "max_heap_mb", }, "javascript.member_mutation_alias_states": { updateMode: "runtime", environment: "REA_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES", defaultValue: DEFAULT_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES, minimum: 1, maximum: MAXIMUM_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES, unit: "count", appliesAt: "next_operation", provider: "rea-javascript-application", relatedProviders: ["web-bundle-static-analysis"], operations: ["analyze_javascript_application", "analyze_web_bundle"], }, "wasm.command_timeout_ms": { updateMode: "runtime", ...timer, defaultValue: DEFAULT_WASM_COMMAND_TIMEOUT_MS, maximum: MAX_NODE_TIMER_DELAY_MS, provider: "wabt-artifact", operations: ["inspect_wasm_artifact"], parameter: "command_timeout_ms", }, "native.matching_representation_bytes": { updateMode: "runtime", environment: "REA_NATIVE_MATCHING_REPRESENTATION_BYTES", defaultValue: DEFAULT_NATIVE_MATCHING_REPRESENTATION_BYTES, minimum: 1, maximum: MAXIMUM_NATIVE_MATCHING_REPRESENTATION_BYTES, unit: "bytes", appliesAt: "next_operation", provider: "rea-function-comparison", operations: ["match_native_functions"], }, "bridge.max_request_bytes": { updateMode: "runtime", environment: "REA_BRIDGE_MAX_REQUEST_BYTES", defaultValue: DEFAULT_BRIDGE_MAX_REQUEST_BYTES, minimum: MINIMUM_BRIDGE_MAX_REQUEST_BYTES, maximum: MAXIMUM_BRIDGE_MAX_REQUEST_BYTES, unit: "bytes", appliesAt: "next_operation", provider: "hopper", relatedProviders: ["ghidra", "cutter"], // Every Hopper bridge operation, every Ghidra bridge operation, and the // Cutter bridge calls. bridgeMaxRequestBytes.test.ts fails when a registry // gains an operation that is missing here. operations: [ ...hopperAndGhidraBridgeOperations, "list_cutter_sessions", "cutter_command", ], }, "bridge.max_reply_bytes": { updateMode: "runtime", environment: "REA_BRIDGE_MAX_REPLY_BYTES", defaultValue: DEFAULT_BRIDGE_MAX_REPLY_BYTES, minimum: MINIMUM_BRIDGE_MAX_REPLY_BYTES, maximum: MAXIMUM_BRIDGE_MAX_REPLY_BYTES, unit: "bytes", appliesAt: "next_operation", provider: "hopper", relatedProviders: ["ghidra"], operations: hopperAndGhidraBridgeOperations, }, "debugger.command_deadline_ms": { updateMode: "runtime", ...timer, environment: "REA_DEBUGGER_COMMAND_DEADLINE_MS", defaultValue: DEFAULT_DEBUGGER_COMMAND_DEADLINE_MS, minimum: MINIMUM_DEBUGGER_COMMAND_DEADLINE_MS, maximum: MAX_NODE_TIMER_DELAY_MS, provider: "gdb", relatedProviders: ["rizin"], operations: [ "start_gdb_session", "gdb_console", "close_gdb_session", "start_rizin_debug_session", "rizin_debug_command", "close_rizin_debug_session", ], }, "debugger.max_command_output_bytes": { updateMode: "runtime", environment: "REA_DEBUGGER_MAX_COMMAND_OUTPUT_BYTES", defaultValue: DEFAULT_DEBUGGER_MAX_COMMAND_OUTPUT_BYTES, minimum: MINIMUM_DEBUGGER_MAX_COMMAND_OUTPUT_BYTES, maximum: MAXIMUM_DEBUGGER_MAX_COMMAND_OUTPUT_BYTES, unit: "bytes", appliesAt: "next_operation", provider: "gdb", relatedProviders: ["rizin"], operations: ["gdb_console", "rizin_debug_command"], }, "hopper.caller_scan_budget_ms": { updateMode: "runtime", ...timer, environment: "REA_HOPPER_CALLER_SCAN_BUDGET_MS", defaultValue: DEFAULT_HOPPER_CALLER_SCAN_BUDGET_MS, minimum: MINIMUM_HOPPER_CALLER_SCAN_BUDGET_MS, provider: "hopper", operations: [ "procedure_callers", "analyze_function", "procedure_references", ], }, "jeb.mcp_timeout_ms": { updateMode: "runtime", ...timer, environment: "REA_JEB_MCP_TIMEOUT_MS", defaultValue: DEFAULT_JEB_MCP_TIMEOUT_MS, provider: "jeb", operations: [ "inspect_jeb_client", "open_jeb_project", "list_jeb_units", "decompile_jeb_item", ], }, "kuna.project_timeout_ms": { updateMode: "runtime", ...timer, environment: "REA_KUNA_PROJECT_TIMEOUT_MS", defaultValue: DEFAULT_KUNA_PROJECT_TIMEOUT_MS, provider: "kuna", operations: [ "list_strings", "search_strings", "list_procedures", "search_procedures", "xrefs", "procedure_callers", "procedure_callees", "procedure_pseudo_code", "procedure_assembly", "analyze_function", ], }, "kuna.function_timeout_ms": { updateMode: "runtime", ...timer, environment: "REA_KUNA_TIMEOUT_MS", defaultValue: DEFAULT_KUNA_FUNCTION_TIMEOUT_MS, provider: "kuna", operations: [ "procedure_pseudo_code", "procedure_assembly", "analyze_function", "list_procedures", ], }, "pwntools.max_input_bytes": { updateMode: "runtime", environment: "REA_PWNTOOLS_MAX_INPUT_BYTES", defaultValue: DEFAULT_PWNTOOLS_MAX_INPUT_BYTES, minimum: 1, maximum: MAXIMUM_PWNTOOLS_MAX_INPUT_BYTES, unit: "bytes", appliesAt: "next_operation", provider: "pwntools", operations: ["inspect_binary_layout", "inspect_recorded_crash"], parameter: "producer_limits.input_bytes", }, "windows.change_time_drift_ms": { updateMode: "runtime", environment: "REA_WINDOWS_CHANGE_TIME_DRIFT_MS", defaultValue: DEFAULT_WINDOWS_CHANGE_TIME_DRIFT_MS, minimum: 1, maximum: MAXIMUM_WINDOWS_CHANGE_TIME_DRIFT_MS, unit: "ms", appliesAt: "next_operation", provider: "rea-artifact-graph", relatedProviders: ["jadx"], operations: windowsChangeTimeOperations, }, "windows.change_time_settle_poll_ms": { updateMode: "runtime", environment: "REA_WINDOWS_CHANGE_TIME_SETTLE_POLL_MS", defaultValue: DEFAULT_WINDOWS_CHANGE_TIME_SETTLE_POLL_MS, minimum: 1, maximum: MAXIMUM_WINDOWS_CHANGE_TIME_SETTLE_POLL_MS, unit: "ms", appliesAt: "next_operation", provider: "rea-artifact-graph", relatedProviders: ["jadx"], operations: windowsChangeTimeOperations, }, "windows.change_time_settle_max_polls": { updateMode: "runtime", environment: "REA_WINDOWS_CHANGE_TIME_SETTLE_MAX_POLLS", defaultValue: DEFAULT_WINDOWS_CHANGE_TIME_SETTLE_MAX_POLLS, minimum: 1, maximum: MAXIMUM_WINDOWS_CHANGE_TIME_SETTLE_MAX_POLLS, unit: "count", appliesAt: "next_operation", provider: "rea-artifact-graph", relatedProviders: ["jadx"], operations: windowsChangeTimeOperations, }, } as const satisfies Record; export const analysisSettingIds = Object.keys( ANALYSIS_SETTINGS, ) as AnalysisSettingId[]; export const analysisSettingIdSchema = z.enum(analysisSettingIds); /** Limit facts a result carries when it records a refusal as a value. */ export const analysisSettingLimitSchema = z.strictObject({ setting: analysisSettingIdSchema, effective_value: z.number().nullable(), unit: z.enum(["bytes", "ms", "seconds", "MiB", "count"]), minimum: z.number(), maximum: z.number(), source: z.enum(["default", "setting", "runtime", "request"]).exactOptional(), applies_at: z.enum(["next_operation", "next_worker", "next_startup"]), agent_editable: z.literal(true), scope: z.literal("rea-runtime"), update_mode: z.enum(["runtime", "restart", "parameter"]), pending_restart: z.literal(false), required_value: z.number().exactOptional(), parameter: z.string().exactOptional(), }) satisfies z.ZodType; /** * A per-call request parameter that bounded a result, named by its contract * field. Adapters render their own spelling of it (a CLI flag) outside the * record. */ export const analysisParameterLimitSchema = z.strictObject({ control_kind: z.literal("parameter"), parameter: z.string(), effective_value: z.number(), unit: z.enum(["bytes", "ms", "seconds", "MiB", "count"]), minimum: z.number(), maximum: z.number(), default_value: z.number(), applies_at: z.literal("next_call"), }) satisfies z.ZodType; /** The control a result records for a refusal: a setting or a request parameter. */ export const analysisResultLimitSchema = z.union([ analysisSettingLimitSchema, analysisParameterLimitSchema, ]); /** * The schemas whose values are controls a result records itself. A result * schema that places one of these instances (directly, optional, in an array * or in a union) declares that position as REA's own control reference, so * an adapter can name its option. Values elsewhere, such as captured file * content that happens to look like a limit, are never read as controls. */ export const recordedControlSchemas = z.registry<{ readonly recordedControl: true; }>(); recordedControlSchemas.add(analysisSettingLimitSchema, { recordedControl: true, }); recordedControlSchemas.add(analysisParameterLimitSchema, { recordedControl: true, }); export const analysisSettingDefinition = ( id: AnalysisSettingId, ): SettingDefinition => ANALYSIS_SETTINGS[id]; export const analysisSettingValueSchema = (id: AnalysisSettingId) => { const definition = analysisSettingDefinition(id); const number = z .number() .int() .min(definition.minimum) .max(definition.maximum); return id === "ghidra.decompile_timeout_seconds" || id === "ghidra.decompile_max_payload_mib" ? number.nullable() : number; }; /** * Caller argument, then the accepted runtime override, then the built-in * default. The third value is that default, not a launch-environment seed. */ export const selectedAnalysisSetting = ( request: number | undefined, override: number | undefined, fallback: number, ): { readonly value: number; readonly source: "default" | "runtime" | "request"; } => { if (request !== undefined) return { value: request, source: "request" }; if (override !== undefined) return { value: override, source: "runtime" }; return { value: fallback, source: "default" }; }; /** Project one owner's accepted budget, including the explicit per-call precedence. */ export const analysisSettingLimit = ( setting: AnalysisSettingId, effective_value: number | null, source: AnalysisSettingLimit["source"], ): AnalysisSettingLimit => { const definition = analysisSettingDefinition(setting); return { setting, effective_value, ...(source === undefined ? {} : { source }), unit: definition.unit, minimum: definition.minimum, maximum: definition.maximum, applies_at: definition.appliesAt, agent_editable: true, scope: "rea-runtime", update_mode: source === "request" ? "parameter" : definition.updateMode, pending_restart: false, ...(definition.parameter === undefined ? {} : { parameter: definition.parameter }), }; }; /** Re-scope an enforced limit for a caller whose runtime ended with this invocation. */ export const invocationLimit = ( limit: AnalysisLimit, ): AnalysisInvocationSettingLimit | AnalysisControlLimit => { if (!("control_kind" in limit)) return invocationSettingLimit(limit); // A new invocation reads its environment without restarting a running REA. return limit.control_kind === "environment" ? { ...limit, applies_at: "next_invocation" } : { ...limit }; }; const invocationSettingLimit = ({ applies_at: _appliesAt, scope: _scope, ...facts }: AnalysisSettingLimit): AnalysisInvocationSettingLimit => { const environment = analysisSettingDefinition(facts.setting).environment; return { ...facts, applies_at: "next_invocation", scope: "rea-invocation", ...(environment === undefined ? {} : { environment }), }; };