fix(session): serialize snapshot lifecycle with admitted requests

This commit is contained in:
morluto
2026-10-09 04:37:43 +08:00
parent 8d39eae6b9
commit e44cb45908
11 changed files with 587 additions and 148 deletions
+11 -2
View File
@@ -9,6 +9,7 @@ import Ajv from "ajv/dist/2020.js";
import addFormats from "ajv-formats";
import { mcpTextValue, requireMcpResult } from "./mcp-verifier-results.mjs";
import { verifyLegacyGhidraReferenceSnapshot } from "./ghidra-reference-snapshot-e2e.mjs";
import { verifyGhidraSnapshotLifecycle } from "./real-ghidra-snapshot-lifecycle.mjs";
/** Probe real Ghidra location, annotation and error contracts through public adapters. */
export async function verifyGhidraBoundaries(
@@ -639,10 +640,16 @@ export async function verifyGhidraBoundaries(
selected,
);
}
await call("close_binary");
const snapshotLifecycle = await verifyGhidraSnapshotLifecycle(
client,
target,
await call("analyze_function", { procedure: address }),
cli,
);
successfulCalls += snapshotLifecycle.successfulCalls;
rejectedCalls += snapshotLifecycle.rejectedCalls;
await assert.rejects(access(socketRoot), { code: "ENOENT" });
await assert.rejects(access(runtimeRoot), { code: "ENOENT" });
await call("open_binary", { path: target.path, provider_id: "ghidra" });
assert.equal(await call("procedure_address", { procedure: name }), address);
assert.equal(await call("address_name", { address }), name);
assert.equal(
@@ -674,6 +681,8 @@ export async function verifyGhidraBoundaries(
complete_function_body_references: true,
ambiguity_candidates_inline: true,
legacy_reference_snapshot_rejected: true,
mutation_snapshot_lifecycle: true,
concurrent_annotation_snapshot_close: true,
source_immutable: true,
reopen_discards_edits: true,
long_tmpdir_private_socket_cleanup: true,
@@ -0,0 +1,180 @@
import assert from "node:assert/strict";
import { access, mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { parseAnalysisSnapshot } from "../../dist/domain/analysisSnapshot.js";
import { parseEvidenceBundle } from "../../dist/domain/evidenceBundle.js";
import { requireMcpResult } from "./mcp-verifier-results.mjs";
/** Keep mutable Ghidra observations out of immutable snapshots across target opens. */
export async function verifyGhidraSnapshotLifecycle(
client,
target,
edited,
cli,
) {
const workspace = await mkdtemp(join(tmpdir(), "rea-ghidra-snapshot-proof-"));
let successfulCalls = 0;
let rejectedCalls = 0;
const call = async (name, args = {}) => {
const result = requireMcpResult(
await client.callTool({ name, arguments: args }, { timeout: 180000 }),
name,
);
successfulCalls++;
return result;
};
const rejectSnapshot = async (name, args) => {
const reply = await client.callTool({ name, arguments: args });
assert.equal(
reply.isError,
true,
`${name} accepted a mutated snapshot binding`,
);
const error = reply.structuredContent?.error;
assert.equal(
error?.code,
"evidence_integrity_mismatch",
JSON.stringify(reply),
);
assert.match(error.message, /after analysis metadata mutations/u);
assert.match(
error.remediation.action,
/close.*without saving a snapshot/iu,
);
assert.match(error.remediation.action, /export_evidence_bundle/u);
rejectedCalls++;
};
try {
const entry = edited.procedure.address;
const snapshotPath = join(workspace, "original.json");
// The CLI has its own unmodified database and writes the production snapshot format.
const original = await cli("function", entry, ["--snapshot", snapshotPath]);
const snapshotBytes = await readFile(snapshotPath);
const snapshot = parseAnalysisSnapshot(
JSON.parse(snapshotBytes.toString("utf8")),
);
assert.equal(snapshot.target.sha256, target.sha256);
assert.notEqual(original.procedure.name, edited.procedure.name);
const runId = (await call("binary_session")).analysis_run.run_id;
const api = await call("inspect_native_api", { procedure: entry });
assert.equal(api.procedure.name, edited.procedure.name);
for (const repeatedOpen of [false, true]) {
if (repeatedOpen)
await call("open_binary", { path: target.path, provider_id: "ghidra" });
const destination = join(
workspace,
repeatedOpen ? "repeated.json" : "edited.json",
);
await rejectSnapshot("close_binary", { snapshot_path: destination });
await assert.rejects(access(destination), { code: "ENOENT" });
assert.equal((await call("binary_session")).analysis_run.run_id, runId);
assert.deepEqual(
await call("analyze_function", { procedure: entry }),
edited,
);
}
await rejectSnapshot("open_binary", {
path: target.path,
provider_id: "ghidra",
snapshot_path: snapshotPath,
});
assert.deepEqual(await readFile(snapshotPath), snapshotBytes);
assert.equal((await call("binary_session")).analysis_run.run_id, runId);
assert.deepEqual(
await call("analyze_function", { procedure: entry }),
edited,
);
// Mutable results remain exportable as observations, with their mutation Evidence.
const bundlePath = join(workspace, "observations.json");
await call("export_evidence_bundle", { path: bundlePath });
const bundle = parseEvidenceBundle(
JSON.parse(await readFile(bundlePath, "utf8")),
);
assert.ok(
bundle.records.some(
(record) => record.operation === "annotate_native_function",
),
);
assert.ok(
bundle.records.some(
(record) =>
record.operation === "inspect_native_api" &&
record.normalized_result.procedure.name === edited.procedure.name,
),
);
await call("close_binary");
await call("open_binary", {
path: target.path,
provider_id: "ghidra",
snapshot_path: snapshotPath,
});
const restored = await call("analyze_function", { procedure: entry });
assert.deepEqual(restored.procedure, original.procedure);
assert.deepEqual(restored.comments, original.comments);
assert.notEqual((await call("binary_session")).analysis_run.run_id, runId);
const freshPath = join(workspace, "fresh.json");
await call("close_binary", { snapshot_path: freshPath });
const fresh = parseAnalysisSnapshot(
JSON.parse(await readFile(freshPath, "utf8")),
);
assert.ok(
!fresh.evidence_bundle.records.some(
(record) =>
record.operation === "annotate_native_function" ||
(record.operation === "inspect_native_api" &&
record.normalized_result.procedure.name === edited.procedure.name),
),
);
await call("open_binary", {
path: target.path,
provider_id: "ghidra",
snapshot_path: freshPath,
});
await verifyConcurrentSnapshotClose({
call,
rejectSnapshot,
workspace,
target,
entry,
original,
});
return { successfulCalls, rejectedCalls };
} finally {
await rm(workspace, { recursive: true, force: true });
}
}
async function verifyConcurrentSnapshotClose({
call,
rejectSnapshot,
workspace,
target,
entry,
original,
}) {
const destination = join(workspace, "concurrent.json");
const annotation = call("annotate_native_function", {
procedure: entry,
name: "rea_concurrent_snapshot_edit",
comment:
"A successful edit must invalidate a concurrently requested snapshot.",
});
const close = rejectSnapshot("close_binary", { snapshot_path: destination });
const [updated] = await Promise.all([annotation, close]);
await assert.rejects(access(destination), { code: "ENOENT" });
assert.equal((await call("binary_session")).open, true);
assert.deepEqual(
await call("analyze_function", { procedure: entry }),
updated.dossier,
);
// Discard the ephemeral edit and leave the caller with a pristine database.
await call("close_binary");
await call("open_binary", { path: target.path, provider_id: "ghidra" });
assert.deepEqual(
(await call("analyze_function", { procedure: entry })).procedure,
original.procedure,
);
}