fix(mcp): keep prompt completion within ready provider sessions

Avoid lazy provider startup for document and procedure completion. Propagate MCP cancellation through completers, cancel lookups during session transitions, and share provider result projection with ordinary operations. Cover cold, starting, warm, queued, cancelled, and closed sessions through MCP composition and provider boundaries.
This commit is contained in:
morluto
2026-10-12 16:27:51 +08:00
parent 0d5033ad12
commit 07b36c1489
16 changed files with 740 additions and 57 deletions
+35 -2
View File
@@ -121,6 +121,7 @@ export class HopperClient {
#launcherFailureDiagnostic: HopperStartupFailureDiagnostic | undefined;
#operationFailure: OperationFailureRecord | undefined;
#startupController: AbortController | undefined;
#ready = false;
#startupFailureResult: AnalysisError | undefined;
#runId: string | undefined;
#startPromise: Promise<Result<HopperServerInfo, AnalysisError>> | undefined;
@@ -257,6 +258,16 @@ export class HopperClient {
};
}
/** True only after the authenticated health handshake has completed. */
isReady(): boolean {
return (
this.#ready &&
this.#socket !== undefined &&
!this.#socket.destroyed &&
this.#closePromise === undefined
);
}
/** Launch the bridge once and complete its authenticated health handshake. */
start(
signal?: AbortSignal,
@@ -292,7 +303,7 @@ export class HopperClient {
if (!result.ok) {
this.#rememberStartupFailure(result.error);
reset();
}
} else this.#ready = true;
},
(cause: unknown) => {
this.#logger.debug(
@@ -466,7 +477,28 @@ export class HopperClient {
): Promise<Result<JsonValue, AnalysisError>> {
const started = await this.start(options.signal);
if (!started.ok) return started;
const result = await this.#request(name, arguments_, options);
return this.#normalizeToolResult(
name,
await this.#request(name, arguments_, options),
);
}
/** Dispatch against the connected bridge without invoking startup or retry. */
callToolIfReady(
name: string,
arguments_: Readonly<Record<string, JsonValue>> = {},
options: { readonly signal?: AbortSignal } = {},
): Promise<Result<JsonValue, AnalysisError> | undefined> {
if (!this.isReady()) return Promise.resolve(undefined);
return this.#request(name, arguments_, options).then((result) =>
this.#normalizeToolResult(name, result),
);
}
#normalizeToolResult(
name: string,
result: Result<JsonValue, AnalysisError>,
): Result<JsonValue, AnalysisError> {
return !result.ok &&
result.error instanceof HopperRemoteError &&
result.error.diagnosticType === "capability_unavailable"
@@ -579,6 +611,7 @@ export class HopperClient {
options: HopperClientCloseOptions = {},
): Promise<Result<null, AnalysisError>> {
this.#closing = true;
this.#ready = false;
try {
return await cleanupHopperSession({
socket: this.#socket,
+54 -32
View File
@@ -5,6 +5,7 @@ import { accessSync, constants, statSync } from "node:fs";
import { createAnalysisExecution } from "../application/AnalysisProvider.js";
import type {
AnalysisClient,
AnalysisOperation,
AnalysisClientContext,
AnalysisProfileResolutionOptions,
AnalysisProviderCandidate,
@@ -16,6 +17,7 @@ import type {
import type { AnalysisProfileCommitment } from "../domain/analysisProfile.js";
import type { AppConfig } from "../config/types.js";
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
import type { JsonValue } from "../domain/jsonValue.js";
import type { Logger } from "pino";
import { AnalysisCapabilityUnavailableError } from "../domain/analysisErrorCore.js";
import { err, ok } from "../domain/result.js";
@@ -251,6 +253,40 @@ export class HopperProvider implements AnalysisProviderCandidate {
this.logger.info(diagnostic, "Hopper bridge reported a diagnostic"),
});
const regexSearch = new HopperRegexSearch(client);
const projectExecution = (
operation: AnalysisOperation,
normalized: JsonValue,
rawResult: JsonValue | null = null,
) =>
createAnalysisExecution(normalized, executionProvider, {
// Only the file-offset mapping differs from Hopper's reply.
rawResult,
...(profile === undefined ? {} : { analysisProfile: profile }),
...(operation === "procedure_callers" ||
operation === "procedure_callees"
? { relationshipCoverage: { status: "complete" as const } }
: {}),
limitations: [
...(preparedImage === undefined
? []
: [
"REA loaded a verified thin Mach-O slice from the original FAT64 source. Identity and file offsets refer to the original file. The owned temporary image and its Hopper document close together, including on MCP shutdown.",
]),
...(CAPABILITIES.find(
(descriptor) => descriptor.operation === operation,
)?.limitations ?? []),
...(operation === "read_bytes"
? [
"Hopper byte reads stop at the containing segment's exclusive end; a readable prefix is returned with complete=false when the requested range crosses it.",
]
: []),
...(operation === "procedure_references"
? [
"Native CallReference classifications and unresolved targets are preserved. Detailed reference flags and coverage beyond reported CallReference objects remain unknown.",
]
: []),
],
});
return {
execute: async (operation, parameters, options) => {
const result =
@@ -265,40 +301,26 @@ export class HopperProvider implements AnalysisProviderCandidate {
? await mapHopperFileOffset(target, result.value, options?.signal)
: result;
return mapped.ok
? {
ok: true,
value: createAnalysisExecution(mapped.value, executionProvider, {
// Only the file-offset mapping differs from Hopper's reply.
rawResult: mapped === result ? null : result.value,
...(profile === undefined ? {} : { analysisProfile: profile }),
...(operation === "procedure_callers" ||
operation === "procedure_callees"
? { relationshipCoverage: { status: "complete" as const } }
: {}),
limitations: [
...(preparedImage === undefined
? []
: [
"REA loaded a verified thin Mach-O slice from the original FAT64 source. Identity and file offsets refer to the original file. The owned temporary image and its Hopper document close together, including on MCP shutdown.",
]),
...(CAPABILITIES.find(
(descriptor) => descriptor.operation === operation,
)?.limitations ?? []),
...(operation === "read_bytes"
? [
"Hopper byte reads stop at the containing segment's exclusive end; a readable prefix is returned with complete=false when the requested range crosses it.",
]
: []),
...(operation === "procedure_references"
? [
"Native CallReference classifications and unresolved targets are preserved. Detailed reference flags and coverage beyond reported CallReference objects remain unknown.",
]
: []),
],
}),
}
? ok(
projectExecution(
operation,
mapped.value,
mapped === result ? null : result.value,
),
)
: mapped;
},
executeIfReady: async (operation, parameters, options) => {
if (operation !== "list_documents" && operation !== "list_procedures")
return undefined;
const result = await client.callToolIfReady(
operation,
parameters,
options?.signal === undefined ? {} : { signal: options.signal },
);
if (result === undefined || !result.ok) return result;
return ok(projectExecution(operation, result.value));
},
runtimeLineageSnapshots: () => {
const observation = client.runtimeLineage();
return observation === null