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
+268 -1
View File
@@ -11,7 +11,22 @@ import { afterEach, describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import type { AnalysisClient } from "../../../src/application/AnalysisProvider.js";
import {
createAnalysisExecution,
type AnalysisClient,
} from "../../../src/application/AnalysisProvider.js";
import type { JsonValue } from "../../../src/domain/jsonValue.js";
import type {
AnalysisProvider,
AnalysisProviderCandidate,
CapabilityDescriptor,
ProviderIdentity,
} from "../../../src/application/AnalysisProvider.js";
import { AnalysisProviderRegistry } from "../../../src/application/binary/AnalysisProviderRegistry.js";
import { composeBinarySession } from "../../../src/application/binary/BinarySessionComposition.js";
import { createAnalysisProfile } from "../../../src/domain/analysisProfile.js";
import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js";
import { err, ok } from "../../../src/domain/result.js";
import {
createCacheProvider,
createTestBinarySession,
@@ -304,6 +319,67 @@ describe("guided prompt completion registry", () => {
});
});
it("guided prompt completion avoids cold startup and cancels warm lookup on close", async () => {
const directory = await createTestTempDirectory("rea-prompt-warm-only-");
temporaryDirectories.push(directory);
const target = join(directory, "target.hop");
await writeFile(target, "fixture target");
const fixture = await createWarmCompletionFixture(target);
const { client } = fixture;
const opened = await client.callTool({
name: "open_binary",
arguments: { path: target },
});
expect(opened.isError).not.toBe(true);
expect(fixture.childrenCreated()).toBe(0);
expect(await complete(client, "document", "")).toEqual([]);
expect(await complete(client, "procedure", "")).toEqual([]);
expect(fixture.childrenCreated()).toBe(0);
const startup = fixture.holdStartup();
const warming = client.callTool({ name: "list_documents", arguments: {} });
await startup.started;
expect(await complete(client, "document", "")).toEqual([]);
expect(await complete(client, "procedure", "")).toEqual([]);
expect(fixture.childrenCreated()).toBe(1);
startup.finish();
const warmed = await warming;
expect(warmed.isError, JSON.stringify(warmed)).not.toBe(true);
expect(fixture.childrenCreated()).toBe(1);
expect(await complete(client, "document", "fixture")).toEqual([
"fixture-document",
]);
const cancellation = fixture.holdCompletion();
const controller = new AbortController();
const cancelled = client.complete(
{
ref: { type: "ref/prompt", name: "investigate_feature" },
argument: { name: "document", value: "" },
},
{ signal: controller.signal },
);
await cancellation.started;
controller.abort();
await expect(cancelled).rejects.toThrow();
await cancellation.aborted;
const lifecycle = fixture.holdCompletion();
const pending = client.complete({
ref: { type: "ref/prompt", name: "investigate_feature" },
argument: { name: "document", value: "" },
});
await lifecycle.started;
const closed = await client.callTool({
name: "close_binary",
arguments: {},
});
expect(closed.isError).not.toBe(true);
await lifecycle.aborted;
expect((await pending).completion.values).toEqual([]);
}, 10_000);
describe("guided prompt completion lifecycle", () => {
it("survives a cancelled completion and ignores its late result", async () => {
let calls = 0;
@@ -410,6 +486,181 @@ describe("guided prompt completion lifecycle", () => {
}, 10_000);
});
const createWarmCompletionFixture = async (target: string) => {
const identity = {
id: "deep-fixture",
name: "Deep fixture provider",
version: "1",
};
const analysisProfile = createAnalysisProfile(identity, { fixture: true });
const observeCompletion = (result: JsonValue) =>
resultOk(createAnalysisExecution(result, identity, { analysisProfile }));
let childrenCreated = 0;
let ready = false;
let startupGate:
| {
readonly started: () => void;
readonly release: Promise<void>;
}
| undefined;
let completionGate:
| {
readonly started: () => void;
readonly aborted: () => void;
}
| undefined;
const deep = deepProvider(identity, {
createClient() {
childrenCreated += 1;
return {
execute: async (operation) => {
if (operation !== "health") {
const gate = startupGate;
if (gate !== undefined) {
startupGate = undefined;
gate.started();
await gate.release;
}
ready = true;
}
return observeCompletion(
operation === "list_documents"
? ["fixture-document"]
: operation === "list_procedures"
? [{ address: "0x1000", value: "fixture-procedure" }]
: null,
);
},
executeIfReady(operation, _parameters, options) {
if (
!ready ||
(operation !== "list_documents" && operation !== "list_procedures")
)
return Promise.resolve(undefined);
const gate = completionGate;
if (gate === undefined)
return Promise.resolve(
observeCompletion(
operation === "list_documents"
? ["fixture-document"]
: [{ address: "0x1000", value: "fixture-procedure" }],
),
);
completionGate = undefined;
gate.started();
return new Promise((resolve) => {
const signal = options?.signal;
const cancel = (): void => {
gate.aborted();
resolve(err(new AnalysisCancelledError(operation)));
};
if (signal?.aborted === true) cancel();
else signal?.addEventListener("abort", cancel, { once: true });
});
},
close: () => Promise.resolve(resultOk(null)),
};
},
});
const auxiliaryIdentity: ProviderIdentity = {
id: "aux-fixture",
name: "Aux fixture",
version: "1",
};
const auxiliary: AnalysisProvider = {
identity: () => auxiliaryIdentity,
capabilities: () => [
promptCapability(auxiliaryIdentity, "inspect_artifact"),
],
createClient: () => ({
execute: () => Promise.resolve(observed(null)),
close: () => Promise.resolve(resultOk(null)),
}),
};
const session = composeBinarySession(new AnalysisProviderRegistry([deep]), [
auxiliary,
]);
const client = await connect(createServer({ kind: "session", session }));
resources.push(session);
return {
client,
childrenCreated: () => childrenCreated,
holdStartup: () => {
let started = (): void => undefined;
let finish = (): void => undefined;
const observedStart = new Promise<void>((resolve) => {
started = resolve;
});
const release = new Promise<void>((resolve) => {
finish = resolve;
});
startupGate = { started, release };
return { started: observedStart, finish };
},
holdCompletion: () => {
let started = (): void => undefined;
let aborted = (): void => undefined;
const observedStart = new Promise<void>((resolve) => {
started = resolve;
});
const observedAbort = new Promise<void>((resolve) => {
aborted = resolve;
});
completionGate = { started, aborted };
return { started: observedStart, aborted: observedAbort };
},
};
};
const deepProvider = (
identity: Parameters<typeof createAnalysisProfile>[0],
options: {
readonly createClient: AnalysisProviderCandidate["createClient"];
},
): AnalysisProviderCandidate => ({
identity: () => identity,
capabilities: () => [
promptCapability(identity, "list_documents"),
promptCapability(identity, "list_procedures"),
],
inspectAvailability: () => ({
status: "available",
code: null,
reason: null,
diagnostics: {},
}),
inspectTargetSupport: () => ({
status: "supported",
code: null,
reason: null,
diagnostics: {},
}),
resolveAnalysisProfile: async () =>
ok({ profile: createAnalysisProfile(identity, { fixture: true }) }),
createClient: options.createClient,
});
const promptCapability = (
provider: ProviderIdentity,
operation: CapabilityDescriptor["operation"],
): CapabilityDescriptor => ({
provider,
operation,
available: true,
reason: null,
cachePolicy: "live",
effects: {
mutatesArtifact: false,
launchesProcess: false,
mayShowUi: false,
mayAccessNetwork: false,
mayWriteFilesystem: false,
changesPermissions: false,
requiresRoot: false,
},
limitations: [],
});
const fixtureProvider = { id: "fixture", name: "Fixture", version: "1" };
const fixtureSession = (
@@ -461,6 +712,22 @@ const lifecycleClient = (
}
return Promise.resolve(observed(null));
},
executeIfReady(operation, parameters) {
if (operation === "list_documents")
return Promise.resolve(observed([`${target}-document`]));
if (operation === "list_procedures") {
requests.push(parameters);
return Promise.resolve(
observed([
{
address: target === "first" ? "0x1000" : "0x2000",
value: `${target}-procedure`,
},
]),
);
}
return Promise.resolve(undefined);
},
close: () => Promise.resolve(resultOk(null)),
});
@@ -150,6 +150,26 @@ afterEach(async () => {
await Promise.all(clients.splice(0).map((client) => client.close()));
});
describe("GhidraClient ready-only dispatch", () => {
it("does not start cold and preserves the normal result once ready", async () => {
const launcher = new FixtureLauncher();
const client = clientFor(launcher);
await expect(client.callToolIfReady("list_strings", {})).resolves.toBe(
undefined,
);
expect(launcher.processes).toEqual([]);
const started = await client.start();
if (!started.ok) throw started.error;
await expect(client.callToolIfReady("list_strings", {})).resolves.toEqual({
ok: true,
value: [expect.objectContaining({ value: "fixture value" })],
});
expect(launcher.processes).toHaveLength(1);
});
});
it("reports unknown startup work and stops notifications when cancellation releases the process", async () => {
vi.useFakeTimers({ toFake: ["setInterval", "clearInterval"] });
const launcher = new FixtureLauncher("silent");
@@ -15,6 +15,26 @@ import {
} from "./hopperClient.fixture.js";
describe("HopperClient protocol", () => {
it("does not start cold and preserves normal dispatch once ready", async () => {
const launcher = new FixtureLauncher();
const client = new HopperClient({ launcher, startupTimeoutMs: 1_000 });
onTestFinished(async () => {
await client.close();
});
await expect(
client.callToolIfReady("echo", { label: "cold" }),
).resolves.toBe(undefined);
expect(launcher.processes).toEqual([]);
const started = await client.start();
if (!started.ok) throw started.error;
await expect(
client.callToolIfReady("echo", { label: "warm" }),
).resolves.toEqual({ ok: true, value: { label: "warm" } });
expect(launcher.processes).toHaveLength(1);
});
it("sanitizes unexpected owned-cleanup exceptions", () => {
expect(
providerCleanupFailure(