diff --git a/src/cli/cliJsonInput.ts b/src/cli/cliJsonInput.ts index ef21b9e1f10..2d24c61cbd3 100644 --- a/src/cli/cliJsonInput.ts +++ b/src/cli/cliJsonInput.ts @@ -19,7 +19,7 @@ import { RegularFileCleanupFailure, regularFileCancellation, } from "../filesystem/OwnedRegularFile.js"; -import { ownCliProcessResource } from "./cliResources.js"; +import { registerCliScopeCleanup } from "./cliResources.js"; import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { AnalysisError } from "../domain/analysisErrorBase.js"; import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js"; @@ -47,20 +47,10 @@ import { reportedSystemCode } from "../kernel/reportedSystemCode.js"; * a handle never loses its owner when parsing returns, and a later read never * inherits an earlier file's cleanup. */ -const retainedJsonInputs = new Set(); -ownCliProcessResource("json-input", async () => { - const failures: unknown[] = []; - for (const resources of retainedJsonInputs) - try { - await resources.close(); - retainedJsonInputs.delete(resources); - } catch (cause: unknown) { - failures.push(cause); - } - if (failures.length === 1) throw failures[0]; - if (failures.length > 1) - throw new AggregateError(failures, "JSON input cleanup failed"); -}); +const retainJsonInput = registerCliScopeCleanup( + "json-input", + "JSON input cleanup failed", +); /** Parse inline JSON, standard input ('-'), or one local JSON file, honoring optional caller cancellation. */ export const parseCliJsonInput = async ( @@ -138,7 +128,7 @@ export const parseCliJsonInput = async ( }; } catch (cause: unknown) { if (cause instanceof RegularFileCleanupFailure) { - retainedJsonInputs.add(resources); + retainJsonInput(resources); return jsonFileCleanupError(value, operation, cause); } if (cause instanceof JsonInputCancellationFailure) diff --git a/src/cli/cliResources.ts b/src/cli/cliResources.ts index b348746956e..0f54b3f02cd 100644 --- a/src/cli/cliResources.ts +++ b/src/cli/cliResources.ts @@ -1,3 +1,5 @@ +import type { ArtifactResourceScope } from "../resources/index.js"; + /** * Process-owned CLI resources. Each owning module registers its closer when it * loads, so a CLI closes only the command families it loaded. @@ -12,6 +14,34 @@ export const ownCliProcessResource = ( if (!closers.has(name)) closers.set(name, close); }; +const throwCleanupFailures = (failures: unknown[], message: string): void => { + if (failures.length === 1) throw failures[0]; + if (failures.length > 1) throw new AggregateError(failures, message); +}; + +/** Register cleanup for one family and retain scopes whose close needs a retry. */ +export const registerCliScopeCleanup = ( + name: string, + failureMessage: string, +) => { + const scopes = new Set(); + ownCliProcessResource(name, async () => { + const failures: unknown[] = []; + for (const scope of scopes) { + try { + await scope.close(); + scopes.delete(scope); + } catch (cause: unknown) { + failures.push(cause); + } + } + throwCleanupFailures(failures, failureMessage); + }); + return (scope: ArtifactResourceScope): void => { + scopes.add(scope); + }; +}; + /** * Settle every process-owned resource, then report each failure. * A closer can throw before it returns a promise. Each call starts on its @@ -24,7 +54,5 @@ export const closeCliResources = async (): Promise => { 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, "CLI resource cleanup failed"); + throwCleanupFailures(failures, "CLI resource cleanup failed"); }; diff --git a/src/cli/toonOutput.ts b/src/cli/toonOutput.ts index d81e8daccfa..36d2dcc81ec 100644 --- a/src/cli/toonOutput.ts +++ b/src/cli/toonOutput.ts @@ -18,25 +18,14 @@ import { AnalysisCleanupLeaseGroup, ArtifactResourceScope, } from "../resources/index.js"; -import { ownCliProcessResource } from "./cliResources.js"; +import { registerCliScopeCleanup } from "./cliResources.js"; import { formatCliOutput } from "./cliOutputFormatter.js"; import { writeOutputParts, writeOutputText } from "./jsonOutput.js"; -const retainedStages = new Set(); -ownCliProcessResource("toon-output", async () => { - const failures: unknown[] = []; - for (const resources of retainedStages) { - try { - await resources.close(); - retainedStages.delete(resources); - } catch (cause: unknown) { - failures.push(cause); - } - } - if (failures.length === 1) throw failures[0]; - if (failures.length > 1) - throw new AggregateError(failures, "TOON staging cleanup failed"); -}); +const retainStage = registerCliScopeCleanup( + "toon-output", + "TOON staging cleanup failed", +); /** The stage owns both its original output failure and retryable cleanup. */ export class ToonOutputCleanupFailure extends ProviderCleanupError { @@ -205,7 +194,7 @@ export const writeToonOutput = async ( if (failure === undefined && lateCancellation !== undefined) failure = { cause: lateCancellation }; if (cleanup.kind === "failed") { - retainedStages.add(resources); + retainStage(resources); retained = true; const error = new ToonOutputCleanupFailure( failure,