diff --git a/src/browser/WebArtifactCleanup.test.ts b/src/browser/WebArtifactCleanup.test.ts index 551d946c1cb..334e6095f41 100644 --- a/src/browser/WebArtifactCleanup.test.ts +++ b/src/browser/WebArtifactCleanup.test.ts @@ -5,7 +5,7 @@ import { LocalWebScriptArtifacts } from "./assets/LocalWebScriptArtifacts.js"; import { LocalWebSourceLocationArtifacts } from "./assets/WebSourceLocationArtifacts.js"; import { LocalWebModuleArtifacts } from "./modules/WebModuleArtifacts.js"; -it("reports a refusal from the shared script and map scope once", async () => { +it("reports a refusal from the inferred shared script and map scope once", async () => { const resources = new ArtifactResourceScope(); const refusal = new Error("shared scope close refused"); let attempts = 0; @@ -16,7 +16,8 @@ it("reports a refusal from the shared script and map scope once", async () => { "trace_web_module_imports", resources, ); - const artifacts = new LocalWebModuleArtifacts(scripts, resources); + const artifacts = new LocalWebModuleArtifacts(scripts); + expect(artifacts.scripts).toBe(scripts); const failure = await artifacts.close().catch((cause: unknown) => cause); expect(failure).toMatchObject({ operation: "trace_web_module_imports", diff --git a/src/browser/WebArtifactCleanup.ts b/src/browser/WebArtifactCleanup.ts index 87d5eb74b83..96d12be0bbf 100644 --- a/src/browser/WebArtifactCleanup.ts +++ b/src/browser/WebArtifactCleanup.ts @@ -2,7 +2,7 @@ import { AnalysisCleanupLease, AnalysisCleanupLeaseGroup, ArtifactReaderFailure, - type ArtifactResourceScope, + ArtifactResourceScope, } from "../resources/index.js"; import type { WebScriptArtifactPort } from "../ports/WebScriptArtifactPort.js"; import { LocalWebScriptArtifacts } from "./assets/LocalWebScriptArtifacts.js"; @@ -40,8 +40,10 @@ const cleanupOwner = (resource: string, close: () => Promise) => { return { resource, lease }; }; -/** Retain each composed artifact owner's release across partial cleanup retries. */ +/** Compose script and map scopes and retain their owners across cleanup retries. */ export class WebArtifactCleanup { + readonly resources: ArtifactResourceScope; + readonly scripts: WebScriptArtifactPort; readonly #owners: readonly { readonly resource: string; readonly lease: AnalysisCleanupLease; @@ -50,23 +52,28 @@ export class WebArtifactCleanup { constructor( private readonly owner: WebArtifactOwner, - resources: ArtifactResourceScope, - scripts: WebScriptArtifactPort, + resources?: ArtifactResourceScope, + scripts?: WebScriptArtifactPort, ) { const sourceLocation = owner === "web-source-location-artifacts"; - this.#operation = sourceLocation + const operation = sourceLocation ? "trace_web_source_location" : "trace_web_module_imports"; + this.#operation = operation; const resource = sourceLocation ? "web source-location artifacts" : "web module artifacts"; + const scriptScope = + scripts instanceof LocalWebScriptArtifacts + ? scripts.resources + : undefined; + const scope = resources ?? scriptScope ?? new ArtifactResourceScope(); + this.resources = scope; + this.scripts = scripts ?? new LocalWebScriptArtifacts(operation, scope); const closeScript = - scripts instanceof LocalWebScriptArtifacts && - scripts.resources === resources - ? undefined - : scripts.close?.bind(scripts); + scriptScope === scope ? undefined : scripts?.close?.bind(scripts); this.#owners = [ - cleanupOwner(resource, () => resources.close()), + cleanupOwner(resource, () => scope.close()), ...(closeScript === undefined ? [] : [cleanupOwner("web scripts", closeScript)]), diff --git a/src/browser/assets/WebSourceLocationArtifacts.ts b/src/browser/assets/WebSourceLocationArtifacts.ts index b9a3a0bc8f9..37323919a51 100644 --- a/src/browser/assets/WebSourceLocationArtifacts.ts +++ b/src/browser/assets/WebSourceLocationArtifacts.ts @@ -6,11 +6,10 @@ import { import type { WebSourceLocationArtifactPort } from "../../ports/WebSourceLocationPorts.js"; import type { WebScriptArtifactPort } from "../../ports/WebScriptArtifactPort.js"; import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; -import { ArtifactResourceScope } from "../../resources/index.js"; +import type { ArtifactResourceScope } from "../../resources/index.js"; import type { WebSourceLocationInput } from "../../domain/webSourceLocation.js"; import { err, ok } from "../../domain/result.js"; import { WebArtifactCleanup } from "../WebArtifactCleanup.js"; -import { LocalWebScriptArtifacts } from "./LocalWebScriptArtifacts.js"; import { decodeWebArtifact, WebArtifactAcquisition, @@ -20,24 +19,18 @@ import { /** Read one verified capture source and the caller-selected map, without fetching. */ export class LocalWebSourceLocationArtifacts implements WebSourceLocationArtifactPort { readonly scripts: WebScriptArtifactPort; - readonly #resources: ArtifactResourceScope; readonly #cleanup: WebArtifactCleanup; constructor( scripts?: WebScriptArtifactPort, - resources = scripts instanceof LocalWebScriptArtifacts - ? scripts.resources - : new ArtifactResourceScope(), + resources?: ArtifactResourceScope, ) { - this.#resources = resources; - this.scripts = - scripts ?? - new LocalWebScriptArtifacts("trace_web_source_location", resources); this.#cleanup = new WebArtifactCleanup( "web-source-location-artifacts", resources, - this.scripts, + scripts, ); + this.scripts = this.#cleanup.scripts; } async close(): Promise { @@ -48,7 +41,7 @@ export class LocalWebSourceLocationArtifacts implements WebSourceLocationArtifac async load(input: WebSourceLocationInput, options?: ExecutionOptions) { const acquisition = new WebArtifactAcquisition(); try { - return await this.#resources.run(() => + return await this.#cleanup.resources.run(() => this.#load(input, acquisition, options), ); } catch (cause: unknown) { @@ -102,7 +95,7 @@ export class LocalWebSourceLocationArtifacts implements WebSourceLocationArtifac input.source_map.path, selectedSourceMapLimits(input.source_map_limits).mapBytes, options?.signal, - this.#resources, + this.#cleanup.resources, ); return ok({ ...loaded.value, diff --git a/src/browser/modules/WebModuleArtifacts.ts b/src/browser/modules/WebModuleArtifacts.ts index ed60905c6ad..69027d1676b 100644 --- a/src/browser/modules/WebModuleArtifacts.ts +++ b/src/browser/modules/WebModuleArtifacts.ts @@ -4,12 +4,11 @@ import type { } from "../../ports/WebModulePorts.js"; import type { WebScriptArtifactPort } from "../../ports/WebScriptArtifactPort.js"; import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; -import { ArtifactResourceScope } from "../../resources/index.js"; +import type { ArtifactResourceScope } from "../../resources/index.js"; import { ownedJsonObjectSchema } from "../../domain/jsonValue.js"; import { err, ok } from "../../domain/result.js"; import { WebArtifactCleanup } from "../WebArtifactCleanup.js"; import type { WebModuleTraceInput } from "../../domain/webModuleTrace.js"; -import { LocalWebScriptArtifacts } from "../assets/LocalWebScriptArtifacts.js"; import { WebArtifactAcquisition, webArtifactReadError, @@ -18,24 +17,18 @@ import { /** Add an explicitly selected import map to a verified captured-script source. */ export class LocalWebModuleArtifacts implements WebModuleArtifactPort { readonly scripts: WebScriptArtifactPort; - readonly #resources: ArtifactResourceScope; readonly #cleanup: WebArtifactCleanup; constructor( scripts?: WebScriptArtifactPort, - resources = scripts instanceof LocalWebScriptArtifacts - ? scripts.resources - : new ArtifactResourceScope(), + resources?: ArtifactResourceScope, ) { - this.#resources = resources; - this.scripts = - scripts ?? - new LocalWebScriptArtifacts("trace_web_module_imports", resources); this.#cleanup = new WebArtifactCleanup( "web-module-artifacts", resources, - this.scripts, + scripts, ); + this.scripts = this.#cleanup.scripts; } async close(): Promise { @@ -46,7 +39,7 @@ export class LocalWebModuleArtifacts implements WebModuleArtifactPort { async load(input: WebModuleTraceInput, options?: ExecutionOptions) { const acquisition = new WebArtifactAcquisition(); try { - return await this.#resources.run(() => + return await this.#cleanup.resources.run(() => this.#load(input, acquisition, options), ); } catch (cause: unknown) { @@ -91,7 +84,7 @@ export class LocalWebModuleArtifacts implements WebModuleArtifactPort { selected.path, 4 * 1024 * 1024, options?.signal, - this.#resources, + this.#cleanup.resources, ); const importMap: WebModuleArtifacts["importMap"] = { file: {