Files
reverse-engineering-anything/tests/composition/directAnalysisEvidenceReuse.test.ts
T
morluto 81fe44d7d8 refactor(evidence): build a completed provider operation's Evidence in one owner (#11673)
* test(adapters): compare CLI and MCP Evidence for every CLI analysis operation

Run each official, workflow, native, artifact and managed operation that
has a CLI command through the CLI command tree and the MCP server over
one simulated provider, and require identical Evidence or failure. The
differences main has today are recorded per operation so the W2 invoke
path can only remove them.

Split bindDirectAnalysis(dependencies) out of createDirectAnalysis so the
CLI can run over injected session owners.

* test(adapters): give each parity case its own CLI and MCP connection

* refactor(evidence): build a completed provider operation's Evidence in one owner

The CLI's DirectAnalysis and the MCP provider-operation handler each
defaulted the subject, admitted the completed analysis and created the
Evidence. completedOperationEvidence in SessionEvidenceReuse now does it
for both, reusing the record the session issued for the same execution
and parameters. The session-Evidence lookup is no longer exported.

* test(evidence): deliver the record a one-shot Ghidra decompile session issued
2026-11-15 22:25:52 -05:00

92 lines
3.3 KiB
TypeScript

import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, vi } from "vitest";
import { runDirectAnalysis } from "../../src/application/DirectAnalysis.js";
import type { DirectAnalysisDependencies } from "../../src/application/DirectAnalysisDependencies.js";
import { BinarySession } from "../../src/application/binary/BinarySession.js";
import { parseConfig } from "../../src/config/parseConfig.js";
import { createAnalysisProfile } from "../../src/domain/analysisProfile.js";
import { ok } from "../../src/domain/result.js";
import {
createAnalysisExecution,
type AnalysisProvider,
} from "../../src/ports/AnalysisProvider.js";
import { createTestProviderRouter } from "../fixtures/binarySession.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
// `rea decompile --snapshot` with Ghidra: the session retains the one-shot
// pseudocode as Evidence, so the CLI has a record to reuse.
const GHIDRA = { id: "ghidra", name: "Ghidra", version: "1" } as const;
const ghidraFixture: AnalysisProvider = {
identity: () => GHIDRA,
capabilities: () => [
{
provider: GHIDRA,
operation: "procedure_pseudo_code",
available: true,
reason: null,
cachePolicy: "snapshot",
effects: {
mutatesArtifact: false,
launchesProcess: false,
mayShowUi: false,
mayAccessNetwork: false,
mayWriteFilesystem: false,
changesPermissions: false,
requiresRoot: false,
},
limitations: [],
},
],
createClient: () => ({
execute: async () => ok(createAnalysisExecution("int main(void);", GHIDRA)),
close: async () => ok(null),
}),
};
it("delivers the record a profiled one-shot session issued and admits it once", async () => {
const directory = await createTestTempDirectory("rea-direct-reuse-");
const path = join(directory, "fixture.com");
await writeFile(path, Buffer.from([0xb8, 0, 0x4c, 0xcd, 0x21]));
const session = new BinarySession(
createTestProviderRouter(ghidraFixture, {
resolveAnalysisProfile: async () =>
ok({ profile: createAnalysisProfile(GHIDRA, {}) }),
}),
);
const admit = vi.spyOn(session, "admitRecoveryObservation");
let issued: string | undefined;
const record = session.recordEvidence.bind(session);
vi.spyOn(session, "recordEvidence").mockImplementation((evidence) => {
issued ??= evidence.evidence_id;
return record(evidence);
});
const create = () => session;
const dependencies: DirectAnalysisDependencies = {
readConfiguration: () => parseConfig({}),
createBinarySession: create,
createNativeDispatchBinarySession: create,
createArtifactBinarySession: create,
createNativeBinarySession: create,
createManagedBinarySession: create,
};
const result = await runDirectAnalysis(
dependencies,
path,
"procedure_pseudo_code",
{ procedure: "0x100" },
{ formatHint: "dos-com", snapshotPath: join(directory, "snapshot.json") },
);
if (!result.ok) throw new Error(JSON.stringify(result.error));
// The CLI delivers the session's record with its own path, not a copy.
expect(issued).toBeDefined();
expect(result.value.evidence_id).toBe(issued);
expect(result.value.caller_input_path).toBe(path);
expect(admit).toHaveBeenCalledTimes(1);
});