Files
reverse-engineering-anything/src/application/UserConfigurationLocation.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

120 lines
3.6 KiB
TypeScript

import {
homeDirectoryFromEnvironment,
selectedHomeDirectorySettings,
} from "../config/homeDirectory.js";
import { USER_CONFIG_FILE_NAME } from "../config/userConfig.js";
import { mergeEnvironmentOverrides } from "../process/snapshotEnvironment.js";
import {
appendCallerPath,
isQualifiedCallerPath,
} from "../filesystem/callerPath.js";
const defaultConfigHome = (
environment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform,
): string =>
appendCallerPath(
homeDirectoryFromEnvironment(environment, platform),
".config",
platform,
);
const selectedConfigHome = (
environment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform,
): string | undefined => {
const xdg = environment.XDG_CONFIG_HOME;
return xdg !== undefined && isQualifiedCallerPath(xdg, platform)
? xdg
: undefined;
};
const configFileAt = (root: string, platform: NodeJS.Platform): string =>
appendCallerPath(
appendCallerPath(root, "rea", platform),
USER_CONFIG_FILE_NAME,
platform,
);
/** Retain the caller's native traversal to the selected config file. */
export const userConfigFilePath = (
environment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform,
): string =>
configFileAt(
selectedConfigHome(environment, platform) ??
defaultConfigHome(environment, platform),
platform,
);
/** Settings needed to select the saved file with the current literal values. */
export const userConfigLocationSettings = (
environment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform,
): ReadonlySet<string> => {
return new Set([
"XDG_CONFIG_HOME",
...(selectedConfigHome(environment, platform) === undefined
? selectedHomeDirectorySettings(platform, environment)
: []),
]);
};
/** A session-independent locator for clients that rely on saved defaults. */
export const userConfigRegistrationFallback = (
environment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform,
): Readonly<Record<string, string>> => {
const selected = selectedConfigHome(environment, platform);
return selected === undefined ||
configFileAt(selected, platform) ===
configFileAt(defaultConfigHome(environment, platform), platform)
? {}
: { XDG_CONFIG_HOME: selected };
};
/** The locator is a fallback. All other proposed settings are overrides. */
export const registrationEnvironmentProposal = (
proposed: Readonly<Record<string, string>>,
): {
readonly configHomeFallback?: string;
readonly overrides: Readonly<Record<string, string>>;
} => {
if (!Object.hasOwn(proposed, "XDG_CONFIG_HOME"))
return { overrides: proposed };
const { XDG_CONFIG_HOME: configHomeFallback, ...overrides } = proposed;
return configHomeFallback === undefined
? { overrides }
: { configHomeFallback, overrides };
};
export const mergeRegistrationEnvironment = <Value>(
existing: Readonly<Record<string, Value>>,
proposed: Readonly<Record<string, string>>,
platform: NodeJS.Platform,
): {
readonly values: Readonly<Record<string, Value | string>>;
readonly configHomeFallbackApplied?: true;
} => {
const { configHomeFallback, overrides } =
registrationEnvironmentProposal(proposed);
const values = mergeEnvironmentOverrides<Value | string>(
existing,
overrides,
platform,
);
if (
configHomeFallback === undefined ||
Object.keys(values).some(
(name) =>
(platform === "win32" ? name.toUpperCase() : name) ===
"XDG_CONFIG_HOME",
)
)
return { values };
return {
values: { ...values, XDG_CONFIG_HOME: configHomeFallback },
configHomeFallbackApplied: true,
};
};