fix(mcp): escalate transient failures before asking for a restart (#6353)

* fix(mcp): escalate transient failures before asking for a restart

Give dropped connections and startup failures one bounded retry, then a
scripted handoff to the user. Point timeout recovery at a named request
field or REA_* setting instead of telling agents those limits are fixed.

Co-authored-by: morluto <[email protected]>

* fix(process): stop unexpected command failures from recommending a retry

The catch can already have launched the target, and the projection stays
non-retryable. Tell the user to check the inputs and run doctor instead.

Co-authored-by: morluto <[email protected]>

* fix(mcp): limit retries to safe calls and recommended settings

A dropped connection may already have changed the target, and a bad
catalog rejects the same startup every time. Retry only a repeatable
call, and change a setting only when the remediation recommends it.

Co-authored-by: morluto <[email protected]>

* test(mcp): match recovery guidance across wrapped lines

Startup handoff capitalization and markdown wrapping still carry the
same sentences. Compare the prose with whitespace collapsed.

Co-authored-by: morluto <[email protected]>

---------

Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: morluto <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-23 01:32:22 +08:00
1 parent e28c11f87d
commit 791f6328b0
15 files changed
+233 -44

No files matched your search

+8
View File
@@ -265,6 +265,13 @@ const cliAnalysisError = (error: AnalysisError): ProcessCliErrorOutput => ({
...projectAnalysisError(error),
});
/**
* This catch includes target execution, and the projection stays non-retryable.
* Tell the user what happened; do not ask them to run the command again.
*/
export const PROCESS_UNEXPECTED_FAILURE_REMEDIATION =
"Do not repeat this command. Tell the user the process command could not complete and ask them to check the input files, then run `rea doctor`.";
/** Preserve typed failures and local diagnostics at the process CLI boundary. */
export const projectProcessCliError = (
cause: unknown,
@@ -276,6 +283,7 @@ export const projectProcessCliError = (
cause,
diagnostics: { phase: "process_cli" },
userMessage: `Process command could not complete: ${describeValidationFailure(cause)}`,
remediationAction: PROCESS_UNEXPECTED_FAILURE_REMEDIATION,
}),
);
};
+1 -1
View File
@@ -100,7 +100,7 @@ describe("agent recovery metadata", () => {
"raise its tool deadline",
);
expect(projected.remediation.action).not.toMatch(
/declared timeout controls|larger server\/provider budget/u,
/declared timeout controls|larger server\/provider budget|without assuming adjustable limits|do not assume adjustable/u,
);
expect(analysisErrorProjectionSchema.parse(projected)).toEqual(projected);
});
+1 -1
View File
@@ -5,5 +5,5 @@ export const PACKAGE_METADATA = {
serverSdkVersion: "2.3.1",
clientSdkVersion: "2.3.1",
coreSdkVersion: "2.3.1",
skillVersion: "44",
skillVersion: "45",
} as const;
+150
View File
@@ -0,0 +1,150 @@
import { readFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { expect, it } from "vitest";
import { PROCESS_UNEXPECTED_FAILURE_REMEDIATION } from "../application/process/ProcessCli.js";
import {
captureDecoderHeapRecovery,
DEFAULT_CAPTURE_DECODER_HEAP,
} from "../config/captureDecoderHeap.js";
import {
DEFAULT_MCP_STARTUP_BUDGETS,
mcpPhaseBudgetRecovery,
} from "../config/mcpStartupBudgets.js";
import { AnalysisTimeoutError } from "../domain/analysisErrorCore.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import {
MCP_CONNECTION_LOST,
MCP_CONNECTION_START_FAILED,
MCP_HTTP_LISTEN_FAILED,
MCP_SHUTDOWN_FAILED,
SERVER_START_FAILED,
} from "./messages.js";
const root = join(dirname(fileURLToPath(import.meta.url)), "../..");
const STALE_LIMIT_COPY =
/without assuming adjustable limits|do not assume adjustable|Generic errors do not assume adjustable/u;
function prose(text: string): string {
return text.replace(/\s+/gu, " ");
}
it("uses one retry then a user handoff for transient MCP failures", () => {
expect(MCP_CONNECTION_LOST).toBe(
"Retry the tool call once only when it is safe to repeat or known not to have started. If it may already have changed the target, or it fails again, tell the user REA lost its MCP client connection and ask them to restart the MCP client.",
);
expect(MCP_CONNECTION_LOST).not.toContain("rea doctor");
expect(MCP_CONNECTION_START_FAILED).toBe(
"Retry once. If it fails again, tell the user REA could not start its MCP connection and ask them to restart the MCP client, then run `rea doctor`.",
);
expect(MCP_CONNECTION_START_FAILED).toContain("Retry once.");
expect(SERVER_START_FAILED).toBe(
"Do not repeat this startup. Tell the user REA could not start and ask them to restart the MCP client, then run `rea doctor`.",
);
expect(SERVER_START_FAILED).not.toMatch(/\bretry\b/iu);
for (const message of [MCP_CONNECTION_START_FAILED, SERVER_START_FAILED]) {
expect(message).toMatch(/tell the user/iu);
expect(message).toContain("restart the MCP client");
expect(message).toContain("`rea doctor`");
}
expect(PROCESS_UNEXPECTED_FAILURE_REMEDIATION).toBe(
"Do not repeat this command. Tell the user the process command could not complete and ask them to check the input files, then run `rea doctor`.",
);
expect(PROCESS_UNEXPECTED_FAILURE_REMEDIATION).not.toMatch(/\bretry\b/iu);
});
it("keeps non-retryable transport failures on their named fix", () => {
expect(MCP_SHUTDOWN_FAILED).toBe(
"REA could not close cleanly. End the REA process before starting it again.",
);
expect(MCP_HTTP_LISTEN_FAILED).toBe(
"REA could not listen for MCP HTTP connections on the selected host and port. Choose another --port or stop the process using it.",
);
expect(MCP_SHUTDOWN_FAILED).not.toContain("Retry");
expect(MCP_HTTP_LISTEN_FAILED).not.toContain("tell the user");
});
it("keeps ceiling dial recoveries and scrubs non-adjustable timeout teaching", async () => {
const heap = captureDecoderHeapRecovery(DEFAULT_CAPTURE_DECODER_HEAP);
expect(heap).toContain("source ");
expect(heap).toContain("then restart REA");
expect(heap).not.toContain("tell the user");
const phase = mcpPhaseBudgetRecovery(DEFAULT_MCP_STARTUP_BUDGETS.initialize);
expect(phase).toContain("Set ");
expect(phase).toContain("then restart REA");
expect(phase).not.toContain("tell the user");
const timeout = projectAnalysisError(
new AnalysisTimeoutError("inspect_binary_layout", 2_500),
);
expect(timeout.remediation.action).not.toMatch(STALE_LIMIT_COPY);
expect(timeout.message).not.toMatch(STALE_LIMIT_COPY);
const files = [
"docs/user-visible-errors.csv",
"docs/mcp-contracts.md",
"src/server/createServer.ts",
"src/domain/analysisErrorPresentation.ts",
".agents/skills/reverse-engineer-anything/SKILL.md",
".agents/skills/reverse-engineer-anything/references/connection-and-recovery.md",
];
for (const path of files) {
const text = await readFile(join(root, path), "utf8");
expect(text, path).not.toMatch(STALE_LIMIT_COPY);
}
const catalog = await readFile(
join(root, "docs/user-visible-errors.csv"),
"utf8",
);
expect(catalog).toContain(MCP_CONNECTION_LOST);
expect(catalog).toContain(MCP_CONNECTION_START_FAILED);
expect(catalog).toContain(SERVER_START_FAILED);
expect(catalog).toContain(PROCESS_UNEXPECTED_FAILURE_REMEDIATION);
expect(catalog).toContain("analysis_timeout_ms");
expect(catalog).toContain("REA_* setting");
const skill = await readFile(
join(
root,
".agents/skills/reverse-engineer-anything/references/connection-and-recovery.md",
),
"utf8",
);
const skillProse = prose(skill);
expect(skillProse).toContain(
"retry that call once only when it is safe to repeat or known not to have started",
);
expect(skillProse).toContain("tell the user REA's connection failed");
expect(skillProse).toContain("Do not invent a setting");
expect(skillProse).toContain("explicitly recommends that change");
expect(skillProse).toContain("not a recommendation to change it");
expect(skillProse).not.toContain("names a request field");
const instructions = prose(
await readFile(join(root, "src/server/createServer.ts"), "utf8"),
);
expect(instructions).toContain(
"retry that call once only when it is safe to repeat or known not to have started",
);
expect(instructions).toContain("explicitly recommends that change");
expect(instructions).toContain("not a recommendation to change it");
expect(instructions).not.toContain("names a request field");
const wakaru = await readFile(
join(root, "src/javascript/recovery/RecoveryFiles.ts"),
"utf8",
);
expect(wakaru).toContain(
"Raising analysis_timeout_ms or max_heap_mb does not raise it",
);
for (const path of [
"docs/mcp-contracts.md",
"docs/user-visible-errors.csv",
]) {
const text = prose(await readFile(join(root, path), "utf8"));
expect(text, path).toContain("explicitly recommends that change");
expect(text, path).toContain("not a recommendation to change it");
expect(text, path).not.toContain("names a request field");
}
});
+3 -3
View File
@@ -1,10 +1,10 @@
export const MCP_CONNECTION_LOST =
"REA lost its MCP client connection. Restart REA from your MCP client.";
"Retry the tool call once only when it is safe to repeat or known not to have started. If it may already have changed the target, or it fails again, tell the user REA lost its MCP client connection and ask them to restart the MCP client.";
export 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.";
"Retry once. If it fails again, tell the user REA could not start its MCP connection and ask them to restart the MCP client, then run `rea doctor`.";
export const MCP_SHUTDOWN_FAILED =
"REA could not close cleanly. End the REA process before starting it again.";
export const SERVER_START_FAILED =
"REA could not start. Run `rea doctor`, then restart REA from your MCP client.";
"Do not repeat this startup. Tell the user REA could not start and ask them to restart the MCP client, then run `rea doctor`.";
export const MCP_HTTP_LISTEN_FAILED =
"REA could not listen for MCP HTTP connections on the selected host and port. Choose another --port or stop the process using it.";
+1 -1
View File
@@ -182,7 +182,7 @@ const ACTIVE_TARGET_INSTRUCTIONS =
// A host deadline can discard the response before error remediation reaches the
// agent. Teach that distinction at initialization, not just in returned errors.
const RECOVERY_INSTRUCTIONS =
"Recovery: For invalid arguments, read the connected tool's input schema (refresh tools/list if stale), fix the reported fields and do not repeat the unchanged call. A returned REA provider_timeout is a server/provider deadline: follow its remediation and reuse accessible partial Evidence. A host error such as 'timed out awaiting tools/call' without a REA result is a client deadline, not proof that work or cleanup stopped. Do not repeat the unchanged analysis. Increase the host's tool deadline with execution/cleanup/delivery headroom; when configuration is not editable, report the required setting (Codex: mcp_servers.<server>.tool_timeout_sec, seconds). Server analysis_timeout_ms, when advertised, is a separate millisecond budget; raising it cannot fix a shorter client deadline. Session state does not establish completion of every target-free operation.";
"Recovery: For invalid arguments, read the connected tool's input schema (refresh tools/list if stale), fix the reported fields and do not repeat the unchanged call. If the MCP connection drops, retry that call once only when it is safe to repeat or known not to have started. If it may already have changed the target, or it fails again, tell the user and ask them to restart the MCP client. If REA could not start its connection, retry once, then tell the user and ask them to restart the MCP client. Do not retry a startup failure that returns the same unchanged result. Run `rea doctor` when REA could not start or tools stay missing after that restart. A returned REA provider_timeout is a server/provider deadline: follow its remediation and reuse accessible partial Evidence. Change a request field or REA_* setting only when the returned remediation explicitly recommends that change. Restart REA when that text says the value is read at startup. A mention that a control does not raise the deadline is not a recommendation to change it. A host error such as 'timed out awaiting tools/call' without a REA result is a client deadline, not proof that work or cleanup stopped. Do not repeat the unchanged analysis. Increase the host's tool deadline with execution/cleanup/delivery headroom; when configuration is not editable, report the required setting (Codex: mcp_servers.<server>.tool_timeout_sec, seconds). Server analysis_timeout_ms, when advertised, is a separate millisecond budget; raising it cannot fix a shorter client deadline. Session state does not establish completion of every target-free operation.";
export interface CreateServerOptions {
/** Preserve the connection's creation time when materializing its runtime. */