Files
reverse-engineering-anything/src/application/SetupRegistrationEnvironment.ts
T
8d6552e928 feat(config): persist provider settings in a user config file (#3023)
* feat(config): persist provider settings in a user config file

Providers currently rediscover install paths from scratch in every
process, so a path set once (for example GHIDRA_INSTALL_DIR) silently
reverts on the next run. Introduce ~/.config/rea/rea.json as the
user-owned persistence layer and merge it into the environment
snapshot before configuration parsing, keeping env > config file >
auto-detect precedence on both the CLI and MCP entry points.

Add a rea config command (list/get/set/unset) as the write surface,
reporting the effective value and where it came from.

* fix(config): address review findings on persistence safety and CLI surface

- stage config writes beside the file and rename into place so an
  interrupted write cannot leave partial JSON (greptile #4, codex #11)
- convert unreadable config files into configuration errors on the CLI
  seam instead of escaping raw filesystem errors (greptile #5, codex #15)
- keep injected runtimes without a config root isolated from the host
  account's rea.json (codex #18)
- surface the config file error on stderr when doctor falls back to the
  raw environment (greptile #3, codex #16)
- route the config command through logCliCommand and return documents so
  --json stays parseable and failures exit non-zero (greptile #2,
  codex #10)
- reject empty --set values, unknown --set/--unset/--get names, and --get
  combined with mutations (greptile #1/#7, codex #9/#12/#13/#17)
- cover the new behavior in the boundary tests and stop leaking one
  temporary home per run (greptile #8)

* fix(config): validate and back up persisted provider settings

* fix(config): keep saved provider defaults out of client registrations

* fix(config): preserve provider discovery and budgets for saved settings

* fix(config): check only settings needed by the IDA deadline lookup

* fix(config): share effective provider settings and verify config file reads

* fix(doctor): share selected settings with Hopper and ILSpy probes

* fix(config): resolve the destination of first-time saves

* fix(doctor): preserve unresolved provider location inputs

* fix(doctor): honor inherited IDA deadline overrides

* fix(config): show empty environment overrides

* fix(setup): preserve client-selected saved configuration roots

---------

Co-authored-by: Jackzigen <zigenhan@gmail.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-21 22:02:35 +00:00

157 lines
5.2 KiB
TypeScript

import type { DoctorProviderInspection } from "../ports/DoctorProviderInspection.js";
import { compareUnicodeCodePoints } from "../domain/unicodeCodePointOrder.js";
const SELECTED_REGISTRATION_SETTINGS = [
"GHIDRA_INSTALL_DIR",
"GHIDRA_HEADLESS_MAXMEM",
"GHIDRA_MAXMEM",
"JAVA_HOME",
"HOPPER_LAUNCHER_PATH",
"HOPPER_LOADER_ARGS_JSON",
"REA_ILSPY_CMD_PATH",
"REA_ADB_PATH",
"REA_APKTOOL_COMMAND",
"REA_TSHARK_COMMAND",
"REA_CAPINFOS_COMMAND",
"REA_TSHARK_PRLIMIT_COMMAND",
"REA_OBJDUMP_COMMAND",
"REA_GDB_COMMAND",
"REA_RIZIN_COMMAND",
"REA_CUTTER_BRIDGE_DIR",
"REA_WABT_BIN_DIRECTORY",
"REA_WAKARU_COMMAND",
"REA_JAVASCRIPT_PRLIMIT_COMMAND",
"REA_BINWALK_COMMAND",
"REA_UNBLOB_COMMAND",
"REA_FIRMWARE_PRLIMIT_COMMAND",
"REA_EVM_PRLIMIT_COMMAND",
"REA_SIGROK_COMMAND",
"REA_SIGROK_PRLIMIT_COMMAND",
"REA_MITMDUMP_COMMAND",
"REA_BROWSER_EXECUTABLE",
"REA_BROWSER_NO_SANDBOX",
"REA_PWNTOOLS_PYTHON",
"REA_PWNTOOLS_MAX_INPUT_BYTES",
"REA_PWNDBG_GDB",
"REA_PWNDBG_GDBINIT",
"REA_PWNDBG_VENV_PATH",
"REA_DARWIN_SWIFTC_PATH",
"REA_DARWIN_MODULE_CACHE_PATH",
"SDKROOT",
"REA_SWIFTC_COMPILE_TIMEOUT_MS",
"REA_LOG_LEVEL",
"REA_REFERENCE_SECRET_PATTERNS_JSON",
"REA_IDA_MCP_CONFIG",
"REA_JADX_MCP_JAR",
"REA_JADX_HEAP_MIB",
"REA_JADX_ACTIVE_PROCESSOR_COUNT",
"REA_JADX_OPERATION_TIMEOUT_MS",
"REA_KUNA_COMMAND",
"REA_KUNA_CACHE_DIR",
"REA_KUNA_CACHE_MAX_BYTES",
"REA_KUNA_PROJECT_TIMEOUT_MS",
"REA_KUNA_TIMEOUT_MS",
"REA_KUNA_JOBS",
"REA_ANALYSIS_PROVIDER",
"REA_ASAR_MAX_HEADER_BYTES",
"REA_ASAR_MAX_LISTING_BYTES",
"REA_GHIDRA_VALUE_FLOW_MAX_OPERATIONS",
"REA_GHIDRA_VALUE_FLOW_MAX_INPUTS_PER_OPERATION",
"REA_GHIDRA_VALUE_FLOW_MAX_EDGES",
"REA_GHIDRA_DECOMPILE_MAX_PAYLOAD_MIB",
"REA_BRIDGE_MAX_REQUEST_BYTES",
"REA_BRIDGE_MAX_REPLY_BYTES",
"REA_CAPTURE_DECODER_OLD_GENERATION_MIB",
"REA_CAPTURE_PACKET_JSON_EXPANSION_BYTES",
"REA_GHIDRA_DECOMPILE_TIMEOUT_SECONDS",
"REA_GHIDRA_STARTUP_TIMEOUT_MS",
"REA_GHIDRA_LANGUAGE_ID",
"REA_GHIDRA_COMPILER_SPEC_ID",
"REA_GHIDRA_DISABLED_ANALYZERS",
"REA_GHIDRA_SEED_FILE",
"REA_GHIDRA_PROJECT_PATH",
"REA_GHIDRA_CHECKPOINT_DIR",
"REA_GHIDRA_PROGRAM_NAME",
"REA_GHIDRA_NATIVEAOT_JAR",
"REA_HOPPER_CALLER_SCAN_BUDGET_MS",
"REA_HOPPER_STARTUP_TIMEOUT_MS",
"REA_JEB_MCP_URL",
"REA_JEB_MCP_TIMEOUT_MS",
"REA_JAVASCRIPT_MEMBER_MUTATION_ALIAS_STATES",
"REA_MCP_INPUT_SCHEMA_PROFILE",
"REA_MCP_TOOL_FAMILIES",
"REA_MCP_OUTPUT_SCHEMA_PROFILE",
"REA_MCP_TOOL_CATALOG_PROFILE",
"REA_MCP_MAX_RESPONSE_BYTES",
"REA_MCP_MAX_INPUT_ELEMENTS",
"REA_NATIVE_MATCHING_REPRESENTATION_BYTES",
"REA_REGEX_MATCHING_DEADLINE_MS",
"REA_REGEX_WORKER_HEAP_REQUEST_MIB",
"REA_WINDOWS_CHANGE_TIME_DRIFT_MS",
"REA_WINDOWS_CHANGE_TIME_SETTLE_MAX_POLLS",
"REA_WINDOWS_CHANGE_TIME_SETTLE_POLL_MS",
"REA_MCP_INITIALIZE_BUDGET_MS",
"REA_MCP_TOOLS_LIST_BUDGET_MS",
"REA_MCP_FIRST_CATALOG_BUDGET_MS",
"REA_MCP_DOCTOR_DEADLINE_MS",
] as const;
/**
* Settings setup can add to a registration from provider discovery when the
* caller did not select them: Hopper's launcher (doctor's Hopper search) and
* Ghidra's installation and Java (Ghidra's inspection). Other providers'
* registration settings echo explicit selections. Update maintenance does not
* run that discovery, so it cannot tell what setup would write for these.
*/
export const DISCOVERED_REGISTRATION_SETTINGS = [
"HOPPER_LAUNCHER_PATH",
"GHIDRA_INSTALL_DIR",
"JAVA_HOME",
] as const;
/** Explicit selections override discovered defaults even when a provider is unavailable.
* This is a registration policy, not a prefix match against the ambient environment.
*/
export const explicitSetupRegistrationEnvironment = (
environment: Readonly<Record<string, string | undefined>>,
): Readonly<Record<string, string>> =>
Object.fromEntries(
SELECTED_REGISTRATION_SETTINGS.flatMap((key) => {
const value = environment[key];
return value === undefined ? [] : [[key, value]];
}),
);
/** Saved values stay in the settings file and must never be copied to a client. */
export const copyableProviderRegistrationSettings = ({
registrationEnvironment,
providerSettings,
}: DoctorProviderInspection): readonly [string, string][] =>
Object.entries(registrationEnvironment).filter(
([key]) => providerSettings?.[key]?.source !== "config",
);
/** Discovered provider defaults; explicit selections are applied separately. */
export const providerRegistrationEnvironment = (
inspections: readonly DoctorProviderInspection[],
): Readonly<Record<string, string>> =>
Object.fromEntries(
inspections
.flatMap(copyableProviderRegistrationSettings)
.sort(([left], [right]) => compareUnicodeCodePoints(left, right)),
);
/** Spell selected settings as setup records them: an explicit selection wins. */
export const registrationSpelledSettings = (
settings: Readonly<Record<string, string>>,
environment: Readonly<Record<string, string | undefined>>,
): Readonly<Record<string, string>> => {
const explicit = explicitSetupRegistrationEnvironment(environment);
return Object.fromEntries(
Object.entries(settings).map(([key, value]) => [
key,
explicit[key] ?? value,
]),
);
};