Files
rea/src/artifacts/StableArtifactFailure.ts
T
morluto d5e0747025 refactor(process): build infrastructure cancellations with a required reason (#10595)
* refactor(errors): require a reason for every analysis cancellation

Add cancelledBy, the one constructor for AnalysisCancelledError. Its
reason argument is required: an aborted signal, an earlier cancellation,
a reason carried from an inner failure, or an OwnerCancellation when REA
closed the work's owner itself.

Closed adb, apktool, firmware, sqlite, gdb, web-bundle analyzer and
binary-session record owners now report OwnerCancellation.ownerClosed
instead of an absent reason. Firmware's predecessor wait returns its
cancellation as a Result, removing an unreachable reason-less fallback.

An architecture guard rejects new direct constructions, with a counted
per-file baseline that can only shrink.

* refactor(errors): share one aborted-signal predicate with cancelledBy

* refactor(process): return a departing caller's reason from a shared startup wait

* refactor(providers): build decompiler-provider cancellations with a required reason (#10587)

Migrate every AnalysisCancelledError construction under src/ghidra,
src/ida, src/cutter, src/jeb and src/kuna to cancelledBy and lower the
guard baseline from 671 to 572. Closed Kuna target commands report
OwnerCancellation.ownerClosed instead of an absent reason, and Kuna and
JEB close abort their lifetimes with that typed reason.

* refactor(process): build infrastructure cancellations with a required reason

Migrate every AnalysisCancelledError construction in shared process,
provider, artifact, domain, server and CLI code and in the smaller
providers to cancelledBy, lowering the guard baseline from 671 to 566.
Option bags that carried a cause are split so the cancellation branch
cannot receive one.

* fix(errors): read the reason of any signal-shaped cancellation source

* test(ci): cover the counted-baseline comparison behind the architecture guard
2026-11-15 17:05:23 +08:00

165 lines
5.8 KiB
TypeScript

import type { StableArtifactSnapshot } from "./readStableArtifact.js";
import type {
AnalysisError,
AnalysisErrorOptions,
} from "../domain/analysisErrorBase.js";
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { ArtifactReaderFailure } from "./ArtifactReader.js";
import {
artifactFileCancellationCause,
bindArtifactFileCleanup,
type ArtifactFilePrimaryFacts,
} from "./ArtifactFileOperation.js";
import { completedStableArtifactObservation } from "./StableArtifactObservation.js";
import { isSelectedRegularFileError } from "../filesystem/RegularFile.js";
import {
AnalysisAccessDeniedError,
AnalysisArtifactChangedError,
AnalysisInputError,
cancelledBy,
carriedReason,
} from "../domain/analysisErrorCore.js";
/** Preserve ownership classification for stable-file identity and cancellation. */
export const projectOwnedArtifactIdentityFailure = (
facts: ArtifactFilePrimaryFacts,
operation: string,
path: string,
): AnalysisError | undefined => {
const { cause, origin } = facts;
if (
origin !== "owned-or-classified" ||
!(cause instanceof ArtifactReaderFailure)
)
return undefined;
if (cause.reason === "integrity")
return new AnalysisArtifactChangedError(operation, path, cause.message, {
cause,
});
if (cause.reason === "cancelled")
return cancelledBy(
operation,
carriedReason(artifactFileCancellationCause(cause)),
);
return undefined;
};
/** The request field that selected a file: a name, or a path into the request. */
export type RequestField = string | readonly (string | number)[];
const requestFieldPath = (field: RequestField): (string | number)[] =>
typeof field === "string" ? [field] : [...field];
/** Project filesystem errors that identify a refused or unreadable selected file. */
export const projectSelectedArtifactFileFailure = (
cause: unknown,
operation: string,
path: string,
description: string,
field: RequestField = "path",
options: AnalysisErrorOptions = {},
): AnalysisError | undefined => {
if (!(cause instanceof Error) || !("code" in cause)) return undefined;
if (cause.code === "EACCES" || cause.code === "EPERM")
return new AnalysisAccessDeniedError(operation, path, cause.code, {
...options,
cause,
});
if (isSelectedRegularFileError(cause))
return new AnalysisInputError(operation, { ...options, cause }, [
{
path: requestFieldPath(field),
reason: "invalid_value",
message: `Selected ${description} could not be read (${String(cause.code)}): ${path}.`,
},
]);
return undefined;
};
/** Explain the selected image read budget and its fixed upper bound. */
export const imageFileSizeRemediation = (
selectedBytes: number,
maximumBytes: number,
): string =>
selectedBytes >= maximumBytes
? `The image exceeds the ${String(maximumBytes)}-byte input ceiling this operation accepts. This release has no runtime override. Select a smaller artifact.`
: `Raise max_file_bytes above ${String(selectedBytes)} bytes or select a smaller artifact; the limit is the caller-selected input read budget.`;
/** Name an observed regular-file retry that admission will validate again. */
export const regularFileSymlinkRecovery = (resolvedPath: string): string =>
`Retry with the resolved regular file path ${resolvedPath}. The selected symlink is not admitted; the retry will validate the target again.`;
/** Offer the verified regular-file target after a selected symlink is rejected. */
export const stableArtifactInputRecovery = (cause: ArtifactReaderFailure) =>
cause.reason === "path" && cause.resolvedPath !== undefined
? {
remediationAction: regularFileSymlinkRecovery(cause.resolvedPath),
conformsToInputContract: true,
}
: {};
/** Report a rejected artifact path or format with its source-provided recovery. */
export const stableArtifactInputFailure = (
cause: ArtifactReaderFailure,
operation: string,
field: RequestField = "path",
options: AnalysisErrorOptions = {},
): AnalysisInputError =>
new AnalysisInputError(
operation,
{ ...options, cause, ...stableArtifactInputRecovery(cause) },
[
{
path: requestFieldPath(field),
reason: "invalid_format",
message: cause.message,
},
],
);
const artifactCleanupFailure = (
cause: unknown,
providerId: string,
operation: string,
): ProviderCleanupError | undefined => {
if (!(cause instanceof ArtifactReaderFailure) || cause.cleanup === undefined)
return undefined;
return new ProviderCleanupError(
providerId,
cause.cleanup.resources,
{ reason: cause.cleanup.reason },
{ cause, operation },
);
};
/** Project descriptor cleanup while keeping its exact leases bound to the error. */
export const projectArtifactCloseFailure = (
cause: unknown,
providerId: string,
operation: string,
): unknown => {
const error = artifactCleanupFailure(cause, providerId, operation);
if (error === undefined) return cause;
bindArtifactFileCleanup(cause, error);
return error;
};
/** Preserve completed physical identity and exact cleanup ownership around a provider's primary failure. */
export const projectStableArtifactFailure = (
cause: unknown,
primary: AnalysisError,
path: string,
providerId: string,
operation: string,
snapshot?: StableArtifactSnapshot,
): AnalysisError => {
const observation = completedStableArtifactObservation(cause, path, snapshot);
if (observation !== undefined) primary.retainPartialObservation(observation);
const cleanup = artifactCleanupFailure(cause, providerId, operation);
if (cleanup === undefined) return primary;
const error = analysisErrorWithCleanupFailure(primary, cleanup, operation);
bindArtifactFileCleanup(cause, error);
return error;
};