mirror of
https://github.com/morluto/rea.git
synced 2026-12-01 19:09:21 +00:00
refactor(cli): share cleanup for retained resource scopes (#14811)
This commit is contained in:
1 parent
7e01f1df85
commit
5fc98e7e18
3 files changed
+43
-36
No files matched your search
+6
-16
@@ -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<ArtifactResourceScope>();
|
||||
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)
|
||||
|
||||
+31
-3
@@ -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<ArtifactResourceScope>();
|
||||
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<void> => {
|
||||
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");
|
||||
};
|
||||
+6
-17
@@ -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<ArtifactResourceScope>();
|
||||
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,
|
||||
|
||||
Reference in new issue
Block a user