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.
192 lines
7.0 KiB
TypeScript
192 lines
7.0 KiB
TypeScript
import { withCanonicalOutputSchema } from "../../fixtures/canonicalOutputSchema.js";
|
|
import { traceSourceMap } from "../../../src/javascript/sourceMaps/TraceSourceMap.js";
|
|
import {
|
|
webSourceLocationArgs as args,
|
|
webSourceLocationFixture,
|
|
} from "../../fixtures/webSourceLocation.js";
|
|
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import { expect, it, onTestFinished, vi } from "vitest";
|
|
import { WebSourceLocationService } from "../../../src/application/WebSourceLocationService.js";
|
|
import { toolContract } from "../../../src/contracts/toolContracts.js";
|
|
import { parseEvidence } from "../../../src/domain/evidence.js";
|
|
import { ok } from "../../../src/domain/result.js";
|
|
import { createServer } from "../../../src/server/createServer.js";
|
|
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
|
import { SourceMapDecoder } from "../../../src/javascript/sourceMaps/SourceMapDecoder.js";
|
|
import { createSystemProcessOwnershipHost } from "../../../src/process/ProcessOwnershipObservation.js";
|
|
import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js";
|
|
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
|
|
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
|
|
|
|
it("publishes valid SDK schemas and records the named module trace without a binary target", async () => {
|
|
const service = new WebSourceLocationService(
|
|
{
|
|
load: () => Promise.resolve(ok(webSourceLocationFixture())),
|
|
},
|
|
{
|
|
trace: (input) =>
|
|
Promise.resolve(
|
|
ok(traceSourceMap(input.text, input.url, input.position)),
|
|
),
|
|
},
|
|
);
|
|
const session = createTestBinarySession(() => {
|
|
throw new Error("binary provider must not start");
|
|
});
|
|
const server = createServer(
|
|
{ kind: "session", session },
|
|
{ webSourceLocation: service },
|
|
);
|
|
const client = new Client(
|
|
{ name: "module-contract", 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 advertised = (await client.listTools()).tools.find(
|
|
(tool) => tool.name === "trace_web_source_location",
|
|
);
|
|
if (advertised === undefined)
|
|
throw new Error("module trace must advertise both schemas");
|
|
const ajv = new Ajv2020({ strict: false, validateFormats: false });
|
|
const inputSchema: Record<string, unknown> = advertised.inputSchema;
|
|
expect(ajv.validate(inputSchema, args)).toBe(true);
|
|
const response = await client.callTool(
|
|
{ name: "trace_web_source_location", arguments: args },
|
|
{ toolDefinition: withCanonicalOutputSchema(advertised) },
|
|
);
|
|
expect(response.isError).not.toBe(true);
|
|
const parsed = toolContract("trace_web_source_location").outputSchema.parse(
|
|
response.structuredContent,
|
|
);
|
|
const evidence = parseEvidence(parsed);
|
|
const data = webSourceLocationFixture();
|
|
expect(evidence).toMatchObject({
|
|
normalized_result: {
|
|
source: { sha256: data.sourceFile.sha256 },
|
|
source_map: {
|
|
association: "caller-selected",
|
|
sha256: data.sourceMap.file.sha256,
|
|
},
|
|
matches: [{ state: "mapped", content: { text: "original" } }],
|
|
execution: "unknown",
|
|
source_authenticity: "unknown",
|
|
},
|
|
raw_result: { source_map_text: data.sourceMap.text },
|
|
});
|
|
expect(session.evidenceById(evidence.evidence_id)).toEqual(evidence);
|
|
const invalid = await client.callTool({
|
|
name: "trace_web_source_location",
|
|
arguments: { ...args, manifest_path: "relative.json" },
|
|
});
|
|
expect(parseMcpToolError(invalid).error).toMatchObject({
|
|
code: "invalid_request",
|
|
details: {
|
|
issues: [{ path: ["manifest_path"], reason: "invalid_format" }],
|
|
},
|
|
});
|
|
});
|
|
|
|
it("preserves missing Linux ps capability and recovery through source-map tracing", async () => {
|
|
const directory = await createTestTempDirectory("source-map-missing-ps-");
|
|
// A Linux host with no ps at a fixed system location or on REA's PATH.
|
|
const prepare = createSystemProcessOwnershipHost(
|
|
"linux",
|
|
{ PATH: directory },
|
|
{ linuxPsLocations: [] },
|
|
).prepare;
|
|
if (prepare === undefined)
|
|
throw new Error("Ownership preparation is required");
|
|
const createRuntime = vi.fn(async () => {
|
|
throw new Error("Missing prerequisite must not acquire a runtime");
|
|
});
|
|
const launcher = vi.fn(async () => {
|
|
throw new Error("Missing prerequisite must not launch a codec");
|
|
});
|
|
const decoder = new SourceMapDecoder({
|
|
prepareOwnership: prepare,
|
|
createRuntime,
|
|
launcher,
|
|
});
|
|
const harness = await createApplicationMcpHarness({
|
|
serverOptions: {
|
|
webSourceLocation: new WebSourceLocationService(
|
|
{ load: async () => ok(webSourceLocationFixture()) },
|
|
decoder,
|
|
),
|
|
},
|
|
});
|
|
onTestFinished(() => harness.close());
|
|
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
const response = await harness.client.callTool({
|
|
name: "trace_web_source_location",
|
|
arguments: args,
|
|
});
|
|
const { error } = parseMcpToolError(response);
|
|
expect(error).toMatchObject({
|
|
code: "capability_unavailable",
|
|
category: "unsupported_provider",
|
|
retryable: false,
|
|
message: expect.stringContaining("procps-compatible ps"),
|
|
remediation: { action: expect.stringContaining("procps-compatible ps") },
|
|
details: {
|
|
provider_id: "process-ownership",
|
|
operation: "prepare_owned_process",
|
|
reason: expect.stringContaining("ENOENT"),
|
|
},
|
|
});
|
|
}
|
|
await harness.client.ping();
|
|
expect(createRuntime).not.toHaveBeenCalled();
|
|
expect(launcher).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects malformed delivery controls before acquiring source-map artifacts", async () => {
|
|
const load = vi.fn(async () => ok(webSourceLocationFixture()));
|
|
const harness = await createApplicationMcpHarness({
|
|
serverOptions: {
|
|
webSourceLocation: new WebSourceLocationService(
|
|
{ load },
|
|
{
|
|
trace: async (input) =>
|
|
ok(traceSourceMap(input.text, input.url, input.position)),
|
|
},
|
|
),
|
|
},
|
|
});
|
|
for (const delivery of [
|
|
{
|
|
detail: "complete",
|
|
view: { kind: "source-location", collection: "matches" },
|
|
},
|
|
{
|
|
detail: "summary",
|
|
view: {
|
|
kind: "source-location",
|
|
collection: "source-text",
|
|
match_index: -1,
|
|
},
|
|
},
|
|
{
|
|
detail: "summary",
|
|
view: { kind: "source-location", collection: "matches", limit: 0 },
|
|
},
|
|
]) {
|
|
const result = await harness.client.callTool({
|
|
name: "trace_web_source_location",
|
|
arguments: { ...args, ...delivery },
|
|
});
|
|
expect(parseMcpToolError(result).error.code).toBe("invalid_request");
|
|
}
|
|
expect(load).not.toHaveBeenCalled();
|
|
});
|