diff --git a/scripts/ci/test-selection.mjs b/scripts/ci/test-selection.mjs index 0ed4241aeb..a0dc505a60 100644 --- a/scripts/ci/test-selection.mjs +++ b/scripts/ci/test-selection.mjs @@ -97,7 +97,8 @@ const isModule = (path) => // Production importers never widen the selection transitively. const isHelper = (path) => !isTest(path) && - (/^tests\/.+\.m?ts$/u.test(path) || + !isCheckerInput(path) && + (/^tests\/.+\.(?:[cm]?[jt]s|d\.m?ts)$/u.test(path) || /^src\/.+\.fixture[^/]*\.ts$/u.test(path) || isVerifier(path) || isScriptLibrary(path)); @@ -199,19 +200,25 @@ export function traceAffectedTests({ files, snapshot }) { } } - // A seed and every helper that imports it, directly or through another. + const isNamedTarget = (path) => + path.startsWith("scripts/") || path.startsWith("tests/fixtures/"); + // A seed and every helper that imports or names it, directly or through another. const throughHelpers = (seed) => { const reached = new Set(seed); for (let grew = true; grew;) { grew = false; - for (const helper of helpers) + const namedReached = [...reached].filter(isNamedTarget); + for (const helper of helpers) { + if (reached.has(helper)) continue; + const source = sources.get(helper) ?? ""; if ( - !reached.has(helper) && - imports.get(helper).some((target) => reached.has(target)) + imports.get(helper).some((target) => reached.has(target)) || + namedReached.some((target) => source.includes(target)) ) { reached.add(helper); grew = true; } + } } return reached; }; @@ -224,13 +231,10 @@ export function traceAffectedTests({ files, snapshot }) { for (const test of tests) if (imports.get(test).some((target) => affected.has(target))) select(test, "imports"); - // Script paths a test can start or read by name: changed files, plus the - // verifiers and shared modules a change reaches. + // Script and fixture paths a test can start or read by name: changed files, + // plus the verifiers, shared modules and fixtures a change reaches. const named = (reached) => [ - ...new Set([ - ...files, - ...[...reached].filter((path) => path.startsWith("scripts/")), - ]), + ...new Set([...files, ...[...reached].filter(isNamedTarget)]), ]; const names = (test, paths) => paths.some((path) => (sources.get(test) ?? "").includes(path)); @@ -240,10 +244,7 @@ export function traceAffectedTests({ files, snapshot }) { // fails closed to the complete suite. for (const fixture of changedNonModuleFixtures) { const reached = throughHelpers([fixture]); - const paths = [ - fixture, - ...[...reached].filter((path) => path.startsWith("scripts/")), - ]; + const paths = [fixture, ...[...reached].filter(isNamedTarget)]; if ( !tests.some( (test) => diff --git a/src/application/Doctor.lifecycle.test.ts b/src/application/Doctor.lifecycle.test.ts index 5e0b26c2e7..8fa4911914 100644 --- a/src/application/Doctor.lifecycle.test.ts +++ b/src/application/Doctor.lifecycle.test.ts @@ -179,3 +179,49 @@ it("joins concurrent sibling probes before closing and prefers physical failure () => ownedHost.close(), ]); }); + +it("does not deadlock when one concurrent probe retains failed cleanup while a peer enters another scoped read", async () => { + const betweenReads = phaseGate(); + const refusedCleanup = controlledCleanup({ refused: true }); + let secondReadCompleted = false; + const host = systemDoctorHost({ + platform: "darwin", + environment: {}, + providerInspections: async (owner) => { + const peerProbe = (async () => { + await owner.run(async () => undefined); + await betweenReads.hold(); + await owner.run(async () => { + secondReadCompleted = true; + }); + })(); + await owner.run(async () => { + owner.retain( + { close: () => refusedCleanup.close() }, + "ida-registration-handle", + ); + }); + betweenReads.release(); + await peerProbe; + return []; + }, + }); + await withVerifierCleanup(async () => { + await expect(host.providerInspections!([])).resolves.toEqual([]); + expect(secondReadCompleted).toBe(true); + const failure = await settledCause(host.close!()); + if (!(failure instanceof AnalysisError)) + throw new Error("Expected retained cleanup failure on close"); + expect(failure.cleanupResources).toEqual(["ida-registration-handle"]); + expect(failure.cleanupIncomplete).toBe(true); + refusedCleanup.allow(); + await host.close!(); + expect(failure.cleanupIncomplete).toBe(false); + }, [ + () => { + refusedCleanup.allow(); + betweenReads.release(); + }, + () => host.close!(), + ]); +}); diff --git a/src/application/Doctor.ts b/src/application/Doctor.ts index 4e56bd510f..ad4fb6a1c8 100644 --- a/src/application/Doctor.ts +++ b/src/application/Doctor.ts @@ -680,6 +680,28 @@ export interface SystemDoctorHostOptions { ) => Promise; } +const runInChildResourceScope = ( + parent: ArtifactResourceScope, + label: string, + operation: (child: ArtifactResourceScope) => Promise, +): Promise => + parent.track(async () => { + const child = new ArtifactResourceScope(); + try { + return await operation(child); + } finally { + if (child.hasPending) { + parent.retain({ + kind: "lease", + resource: label, + lease: AnalysisCleanupLease.forCloseOwner(child, label), + }); + } else { + await child.close(); + } + } + }); + /** Create diagnostics backed by the current process and host commands. */ export const systemDoctorHost = ( options: SystemDoctorHostOptions = {}, @@ -701,7 +723,7 @@ export const systemDoctorHost = ( // ILSpy probe's ten-second allowance and bound each captured stream to 64 KiB. // Baseline diagnostics must work without provider ownership prerequisites. const hostExecFileOutput: typeof execFileOutput = (command, args, input) => - resources.run(async () => { + resources.track(async () => { try { const output = await execute(command, args, { ...input, @@ -746,7 +768,7 @@ export const systemDoctorHost = ( resource, lease: AnalysisCleanupLease.forCloseOwner(owner, resource), }), - run: (operation) => resources.run(operation), + run: (operation) => resources.track(operation), }; const resolveProviderRequestBudget = options.resolveProviderRequestBudget === undefined @@ -869,7 +891,7 @@ export const systemDoctorHost = ( ? {} : { providerInspections: (clients) => - resources.run(() => + resources.track(() => options.providerInspections!( resourceScope, inspectionSignal, @@ -916,12 +938,14 @@ export const systemDoctorHost = ( } }, installedSkillIdentity: (clientIds) => - readInstalledSkillIdentity( - homeDirectory, - clientIds, - environment, - platform, - resources, + runInChildResourceScope(resources, "skill-identity", (child) => + readInstalledSkillIdentity( + homeDirectory, + clientIds, + environment, + platform, + child, + ), ), close: async () => { controller.abort(OwnerCancellation.startupOwnerClosed); @@ -949,29 +973,33 @@ export const systemDoctorHost = ( ownedProviderSetting, observeProviderSetting, ) => - readClientRegistrationStatuses(homeDirectory, undefined, { - platform, - environment, - resources, - ...(selectedClientIds === undefined ? {} : { selectedClientIds }), - ...(ownedProviderSetting === undefined ? {} : { ownedProviderSetting }), - ...(observeProviderSetting === undefined - ? {} - : { observeProviderSetting }), - ...(providerSettings === undefined - ? {} - : { - providerSettings: registrationSpelledSettings( - providerSettings, - environment, - ), - }), - ...(resolveProviderRequestBudget === undefined - ? {} - : { - resolveProviderRequestBudget, - }), - }), + runInChildResourceScope(resources, "client-registrations", (child) => + readClientRegistrationStatuses(homeDirectory, undefined, { + platform, + environment, + resources: child, + ...(selectedClientIds === undefined ? {} : { selectedClientIds }), + ...(ownedProviderSetting === undefined + ? {} + : { ownedProviderSetting }), + ...(observeProviderSetting === undefined + ? {} + : { observeProviderSetting }), + ...(providerSettings === undefined + ? {} + : { + providerSettings: registrationSpelledSettings( + providerSettings, + environment, + ), + }), + ...(resolveProviderRequestBudget === undefined + ? {} + : { + resolveProviderRequestBudget, + }), + }), + ), }; const readFacts = options.desktopBuildFacts; return readFacts === undefined @@ -979,12 +1007,12 @@ export const systemDoctorHost = ( : { ...host, desktopBuildFacts: (clientIds) => - resources.run(() => + runInChildResourceScope(resources, "desktop-build-facts", (child) => readFacts({ environment, platform, signal: inspectionSignal, - resources, + resources: child, run: hostExecFileOutput, clientIds, registrations: host.clientRegistrations!, diff --git a/src/doctorRuntime.ts b/src/doctorRuntime.ts index ebf95db4eb..9815c95f99 100644 --- a/src/doctorRuntime.ts +++ b/src/doctorRuntime.ts @@ -27,7 +27,11 @@ import { selectLinuxPrivateDisplayStrategy } from "./hopper/LinuxPrivateDisplayP import { ok } from "./domain/result.js"; import { cancelledBy, carriedReason } from "./domain/analysisErrorCore.js"; import { ProviderCleanupError } from "./domain/providerCleanupError.js"; -import { AnalysisCleanupLeaseGroup } from "./resources/index.js"; +import { + AnalysisCleanupLease, + AnalysisCleanupLeaseGroup, + ArtifactResourceScope, +} from "./resources/index.js"; import type { RetainedResourceScope } from "./ports/AnalysisProvider.js"; import { RegularFileCleanupFailure } from "./filesystem/OwnedRegularFile.js"; import { readEffectiveClientRegistrationConfiguration } from "./application/ClientConfigurationFiles.js"; @@ -292,6 +296,30 @@ const unresolvedProvider = ( }; }; +const withProbeResourceScope = ( + parent: RetainedResourceScope, + label: string, + operation: (scope: RetainedResourceScope) => Promise, +): Promise => + parent.run(async () => { + const child = new ArtifactResourceScope(); + const scope: RetainedResourceScope = { + retain: (owner, resource) => + child.retain({ + kind: "lease", + resource, + lease: AnalysisCleanupLease.forCloseOwner(owner, resource), + }), + run: (work) => child.run(work), + }; + try { + return await operation(scope); + } finally { + if (child.hasPending) parent.retain(child, label); + else await child.close(); + } + }); + /** Compose provider diagnostics at the outer CLI adapter boundary. */ export const createSystemDoctorHost = ( environment: Readonly = process.env, @@ -316,10 +344,12 @@ export const createSystemDoctorHost = ( } let registration = byPath.get(path); if (registration === undefined) { - registration = readIdaConfiguration(path, { - resourceScope, - signal: inspectionSignal, - }); + registration = withProbeResourceScope(resourceScope, path, (probeScope) => + readIdaConfiguration(path, { + resourceScope: probeScope, + signal: inspectionSignal, + }), + ); byPath.set(path, registration); } return registration; @@ -355,12 +385,17 @@ export const createSystemDoctorHost = ( inspectionSignal: AbortSignal, clients: readonly string[], ) => { - const selected = await selectedRegistrationEnvironment( - clients, - environment, + const selected = await withProbeResourceScope( resourceScope, - inspectionSignal, - platform, + "selected-registration", + (probeScope) => + selectedRegistrationEnvironment( + clients, + environment, + probeScope, + inspectionSignal, + platform, + ), ); const inspectionEnvironment = selectedProviderSettings( selected, @@ -394,47 +429,63 @@ export const createSystemDoctorHost = ( return await settleConcurrent([ ghidraUnresolved !== undefined ? Promise.resolve(ghidraUnresolved) - : inspectSystemGhidraProvider( - inspectionEnvironment, + : withProbeResourceScope( resourceScope, - inspectionSignal, + "ghidra-doctor", + (probeScope) => + inspectSystemGhidraProvider( + inspectionEnvironment, + probeScope, + inspectionSignal, + ), ), idaUnresolved !== undefined ? Promise.resolve(idaUnresolved) - : inspectIdaRegistration( - inspectionEnvironment.REA_IDA_MCP_CONFIG, - resourceScope, - inspectionSignal, - inspectionEnvironment.REA_IDA_MCP_CONFIG === undefined - ? undefined - : readRegistration( - inspectionEnvironment.REA_IDA_MCP_CONFIG, - resourceScope, - inspectionSignal, - ), + : withProbeResourceScope(resourceScope, "ida-doctor", (probeScope) => + inspectIdaRegistration( + inspectionEnvironment.REA_IDA_MCP_CONFIG, + probeScope, + inspectionSignal, + inspectionEnvironment.REA_IDA_MCP_CONFIG === undefined + ? undefined + : readRegistration( + inspectionEnvironment.REA_IDA_MCP_CONFIG, + resourceScope, + inspectionSignal, + ), + ), ), jadxUnresolved !== undefined ? Promise.resolve(jadxUnresolved) - : inspectJadxProvider( - inspectionEnvironment, - resourceScope, - host, - inspectionSignal, + : withProbeResourceScope(resourceScope, "jadx-doctor", (probeScope) => + inspectJadxProvider( + inspectionEnvironment, + probeScope, + host, + inspectionSignal, + ), ), kunaUnresolved !== undefined ? Promise.resolve(kunaUnresolved) - : inspectKunaProvider(inspectionEnvironment, platform, { - resourceScope, - signal: inspectionSignal, - }), + : withProbeResourceScope(resourceScope, "kuna-doctor", (probeScope) => + inspectKunaProvider(inspectionEnvironment, platform, { + resourceScope: probeScope, + signal: inspectionSignal, + }), + ), ]); }, desktopBuildFacts: readDesktopBuildFacts, linuxDemoRuntimeCheck: (resourceScope, inspectionSignal) => - inspectLinuxHopperDemoRuntime( - environment, + withProbeResourceScope( resourceScope, - inspectionSignal, + "hopper-demo-runtime", + (probeScope) => + inspectLinuxHopperDemoRuntime( + environment, + probeScope, + inspectionSignal, + ), ), }); }; diff --git a/tests/boundary/cli/jsonInputStatus.test.ts b/tests/boundary/cli/jsonInputStatus.test.ts index e172c03afc..34a378d6d9 100644 --- a/tests/boundary/cli/jsonInputStatus.test.ts +++ b/tests/boundary/cli/jsonInputStatus.test.ts @@ -1,20 +1,33 @@ import { chmod, mkdir, readFile, writeFile } from "node:fs/promises"; import { join } from "node:path"; -import { describe, expect } from "vitest"; +import { afterAll, beforeAll, describe, expect } from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; +import { + createTestWorkspace, + removeTestWorkspace, +} from "../../support/workspace/workspaceFixture.js"; -let isolatedHomeEnvironment: Promise | undefined; -const sharedIsolatedHome = (): Promise => - (isolatedHomeEnvironment ??= createTestTempDirectory( - "rea-cli-json-isolated-home-", - ).then((home) => ({ - HOME: home, - XDG_CONFIG_HOME: home, - XDG_CACHE_HOME: home, - }))); +let isolatedHomeRoot = ""; +let isolatedHomeEnvironment: NodeJS.ProcessEnv = {}; + +beforeAll(async () => { + isolatedHomeRoot = (await createTestWorkspace("rea-cli-json-isolated-home-")) + .root; + isolatedHomeEnvironment = { + HOME: isolatedHomeRoot, + XDG_CONFIG_HOME: isolatedHomeRoot, + XDG_CACHE_HOME: isolatedHomeRoot, + }; +}); + +afterAll(async () => { + if (isolatedHomeRoot !== "") await removeTestWorkspace(isolatedHomeRoot); +}); + +const sharedIsolatedHome = (): NodeJS.ProcessEnv => isolatedHomeEnvironment; const JSON_COMMANDS = [ ["inspect-analysis-view", "inspect_analysis_view"], @@ -46,7 +59,7 @@ describe("compiled CLI JSON input failure status", () => { const input = "{"; const result = await cli.run({ arguments: [command, input, "--json"], - environment: await sharedIsolatedHome(), + environment: sharedIsolatedHome(), }); expect(result.json).toMatchObject({ @@ -72,7 +85,7 @@ describe("compiled CLI JSON input failure status", () => { const result = await cli.run({ arguments: ["compare-javascript-export-shapes", input, "--json"], - environment: await sharedIsolatedHome(), + environment: sharedIsolatedHome(), }); expect(result.json).toMatchObject({ @@ -161,7 +174,7 @@ describe("compiled CLI JSON document omissions", () => { }), "--json", ], - environment: await sharedIsolatedHome(), + environment: sharedIsolatedHome(), }); expect(result.json).toMatchObject({ code: "invalid_request", @@ -283,7 +296,7 @@ describe("compiled CLI JSON input diagnostic preservation", () => { const input = join(root, "missing.json"); const result = await cli.run({ arguments: ["compare-javascript-export-shapes", input, ...flags], - environment: await sharedIsolatedHome(), + environment: sharedIsolatedHome(), }); for (const diagnostic of [ "invalid_request", @@ -399,7 +412,7 @@ describe("compiled CLI JSON path ambiguity", () => { const result = await cli.run({ arguments: ["capture-browser-scenario", input, "--json"], cwd: root, - environment: await sharedIsolatedHome(), + environment: sharedIsolatedHome(), }); expect(result.json).toMatchObject({ diff --git a/tests/boundary/cli/sharedJavaScriptMcpSession.test.ts b/tests/boundary/cli/sharedJavaScriptMcpSession.test.ts index ce281a7431..c3a66f37ef 100644 --- a/tests/boundary/cli/sharedJavaScriptMcpSession.test.ts +++ b/tests/boundary/cli/sharedJavaScriptMcpSession.test.ts @@ -106,6 +106,29 @@ test("does not open a shared MCP server after file cleanup starts", async () => ); }); +test("retries pending cleanup on later calls after a transient drain failure", async () => { + let connectAttempts = 0; + let cleanupFails = true; + const session = createSharedMcpSession(async (registerCleanup) => { + connectAttempts += 1; + registerCleanup(async () => { + if (cleanupFails) throw new Error("transient cleanup failure"); + }); + if (connectAttempts === 1) throw new Error("first handshake failed"); + return fakeCall; + }); + await expect(session.session.call()).rejects.toThrow( + "first handshake failed", + ); + await expect(session.session.call()).rejects.toThrow( + "transient cleanup failure", + ); + cleanupFails = false; + await expect(session.session.call()).resolves.toBe(fakeCall); + expect(connectAttempts).toBe(2); + await session.close(); +}); + test("keeps a failed MCP handshake when its cleanup also fails", async () => { const startup = new Error("handshake failed"); const cleanup = new Error("teardown failed"); diff --git a/tests/evaluation/ciTestSelection.test.ts b/tests/evaluation/ciTestSelection.test.ts index bcb6feaecf..8874e7dfed 100644 --- a/tests/evaluation/ciTestSelection.test.ts +++ b/tests/evaluation/ciTestSelection.test.ts @@ -319,6 +319,34 @@ it("traces shared script modules to their tests and falls back for other consume }); }); +it("traces JavaScript fixture modules transitively through JS and TS helpers", () => { + const sources = { + "tests/fixtures/fakeHopperReplies.mjs": + "export const fakeHopperReply = () => ({});", + "tests/fixtures/fakeHopper.mjs": + 'import { fakeHopperReply } from "./fakeHopperReplies.mjs"; export { fakeHopperReply };', + "tests/boundary/providers/hopper/hopperClient.fixture.ts": + 'export const bridge = new URL("../../../fixtures/fakeHopper.mjs", import.meta.url);', + "tests/boundary/providers/hopper/fakeHopperReplyContracts.test.ts": + 'import { fakeHopperReply } from "../../../fixtures/fakeHopperReplies.mjs"; void fakeHopperReply;', + "tests/boundary/providers/hopper/hopperSocket.test.ts": + 'import { bridge } from "./hopperClient.fixture.js"; void bridge;', + "tests/acceptance/providers/hopperStartupRecovery.test.ts": + 'const path = "tests/fixtures/fakeHopper.mjs"; void path;', + "src/unrelated.test.ts": "", + }; + expect(plan(["tests/fixtures/fakeHopperReplies.mjs"], sources)).toMatchObject( + { + mode: "selected", + files: [ + "tests/acceptance/providers/hopperStartupRecovery.test.ts", + "tests/boundary/providers/hopper/fakeHopperReplyContracts.test.ts", + "tests/boundary/providers/hopper/hopperSocket.test.ts", + ], + }, + ); +}); + it("parses only well-formed plans and derives the selected shard count", () => { const selected = { version: 3, diff --git a/tests/fixtures/localToolsMcp.ts b/tests/fixtures/localToolsMcp.ts index 13d3a59135..0b3ffab3c3 100644 --- a/tests/fixtures/localToolsMcp.ts +++ b/tests/fixtures/localToolsMcp.ts @@ -15,6 +15,7 @@ import { closeVerifierResources } from "../../scripts/lib/verifier-cleanup.mjs"; import { fileURLToPath } from "node:url"; import { afterAll, beforeAll, expect, onTestFinished } from "vitest"; import { createMcpSchemaValidation } from "./mcpSchemaValidation.js"; +import { createSharedMcpSession } from "../support/mcp/sharedMcpSession.js"; /** Default to native tools while preserving explicit caller configuration. */ export const localToolsMcpEnvironment = ( @@ -100,68 +101,20 @@ export async function connectLocalToolsMcp( /** * Lazy file-scoped production stdio MCP client. Connects on first use so - * skipped files pay zero startup cost, and drains cleanups in LIFO order while - * retaining any cleanup that fails. + * skipped files pay zero startup cost, sharing `createSharedMcpSession`'s + * retryable cleanup queue and handshake-failure attribution. */ export const lazyLocalToolsMcp = ( environment: Readonly> = {}, ): (() => Promise>>) => { - type ConnectedLocalToolsMcp = Awaited< - ReturnType - >; - let pending: Promise | undefined; - let closed = false; - const cleanups: Array<() => Promise> = []; - const drain = async (): Promise => { - const failures: unknown[] = []; - const retained: Array<() => Promise> = []; - while (cleanups.length > 0) { - const cleanup = cleanups.pop(); - if (cleanup === undefined) break; - try { - await cleanup(); - } catch (error) { - failures.push(error); - retained.push(cleanup); - } - } - for (const cleanup of retained.reverse()) cleanups.push(cleanup); - if (failures.length > 1) { - throw new AggregateError( - failures, - "File-scoped MCP session cleanup failed", - ); - } - if (failures.length === 1) throw failures[0]; - }; - afterAll(async () => { - closed = true; - if (pending !== undefined) await pending.catch(() => undefined); - await drain(); - }); - return () => { - if (closed) { - return Promise.reject(new Error("File-scoped MCP session is closed")); - } - pending ??= (async () => { - if (cleanups.length > 0) await drain(); - if (closed) throw new Error("File-scoped MCP session is closed"); - try { - const opened = await connectLocalToolsMcp(environment, (cleanup) => { - cleanups.push(cleanup); - }); - if (closed) throw new Error("File-scoped MCP session is closed"); - return opened; - } catch (error) { - if (!closed) { - pending = undefined; - await drain().catch(() => undefined); - } - throw error; - } - })(); - return pending; - }; + const { session, close } = createSharedMcpSession( + async (registerCleanup, isClosed) => { + if (isClosed()) throw new Error("Shared MCP session is closed"); + return connectLocalToolsMcp(environment, registerCleanup); + }, + ); + afterAll(() => close()); + return () => session.call(); }; /** diff --git a/tests/support/cli/cliFixture.ts b/tests/support/cli/cliFixture.ts index f45f983593..f34464b63c 100644 --- a/tests/support/cli/cliFixture.ts +++ b/tests/support/cli/cliFixture.ts @@ -22,6 +22,10 @@ import { createCliCommandSession, type CliCommandSession, } from "./cliCommandSession.js"; +import { + createSharedMcpSession, + type SharedMcpSession as GenericSharedMcpSession, +} from "../mcp/sharedMcpSession.js"; import { REGISTRATION_GUARD_NODE_OPTIONS_ENV } from "./registrationEnvironment.js"; import { terminalStdoutInvocation } from "./terminalStdout.js"; @@ -73,141 +77,13 @@ export interface TestCli { } /** One file-scoped stdio MCP session. */ -export interface SharedMcpSession { - call(): Promise; -} +export type SharedMcpSession = GenericSharedMcpSession; + +export { createSharedMcpSession }; const sharedMcpSessionClosed = (): Error => new Error("Shared MCP session is closed"); -/** Keep the handshake as the caller error and retain the cleanup failure beside it. */ -const startupFailureWithCleanup = ( - startup: unknown, - cleanup: unknown, -): Error => { - const primary = - startup instanceof Error ? startup : new Error(String(startup)); - return new Error(primary.message, { - cause: new AggregateError([primary, cleanup], primary.message, { - cause: primary, - }), - }); -}; - -/** - * Start at most one server for the file. Close waits for startup still in - * flight. A connector that sees `isClosed` can skip the server; a server - * registered after close is still cleaned up. A failed cleanup stays owned, - * and the next call does not open another server until that cleanup succeeds. - */ -export const createSharedMcpSession = ( - connect: ( - registerCleanup: (cleanup: () => Promise) => void, - isClosed: () => boolean, - ) => Promise, -): { session: SharedMcpSession; close(): Promise } => { - let pending: Promise | undefined; - let closed = false; - let closing: Promise | undefined; - let closeGeneration: symbol | undefined; - let cleanupQueue: Promise = Promise.resolve(); - const cleanups: Array<() => Promise> = []; - const drain = (): Promise => { - const run = cleanupQueue - .then( - () => undefined, - () => undefined, - ) - .then(async () => { - const failures: unknown[] = []; - const retained: Array<() => Promise> = []; - while (cleanups.length > 0) { - const cleanup = cleanups.pop(); - if (cleanup === undefined) break; - try { - await cleanup(); - } catch (error) { - failures.push(error); - retained.push(cleanup); - } - } - for (const cleanup of retained.reverse()) cleanups.push(cleanup); - // A reasonless rejection is `undefined` and still owns the cleanup. - if (failures.length > 1) { - throw new AggregateError( - failures, - "Shared MCP session cleanup failed", - ); - } - if (failures.length === 1) throw failures[0]; - }); - cleanupQueue = run; - return run; - }; - const registerCleanup = (cleanup: () => Promise): void => { - cleanups.push(cleanup); - }; - return { - session: { - call: () => { - if (closed) return Promise.reject(sharedMcpSessionClosed()); - pending ??= (async () => { - try { - if (cleanups.length > 0) await drain(); - } catch (cleanupError) { - // Close owns cleanup once shutdown has started, so a failed - // handshake cannot clear the startup close is awaiting. - if (!closed) pending = undefined; - throw cleanupError; - } - if (closed) throw sharedMcpSessionClosed(); - try { - const opened = await connect(registerCleanup, () => closed); - if (closed) throw sharedMcpSessionClosed(); - return opened; - } catch (error) { - if (!closed) { - pending = undefined; - try { - await drain(); - } catch (cleanupError) { - throw startupFailureWithCleanup(error, cleanupError); - } - } - throw error; - } - })(); - return pending; - }, - }, - close: () => { - closed = true; - if (closing !== undefined) return closing; - const starting = pending; - const generation = Symbol("shared-mcp-close"); - closeGeneration = generation; - const attempt = (async () => { - try { - if (starting !== undefined) { - await starting.catch(() => undefined); - } - await drain(); - } catch (error) { - // A failed close stays retryable. Completion is cached only after - // every retained cleanup has succeeded. - if (closeGeneration === generation) { - closing = undefined; - closeGeneration = undefined; - } - throw error; - } - })(); - closing = attempt; - return attempt; - }, - }; -}; - const createFileMcpSession = ( compileCache: string, ): { session: SharedMcpSession; close(): Promise } => diff --git a/tests/support/mcp/sharedMcpSession.ts b/tests/support/mcp/sharedMcpSession.ts new file mode 100644 index 0000000000..e58fc9d107 --- /dev/null +++ b/tests/support/mcp/sharedMcpSession.ts @@ -0,0 +1,135 @@ +/** One file-scoped stdio MCP session. */ +export interface SharedMcpSession { + call(): Promise; +} + +const sharedMcpSessionClosed = (): Error => + new Error("Shared MCP session is closed"); + +/** Keep the handshake as the caller error and retain the cleanup failure beside it. */ +const startupFailureWithCleanup = ( + startup: unknown, + cleanup: unknown, +): Error => { + const primary = + startup instanceof Error ? startup : new Error(String(startup)); + return new Error(primary.message, { + cause: new AggregateError([primary, cleanup], primary.message, { + cause: primary, + }), + }); +}; + +/** + * Start at most one server for the file. Close waits for startup still in + * flight. A connector that sees `isClosed` can skip the server; a server + * registered after close is still cleaned up. A failed cleanup stays owned, + * and the next call does not open another server until that cleanup succeeds. + */ +export const createSharedMcpSession = ( + connect: ( + registerCleanup: (cleanup: () => Promise) => void, + isClosed: () => boolean, + ) => Promise, +): { session: SharedMcpSession; close(): Promise } => { + let pending: Promise | undefined; + let closed = false; + let closing: Promise | undefined; + let closeGeneration: symbol | undefined; + let cleanupQueue: Promise = Promise.resolve(); + const cleanups: Array<() => Promise> = []; + const drain = (): Promise => { + const run = cleanupQueue + .then( + () => undefined, + () => undefined, + ) + .then(async () => { + const failures: unknown[] = []; + const retained: Array<() => Promise> = []; + while (cleanups.length > 0) { + const cleanup = cleanups.pop(); + if (cleanup === undefined) break; + try { + await cleanup(); + } catch (error) { + failures.push(error); + retained.push(cleanup); + } + } + for (const cleanup of retained.reverse()) cleanups.push(cleanup); + // A reasonless rejection is `undefined` and still owns the cleanup. + if (failures.length > 1) { + throw new AggregateError( + failures, + "Shared MCP session cleanup failed", + ); + } + if (failures.length === 1) throw failures[0]; + }); + cleanupQueue = run; + return run; + }; + const registerCleanup = (cleanup: () => Promise): void => { + cleanups.push(cleanup); + }; + return { + session: { + call: () => { + if (closed) return Promise.reject(sharedMcpSessionClosed()); + pending ??= (async () => { + try { + if (cleanups.length > 0) await drain(); + } catch (cleanupError) { + // Close owns cleanup once shutdown has started, so a failed + // handshake cannot clear the startup close is awaiting. + if (!closed) pending = undefined; + throw cleanupError; + } + if (closed) throw sharedMcpSessionClosed(); + try { + const opened = await connect(registerCleanup, () => closed); + if (closed) throw sharedMcpSessionClosed(); + return opened; + } catch (error) { + if (!closed) { + pending = undefined; + try { + await drain(); + } catch (cleanupError) { + throw startupFailureWithCleanup(error, cleanupError); + } + } + throw error; + } + })(); + return pending; + }, + }, + close: () => { + closed = true; + if (closing !== undefined) return closing; + const starting = pending; + const generation = Symbol("shared-mcp-close"); + closeGeneration = generation; + const attempt = (async () => { + try { + if (starting !== undefined) { + await starting.catch(() => undefined); + } + await drain(); + } catch (error) { + // A failed close stays retryable. Completion is cached only after + // every retained cleanup has succeeded. + if (closeGeneration === generation) { + closing = undefined; + closeGeneration = undefined; + } + throw error; + } + })(); + closing = attempt; + return attempt; + }, + }; +};