feat!: make evidence bundles canonical MCP resources

Add immutable session bundle snapshots, bounded resource discovery, paginated unknowns, mutable current analysis snapshots, and provider-neutral navigation/address context tools.

BREAKING CHANGE: export_evidence_bundle now requires a filesystem path and no longer returns an inline bundle.
This commit is contained in:
morluto
2026-07-29 17:36:40 +00:00
parent 3e1e8cd3f8
commit 1712e6d821
27 changed files with 901 additions and 291 deletions
+140
View File
@@ -0,0 +1,140 @@
import { describe, expect, it } from "vitest";
import {
getNavigationContext,
inspectAddressContext,
} from "../src/application/AnalysisContextQueries.js";
import {
createAnalysisExecution,
type AnalysisOperationPort,
} from "../src/application/AnalysisProvider.js";
import { AnalysisProtocolError } from "../src/domain/errors.js";
import { err, ok } from "../src/domain/result.js";
const provider = { id: "fixture", name: "Fixture", version: "1" };
describe("analysis context queries", () => {
it("aggregates volatile navigation and treats a missing procedure as null", async () => {
const calls: {
operation: string;
parameters: Readonly<Record<string, unknown>>;
}[] = [];
const analysis: AnalysisOperationPort = {
execute: (operation, parameters) => {
calls.push({ operation, parameters });
if (operation === "current_document")
return Promise.resolve(
ok(createAnalysisExecution("fixture", provider)),
);
if (operation === "current_address")
return Promise.resolve(
ok(createAnalysisExecution("0x401000", provider)),
);
if (operation === "current_procedure")
return Promise.resolve(
err(new AnalysisProtocolError("cursor is not in a procedure")),
);
return Promise.resolve(
err(new AnalysisProtocolError(`unexpected ${operation}`)),
);
},
};
await expect(getNavigationContext(analysis, {})).resolves.toEqual(
ok({
document: "fixture",
address: "0x401000",
procedure: null,
}),
);
expect(calls).toContainEqual({
operation: "current_document",
parameters: {},
});
});
it("uses an explicit document without passing it to current_document and preserves procedure failures", async () => {
const calls: string[] = [];
const failure = new AnalysisProtocolError("provider response malformed");
const analysis: AnalysisOperationPort = {
execute: (operation) => {
calls.push(operation);
if (operation === "current_address")
return Promise.resolve(
ok(createAnalysisExecution("0x401000", provider)),
);
return Promise.resolve(err(failure));
},
};
await expect(
getNavigationContext(analysis, { document: "fixture" }),
).resolves.toEqual(err(failure));
expect(calls).toEqual(["current_address", "current_procedure"]);
});
it("keeps unsupported address facets local and filters bookmarks", async () => {
const bookmarkInputs: Readonly<Record<string, unknown>>[] = [];
const analysis: AnalysisOperationPort = {
execute: (operation, input) => {
if (operation === "comment")
return Promise.resolve(
err(new AnalysisProtocolError("comments unavailable")),
);
switch (operation) {
case "address_name":
return Promise.resolve(
ok(createAnalysisExecution("entry", provider)),
);
case "resolve_containing_procedure":
return Promise.resolve(
ok(
createAnalysisExecution(
{ address: "0x401000", name: "main" },
provider,
),
),
);
case "inline_comment":
return Promise.resolve(ok(createAnalysisExecution(null, provider)));
case "list_bookmarks":
bookmarkInputs.push(input);
return Promise.resolve(
ok(
createAnalysisExecution(
[
{ address: "0x401000", value: "review" },
{ address: "0x402000", value: "other" },
],
provider,
),
),
);
default:
return Promise.resolve(
err(new AnalysisProtocolError(`unexpected ${operation}`)),
);
}
},
};
const result = await inspectAddressContext(analysis, {
address: "0x401000",
});
expect(result).toMatchObject({
ok: true,
value: {
name: { state: "available", value: "entry" },
comment: {
state: "unavailable",
reason: "comments unavailable",
},
bookmarks: {
state: "available",
value: [{ address: "0x401000", value: "review" }],
},
},
});
expect(bookmarkInputs).toEqual([{}]);
});
});
+22 -8
View File
@@ -430,17 +430,31 @@ describe("artifact graph MCP integration", () => {
arguments: { domain: "artifact-comparison" },
});
expect(unknowns.structuredContent).toMatchObject({
result: [expect.objectContaining({ domain: "artifact-comparison" })],
result: {
items: [
expect.objectContaining({
unknown: expect.objectContaining({
domain: "artifact-comparison",
}),
}),
],
},
});
const exported = await client.callTool({
name: "export_evidence_bundle",
const snapshotted = await client.callTool({
name: "snapshot_evidence_bundle",
arguments: {},
});
const envelope = z
.object({ result: evidenceBundleSchema })
.parse(exported.structuredContent);
expect(envelope.result.records).toHaveLength(6);
expect(envelope.result.artifacts).toContainEqual({
const bundleUri = z
.object({ result: z.object({ bundle_uri: z.string() }) })
.parse(snapshotted.structuredContent).result.bundle_uri;
const bundleResource = await client.readResource({ uri: bundleUri });
const bundle = evidenceBundleSchema.parse(
JSON.parse(
z.object({ text: z.string() }).parse(bundleResource.contents[0]).text,
),
);
expect(bundle.records).toHaveLength(6);
expect(bundle.artifacts).toContainEqual({
digest: { sha256: evidence.subject?.digest.sha256 },
format: "ipa",
architecture: null,
+4 -1
View File
@@ -167,7 +167,7 @@ describe("tool contract surface", () => {
expect(Object.keys(TOOL_EFFECTS).sort()).toEqual(names.sort());
});
it("keeps exactly nineteen additive session contracts", () => {
it("keeps exactly twenty-two additive session contracts", () => {
expect(
SESSION_TOOL_CONTRACTS.map(({ name, kind }) => ({ name, kind })),
).toEqual([
@@ -190,6 +190,9 @@ describe("tool contract surface", () => {
{ name: "update_unknown", kind: "session" },
{ name: "verify_unknown_resolution", kind: "session" },
{ name: "run_replay_machine", kind: "session" },
{ name: "snapshot_evidence_bundle", kind: "session" },
{ name: "get_navigation_context", kind: "session" },
{ name: "inspect_address_context", kind: "session" },
]);
expect(
SESSION_TOOL_CONTRACTS.find(
+69 -9
View File
@@ -1,5 +1,6 @@
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { describe, expect, it } from "vitest";
import { z } from "zod";
import { BinarySession } from "../src/application/BinarySession.js";
import { createEvidence } from "../src/domain/evidence.js";
@@ -11,6 +12,41 @@ import { createInvestigationWorkspace } from "../src/domain/investigationWorkspa
const provider = { id: "fixture", name: "Fixture", version: "1" };
describe("evidence MCP resources", () => {
it("retains bundles without eviction and rejects a seventeenth digest", () => {
const session = new BinarySession(() => ({
health: () => Promise.resolve(),
execute: () => Promise.resolve(observed(null)),
close: () => Promise.resolve(),
}));
const first = session.snapshotEvidenceBundle();
expect(first.ok).toBe(true);
for (let index = 0; index < 15; index += 1) {
session.recordEvidence(
createEvidence(undefined, provider, {
operation: `retention_probe_${String(index)}`,
parameters: {},
result: index,
}),
);
expect(session.snapshotEvidenceBundle().ok).toBe(true);
}
session.recordEvidence(
createEvidence(undefined, provider, {
operation: "retention_probe_overflow",
parameters: {},
result: true,
}),
);
expect(session.snapshotEvidenceBundle()).toMatchObject({
ok: false,
error: { _tag: "EvidenceLimitError", limit: "records", maximum: 16 },
});
if (first.ok)
expect(session.retainedEvidenceBundle(first.value.bundleDigest)).toEqual(
expect.any(String),
);
});
it("lists and reads only evidence owned by the current session", async () => {
const session = new BinarySession(() => ({
health: () => Promise.resolve(),
@@ -71,7 +107,7 @@ describe("evidence MCP resources", () => {
expect.arrayContaining([
"rea://artifact/{manifestId}/{collection}",
"rea://function/{targetSha256}/{address}",
"rea://snapshot/{snapshotDigest}",
"rea://evidence-bundle/{bundleDigest}",
"rea://workspace/{workspaceId}/revision/{revision}",
]),
);
@@ -82,6 +118,11 @@ describe("evidence MCP resources", () => {
);
const listed = await client.listResources();
expect(listed.resources).toContainEqual(
expect.objectContaining({
uri: "rea://snapshot/current",
}),
);
expect(listed.resources).not.toContainEqual(
expect.objectContaining({
uri: `rea://evidence/${evidence.evidence_id}`,
}),
@@ -96,6 +137,33 @@ describe("evidence MCP resources", () => {
text: expect.stringContaining(evidence.evidence_id),
}),
]);
const firstSnapshot = await client.callTool({
name: "snapshot_evidence_bundle",
arguments: {},
});
const secondSnapshot = await client.callTool({
name: "snapshot_evidence_bundle",
arguments: {},
});
expect(secondSnapshot.structuredContent).toEqual(
firstSnapshot.structuredContent,
);
expect(firstSnapshot.content).toContainEqual(
expect.objectContaining({
type: "text",
text: expect.stringContaining("Copy the opaque URI exactly"),
}),
);
const bundleUri = z
.object({ result: z.object({ bundle_uri: z.string() }) })
.parse(firstSnapshot.structuredContent).result.bundle_uri;
const retainedBundle = await client.readResource({ uri: bundleUri });
expect(retainedBundle.contents[0]).toEqual(
expect.objectContaining({
uri: bundleUri,
text: expect.stringContaining(evidence.evidence_id),
}),
);
const terminal = await client.readResource({
uri: `rea://evidence/${capture.evidence_id}/section/terminal`,
});
@@ -119,14 +187,6 @@ describe("evidence MCP resources", () => {
text: expect.stringContaining(unknown.value.revision_digest),
}),
);
const active = await client.readResource({
uri: "rea://unknowns/active",
});
expect(active.contents[0]).toEqual(
expect.objectContaining({
text: expect.stringContaining(unknown.value.unknown_id),
}),
);
}
await expect(
client.readResource({ uri: `rea://evidence/ev_${"0".repeat(64)}` }),
+14 -10
View File
@@ -49,17 +49,21 @@ describe("function comparison MCP integration", () => {
arguments: { domain: "function-comparison" },
});
expect(unknowns.structuredContent).toMatchObject({
result: [
expect.objectContaining({
domain: "function-comparison",
recommended_probes: [
expect.objectContaining({
rationale:
"Capture complete dossiers for both functions under the same target context and analysis limits.",
result: {
items: [
expect.objectContaining({
unknown: expect.objectContaining({
domain: "function-comparison",
recommended_probes: [
expect.objectContaining({
rationale:
"Capture complete dossiers for both functions under the same target context and analysis limits.",
}),
],
}),
],
}),
],
}),
],
},
});
} finally {
await Promise.allSettled([
+20 -16
View File
@@ -265,7 +265,7 @@ describe("Ghidra MCP and shared CLI composition", () => {
arguments: {},
})
).structuredContent,
).toMatchObject({ result: [] });
).toMatchObject({ result: { items: [] } });
const approvedInspection = sessionEvidence(
session,
@@ -294,22 +294,26 @@ describe("Ghidra MCP and shared CLI composition", () => {
})
).structuredContent,
).toMatchObject({
result: [
{
status: "open",
domain: "native-api",
question: expect.stringContaining(
"additional data sources or targets",
),
supporting_evidence_ids: [approvedInspection.evidence_id],
recommended_probes: [
{
operation: "inspect_native_api",
rationale: expect.stringContaining("ABI probe"),
result: {
items: [
{
unknown: {
status: "open",
domain: "native-api",
question: expect.stringContaining(
"additional data sources or targets",
),
supporting_evidence_ids: [approvedInspection.evidence_id],
recommended_probes: [
{
operation: "inspect_native_api",
rationale: expect.stringContaining("ABI probe"),
},
],
},
],
},
],
},
],
},
});
} finally {
await harness.close();
+39 -27
View File
@@ -352,13 +352,17 @@ describe("full MCP integration with multi-tool sequences", () => {
await client.callTool({ name: "list_unknowns", arguments: {} }),
);
expect(listed).toMatchObject({
result: [
{
status: "open",
domain: "control-flow",
question: "Investigation reached the operation budget.",
},
],
result: {
items: [
{
unknown: {
status: "open",
domain: "control-flow",
question: "Investigation reached the operation budget.",
},
},
],
},
});
});
@@ -412,19 +416,23 @@ describe("full MCP integration with multi-tool sequences", () => {
await client.callTool({ name: "list_unknowns", arguments: {} }),
),
).toMatchObject({
result: [
{
domain: "analysis-capability",
question:
"The requested analysis is unavailable for the current target.",
recommended_probes: [
{
rationale:
"Choose another analysis or target that can answer this question.",
result: {
items: [
{
unknown: {
domain: "analysis-capability",
question:
"The requested analysis is unavailable for the current target.",
recommended_probes: [
{
rationale:
"Choose another analysis or target that can answer this question.",
},
],
},
],
},
],
},
],
},
});
});
@@ -510,14 +518,18 @@ describe("full MCP integration with multi-tool sequences", () => {
await client.callTool({ name: "list_unknowns", arguments: {} }),
),
).toMatchObject({
result: [
{
status: "contradicted",
domain: "process-comparison",
question: "Process captures disagree across: interaction, shim",
contradicting_evidence_ids: [rightEvidence.evidence_id],
},
],
result: {
items: [
{
unknown: {
status: "contradicted",
domain: "process-comparison",
question: "Process captures disagree across: interaction, shim",
contradicting_evidence_ids: [rightEvidence.evidence_id],
},
},
],
},
});
});
+46 -21
View File
@@ -175,11 +175,21 @@ describe("target-free MCP lifecycle", () => {
expect(
text(await mcp.callTool({ name: "current_document", arguments: {} })),
).toContain("first.hop");
const exported = await mcp.callTool({
name: "export_evidence_bundle",
const snapshotted = await mcp.callTool({
name: "snapshot_evidence_bundle",
arguments: {},
});
const bundle = evidenceBundleSchema.parse(structured(exported).result);
const snapshotResult = z
.object({ result: z.object({ bundle_uri: z.string() }) })
.parse(structured(snapshotted)).result;
const bundleResource = await mcp.readResource({
uri: snapshotResult.bundle_uri,
});
const bundle = evidenceBundleSchema.parse(
JSON.parse(
z.object({ text: z.string() }).parse(bundleResource.contents[0]).text,
),
);
expect(bundle.records).toHaveLength(1);
const evidencePath = join(directory, "evidence.json");
expect(
@@ -221,7 +231,7 @@ describe("target-free MCP lifecycle", () => {
).result,
).toEqual({ imported: 1, total: 2 });
await new Promise<void>((resolve) => setImmediate(resolve));
expect(resourceListChanges).toBe(changesBeforeMutation + 1);
expect(resourceListChanges).toBe(changesBeforeMutation);
changesBeforeMutation = resourceListChanges;
const recordedUnknown = z
.object({
@@ -250,15 +260,18 @@ describe("target-free MCP lifecycle", () => {
).result;
expect(recordedUnknown.revision).toBe(1);
await new Promise<void>((resolve) => setImmediate(resolve));
expect(resourceListChanges).toBe(changesBeforeMutation + 1);
expect(resourceListChanges).toBe(changesBeforeMutation);
const listedUnknowns = await mcp.callTool({
name: "list_unknowns",
arguments: {},
});
expect(listedUnknowns.isError, text(listedUnknowns)).not.toBe(true);
expect(
z
.object({ result: z.array(z.unknown()) })
.parse(
structured(
await mcp.callTool({ name: "list_unknowns", arguments: {} }),
),
).result,
.object({
result: z.object({ items: z.array(z.unknown()) }),
})
.parse(structured(listedUnknowns)).result.items,
).toHaveLength(1);
const resolved = await mcp.callTool({
name: "update_unknown",
@@ -284,7 +297,7 @@ describe("target-free MCP lifecycle", () => {
});
expect(resolved.isError).not.toBe(true);
await new Promise<void>((resolve) => setImmediate(resolve));
expect(resourceListChanges).toBe(changesBeforeMutation + 2);
expect(resourceListChanges).toBe(changesBeforeMutation);
expect(
structured(
await mcp.callTool({
@@ -324,7 +337,7 @@ describe("target-free MCP lifecycle", () => {
expect(
structured(await mcp.callTool({ name: "list_unknowns", arguments: {} }))
.result,
).toEqual([]);
).toMatchObject({ items: [] });
expect(
(
await mcp.callTool({
@@ -368,18 +381,30 @@ describe("target-free MCP lifecycle", () => {
},
});
expect(captured.isError, text(captured)).not.toBe(true);
const listedUnknowns = await mcp.callTool({
name: "list_unknowns",
arguments: {},
});
expect(listedUnknowns.isError, text(listedUnknowns)).not.toBe(true);
const listed = z
.object({
result: z.array(z.object({ question: z.string(), domain: z.string() })),
result: z.object({
items: z.array(
z.object({
unknown: z.object({
question: z.string(),
domain: z.string(),
}),
}),
),
}),
})
.parse(
structured(
await mcp.callTool({ name: "list_unknowns", arguments: {} }),
),
).result;
.parse(structured(listedUnknowns)).result.items;
expect(listed).toContainEqual({
question: "Was network behavior fully observed during capture?",
domain: "process-network",
unknown: expect.objectContaining({
question: "Was network behavior fully observed during capture?",
domain: "process-network",
}),
});
});
});
+3
View File
@@ -86,6 +86,9 @@ describe("tool contract inventory", () => {
"update_unknown",
"verify_unknown_resolution",
"run_replay_machine",
"snapshot_evidence_bundle",
"get_navigation_context",
"inspect_address_context",
]);
expect(new Set(TOOL_CONTRACTS.map(({ name }) => name)).size).toBe(
TOOL_CONTRACTS.length,