Files
reverse-engineering-anything/src/application/SessionWorkflowEvidence.ts
T
morluto cc888f5f7d fix(hopper): report the stalled export function and shell recovery (#14855)
* fix(hopper): report the stalled export function and shell recovery

decompile_all reports a progress update every five seconds while one
function stays pending, naming the function and its position. MCP sends it
as a progress notification and the CLI as a rea_progress line on stderr.

A retained Hopper document now carries remediation that holds after a
one-shot command has ended: Hopper is still running the named operation and
must finish or be quit. The close_binary retry text is kept as guidance for
a continuing caller.

Refs #14840

* fix(hopper): qualify the advice to quit Hopper while it is still working

The recovery text for an ended invocation told the user to quit Hopper to stop
a running operation without the condition its sibling text carries. Hopper is
the user's application and may hold other documents, so the advice now applies
only when it has no other work.

* fix(decompile-all): time only provider requests when reporting a function wait

The wait reporter timed a whole function attempt, including journal appends and file commits, so a slow output volume was reported as the provider still decompiling. It now wraps each provider request (procedure_info and procedure_pseudo_code) and names the request it waits on. The progress test now uses phase gates and releases and joins the export on every path.

* docs(decompile-all): say the wait report times provider requests only

* fix(progress): rate-limit each progress phase separately

The shared reporter dropped any intermediate update within 100 ms of the previous one, whatever its phase. Hopper's one-second request heartbeat could therefore suppress every decompile_all wait report that fired just after it. The interval now applies per phase, so a heartbeat cannot hide a rarer update from another phase. The decompile_all progress test also restores its commitFunctionFile spy.
2026-11-27 05:07:20 -05:00

178 lines
6.0 KiB
TypeScript

import type { BinarySessionPort } from "./binary/BinarySessionPort.js";
import type { ValidatedEnhancedCall } from "./EnhancedToolTypes.js";
import { retainAnalysisResultSources } from "./investigation/recordEvidenceSources.js";
import {
composedLimitationBudget,
createWorkflowEvidence,
executeComposedWorkflow,
recordWorkflowUnknowns,
retainComposedWorkflowSources,
retainPartialWorkflowEvidence,
workflowSnapshotRecord,
} from "./WorkflowEvidence.js";
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import type { Evidence } from "../domain/evidence.js";
import type { JsonValue } from "../domain/jsonValue.js";
import { err, ok, type Result } from "../domain/result.js";
import type {
AnalysisOperationPort,
ExecutionOptions,
} from "../ports/AnalysisProvider.js";
/**
* The session records a provider-backed workflow reads and writes. Without a
* session nothing is retained, recorded or replayed.
*/
export type WorkflowSession = Pick<
BinarySessionPort,
| "activeTarget"
| "analysisProfile"
| "recordEvidence"
| "evidenceForAnalysis"
| "admitRecoveryObservation"
| "recordUnknown"
| "allowsSnapshotReplay"
| "recordWorkflowSnapshot"
>;
/**
* Completed workflow Evidence and, when the workflow recorded it in the
* session, that record's result, so delivery does not record it again.
*/
export interface SessionWorkflowOutcome {
readonly evidence: Evidence;
readonly recorded?: Result<"added" | "duplicate", AnalysisError>;
}
type WorkflowRequest = Required<Pick<ExecutionOptions, "signal" | "progress">>;
/** The parameters Evidence names: the caller's input without absent fields. */
const jsonParameters = (
input: Readonly<Record<string, JsonValue | undefined>>,
): Record<string, JsonValue> =>
Object.fromEntries(
Object.entries(input).filter(
(entry): entry is [string, JsonValue] => entry[1] !== undefined,
),
);
/**
* Run one composed workflow on the admitted analysis and complete its
* Evidence in the session: retain the sources it read (or its partial
* Evidence when it stops), record the completed Evidence, its replayable
* workflow snapshot and its residual unknowns. Every provider read reports
* progress to the caller. The outcome carries the session's record of the
* Evidence.
*/
export const executeSessionWorkflowEvidence = async (
call: ValidatedEnhancedCall,
admitted: AnalysisOperationPort,
session: WorkflowSession | undefined,
{ signal, progress }: WorkflowRequest,
): Promise<Result<SessionWorkflowOutcome, AnalysisError>> => {
const { name } = call;
const parameters = jsonParameters(call.input);
const analysis: AnalysisOperationPort = {
resourceScope: admitted.resourceScope,
execute: (operation, operationParameters, options) =>
admitted.execute(operation, operationParameters, {
...options,
progress,
}),
};
const recordEvidence =
session === undefined
? undefined
: (evidence: Evidence) => session.recordEvidence(evidence);
const evidenceInput = {
target: session?.activeTarget(),
operation: name,
parameters,
upstreamProfile:
session === undefined
? undefined
: (session.analysisProfile() ?? session.analysisProfile(name)),
};
const { EnhancedTools } = await import("./EnhancedTools.js");
const executed = await executeComposedWorkflow(
analysis,
(observed) =>
new EnhancedTools(observed).executeValidated(call, signal, progress),
composedLimitationBudget(name),
);
retainPartialWorkflowEvidence(executed, evidenceInput);
const result = retainComposedWorkflowSources(
recordEvidence,
executed,
evidenceInput,
session === undefined ? undefined : (id) => session.evidenceForAnalysis(id),
session === undefined
? undefined
: (input, operation, additional) =>
session.admitRecoveryObservation(input, operation, additional),
);
if (!result.ok) return result;
const evidence = createWorkflowEvidence({
...evidenceInput,
result: result.value.result,
upstreamLimitations: result.value.limitations,
...(result.value.evidenceLinks === undefined
? {}
: { upstreamEvidenceLinks: result.value.evidenceLinks }),
});
if (session === undefined) return ok({ evidence });
const recorded = session.recordEvidence(evidence);
if (!recorded.ok) return recorded;
if (name !== "trace_native_ui_action" && session.allowsSnapshotReplay(name)) {
const workflowRecord = workflowSnapshotRecord(
evidence,
name,
evidenceInput.upstreamProfile,
result.value.cachePolicy,
);
if (workflowRecord !== undefined) {
const snapshot = session.recordWorkflowSnapshot(workflowRecord);
if (!snapshot.ok) return err(snapshot.error);
}
}
const unknowns = recordWorkflowUnknowns({
name,
result: result.value.result,
evidenceId: evidence.evidence_id,
recordUnknown: (unknown) => session.recordUnknown(unknown),
evidenceRetained: true,
});
return unknowns.ok ? ok({ evidence, recorded }) : unknowns;
};
/**
* Analyze one function on the admitted analysis and retain the Evidence it
* cites in the session.
*/
export const executeSessionFunctionAnalysis = async (
input: Readonly<Record<string, JsonValue | undefined>>,
analysis: AnalysisOperationPort,
session: WorkflowSession | undefined,
request: WorkflowRequest,
): Promise<Result<SessionWorkflowOutcome, AnalysisError>> => {
const { executeFunctionAnalysisEvidence } =
await import("./FunctionAnalysisEvidence.js");
const analyzed = retainAnalysisResultSources(
"analyze_function",
session === undefined
? undefined
: (evidence) => session.recordEvidence(evidence),
await executeFunctionAnalysisEvidence(
analysis,
jsonParameters(input),
session?.activeTarget(),
request,
{
admitRecovery: session?.admitRecoveryObservation.bind(session),
evidenceById: session?.evidenceForAnalysis.bind(session),
},
),
);
return analyzed.ok ? ok({ evidence: analyzed.value }) : analyzed;
};