Every configuration failure projected the same message, "REA configuration is invalid. Run `rea doctor` and fix the reported setting.", with no details. The environment parser discarded the issue that names the variable (for example `REA_GHIDRA_NATIVEAOT_JAR must be absolute`), and doctor can report healthy for that environment, so neither the CLI, the MCP startup message, nor doctor told callers which setting to correct. ConfigurationError now carries the rejected settings and their constraints (never their values). The projection names them in its message, lists them in `details.settings`, and asks the caller to correct those settings where REA is started. Errors without a named setting keep the previous message. Fixes #1184 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
import { z } from "zod";
|
|
import { isAbsolute } from "node:path";
|
|
|
|
import { ConfigurationError } from "../domain/configurationErrors.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
import { analysisProviderSelectorSchema } from "../contracts/providerSelection.js";
|
|
import { ghidraStartupTimeoutSchema } from "./ghidraStartupTimeout.js";
|
|
import { mcpResponseBudgetSchema } from "./mcpResponseBudget.js";
|
|
|
|
const environmentSchema = z.object({
|
|
REA_ANALYSIS_PROVIDER: analysisProviderSelectorSchema.default("auto"),
|
|
REA_MCP_MAX_RESPONSE_BYTES: mcpResponseBudgetSchema.optional(),
|
|
REA_IDA_MCP_CONFIG: z
|
|
.string()
|
|
.min(1)
|
|
.refine(isAbsolute, "REA_IDA_MCP_CONFIG must be absolute")
|
|
.optional(),
|
|
GHIDRA_INSTALL_DIR: z
|
|
.string()
|
|
.min(1)
|
|
.refine(isAbsolute, "GHIDRA_INSTALL_DIR must be absolute")
|
|
.optional(),
|
|
JAVA_HOME: z
|
|
.string()
|
|
.min(1)
|
|
.refine(isAbsolute, "JAVA_HOME must be absolute")
|
|
.optional(),
|
|
REA_GHIDRA_NATIVEAOT_JAR: z
|
|
.string()
|
|
.min(1)
|
|
.refine(isAbsolute, "REA_GHIDRA_NATIVEAOT_JAR must be absolute")
|
|
.optional(),
|
|
REA_GHIDRA_STARTUP_TIMEOUT_MS: ghidraStartupTimeoutSchema,
|
|
REA_ILSPY_CMD_PATH: z
|
|
.string()
|
|
.min(1)
|
|
.refine(isAbsolute, "REA_ILSPY_CMD_PATH must be absolute")
|
|
.optional(),
|
|
HOPPER_LAUNCHER_PATH: z.string().min(1).optional(),
|
|
HOPPER_TARGET_PATH: z.string().min(1).optional(),
|
|
HOPPER_TARGET_KIND: z.enum(["executable", "database"]).default("executable"),
|
|
HOPPER_LOADER_ARGS_JSON: z.string().optional(),
|
|
REA_LOG_LEVEL: z
|
|
.enum(["trace", "debug", "info", "warn", "error", "fatal", "silent"])
|
|
.default("info"),
|
|
REA_REFERENCE_SECRET_PATTERNS_JSON: z.string().default("[]"),
|
|
});
|
|
|
|
export type Environment = z.infer<typeof environmentSchema>;
|
|
|
|
export const parseEnvironment = (
|
|
environment: Readonly<Record<string, string | undefined>>,
|
|
): Result<Environment, ConfigurationError> => {
|
|
const parsed = environmentSchema.safeParse(environment);
|
|
if (!parsed.success) {
|
|
// Zod messages name the constraint, never the rejected value.
|
|
const settings = parsed.error.issues.map((issue) => ({
|
|
setting: String(issue.path[0] ?? "environment"),
|
|
constraint: issue.message,
|
|
}));
|
|
return err(
|
|
new ConfigurationError("Invalid REA environment configuration", {
|
|
cause: parsed.error,
|
|
settings,
|
|
}),
|
|
);
|
|
}
|
|
return ok(parsed.data);
|
|
};
|