Files
rea/src/cli/cliResources.ts
T
Sandiv f5055cd2ed fix(process): report denied inspection as typed cleanup failures (#9261)
* fix(process): report denied inspection as typed cleanup failures

Preserve the native ps diagnostic through owned-worker cleanup and finalize one-shot JavaScript analysis before Evidence delivery. Retain mandatory inspection and existing cleanup authority.

Preserve the inspector diagnostic when the initial process-table read succeeds but pre-signal revalidation fails, and remove the unreachable typed-error branch so retained artifact retry failures project through the existing cleanup contract.

* fix(resources): preserve independent close failure ownership

* fix(cli): retain cleanup recovery and nested close evidence

* refactor(providers): classify rejected closes at the shared boundary

* test(cli): preserve bundle metadata and physical inspection failures

* fix(cli): preserve the JavaScript failure envelope after finalization

* test(cli): validate the JavaScript error envelope at its boundary

* fix(providers): preserve unnamed close failure resources

* fix(cli): retain invocation owners through outer cleanup

* test(cli): distinguish reported cleanup failures from uncaught errors

* fix(cli): compose each runtime’s declared cleanup owners

* test(cli): preserve the original cleanup rejection

* test(cli): allow cleanup retry recovery after a reported failure

* fix(cli): report final cleanup after returned results

* fix(cli): preserve launcher executable mode

* test(cli): retain invocation cleanup in file sessions

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-11-30 19:38:45 +08:00

77 lines
2.4 KiB
TypeScript

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.
*/
const closers = new Map<string, () => Promise<void>>();
/** Register an owner's closer. A repeated name keeps its first closer. */
export const ownCliProcessResource = (
name: string,
close: () => Promise<void>,
): void => {
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
* own turn so that failure stays with its owner and the others still settle.
*/
const settleCliResources = async (
owners: readonly (() => Promise<void>)[],
): Promise<void> => {
const results = await Promise.allSettled(
owners.map((close) => Promise.resolve().then(close)),
);
const failures = results.flatMap((result) =>
result.status === "rejected" ? [result.reason] : [],
);
throwCleanupFailures(failures, "CLI resource cleanup failed");
};
export const closeCliResources = (): Promise<void> =>
settleCliResources([...closers.values()]);
/** Retain all runtime owners and share each attempt until every owner settles. */
export const createCliResourceCloser = (
...owners: readonly (() => Promise<void>)[]
): (() => Promise<void>) => {
let closing: Promise<void> | undefined;
return () => {
closing ??= settleCliResources(owners).finally(() => {
closing = undefined;
});
return closing;
};
};