fix(errors): improve recovery guidance

This commit is contained in:
morluto
2026-07-13 19:09:27 +00:00
parent af8c4efb4a
commit 77454dc8f4
42 changed files with 1492 additions and 304 deletions
+67 -24
View File
@@ -14,15 +14,50 @@ import { createServer } from "./server/createServer.js";
import { createLogger } from "./logger.js";
import { projectAnalysisError } from "./domain/errors.js";
const MCP_CONNECTION_LOST =
"REA lost its MCP client connection. Restart REA from your MCP client.";
const MCP_CONNECTION_START_FAILED =
"REA could not start its MCP connection. Restart REA from your MCP client; run `rea doctor` if it fails again.";
const MCP_SHUTDOWN_FAILED =
"REA could not close cleanly. End the REA process before starting it again.";
const SERVER_START_FAILED =
"REA could not start. Run `rea doctor`, then restart REA from your MCP client.";
/** Process boundaries owned by the MCP executable adapter. */
interface RuntimeDependencies {
readonly env: NodeJS.ProcessEnv;
readonly serve: typeof serveStdio;
readonly writeStderr: (text: string) => void;
readonly setExitCode: (code: number) => void;
readonly registerShutdown: (handler: () => void) => void;
}
const runtimeDependencies = (): RuntimeDependencies => ({
env: process.env,
serve: serveStdio,
writeStderr: (text) => process.stderr.write(text),
setExitCode: (code) => {
process.exitCode = code;
},
registerShutdown: (handler) => {
process.once("SIGINT", handler);
process.once("SIGTERM", handler);
process.stdin.once("end", handler);
process.stdin.once("close", handler);
},
});
/**
* Start the long-lived MCP adapter and install idempotent shutdown handlers.
* The adapter owns its BinarySession for the process lifetime; EOF and process
* signals close bridge resources without assuming ownership of the Hopper app.
*/
export const run = async (): Promise<number> => {
const config = parseConfig(process.env);
export const run = async (
dependencies: RuntimeDependencies = runtimeDependencies(),
): Promise<number> => {
const config = parseConfig(dependencies.env);
if (!config.ok) {
process.stderr.write(`${projectAnalysisError(config.error).message}\n`);
dependencies.writeStderr(`${projectAnalysisError(config.error).message}\n`);
return 1;
}
const logger = createLogger("mcp", config.value.logLevel);
@@ -39,13 +74,15 @@ export const run = async (): Promise<number> => {
{ errorTag: opened.error._tag },
"Initial target failed to open",
);
process.stderr.write(`${projectAnalysisError(opened.error).message}\n`);
dependencies.writeStderr(
`${projectAnalysisError(opened.error).message}\n`,
);
return 1;
}
}
let handle: StdioServerHandle;
try {
handle = serveStdio(
handle = dependencies.serve(
() =>
createServer(session, session, {
logger,
@@ -54,15 +91,15 @@ export const run = async (): Promise<number> => {
}),
{
onerror: () => {
serverLogger.error("MCP stdio transport error");
process.stderr.write("MCP stdio transport error\n");
serverLogger.error(MCP_CONNECTION_LOST);
dependencies.writeStderr(`${MCP_CONNECTION_LOST}\n`);
},
},
);
} catch {
await session.close();
serverLogger.error("Failed to start MCP stdio transport");
process.stderr.write("Failed to start MCP stdio transport\n");
serverLogger.error(MCP_CONNECTION_START_FAILED);
dependencies.writeStderr(`${MCP_CONNECTION_START_FAILED}\n`);
return 1;
}
@@ -76,30 +113,36 @@ export const run = async (): Promise<number> => {
};
const requestShutdown = (): void => {
shutdown().catch(() => {
process.exitCode = 1;
serverLogger.error("Failed to shut down cleanly");
process.stderr.write("Failed to shut down cleanly\n");
dependencies.setExitCode(1);
serverLogger.error(MCP_SHUTDOWN_FAILED);
dependencies.writeStderr(`${MCP_SHUTDOWN_FAILED}\n`);
});
};
process.once("SIGINT", requestShutdown);
process.once("SIGTERM", requestShutdown);
process.stdin.once("end", requestShutdown);
process.stdin.once("close", requestShutdown);
dependencies.registerShutdown(requestShutdown);
return 0;
};
/** Run the executable boundary without exposing an unexpected startup cause. */
export const runEntrypoint = async (
start: () => Promise<number> = () => run(),
writeStderr: (text: string) => void = (text) => process.stderr.write(text),
setExitCode: (code: number) => void = (code) => {
process.exitCode = code;
},
): Promise<void> => {
try {
setExitCode(await start());
} catch {
writeStderr(`${SERVER_START_FAILED}\n`);
setExitCode(1);
}
};
const entryPath = process.argv[1];
if (
entryPath !== undefined &&
pathToFileURL(realpathSync(entryPath)).href === import.meta.url
) {
run()
.then((exitCode) => {
process.exitCode = exitCode;
})
.catch(() => {
process.stderr.write("Unexpected server startup failure\n");
process.exitCode = 1;
});
void runEntrypoint();
}