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.
166 lines
6.3 KiB
TypeScript
166 lines
6.3 KiB
TypeScript
import { AjvJsonSchemaValidator } from "@modelcontextprotocol/client/validators/ajv";
|
|
import { expect, it, vi } from "vitest";
|
|
import {
|
|
createMcpSchemaValidation,
|
|
SHARED_COMPILED_VALIDATORS,
|
|
} from "../../fixtures/mcpSchemaValidation.js";
|
|
|
|
it("compiles a listed tool's validator only when it first validates", () => {
|
|
// The SDK requests a validator for every listed tool before its first call.
|
|
const compile = vi.spyOn(AjvJsonSchemaValidator.prototype, "getValidator");
|
|
const validation = createMcpSchemaValidation();
|
|
try {
|
|
const listed = [
|
|
{ type: "string" },
|
|
{ type: "integer" },
|
|
{ type: "boolean" },
|
|
];
|
|
const validators = listed.map((schema) => validation.getValidator(schema));
|
|
expect(compile).not.toHaveBeenCalled();
|
|
expect(validators[1]?.(7).valid).toBe(true);
|
|
expect(validators[1]?.("seven").valid).toBe(false);
|
|
expect(compile).toHaveBeenCalledTimes(1);
|
|
} finally {
|
|
compile.mockRestore();
|
|
validation.clear();
|
|
}
|
|
});
|
|
|
|
it("shares compiled validators across fixtures and evicts the least recently used", () => {
|
|
const compile = vi.spyOn(AjvJsonSchemaValidator.prototype, "getValidator");
|
|
const first = createMcpSchemaValidation();
|
|
const second = createMcpSchemaValidation();
|
|
// A per-test title keeps schemas compiled by other tests in this worker
|
|
// out of the shared cache counts.
|
|
const nonce = crypto.randomUUID();
|
|
const schema = (index: number) => ({
|
|
title: `${nonce}-${index}`,
|
|
type: "integer",
|
|
});
|
|
try {
|
|
expect(first.getValidator(schema(0))(1).valid).toBe(true);
|
|
expect(compile).toHaveBeenCalledTimes(1);
|
|
// Another fixture, and the same fixture after clear(), reuse the result.
|
|
expect(second.getValidator(schema(0))("one").valid).toBe(false);
|
|
first.clear();
|
|
expect(first.getValidator(schema(0))(2).valid).toBe(true);
|
|
expect(compile).toHaveBeenCalledTimes(1);
|
|
|
|
// Fill the remaining capacity, touch the oldest entry, then overflow:
|
|
// the least recently used entry (schema 1) goes, schema 0 stays.
|
|
for (let index = 1; index < SHARED_COMPILED_VALIDATORS; index += 1) {
|
|
expect(first.getValidator(schema(index))(index).valid).toBe(true);
|
|
}
|
|
expect(compile).toHaveBeenCalledTimes(SHARED_COMPILED_VALIDATORS);
|
|
// A fixture's own validator keeps its compiled function, so a new fixture
|
|
// is what reaches the shared cache again.
|
|
const touch = createMcpSchemaValidation();
|
|
expect(touch.getValidator(schema(0))(3).valid).toBe(true);
|
|
touch.clear();
|
|
expect(
|
|
second.getValidator(schema(SHARED_COMPILED_VALIDATORS))(4).valid,
|
|
).toBe(true);
|
|
expect(compile).toHaveBeenCalledTimes(SHARED_COMPILED_VALIDATORS + 1);
|
|
const fresh = createMcpSchemaValidation();
|
|
expect(fresh.getValidator(schema(0))(5).valid).toBe(true);
|
|
expect(compile).toHaveBeenCalledTimes(SHARED_COMPILED_VALIDATORS + 1);
|
|
expect(fresh.getValidator(schema(1))(6).valid).toBe(true);
|
|
expect(compile).toHaveBeenCalledTimes(SHARED_COMPILED_VALIDATORS + 2);
|
|
fresh.clear();
|
|
} finally {
|
|
compile.mockRestore();
|
|
first.clear();
|
|
second.clear();
|
|
}
|
|
});
|
|
|
|
it("reuses equal schemas and recompiles changed schemas sharing an id", () => {
|
|
const validation = createMcpSchemaValidation();
|
|
try {
|
|
const schema = {
|
|
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
$id: "https://fixture.test/result",
|
|
$defs: { value: { type: "string" as "string" | "integer" } },
|
|
$ref: "#/$defs/value",
|
|
};
|
|
const first = validation.getValidator(schema);
|
|
expect(validation.getValidator(structuredClone(schema))).toBe(first);
|
|
expect(first("recorded").valid).toBe(true);
|
|
expect(first(42).valid).toBe(false);
|
|
expect(validation.assertions.validate(schema, "recorded")).toBe(true);
|
|
expect(validation.assertions.validate(schema, 42)).toBe(false);
|
|
|
|
schema.$defs.value.type = "integer";
|
|
const changed = validation.getValidator(schema);
|
|
expect(changed).not.toBe(first);
|
|
expect(changed(42).valid).toBe(true);
|
|
expect(changed("recorded").valid).toBe(false);
|
|
expect(validation.assertions.validate(schema, 42)).toBe(true);
|
|
expect(validation.assertions.validate(schema, "recorded")).toBe(false);
|
|
|
|
validation.clear();
|
|
expect(validation.getValidator(schema)).not.toBe(changed);
|
|
} finally {
|
|
validation.clear();
|
|
}
|
|
});
|
|
|
|
it.each([
|
|
"https://json-schema.org/draft/2020-12/schema",
|
|
"https://json-schema.org/draft/2019-09/schema",
|
|
"http://json-schema.org/draft-07/schema#",
|
|
])("preserves SDK format validation and references for %s", (dialect) => {
|
|
const validation = createMcpSchemaValidation();
|
|
try {
|
|
const schema = {
|
|
$schema: dialect,
|
|
$defs: { address: { type: "string", format: "email" } },
|
|
$ref: "#/$defs/address",
|
|
} as const;
|
|
const validator = validation.getValidator(schema);
|
|
expect(validator("analyst@example.test").valid).toBe(true);
|
|
expect(validator("invalid email").valid).toBe(false);
|
|
// Explicit schema assertions retain their separate format-off policy.
|
|
expect(
|
|
validation.assertions.validate(
|
|
{ type: "string", format: "email" },
|
|
"invalid email",
|
|
),
|
|
).toBe(true);
|
|
expect(
|
|
validation.assertions.validateSchema({ type: "string", minLength: -1 }),
|
|
).toBe(false);
|
|
} finally {
|
|
validation.clear();
|
|
}
|
|
});
|
|
|
|
it("owns nested schema values independently of later caller mutation", () => {
|
|
const validation = createMcpSchemaValidation();
|
|
try {
|
|
const schema = { enum: [{ state: "recorded" }, { state: "other" }] };
|
|
const originalSchema = structuredClone(schema);
|
|
const original = validation.getValidator(schema);
|
|
expect(validation.assertions.validate(schema, { state: "recorded" })).toBe(
|
|
true,
|
|
);
|
|
schema.enum[0]!.state = "changed";
|
|
const changed = validation.getValidator(schema);
|
|
expect(original({ state: "recorded" }).valid).toBe(true);
|
|
expect(original({ state: "changed" }).valid).toBe(false);
|
|
expect(changed({ state: "changed" }).valid).toBe(true);
|
|
expect(changed({ state: "recorded" }).valid).toBe(false);
|
|
expect(
|
|
validation.assertions.validate(originalSchema, { state: "recorded" }),
|
|
).toBe(true);
|
|
expect(validation.assertions.validate(schema, { state: "recorded" })).toBe(
|
|
false,
|
|
);
|
|
expect(validation.assertions.validate(schema, { state: "changed" })).toBe(
|
|
true,
|
|
);
|
|
} finally {
|
|
validation.clear();
|
|
}
|
|
});
|