From 5a2a08a0481a37c94365cfc69175b278f0643e41 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:28:59 +0800 Subject: [PATCH] perf(ci): split lazy runtime chunks, cache Windows typecheck, and reuse test analysis workers (#11827) * perf(runtime): eliminate static imports that defeat lazy provider and workflow chunk splitting * perf(ci): cache Windows typecheck in Turbo, bound official compiler checkers, and reuse portable build cache in reverse-tools * perf(package): batch staging file copies and extract packed metadata in one pass * perf(tests): reuse parked JavaScript analysis workers and eliminate redundant CLI and Node spawns * fix(tests): retain failed cleanup owners across non-isolated files and settle concurrent verifier copies * fix(providers): propagate underlying cleanup release claims through DeferredAnalysisClient --- .github/workflows/ci.yml | 12 +- .github/workflows/release-recovery.yml | 1 + scripts/lib/verify-package-core.d.mts | 5 + scripts/lib/verify-package-core.mjs | 25 ++ scripts/run-typescript.mjs | 23 +- scripts/verify-package-install.mjs | 4 + scripts/verify-package-pack.mjs | 122 +++++----- scripts/verify-repository-skill.mjs | 23 +- scripts/verify-windows-package.mjs | 11 +- src/application/SessionWorkflowEvidence.ts | 5 +- .../binary/DeferredAnalysisClient.test.ts | 37 +++ src/composition/auxiliaryAnalysisProviders.ts | 22 +- src/composition/binary.ts | 8 +- src/providers/AnalysisClientCreation.ts | 20 +- .../exportedFunctionBindingsCli.test.ts | 226 +++++++++++++++--- .../cliBuiltinDocumentTokens.test.ts | 12 +- .../registrationRuntimeEnvironmentCli.test.ts | 50 ++-- .../javascriptApplicationScope.test.ts | 119 +++++++++ .../process-global/curatedWindowsLane.test.ts | 7 +- tests/support/cli/cliCommandSession.ts | 17 +- tests/support/javascriptApplicationScope.ts | 178 ++++++++++++-- 21 files changed, 747 insertions(+), 180 deletions(-) create mode 100644 tests/boundary/javascript/javascriptApplicationScope.test.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 02b46d71de..91380c8c9f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -396,6 +396,8 @@ jobs: id: npm-cache uses: ./.github/actions/npm-cache - run: npm ci + - name: Reuse portable build outputs across runs + uses: ./.github/actions/portable-build-cache - name: Run reverse tools tests with coverage run: npm run test:coverage:reverse-tools - name: Preflight both reverse tools mutation configurations @@ -972,11 +974,6 @@ jobs: with: save: "true" - run: npm ci - - name: Typecheck with the official Windows compiler - env: - GOMAXPROCS: "2" - GOMEMLIMIT: "3GiB" - run: npm run typecheck -- --checkers 2 - name: Restore Windows Turbo build cache id: windows-build-cache uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 @@ -984,6 +981,11 @@ jobs: path: .turbo/cache/ key: rea-windows-build-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('.nvmrc', '.npmrc', 'package-lock.json') }}-${{ github.sha }} restore-keys: rea-windows-build-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('.nvmrc', '.npmrc', 'package-lock.json') }}- + - name: Typecheck with the official Windows compiler + env: + GOMAXPROCS: "2" + GOMEMLIMIT: "3GiB" + run: node scripts/run-turbo.mjs run typecheck - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: windows-native-controls diff --git a/.github/workflows/release-recovery.yml b/.github/workflows/release-recovery.yml index 56a7313879..76e3699498 100644 --- a/.github/workflows/release-recovery.yml +++ b/.github/workflows/release-recovery.yml @@ -32,6 +32,7 @@ jobs: - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: fetch-depth: 0 + filter: blob:none persist-credentials: false - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 with: diff --git a/scripts/lib/verify-package-core.d.mts b/scripts/lib/verify-package-core.d.mts index 88d04b9142..6ed8cb9865 100644 --- a/scripts/lib/verify-package-core.d.mts +++ b/scripts/lib/verify-package-core.d.mts @@ -4,3 +4,8 @@ export function verifyCompleteToolCatalog( options?: Parameters[1], ): Promise; export function pathExists(path: string): Promise; +export function runSettledConcurrent( + items: readonly T[], + concurrency: number, + worker: (item: T) => Promise, +): Promise; diff --git a/scripts/lib/verify-package-core.mjs b/scripts/lib/verify-package-core.mjs index 2f5c65e599..cedb9e4239 100644 --- a/scripts/lib/verify-package-core.mjs +++ b/scripts/lib/verify-package-core.mjs @@ -91,3 +91,28 @@ export const pathExists = async (path) => { throw cause; } }; + +/** + * Run bounded concurrent work, stop taking new items after the first failure, + * and wait for every started worker to settle before propagating failures. + */ +export const runSettledConcurrent = async (items, concurrency, worker) => { + if (items.length === 0) return; + let index = 0; + const failures = []; + await Promise.allSettled( + Array.from({ length: Math.min(concurrency, items.length) }, async () => { + while (failures.length === 0 && index < items.length) { + const current = index++; + try { + await worker(items[current]); + } catch (cause) { + failures.push(cause); + } + } + }), + ); + if (failures.length === 1) throw failures[0]; + if (failures.length > 1) + throw new AggregateError(failures, "Concurrent verifier tasks failed"); +}; diff --git a/scripts/run-typescript.mjs b/scripts/run-typescript.mjs index 1fc0c2351a..e0c282c9f2 100644 --- a/scripts/run-typescript.mjs +++ b/scripts/run-typescript.mjs @@ -55,18 +55,17 @@ const main = async () => { throw new Error( "Compiler identity is missing or stale. Run Turbo through scripts/run-turbo.mjs.", ); - if (compiler.name === "tsrs") { - const args = process.argv.slice(2); - // Rust consumes --maxMemory once; duplicating it becomes an unknown option. - // Preserve explicit resource choices rather than injecting another value. - const defaults = [ - ["--checkers", String(Math.min(2, availableParallelism()))], - ["--maxMemory", "3G"], - ].flatMap(([option, value]) => - args.includes(option) ? [] : [option, value], - ); - process.argv.splice(2, 0, ...defaults); - } + const args = process.argv.slice(2); + // Both tsrs and TypeScript 7 (tsgo) accept --checkers; only tsrs accepts + // --maxMemory (TypeScript 7 uses GOMEMLIMIT). Preserve explicit resource + // choices rather than injecting another value. + const defaults = [ + ["--checkers", String(Math.min(2, availableParallelism()))], + ...(compiler.name === "tsrs" ? [["--maxMemory", "3G"]] : []), + ].flatMap(([option, value]) => + args.includes(option) ? [] : [option, value], + ); + process.argv.splice(2, 0, ...defaults); // Delegate execution, diagnostics, status and signals to the package's launcher. // An unavailable binary on a supported host must fail rather than hide drift. await import(pathToFileURL(compiler.launcher).href); diff --git a/scripts/verify-package-install.mjs b/scripts/verify-package-install.mjs index 27f3315c23..7616a4bcb8 100644 --- a/scripts/verify-package-install.mjs +++ b/scripts/verify-package-install.mjs @@ -16,6 +16,8 @@ export async function verifyPackageInstall({ "--global", "--ignore-scripts", "--omit=dev", + "--no-audit", + "--no-fund", "--install-strategy=nested", "--prefix", prefix, @@ -61,6 +63,8 @@ export async function verifyPackageInstall({ "--global", "--ignore-scripts", "--omit=optional", + "--no-audit", + "--no-fund", "--prefix", noOptionalPrefix, tarball, diff --git a/scripts/verify-package-pack.mjs b/scripts/verify-package-pack.mjs index ac9b5ce2d7..5a3d7d6e24 100644 --- a/scripts/verify-package-pack.mjs +++ b/scripts/verify-package-pack.mjs @@ -5,6 +5,7 @@ import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; import { DEFAULT_CATALOG_IDENTITY } from "../dist/contracts/catalog/catalogIdentity.js"; import { parseTarListing } from "./lib/verify-package-listing.mjs"; +import { runSettledConcurrent } from "./lib/verify-package-core.mjs"; import { verifyPackedBridge } from "./verify-packed-bridge.mjs"; import { pluginPackageMetadata } from "./lib/plugin-package.mjs"; @@ -16,16 +17,19 @@ const { parseSkillManifestMetadata } = await importVerifierSource( /** Create a tarball and assert production packaging constraints. */ export async function verifyPackagePack({ root, workspace, commands }) { const packDirectory = join(workspace, "pack"); - await mkdir(packDirectory); const stagingDirectory = join(workspace, "plugin-package"); - await mkdir(stagingDirectory); + await runSettledConcurrent( + [packDirectory, stagingDirectory], + 2, + (directory) => mkdir(directory), + ); const [{ files }] = JSON.parse( (await commands.execNpm(["pack", "--dry-run", "--json"], { cwd: root })) .stdout, ); // Reuse npm's file selection, then project metadata in an owned directory. // Source files and the development lock are never rewritten by packaging. - for (const { path } of files) { + const copyPlan = files.map(({ path }) => { const source = resolve(root, path); const contained = relative(root, source); if ( @@ -34,10 +38,16 @@ export async function verifyPackagePack({ root, workspace, commands }) { isAbsolute(contained) ) throw new Error(`npm selected a path outside the package: ${path}`); - const destination = join(stagingDirectory, contained); - await mkdir(dirname(destination), { recursive: true }); - await copyFile(source, destination); - } + return { source, destination: join(stagingDirectory, contained) }; + }); + await runSettledConcurrent( + [...new Set(copyPlan.map(({ destination }) => dirname(destination)))], + 32, + (directory) => mkdir(directory, { recursive: true }), + ); + await runSettledConcurrent(copyPlan, 32, ({ source, destination }) => + copyFile(source, destination), + ); const { packageJson, shrinkwrap } = pluginPackageMetadata( JSON.parse(await readFile(join(root, "package.json"), "utf8")), JSON.parse(await readFile(join(root, "package-lock.json"), "utf8")), @@ -123,24 +133,42 @@ export async function verifyPackagePack({ root, workspace, commands }) { ) ).stdout.trim(), ); - const packedFiles = parseTarListing( - (await commands.exec("tar", ["-tf", tarball])).stdout, - ); - const packedManifest = JSON.parse( - (await commands.exec("tar", ["-xOf", tarball, "package/package.json"])) - .stdout, - ); + const listingOutput = await commands.exec("tar", ["-tf", tarball]); + const packedFiles = parseTarListing(listingOutput.stdout); + const packedFileSet = new Set(packedFiles); + const metadataDirectory = join(workspace, "packed-metadata"); + await mkdir(metadataDirectory); + const metadataCandidates = [ + "package/package.json", + "package/npm-shrinkwrap.json", + "package/node_modules/@lydell/node-pty/package.json", + "package/skills/reverse-engineer-anything/SKILL.md", + "package/.claude-plugin/plugin.json", + "package/.codex-plugin/plugin.json", + "package/.codex-plugin/mcp.json", + "package/.cursor-plugin/plugin.json", + "package/mcp.json", + ].filter((archivePath) => packedFileSet.has(archivePath)); + if (metadataCandidates.length > 0) { + await commands.exec("tar", [ + "-xzf", + tarball, + "-C", + metadataDirectory, + ...metadataCandidates, + ]); + } + const readPackedText = async (archivePath) => { + if (!packedFileSet.has(archivePath)) + throw new Error(`package omitted ${archivePath}`); + return readFile(join(metadataDirectory, archivePath), "utf8"); + }; + const readPackedJson = async (archivePath) => + JSON.parse(await readPackedText(archivePath)); + const packedManifest = await readPackedJson("package/package.json"); if (packedManifest.mcpName !== "io.github.morluto/rea") throw new Error("package did not retain its MCP Registry ownership marker"); - const packedLock = JSON.parse( - ( - await commands.exec("tar", [ - "-xOf", - tarball, - "package/npm-shrinkwrap.json", - ]) - ).stdout, - ); + const packedLock = await readPackedJson("package/npm-shrinkwrap.json"); if ( packedManifest.overrides !== undefined || packedManifest.devDependencies !== undefined || @@ -162,14 +190,8 @@ export async function verifyPackagePack({ root, workspace, commands }) { throw new Error( `Plugin runtime dependency is not bundled: ${dependency}`, ); - const pty = JSON.parse( - ( - await commands.exec("tar", [ - "-xOf", - tarball, - "package/node_modules/@lydell/node-pty/package.json", - ]) - ).stdout, + const pty = await readPackedJson( + "package/node_modules/@lydell/node-pty/package.json", ); for (const dependency of Object.keys(pty.optionalDependencies)) if ( @@ -203,13 +225,9 @@ export async function verifyPackagePack({ root, workspace, commands }) { ); if (packedFiles.some((path) => path.startsWith("package/.cache/"))) throw new Error("package included development cache or test metadata"); - const skill = ( - await commands.exec("tar", [ - "-xOf", - tarball, - "package/skills/reverse-engineer-anything/SKILL.md", - ]) - ).stdout; + const skill = await readPackedText( + "package/skills/reverse-engineer-anything/SKILL.md", + ); const skillMetadata = parseSkillManifestMetadata(skill); if ( !skillMetadata.ok || @@ -224,19 +242,9 @@ export async function verifyPackagePack({ root, workspace, commands }) { ".claude-plugin/plugin.json", ".codex-plugin/plugin.json", ]) { - const manifest = JSON.parse( - (await commands.exec("tar", ["-xOf", tarball, `package/${path}`])).stdout, - ); + const manifest = await readPackedJson(`package/${path}`); const serverConfig = path.startsWith(".codex") - ? JSON.parse( - ( - await commands.exec("tar", [ - "-xOf", - tarball, - "package/.codex-plugin/mcp.json", - ]) - ).stdout, - ) + ? await readPackedJson("package/.codex-plugin/mcp.json") : manifest; const expectedArgs = [ `${path.startsWith(".claude") ? "${CLAUDE_PLUGIN_ROOT}/" : "./"}scripts/plugin-mcp-launcher.mjs`, @@ -267,18 +275,10 @@ export async function verifyPackagePack({ root, workspace, commands }) { ) throw new Error(`Packaged plugin ${path} is missing its shared icon`); } - const cursorManifest = JSON.parse( - ( - await commands.exec("tar", [ - "-xOf", - tarball, - "package/.cursor-plugin/plugin.json", - ]) - ).stdout, - ); - const cursorServer = JSON.parse( - (await commands.exec("tar", ["-xOf", tarball, "package/mcp.json"])).stdout, + const cursorManifest = await readPackedJson( + "package/.cursor-plugin/plugin.json", ); + const cursorServer = await readPackedJson("package/mcp.json"); const cursorLogo = String(cursorManifest.logo ?? "").replace(/^\.\//u, ""); const cursorSkill = "package/.cursor-plugin/skills/reverse-engineer-anything/SKILL.md"; diff --git a/scripts/verify-repository-skill.mjs b/scripts/verify-repository-skill.mjs index 62960ceded..e3a8838066 100644 --- a/scripts/verify-repository-skill.mjs +++ b/scripts/verify-repository-skill.mjs @@ -1,7 +1,10 @@ import { cp, mkdir, readFile, readdir } from "node:fs/promises"; import { dirname, join, relative } from "node:path"; -import { pathExists } from "./lib/verify-package-core.mjs"; +import { + pathExists, + runSettledConcurrent, +} from "./lib/verify-package-core.mjs"; /** Install the separately locked Skills CLI so verification resolves no registry metadata. */ const lockedSkillsCli = async (commands, root, workspace) => { @@ -37,22 +40,30 @@ export async function verifyRepositorySkill({ root, workspace, commands }) { ).stdout .split("\0") .filter(Boolean); + const directories = new Set(); + const filesToCopy = []; for (const entry of tracked) { const mode = entry.slice(0, 6); const path = entry.slice(7); const destination = join(snapshot, path); // A GitHub clone without submodule initialization has only these directories. if (mode === "160000") { - await mkdir(destination, { recursive: true }); + directories.add(destination); continue; } - await mkdir(dirname(destination), { recursive: true }); - await cp(join(root, path), destination, { verbatimSymlinks: true }); + directories.add(dirname(destination)); + filesToCopy.push({ source: join(root, path), destination }); } + await runSettledConcurrent( + [consumer, home, ...directories], + 32, + (directory) => mkdir(directory, { recursive: true }), + ); + await runSettledConcurrent(filesToCopy, 32, ({ source, destination }) => + cp(source, destination, { verbatimSymlinks: true }), + ); if (await pathExists(join(snapshot, "skills"))) throw new Error("repository skill check must run without generated skills"); - await mkdir(consumer, { recursive: true }); - await mkdir(home, { recursive: true }); const { version: skillsVersion, entry } = await lockedSkillsCli( commands, root, diff --git a/scripts/verify-windows-package.mjs b/scripts/verify-windows-package.mjs index 532fcaf02c..cac0d5d4dd 100644 --- a/scripts/verify-windows-package.mjs +++ b/scripts/verify-windows-package.mjs @@ -70,7 +70,16 @@ try { const tarball = join(workspace, packageResult.filename); await access(tarball); await commands.execNpm( - ["install", "--no-package-lock", "--no-save", "--prefix", prefix, tarball], + [ + "install", + "--no-package-lock", + "--no-save", + "--no-audit", + "--no-fund", + "--prefix", + prefix, + tarball, + ], { cwd: workspace, windowsHide: true }, ); const entry = join( diff --git a/src/application/SessionWorkflowEvidence.ts b/src/application/SessionWorkflowEvidence.ts index 3bc721bc3c..14a7e97a15 100644 --- a/src/application/SessionWorkflowEvidence.ts +++ b/src/application/SessionWorkflowEvidence.ts @@ -1,7 +1,5 @@ import type { BinarySessionPort } from "./binary/BinarySessionPort.js"; -import { EnhancedTools } from "./EnhancedTools.js"; import type { ValidatedEnhancedCall } from "./EnhancedToolTypes.js"; -import { executeFunctionAnalysisEvidence } from "./FunctionAnalysisEvidence.js"; import { retainAnalysisResultSources } from "./investigation/recordEvidenceSources.js"; import { composedLimitationBudget, @@ -95,6 +93,7 @@ export const executeSessionWorkflowEvidence = async ( ? undefined : (session.analysisProfile() ?? session.analysisProfile(name)), }; + const { EnhancedTools } = await import("./EnhancedTools.js"); const executed = await executeComposedWorkflow( analysis, (observed) => new EnhancedTools(observed).executeValidated(call, signal), @@ -155,6 +154,8 @@ export const executeSessionFunctionAnalysis = async ( session: WorkflowSession | undefined, request: WorkflowRequest, ): Promise> => { + const { executeFunctionAnalysisEvidence } = + await import("./FunctionAnalysisEvidence.js"); const analyzed = retainAnalysisResultSources( "analyze_function", session === undefined diff --git a/src/application/binary/DeferredAnalysisClient.test.ts b/src/application/binary/DeferredAnalysisClient.test.ts index dbad09f781..3b12908b30 100644 --- a/src/application/binary/DeferredAnalysisClient.test.ts +++ b/src/application/binary/DeferredAnalysisClient.test.ts @@ -583,3 +583,40 @@ it("retries only unresolved prerequisite and native owners across mixed cleanup await client.close(); } }); + +it("settles deferred client close failure when the underlying owned resource lease releases", async () => { + let released = false; + const underlyingOwner = { + get cleanupResources() { + return released ? [] : ["owned-descriptor"]; + }, + }; + const underlyingClaim = { + get cleanupReleased() { + return released; + }, + }; + const client = new DeferredAnalysisClient( + async () => + child(async () => { + if (released) return ok(null); + return err( + new ProviderCleanupError(identity.id, ["owned-descriptor"], { + reason: "descriptor close denied", + }) + .bindCleanupResourceOwner(underlyingOwner) + .bindCleanupReleaseClaim(underlyingClaim), + ); + }), + identity.id, + ); + const executed = await client.execute("analyze_function", {}); + expect(executed.ok).toBe(true); + const closed = await client.close(); + if (closed.ok) throw new Error("Expected retained close failure"); + expect(closed.error.cleanupIncomplete).toBe(true); + expect(closed.error.cleanupResources).toEqual(["owned-descriptor"]); + released = true; + expect(closed.error.cleanupIncomplete).toBe(false); + expect(closed.error.cleanupResources).toEqual([]); +}); diff --git a/src/composition/auxiliaryAnalysisProviders.ts b/src/composition/auxiliaryAnalysisProviders.ts index 7c91aa74e5..4789179c01 100644 --- a/src/composition/auxiliaryAnalysisProviders.ts +++ b/src/composition/auxiliaryAnalysisProviders.ts @@ -15,6 +15,18 @@ type AuxiliaryProviderDeclaration = ConstructorParameters< typeof LazyAnalysisProvider >[0]; +/** Declare the lazy execution-free .NET static provider wiring. */ +export const managedStaticProviderDeclaration = + (): AuxiliaryProviderDeclaration => ({ + identity: MANAGED_STATIC_PROVIDER, + capabilities: managedStaticCapabilities(), + load: async () => { + const { ManagedStaticProvider } = + await import("../dotnet/ManagedStaticProvider.js"); + return new ManagedStaticProvider(); + }, + }); + /** Wire the three existing auxiliary AnalysisProvider implementations lazily. */ export const auxiliaryAnalysisProviderDeclarations = ( selectedEnvironment: Readonly, @@ -41,14 +53,6 @@ export const auxiliaryAnalysisProviderDeclarations = ( return new NativeMacOSProvider(environment, undefined, platform); }, }, - { - identity: MANAGED_STATIC_PROVIDER, - capabilities: managedStaticCapabilities(), - load: async () => { - const { ManagedStaticProvider } = - await import("../dotnet/ManagedStaticProvider.js"); - return new ManagedStaticProvider(); - }, - }, + managedStaticProviderDeclaration(), ]; }; diff --git a/src/composition/binary.ts b/src/composition/binary.ts index 79d7754d9d..caea5f8345 100644 --- a/src/composition/binary.ts +++ b/src/composition/binary.ts @@ -10,12 +10,14 @@ import { createIdaMcpConnection } from "../ida/IdaMcpConnection.js"; import { IdaProvider } from "../ida/IdaProvider.js"; import { KunaProvider } from "../kuna/KunaProvider.js"; import type { Logger } from "pino"; -import { auxiliaryAnalysisProviderDeclarations } from "./auxiliaryAnalysisProviders.js"; +import { + auxiliaryAnalysisProviderDeclarations, + managedStaticProviderDeclaration, +} from "./auxiliaryAnalysisProviders.js"; import { AnalysisProviderRegistry } from "../application/binary/AnalysisProviderRegistry.js"; import { composeBinarySession } from "../application/binary/BinarySessionComposition.js"; import { LINUX_HOPPER_LAUNCHER_VERIFICATION } from "../application/LinuxHopper.js"; import { LazyAnalysisProvider } from "../application/binary/LazyAnalysisProvider.js"; -import { ManagedStaticProvider } from "../dotnet/ManagedStaticProvider.js"; import { HOST_PLATFORM } from "../platform/HostPlatform.js"; import { combineBundleResourceClients } from "../application/binary/BundleResourceSession.js"; import { @@ -147,7 +149,7 @@ const createBundleResourceFactory = ( /** Compose an execution-free managed session without native provider selection. */ export const createManagedBinarySession = (): BinarySession => composeBinarySession(new AnalysisProviderRegistry([]), [ - new ManagedStaticProvider(), + new LazyAnalysisProvider(managedStaticProviderDeclaration()), ]); /** Compose the artifact operations without candidates for deep analysis engines. */ diff --git a/src/providers/AnalysisClientCreation.ts b/src/providers/AnalysisClientCreation.ts index 333c33e498..f5bdbacdd0 100644 --- a/src/providers/AnalysisClientCreation.ts +++ b/src/providers/AnalysisClientCreation.ts @@ -258,6 +258,7 @@ export class AnalysisClientCreation { await this.#clientLease.release(); return ok(null); } catch (cause: unknown) { + const clientLease = this.#clientLease; const failure = new ProviderCleanupError( this.providerId, cause instanceof AnalysisError && cause.cleanupResources.length > 0 @@ -272,8 +273,23 @@ export class AnalysisClientCreation { }, { cause, operation: "create_analysis_client" }, ); - failure.bindCleanupResourceOwner(this.#clientLease); - this.#clientLease.bindCleanupError(failure); + if (cause instanceof AnalysisError && cause.hasCleanupOwner) { + failure.bindCleanupResourceOwner({ + get cleanupResources() { + return clientLease.released || !cause.cleanupIncomplete + ? [] + : cause.cleanupResources; + }, + }); + failure.bindCleanupReleaseClaim({ + get cleanupReleased() { + return !cause.cleanupIncomplete; + }, + }); + } else { + failure.bindCleanupResourceOwner(clientLease); + } + clientLease.bindCleanupError(failure); return err(failure); } } diff --git a/tests/acceptance/applications/exportedFunctionBindingsCli.test.ts b/tests/acceptance/applications/exportedFunctionBindingsCli.test.ts index 9c7b010972..d3b3834ff3 100644 --- a/tests/acceptance/applications/exportedFunctionBindingsCli.test.ts +++ b/tests/acceptance/applications/exportedFunctionBindingsCli.test.ts @@ -1,14 +1,14 @@ -import { execFile } from "node:child_process"; +import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process"; import { INLINE_EVIDENCE_MCP_ENVIRONMENT } from "../../../scripts/lib/mcp-verification-environment.mjs"; import { createHash } from "node:crypto"; import { mkdir, writeFile } from "node:fs/promises"; import { createRequire } from "node:module"; import { join, resolve } from "node:path"; -import { promisify } from "node:util"; +import { pathToFileURL } from "node:url"; import { Client } from "@modelcontextprotocol/client"; import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; -import { afterAll, describe, expect, it } from "vitest"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { z } from "zod"; import { closeVerifierResources } from "../../../scripts/lib/verifier-cleanup.mjs"; @@ -17,11 +17,19 @@ import { javaScriptExportShapeComparisonResultSchema } from "../../../src/domain import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { createCliCommandSession, + stopOwnedChildProcess, type CliCommandSession, } from "../../support/cli/cliCommandSession.js"; +import { + createCliSessionFrameReader, + encodeCliSessionFrame, +} from "../../support/cli/cliCommandSessionFrames.mjs"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; +import { + createTestWorkspace, + removeTestWorkspace, +} from "../../support/workspace/workspaceFixture.js"; -const execute = promisify(execFile); const requireFixture = createRequire(import.meta.url); const comparisonEnvelope = z.object({ normalized_result: javaScriptExportShapeComparisonResultSchema, @@ -30,6 +38,33 @@ const applicationEnvelope = z.object({ normalized_result: javascriptApplicationAnalysisResultSchema, }); let cliSession: CliCommandSession | undefined; +let compileCacheRoot = ""; +let compileCacheWorkspace = ""; +interface RuntimeExportWorker { + readonly child: ChildProcessWithoutNullStreams; + readonly invoke: ( + entries: readonly (readonly [path: string, exportName: string])[], + ) => Promise; +} +let activeRuntimeWorker: RuntimeExportWorker | undefined; +const ownedRuntimeWorkers = new Set(); +let runtimeQueue: Promise = Promise.resolve(); + +const closeOwnedRuntimeWorkers = async (): Promise => { + activeRuntimeWorker = undefined; + const results = await Promise.allSettled( + [...ownedRuntimeWorkers].map(async (child) => { + await stopOwnedChildProcess(child, 1_000, "runtime export worker"); + ownedRuntimeWorkers.delete(child); + }), + ); + const failures = results.flatMap((result) => + result.status === "rejected" ? [result.reason] : [], + ); + if (failures.length === 1) throw failures[0]; + if (failures.length > 1) + throw new AggregateError(failures, "Runtime export worker cleanup failed"); +}; // Source semantics use one real stdio MCP session. CLI parity uses the shared // file-scoped session, including its worker rotation and cleanup policy. @@ -41,6 +76,13 @@ let mcpConnection: } | undefined; +beforeAll(async () => { + const workspace = await createTestWorkspace("rea-exported-bindings-cache-"); + compileCacheWorkspace = workspace.root; + compileCacheRoot = workspace.path("compile-cache"); + await mkdir(compileCacheRoot, { recursive: true }); +}); + afterAll(async () => { if (mcpConnection !== undefined) { try { @@ -59,6 +101,11 @@ afterAll(async () => { async () => { await cliSession?.close(); }, + closeOwnedRuntimeWorkers, + async () => { + if (compileCacheWorkspace !== "") + await removeTestWorkspace(compileCacheWorkspace); + }, ]); }); @@ -1224,9 +1271,13 @@ const runCli = async (args: readonly string[]): Promise => { requestTimeoutMs: 20_000, }); const environment = Object.fromEntries( - Object.entries({ ...process.env, REA_LOG_LEVEL: "silent" }).filter( - (entry): entry is [string, string] => entry[1] !== undefined, - ), + Object.entries({ + ...process.env, + REA_LOG_LEVEL: "silent", + ...(compileCacheRoot === "" || process.env.NODE_V8_COVERAGE !== undefined + ? {} + : { NODE_COMPILE_CACHE: compileCacheRoot }), + }).filter((entry): entry is [string, string] => entry[1] !== undefined), ); const result = await cliSession.run({ arguments: args, @@ -1252,10 +1303,10 @@ const analyzeVersions = async ( writeFile(leftPath, source(1)), writeFile(rightPath, source(2)), ]); - const [left, right] = await Promise.all([ - analyzeThroughMcp(leftRoot), - analyzeThroughMcp(rightRoot), - ]); + // Reuse the MCP session's single parked JavaScript worker instead of racing + // two concurrent analyses that force a cold ephemeral worker per fixture. + const left = await analyzeThroughMcp(leftRoot); + const right = await analyzeThroughMcp(rightRoot); return { root, leftPath, @@ -1327,6 +1378,10 @@ const getMcpClient = async (): Promise => { PATH: process.env.PATH ?? "", REA_LOG_LEVEL: "silent", ...INLINE_EVIDENCE_MCP_ENVIRONMENT, + ...(compileCacheRoot === "" || + process.env.NODE_V8_COVERAGE !== undefined + ? {} + : { NODE_COMPILE_CACHE: compileCacheRoot }), }, stderr: "pipe", }); @@ -1348,27 +1403,138 @@ const getMcpClient = async (): Promise => { return mcpConnection.client; }; -const runtimeExports = async ( +const runtimeReplySchema = z.object({ + id: z.number(), + ok: z.boolean(), + results: z.array(z.object({ value: z.unknown().optional() })).optional(), + error: z.string().optional(), +}); + +const startRuntimeWorker = (): RuntimeExportWorker => { + const framesUrl = pathToFileURL( + resolve("tests/support/cli/cliCommandSessionFrames.mjs"), + ).href; + const child = spawn( + process.execPath, + [ + "--input-type=module", + "-e", + `import { pathToFileURL } from 'node:url'; + import { createCliSessionFrameReader, encodeCliSessionFrame } from ${JSON.stringify(framesUrl)}; + const writeFrameStdout = process.stdout.write.bind(process.stdout); + process.stdout.write = () => true; + const send = (payload) => new Promise((resolveSend, rejectSend) => { + writeFrameStdout(encodeCliSessionFrame(payload), (err) => err ? rejectSend(err) : resolveSend()); + }); + let chain = Promise.resolve(); + const reader = createCliSessionFrameReader((request) => { + chain = chain.then(async () => { + const { id, entries } = request; + try { + const results = []; + for (const [path, exportName] of entries) { + const module = await import(pathToFileURL(path).href); + const value = module[exportName]; + results.push({ value: typeof value === 'function' ? value() : value }); + } + await send({ id, ok: true, results }); + } catch (error) { + await send({ id, ok: false, error: error instanceof Error ? error.stack ?? error.message : String(error) }); + } + }); + }); + process.stdin.on('data', (chunk) => reader(chunk));`, + ], + { + cwd: process.cwd(), + stdio: ["pipe", "pipe", "pipe"], + }, + ); + ownedRuntimeWorkers.add(child); + let nextId = 1; + let brokenCause: unknown; + const pending = new Map< + number, + { resolve: (value: unknown[]) => void; reject: (error: unknown) => void } + >(); + let workerInstance: RuntimeExportWorker | undefined; + const retireWorker = (cause: unknown) => { + brokenCause ??= cause; + if (activeRuntimeWorker === workerInstance) activeRuntimeWorker = undefined; + for (const handler of pending.values()) handler.reject(brokenCause); + pending.clear(); + }; + const reader = createCliSessionFrameReader((raw: unknown) => { + const parsed = runtimeReplySchema.parse(raw); + const handler = pending.get(parsed.id); + if (handler === undefined) { + throw new Error( + `Unexpected runtime export reply id ${String(parsed.id)}`, + ); + } + pending.delete(parsed.id); + if (!parsed.ok) { + handler.reject(new Error(parsed.error ?? "runtime worker failed")); + return; + } + handler.resolve((parsed.results ?? []).map(({ value }) => value)); + }); + child.stdout.on("data", (chunk: Buffer) => { + try { + reader(chunk); + } catch (cause) { + retireWorker(cause); + } + }); + child.stderr.on("data", () => undefined); + child.on("error", retireWorker); + child.on("exit", (code, signal) => { + retireWorker( + new Error( + `runtime worker exited (${String(code ?? signal ?? "unknown")})`, + ), + ); + }); + workerInstance = { + child, + invoke: (entries) => + new Promise((resolveRun, rejectRun) => { + if (brokenCause !== undefined) { + rejectRun(brokenCause); + return; + } + const id = nextId++; + pending.set(id, { resolve: resolveRun, reject: rejectRun }); + child.stdin.write(encodeCliSessionFrame({ id, entries }), (error) => { + if (error) { + pending.delete(id); + retireWorker(error); + rejectRun(error); + } + }); + }), + }; + return workerInstance; +}; + +const runtimeExports = ( entries: readonly (readonly [path: string, exportName: string])[], ): Promise => { - const { stdout } = await execute(process.execPath, [ - "--input-type=module", - "-e", - `import { pathToFileURL } from 'node:url'; - const results = []; - for (const [path, exportName] of JSON.parse(process.argv[1])) { - const module = await import(pathToFileURL(path).href); - const value = module[exportName]; - results.push({ value: typeof value === 'function' ? value() : value }); - } - console.log(JSON.stringify(results));`, - JSON.stringify(entries), - ]); - return z - .array(z.object({ value: z.unknown().optional() })) - .length(entries.length) - .parse(JSON.parse(stdout)) - .map(({ value }) => value); + const run = async (): Promise => { + if (activeRuntimeWorker === undefined && ownedRuntimeWorkers.size > 0) { + await closeOwnedRuntimeWorkers(); + } + const worker = (activeRuntimeWorker ??= startRuntimeWorker()); + const values = await worker.invoke(entries); + expect(values).toHaveLength(entries.length); + return values; + }; + const scheduled = runtimeQueue.then(run); + runtimeQueue = scheduled.then( + () => undefined, + () => undefined, + ); + return scheduled; }; const runtimeExport = async ( diff --git a/tests/acceptance/cliBuiltinDocumentTokens.test.ts b/tests/acceptance/cliBuiltinDocumentTokens.test.ts index 61c27bf2f5..1a7d8c327f 100644 --- a/tests/acceptance/cliBuiltinDocumentTokens.test.ts +++ b/tests/acceptance/cliBuiltinDocumentTokens.test.ts @@ -13,18 +13,14 @@ workspaceCliTest( arguments_.join(" ") === "--llms-full --token-limit 1"; const result = await cli.run({ ...invocation, - freshProcess: !( - !freshTokenCount && - !freshTokenLimit && - (arguments_.includes("--token-count") || - arguments_.includes("--token-limit")) - ), + freshProcess: freshTokenCount || freshTokenLimit, }); expect(result.exitCode, result.stderr).toBe(0); expect(result.stderr).toBe(""); return result.stdout; }; + let llmsFullDocument = ""; for (const args of [ ["--llms"], ["--llms-full"], @@ -33,12 +29,14 @@ workspaceCliTest( ["inspect-devicetree", "--schema", "--format", "json"], ]) { const document = await run(...args); + if (args.length === 1 && args[0] === "--llms-full") + llmsFullDocument = document; expect(await run(...args, "--token-count")).toBe( `${estimateTokenCount(document)}\n`, ); } - const document = await run("--llms-full"); + const document = llmsFullDocument; const page = await run("--llms-full", "--token-limit", "1"); expect(page.length).toBeLessThan(document.length); expect(page).toMatch(/\[truncated: showing tokens 0–1 of \d+\]\n$/); diff --git a/tests/acceptance/setup/registrationRuntimeEnvironmentCli.test.ts b/tests/acceptance/setup/registrationRuntimeEnvironmentCli.test.ts index d524ce09d2..6ffcdc29fd 100644 --- a/tests/acceptance/setup/registrationRuntimeEnvironmentCli.test.ts +++ b/tests/acceptance/setup/registrationRuntimeEnvironmentCli.test.ts @@ -1,6 +1,13 @@ -import { access, mkdir, readFile, writeFile } from "node:fs/promises"; +import { + access, + mkdir, + readdir, + readFile, + rm, + writeFile, +} from "node:fs/promises"; import { dirname, join, resolve } from "node:path"; -import { expect } from "vitest"; +import { afterAll, beforeAll, beforeEach, expect } from "vitest"; import { Scalar, stringify } from "yaml"; import { @@ -8,8 +15,29 @@ import { parseClientConfiguration, clientRegistrationEntry, } from "../../../src/application/ClientConfigurationDocument.js"; -import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; +import { + createTestWorkspace, + removeTestWorkspace, +} from "../../support/workspace/workspaceFixture.js"; + +let home = ""; + +beforeAll(async () => { + home = (await createTestWorkspace("rea-registration-runtime-")).root; +}); + +beforeEach(async () => { + await Promise.all( + (await readdir(home)).map((entry) => + rm(join(home, entry), { recursive: true, force: true }), + ), + ); +}); + +afterAll(async () => { + if (home !== "") await removeTestWorkspace(home); +}); // Registration configuration tests must not discover installed providers or // plan provider-environment edits based on the machine running the suite. @@ -22,7 +50,6 @@ const missingHopperAndGhidraEnvironment = (home: string) => ({ cliTest( "retains selected MCP settings and unavailable provider paths in a new registration", async ({ cli }) => { - const home = await createTestTempDirectory("rea-selected-registration-"); const config = join(home, ".qwen/settings.json"); const selected = { HOPPER_LAUNCHER_PATH: join(home, "missing-hopper"), @@ -144,7 +171,6 @@ cliTest( cliTest( "persists, updates and validates tool-family scopes without losing client configuration", async ({ cli }) => { - const home = await createTestTempDirectory("rea-family-registration-"); const config = join(home, ".qwen/settings.json"); const foreign = { command: "foreign-tool", args: ["keep"] }; const original = JSON.stringify({ mcpServers: { foreign } }); @@ -212,7 +238,6 @@ cliTest( cliTest.skipIf(process.platform !== "win32")( "rejects lowercase Windows provider settings in doctor and setup without writing", async ({ cli }) => { - const home = await createTestTempDirectory("rea-registration-case-"); const config = join(home, ".cursor/mcp.json"); await mkdir(dirname(config), { recursive: true }); const original = JSON.stringify({ @@ -329,7 +354,6 @@ cliTest.for([ ] as const)( "refuses a NUL environment $position in $client before setup writes", async (scenario, { cli }) => { - const home = await createTestTempDirectory("rea-registration-nul-"); const config = join(home, scenario.path); await mkdir(dirname(config), { recursive: true }); const environment = { @@ -448,7 +472,6 @@ for (const scenario of scenarios) cliTest( `setup and doctor reject ${scenario.client} overrides rejected by MCP startup`, async ({ cli }) => { - const home = await createTestTempDirectory("rea-registration-runtime-"); const config = join(home, scenario.path); await mkdir(dirname(config), { recursive: true }); const environment = { @@ -570,7 +593,6 @@ for (const scenario of scenarios) cliTest( "OpenCode CLI validates dependent settings after merging JSON and JSONC", async ({ cli }) => { - const home = await createTestTempDirectory("rea-layered-runtime-cli-"); const directory = join(home, ".config/opencode"); await mkdir(directory, { recursive: true }); const lower = join(directory, "opencode.json"); @@ -661,9 +683,6 @@ cliTest( cliTest( "public setup propagates selected provider deadlines and input profile to Codex and Kimi", async ({ cli }) => { - const home = await createTestTempDirectory( - "rea-selected-registration-env-", - ); const codexHome = join(home, "codex"); const kimiHome = join(home, "kimi"); await Promise.all( @@ -776,9 +795,6 @@ for (const [client, provider] of [ cliTest( `public ${client} setup and doctor use the effective IDA registration request budget with ${provider} selection`, async ({ cli }) => { - const home = await createTestTempDirectory( - "rea-ida-registration-budget-", - ); const clientHome = join(home, client); await mkdir(clientHome, { recursive: true }); const idaConfig = join(home, "ida.json"); @@ -912,9 +928,6 @@ for (const invalidSetting of [ cliTest( `setup rejects malformed selected ${invalidSetting.setting} before writing`, async ({ cli }) => { - const home = await createTestTempDirectory( - "rea-invalid-selected-registration-env-", - ); const kimiHome = join(home, "kimi"); await mkdir(kimiHome, { recursive: true }); const config = join(kimiHome, "mcp.json"); @@ -958,7 +971,6 @@ for (const invalidSetting of [ cliTest( "public VS Code setup registers in VSCODE_APPDATA without changing the default configuration", async ({ cli }) => { - const home = await createTestTempDirectory("rea-vscode-appdata-cli-"); const selectedData = join(home, "selected data"); const config = join(selectedData, "Code", "User", "mcp.json"); const defaultData = diff --git a/tests/boundary/javascript/javascriptApplicationScope.test.ts b/tests/boundary/javascript/javascriptApplicationScope.test.ts new file mode 100644 index 0000000000..14cf88ecaf --- /dev/null +++ b/tests/boundary/javascript/javascriptApplicationScope.test.ts @@ -0,0 +1,119 @@ +import { describe, expect, it } from "vitest"; +import { runSettledConcurrent } from "../../../scripts/lib/verify-package-core.mjs"; +import { ArtifactResourceScope } from "../../../src/resources/index.js"; +import { withReusableJavaScriptScopeForTest } from "../../support/javascriptApplicationScope.js"; + +describe("withReusableJavaScriptScopeForTest", () => { + it("re-registers teardown across consecutive files sharing module state", async () => { + const teardowns: Array<() => Promise> = []; + const closed: string[] = []; + const makeScope = (label: string) => { + const scope = new ArtifactResourceScope(); + scope.addCloseHook(async () => { + closed.push(label); + }); + return scope; + }; + + await withReusableJavaScriptScopeForTest(async () => "file-a-1", { + testPath: "/suite/fileA.test.ts", + registerTeardown: (fn) => teardowns.push(fn), + createScope: () => makeScope("A"), + }); + await withReusableJavaScriptScopeForTest(async () => "file-a-2", { + testPath: "/suite/fileA.test.ts", + registerTeardown: (fn) => teardowns.push(fn), + createScope: () => makeScope("A-unexpected"), + }); + expect(teardowns).toHaveLength(1); + await teardowns[0]!(); + expect(closed).toEqual(["A"]); + + await withReusableJavaScriptScopeForTest(async () => "file-b-1", { + testPath: "/suite/fileB.test.ts", + registerTeardown: (fn) => teardowns.push(fn), + createScope: () => makeScope("B"), + }); + expect(teardowns).toHaveLength(2); + await teardowns[1]!(); + expect(closed).toEqual(["A", "B"]); + }); + + it("retains a scope whose close failed and blocks replacement until retry succeeds", async () => { + const teardowns: Array<() => Promise> = []; + let allowClose = false; + let closeAttempts = 0; + let created = 0; + const createScope = () => { + created += 1; + const scope = new ArtifactResourceScope(); + scope.addCloseHook(async () => { + closeAttempts += 1; + if (!allowClose) throw new Error("injected close failure"); + }); + return scope; + }; + + await expect( + withReusableJavaScriptScopeForTest( + async () => { + throw new Error("primary analysis failure"); + }, + { + testPath: "/suite/retry.test.ts", + registerTeardown: (fn) => teardowns.push(fn), + createScope, + }, + ), + ).rejects.toThrow("primary analysis failure"); + expect(created).toBe(1); + expect(closeAttempts).toBe(1); + + // Next call must retry the retained failed scope before creating a new one. + await expect( + withReusableJavaScriptScopeForTest(async () => "blocked", { + testPath: "/suite/retry.test.ts", + registerTeardown: (fn) => teardowns.push(fn), + createScope, + }), + ).rejects.toThrow("injected close failure"); + expect(created).toBe(1); + expect(closeAttempts).toBe(2); + + allowClose = true; + const recovered = await withReusableJavaScriptScopeForTest( + async () => "recovered", + { + testPath: "/suite/retry.test.ts", + registerTeardown: (fn) => teardowns.push(fn), + createScope, + }, + ); + expect(recovered).toBe("recovered"); + expect(created).toBe(2); + expect(closeAttempts).toBe(3); + + await teardowns[0]!(); + expect(closeAttempts).toBe(4); + }); +}); + +describe("runSettledConcurrent", () => { + it("waits for started workers to finish and stops taking new items on failure", async () => { + const events: string[] = []; + let inFlightFinished = false; + await expect( + runSettledConcurrent([1, 2, 3, 4], 2, async (item) => { + events.push(`start:${String(item)}`); + if (item === 1) { + throw new Error("first worker failed"); + } + await new Promise((resolve) => setTimeout(resolve, 30)); + inFlightFinished = true; + events.push(`done:${String(item)}`); + }), + ).rejects.toThrow("first worker failed"); + expect(inFlightFinished).toBe(true); + expect(events).toEqual(["start:1", "start:2", "done:2"]); + }); +}); diff --git a/tests/process-global/curatedWindowsLane.test.ts b/tests/process-global/curatedWindowsLane.test.ts index 230720c67c..43cf4f2542 100644 --- a/tests/process-global/curatedWindowsLane.test.ts +++ b/tests/process-global/curatedWindowsLane.test.ts @@ -15,6 +15,7 @@ const workflowSchema = z.object({ steps: z .array( z.object({ + id: z.string().optional(), run: z.string().optional(), with: z.object({ name: z.string().optional() }).optional(), }), @@ -207,11 +208,13 @@ it("checks the complete source with the fallback compiler before Windows build r parse(await readFile(WORKFLOW, "utf8")), ); const steps = workflow.jobs["windows-curated"]?.steps ?? []; + const restored = steps.findIndex((step) => step.id === "windows-build-cache"); const checked = steps.findIndex( - (step) => step.run === "npm run typecheck -- --checkers 2", + (step) => step.run === "node scripts/run-turbo.mjs run typecheck", ); const built = steps.findIndex((step) => step.run === "npm run build:cached"); - expect(checked).toBeGreaterThan(-1); + expect(restored).toBeGreaterThan(-1); + expect(checked).toBeGreaterThan(restored); expect(built).toBeGreaterThan(checked); const vitestRuns = steps .map((step) => step.run) diff --git a/tests/support/cli/cliCommandSession.ts b/tests/support/cli/cliCommandSession.ts index 21b890b886..00a6c7cd12 100644 --- a/tests/support/cli/cliCommandSession.ts +++ b/tests/support/cli/cliCommandSession.ts @@ -124,12 +124,16 @@ const signalChild = ( } }; -const stopChild = async ( +/** + * Stop an owned test child process via EOF, SIGTERM, and SIGKILL, throwing + * when the process still has not exited so the caller retains ownership. + */ +export const stopOwnedChildProcess = async ( child: ChildProcessWithoutNullStreams, graceMs: number, + label = "shared CLI process", ): Promise => { if (child.exitCode !== null || child.signalCode !== null) return; - // EOF lets the worker await closeCliResources before exiting. if (!child.stdin.writableEnded) child.stdin.end(); if (await waitForExit(child, graceMs)) return; signalChild(child, "SIGTERM"); @@ -137,11 +141,14 @@ const stopChild = async ( if (child.exitCode !== null || child.signalCode !== null) return; signalChild(child, "SIGKILL"); if (await waitForExit(child, graceMs)) return; - throw new Error( - `shared CLI process ${String(child.pid)} did not exit after SIGKILL`, - ); + throw new Error(`${label} ${String(child.pid)} did not exit after SIGKILL`); }; +const stopChild = ( + child: ChildProcessWithoutNullStreams, + graceMs: number, +): Promise => stopOwnedChildProcess(child, graceMs, "shared CLI process"); + const commandResult = (message: SessionResponse): CliCommandResult => ({ exitCode: message.exitCode ?? null, signal: null, diff --git a/tests/support/javascriptApplicationScope.ts b/tests/support/javascriptApplicationScope.ts index caeb897b41..b61b666c42 100644 --- a/tests/support/javascriptApplicationScope.ts +++ b/tests/support/javascriptApplicationScope.ts @@ -1,3 +1,4 @@ +import { afterAll, expect } from "vitest"; import { withArtifactResourceScope } from "../fixtures/artifactInventory.js"; import { analyzeJavaScriptApplication as analyzeOwned, @@ -8,9 +9,137 @@ import { analyzeJavaScriptApplicationInputSchema } from "../../src/domain/javasc import type { ProgressReporter } from "../../src/ports/ProgressReporter.js"; import { javascriptSourceAnalysis } from "../../src/composition/javascriptSourceAnalysis.js"; import { readJavaScriptArtifactFiles as readOwned } from "../../src/artifacts/javascript/JavaScriptArtifactFiles.js"; -import type { ArtifactReader } from "../../src/resources/ArtifactReader.js"; +import { + ArtifactReaderFailure, + ArtifactResourceScope, + type ArtifactReader, +} from "../../src/resources/index.js"; import type { ArtifactInventorySnapshot } from "../../src/domain/artifactInventorySnapshot.js"; +interface FileJavaScriptScopeState { + active: ArtifactResourceScope | undefined; + readonly retained: Set; + queue: Promise; + teardownRegistered: boolean; +} + +const fileStates = new Map(); + +const closeScope = async ( + state: FileJavaScriptScopeState, + scope: ArtifactResourceScope, +): Promise => { + await scope.close(); + state.retained.delete(scope); +}; + +const closeRetainedScopes = async ( + state: FileJavaScriptScopeState, +): Promise => { + const results = await Promise.allSettled( + [...state.retained].map((scope) => closeScope(state, scope)), + ); + const failures = results.flatMap((result) => + result.status === "rejected" ? [result.reason] : [], + ); + if (failures.length === 1) throw failures[0]; + if (failures.length > 1) + throw new AggregateError(failures, "JavaScript test scope cleanup failed"); +}; + +/** + * Resolve per-file state by current Vitest `testPath`, re-registering `afterAll` + * for each test file even in `isolate: false` projects (`mcp-boundary`, + * `cli-boundary`) that share module state across files. + */ +const ensureFileState = ( + testPath = expect.getState().testPath ?? "__default__", + registerTeardown: (fn: () => Promise) => void = afterAll, +): FileJavaScriptScopeState => { + let state = fileStates.get(testPath); + if (state === undefined) { + state = { + active: undefined, + retained: new Set(), + queue: Promise.resolve(), + teardownRegistered: false, + }; + fileStates.set(testPath, state); + } + if (!state.teardownRegistered) { + state.teardownRegistered = true; + const ownedState = state; + registerTeardown(async () => { + ownedState.teardownRegistered = false; + ownedState.active = undefined; + await closeRetainedScopes(ownedState); + if (ownedState.retained.size === 0 && !ownedState.teardownRegistered) + fileStates.delete(testPath); + }); + } + return state; +}; + +/** + * Reuse one parked JavaScript worker per test file for uncancelled analyses, + * matching production CLI/MCP session reuse while keeping any scope whose + * cleanup failed in `state.retained` until a retry succeeds. + */ +export const withReusableJavaScriptScopeForTest = ( + operation: (scope: ArtifactResourceScope) => Promise, + hooks?: { + readonly testPath?: string; + readonly registerTeardown?: (fn: () => Promise) => void; + readonly createScope?: () => ArtifactResourceScope; + }, +): Promise => { + const state = ensureFileState(hooks?.testPath, hooks?.registerTeardown); + const run = async (): Promise => { + if (state.active === undefined && state.retained.size > 0) { + // Finish any previously failed cleanup before starting a replacement worker. + await closeRetainedScopes(state); + } + let scope = state.active; + if (scope === undefined) { + scope = hooks?.createScope?.() ?? new ArtifactResourceScope(); + state.retained.add(scope); + state.active = scope; + } + let outcome: + | { readonly kind: "completed"; readonly value: T } + | { readonly kind: "failed"; readonly cause: unknown }; + try { + outcome = { kind: "completed", value: await operation(scope) }; + } catch (cause: unknown) { + outcome = { kind: "failed", cause }; + } + if (outcome.kind === "failed" || scope.hasPending) { + if (state.active === scope) state.active = undefined; + try { + await closeScope(state, scope); + } catch (cleanupCause: unknown) { + if (outcome.kind === "failed") + throw ArtifactReaderFailure.withCleanup( + outcome.cause, + ArtifactReaderFailure.cleanupObservation( + cleanupCause, + "artifact resources", + ), + ); + throw cleanupCause; + } + } + if (outcome.kind === "failed") throw outcome.cause; + return outcome.value; + }; + const scheduled = state.queue.then(run); + state.queue = scheduled.then( + () => undefined, + () => undefined, + ); + return scheduled; +}; + /** Read a fixture's nested containers with an awaited cleanup owner. */ export const readJavaScriptArtifactFiles = ( reader: ArtifactReader, @@ -27,12 +156,19 @@ export const analyzeJavaScriptApplication = ( input: unknown, options: Omit = {}, ) => - withArtifactResourceScope((scope) => - analyzeOwned(input, scope, { - sourceAnalysis: javascriptSourceAnalysis, - ...options, - }), - ); + options.signal !== undefined + ? withArtifactResourceScope((scope) => + analyzeOwned(input, scope, { + sourceAnalysis: javascriptSourceAnalysis, + ...options, + }), + ) + : withReusableJavaScriptScopeForTest((scope) => + analyzeOwned(input, scope, { + sourceAnalysis: javascriptSourceAnalysis, + ...options, + }), + ); /** Reconstruct a fixture with an awaited, test-owned artifact scope. */ export const reconstructJavaScriptArtifact = ( @@ -40,12 +176,22 @@ export const reconstructJavaScriptArtifact = ( signal?: AbortSignal, progress?: ProgressReporter, ) => - withArtifactResourceScope((scope) => - reconstructOwned( - analyzeJavaScriptApplicationInputSchema.parse(rawInput), - scope, - javascriptSourceAnalysis, - signal, - progress, - ), - ); + signal !== undefined + ? withArtifactResourceScope((scope) => + reconstructOwned( + analyzeJavaScriptApplicationInputSchema.parse(rawInput), + scope, + javascriptSourceAnalysis, + signal, + progress, + ), + ) + : withReusableJavaScriptScopeForTest((scope) => + reconstructOwned( + analyzeJavaScriptApplicationInputSchema.parse(rawInput), + scope, + javascriptSourceAnalysis, + signal, + progress, + ), + );