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:
@@ -2,9 +2,11 @@ import type { EvidenceReader } from "../application/investigation/InvestigationR
|
||||
import { z } from "zod";
|
||||
|
||||
import { uniqueSorted } from "../domain/canonicalOrdering.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import type { AnalysisOperationPort } from "../application/AnalysisProvider.js";
|
||||
import type { BinarySessionPort } from "../application/binary/BinarySessionPort.js";
|
||||
import type { PromptCompletionKind } from "../contracts/promptContracts.js";
|
||||
import { currentMcpRequestSignal } from "./EvidenceMcpServer.js";
|
||||
import { artifactInventoryResultSchema } from "../domain/artifactGraph.js";
|
||||
import { artifactInspectionResultSchema } from "../domain/artifactInspection.js";
|
||||
import { parseProcessCapture } from "../domain/process/processCaptureParsing.js";
|
||||
@@ -57,9 +59,13 @@ const completionCandidates = async (
|
||||
): Promise<readonly string[]> => {
|
||||
switch (kind) {
|
||||
case "document":
|
||||
return documentCandidates(analysis);
|
||||
return documentCandidates(analysis, session);
|
||||
case "procedure":
|
||||
return procedureCandidates(analysis, context?.arguments?.document);
|
||||
return procedureCandidates(
|
||||
analysis,
|
||||
session,
|
||||
context?.arguments?.document,
|
||||
);
|
||||
case "provider":
|
||||
return providerCandidates(session);
|
||||
case "evidence":
|
||||
@@ -74,8 +80,15 @@ const completionCandidates = async (
|
||||
|
||||
const documentCandidates = async (
|
||||
analysis: AnalysisOperationPort,
|
||||
session: BinarySessionPort | undefined,
|
||||
): Promise<readonly string[]> => {
|
||||
const result = await analysis.execute("list_documents", {});
|
||||
const result = await completionInventoryLookup(
|
||||
analysis,
|
||||
session,
|
||||
"list_documents",
|
||||
{},
|
||||
);
|
||||
if (result === undefined) return [];
|
||||
if (!result.ok) return [];
|
||||
const parsed = documentListSchema.safeParse(result.value.result);
|
||||
return parsed.success ? parsed.data : [];
|
||||
@@ -83,12 +96,19 @@ const documentCandidates = async (
|
||||
|
||||
const procedureCandidates = async (
|
||||
analysis: AnalysisOperationPort,
|
||||
session: BinarySessionPort | undefined,
|
||||
document: string | undefined,
|
||||
): Promise<readonly string[]> => {
|
||||
const byAddress = new Map<string, string>();
|
||||
const input =
|
||||
document === undefined || document.length === 0 ? {} : { document };
|
||||
const result = await analysis.execute("list_procedures", input);
|
||||
const result = await completionInventoryLookup(
|
||||
analysis,
|
||||
session,
|
||||
"list_procedures",
|
||||
input,
|
||||
);
|
||||
if (result === undefined) return [];
|
||||
if (result.ok) {
|
||||
const parsed = z
|
||||
.array(z.object({ address: z.string(), value: z.string() }))
|
||||
@@ -111,6 +131,21 @@ const procedureCandidates = async (
|
||||
return candidates;
|
||||
};
|
||||
|
||||
const completionInventoryLookup = async (
|
||||
analysis: AnalysisOperationPort,
|
||||
session: BinarySessionPort | undefined,
|
||||
operation: "list_documents" | "list_procedures",
|
||||
parameters: Readonly<Record<string, JsonValue>>,
|
||||
) => {
|
||||
const signal = currentMcpRequestSignal();
|
||||
const options = signal === undefined ? {} : { signal };
|
||||
const result =
|
||||
session === undefined
|
||||
? await analysis.execute(operation, parameters, options)
|
||||
: await session.executeIfProviderReady(operation, parameters, options);
|
||||
return signal?.aborted === true ? undefined : result;
|
||||
};
|
||||
|
||||
const providerCandidates = (
|
||||
session: BinarySessionPort | undefined,
|
||||
): readonly string[] => {
|
||||
|
||||
Reference in New Issue
Block a user