mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 14:05:49 +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.
116 lines
4.1 KiB
TypeScript
116 lines
4.1 KiB
TypeScript
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import type { CallToolResult } from "@modelcontextprotocol/server";
|
|
import { expect, it, onTestFinished, vi } from "vitest";
|
|
import { z } from "zod";
|
|
|
|
import { parseEvidence } from "../../../src/domain/evidence.js";
|
|
import { silentLogger } from "../../../src/logger.js";
|
|
import { createServer } from "../../../src/server/createServer.js";
|
|
import { ToolResultDelivery } from "../../../src/server/ToolResultDelivery.js";
|
|
import { createJadxProtocolFixture } from "../../fixtures/android/jadx.js";
|
|
import {
|
|
createDeferred,
|
|
createTestBinarySession,
|
|
} from "../../fixtures/binarySession.js";
|
|
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
|
|
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
|
|
|
|
it("retains completed Android Evidence through the MCP cancellation delivery path", async () => {
|
|
const fixture = await createJadxProtocolFixture();
|
|
const controller = new AbortController();
|
|
let cancelOnCompletion = false;
|
|
const session = createTestBinarySession(() => {
|
|
throw new Error("Unexpected native admission");
|
|
});
|
|
const server = createServer(
|
|
{ kind: "session", session },
|
|
{
|
|
logger: silentLogger,
|
|
androidAnalysis: {
|
|
inspectAvailability: fixture.provider.inspectAvailability.bind(
|
|
fixture.provider,
|
|
),
|
|
close: fixture.provider.close.bind(fixture.provider),
|
|
execute: async (target, request, options) => {
|
|
const result = await fixture.provider.execute(
|
|
target,
|
|
request,
|
|
options,
|
|
);
|
|
if (result.ok && cancelOnCompletion) {
|
|
const signal = options?.signal;
|
|
if (signal === undefined)
|
|
throw new Error("MCP did not forward cancellation");
|
|
controller.abort();
|
|
if (!signal.aborted)
|
|
await new Promise<void>((resolve) =>
|
|
signal.addEventListener("abort", () => resolve(), {
|
|
once: true,
|
|
}),
|
|
);
|
|
}
|
|
return result;
|
|
},
|
|
},
|
|
},
|
|
);
|
|
const client = new Client(
|
|
{ name: "android-late-completion", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
onTestFinished(async () => {
|
|
controller.abort();
|
|
vi.restoreAllMocks();
|
|
await client.close();
|
|
await server.close();
|
|
await session.close();
|
|
});
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
const request = {
|
|
name: "inspect_android_package",
|
|
arguments: { path: fixture.apk },
|
|
};
|
|
const controlReply = await client.callTool(request);
|
|
expect(controlReply.isError).not.toBe(true);
|
|
const control = parseEvidence(controlReply.structuredContent);
|
|
const delivered = createDeferred<CallToolResult>();
|
|
const deliver = server.delivery.toPartialErrorToolResult.bind(
|
|
server.delivery,
|
|
);
|
|
vi.spyOn(
|
|
ToolResultDelivery.prototype,
|
|
"toPartialErrorToolResult",
|
|
).mockImplementation((...args) => {
|
|
const result = deliver(...args);
|
|
delivered.resolve(result);
|
|
return result;
|
|
});
|
|
cancelOnCompletion = true;
|
|
await expect(
|
|
client.callTool(request, { signal: controller.signal }),
|
|
).rejects.toThrow();
|
|
// The SDK suppresses a cancelled reply; observe the server's canonical
|
|
// recording/delivery boundary rather than treating client rejection as proof.
|
|
const { error } = parseMcpToolError(await delivered.promise);
|
|
expect(error.code).toBe("cancelled");
|
|
const reference = z
|
|
.object({
|
|
partial_observation: z.object({
|
|
kind: z.literal("retained-evidence"),
|
|
evidence_id: z.string(),
|
|
}),
|
|
})
|
|
.parse(error.details).partial_observation;
|
|
expect(session.evidenceById(reference.evidence_id)).toMatchObject({
|
|
operation: control.operation,
|
|
normalized_result: control.normalized_result,
|
|
subject: control.subject,
|
|
parameters: control.parameters,
|
|
confidence: control.confidence,
|
|
});
|
|
await expect(client.ping()).resolves.toEqual({});
|
|
});
|