* fix(cleanup): preserve owner recovery instructions Keep cleanup remediation through combined errors and direct or managed one-shot analysis while retaining primary diagnostics. Refs: #4227 * fix(hopper): recover late startup and retained cleanup Authenticate initial and cleanup handshakes through the same identity owner. Expose startup and cleanup activity, retain health after target retirement, and expire abandoned unauthenticated listeners without timing out authenticated work. Refs: #4227 * fix(providers): retain composite lifecycle facts across retries Preserve observed child health with incomplete peer coverage, retry only unresolved clients, and retain all child cleanup recovery actions. Refs: #4233 * fix(process): honor cancellation through capture cleanup Check the request signal after cleanup and before final publication, retaining completed observations under typed cancellation. Refs: #4005 * fix(native): preserve failed LLDB launch ownership and exit proof Publish failed-launch process identity and observed exit separately from object validity. Retire only the exact owned process and retain uncertainty when exit cannot be established. Refs: #4232 * fix(native): honor cancellation after trace finalization Check cancellation after runtime-root cleanup and retain collected trace observations as partial cancellation evidence. Refs: #4009 * fix(cli): preserve analysis results when provider cleanup fails Reuse the existing one-shot finalizer across thirteen command families to retain completed Evidence and primary diagnostics on close failure. Cover success, denied cleanup, and combined primary/cleanup failure through the public CLI. Refs: #3935 * docs(lifecycle): define completion invariants and regression scenarios Document distinct ownership/readiness/exit facts, late cancellation and atomic publication boundaries, partial child cleanup, and diagnostic retention. Link representative regressions and route contributor guidance through AGENTS.md. Refs: #4227 * fix(session): retain run identity through pending cleanup * fix(cli): preserve original cleanup resource diagnostics * fix(native): retain completed facts through finalization failure * fix(providers): report retained cleanup paths at their owners
102 lines
3.6 KiB
TypeScript
102 lines
3.6 KiB
TypeScript
import { analysisErrorProjectionSchema } from "../contracts/errorSchemas.js";
|
|
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
|
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
|
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
|
|
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
|
|
import { err, type Result } from "../domain/result.js";
|
|
import type { AnalysisExecution } from "./AnalysisProvider.js";
|
|
import type { BinarySession } from "./binary/BinarySession.js";
|
|
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
|
|
|
|
/** Await one-shot cleanup and retain either the outcome or both failures. */
|
|
export const withSessionCleanup = async <Value>(
|
|
session: BinarySession,
|
|
operation: () => Promise<Value>,
|
|
onCleanupFailure: (value: Value, error: AnalysisError) => Value,
|
|
): Promise<Value> => {
|
|
let value: Value;
|
|
try {
|
|
value = await operation();
|
|
} catch (cause: unknown) {
|
|
const closed = await session.close();
|
|
if (!closed.ok)
|
|
throw new AggregateError(
|
|
[cause, closed.error],
|
|
"Analysis and provider cleanup both failed",
|
|
{ cause },
|
|
);
|
|
throw cause;
|
|
}
|
|
const closed = await session.close();
|
|
return closed.ok ? value : onCleanupFailure(value, closed.error);
|
|
};
|
|
|
|
/** Keep the primary CLI failure or completed Evidence alongside cleanup failure. */
|
|
export const directAnalysisCleanupFailure = (
|
|
output: JsonValue,
|
|
error: AnalysisError,
|
|
): JsonValue => {
|
|
const cleanup = projectAnalysisError(error);
|
|
if (typeof output === "object" && output !== null && !Array.isArray(output)) {
|
|
const { error: label, ...projection } = output;
|
|
const primary = analysisErrorProjectionSchema.safeParse(projection);
|
|
if (typeof label === "string" && primary.success)
|
|
return {
|
|
error: label,
|
|
...primary.data,
|
|
code: "cleanup_incomplete",
|
|
retryable: false,
|
|
remediation: cleanup.remediation,
|
|
details: {
|
|
// Primary fields describe the original failure; the later close
|
|
// assessment, including its resources, belongs to cleanup_error.
|
|
resources: error.cleanupResources.slice(),
|
|
...primary.data.details,
|
|
cleanup: "incomplete",
|
|
execution_failure:
|
|
primary.data.details !== undefined &&
|
|
"execution_failure" in primary.data.details
|
|
? (primary.data.details.execution_failure ?? primary.data.code)
|
|
: primary.data.code,
|
|
cleanup_error: cleanup,
|
|
},
|
|
};
|
|
}
|
|
return {
|
|
error: "Analysis failed",
|
|
...cleanup,
|
|
code: "cleanup_incomplete",
|
|
retryable: false,
|
|
details: {
|
|
...cleanup.details,
|
|
cleanup: "incomplete",
|
|
partial_observation: output,
|
|
},
|
|
};
|
|
};
|
|
|
|
/** Preserve managed observations or the original typed failure in diagnostics. */
|
|
export const managedAnalysisCleanupFailure = (
|
|
result: Result<AnalysisExecution, AnalysisError>,
|
|
error: AnalysisError,
|
|
): Result<AnalysisExecution, AnalysisError> => {
|
|
if (!result.ok)
|
|
return err(analysisErrorWithCleanupFailure(result.error, error));
|
|
const cleanup = projectAnalysisError(error);
|
|
return err(
|
|
new ProviderAdapterError(
|
|
error instanceof ProviderAdapterError ? error.providerId : "rea",
|
|
"close_binary",
|
|
{
|
|
cause: error,
|
|
remediationAction: cleanup.remediation.action,
|
|
cleanup: { reason: cleanup.message, resources: error.cleanupResources },
|
|
diagnostics: {
|
|
cleanup_error: cleanup,
|
|
partial_observation: jsonValueSchema.parse(result.value),
|
|
},
|
|
},
|
|
),
|
|
);
|
|
};
|