Files
reverse-engineering-anything/src/domain/analysisErrorCleanup.ts
T
b6363efc97 refactor(errors): compose multi-part provider cleanup failures through one owner (#12002)
* refactor(errors): compose multi-part provider cleanup failures through one owner

CdpBrowserProvider, CdpElectronProvider, FridaInstrumentationManager and
PlaywrightBrowserScenarioProvider each built the same composite by hand: a
ProviderCleanupError listing the parts' resources, an AggregateError cause,
and bindCleanupDependencies over the parts. providerCleanupErrorFromFailures
in src/domain/analysisErrorCleanup.ts now owns that composition. `causes`
keeps non-AnalysisError failures in the aggregate cause without making them
own completion, as the CDP providers did; `message` keeps the aggregate
messages Frida and Playwright used.

Tests: helper unit tests, new close-composition tests for both CDP providers,
and a Playwright test with two failed cleanup retries. They pass on the base
revision and on this change, and fail when the helper drops the dependency
binding, the extra causes or the message.

Lowers the cleanup-dependency-binding baseline to 43 files / 73 sites.

Refs #11859

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* style(test): format the Playwright cleanup test with oxfmt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(errors): assert cleanup composition facts instead of reason prose

Follows docs/development/testing.md: the close-composition tests now check
provider, operation, resources, aggregate causes and completion, not the
explanatory reason or aggregate message. A harmless wording change passes;
a lost binding, lost cause or wrong operation still fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Anant Mandanka <anant.mandanka@renaissancejewel.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-19 12:51:28 +00:00

144 lines
4.9 KiB
TypeScript

import { ProviderAdapterError } from "./providerAdapterError.js";
import { ProviderCleanupError } from "./providerCleanupError.js";
import { childFailureDiagnostics } from "./analysisErrorProjection.js";
import type { AnalysisError } 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",
): AnalysisError =>
analysisErrorWithCleanupFailure(
primary,
cleanup,
operation,
).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' 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[],
reason: string,
options: {
readonly operation: string;
readonly causes?: readonly unknown[];
readonly message?: string;
},
): ProviderCleanupError =>
new ProviderCleanupError(
providerId,
failures.flatMap((failure) => failure.cleanupResources),
{ reason },
{
operation: options.operation,
cause: new AggregateError(options.causes ?? failures, options.message),
},
).bindCleanupDependencies(failures);
/**
* 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;