Files
rea/src/application/UserConfigurationStore.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

347 lines
11 KiB
TypeScript

import { lstat, mkdir } from "node:fs/promises";
import { dirname } from "node:path";
import {
applyUserConfigurationDefaults,
parseUserConfig,
parseUserConfigText,
type PersistedSetting,
type UserConfiguration,
type UserSettings,
} from "../config/userConfig.js";
import {
analysisErrorWithCleanupDependencies,
withInheritedCleanupOwnership,
} from "../domain/analysisErrorCleanup.js";
import { ConfigurationError } from "../domain/configurationErrors.js";
import { err, ok, type Result } from "../domain/result.js";
import { OwnedStagedOutput } from "../filesystem/OwnedStagedOutput.js";
import { RegularFileCleanupFailure } from "../filesystem/OwnedRegularFile.js";
import {
NonRegularFileReadError,
RejectedRegularFileSymlink,
RegularFileChangedError,
} from "../filesystem/RegularFile.js";
import { errorMessage } from "../kernel/errorMessage.js";
import { HOST_PLATFORM } from "../platform/HostPlatform.js";
import {
bindRegularFileCleanup,
InvalidUtf8FileError,
readRegularFileTextSnapshot,
} from "../process/RegularFileRead.js";
import {
AnalysisCleanupLeaseGroup,
ArtifactReaderFailure,
ArtifactResourceScope,
writeTextFile,
} from "../resources/index.js";
import { resolveClientConfigTransactionPath } from "./ClientConfigPath.js";
import {
isClientConfigurationCleanupFailure,
retryClientConfigurationCleanup,
} from "./ClientConfigurationCleanup.js";
import { ClientConfigurationPublication } from "./ClientConfigurationPublication.js";
import { userConfigFilePath } from "./UserConfigurationLocation.js";
// Keep exact failed-close owners available for subsequent reads or edits.
const resources = new ArtifactResourceScope();
type ConfigSnapshot = Awaited<ReturnType<typeof readRegularFileTextSnapshot>>;
type FileErrorOptions = ConstructorParameters<typeof ConfigurationError>[1];
const errorCode = (cause: unknown): unknown =>
cause instanceof Error && "code" in cause ? cause.code : undefined;
const configFileError = (
path: string,
detail: string,
options?: FileErrorOptions,
): ConfigurationError =>
new ConfigurationError(
`Cannot use the REA user configuration at ${path}. ${detail}`,
{
settings: [{ setting: path, constraint: detail }],
remediationAction:
"Check the named file and its parent directory before retrying.",
...options,
},
);
const configReadFailure = (
path: string,
cause: unknown,
): ConfigurationError => {
const primary =
cause instanceof RegularFileCleanupFailure &&
cause.outcome.kind === "failed"
? cause.outcome.cause
: cause;
const detail =
primary instanceof NonRegularFileReadError ||
primary instanceof RejectedRegularFileSymlink
? "could not be read as a regular JSON file"
: primary instanceof InvalidUtf8FileError
? "must be valid UTF-8"
: primary instanceof RegularFileChangedError
? "changed while being read; retry after the edit finishes"
: "could not be read";
const cleanup =
cause instanceof RegularFileCleanupFailure
? { reason: errorMessage(cause.cleanupCause), resources: [cause.path] }
: cause instanceof ArtifactReaderFailure
? cause.cleanup
: undefined;
const failure = configFileError(path, detail, { cause, cleanup });
bindRegularFileCleanup(cause, failure);
return failure;
};
/** Read one stable snapshot, including the mode and identity used by an edit. */
const readConfigSnapshot = async (
path: string,
symlinks: "follow" | "reject",
): Promise<Result<ConfigSnapshot | undefined, ConfigurationError>> => {
try {
return ok(await readRegularFileTextSnapshot(path, { symlinks, resources }));
} catch (cause: unknown) {
return errorCode(cause) === "ENOENT"
? ok(undefined)
: err(configReadFailure(path, cause));
}
};
const parseConfigSnapshot = (
path: string,
snapshot: ConfigSnapshot | undefined,
): Result<UserSettings, ConfigurationError> => {
if (snapshot === undefined) return ok({});
const result = parseUserConfigText(snapshot.text);
if (result.ok) return result;
const detail = result.error.settings
.map(({ setting, constraint }) => `${setting}: ${constraint}`)
.join(", ");
return err(configFileError(path, detail, { cause: result.error }));
};
/** Read validated saved values independently of the environment that can override them. */
export const loadUserConfigurationDefaults = async (
environment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform = HOST_PLATFORM.platform,
): Promise<Result<UserSettings, ConfigurationError>> => {
const path = userConfigFilePath(environment, platform);
const input = await readConfigSnapshot(path, "follow");
return input.ok ? parseConfigSnapshot(path, input.value) : input;
};
/** Resolve saved settings once for a production invocation. */
export const loadUserConfiguration = async (
environment: Readonly<NodeJS.ProcessEnv>,
platform: NodeJS.Platform = HOST_PLATFORM.platform,
): Promise<Result<UserConfiguration, ConfigurationError>> => {
const defaults = await loadUserConfigurationDefaults(environment, platform);
return defaults.ok
? ok(applyUserConfigurationDefaults(environment, defaults.value))
: defaults;
};
const configWriteFailure = async (
path: string,
cause: unknown,
): Promise<ConfigurationError> => {
if (isClientConfigurationCleanupFailure(cause)) {
const retry = await retryClientConfigurationCleanup(cause, resources);
const failure = configFileError(
path,
"could not complete the configuration write",
{
cause: retry.primary,
cleanup: retry.cleanup,
},
);
if (retry.cleanupLeases.length > 0)
new AnalysisCleanupLeaseGroup(failure, retry.cleanupLeases);
return failure;
}
if (cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined) {
const failure = configFileError(
path,
"could not release resources from an earlier edit",
{
cause,
cleanup: cause.cleanup,
},
);
bindRegularFileCleanup(cause, failure);
return failure;
}
return configFileError(
path,
cause instanceof RegularFileChangedError
? "changed during the edit; retry with the current file"
: "could not write the configuration",
{ cause },
);
};
const serializeConfig = (settings: UserSettings): string =>
JSON.stringify(
Object.fromEntries(
Object.entries(settings).sort(([a], [b]) => a.localeCompare(b)),
),
null,
2,
) + "\n";
const preserveOriginal = async (
path: string,
text: string,
): Promise<Result<void, ConfigurationError>> => {
const backup = `${path}.rea.backup`;
try {
await lstat(backup);
return ok(undefined);
} catch (cause: unknown) {
if (errorCode(cause) !== "ENOENT") throw cause;
}
const write = await writeTextFile(text, backup, false, resources);
if (write.ok || write.error.reason === "exists") return ok(undefined);
return err(
withInheritedCleanupOwnership(
configFileError(backup, "could not preserve the original configuration", {
cause: write.error,
cleanup: write.error.cleanup,
}),
write.error,
),
);
};
const publishEdit = async (
path: string,
text: string,
input: ConfigSnapshot | undefined,
): Promise<Result<void, ConfigurationError>> => {
const transactionPath = await resolveClientConfigTransactionPath(path);
if (transactionPath === undefined)
return err(configFileError(path, "could not resolve the edit destination"));
if (input !== undefined) {
const backup = await preserveOriginal(transactionPath, input.text);
if (!backup.ok) return backup;
}
const publication = await ClientConfigurationPublication.write(
transactionPath,
text,
input?.text,
{ requestedPath: path, state: input?.state },
);
await publication.verify(async () => {
const current = await readRegularFileTextSnapshot(publication.path, {
resources,
symlinks: "reject",
});
await publication.assertCurrent(current.text);
});
return ok(undefined);
};
interface UserConfigEdit {
readonly settings: UserSettings;
readonly removed: readonly PersistedSetting[];
}
const reviseConfig = async (
path: string,
settings: UserSettings,
remove: readonly PersistedSetting[],
): Promise<Result<UserConfigEdit, ConfigurationError>> => {
const input = await readConfigSnapshot(path, "reject");
if (!input.ok) return input;
const before = parseConfigSnapshot(path, input.value);
if (!before.ok) return before;
const next = { ...before.value, ...settings };
const removed = [...new Set(remove)].filter((key) =>
Object.hasOwn(next, key),
);
for (const key of removed) delete next[key];
const text = serializeConfig(next);
if (serializeConfig(before.value) !== text) {
const publication = await publishEdit(path, text, input.value);
if (!publication.ok) return publication;
}
return ok({ settings: next, removed });
};
/** Apply a complete edit under one cross-process lock. */
export const updateUserConfig = async (
environment: Readonly<NodeJS.ProcessEnv>,
settings: UserSettings,
remove: readonly PersistedSetting[],
platform: NodeJS.Platform = HOST_PLATFORM.platform,
): Promise<UserConfigEdit> => {
const path = userConfigFilePath(environment, platform);
const checked = parseUserConfig(settings);
if (!checked.ok) throw checked.error;
if (remove.some((name) => Object.hasOwn(settings, name)))
throw configFileError(
path,
"a setting cannot be set and unset in the same edit",
);
if (resources.hasPending) {
try {
await resources.retryCleanup();
} catch (cause: unknown) {
throw await configWriteFailure(path, cause);
}
}
const hasNewSettings = Object.keys(checked.value).length > 0;
if (hasNewSettings)
await mkdir(dirname(path), { recursive: true }).catch((cause: unknown) => {
throw configFileError(
path,
"could not create the configuration directory",
{ cause },
);
});
const lock = `${path}.lock`;
try {
await mkdir(lock);
} catch (cause: unknown) {
if (errorCode(cause) === "ENOENT" && !hasNewSettings)
return { settings: {}, removed: [] };
if (errorCode(cause) === "EEXIST")
throw configFileError(
path,
`another edit holds ${lock}. Retry after it finishes. If a previous process was interrupted, remove the empty lock directory after confirming no edit is active`,
{ cause },
);
throw configFileError(path, "could not acquire the configuration lock", {
cause,
});
}
const lockOwner = await OwnedStagedOutput.create(lock);
const edit = await reviseConfig(path, checked.value, remove).catch(
async (cause: unknown) => err(await configWriteFailure(path, cause)),
);
const release = await resources.release({
kind: "staged-output",
resource: lock,
output: lockOwner,
});
if (release.kind === "failed") {
const failure = configFileError(
lock,
"could not release the configuration lock",
{
cause: release.cause,
cleanup: { reason: errorMessage(release.cause), resources: [lock] },
},
);
release.lease.bindCleanupError(failure);
throw edit.ok
? failure
: analysisErrorWithCleanupDependencies(edit.error, failure, "config");
}
if (!edit.ok) throw edit.error;
return edit.value;
};