mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 19:53:50 +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.
117 lines
3.9 KiB
TypeScript
117 lines
3.9 KiB
TypeScript
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import { expect, it, vi } from "vitest";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
|
|
const fault = vi.hoisted(() => ({
|
|
method: "",
|
|
path: "",
|
|
error: undefined as Error | undefined,
|
|
calls: 0,
|
|
}));
|
|
vi.mock("node:fs/promises", async (importOriginal) => {
|
|
const native = await importOriginal<typeof import("node:fs/promises")>();
|
|
const inject = (method: string, path: unknown): void => {
|
|
if (fault.method !== method || String(path) !== fault.path) return;
|
|
fault.calls += 1;
|
|
throw fault.error;
|
|
};
|
|
return {
|
|
...native,
|
|
lstat: (...args: Parameters<typeof native.lstat>) => {
|
|
inject("lstat", args[0]);
|
|
return native.lstat(...args);
|
|
},
|
|
realpath: (...args: Parameters<typeof native.realpath>) => {
|
|
inject("realpath", args[0]);
|
|
return native.realpath(...args);
|
|
},
|
|
access: (...args: Parameters<typeof native.access>) => {
|
|
inject("access", args[0]);
|
|
return native.access(...args);
|
|
},
|
|
};
|
|
});
|
|
import { createServer } from "../../src/server/createServer.js";
|
|
import { createTestBinarySession } from "../fixtures/binarySession.js";
|
|
import { createMcpSchemaValidation } from "../fixtures/mcpSchemaValidation.js";
|
|
|
|
it.each([
|
|
{ method: "lstat", code: "EACCES" },
|
|
{ method: "realpath", code: "EIO" },
|
|
{ method: "access", code: "EMFILE" },
|
|
])(
|
|
"retains selected input and host facts through MCP after $method/$code refusal and accepts a readable retry",
|
|
async ({ method, code }) => {
|
|
const root = await createTestTempDirectory("rea-mcp-js-filesystem-");
|
|
const session = createTestBinarySession(() => {
|
|
throw new Error(
|
|
"Static JavaScript analysis must not start a binary provider",
|
|
);
|
|
});
|
|
const server = createServer({ kind: "session", session });
|
|
const client = new Client(
|
|
{ name: "javascript-filesystem", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
try {
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
fault.method = method;
|
|
fault.path = root;
|
|
fault.calls = 0;
|
|
fault.error = Object.assign(
|
|
new Error(`${code}: selected ${method} failed at ${root}`),
|
|
{ code, path: root, syscall: method },
|
|
);
|
|
const response = await client.callTool({
|
|
name: "analyze_javascript_application",
|
|
arguments: { input_path: root, format: "directory" },
|
|
});
|
|
expect(fault.calls).toBe(1);
|
|
expect(response.isError).toBe(true);
|
|
const text = response.content.find((item) => item.type === "text");
|
|
if (text?.type !== "text")
|
|
throw new Error("Expected structured text error");
|
|
const result = JSON.parse(text.text);
|
|
if (code === "EACCES")
|
|
expect(result.error).toMatchObject({
|
|
code: "access_denied",
|
|
category: "unavailable",
|
|
details: {
|
|
path: root,
|
|
system_code: code,
|
|
boundary: "filesystem-read",
|
|
},
|
|
});
|
|
else
|
|
expect(result.error).toMatchObject({
|
|
code: "artifact_operation_failed",
|
|
details: {
|
|
reason: "io",
|
|
detail: fault.error.message,
|
|
diagnostics: {
|
|
input_path: root,
|
|
path: root,
|
|
system_code: code,
|
|
syscall: method,
|
|
},
|
|
},
|
|
});
|
|
fault.method = "";
|
|
const control = await client.callTool({
|
|
name: "analyze_javascript_application",
|
|
arguments: { input_path: root, format: "directory" },
|
|
});
|
|
expect(control.isError, JSON.stringify(control)).not.toBe(true);
|
|
} finally {
|
|
fault.method = "";
|
|
fault.error = undefined;
|
|
await client.close();
|
|
await server.close();
|
|
await session.close();
|
|
}
|
|
},
|
|
);
|