Files
rea/src/cli/finishAnalysisCommand.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

167 lines
5.8 KiB
TypeScript

import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
import {
AnalysisError,
type AnalysisPartialObservation,
} from "../domain/analysisErrorBase.js";
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
import {
projectAnalysisError,
projectAnalysisErrorWithoutCapturedOutput,
} from "../domain/analysisErrorProjection.js";
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { err, ok, type Result } from "../domain/result.js";
import { dispatchQueuedInterrupts } from "../process/queuedInterrupts.js";
/** Finish a one-shot CLI operation while retaining primary results through close. */
export const finishAnalysisCommand = async <
Value extends AnalysisPartialObservation,
>(
run: () => Promise<Result<Value, AnalysisError>>,
service: { close(): Promise<void> },
providerId: string,
canonicalOperation: string,
signal: AbortSignal,
fallbackResource?: string,
options: {
readonly failureProjection?: "initial" | "final";
/** Apply invocation-specific exclusions after all owned cleanup is composed. */
readonly redactFailure?: (error: AnalysisError) => AnalysisError;
/** Validate producer-attested completed output against this command's exact result contract. */
readonly recoverCompletedObservation?: (
observation: AnalysisPartialObservation,
) => Value | undefined;
} = {},
): Promise<Result<Value, ReturnType<typeof projectAnalysisError>>> => {
// The command releases its provider resources and runtime before the caller
// reads this result, so guidance needing a continuing session is omitted.
const projectFailure = (error: AnalysisError) =>
projectAnalysisError(options.redactFailure?.(error) ?? error, "invocation");
let result: Result<Value, AnalysisError>;
let cleanup: AnalysisError | undefined;
try {
result = await run();
} catch (cause) {
result = {
ok: false,
error:
cause instanceof AnalysisError
? cause
: new ProviderAdapterError(providerId, canonicalOperation, { cause }),
};
}
// Some one-shot commands promise the original failure snapshot even when
// close retries and mutates the producer's live cleanup status.
const initialFailure =
!result.ok && options.failureProjection === "initial"
? projectFailure(result.error)
: undefined;
const initialDiagnosticFailure =
initialFailure !== undefined &&
!result.ok &&
result.error.capturedOutput !== undefined
? projectAnalysisErrorWithoutCapturedOutput(
options.redactFailure?.(result.error) ?? result.error,
"invocation",
)
: initialFailure;
try {
// Resource owners may resolve their retained errors during close. This
// adapter cannot infer resolution of a producer's earlier cleanup failure.
await service.close();
} catch (cause) {
if (cause instanceof AnalysisError) cleanup = cause;
else {
const observation =
cause instanceof ArtifactReaderFailure
? ArtifactReaderFailure.cleanupObservation(
cause,
fallbackResource ?? `${canonicalOperation} resources`,
)
: {
reason: cause instanceof Error ? cause.message : String(cause),
resources: [],
};
cleanup = new ProviderCleanupError(
providerId,
observation.resources,
{ reason: observation.reason },
{ cause, operation: canonicalOperation },
);
}
}
await dispatchQueuedInterrupts();
if (initialFailure !== undefined && cleanup === undefined)
return err(initialFailure);
if (
initialFailure === undefined &&
!result.ok &&
result.error instanceof ProviderCleanupError &&
!result.error.cleanupIncomplete &&
result.error.cleanupPrimary === undefined &&
result.error.completedObservation !== undefined
) {
const completed = options.recoverCompletedObservation?.(
result.error.completedObservation,
);
if (completed !== undefined) {
const recoveredCleanup = result.error;
result = signal.aborted
? {
ok: false,
error: new AnalysisCancelledError(canonicalOperation, {
cause: signal.reason,
partialObservation: completed,
diagnostics: {
cleanup_history: projectFailure(recoveredCleanup),
},
}),
}
: { ok: true, value: completed };
}
}
if (result.ok && signal.aborted)
result = {
ok: false,
error: new AnalysisCancelledError(canonicalOperation, {
cause: signal.reason,
partialObservation: result.value,
}),
};
if (cleanup !== undefined) {
if (result.ok) cleanup.retainPartialObservation(result.value);
result = {
ok: false,
error: result.ok
? cleanup
: analysisErrorWithCleanupFailure(
result.error,
cleanup,
canonicalOperation,
).bindCleanupDependencies([result.error, cleanup]),
};
}
if (result.ok) return ok(result.value);
const failure = projectFailure(result.error);
const diagnostics = failure.details?.diagnostics;
return err(
initialFailure === undefined || cleanup === undefined
? failure
: {
...failure,
details: {
...failure.details,
diagnostics: {
...(typeof diagnostics === "object" &&
diagnostics !== null &&
!Array.isArray(diagnostics)
? diagnostics
: {}),
primary_error: initialDiagnosticFailure ?? initialFailure,
},
},
},
);
};