Files
reverse-engineering-anything/src/desktop/BuildComputerEnvironment.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

128 lines
4.6 KiB
TypeScript

import { stat } from "node:fs/promises";
import { isAbsolute } from "node:path";
import { reaCacheDirectory } from "../filesystem/cacheDirectory.js";
import { appendCallerPath } from "../filesystem/callerPath.js";
import { PRODUCT_IDENTITY } from "../identity.js";
import { HOST_PLATFORM } from "../platform/HostPlatform.js";
import { windowsPathCandidates } from "../process/executableSearch.js";
import { BUILD_COMPUTER_SETTINGS } from "../domain/desktop/buildComputerSettings.js";
import { resolveProgram } from "./ProgramLookup.js";
/** One of the build computer's settings. */
export type BuildComputerSetting = (typeof BUILD_COMPUTER_SETTINGS)[number];
/** The REA-owned folder `rea setup --desktop-build` packs runtime archives into. */
export const buildComputerArchiveDirectory = (
environment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform,
): string => reaCacheDirectory(environment, platform, "build-computer");
/** The runtime archive packed for this REA version, named by that version. */
export const defaultBuildComputerArchive = (
environment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform,
directory = buildComputerArchiveDirectory(environment, platform),
): string =>
appendCallerPath(
directory,
`rea-${PRODUCT_IDENTITY.packageVersion}.tar.xz`,
platform,
);
/** The agentenv-framework version `agent-env --version` reports, as `agent-env <version>`. */
export const agentEnvReportedVersion = (stdout: string): string | undefined =>
/^agent-env (\S+)\s*$/u.exec(stdout.trim())?.[1];
/**
* An absolute program on this host's PATH, never one a relative PATH entry
* found. Windows searches PATH and PATHEXT as its launcher does; other hosts
* search POSIX PATH.
*/
export const programOnPath = async (
program: string,
environment: Readonly<NodeJS.ProcessEnv>,
): Promise<string | undefined> => {
if (HOST_PLATFORM.platform === "win32") {
const candidates = windowsPathCandidates(program, environment);
if (!candidates.ok) return undefined;
for (const candidate of candidates.value)
if (!candidate.relativeEntry && (await isFile(candidate.path)))
return candidate.path;
return undefined;
}
const found = await resolveProgram(program, environment, process.cwd());
return found === undefined || found.fromRelativeEntry
? undefined
: found.path;
};
/** Resolve a recorded command using the same empty-value fallback as runtime. */
export const resolveBuildComputerProgram = (
value: string | undefined,
program: string,
environment: Readonly<NodeJS.ProcessEnv>,
): Promise<string | undefined> =>
value === undefined || value === ""
? programOnPath(program, environment)
: Promise.resolve(value);
const isFile = (path: string): Promise<boolean> =>
stat(path).then(
(entry) => entry.isFile(),
() => false,
);
/** Where unset build settings are looked for; a test points both at its own folders. */
export interface BuildComputerDefaults {
/** The folder holding the archive packed for this REA version. */
readonly archiveDirectory?: string;
/** The PATH searched for `agent-env` and `docker`. */
readonly searchPath?: string;
}
/**
* Fill each unset build setting with its default: `agent-env` and `docker`
* on PATH, and the archive packed for this REA version when it exists.
*/
export const resolveBuildComputerEnvironment = async (
environment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform,
sources: BuildComputerDefaults = {},
): Promise<Readonly<NodeJS.ProcessEnv>> => {
const defaults: Record<string, string> = {};
const unset = (name: BuildComputerSetting) =>
environment[name] === undefined || environment[name] === "";
const search = { PATH: sources.searchPath ?? environment.PATH };
const agentEnv = unset("REA_AGENT_ENV_COMMAND")
? await resolveBuildComputerProgram(
environment.REA_AGENT_ENV_COMMAND,
"agent-env",
search,
)
: undefined;
if (agentEnv !== undefined) defaults.REA_AGENT_ENV_COMMAND = agentEnv;
const docker = unset("REA_DOCKER_COMMAND")
? await resolveBuildComputerProgram(
environment.REA_DOCKER_COMMAND,
"docker",
search,
)
: undefined;
if (docker !== undefined) defaults.REA_DOCKER_COMMAND = docker;
const archive = defaultBuildComputerArchive(
environment,
platform,
sources.archiveDirectory,
);
if (
unset("REA_BUILD_COMPUTER_REA_ARCHIVE") &&
isAbsolute(archive) &&
(await isFile(archive))
)
defaults.REA_BUILD_COMPUTER_REA_ARCHIVE = archive;
return Object.keys(defaults).length === 0
? environment
: { ...environment, ...defaults };
};