mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 14:59:05 +00:00
* 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>
231 lines
8.1 KiB
TypeScript
231 lines
8.1 KiB
TypeScript
import type { JsonValue } from "./jsonValue.js";
|
|
import { ProviderAdapterError } from "./providerAdapterError.js";
|
|
import { ProviderCleanupError } from "./providerCleanupError.js";
|
|
import { childFailureDiagnostics } from "./analysisErrorProjection.js";
|
|
import type {
|
|
AnalysisError,
|
|
AnalysisErrorOptions,
|
|
} from "./analysisErrorBase.js";
|
|
import {
|
|
analysisErrorUserMessage,
|
|
composedCallerGuidance,
|
|
} from "./analysisErrorPresentation.js";
|
|
|
|
/**
|
|
* Preserve the original lifecycle failure alongside incomplete cleanup. The
|
|
* composite waits for an owner to bind its completion; compose with
|
|
* `analysisErrorWithCleanupDependencies` when the parts own it.
|
|
*/
|
|
export const analysisErrorWithCleanupFailure = (
|
|
primary: AnalysisError,
|
|
cleanup: AnalysisError,
|
|
operation = "close_binary",
|
|
): AnalysisError =>
|
|
new ProviderAdapterError(
|
|
cleanup instanceof ProviderAdapterError ? cleanup.providerId : "rea",
|
|
operation,
|
|
{
|
|
cleanupPrimary: primary,
|
|
cause: new AggregateError(
|
|
[primary, cleanup],
|
|
"Analysis failed and cleanup remains incomplete.",
|
|
{ cause: primary },
|
|
),
|
|
// A retry offer belongs to the owner of the composed resources, if any.
|
|
...composedCallerGuidance({ message: primary, remediation: cleanup }),
|
|
...(primary.capturedOutput === undefined
|
|
? {}
|
|
: { capturedOutput: primary.capturedOutput }),
|
|
...(primary.partialObservation === undefined
|
|
? {}
|
|
: { partialObservation: primary.partialObservation }),
|
|
cleanup: {
|
|
reason:
|
|
cleanup.cleanup?.reason ??
|
|
analysisErrorUserMessage(cleanup, "invocation"),
|
|
resources: [
|
|
...new Set([
|
|
...(primary.cleanupIncomplete ? primary.cleanupResources : []),
|
|
...cleanup.cleanupResources,
|
|
]),
|
|
],
|
|
},
|
|
...childFailureDiagnostics({
|
|
primary_error: {
|
|
error: primary,
|
|
omitRecovery: true,
|
|
...(primary.capturedOutput === undefined
|
|
? {}
|
|
: { capturedOutput: "omit" as const }),
|
|
},
|
|
cleanup_error: { error: cleanup },
|
|
}),
|
|
},
|
|
);
|
|
|
|
/**
|
|
* Compose a failure with the cleanup failure that followed it. The composite
|
|
* has no cleanup record of its own: it completes once the primary's and the
|
|
* cleanup's records both report release, each read from its owner.
|
|
*/
|
|
export const analysisErrorWithCleanupDependencies = (
|
|
primary: AnalysisError,
|
|
cleanup: AnalysisError,
|
|
operation = "close_binary",
|
|
compose?: (primary: AnalysisError, cleanup: AnalysisError) => AnalysisError,
|
|
): AnalysisError => {
|
|
const failure =
|
|
compose?.(primary, cleanup) ??
|
|
analysisErrorWithCleanupFailure(primary, cleanup, operation);
|
|
// An existing envelope already owns its dependencies. New projections
|
|
// must follow both source errors without acquiring either owner again.
|
|
return failure === cleanup || failure === primary
|
|
? failure
|
|
: failure.bindCleanupDependencies([primary, cleanup]);
|
|
};
|
|
|
|
/**
|
|
* Compose a failure with a close rejection whose completion may be unknown. A
|
|
* rejection without a live cleanup record has unknown completion, not
|
|
* confirmed release, so the composite binds its parts only while the rejection
|
|
* is live. Otherwise it keeps its own pending record and never reads as
|
|
* complete.
|
|
*/
|
|
export const analysisErrorWithCloseRejection = (
|
|
primary: AnalysisError,
|
|
rejection: AnalysisError,
|
|
operation = "close_binary",
|
|
): AnalysisError =>
|
|
rejection.cleanupIncomplete
|
|
? analysisErrorWithCleanupDependencies(primary, rejection, operation)
|
|
: analysisErrorWithCleanupFailure(primary, rejection, operation);
|
|
|
|
/**
|
|
* Compose two close rejections. Each may have unknown completion, so the
|
|
* composite binds them only while both are live.
|
|
*/
|
|
export const analysisErrorWithCloseRejections = (
|
|
first: AnalysisError,
|
|
second: AnalysisError,
|
|
operation = "close_binary",
|
|
): AnalysisError =>
|
|
first.cleanupIncomplete
|
|
? analysisErrorWithCloseRejection(first, second, operation)
|
|
: analysisErrorWithCleanupFailure(first, second, operation);
|
|
|
|
/**
|
|
* Compose several cleanup failures into one provider cleanup failure. It lists
|
|
* the parts' distinct resources and completes once every part reports release. Pass
|
|
* `causes` to keep failures that are not AnalysisErrors in the aggregate
|
|
* cause; only `failures` own completion.
|
|
*/
|
|
export const providerCleanupErrorFromFailures = (
|
|
providerId: string,
|
|
failures: readonly AnalysisError[],
|
|
diagnostics: string | Readonly<Record<string, JsonValue>>,
|
|
options: {
|
|
readonly operation: string;
|
|
readonly causes?: readonly unknown[];
|
|
readonly message?: string;
|
|
readonly remediationAction?: string;
|
|
},
|
|
): ProviderCleanupError =>
|
|
new ProviderCleanupError(
|
|
providerId,
|
|
[...new Set(failures.flatMap((failure) => failure.cleanupResources))],
|
|
typeof diagnostics === "string" ? { reason: diagnostics } : diagnostics,
|
|
{
|
|
operation: options.operation,
|
|
cause: new AggregateError(options.causes ?? failures, options.message),
|
|
...(options.remediationAction === undefined
|
|
? {}
|
|
: { remediationAction: options.remediationAction }),
|
|
},
|
|
).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<Record<string, JsonValue>>,
|
|
options: {
|
|
readonly operation: string;
|
|
readonly message: string;
|
|
readonly remediationAction?: string;
|
|
readonly invocationDiagnostics?: AnalysisErrorOptions["invocationDiagnostics"];
|
|
},
|
|
): 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,
|
|
...(options.invocationDiagnostics === undefined
|
|
? {}
|
|
: { invocationDiagnostics: options.invocationDiagnostics }),
|
|
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
|
|
* owner is not live (`!owner.cleanupIncomplete`), any pending cleanup on the
|
|
* wrapper stays unknown.
|
|
*/
|
|
export const withInheritedCleanupOwnership = <Failure extends AnalysisError>(
|
|
failure: Failure,
|
|
owner: AnalysisError,
|
|
): Failure =>
|
|
owner.cleanupIncomplete ? failure.bindCleanupDependencies([owner]) : failure;
|
|
|
|
/** Delegate both cleanup completion and live resource reporting to `owner`. */
|
|
export const withDelegatedCleanupOwner = <Failure extends AnalysisError>(
|
|
failure: Failure,
|
|
owner: AnalysisError,
|
|
): Failure =>
|
|
failure.bindCleanupDependencies([owner]).bindCleanupResourceOwner(owner);
|
|
|
|
/** Retain `cleanup` as a secondary failure and follow its cleanup completion. */
|
|
export const withSecondaryCleanupFailure = <Failure extends AnalysisError>(
|
|
primary: Failure,
|
|
cleanup: AnalysisError,
|
|
): Failure =>
|
|
primary.retainSecondaryFailure(cleanup).bindCleanupDependencies([cleanup]);
|