mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +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.
199 lines
7.0 KiB
TypeScript
199 lines
7.0 KiB
TypeScript
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import { mkdir, symlink, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { expect, onTestFinished } from "vitest";
|
|
import { createWebModuleTraceService } from "../../src/composition/webModules.js";
|
|
import { createServer } from "../../src/server/createServer.js";
|
|
import { createTestBinarySession } from "../fixtures/binarySession.js";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
import { webModuleArtifactsFixture } from "../fixtures/webModuleTrace.js";
|
|
import { cliTest } from "../support/cli/cliFixture.js";
|
|
import { createMcpSchemaValidation } from "../fixtures/mcpSchemaValidation.js";
|
|
|
|
cliTest(
|
|
"accepts zero-based numeric source indices and returns the same engine-free CLI/MCP trace",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-module-cli-");
|
|
const artifacts = webModuleArtifactsFixture("import(name)");
|
|
const manifest = {
|
|
...artifacts.manifest,
|
|
output_directory: root,
|
|
analysis_input: { input_path: join(root, "files"), format: "directory" },
|
|
};
|
|
await mkdir(join(root, "files", "modules"), { recursive: true });
|
|
await writeFile(
|
|
join(root, "files", "modules", "main.js"),
|
|
artifacts.source,
|
|
);
|
|
let manifestPath = join(root, "manifest.json");
|
|
await writeFile(manifestPath, JSON.stringify(manifest));
|
|
if (process.platform !== "win32") {
|
|
await mkdir(join(root, "lexical"));
|
|
await mkdir(join(root, "nested"));
|
|
await symlink(join(root, "nested"), join(root, "lexical", "link"));
|
|
manifestPath = `${root}/lexical/link/../manifest.json`;
|
|
}
|
|
const environment = {
|
|
REA_LOG_LEVEL: "silent",
|
|
REA_BROWSER_EXECUTABLE: "relative-missing-browser",
|
|
};
|
|
const cliResponse = await cli.run({
|
|
arguments: [
|
|
"trace-web-module-imports",
|
|
process.platform === "win32"
|
|
? "manifest.json"
|
|
: "lexical/link/../manifest.json",
|
|
"0",
|
|
"--json",
|
|
],
|
|
cwd: root,
|
|
environment: {
|
|
...environment,
|
|
// This single CLI invocation cannot reuse a newly written cache.
|
|
NODE_DISABLE_COMPILE_CACHE: "1",
|
|
},
|
|
});
|
|
expect(cliResponse.exitCode, cliResponse.stdout + cliResponse.stderr).toBe(
|
|
0,
|
|
);
|
|
expect(cliResponse.json).toMatchObject({
|
|
normalized_result: {
|
|
engine: null,
|
|
manifest: { path: manifestPath, reported_output_directory: root },
|
|
source: {
|
|
script_index: 0,
|
|
path: join(root, "files", "modules", "main.js"),
|
|
},
|
|
imports: [
|
|
{ resolution: { state: "unknown", reason: "computed-specifier" } },
|
|
],
|
|
},
|
|
});
|
|
const session = createTestBinarySession(() => {
|
|
throw new Error("no binary acquisition");
|
|
});
|
|
const server = createServer(
|
|
{ kind: "session", session },
|
|
{
|
|
webModuleTrace: createWebModuleTraceService(environment),
|
|
},
|
|
);
|
|
const client = new Client(
|
|
{ name: "module-cli-parity", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
onTestFinished(async () => {
|
|
await client.close();
|
|
await server.close();
|
|
await session.close();
|
|
});
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
const response = await client.callTool({
|
|
name: "trace_web_module_imports",
|
|
arguments: { manifest_path: manifestPath, script_index: 0 },
|
|
});
|
|
expect(response.isError).not.toBe(true);
|
|
const expected = cliResponse.json;
|
|
expect(response.structuredContent).toEqual(expected);
|
|
// The CLI renders native separators; MCP keeps the caller's spelling and
|
|
// still selects the same manifest root from a forward-slash Windows path.
|
|
const forwardSlash = manifestPath.replaceAll("\\", "/");
|
|
const forward = await client.callTool({
|
|
name: "trace_web_module_imports",
|
|
arguments: { manifest_path: forwardSlash, script_index: 0 },
|
|
});
|
|
expect(forward.isError, JSON.stringify(forward)).not.toBe(true);
|
|
const { manifest: _manifest, ...cliRest } = (
|
|
expected as { normalized_result: Record<string, unknown> }
|
|
).normalized_result;
|
|
expect(forward.structuredContent).toMatchObject({
|
|
normalized_result: {
|
|
...cliRest,
|
|
manifest: { path: forwardSlash, reported_output_directory: root },
|
|
},
|
|
});
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"returns a failing outcome when the selected source digest disagrees",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-module-integrity-");
|
|
const artifacts = webModuleArtifactsFixture("export const a = 1;");
|
|
const files = join(root, "files");
|
|
await mkdir(join(files, "modules"), { recursive: true });
|
|
await writeFile(join(files, "modules", "main.js"), "export const a = 2;");
|
|
const manifest = join(root, "manifest.json");
|
|
await writeFile(
|
|
manifest,
|
|
JSON.stringify({
|
|
...artifacts.manifest,
|
|
output_directory: root,
|
|
analysis_input: { input_path: files, format: "directory" },
|
|
}),
|
|
);
|
|
for (const full of [false, true]) {
|
|
const result = await cli.run({
|
|
arguments: [
|
|
"trace-web-module-imports",
|
|
manifest,
|
|
"0",
|
|
"--json",
|
|
...(full ? ["--full-output"] : []),
|
|
],
|
|
});
|
|
expect(result.exitCode, result.stdout + result.stderr).toBe(1);
|
|
const failure = {
|
|
code: "artifact_integrity_mismatch",
|
|
details: {
|
|
partial_observation: {
|
|
kind: "web-artifact-acquisition",
|
|
analysis: "not-performed",
|
|
},
|
|
},
|
|
};
|
|
if (full)
|
|
expect(result.json).toMatchObject({
|
|
ok: false,
|
|
error: failure,
|
|
});
|
|
else expect(result.json).toMatchObject(failure);
|
|
}
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"retains an invalid caller-selected browser path in trace errors",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-module-browser-config-");
|
|
const artifacts = webModuleArtifactsFixture('import "./dep.js"');
|
|
const files = join(root, "files");
|
|
await mkdir(join(files, "modules"), { recursive: true });
|
|
await writeFile(join(files, "modules", "main.js"), artifacts.source);
|
|
const manifest = join(root, "manifest.json");
|
|
await writeFile(
|
|
manifest,
|
|
JSON.stringify({
|
|
...artifacts.manifest,
|
|
output_directory: root,
|
|
analysis_input: { input_path: files, format: "directory" },
|
|
}),
|
|
);
|
|
const result = await cli.run({
|
|
arguments: ["trace-web-module-imports", manifest, "0", "--json"],
|
|
environment: { REA_BROWSER_EXECUTABLE: "relative/chromium" },
|
|
});
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.json).toMatchObject({
|
|
code: "capability_unavailable",
|
|
details: {
|
|
reason: "invalid_browser_executable",
|
|
diagnostics: { executable: "relative/chromium" },
|
|
},
|
|
});
|
|
},
|
|
);
|