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.
154 lines
5.1 KiB
TypeScript
154 lines
5.1 KiB
TypeScript
import { mkdir, rm } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import type { CallToolResult } from "@modelcontextprotocol/server";
|
|
import { expect, it, vi } from "vitest";
|
|
import { WebSourceLocationService } from "../../src/application/WebSourceLocationService.js";
|
|
import { ok } from "../../src/domain/result.js";
|
|
import { systemProcessOwnershipHost } from "../../src/process/ProcessOwnershipObservation.js";
|
|
import { createServer } from "../../src/server/createServer.js";
|
|
import * as nativeLoader from "../../src/windows/WindowsNativeLoader.js";
|
|
import { createTestBinarySession } from "../fixtures/binarySession.js";
|
|
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
import {
|
|
webSourceLocationArgs,
|
|
webSourceLocationFixture,
|
|
} from "../fixtures/webSourceLocation.js";
|
|
import { createMcpSchemaValidation } from "../fixtures/mcpSchemaValidation.js";
|
|
|
|
// The owned launcher selects the Windows native path from HOST_PLATFORM.
|
|
vi.mock("../../src/platform/HostPlatform.js", () => ({
|
|
HOST_PLATFORM: Object.freeze({ platform: "win32", architecture: "x64" }),
|
|
}));
|
|
|
|
// Bun on Windows is the runtime whose codec launch requests a Job Object limit.
|
|
vi.mock(
|
|
"../../src/process/JavaScriptWorkerMemoryLimit.js",
|
|
async (original) => {
|
|
const actual =
|
|
await original<
|
|
typeof import("../../src/process/JavaScriptWorkerMemoryLimit.js")
|
|
>();
|
|
return {
|
|
...actual,
|
|
javaScriptWorkerMemoryLimitBytes: (heapBudgetMiB: number) =>
|
|
actual.javaScriptWorkerMemoryLimitBytes(heapBudgetMiB, {
|
|
platform: "win32",
|
|
versions: { node: "24.18.0", bun: "1.4.2" },
|
|
}),
|
|
};
|
|
},
|
|
);
|
|
|
|
/** An admitted artifact that predates the optional Job Object limit control. */
|
|
const earlierArtifact = (
|
|
spawns: string[],
|
|
): nativeLoader.WindowsNativeAuthority => ({
|
|
inspection: {
|
|
abiVersion: 1,
|
|
nodeApiVersion: 8,
|
|
architecture: "x64",
|
|
filesystem: {
|
|
requestedPath: "C:\\probe",
|
|
finalPath: "C:\\probe",
|
|
filesystem: "NTFS",
|
|
volumeSerial: "0".repeat(16),
|
|
fileId: "0".repeat(32),
|
|
size: 0,
|
|
directory: true,
|
|
},
|
|
privateDacl: true,
|
|
privateBridgeDescriptorAccess: true,
|
|
atomicJobAssignment: true,
|
|
killOnOwnerClose: true,
|
|
protocolStdin: true,
|
|
},
|
|
call: (operation) => {
|
|
spawns.push(operation);
|
|
throw new Error(`Unexpected native ${operation}`);
|
|
},
|
|
});
|
|
|
|
it("reports a missing Windows native control as an unavailable capability", async () => {
|
|
const spawns: string[] = [];
|
|
const parent = await createTestTempDirectory("rea-native-control-");
|
|
const root = join(parent, "runtime");
|
|
await mkdir(root);
|
|
let rootClosed = false;
|
|
const session = createTestBinarySession(() => {
|
|
throw new Error("binary provider must not start");
|
|
});
|
|
let client: Client | undefined;
|
|
let server: ReturnType<typeof createServer> | undefined;
|
|
try {
|
|
vi.spyOn(nativeLoader, "requireWindowsNativeAuthority").mockReturnValue(
|
|
earlierArtifact(spawns),
|
|
);
|
|
vi.spyOn(systemProcessOwnershipHost, "prepare").mockResolvedValue(
|
|
undefined,
|
|
);
|
|
const { SourceMapDecoder } =
|
|
await import("../../src/javascript/sourceMaps/SourceMapDecoder.js");
|
|
const decoder = new SourceMapDecoder({
|
|
prepareOwnership: async () => {},
|
|
createRuntime: async () => ({
|
|
path: root,
|
|
close: async () => {
|
|
await rm(root, { recursive: true, force: true });
|
|
rootClosed = true;
|
|
},
|
|
}),
|
|
});
|
|
server = createServer(
|
|
{ kind: "session", session },
|
|
{
|
|
webSourceLocation: new WebSourceLocationService(
|
|
{ load: async () => ok(webSourceLocationFixture()) },
|
|
decoder,
|
|
),
|
|
},
|
|
);
|
|
client = new Client(
|
|
{ name: "native-control-capability", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
|
|
const response = await client.callTool({
|
|
name: "trace_web_source_location",
|
|
arguments: webSourceLocationArgs,
|
|
});
|
|
|
|
const { error } = parseMcpToolError(response as CallToolResult);
|
|
expect(error).toMatchObject({
|
|
code: "capability_unavailable",
|
|
retryable: false,
|
|
message: expect.stringContaining(
|
|
"does not support Job Object memory limits",
|
|
),
|
|
remediation: {
|
|
action: expect.stringContaining(
|
|
"matching current Windows x64 REA package",
|
|
),
|
|
},
|
|
details: {
|
|
provider_id: "source-map-decoder",
|
|
operation: "trace_web_source_location",
|
|
},
|
|
});
|
|
// The refusal still precedes any native launch and releases the runtime.
|
|
expect(spawns).toEqual([]);
|
|
expect(rootClosed).toBe(true);
|
|
await decoder.close();
|
|
} finally {
|
|
vi.restoreAllMocks();
|
|
await client?.close();
|
|
await server?.close();
|
|
await session.close();
|
|
}
|
|
});
|