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
+28 -1
View File
@@ -115,6 +115,7 @@ export class GhidraClient {
#lastDiagnostics: Readonly<Record<string, JsonValue>> = {};
#startupController: AbortController | undefined;
#startPromise: Promise<GhidraStartResult> | undefined;
#ready = false;
#closePromise: Promise<void> | undefined;
readonly #failure = bindGhidraSessionFailure(
() => this.#diagnostics(),
@@ -216,7 +217,8 @@ export class GhidraClient {
void started.then(
(result) => {
signal?.removeEventListener("abort", onAbort);
if (!result.ok) reset();
if (result.ok) this.#ready = true;
else reset();
},
(cause: unknown) => {
this.#logger.debug(
@@ -293,6 +295,20 @@ export class GhidraClient {
);
}
/** Dispatch against the connected bridge without invoking startup or retry. */
callToolIfReady(
operation: GhidraOperation,
parameters: Readonly<Record<string, JsonValue>>,
options: { readonly signal?: AbortSignal } = {},
): Promise<Result<JsonValue, GhidraSessionError> | undefined> {
if (!this.isReady()) return Promise.resolve(undefined);
return this.#requestQueue.run(
operation,
parameters,
options.signal === undefined ? {} : { signal: options.signal },
);
}
/** Read the private immutable source for format parsers that require captured bytes. */
readTargetSnapshot(
maximumBytes?: number,
@@ -416,6 +432,16 @@ export class GhidraClient {
}
}
/** True only after the authenticated startup handshake has completed. */
isReady(): boolean {
return (
this.#ready &&
this.#socket !== undefined &&
!this.#socket.destroyed &&
this.#closePromise === undefined
);
}
#cleanupError(cause: unknown): AnalysisError {
if (cause instanceof AnalysisError && cause.cleanupIncomplete) return cause;
return new ProviderCleanupError(
@@ -462,6 +488,7 @@ export class GhidraClient {
}
#requestClose(forceStop: boolean): Promise<void> {
this.#ready = false;
const starting = this.#startPromise;
const controller = this.#startupController;
controller?.abort();