Files
reverse-engineering-anything/src/application/DirectAnalysisCleanup.ts
T
Kaoru0822andClaude Opus 5.5 85013da0ce fix(cli): describe one-shot timeouts for an invocation whose session has ended (#8535)
* fix(cli): describe one-shot timeouts for an invocation whose session has ended

A one-shot CLI analysis that reached a provider deadline told its caller
that the Ghidra session remained usable and reported a runtime-editable
limit applying to the next operation, although the command had already
released its provider session and runtime.

The shared error projection now takes an explicit caller lifetime.
Providers keep guidance that holds only for a continuing caller (session
reuse, same-session runtime updates) apart from the request outcome, and
the presentation adds it only for a continuing caller. One-shot owners
(runDirectAnalysis, runProviderAnalysis, finishAnalysisCommand and the
JavaScript application command) project for the invocation: details.limit
reports scope rea-invocation, applies_at next_invocation and the launch
environment seed, and the remediation leads with that setting for a new
invocation. MCP output keeps its runtime-update and same-session guidance.

Covers the Ghidra decompile timeout, decompiler failure, regex stack
exhaustion and startup timeout, the shared regex deadline, the Kuna
decompile timeout and the Hopper startup timeout.

Fixes #8311

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

* fix(cli): project composed child failures for the caller lifetime

Cleanup composition embedded its primary and cleanup failures as
continuing-caller projections, and baked the primary's message and the
cleanup's remediation into the composed error. A one-shot command whose
Ghidra startup timed out while shutdown also failed therefore still
reported a rea-runtime limit and update_analysis_settings in
details.diagnostics.primary_error.

childFailureDiagnostics now snapshots each embedded child at composition
for both lifetimes when its projection depends on the caller, so cleanup
history keeps its composition-time state and the final projection selects
the caller's form. composedCallerGuidance re-attributes message and
remediation with their continuing-only parts kept separate. Error copies
and redaction keep the invocation diagnostics. The Hopper startup timeout
now carries its continuing guidance on the error like the other producers.

Invocation limits keep agent_editable and rea_restart_required, with
values that hold for a new invocation, so only scope and applies_at
change and environment is added.

In existing-project mode a decompile timeout or a cancelled session now
says the next session copies and opens REA_GHIDRA_PROJECT_PATH again
rather than repeating import and analysis.

Tests compare delivered guidance with the owning formatters and constants
instead of freezing prose.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 08:57:32 -08:00

155 lines
5.0 KiB
TypeScript

import type { AnalysisError } from "../domain/analysisErrorBase.js";
import {
childFailureDiagnostics,
projectAnalysisError,
} from "../domain/analysisErrorProjection.js";
import {
analysisErrorUserMessage,
composedCallerGuidance,
} from "../domain/analysisErrorPresentation.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";
import {
tryParseEvidence,
withEvidenceCallerInputPath,
} from "../domain/evidence.js";
/**
* Project a one-shot failure for its invocation, which has released its
* provider session and runtime settings by the time the caller reads it.
*/
export const projectInvocationFailure = (
error: AnalysisError,
): ReturnType<typeof projectAnalysisError> =>
projectAnalysisError(error, "invocation");
/** Add the actual outer request path to authenticated partial Evidence. */
export const projectDirectAnalysisFailure = (
error: AnalysisError,
callerInputPath: string,
): ReturnType<typeof projectAnalysisError> => {
const projection = projectInvocationFailure(error);
const observation = tryParseEvidence(projection.details?.partial_observation);
return observation === undefined
? projection
: {
...projection,
details: {
...projection.details,
partial_observation: withEvidenceCallerInputPath(
observation,
callerInputPath,
),
},
};
};
/** 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);
};
/** Project typed one-shot outcomes only after execution and cleanup settle. */
export const directAnalysisCleanupFailure = (
result: Result<JsonValue, AnalysisError>,
error: AnalysisError,
callerInputPath?: string,
): JsonValue => {
const project = (failure: AnalysisError) =>
callerInputPath === undefined
? projectInvocationFailure(failure)
: projectDirectAnalysisFailure(failure, callerInputPath);
const cleanup = project(error);
if (!result.ok) {
const primary = project(result.error);
const combined = analysisErrorWithCleanupFailure(result.error, error);
return {
error: "Analysis failed",
...primary,
code: "cleanup_incomplete",
retryable: false,
remediation: cleanup.remediation,
details: {
...primary.details,
// The domain composition owns the union of still-retained resources;
// neither projection may overwrite the other owner's cleanup state.
resources: [...combined.cleanupResources],
cleanup: "incomplete",
execution_failure: primary.details?.execution_failure ?? primary.code,
cleanup_error: cleanup,
},
};
}
return {
error: "Analysis failed",
...cleanup,
code: "cleanup_incomplete",
retryable: false,
details: {
...cleanup.details,
cleanup: "incomplete",
partial_observation: result.value,
},
};
};
/** Preserve managed observations or the original typed failure in diagnostics. */
export const managedAnalysisCleanupFailure = (
result: Result<AnalysisExecution, AnalysisError>,
error: AnalysisError,
): Result<AnalysisExecution, AnalysisError> => {
// A close rejection without a live cleanup state has unknown completion;
// an empty dependency set must not turn that rejection into confirmed cleanup.
if (!result.ok) {
const failure = analysisErrorWithCleanupFailure(result.error, error);
return err(
error.cleanupIncomplete
? failure.bindCleanupDependencies([result.error, error])
: failure,
);
}
const failure = new ProviderAdapterError(
error instanceof ProviderAdapterError ? error.providerId : "rea",
"close_binary",
{
cause: error,
...composedCallerGuidance({ remediation: error }),
cleanup: {
reason: analysisErrorUserMessage(error, "invocation"),
resources: error.cleanupResources,
},
...childFailureDiagnostics(
{ cleanup_error: { error } },
{ partial_observation: jsonValueSchema.parse(result.value) },
),
},
);
return err(
error.cleanupIncomplete
? failure.bindCleanupDependencies([error])
: failure,
);
};