diff --git a/src/browser/PlaywrightElectronActiveProvider.ts b/src/browser/PlaywrightElectronActiveProvider.ts index 814f6d7dec..f641519df6 100644 --- a/src/browser/PlaywrightElectronActiveProvider.ts +++ b/src/browser/PlaywrightElectronActiveProvider.ts @@ -5,11 +5,9 @@ import { } from "../providers/OwnedCommandCleanup.js"; import { runWithExecutionLimits } from "./PlaywrightElectronExecution.js"; import { - ArtifactReaderFailure, - bindArtifactFileCleanup, + artifactFileCleanupError, ArtifactResourceScope, } from "../resources/index.js"; -import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { ElectronCaptureCleanup } from "./ElectronCaptureCleanup.js"; import { analysisErrorWithCleanupDependencies } from "../domain/analysisErrorCleanup.js"; import { analysisErrorWithCancellation } from "../domain/analysisErrorCancellation.js"; @@ -584,16 +582,12 @@ const containLaunchRejections = async ( const providerError = (cause: unknown): AnalysisError => { if (cause instanceof AnalysisError) return cause; - if (cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined) { - const failure = new ProviderCleanupError( - PLAYWRIGHT_ELECTRON_ACTIVE_PROVIDER_IDENTITY.id, - cause.cleanup.resources, - { reason: cause.cleanup.reason }, - { operation: OPERATION, cause }, - ); - bindArtifactFileCleanup(cause, failure); - return failure; - } + const fileCleanup = artifactFileCleanupError( + cause, + PLAYWRIGHT_ELECTRON_ACTIVE_PROVIDER_IDENTITY.id, + OPERATION, + ); + if (fileCleanup !== undefined) return fileCleanup; return new ProviderAdapterError( PLAYWRIGHT_ELECTRON_ACTIVE_PROVIDER_IDENTITY.id, OPERATION, diff --git a/src/browser/assets/WebArtifactReads.ts b/src/browser/assets/WebArtifactReads.ts index 3f2dfaa014..f6438cffe8 100644 --- a/src/browser/assets/WebArtifactReads.ts +++ b/src/browser/assets/WebArtifactReads.ts @@ -3,11 +3,11 @@ import type { WebArtifactAcquisition as Acquisition } from "../../domain/webArti import { isAbortOf } from "../../process/SignalAbort.js"; import { z } from "zod"; import { + artifactFileCleanupError, ArtifactReaderFailure, stableArtifactInputRecovery, artifactFileCancellationError, artifactFilePrimaryFacts, - bindArtifactFileCleanup, type ArtifactFilePrimaryFacts, type ArtifactResourceScope, completedStableArtifact, @@ -27,7 +27,6 @@ import { } from "../../domain/analysisErrorCore.js"; import { ArtifactOperationError } from "../../domain/artifactOperationError.js"; import { ProviderAdapterError } from "../../domain/providerAdapterError.js"; -import { ProviderCleanupError } from "../../domain/providerCleanupError.js"; import { analysisErrorWithCleanupDependencies } from "../../domain/analysisErrorCleanup.js"; import { safeParseJson } from "../../domain/safeJson.js"; import { @@ -152,14 +151,12 @@ export const webArtifactReadError = ( context, signal, ); - if (cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined) { - const cleanup = new ProviderCleanupError( - "captured-web-artifacts", - cause.cleanup.resources, - { reason: cause.cleanup.reason }, - { cause, operation: context.operation }, - ); - bindArtifactFileCleanup(cause, cleanup); + const cleanup = artifactFileCleanupError( + cause, + "captured-web-artifacts", + context.operation, + ); + if (cleanup !== undefined) { return analysisErrorWithCleanupDependencies( primary, cleanup, diff --git a/src/cutter/CutterBridgeClient.ts b/src/cutter/CutterBridgeClient.ts index 38dc8a2b92..3991309283 100644 --- a/src/cutter/CutterBridgeClient.ts +++ b/src/cutter/CutterBridgeClient.ts @@ -5,16 +5,15 @@ import { } from "../domain/analysisErrorCleanup.js"; import { ProviderAdapterError } from "../domain/providerAdapterError.js"; import { + artifactFileCleanupError, ArtifactResourceScope, ArtifactReaderFailure, withArtifactRegularFile, ArtifactFileCleanupFailure, artifactFileCompletion, - bindArtifactFileCleanup, artifactFileCancellationError, assertArtifactReadActive, } from "../resources/index.js"; -import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { isUtf8 } from "node:buffer"; import { AnalysisCapabilityUnavailableError, @@ -1314,14 +1313,8 @@ const discoveryFailure = ( ); } if (!(cause instanceof ArtifactReaderFailure)) return cause; - if (cause.cleanup !== undefined) { - const cleanup = new ProviderCleanupError( - "cutter", - cause.cleanup.resources, - { reason: cause.cleanup.reason }, - { operation, cause }, - ); - bindArtifactFileCleanup(cause, cleanup); + const cleanup = artifactFileCleanupError(cause, "cutter", operation); + if (cleanup !== undefined) { const outcome = cause instanceof ArtifactFileCleanupFailure ? cause.fileCleanupFailure.outcome diff --git a/src/electron/ElectronFuseFailures.ts b/src/electron/ElectronFuseFailures.ts index ddb4d400fc..a9aadd4f29 100644 --- a/src/electron/ElectronFuseFailures.ts +++ b/src/electron/ElectronFuseFailures.ts @@ -4,13 +4,12 @@ import { } from "../domain/parseFailureAnalysisError.js"; import { ELECTRON_FUSE_PROVIDER_IDENTITY } from "./ElectronFuseProviderIdentity.js"; import { + artifactFileCleanupError, ArtifactReaderFailure, ArtifactFileCleanupFailure, artifactFileCompletion, artifactFileCancellationError, - bindArtifactFileCleanup, } from "../resources/index.js"; -import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { analysisErrorWithCleanupDependencies } from "../domain/analysisErrorCleanup.js"; import { regularFileCancellation } from "../filesystem/OwnedRegularFile.js"; import { AnalysisError } from "../domain/analysisErrorBase.js"; @@ -42,14 +41,12 @@ export const electronFuseFailure = ( operation = OPERATION, ): AnalysisError => { if (cause instanceof AnalysisError) return cause; - if (cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined) { - const cleanup = new ProviderCleanupError( - ELECTRON_FUSE_PROVIDER_IDENTITY.id, - cause.cleanup.resources, - { reason: cause.cleanup.reason }, - { operation, cause }, - ); - bindArtifactFileCleanup(cause, cleanup); + const cleanup = artifactFileCleanupError( + cause, + ELECTRON_FUSE_PROVIDER_IDENTITY.id, + operation, + ); + if (cleanup !== undefined) { if (cause instanceof ArtifactFileCleanupFailure) { const outcome = cause.fileCleanupFailure.outcome; const cancellation = artifactFileCompletion(cause)?.cancellation; diff --git a/src/javascript/analysis/JavaScriptAnalysisWorker.ts b/src/javascript/analysis/JavaScriptAnalysisWorker.ts index f63bbd193e..ccbfd2599e 100644 --- a/src/javascript/analysis/JavaScriptAnalysisWorker.ts +++ b/src/javascript/analysis/JavaScriptAnalysisWorker.ts @@ -11,9 +11,8 @@ import { } from "../../domain/javascript/javascriptSemanticWorkAllowance.js"; import { MAX_NODE_TIMER_DELAY_MS } from "../../domain/analysisResourceLimits.js"; import { + artifactFileCleanupError, ArtifactResourceScope, - ArtifactReaderFailure, - bindArtifactFileCleanup, } from "../../resources/index.js"; import { analysisErrorWithCleanupDependencies } from "../../domain/analysisErrorCleanup.js"; import { @@ -1443,16 +1442,8 @@ export class JavaScriptAnalysisWorker { : cancellation.capturedOutput === undefined ? cancellation.retainCapturedOutput(this.#captured()) : cancellation; - if (cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined) { - const error = new ProviderCleanupError( - PROVIDER, - cause.cleanup.resources, - { reason: cause.cleanup.reason }, - { operation: OPERATION, cause }, - ); - bindArtifactFileCleanup(cause, error); - return error; - } + const fileCleanup = artifactFileCleanupError(cause, PROVIDER, OPERATION); + if (fileCleanup !== undefined) return fileCleanup; if ( cause instanceof AnalysisError && (deadline === undefined || this.#phase === "startup") diff --git a/src/kuna/KunaTargetCommands.ts b/src/kuna/KunaTargetCommands.ts index 02759aece9..52a627b174 100644 --- a/src/kuna/KunaTargetCommands.ts +++ b/src/kuna/KunaTargetCommands.ts @@ -1,4 +1,5 @@ import { + artifactFileCleanupError, ArtifactFileCleanupFailure, artifactFilePrimaryFacts, bindArtifactFileCleanup, @@ -354,19 +355,12 @@ export class KunaTargetCommands { try { return await this.#resources.run(work); } catch (cause: unknown) { - if ( - cause instanceof ArtifactReaderFailure && - cause.cleanup !== undefined - ) { - const failure = new ProviderCleanupError( - KUNA_PROVIDER_ID, - cause.cleanup.resources, - { reason: cause.cleanup.reason }, - { cause, operation }, - ); - bindArtifactFileCleanup(cause, failure); - return err(failure); - } + const fileCleanup = artifactFileCleanupError( + cause, + KUNA_PROVIDER_ID, + operation, + ); + if (fileCleanup !== undefined) return err(fileCleanup); if (this.#closing && cause instanceof ArtifactReaderFailure) return err(cancelledBy(operation, carriedReason(cause))); throw cause; diff --git a/src/resources/StableArtifactFailure.ts b/src/resources/StableArtifactFailure.ts index cf46a73226..571a1d3997 100644 --- a/src/resources/StableArtifactFailure.ts +++ b/src/resources/StableArtifactFailure.ts @@ -215,18 +215,24 @@ const artifactCleanupFailure = ( ); }; -/** Project descriptor cleanup while keeping its exact leases bound to the error. */ +/** Build the provider cleanup error and bind it to the reader's exact leases. */ +export const artifactFileCleanupError = ( + cause: unknown, + providerId: string, + operation: string, +): ProviderCleanupError | undefined => { + if (!(cause instanceof ArtifactReaderFailure)) return undefined; + const error = artifactCleanupFailure(cause, providerId, operation); + if (error !== undefined) bindArtifactFileCleanup(cause, error); + return error; +}; + +/** Preserve the original close failure when the reader has no cleanup evidence. */ export const projectArtifactCloseFailure = ( cause: unknown, providerId: string, operation: string, -): unknown => { - if (!(cause instanceof ArtifactReaderFailure)) return cause; - const error = artifactCleanupFailure(cause, providerId, operation); - if (error === undefined) return cause; - bindArtifactFileCleanup(cause, error); - return error; -}; +): unknown => artifactFileCleanupError(cause, providerId, operation) ?? cause; /** Close a provider's descriptor scope and preserve its cleanup leases on failure. */ export const closeArtifactResources = async ( diff --git a/src/resources/index.ts b/src/resources/index.ts index 0c00a9d7bc..aa1947dcad 100644 --- a/src/resources/index.ts +++ b/src/resources/index.ts @@ -100,6 +100,7 @@ export type { export { imageFileSizeRemediation, closeArtifactResources, + artifactFileCleanupError, projectArtifactCloseFailure, withArtifactCleanupFailure, artifactFileCancellationError,