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.
140 lines
4.9 KiB
TypeScript
140 lines
4.9 KiB
TypeScript
import { Client } from "@modelcontextprotocol/client";
|
|
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
|
import { chmod, mkdir, rm, symlink, writeFile } from "node:fs/promises";
|
|
import { join, resolve } from "node:path";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { parseEvidence } from "../../../src/domain/evidence.js";
|
|
import { nativeDispatchMetadataResultSchema } from "../../../src/domain/native/objcSwiftMetadata.js";
|
|
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
|
|
import { localToolsMcpEnvironment } from "../../fixtures/localToolsMcp.js";
|
|
import { objcRecordBudgetMacho } from "../../fixtures/objcRecordBudgetMacho.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
|
|
|
|
type Replacement = "missing" | "directory" | "loop" | "unreadable";
|
|
|
|
// Real native-macos dispatch acquisition, through the compiled stdio server.
|
|
// The self-authored Mach-O fixture is only read, never executed.
|
|
const inspectReplacements = async (replacements: readonly Replacement[]) => {
|
|
const root = await createTestTempDirectory("rea-dispatch-bound-target-");
|
|
const path = join(root, "fixture.macho");
|
|
const bytes = objcRecordBudgetMacho("class");
|
|
await writeFile(path, bytes);
|
|
const env: Record<string, string> = {
|
|
PATH: process.env.PATH ?? "",
|
|
REA_LOG_LEVEL: "silent",
|
|
};
|
|
for (const key of [
|
|
"HOME",
|
|
"XDG_CONFIG_HOME",
|
|
"XDG_DATA_HOME",
|
|
"XDG_CACHE_HOME",
|
|
"XDG_STATE_HOME",
|
|
"TMPDIR",
|
|
]) {
|
|
const directory = join(root, key.toLowerCase());
|
|
env[key] = directory;
|
|
await mkdir(directory);
|
|
}
|
|
const client = new Client(
|
|
{ name: "dispatch-bound-target", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
const transport = new StdioClientTransport({
|
|
command: process.execPath,
|
|
args: [resolve("scripts/rea.mjs"), "mcp"],
|
|
cwd: process.cwd(),
|
|
env: localToolsMcpEnvironment(env),
|
|
stderr: "pipe",
|
|
});
|
|
let stderr = "";
|
|
transport.stderr?.on("data", (value) => {
|
|
stderr += value;
|
|
});
|
|
const inspect = () =>
|
|
client.callTool({
|
|
name: "inspect_native_dispatch_metadata",
|
|
arguments: { max_records: 100 },
|
|
});
|
|
const successfulRead = async () => {
|
|
const reply = await inspect();
|
|
expect(reply.isError).not.toBe(true);
|
|
return nativeDispatchMetadataResultSchema.parse(
|
|
parseEvidence(reply.structuredContent).normalized_result,
|
|
);
|
|
};
|
|
try {
|
|
await client.connect(transport);
|
|
const opened = await client.callTool({
|
|
name: "open_binary",
|
|
arguments: { path },
|
|
});
|
|
expect(opened.isError, JSON.stringify(opened)).not.toBe(true);
|
|
const control = await successfulRead();
|
|
for (const replacement of replacements) {
|
|
await rm(path, { recursive: true, force: true });
|
|
if (replacement === "directory") await mkdir(path);
|
|
if (replacement === "loop") await symlink("fixture.macho", path);
|
|
if (replacement === "unreadable") {
|
|
await writeFile(path, bytes);
|
|
await chmod(path, 0);
|
|
}
|
|
try {
|
|
const { error } = parseMcpToolError(await inspect());
|
|
const denied = replacement === "unreadable";
|
|
expect(error).toMatchObject({
|
|
code: denied ? "access_denied" : "artifact_changed",
|
|
category: denied ? "unavailable" : "integrity_mismatch",
|
|
details: {
|
|
operation: "inspect_native_dispatch_metadata",
|
|
path,
|
|
boundary: denied ? "filesystem-read" : "stable-artifact-read",
|
|
},
|
|
});
|
|
if (replacement === "unreadable")
|
|
expect(error.details?.system_code).toBe("EACCES");
|
|
else {
|
|
const code = {
|
|
missing: "ENOENT",
|
|
directory: "EISDIR",
|
|
loop: "ELOOP",
|
|
}[replacement];
|
|
expect(error.details?.reason).toContain(code);
|
|
}
|
|
expect(error.remediation.action).not.toContain("rea doctor");
|
|
} finally {
|
|
if (replacement === "unreadable") await chmod(path, 0o600);
|
|
}
|
|
await rm(path, { recursive: true, force: true });
|
|
await writeFile(path, bytes);
|
|
expect(await successfulRead()).toEqual(control);
|
|
}
|
|
const closed = await client.callTool({
|
|
name: "close_binary",
|
|
arguments: {},
|
|
});
|
|
expect(closed.isError).not.toBe(true);
|
|
expect(stderr).toBe("");
|
|
} finally {
|
|
await client.close();
|
|
await transport.close();
|
|
}
|
|
};
|
|
|
|
describe.skipIf(process.platform !== "darwin")(
|
|
"bound dispatch target read refusals",
|
|
() => {
|
|
it("reports missing, directory and symlink-loop replacements as artifact changes and recovers in the same session", async () => {
|
|
await inspectReplacements(["missing", "directory", "loop"]);
|
|
});
|
|
|
|
it.skipIf(process.getuid?.() === 0)(
|
|
"keeps permission denial distinct and recovers after restoring read access",
|
|
async () => {
|
|
await inspectReplacements(["unreadable"]);
|
|
},
|
|
);
|
|
},
|
|
);
|