From b2bb427553f9a491ae772f1e1d430716f837ca9f Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:00:10 -0500 Subject: [PATCH] fix(cleanup): report live resources independently of failed closes (#12589) fix(cleanup): follow live resources across owner close failures --- .../baselines/cleanup-dependency-binding.json | 2 - .../WebSourceLocationService.test.ts | 39 +++++++++++++ src/application/closeOwnedWebResources.ts | 25 ++++----- .../ReleaseExposureService.test.ts | 16 ++++++ .../releaseExposure/ReleaseExposureService.ts | 37 +++++------- src/domain/analysisErrorCleanup.ts | 56 +++++++++++++++++++ 6 files changed, 134 insertions(+), 41 deletions(-) diff --git a/scripts/verify/baselines/cleanup-dependency-binding.json b/scripts/verify/baselines/cleanup-dependency-binding.json index b5f61906ba..9b581be746 100644 --- a/scripts/verify/baselines/cleanup-dependency-binding.json +++ b/scripts/verify/baselines/cleanup-dependency-binding.json @@ -3,10 +3,8 @@ "src/application/SetupCleanupOwner.ts": 2, "src/application/binary/AnalysisProviderRegistry.ts": 2, "src/application/binary/CompositeProvider.ts": 1, - "src/application/closeOwnedWebResources.ts": 1, "src/application/desktop/DesktopApplicationExport.ts": 2, "src/application/frida/FridaInstrumentationService.ts": 2, - "src/application/releaseExposure/ReleaseExposureService.ts": 1, "src/browser/PlaywrightBrowserScenarioProvider.ts": 1, "src/browser/WebSourceMapFetcher.ts": 2, "src/cli/desktopCaptureCommands.ts": 1, diff --git a/src/application/WebSourceLocationService.test.ts b/src/application/WebSourceLocationService.test.ts index 868373c788..3bceb2d01c 100644 --- a/src/application/WebSourceLocationService.test.ts +++ b/src/application/WebSourceLocationService.test.ts @@ -18,9 +18,48 @@ import { } from "../domain/webSourceLocation.js"; import { WebSourceLocationService } from "./WebSourceLocationService.js"; import type { WebSourceMapPort } from "../ports/WebSourceLocationPorts.js"; +import { ProviderCleanupError } from "../domain/providerCleanupError.js"; +import { releaseOwnedCleanup } from "../../tests/support/cleanupRelease.js"; const service = (data = webSourceLocationFixture(), port = decoder) => new WebSourceLocationService({ load: () => Promise.resolve(ok(data)) }, port); + +it.each(["opaque", "already released"] as const)( + "tracks the remaining web cleanup owner beside an %s sibling", + async (sibling) => { + const live = new ProviderCleanupError("artifacts", ["artifact-root"], {}); + const released = new ProviderCleanupError("codec", ["codec-root"], {}); + released.bindCleanupResourceOwner({ cleanupResources: ["codec-root"] }); + releaseOwnedCleanup(released); + const other = + sibling === "opaque" ? new Error("codec close unconfirmed") : released; + const workflow = new WebSourceLocationService( + { + load: async () => ok(webSourceLocationFixture()), + close: async () => { + throw live; + }, + }, + { + ...decoder, + close: async () => { + throw other; + }, + }, + ); + const failure: unknown = await workflow + .close() + .catch((cause: unknown) => cause); + if (!(failure instanceof ProviderCleanupError)) + throw new Error("expected aggregate cleanup failure"); + expect(failure.cause).toMatchObject({ errors: [live, other] }); + expect(failure.cleanupIncomplete).toBe(true); + expect(failure.cleanupResources).toEqual(["artifact-root"]); + releaseOwnedCleanup(live); + expect(failure.cleanupResources).toEqual([]); + expect(failure.cleanupIncomplete).toBe(sibling === "opaque"); + }, +); const expectCodecDeadline = ( evidence: { readonly limitations: readonly string[]; diff --git a/src/application/closeOwnedWebResources.ts b/src/application/closeOwnedWebResources.ts index 10043a1ffa..2a282a8118 100644 --- a/src/application/closeOwnedWebResources.ts +++ b/src/application/closeOwnedWebResources.ts @@ -1,4 +1,5 @@ import { AnalysisError } from "../domain/analysisErrorBase.js"; +import { providerCleanupErrorFromCloseRejections } from "../domain/analysisErrorCleanup.js"; import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; import { errorMessage } from "../kernel/errorMessage.js"; @@ -29,11 +30,16 @@ export const closeOwnedWebResources = async ( if (failures.length > 1) { const normalized = failures.map((cause) => ({ cause, + // A resolved error without an owner does not establish that this close settled. + cleanupOwner: + cause instanceof AnalysisError && + (cause.cleanupIncomplete || cause.hasCleanupOwner) + ? cause + : undefined, projection: cause instanceof AnalysisError ? projectAnalysisError(cause) : undefined, - resources: cause instanceof AnalysisError ? cause.cleanupResources : [], })); const recoveryActions = [ ...new Set( @@ -42,9 +48,9 @@ export const closeOwnedWebResources = async ( ), ), ]; - const aggregate = new ProviderCleanupError( + throw providerCleanupErrorFromCloseRejections( "web-workflow", - [...new Set(normalized.flatMap(({ resources }) => resources))], + normalized, { failures: normalized.map( ({ cause, projection }) => @@ -54,23 +60,12 @@ export const closeOwnedWebResources = async ( ), }, { - cause: new AggregateError( - normalized.map(({ cause }) => cause), - message, - ), operation: "close_web_workflow", + message, ...(recoveryActions.length === 0 ? {} : { remediationAction: recoveryActions.join("; ") }), }, ); - const dependencies = failures.filter( - (cause): cause is AnalysisError => - cause instanceof AnalysisError && cause.cleanupIncomplete, - ); - // Opaque or already-resolved rejections supply no live release authority. - if (dependencies.length === failures.length) - aggregate.bindCleanupDependencies(dependencies); - throw aggregate; } }; diff --git a/src/application/releaseExposure/ReleaseExposureService.test.ts b/src/application/releaseExposure/ReleaseExposureService.test.ts index b7992396f8..3aa660aaaa 100644 --- a/src/application/releaseExposure/ReleaseExposureService.test.ts +++ b/src/application/releaseExposure/ReleaseExposureService.test.ts @@ -3323,3 +3323,19 @@ it("follows the remaining release-exposure owner when a sibling has already rele releaseOwnedCleanup(second); expect(failure.cleanupIncomplete).toBe(false); }); + +it("drops a released typed resource even while another owner remains unconfirmed", async () => { + const { service, desktop, goBinaries } = owners(scan()); + const typed = new ProviderCleanupError("desktop", ["desktop-root"], {}); + const opaque = new Error("cannot confirm Go owner closed"); + desktop.close.mockRejectedValue(typed); + goBinaries.close.mockRejectedValue(opaque); + const failure = await service.close().catch((cause: unknown) => cause); + if (!(failure instanceof ProviderCleanupError)) + throw new Error("expected typed aggregate"); + expect(failure.cleanupResources).toEqual(["desktop-root"]); + expect(failure.cleanupIncomplete).toBe(true); + releaseOwnedCleanup(typed); + expect(failure.cleanupIncomplete).toBe(true); + expect(failure.cleanupResources).toEqual([]); +}); diff --git a/src/application/releaseExposure/ReleaseExposureService.ts b/src/application/releaseExposure/ReleaseExposureService.ts index 66c77d9ae4..1e5a3f8f9d 100644 --- a/src/application/releaseExposure/ReleaseExposureService.ts +++ b/src/application/releaseExposure/ReleaseExposureService.ts @@ -11,6 +11,7 @@ import type { RustBinaryService } from "../rust/RustBinaryService.js"; import { parseAnalysisInput } from "../AnalysisInput.js"; import { completeProviderEvidence } from "../ProviderOperationExecution.js"; import { AnalysisError } from "../../domain/analysisErrorBase.js"; +import { providerCleanupErrorFromCloseRejections } from "../../domain/analysisErrorCleanup.js"; import { AnalysisCancelledError, AnalysisOutputError, @@ -30,7 +31,6 @@ import { recognizeEvidence } from "../../domain/evidenceValidation.js"; import { evidenceSubjectFormatSchema } from "../../domain/evidenceSubject.js"; import { goBinarySchema } from "../../domain/go/goBinary.js"; import { RELEASE_EXPOSURE_PROVIDER } from "../../domain/investigationProviders.js"; -import { ProviderCleanupError } from "../../domain/providerCleanupError.js"; import { scanCredentialMarkers, type CredentialMarkerHit, @@ -557,18 +557,18 @@ const ownerCloseFailure = ( ): unknown => { const [first] = failures; if (failures.length === 1 && first !== undefined) return first.cause; - const typed = failures.flatMap(({ cause }) => - cause instanceof AnalysisError ? [cause] : [], - ); - const resources = failures.flatMap(({ cause }) => { - if (cause instanceof AnalysisError) return cause.cleanupResources; - return cause instanceof ArtifactReaderFailure - ? (cause.cleanup?.resources ?? []) - : []; - }); - const aggregate = new ProviderCleanupError( + return providerCleanupErrorFromCloseRejections( RELEASE_EXPOSURE_PROVIDER.id, - [...new Set(resources)], + failures.map(({ cause }) => ({ + cause, + cleanupOwner: + cause instanceof AnalysisError && cause.carriesCleanup + ? cause + : undefined, + ...(cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined + ? { unconfirmedResources: cause.cleanup.resources } + : {}), + })), { failures: failures.map(({ owner, cause }) => ({ owner, @@ -581,21 +581,10 @@ const ownerCloseFailure = ( })), }, { - cause: new AggregateError( - failures.map(({ cause }) => cause), - `Release exposure cleanup failed: ${failures.map(({ owner }) => owner).join(", ")}.`, - ), + message: `Release exposure cleanup failed: ${failures.map(({ owner }) => owner).join(", ")}.`, operation: RELEASE_EXPOSURE_OPERATION, }, ); - // An owner that already released still accounts for its rejection. An opaque - // rejection supplies no release authority and keeps the aggregate incomplete. - if ( - typed.length === failures.length && - typed.every((cause) => cause.carriesCleanup) - ) - aggregate.bindCleanupDependencies(typed); - return aggregate; }; /** diff --git a/src/domain/analysisErrorCleanup.ts b/src/domain/analysisErrorCleanup.ts index 55ac1ffa56..f991ee3aa8 100644 --- a/src/domain/analysisErrorCleanup.ts +++ b/src/domain/analysisErrorCleanup.ts @@ -131,6 +131,62 @@ export const providerCleanupErrorFromFailures = ( }, ).bindCleanupDependencies(failures); +/** Compose rejected closes and follow known resource ownership independently of completion. */ +export const providerCleanupErrorFromCloseRejections = ( + providerId: string, + rejections: readonly { + readonly cause: unknown; + readonly cleanupOwner?: AnalysisError | undefined; + readonly unconfirmedResources?: readonly string[]; + }[], + diagnostics: Readonly>, + options: { + readonly operation: string; + readonly message: string; + readonly remediationAction?: string; + }, +): ProviderCleanupError => { + const owners = rejections.flatMap(({ cleanupOwner }) => + cleanupOwner === undefined ? [] : [cleanupOwner], + ); + const resources = () => [ + ...new Set( + rejections.flatMap(({ cleanupOwner, unconfirmedResources }) => { + if (cleanupOwner === undefined) return unconfirmedResources ?? []; + return cleanupOwner.cleanupIncomplete + ? cleanupOwner.cleanupResources + : []; + }), + ), + ]; + const aggregate = new ProviderCleanupError( + providerId, + resources(), + diagnostics, + { + operation: options.operation, + cause: new AggregateError( + rejections.map(({ cause }) => cause), + options.message, + ), + ...(options.remediationAction === undefined + ? {} + : { remediationAction: options.remediationAction }), + }, + ); + if (owners.length === rejections.length) + return aggregate.bindCleanupDependencies(owners); + // Unconfirmed closes keep the aggregate pending. Known owners still account + // for their own resources as those resources are released. + if (owners.length > 0) + aggregate.bindCleanupResourceOwner({ + get cleanupResources() { + return resources(); + }, + }); + return aggregate; +}; + /** * Follow an existing live owner's cleanup completion from a wrapper error while * keeping its released resource history empty once that owner finishes. If the