Files
reverse-engineering-anything/tests/boundary/cli/bundleComparisonCli.test.ts
T
morluto 858ff73880 perf(tests): give MCP test clients the shared lazy schema validation (#10960)
* perf(tests): give MCP test clients the shared lazy schema validation

The MCP SDK client compiles every listed tool's output schema on its
first tool call. With the SDK's default Ajv options, REA's catalog costs
gigabytes of allocation per client (inspect_analysis_view alone: 1.7 s and
~500 MiB retained), so native MCP files peaked at 1-2 GiB per worker.

tests/fixtures/mcpSchemaValidation already owns lazy, reference-preserving
validation. Route the native MCP, process-boundary and shared harness
clients through it, share compiled validators across clients in a worker
(bounded LRU keyed by schema text), and add a counted architecture guard so
default-validation test clients cannot spread; the remaining copies are the
baseline.

* fix(guards): read call arguments past parentheses inside string literals

* perf(tests): give MCP boundary test clients the shared schema validation (#11022)

* perf(tests): give MCP boundary test clients the shared schema validation

* perf(tests): give the remaining MCP test clients the shared schema validation (#11025)

* fix(guards): read an MCP client's validator option from parsed code

The default-validation counter searched each new Client(...) call's text for jsonSchemaValidator, so an inline comment, a string literal or a client name naming the option hid a default client. Parse the source with @babel/parser and count NewExpressions of Client whose object-literal arguments have no jsonSchemaValidator property; a source that does not parse counts every textual call. The guard reads raw test sources, and the generated-child-script allowlist entry, which no longer counts as a call, is removed.

* fix(guards): read an MCP client's options only from a whole object-literal argument

The default-validation counter skipped files without the text
'new Client(', so a comment between the tokens hid a client, and it took
any depth-two key as an option, so 'enabled ? { jsonSchemaValidator } : {}'
passed while its false branch uses the SDK default. The skip now only
requires the name Client, and the validator counts only when it is a key
of an object literal that is a whole argument; anything else counts as the
default.
2026-11-15 21:53:53 -05:00

273 lines
8.9 KiB
TypeScript

import { displayedEvidenceSubject } from "../../fixtures/evidenceSubject.js";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { expect, onTestFinished } from "vitest";
import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js";
import { bundleComparisonResultSchema } from "../../../src/domain/bundleComparison.js";
import {
createEvidenceBundle,
serializeEvidenceBundle,
} from "../../../src/domain/evidenceBundle.js";
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
cliTest("rejects the removed compare-bundles command", async ({ cli }) => {
const result = await cli.run({
arguments: ["compare-bundles", "left.json", "right.json", "--json"],
});
expect(result.exitCode).toBe(1);
expect(result.stdout).toContain("COMMAND_NOT_FOUND");
});
cliTest(
"compares Evidence bundles without unexplained display-only changes",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-bundle-comparison-cli-");
const leftPath = join(root, "left.json");
const displayPath = join(root, "display.json");
const changedPath = join(root, "changed.json");
const target = {
path: join(root, "left.elf"),
sha256: "a".repeat(64),
format: "elf" as const,
architecture: "x86_64" as const,
};
const record = createEvidence(
target,
{ id: "fixture", name: "Fixture", version: "1" },
{
operation: "observe",
parameters: {},
result: { observed: true },
confidence: "observed",
authority: "shipped-artifact",
},
);
const left = createEvidenceBundle([record]);
const displayOnly = {
...left,
records: [
{
...record,
subject: displayedEvidenceSubject(record.subject, {
name: "renamed.elf",
local_path: join(root, "renamed.elf"),
}),
},
],
};
const different = createEvidenceBundle([
createEvidence(
target,
{ id: "fixture", name: "Fixture", version: "1" },
{
operation: "observe",
parameters: {},
result: { observed: false },
confidence: "observed",
authority: "shipped-artifact",
},
),
]);
await Promise.all([
writeFile(leftPath, serializeEvidenceBundle(left)),
writeFile(displayPath, serializeEvidenceBundle(displayOnly)),
writeFile(changedPath, serializeEvidenceBundle(different)),
]);
const compare = async (path: string) =>
cli.run({ arguments: ["compare", leftPath, path, "--json"] });
const same = await compare(leftPath);
expect(same.exitCode).toBe(0);
expect(bundleComparisonResultSchema.parse(same.json)).toMatchObject({
status: "unchanged",
changes: [],
});
const display = await compare(displayPath);
expect(display.exitCode).toBe(0);
const displayResult = bundleComparisonResultSchema.parse(display.json);
expect(displayResult).toMatchObject({
status: "unchanged",
summary: { records_unchanged: 1, unresolved: 0 },
changes: [],
});
expect(displayResult.left_bundle_sha256).not.toBe(
displayResult.right_bundle_sha256,
);
const differentResult = await compare(changedPath);
expect(differentResult.exitCode).toBe(0);
expect(
bundleComparisonResultSchema.parse(differentResult.json),
).toMatchObject({
status: "unknown",
summary: { unresolved: 2 },
changes: [
expect.objectContaining({
entity: "evidence",
conclusion_kind: "unresolved_branch",
}),
expect.objectContaining({
entity: "evidence",
conclusion_kind: "unresolved_branch",
}),
],
});
const pairs = [
{
left_evidence_id: record.evidence_id,
right_evidence_id: different.records[0]!.evidence_id,
},
];
const pairArgs = ["compare", leftPath, changedPath, "--record_pairs"];
const compareWithPairs = (value: string) =>
cli.run({ arguments: [...pairArgs, value, "--json"] });
const paired = await compareWithPairs(JSON.stringify(pairs));
expect(paired.exitCode).toBe(0);
const pairedResult = bundleComparisonResultSchema.parse(paired.json);
expect(pairedResult).toMatchObject({
status: "changed",
summary: { records_changed: 1, unresolved: 0 },
changes: [
{ classification: "changed", conclusion_kind: "observed_change" },
],
});
const pairFile = join(root, "pairs.json");
await writeFile(pairFile, JSON.stringify(pairs));
const fromFile = await compareWithPairs(pairFile);
expect(fromFile.exitCode).toBe(0);
expect(bundleComparisonResultSchema.parse(fromFile.json)).toEqual(
pairedResult,
);
const missingFile = join(root, "missing-pairs.json");
const absentFile = await compareWithPairs(missingFile);
expect(absentFile.exitCode).toBe(1);
expect(absentFile.json).toMatchObject({
code: "invalid_request",
input_path: missingFile,
input_reason: "read-failed",
});
const call = await cli.sharedMcpCall?.();
if (call === undefined) throw new Error("Expected shared MCP session");
const mcp = await call("compare_bundles", {
left_bundle_path: leftPath,
right_bundle_path: changedPath,
record_pairs: pairs,
});
expect(mcp.isError).not.toBe(true);
expect(
bundleComparisonResultSchema.parse(
parseEvidence(mcp.structuredContent).normalized_result,
),
).toEqual(pairedResult);
const absentPair = await compareWithPairs(
JSON.stringify([
{ ...pairs[0], left_evidence_id: `ev_${"f".repeat(64)}` },
]),
);
expect(absentPair.exitCode).toBe(1);
expect(absentPair.json).toMatchObject({
code: "invalid_request",
details: { issues: [{ path: ["record_pairs", 0, "left_evidence_id"] }] },
});
},
);
cliTest(
"rejects relative bundle paths over stdio MCP while CLI resolves them from its cwd",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-bundle-path-boundary-");
const leftPath = join(root, "left.json");
const rightPath = join(root, "right.json");
const encoded = serializeEvidenceBundle(createEvidenceBundle([]));
await Promise.all([
writeFile(leftPath, encoded),
writeFile(rightPath, encoded),
]);
const client = new Client(
{
name: "bundle-path-contract-test",
version: "1",
},
{ jsonSchemaValidator: createMcpSchemaValidation() },
);
const transport = new StdioClientTransport({
command: process.execPath,
args: [
fileURLToPath(new URL("../../../scripts/rea.mjs", import.meta.url)),
"mcp",
],
cwd: root,
env: {
PATH: process.env.PATH ?? "",
HOME: root,
REA_LOG_LEVEL: "silent",
},
stderr: "pipe",
});
transport.stderr?.on("data", () => undefined);
onTestFinished(async () => {
try {
await client.close();
} finally {
await transport.close();
}
});
await client.connect(transport);
for (const [left, right, rejectedField] of [
["left.json", "right.json", "left_bundle_path"],
["left.json", rightPath, "left_bundle_path"],
[leftPath, "right.json", "right_bundle_path"],
] as const) {
const result = await client.callTool({
name: "compare_bundles",
arguments: { left_bundle_path: left, right_bundle_path: right },
});
expect(parseMcpToolError(result).error).toMatchObject({
code: "invalid_request",
category: "invalid_input",
details: {
operation: "compare_bundles",
issues: expect.arrayContaining([
expect.objectContaining({
path: [rejectedField],
reason: "invalid_format",
expected: "regex",
}),
]),
},
});
}
const absolute = await client.callTool({
name: "compare_bundles",
arguments: {
left_bundle_path: leftPath,
right_bundle_path: rightPath,
},
});
expect(absolute.isError).not.toBe(true);
expect(absolute.structuredContent).toMatchObject({
normalized_result: { status: "unchanged" },
});
const relativeCli = await cli.run({
arguments: ["compare", "left.json", "right.json", "--json"],
cwd: root,
environment: { HOME: root, REA_LOG_LEVEL: "silent" },
});
expect(relativeCli.exitCode).toBe(0);
expect(bundleComparisonResultSchema.parse(relativeCli.json)).toMatchObject({
status: "unchanged",
changes: [],
});
},
);