mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 23:15:58 +00:00
* 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.
375 lines
13 KiB
TypeScript
375 lines
13 KiB
TypeScript
import { unknownTarget } from "../../../src/domain/targetIdentity.js";
|
|
import {
|
|
canonicalOutputSchema,
|
|
withCanonicalOutputSchema,
|
|
} from "../../fixtures/canonicalOutputSchema.js";
|
|
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import { expect, it, onTestFinished } from "vitest";
|
|
import { z } from "zod";
|
|
import { createAnalysisExecution } from "../../../src/ports/AnalysisProvider.js";
|
|
import { toolContract } from "../../../src/contracts/toolContracts.js";
|
|
import { inspectAnalysisViewInputSchema } from "../../../src/domain/analysisView/analysisViewRequest.js";
|
|
import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js";
|
|
import { NATIVE_INVENTORY_OPERATIONS } from "../../../src/domain/native/nativeInventory.js";
|
|
import { ok } from "../../../src/domain/result.js";
|
|
import { createServer } from "../../../src/server/createServer.js";
|
|
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
|
|
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
|
import { connectGhidraMcp } from "./ghidraMcpHarness.js";
|
|
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
|
|
|
|
const procedureRows = [
|
|
{
|
|
address: "0x1000",
|
|
value: "first",
|
|
procedure: { external: false, thunk: false, thunk_target: null },
|
|
},
|
|
{
|
|
address: "0x2000",
|
|
value: "second",
|
|
procedure: { external: false, thunk: false, thunk_target: null },
|
|
},
|
|
];
|
|
// Symbols came from the source-owned PowerPC DOL capture; classification models the updated bridge output.
|
|
const searchProcedureRows = [
|
|
{
|
|
address: "0x80003100",
|
|
value: "FUN_80003100",
|
|
procedure: { external: false, thunk: false, thunk_target: null },
|
|
symbol: {
|
|
primary: true,
|
|
dynamic: false,
|
|
external: false,
|
|
type: "function",
|
|
source: "default",
|
|
},
|
|
},
|
|
{
|
|
address: "0x80003130",
|
|
value: "__start",
|
|
procedure: { external: false, thunk: false, thunk_target: null },
|
|
symbol: {
|
|
primary: true,
|
|
dynamic: false,
|
|
external: false,
|
|
type: "function",
|
|
source: "user_defined",
|
|
},
|
|
},
|
|
];
|
|
const stringRows = [
|
|
{
|
|
address: "0x3000",
|
|
value: "hello",
|
|
string: {
|
|
encoding: "utf-8",
|
|
termination: "present_or_not_required",
|
|
byte_length: 6,
|
|
},
|
|
},
|
|
];
|
|
const nameRows = [
|
|
{
|
|
address: "0x1000",
|
|
value: "first",
|
|
symbol: {
|
|
primary: true,
|
|
dynamic: false,
|
|
external: false,
|
|
type: "function",
|
|
source: "analysis",
|
|
},
|
|
},
|
|
];
|
|
|
|
it("shares complete/summary source identity and advertised contracts across all five inventory tools", async () => {
|
|
const harness = await connectGhidraMcp(
|
|
"native-inventory-views",
|
|
(operation) => {
|
|
if (operation === "search_procedures")
|
|
return Promise.resolve(ok(searchProcedureRows));
|
|
if (operation === "search_strings")
|
|
return Promise.resolve(ok(stringRows));
|
|
const result =
|
|
operation === "list_names"
|
|
? nameRows
|
|
: operation.endsWith("strings")
|
|
? stringRows
|
|
: procedureRows;
|
|
return Promise.resolve(ok(result));
|
|
},
|
|
);
|
|
onTestFinished(() => harness.close());
|
|
const tools = (await harness.mcp.listTools()).tools;
|
|
const ajv = new Ajv2020({ strict: false, validateFormats: false });
|
|
const viewTool = tools.find((tool) => tool.name === "inspect_analysis_view");
|
|
if (viewTool === undefined) throw new Error("Missing view schema");
|
|
const viewInputSchema: Record<string, unknown> = viewTool.inputSchema;
|
|
const viewOutputSchema: Record<string, unknown> = canonicalOutputSchema(
|
|
viewTool.name,
|
|
);
|
|
expect(ajv.validateSchema(viewInputSchema)).toBe(true);
|
|
expect(ajv.validateSchema(viewOutputSchema)).toBe(true);
|
|
for (const operation of NATIVE_INVENTORY_OPERATIONS) {
|
|
const contract = toolContract(operation);
|
|
const advertised = tools.find((tool) => tool.name === operation);
|
|
if (advertised === undefined) throw new Error("Missing operation schema");
|
|
const inputSchema: Record<string, unknown> = advertised.inputSchema;
|
|
const outputSchema: Record<string, unknown> = canonicalOutputSchema(
|
|
advertised.name,
|
|
);
|
|
expect(ajv.validateSchema(inputSchema)).toBe(true);
|
|
expect(ajv.validateSchema(outputSchema)).toBe(true);
|
|
const parameters = operation.startsWith("search_")
|
|
? { pattern: ".", mode: "regex" }
|
|
: {};
|
|
const completeReply = await harness.mcp.callTool(
|
|
{ name: operation, arguments: parameters },
|
|
{ toolDefinition: withCanonicalOutputSchema(advertised) },
|
|
);
|
|
if (completeReply.isError) throw new Error(JSON.stringify(completeReply));
|
|
const complete = parseEvidence(
|
|
contract.outputSchema.parse(completeReply.structuredContent),
|
|
);
|
|
if (!Array.isArray(complete.normalized_result))
|
|
throw new Error("Expected complete row array");
|
|
expect(complete.normalized_result.length).toBeGreaterThan(0);
|
|
if (operation === "search_procedures")
|
|
expect(complete.normalized_result).toEqual(searchProcedureRows);
|
|
if (operation === "search_strings")
|
|
expect(complete.normalized_result).toEqual(stringRows);
|
|
expect(complete.parameters).not.toHaveProperty("detail");
|
|
const summaryReply = await harness.mcp.callTool(
|
|
{ name: operation, arguments: { ...parameters, detail: "summary" } },
|
|
{ toolDefinition: withCanonicalOutputSchema(advertised) },
|
|
);
|
|
if (summaryReply.isError) throw new Error(JSON.stringify(summaryReply));
|
|
const summary = parseEvidence(
|
|
contract.outputSchema.parse(summaryReply.structuredContent),
|
|
);
|
|
expect(summary).toMatchObject({
|
|
operation: "inspect_analysis_view",
|
|
evidence_links: [complete.evidence_id],
|
|
normalized_result: {
|
|
parent_evidence_id: complete.evidence_id,
|
|
source_provider: complete.provider,
|
|
source_analysis_profile_digest:
|
|
complete.analysis_profile?.digest ?? null,
|
|
source_parameters: complete.parameters,
|
|
summary: {
|
|
row_count: (complete.normalized_result as unknown[]).length,
|
|
discovery_coverage: "unknown",
|
|
},
|
|
},
|
|
});
|
|
expect(summary.limitations).toEqual(
|
|
expect.arrayContaining(complete.limitations),
|
|
);
|
|
expect(harness.session.evidenceById(complete.evidence_id)).toEqual(
|
|
complete,
|
|
);
|
|
const allRows = [];
|
|
for (
|
|
let offset = 0;
|
|
offset < (complete.normalized_result as unknown[]).length;
|
|
offset++
|
|
) {
|
|
const pageReply = await harness.mcp.callTool(
|
|
{
|
|
name: "inspect_analysis_view",
|
|
arguments: {
|
|
source: {
|
|
kind: "retained-evidence",
|
|
evidence_id: complete.evidence_id,
|
|
},
|
|
view: { kind: "native-inventory", offset, limit: 1 },
|
|
},
|
|
},
|
|
{ toolDefinition: withCanonicalOutputSchema(viewTool) },
|
|
);
|
|
if (pageReply.isError) throw new Error(JSON.stringify(pageReply));
|
|
const page = toolContract("inspect_analysis_view").outputSchema.parse(
|
|
pageReply.structuredContent,
|
|
).normalized_result;
|
|
if (page.kind !== "native-inventory")
|
|
throw new Error("Expected inventory page");
|
|
expect(page.rows[0]?.source_index).toBe(offset);
|
|
allRows.push(...page.rows.map((row) => row.record));
|
|
}
|
|
expect(allRows).toEqual(complete.normalized_result);
|
|
}
|
|
expect(
|
|
harness.calls.filter((name) =>
|
|
NATIVE_INVENTORY_OPERATIONS.some((operation) => operation === name),
|
|
),
|
|
).toHaveLength(10);
|
|
});
|
|
|
|
it("recovers an oversized complete inventory through a working retained page without another provider read", async () => {
|
|
const items = Array.from({ length: 1000 }, (_, index) => ({
|
|
address: `0x${(0x1000 + index * 0x2000).toString(16)}`,
|
|
value: "x".repeat(7000),
|
|
string: {
|
|
encoding: "utf-8",
|
|
termination: "present_or_not_required",
|
|
byte_length: 7001,
|
|
},
|
|
}));
|
|
const harness = await connectGhidraMcp("native-inventory-capacity", () =>
|
|
Promise.resolve(ok(items)),
|
|
);
|
|
onTestFinished(() => harness.close());
|
|
const complete = await harness.mcp.callTool({
|
|
name: "list_strings",
|
|
arguments: {},
|
|
});
|
|
expect(complete.isError).toBe(true);
|
|
const error = parseMcpToolError(complete).error;
|
|
expect(error.code).toBe("resource_constraint");
|
|
const recovery = z
|
|
.object({
|
|
reported_limits: z.object({
|
|
boundary: z.literal("mcp-response"),
|
|
continuation: z.unknown(),
|
|
}),
|
|
})
|
|
.parse(error.details).reported_limits;
|
|
const continuation = z
|
|
.object({
|
|
name: z.literal("inspect_analysis_view"),
|
|
arguments: inspectAnalysisViewInputSchema,
|
|
})
|
|
.parse(recovery.continuation);
|
|
const pageReply = await harness.mcp.callTool(continuation);
|
|
if (pageReply.isError) throw new Error(JSON.stringify(pageReply));
|
|
const page = toolContract("inspect_analysis_view").outputSchema.parse(
|
|
pageReply.structuredContent,
|
|
).normalized_result;
|
|
if (page.kind !== "native-inventory")
|
|
throw new Error("Expected inventory page");
|
|
expect(page.rows.map((row) => row.record)).toEqual(items.slice(0, 64));
|
|
expect(page.coverage).toMatchObject({ total: 1000, next_offset: 64 });
|
|
expect(harness.calls).toEqual(["list_strings"]);
|
|
const summaryReply = await harness.mcp.callTool({
|
|
name: "list_strings",
|
|
arguments: { detail: "summary" },
|
|
});
|
|
if (summaryReply.isError) throw new Error(JSON.stringify(summaryReply));
|
|
expect(
|
|
parseEvidence(summaryReply.structuredContent).normalized_result,
|
|
).toMatchObject({ summary: { row_count: 1000 } });
|
|
});
|
|
|
|
it("refuses summary before provider work when no retaining session is configured", async () => {
|
|
let calls = 0;
|
|
const server = createServer({
|
|
kind: "fixed",
|
|
analysis: {
|
|
execute: async () => {
|
|
calls++;
|
|
return ok(
|
|
createAnalysisExecution(procedureRows, {
|
|
id: "fixture",
|
|
name: "Fixture",
|
|
version: "1",
|
|
}),
|
|
);
|
|
},
|
|
},
|
|
});
|
|
const client = new Client(
|
|
{
|
|
name: "unretained-native-summary",
|
|
version: "1",
|
|
},
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
onTestFinished(async () => {
|
|
await client.close();
|
|
await server.close();
|
|
});
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
for (const [name, parameters] of [
|
|
["list_procedures", {}],
|
|
["inspect_native_load_image", {}],
|
|
["procedure_address", { procedure: "fixture_main" }],
|
|
["procedure_callees", { procedure: "fixture_main" }],
|
|
["procedure_callers", { procedure: "fixture_main" }],
|
|
["resolve_containing_procedure", { address: "0x401001" }],
|
|
["get_call_graph", { address: "fixture_main" }],
|
|
] as const) {
|
|
const reply = await client.callTool({
|
|
name,
|
|
arguments: { ...parameters, detail: "summary" },
|
|
});
|
|
expect(reply.isError).toBe(true);
|
|
expect(parseMcpToolError(reply).error.code).toBe("capability_unavailable");
|
|
}
|
|
expect(calls).toBe(0);
|
|
});
|
|
|
|
it("projects imported inventories with an unknown artifact without inventing a path or opening a provider", async () => {
|
|
const session = createTestBinarySession(() => {
|
|
throw new Error("Retained views must not open a provider");
|
|
});
|
|
const source = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
{ id: "borrowed.native", name: "Borrowed engine", version: "1" },
|
|
{
|
|
operation: "list_procedures",
|
|
parameters: {},
|
|
result: procedureRows,
|
|
limitations: [
|
|
"The engine did not bind this observation to an artifact digest.",
|
|
],
|
|
},
|
|
);
|
|
const recorded = session.recordEvidence(source);
|
|
if (!recorded.ok) throw recorded.error;
|
|
const server = createServer({ kind: "session", session });
|
|
const client = new Client(
|
|
{ name: "unbound-native-views", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
onTestFinished(async () => {
|
|
await client.close();
|
|
await server.close();
|
|
const closed = await session.close();
|
|
if (!closed.ok) throw closed.error;
|
|
});
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
for (const view of [
|
|
{ kind: "summary" },
|
|
{ kind: "native-inventory", fields: [] },
|
|
]) {
|
|
const reply = await client.callTool({
|
|
name: "inspect_analysis_view",
|
|
arguments: {
|
|
source: { kind: "retained-evidence", evidence_id: source.evidence_id },
|
|
view,
|
|
},
|
|
});
|
|
if (reply.isError) throw new Error(JSON.stringify(reply));
|
|
const evidence = parseEvidence(reply.structuredContent);
|
|
expect(evidence).toMatchObject({
|
|
subject: {
|
|
identity: { kind: "unknown", reason: "derived-from-linked-evidence" },
|
|
},
|
|
locations: [],
|
|
evidence_links: [source.evidence_id],
|
|
normalized_result: {
|
|
artifact: null,
|
|
source_provider: source.provider,
|
|
source_parameters: {},
|
|
},
|
|
});
|
|
}
|
|
});
|