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.
137 lines
4.9 KiB
TypeScript
137 lines
4.9 KiB
TypeScript
import type { BuildComputerDefaults } from "../../src/desktop/BuildComputerEnvironment.js";
|
|
import type { ComputerStartTiming } from "../../src/desktop/AgentEnvComputer.js";
|
|
import { closeVerifierResources } from "../../scripts/lib/verifier-cleanup.mjs";
|
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
|
|
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import type { CallToolResult } from "@modelcontextprotocol/server";
|
|
import { expect, onTestFinished } from "vitest";
|
|
|
|
import type { BuildSettle } from "../../src/application/desktop/DesktopBuildSettle.js";
|
|
import { toolContract } from "../../src/contracts/toolContracts.js";
|
|
import type { SettleTiming } from "../../src/domain/desktop/appSettle.js";
|
|
import { createServer } from "../../src/server/createServer.js";
|
|
import { createTestBinarySession } from "./binarySession.js";
|
|
import {
|
|
fakeAgentEnvRunner,
|
|
isolatedBuildDefaults,
|
|
startFakeComputer,
|
|
type FakeAgentEnvRunner,
|
|
type FakeComputer,
|
|
type FakeComputerOptions,
|
|
} from "./fakeAgentEnvComputer.js";
|
|
import { createTestTempDirectory } from "./temporaryDirectory.js";
|
|
import { createMcpSchemaValidation } from "./mcpSchemaValidation.js";
|
|
|
|
/** The host's environment without the settings the opt-in real test exports, which would reach past the fakes. */
|
|
const hostEnvironment = Object.fromEntries(
|
|
Object.entries(process.env).filter(
|
|
([name]) =>
|
|
![
|
|
"REA_AGENT_ENV_COMMAND",
|
|
"REA_DOCKER_COMMAND",
|
|
"REA_BUILD_COMPUTER_REA_ARCHIVE",
|
|
].includes(name),
|
|
),
|
|
);
|
|
|
|
/** A check grades its first read, for tests whose app changes nothing while it is checked. */
|
|
const FIRST_READ: SettleTiming = {
|
|
intervalMs: 0,
|
|
windowMs: 0,
|
|
boundMs: 0,
|
|
};
|
|
|
|
/** An MCP client of a REA server whose build computers reach the fakes through `environment`, read apps with `settle` and start on `startTiming`. */
|
|
export const connectBuildServer = async (
|
|
environment: Readonly<Record<string, string>>,
|
|
settle: BuildSettle = FIRST_READ,
|
|
defaults?: BuildComputerDefaults,
|
|
startTiming?: ComputerStartTiming,
|
|
): Promise<Client> => {
|
|
const session = createTestBinarySession(() => {
|
|
throw new Error("build computers must not start a deep provider");
|
|
});
|
|
const server = createServer(
|
|
{ kind: "session", session },
|
|
{
|
|
environment: {
|
|
...hostEnvironment,
|
|
...environment,
|
|
},
|
|
desktopBuildSettle: settle,
|
|
desktopBuildDefaults: defaults ?? (await isolatedBuildDefaults()),
|
|
...(startTiming === undefined
|
|
? {}
|
|
: { desktopBuildStartTiming: startTiming }),
|
|
},
|
|
);
|
|
const client = new Client(
|
|
{ name: "build-computer-mcp", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
onTestFinished(async () => {
|
|
await closeVerifierResources([
|
|
() => client.close(),
|
|
() => server.close(),
|
|
async () => {
|
|
const closed = await session.close();
|
|
if (!closed.ok) throw closed.error;
|
|
},
|
|
]);
|
|
});
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
return client;
|
|
};
|
|
|
|
/** A fake computer and the fake runner that hands it out. */
|
|
export const buildComputerFakes = async (
|
|
options: FakeComputerOptions = {},
|
|
): Promise<{ computer: FakeComputer; runner: FakeAgentEnvRunner }> => {
|
|
const computer = await startFakeComputer(options);
|
|
onTestFinished(() => computer.close());
|
|
const runner = await fakeAgentEnvRunner(computer);
|
|
return { computer, runner };
|
|
};
|
|
|
|
/** An exported bundle's folder as far as the fake runner reads it; given a fake computer, the runner hands that one out for it. */
|
|
export const bundleFolder = async (
|
|
computer?: Pick<FakeComputer, "url">,
|
|
): Promise<string> => {
|
|
const bundle = await createTestTempDirectory("exported-bundle-");
|
|
await mkdir(join(bundle, "tasks"));
|
|
await writeFile(join(bundle, "tasks", "smoke.json"), "[]\n");
|
|
if (computer !== undefined)
|
|
await writeFile(join(bundle, "fake-computer-url"), computer.url);
|
|
return bundle;
|
|
};
|
|
|
|
/** Call a tool and fail the test with its result when it is an error. */
|
|
export const callOk = async (
|
|
client: Client,
|
|
name: string,
|
|
args: Record<string, unknown>,
|
|
): Promise<CallToolResult> => {
|
|
const result = await client.callTool({ name, arguments: args });
|
|
expect(result.isError, JSON.stringify(result)).not.toBe(true);
|
|
return result as CallToolResult;
|
|
};
|
|
|
|
/** Start a build computer and return its id. */
|
|
export const startBuildComputer = async (
|
|
client: Client,
|
|
source: Record<string, unknown>,
|
|
): Promise<string> =>
|
|
toolContract("start_build_computer").outputSchema.parse(
|
|
(await callOk(client, "start_build_computer", { source }))
|
|
.structuredContent,
|
|
).computer_id;
|
|
|
|
/** No delivered text, success or failure, may hold the control token. */
|
|
export const expectNoToken = (computer: FakeComputer, result: unknown) =>
|
|
expect(JSON.stringify(result)).not.toContain(computer.token);
|