Files
rea/src/ida/IdaConfiguration.ts
T
Kaoru0822andClaude Opus 5.5 0a0244db42 fix(config): name the rejected setting and its constraint in configuration errors
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>
2026-10-09 04:08:43 +00:00

91 lines
2.9 KiB
TypeScript

import { readFileSync, statSync } from "node:fs";
import { isAbsolute } from "node:path";
import { z } from "zod";
import { ConfigurationError } from "../domain/configurationErrors.js";
import { err, ok, type Result } from "../domain/result.js";
const common = {
mode: z.enum(["attached", "headless"]).default("attached"),
timeoutMs: z.number().int().positive().max(2_147_483_647).default(300_000),
workspaceRoot: z
.string()
.refine(isAbsolute, "workspaceRoot must be absolute on the REA host")
.optional(),
};
const registrationSchema = z.union([
z
.object({
...common,
type: z.literal("stdio").optional(),
command: z.string().min(1),
args: z.array(z.string()).default([]),
env: z.record(z.string(), z.string()).default({}),
})
.strict(),
z
.object({
...common,
type: z.literal("http").optional(),
url: z.url().refine((value) => {
const url = new URL(value);
return (
["http:", "https:"].includes(url.protocol) &&
["localhost", "127.0.0.1", "[::1]"].includes(url.hostname) &&
url.username === "" &&
url.password === ""
);
}, "IDA MCP must use a local HTTP endpoint without URL credentials"),
headers: z.record(z.string(), z.string()).default({}),
})
.strict(),
]);
/** An upstream MCP registration plus REA's database lifecycle selection. */
export type IdaConfiguration = z.infer<typeof registrationSchema>;
/** Read one explicit registration without starting or installing a provider. */
export const readIdaConfiguration = (
path: string,
): Result<IdaConfiguration, ConfigurationError> => {
try {
if (statSync(path).size > 65_536)
return err(
idaConfigurationError(
"REA_IDA_MCP_CONFIG exceeds the 64 KiB registration size.",
),
);
const input: unknown = JSON.parse(readFileSync(path, "utf8"));
const envelope = z
.object({ mcpServers: z.object({ "ida-pro-mcp": z.unknown() }) })
.safeParse(input);
const parsed = registrationSchema.safeParse(
envelope.success ? envelope.data.mcpServers["ida-pro-mcp"] : input,
);
return parsed.success
? ok(parsed.data)
: err(
idaConfigurationError(
"REA_IDA_MCP_CONFIG must contain an IDA MCP command/args or local url registration; see docs/ida-provider.md.",
parsed.error,
),
);
} catch (cause: unknown) {
return err(
idaConfigurationError(
`Cannot read IDA MCP registration ${path}; provide a readable JSON configuration.`,
cause,
),
);
}
};
/** A REA_IDA_MCP_CONFIG rejection that names the setting it concerns. */
export const idaConfigurationError = (
constraint: string,
cause?: unknown,
): ConfigurationError =>
new ConfigurationError(constraint, {
...(cause === undefined ? {} : { cause }),
settings: [{ setting: "REA_IDA_MCP_CONFIG", constraint }],
});