fix(hopper): diagnose MCP provider failures with stage, state, and request context (#1044)

* fix(hopper): diagnose MCP provider failures with stage, state, and request context

* fix(hopper): retain health at the actual provider lifecycle boundary

* test(hopper): distinguish socket loss from later observed process exit

* test(hopper): cover observed exit separately from socket-first replies

---------

Co-authored-by: N0zoM1z0 <1705004398@qq.com>
This commit is contained in:
Alan Vo
2026-10-09 00:33:56 +08:00
committed by GitHub
co-authored by N0zoM1z0
parent b5c2954ad6
commit 5a67c7fc54
20 changed files with 990 additions and 62 deletions
+37 -3
View File
@@ -25,6 +25,7 @@ import {
} from "./hopperErrors.js";
import { ProviderSelectionError } from "./providerSelectionError.js";
import { UnknownRegistryError } from "./unknownRegistryError.js";
import { providerRetryAction } from "./providerOperationHealth.js";
import {
type AnalysisError,
type AnalysisErrorTag,
@@ -49,7 +50,7 @@ export const analysisErrorRemediationAction = (
? "Check binary_session.analysis_activity, wait for the active Hopper request to finish, then retry."
: "Check binary_session for Hopper health, then retry the operation.";
if (error instanceof HopperProcessError)
return "Check binary_session provider health. Restart the owned Hopper process only when it has stopped.";
return hopperProcessRemediation(error);
if (error instanceof HopperStartError)
return error.ownerRunId === undefined
? "Check the Hopper launcher and target details, then retry opening the target."
@@ -191,8 +192,7 @@ const hopperErrorUserMessage = (error: AnalysisError): string | undefined => {
? `Hopper timed out during ${request} while the provider remained busy. Check binary_session.analysis_activity, wait for the active request to finish, then retry.`
: `Hopper timed out during ${request} before it started. Check binary_session for provider health, then retry.`;
}
if (error instanceof HopperProcessError)
return `Hopper stopped during ${error.operation ?? "connection"}${error.exitCode === null ? "; its exit was not observed" : ` with exit code ${String(error.exitCode)}`}.${error.userMessage === undefined ? "" : ` ${error.userMessage}`} Check binary_session provider health before retrying.`;
if (error instanceof HopperProcessError) return hopperProcessMessage(error);
if (error instanceof HopperStartError)
return (
error.userMessage ??
@@ -203,6 +203,40 @@ const hopperErrorUserMessage = (error: AnalysisError): string | undefined => {
return undefined;
};
const RESTART_OWNED_PROVIDER =
"Restart the owned provider by calling close_binary, then open the target again and retry.";
const hopperProcessRemediation = (error: HopperProcessError): string => {
if (error.failureCode !== undefined && error.userMessage !== undefined)
return error.userMessage;
if (providerRetryAction(error.providerState) === "restart_provider")
return RESTART_OWNED_PROVIDER;
return "Read binary_session.provider_operation_health for this request before retrying or restarting the owned provider.";
};
const hopperProcessMessage = (error: HopperProcessError): string => {
const stage = error.stage;
const where = error.operation ?? stage;
const request =
error.requestId === undefined
? ""
: ` Request id ${String(error.requestId)}.`;
const startup =
error.userMessage === undefined ? "" : ` ${error.userMessage}`;
if (error.providerState === "exited") {
const exit =
error.exitCode === null
? "no exit code was observed"
: `exit code ${String(error.exitCode)}`;
const recovery =
error.failureCode === undefined ? ` ${RESTART_OWNED_PROVIDER}` : "";
return `Hopper exited during ${where} (${exit}).${startup}${recovery}${request}`;
}
if (error.providerState === "unreachable")
return `Hopper became unreachable during ${where}; its process exit was not observed. ${RESTART_OWNED_PROVIDER}${startup}${request}`;
return `Hopper failed during ${where} and REA could not determine whether the provider is busy, exited, or unreachable.${startup} Read binary_session.provider_operation_health for this request before retrying or restarting.${request}`;
};
const standardErrorMessage = (tag: AnalysisErrorTag): string | undefined => {
if (UNREADABLE_OUTPUT_TAGS.has(tag))
return "Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.";
@@ -390,3 +390,40 @@ describe("analysis error projection: caller contract", () => {
});
});
});
describe("analysis error projection: operational diagnostics", () => {
it("distinguishes an observed Hopper exit from an undetermined provider failure", () => {
const exited = projectAnalysisError(
new HopperProcessError(7, undefined, "search_strings", 4),
);
expect(exited).toMatchObject({
code: "provider_unavailable",
retryable: true,
details: {
stage: "analysis",
provider_state: "exited",
retry_action: "restart_provider",
operation: "search_strings",
request_id: 4,
exit_code: 7,
},
});
expect(exited.message).not.toContain("Run `rea doctor`, then try again.");
expect(exited.remediation.action).toContain("Restart the owned provider");
const unknown = projectAnalysisError(
new HopperProcessError(null, undefined, "search_strings", 5),
);
expect(unknown.details).toMatchObject({
stage: "analysis",
provider_state: "unknown",
retry_action: "unknown",
operation: "search_strings",
request_id: 5,
exit_code: null,
});
expect(unknown.message).toContain("could not determine");
expect(unknown.message).not.toContain("stopped");
expect(unknown.remediation.action).toContain("provider_operation_health");
});
});
+22 -3
View File
@@ -25,11 +25,13 @@ import {
HopperCancelledError,
HopperProcessError,
HopperRemoteError,
HopperStartError,
HopperTimeoutError,
} from "./hopperErrors.js";
import { ProviderAdapterError } from "./providerAdapterError.js";
import { ProviderSelectionError } from "./providerSelectionError.js";
import { UnknownRegistryError } from "./unknownRegistryError.js";
import { providerRetryAction } from "./providerOperationHealth.js";
import {
type AnalysisError,
type AnalysisErrorTag,
@@ -327,10 +329,16 @@ const providerErrorDetails = (
...(error.operation === undefined ? {} : { operation: error.operation }),
...(error.requestId === undefined ? {} : { request_id: error.requestId }),
};
if (error instanceof HopperProcessError)
if (error instanceof HopperProcessError) {
const stage = error.stage;
return {
exit_code: error.exitCode,
stage: error.operation === undefined ? "connection" : "analysis",
stage,
provider_state: error.providerState,
retry_action: providerRetryAction(
error.providerState,
error.failureCode !== undefined,
),
...(error.failureCode === undefined
? {}
: { failure_code: error.failureCode }),
@@ -340,6 +348,16 @@ const providerErrorDetails = (
? {}
: { diagnostics: { ...error.diagnostic } }),
};
}
if (error instanceof HopperStartError)
return {
stage: "launch",
provider_state: "unknown",
retry_action: error.ownerRunId === undefined ? "retry" : "unknown",
...(error.ownerRunId === undefined
? {}
: { owner_run_id: error.ownerRunId }),
};
return undefined;
};
@@ -369,9 +387,10 @@ const lifecycleErrorDetails = (
return { operation: error.operation, timeout_ms: error.timeoutMs };
if (error instanceof HopperTimeoutError)
return {
stage: error.operation === undefined ? "startup" : "analysis",
stage: error.operation === undefined ? "startup" : error.stage,
timeout_ms: error.timeoutMs,
provider_state: error.providerState,
retry_action: error.providerState === "busy" ? "wait" : "retry",
...(error.operation === undefined ? {} : { operation: error.operation }),
...(error.requestId === undefined ? {} : { request_id: error.requestId }),
};
+20
View File
@@ -1,4 +1,8 @@
import { AnalysisError } from "./analysisErrorBase.js";
import type {
HopperProcessProviderState,
ProviderFailureStage,
} from "./providerOperationHealth.js";
import {
hopperStartupFailure,
type HopperStartupDiagnostic,
@@ -18,6 +22,9 @@ export class HopperTimeoutError extends HopperError {
readonly operation?: string,
readonly requestId?: number,
readonly providerState: "busy" | "not_started" = "not_started",
readonly stage: ProviderFailureStage = operation === undefined
? "launch"
: "analysis",
) {
super(
`Hopper ${operation === undefined ? "startup" : operation} timed out after ${String(timeoutMs)}ms`,
@@ -76,17 +83,30 @@ export class HopperProcessError extends HopperError {
readonly _tag = "HopperProcessError";
readonly failureCode: HopperStartupFailureCode | undefined;
override readonly userMessage: string | undefined;
readonly providerState: HopperProcessProviderState;
readonly stage: ProviderFailureStage;
constructor(
readonly exitCode: number | null,
readonly diagnostic?: HopperStartupFailureDiagnostic,
readonly operation?: string,
readonly requestId?: number,
providerState?: HopperProcessProviderState,
stage?: ProviderFailureStage,
) {
super(`Hopper bridge stopped unexpectedly with code ${String(exitCode)}`);
const failure = hopperStartupFailure(exitCode);
this.stage =
stage ??
(failure === undefined
? operation === undefined
? "connection"
: "analysis"
: "launch");
this.failureCode = failure?.code;
this.userMessage = failure?.message;
this.providerState =
providerState ?? (exitCode === null ? "unknown" : "exited");
}
}
+88
View File
@@ -0,0 +1,88 @@
/** Caller-visible operational state for one provider bridge. */
export const PROVIDER_OPERATION_STATES = [
"idle",
"busy",
"exited",
"unreachable",
"not_started",
"unknown",
] as const;
/** Operational state REA can support with evidence, or unknown. */
export type ProviderOperationState = (typeof PROVIDER_OPERATION_STATES)[number];
/** States a Hopper process failure can establish. */
export type HopperProcessProviderState = "exited" | "unreachable" | "unknown";
/** What the caller should do next for this provider state. */
export const PROVIDER_RETRY_ACTIONS = [
"retry",
"wait",
"restart_provider",
"unknown",
] as const;
/** Recovery choice for a provider operation failure. */
export type ProviderRetryAction = (typeof PROVIDER_RETRY_ACTIONS)[number];
/** Stage that was in progress when the provider operation failed. */
export const PROVIDER_FAILURE_STAGES = [
"launch",
"connection",
"analysis",
"decompilation",
"unknown",
] as const;
/** Known provider-operation stage, including an explicit unknown. */
export type ProviderFailureStage = (typeof PROVIDER_FAILURE_STAGES)[number];
/** One provider request retained with a failure or an in-flight operation. */
export interface ProviderOperationRequest {
readonly requestId: number;
readonly operation: string;
readonly stage: ProviderFailureStage;
}
/** Sanitized provider health a session can show after the request returns. */
export interface ProviderOperationHealth {
readonly state: ProviderOperationState;
readonly stage: ProviderFailureStage | null;
readonly retryAction: ProviderRetryAction | null;
readonly exitCode: number | null;
readonly requests: readonly ProviderOperationRequest[];
}
/**
* Choose a recovery action from the observed state.
* Startup failures keep their existing install or configuration guidance.
*/
export const providerRetryAction = (
state: ProviderOperationState,
startupFailure = false,
): ProviderRetryAction | null => {
if (state === "idle") return null;
if (startupFailure && state === "exited") return "retry";
switch (state) {
case "busy":
return "wait";
case "exited":
case "unreachable":
return "restart_provider";
case "not_started":
return "retry";
case "unknown":
return "unknown";
}
};
/** Use one stage when every retained request agrees, otherwise unknown. */
export const sharedProviderFailureStage = (
requests: readonly ProviderOperationRequest[],
): ProviderFailureStage | null => {
const first = requests[0]?.stage;
if (first === undefined) return null;
return requests.every((request) => request.stage === first)
? first
: "unknown";
};