Files
rea/tests/boundary/mcp/artifactMcp.test.ts
T
Kaoru0822andClaude Opus 5.5 5aafaa00ec fix(artifacts): carry recorded integrity contradictions into application workflows
A recorded contradiction appeared only in the analyze_javascript_application
response. Version and source comparisons, feature traces, and export-shape
comparisons read the application graph, which still claimed complete
coverage, so they could report complete-within-inputs results over
untrusted bytes. The graph is now partial when contradictions were
recorded, and its limitations, inherited by the result and the semantic
graph, name every contradicted path.

The mismatch remediation names both spellings: integrity_policy in MCP
and --integrity-policy on the CLI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 08:50:41 +00:00

560 lines
20 KiB
TypeScript

import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { createPackageWithOptions } from "@electron/asar";
import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js";
import { expect, it } from "vitest";
import { z } from "zod";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
import { ArtifactProvider } from "../../../src/artifacts/ArtifactProvider.js";
import { ARTIFACT_COMPARISON_EXAMPLE } from "../../../src/contracts/artifactComparisonExample.js";
import { createServer } from "../../../src/server/createServer.js";
import { observed } from "../../fixtures/analysisExecution.js";
it.each([
{ unpacked: false, label: "embedded" },
{ unpacked: true, label: "unpacked" },
])(
"returns actionable local details for an $label ASAR integrity error",
async ({ unpacked }) => {
const root = await createTestTempDirectory("rea-asar-integrity-mcp-");
const source = join(root, "source");
const original = "console.log('ok');\n";
const changed = "console.log('no');\n";
await mkdir(source);
await writeFile(join(source, "main.js"), original);
const archive = join(root, "fixture.asar");
await createPackageWithOptions(
source,
archive,
unpacked ? { unpack: "*.js" } : {},
);
if (unpacked) {
await writeFile(join(`${archive}.unpacked`, "main.js"), changed);
} else {
const bytes = await readFile(archive);
const contentOffset = bytes.indexOf(original);
expect(contentOffset).toBeGreaterThanOrEqual(0);
bytes.write(changed, contentOffset, "utf8");
await writeFile(archive, bytes);
}
const session = createTestBinarySession(new ArtifactProvider());
const server = createServer(session, session);
const client = new Client({ name: "asar-integrity-test", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const opened = await client.callTool({
name: "open_binary",
arguments: { path: archive },
});
expect(opened.isError).not.toBe(true);
const result = await client.callTool({
name: "inspect_artifact",
arguments: {},
});
expect(result.isError).toBe(true);
expect(result.structuredContent).toEqual({
error: {
code: "artifact_integrity_mismatch",
category: "integrity_mismatch",
message: INTEGRITY_REMEDIATION,
details: {
logical_path: "main.js",
declared_sha256: expect.stringMatching(/^[a-f0-9]{64}$/u),
calculated_sha256: expect.stringMatching(/^[a-f0-9]{64}$/u),
unpacked,
},
retryable: false,
remediation: {
action: INTEGRITY_REMEDIATION,
},
},
});
} finally {
await Promise.allSettled([
client.close(),
server.close(),
session.close(),
]);
}
},
20_000,
);
const INTEGRITY_REMEDIATION =
"Artifact bytes contradict their declared integrity. If that is expected, such as an unpacked native module signed after packaging, rerun with the record-and-continue integrity policy (integrity_policy in MCP, --integrity-policy on the CLI) to keep the mismatch as an untrusted contradiction; otherwise get a fresh copy.";
const artifactInventoryResultSchema = z.object({
occurrences: z.array(
z.object({ logical_path: z.string(), hash_status: z.string() }),
),
integrity_contradictions: z.array(
z.object({
logical_path: z.string(),
declared_sha256: z.string(),
observed_sha256: z.string(),
trust: z.literal("observed-untrusted"),
}),
),
});
it("extracts an active archive through MCP when requested", async () => {
const root = await createTestTempDirectory("rea-artifact-extract-mcp-");
const archive = join(root, "fixture.zip");
const zip = new ZipWriter(new Uint8ArrayWriter());
await zip.add("main.js", new TextReader("console.log('extract');\n"));
await writeFile(archive, await zip.close());
const session = createTestBinarySession(new ArtifactProvider());
const server = createServer(session, session);
const client = new Client({ name: "artifact-extract-test", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
let outputRoot: string | undefined;
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const opened = await client.callTool({
name: "open_binary",
arguments: { path: archive },
});
expect(opened.isError).not.toBe(true);
const result = await client.callTool({
name: "extract_artifact",
arguments: {},
});
expect(result.isError).not.toBe(true);
const normalized = z
.object({ output_root: z.string(), artifacts: z.array(z.unknown()) })
.parse(compactResult(result.structuredContent).result);
outputRoot = normalized.output_root;
expect(await readFile(join(outputRoot, "main.js"), "utf8")).toBe(
"console.log('extract');\n",
);
expect(normalized.artifacts).toHaveLength(1);
} finally {
await Promise.allSettled([client.close(), server.close(), session.close()]);
if (outputRoot !== undefined)
await rm(outputRoot, { recursive: true, force: true });
}
});
it("extracts an unpacked integrity mismatch only when the caller records it", async () => {
const root = await createTestTempDirectory("rea-asar-extract-continue-mcp-");
const source = join(root, "source");
await mkdir(source);
await writeFile(join(source, "main.js"), "console.log('ok');\n");
await writeFile(join(source, "kept.txt"), "kept\n");
const archive = join(root, "fixture.asar");
await createPackageWithOptions(source, archive, { unpack: "*.js" });
// Code signing after packaging changes unpacked bytes in the same way.
await writeFile(
join(`${archive}.unpacked`, "main.js"),
"console.log('no');\n",
);
const session = createTestBinarySession(new ArtifactProvider());
const server = createServer(session, session);
const client = new Client({ name: "asar-extract-continue", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
let outputRoot: string | undefined;
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
await client.callTool({
name: "open_binary",
arguments: { path: archive },
});
const strict = await client.callTool({
name: "extract_artifact",
arguments: {},
});
expect(strict.isError).toBe(true);
expect(strict.structuredContent).toMatchObject({
error: {
code: "artifact_integrity_mismatch",
message: INTEGRITY_REMEDIATION,
},
});
const result = await client.callTool({
name: "extract_artifact",
arguments: { integrity_policy: "record-and-continue" },
});
expect(result.isError, JSON.stringify(result.structuredContent)).not.toBe(
true,
);
const extraction = z
.object({
output_root: z.string(),
integrity_contradictions:
artifactInventoryResultSchema.shape.integrity_contradictions,
limitations: z.array(z.string()),
})
.parse(compactResult(result.structuredContent).result);
outputRoot = extraction.output_root;
expect(await readFile(join(outputRoot, "main.js"), "utf8")).toBe(
"console.log('no');\n",
);
expect(await readFile(join(outputRoot, "kept.txt"), "utf8")).toBe("kept\n");
expect(extraction.integrity_contradictions).toEqual([
expect.objectContaining({
logical_path: "main.js",
trust: "observed-untrusted",
}),
]);
expect(extraction.limitations).toContain(
"1 extracted file(s) contradict declared integrity; their observed bytes were written and are untrusted.",
);
} finally {
await Promise.allSettled([client.close(), server.close(), session.close()]);
if (outputRoot !== undefined)
await rm(outputRoot, { recursive: true, force: true });
}
});
it("records an explicitly continued mismatch, preserves verified siblings, and never reports equivalence", async () => {
const root = await createTestTempDirectory("rea-asar-continue-mcp-");
const source = join(root, "source");
await mkdir(source);
const original = "console.log('ok');\n",
changed = "console.log('no');\n",
secondOriginal = "console.log('up');\n",
secondChanged = "console.log('dn');\n";
await writeFile(join(source, "main.js"), original);
await writeFile(join(source, "second.js"), secondOriginal);
await writeFile(join(source, "sibling.js"), "console.log('sibling');\n");
const archive = join(root, "fixture.asar");
await createPackageWithOptions(source, archive, {});
const bytes = await readFile(archive);
const contentOffset = bytes.indexOf(original);
const secondOffset = bytes.indexOf(secondOriginal);
expect(contentOffset).toBeGreaterThanOrEqual(0);
expect(secondOffset).toBeGreaterThanOrEqual(0);
bytes.write(changed, contentOffset, "utf8");
bytes.write(secondChanged, secondOffset, "utf8");
await writeFile(archive, bytes);
const session = createTestBinarySession(new ArtifactProvider());
const server = createServer(session, session);
const client = new Client({ name: "asar-continue-test", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
await client.callTool({
name: "open_binary",
arguments: { path: archive },
});
const result = await client.callTool({
name: "inspect_artifact",
arguments: {
integrity_policy: "record-and-continue",
},
});
expect(result.isError, JSON.stringify(result.structuredContent)).not.toBe(
true,
);
const compact = compactResult(result.structuredContent);
const evidence = session.evidenceById(compact.evidence_id);
if (evidence === undefined) throw new Error("missing inventory Evidence");
const inspection = z
.object({
contradictions: z.array(z.unknown()),
substeps: z.array(
z.object({
evidence: z.object({ normalized_result: z.unknown() }),
}),
),
})
.parse(compact.result);
const inventory = artifactInventoryResultSchema.parse(
inspection.substeps[0]?.evidence.normalized_result,
);
expect(inspection.contradictions).toHaveLength(2);
expect(inventory.integrity_contradictions).toEqual(
expect.arrayContaining([
expect.objectContaining({
logical_path: "main.js",
trust: "observed-untrusted",
declared_sha256: expect.stringMatching(/^[a-f0-9]{64}$/u),
observed_sha256: expect.stringMatching(/^[a-f0-9]{64}$/u),
}),
expect.objectContaining({ logical_path: "second.js" }),
]),
);
expect(inventory.integrity_contradictions).toHaveLength(2);
expect(inventory.occurrences).toEqual(
expect.arrayContaining([
expect.objectContaining({
logical_path: "main.js",
hash_status: "mismatched",
}),
expect.objectContaining({
logical_path: "sibling.js",
hash_status: "verified",
}),
expect.objectContaining({
logical_path: "second.js",
hash_status: "mismatched",
}),
]),
);
const compared = await client.callTool({
name: "compare_artifacts",
arguments: {
left: evidence,
right: evidence,
},
});
expect(compared.isError).not.toBe(true);
expect(compactResult(compared.structuredContent).result).toMatchObject({
status: "contradiction",
summary: { contradiction: 2 },
changes: expect.arrayContaining([
expect.objectContaining({
logical_path: "main.js",
classification: "contradiction",
dimensions: ["integrity"],
}),
expect.objectContaining({
logical_path: "second.js",
classification: "contradiction",
}),
]),
});
} finally {
await Promise.allSettled([client.close(), server.close(), session.close()]);
}
});
it("rejects inline artifact Evidence whose content does not match its ID", async () => {
const session = createTestBinarySession(() => ({
health: () => Promise.resolve(),
execute: () => Promise.resolve(observed(null)),
close: () => Promise.resolve(),
}));
const server = createServer(session, session);
const client = new Client({
name: "artifact-authority-test",
version: "1",
});
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const result = await client.callTool({
name: "compare_artifacts",
arguments: {
...ARTIFACT_COMPARISON_EXAMPLE,
left: {
...ARTIFACT_COMPARISON_EXAMPLE.left,
limitations: ["caller altered this record"],
},
},
});
expect(result.isError).toBe(true);
expect(
session.evidenceById(ARTIFACT_COMPARISON_EXAMPLE.left.evidence_id),
).toBeUndefined();
expect(
session.evidenceById(ARTIFACT_COMPARISON_EXAMPLE.right.evidence_id),
).toBeUndefined();
} finally {
await Promise.allSettled([client.close(), server.close(), session.close()]);
}
});
it("compares inline inventory Evidence without prior session calls", async () => {
const session = createTestBinarySession(() => ({
health: () => Promise.resolve(),
execute: () => Promise.resolve(observed(null)),
close: () => Promise.resolve(),
}));
const server = createServer(session, session);
const client = new Client({
name: "artifact-ownership-test",
version: "1",
});
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const result = await client.callTool({
name: "compare_artifacts",
arguments: ARTIFACT_COMPARISON_EXAMPLE,
});
expect(result.isError, JSON.stringify(result.structuredContent)).not.toBe(
true,
);
expect(compactResult(result.structuredContent).result).toMatchObject({
status: "changed",
});
expect(
session.evidenceById(ARTIFACT_COMPARISON_EXAMPLE.left.evidence_id),
).toBeDefined();
expect(
session.evidenceById(ARTIFACT_COMPARISON_EXAMPLE.right.evidence_id),
).toBeDefined();
} finally {
await Promise.allSettled([client.close(), server.close(), session.close()]);
}
});
it("returns full artifact graphs inline and compares changed inventories", async () => {
const directory = await createTestTempDirectory("rea-artifact-mcp-");
const archive = join(directory, "fixture.ipa");
const changedArchive = join(directory, "changed.ipa");
const writer = new ZipWriter(new Uint8ArrayWriter());
await writer.add("Payload/Fixture.app/main.js", new TextReader("main();"));
await writeFile(archive, await writer.close());
const changedWriter = new ZipWriter(new Uint8ArrayWriter());
await changedWriter.add(
"Payload/Fixture.app/main.js",
new TextReader("changed();"),
);
await writeFile(changedArchive, await changedWriter.close());
const session = createTestBinarySession(new ArtifactProvider());
const server = createServer(session, session);
const client = new Client({ name: "artifact-mcp-test", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const opened = await client.callTool({
name: "open_binary",
arguments: { path: archive },
});
expect(opened.isError).not.toBe(true);
const inspected = await client.callTool({
name: "inspect_artifact",
arguments: {},
});
expect(inspected.isError).not.toBe(true);
const inspectionResult = compactResult(inspected.structuredContent);
const inspectionEvidence = session.evidenceById(
inspectionResult.evidence_id,
);
if (inspectionEvidence === undefined)
throw new Error("missing inspection Evidence");
const inspection = z
.object({
substeps: z.array(
z.object({
evidence_id: z.string(),
status: z.literal("completed"),
}),
),
coverage: z.object({
status: z.literal("complete-within-substeps"),
substeps_completed: z.literal(1),
}),
})
.parse(inspectionResult.result);
expect(inspection.substeps).toHaveLength(1);
expect(
session.evidenceById(inspection.substeps[0]?.evidence_id ?? ""),
).toMatchObject({ operation: "inventory_artifact" });
expect(inspectionEvidence.evidence_links).toEqual([
inspection.substeps[0]?.evidence_id,
]);
const inventory = await client.callTool({
name: "inspect_artifact",
arguments: {},
});
expect(inventory.isError).not.toBe(true);
const inventoryResult = compactResult(inventory.structuredContent);
const evidence = session.evidenceById(inventoryResult.evidence_id);
if (evidence === undefined) throw new Error("missing inventory Evidence");
expect(evidence).toMatchObject({
provider: { id: "rea-artifact-graph" },
subject: { format: "ipa" },
});
const inventoryInspection = z
.object({
substeps: z.array(z.object({ evidence: z.unknown() })),
})
.parse(inventoryResult.result);
const inventoryEvidence = z
.object({
normalized_result: z.object({
manifest: z.object({ root_format: z.literal("ipa") }),
occurrences: z.array(z.object({ logical_path: z.string() })),
}),
})
.parse(inventoryInspection.substeps[0]?.evidence);
expect(inventoryEvidence.normalized_result.occurrences).toEqual(
expect.arrayContaining([
expect.objectContaining({
logical_path: "Payload/Fixture.app/main.js",
}),
]),
);
const openedChanged = await client.callTool({
name: "open_binary",
arguments: { path: changedArchive },
});
expect(openedChanged.isError).not.toBe(true);
const changedInventory = await client.callTool({
name: "inspect_artifact",
arguments: {},
});
const changedResult = compactResult(changedInventory.structuredContent);
const changedInspection = z
.object({
substeps: z.array(z.object({ evidence: z.unknown() })),
})
.parse(changedResult.result);
const compared = await client.callTool({
name: "compare_artifacts",
arguments: {
left: inventoryInspection.substeps[0]?.evidence,
right: changedInspection.substeps[0]?.evidence,
},
});
expect(compared.isError).not.toBe(true);
expect(compactResult(compared.structuredContent)).toMatchObject({
result: { status: "changed" },
evidence_id: expect.stringMatching(/^ev_/u),
});
const unknowns = await client.callTool({
name: "list_unknowns",
arguments: { domain: "artifact-comparison" },
});
expect(unknowns.structuredContent).toMatchObject({
result: {
items: [
expect.objectContaining({
domain: "artifact-comparison",
}),
],
},
});
} finally {
await Promise.allSettled([client.close(), server.close(), session.close()]);
}
});
const compactResult = (value: unknown) =>
z
.object({
result: z.unknown(),
evidence_id: z.string(),
})
.parse(value);