diff --git a/src/application/javascript/JavaScriptArtifactReconstruction.ts b/src/application/javascript/JavaScriptArtifactReconstruction.ts index cbf61b908..cd5b6c501 100644 --- a/src/application/javascript/JavaScriptArtifactReconstruction.ts +++ b/src/application/javascript/JavaScriptArtifactReconstruction.ts @@ -122,8 +122,16 @@ export const reconstructJavaScriptArtifact = async ( ); // Release acquisition before analysis consumes these owned file facts. const earlyCleanup = await scope.release(readerOwner); - if (earlyCleanup.kind === "failed") throw earlyCleanup.cause; readerOpen = false; + if (earlyCleanup.kind === "failed") + throw ArtifactReaderFailure.withCleanup( + earlyCleanup.cause, + ArtifactReaderFailure.cleanupObservation( + earlyCleanup.cause, + readerOwner.resource, + ), + { kind: "artifact-inventory", inventory: snapshot }, + ); await reportPhase( "parse_javascript_sources", `Parsing and projecting ${String(files.files.length)} application source files`, diff --git a/src/artifacts/ArtifactResourceScope.test.ts b/src/artifacts/ArtifactResourceScope.test.ts index 9bf8fe87b..395f113b3 100644 --- a/src/artifacts/ArtifactResourceScope.test.ts +++ b/src/artifacts/ArtifactResourceScope.test.ts @@ -1,5 +1,7 @@ import { expect, it } from "vitest"; +import { err } from "../domain/result.js"; +import { AnalysisOutputError } from "../domain/analysisErrorCore.js"; import type { ArtifactReader } from "./ArtifactReader.js"; import { ArtifactResourceScope } from "./ArtifactResourceScope.js"; @@ -66,3 +68,47 @@ it("retains the same failed reader owner and retries before admitting work", asy await scope.close(); expect(closeAttempts).toBe(4); }); + +it("retains a failed source analyzer until shutdown can verify its release", async () => { + const scope = new ArtifactResourceScope(); + let releaseAllowed = false; + const failure = new AnalysisOutputError( + "analyze_javascript_application", + "Owned worker is still running", + ); + const owner = { + kind: "javascript-source-analysis" as const, + resource: "fixture source analyzer", + analysis: { + async analyze() { + return err({ + error: failure, + javascript: null, + module: null, + projection: null, + }); + }, + async close() { + if (!releaseAllowed) throw failure; + }, + }, + }; + expect(await scope.release(owner)).toEqual({ + kind: "failed", + cause: failure, + }); + let admitted = false; + await expect( + scope.run(async () => { + admitted = true; + }), + ).rejects.toMatchObject({ + cleanup: { resources: ["fixture source analyzer"] }, + }); + expect(admitted).toBe(false); + await expect(scope.close()).rejects.toMatchObject({ + cleanup: { resources: ["fixture source analyzer"] }, + }); + releaseAllowed = true; + await scope.close(); +}); diff --git a/tests/boundary/mcp/javascriptAnalysisResources.test.ts b/tests/boundary/mcp/javascriptAnalysisResources.test.ts index b28e61cfa..a2d172183 100644 --- a/tests/boundary/mcp/javascriptAnalysisResources.test.ts +++ b/tests/boundary/mcp/javascriptAnalysisResources.test.ts @@ -233,6 +233,10 @@ it("returns a directly usable partial analysis reference for an oversized timeou parse_failures: 0, }); expect(summary.coverage.semantic_unknowns).toBeGreaterThan(1500); + await verifyRepeatedSourcePaths( + client, + projected.error.details.partial_observation, + ); await client.ping(); const next = await client.callTool({ name: "analyze_javascript_application", @@ -270,3 +274,58 @@ const createResourceClient = () => { }); return { client, transport, diagnostics: () => diagnostics }; }; + +const verifyRepeatedSourcePaths = async ( + client: Client, + source: { readonly kind: "retained-evidence"; readonly evidence_id: string }, +): Promise => { + const page = await client.callTool({ + name: "inspect_analysis_view", + arguments: { + source, + view: { kind: "page", collection: "modules", offset: 0, limit: 4 }, + }, + }); + expect(page.isError).not.toBe(true); + const modules = z + .object({ + normalized_result: z.object({ + items: z.array(z.object({ kind: z.string(), node_id: z.string() })), + }), + }) + .parse(page.structuredContent); + const asset = modules.normalized_result.items.find( + ({ kind }) => kind === "javascript-asset", + ); + if (asset === undefined) + throw new Error( + "Identical source files disappeared from the retained graph", + ); + const item = await client.callTool({ + name: "inspect_analysis_view", + arguments: { + source, + view: { + kind: "item", + collection: "modules", + selector: { node_id: asset.node_id }, + }, + }, + }); + expect(item.isError).not.toBe(true); + const observations = z + .object({ + normalized_result: z.object({ + item: z.object({ + observations: z.array(z.object({ label: z.string() })), + }), + }), + }) + .parse(item.structuredContent).normalized_result.item.observations; + expect(observations.map(({ label }) => label).sort()).toEqual( + Array.from( + { length: 1500 }, + (_, index) => `file-${String(index)}.js`, + ).sort(), + ); +};