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.
367 lines
13 KiB
TypeScript
367 lines
13 KiB
TypeScript
import { canonicalOutputSchema } from "../../fixtures/canonicalOutputSchema.js";
|
|
import { fileURLToPath } from "node:url";
|
|
import { access, chmod, mkdir, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
|
|
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
import { afterEach, expect, it } from "vitest";
|
|
import { z } from "zod";
|
|
|
|
import { toolContract } from "../../../src/contracts/toolContracts.js";
|
|
import {
|
|
createCacheProvider,
|
|
createTestBinarySession,
|
|
} from "../../fixtures/binarySession.js";
|
|
import { createServer } from "../../../src/server/createServer.js";
|
|
import { silentLogger } from "../../../src/logger.js";
|
|
import type { ProcessCapture } from "../../../src/domain/process/processCaptureParsing.js";
|
|
import { itWithCaptureCapability } from "./processCaptureCapability.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
|
|
|
|
const processFixture = fileURLToPath(
|
|
new URL("../../fixtures/processFidelity.mjs", import.meta.url),
|
|
);
|
|
const resources: Array<{ close(): Promise<unknown> }> = [];
|
|
|
|
const captureOutput = (value: unknown): ProcessCapture => {
|
|
const normalized = toolContract(
|
|
"capture_process_scenario",
|
|
).outputSchema.parse(value).normalized_result;
|
|
if ("status" in normalized && normalized.status === "planned")
|
|
throw new Error("expected process capture output");
|
|
return normalized as ProcessCapture;
|
|
};
|
|
|
|
afterEach(async () => {
|
|
await Promise.all(resources.splice(0).map((resource) => resource.close()));
|
|
});
|
|
|
|
it("rejects oversized initial and resize terminal buffers through MCP before launching", async () => {
|
|
const root = await createTestTempDirectory("rea-terminal-admission-");
|
|
const marker = join(root, "launched");
|
|
const session = createTestBinarySession(() => {
|
|
throw new Error("Terminal admission must not launch a binary provider");
|
|
});
|
|
const server = createServer(
|
|
{ kind: "session", session },
|
|
{ logger: silentLogger },
|
|
);
|
|
const client = new Client(
|
|
{ name: "terminal-admission", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
resources.push(client, server, session);
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
for (const input of [
|
|
{ terminal: { columns: 1_000, rows: 1, scrollback: 1_000 } },
|
|
{ events: [{ type: "resize", at_ms: 10, columns: 1_000, rows: 1 }] },
|
|
] as const) {
|
|
const result = await client.callTool({
|
|
name: "capture_process_scenario",
|
|
arguments: {
|
|
executable: process.execPath,
|
|
arguments: [
|
|
"-e",
|
|
"require('node:fs').writeFileSync(process.argv[1], 'launched')",
|
|
marker,
|
|
],
|
|
...input,
|
|
},
|
|
});
|
|
expect(result.isError).toBe(true);
|
|
expect(result.content).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "text",
|
|
text: expect.stringContaining("1000000"),
|
|
}),
|
|
);
|
|
await expect(access(marker)).rejects.toMatchObject({ code: "ENOENT" });
|
|
}
|
|
await client.ping();
|
|
});
|
|
|
|
itWithCaptureCapability(
|
|
"returns a planned scenario without launching the target",
|
|
async () => {
|
|
const root = await createTestTempDirectory("rea-process-plan-");
|
|
const marker = join(root, "launched");
|
|
const session = createTestBinarySession(() => {
|
|
throw new Error("A dry run must not launch a binary provider");
|
|
});
|
|
const server = createServer(
|
|
{ kind: "session", session },
|
|
{ logger: silentLogger },
|
|
);
|
|
const client = new Client(
|
|
{ name: "process-plan", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
resources.push(client, server, session);
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
|
|
const result = await client.callTool({
|
|
name: "capture_process_scenario",
|
|
arguments: {
|
|
executable: process.execPath,
|
|
working_directory: root,
|
|
arguments: [
|
|
"-e",
|
|
"require('node:fs').writeFileSync(process.argv[1], 'launched')",
|
|
marker,
|
|
],
|
|
dry_run: true,
|
|
},
|
|
});
|
|
|
|
expect(result.isError, JSON.stringify(result.content)).not.toBe(true);
|
|
await expect(access(marker)).rejects.toMatchObject({ code: "ENOENT" });
|
|
const parsed = toolContract("capture_process_scenario").outputSchema.parse(
|
|
result.structuredContent,
|
|
);
|
|
expect(parsed.predicate_type).toBe("rea.process-scenario-plan");
|
|
expect(parsed.normalized_result).toMatchObject({
|
|
status: "planned",
|
|
ambient_environment: "inherited-at-run",
|
|
scenario: { executable: process.execPath },
|
|
});
|
|
},
|
|
);
|
|
|
|
itWithCaptureCapability(
|
|
"bounds normalized PTY text and preserves equal-size output through MCP",
|
|
async () => {
|
|
const root = await createTestTempDirectory(
|
|
"rea-process-normalized-budget-",
|
|
);
|
|
const session = createTestBinarySession(createCacheProvider([]));
|
|
const server = createServer(
|
|
{ kind: "session", session },
|
|
{ logger: silentLogger },
|
|
);
|
|
const client = new Client(
|
|
{
|
|
name: "process-normalized-budget",
|
|
version: "1",
|
|
},
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
resources.push(client, server, session);
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
|
|
const capture = async (replacement: string) => {
|
|
const result = await client.callTool({
|
|
name: "capture_process_scenario",
|
|
arguments: {
|
|
executable: process.execPath,
|
|
working_directory: root,
|
|
arguments: ["-e", 'process.stdout.write("a".repeat(100) + "\\n")'],
|
|
limits: { output_bytes: 1_000 },
|
|
normalization: {
|
|
paths: false,
|
|
pids: false,
|
|
ports: false,
|
|
patterns: [{ pattern: "a", replacement }],
|
|
},
|
|
settle_ms: 50,
|
|
},
|
|
});
|
|
expect(result.isError, JSON.stringify(result.content)).not.toBe(true);
|
|
return captureOutput(result.structuredContent);
|
|
};
|
|
|
|
const expanded = await capture("x".repeat(500));
|
|
expect(expanded.truncated).toBe(true);
|
|
const retainedNormalized = expanded.frames.map(({ data }) => data).join("");
|
|
const retainedRaw = expanded.frames
|
|
.map(({ raw_data, data }) => raw_data ?? data)
|
|
.join("");
|
|
// PTY read boundaries need not match the child's write. A small chunk can
|
|
// fit even after another chunk was omitted for exceeding the budget.
|
|
expect(retainedNormalized).toBe(
|
|
retainedRaw.replaceAll("a", "x".repeat(500)),
|
|
);
|
|
expect(Buffer.byteLength(retainedNormalized)).toBeLessThanOrEqual(1_000);
|
|
expect(expanded.truncation_details.normalized_terminal).toMatchObject({
|
|
budget_bytes: 1_000,
|
|
retained_bytes: Buffer.byteLength(retainedNormalized),
|
|
omitted_frames: expect.any(Number),
|
|
});
|
|
expect(
|
|
expanded.truncation_details.normalized_terminal?.omitted_frames,
|
|
).toBeGreaterThan(0);
|
|
expect(expanded.truncation_details.raw_terminal).toMatchObject({
|
|
budget_bytes: 1_000,
|
|
observed_bytes: expect.any(Number),
|
|
retained_bytes: Buffer.byteLength(retainedRaw),
|
|
observed_frames: expect.any(Number),
|
|
retained_frames: expanded.frames.length,
|
|
});
|
|
expect(
|
|
expanded.limitations.some((limitation) =>
|
|
limitation.includes(
|
|
"final normalized sizes for the latter omissions are unknown",
|
|
),
|
|
),
|
|
).toBe(true);
|
|
expect(expanded.cleanup).toMatchObject({
|
|
owned_process_group: "verified",
|
|
temporary_root: "removed",
|
|
});
|
|
|
|
const equalSize = await capture("b");
|
|
// This short-lived target can exit before process sampling finishes.
|
|
// Assert the terminal coverage that this normalization control exercises.
|
|
expect(equalSize.truncation_details.raw_terminal).toMatchObject({
|
|
observed_bytes: Buffer.byteLength(`${"a".repeat(100)}\r\n`),
|
|
retained_bytes: Buffer.byteLength(`${"a".repeat(100)}\r\n`),
|
|
});
|
|
expect(equalSize.frames.map(({ data }) => data).join("")).toBe(
|
|
`${"b".repeat(100)}\r\n`,
|
|
);
|
|
expect(equalSize.truncation_details.normalized_terminal).toMatchObject({
|
|
budget_bytes: 1_000,
|
|
retained_bytes: Buffer.byteLength(`${"b".repeat(100)}\r\n`),
|
|
omitted_frames: 0,
|
|
});
|
|
expect(equalSize.cleanup).toMatchObject({
|
|
owned_process_group: "verified",
|
|
temporary_root: "removed",
|
|
});
|
|
},
|
|
30_000,
|
|
);
|
|
|
|
itWithCaptureCapability(
|
|
"records process residuals linked to capture Evidence",
|
|
async () => {
|
|
const session = createTestBinarySession(createCacheProvider([]));
|
|
const server = createServer(
|
|
{ kind: "session", session },
|
|
{
|
|
logger: silentLogger,
|
|
},
|
|
);
|
|
const mcp = new Client(
|
|
{ name: "process-unknown", version: "1.0.0" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
resources.push(mcp, server);
|
|
await server.connect(serverTransport);
|
|
await mcp.connect(clientTransport);
|
|
|
|
const captured = await mcp.callTool({
|
|
name: "capture_process_scenario",
|
|
arguments: {
|
|
executable: process.execPath,
|
|
arguments: [processFixture, "partial"],
|
|
},
|
|
});
|
|
expect(captured.isError, JSON.stringify(captured.content)).not.toBe(true);
|
|
const contract = toolContract("capture_process_scenario");
|
|
const wire = (await mcp.listTools()).tools.find(
|
|
({ name }) => name === contract.name,
|
|
);
|
|
if (wire === undefined)
|
|
throw new Error("Missing process capture output schema");
|
|
expect(
|
|
new Ajv2020({ strict: false, validateFormats: false }).validate(
|
|
canonicalOutputSchema(wire.name),
|
|
captured.structuredContent,
|
|
),
|
|
).toBe(true);
|
|
const evidence = contract.outputSchema.parse(captured.structuredContent);
|
|
const listedUnknowns = await mcp.callTool({
|
|
name: "list_unknowns",
|
|
arguments: {},
|
|
});
|
|
expect(
|
|
listedUnknowns.isError,
|
|
JSON.stringify(listedUnknowns.content),
|
|
).not.toBe(true);
|
|
const listed = z
|
|
.object({
|
|
result: z.object({
|
|
items: z.array(
|
|
z.object({
|
|
question: z.string(),
|
|
domain: z.string(),
|
|
}),
|
|
),
|
|
}),
|
|
})
|
|
.parse(listedUnknowns.structuredContent).result.items;
|
|
expect(listed).toContainEqual(
|
|
expect.objectContaining({
|
|
question: `Was network behavior fully observed during capture? (Evidence ${evidence.evidence_id})`,
|
|
domain: "process-network",
|
|
}),
|
|
);
|
|
},
|
|
);
|
|
|
|
itWithCaptureCapability(
|
|
"resolves and inherits the server's selected environment through MCP",
|
|
async () => {
|
|
const root = await createTestTempDirectory("rea-process-mcp-env-");
|
|
const bin = join(root, "bin");
|
|
const executable = join(bin, "rea-injected-process-probe");
|
|
await mkdir(bin);
|
|
await writeFile(
|
|
executable,
|
|
[
|
|
"#!/bin/sh",
|
|
'printf "%s|%s\\n" "$REA_CAPTURE_INJECTED_MARKER" "$REA_CAPTURE_SCENARIO_OVERRIDE"',
|
|
].join("\n"),
|
|
);
|
|
await chmod(executable, 0o755);
|
|
const session = createTestBinarySession(createCacheProvider([]));
|
|
const server = createServer(
|
|
{ kind: "session", session },
|
|
{
|
|
logger: silentLogger,
|
|
environment: {
|
|
PATH: bin,
|
|
REA_CAPTURE_INJECTED_MARKER: "injected-value",
|
|
REA_CAPTURE_SCENARIO_OVERRIDE: "host-value",
|
|
},
|
|
},
|
|
);
|
|
const client = new Client(
|
|
{ name: "process-mcp-env", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
resources.push(client, server, session);
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
|
|
const result = await client.callTool({
|
|
name: "capture_process_scenario",
|
|
arguments: {
|
|
executable: "rea-injected-process-probe",
|
|
working_directory: root,
|
|
environment: { REA_CAPTURE_SCENARIO_OVERRIDE: "scenario-value" },
|
|
},
|
|
});
|
|
expect(result.isError, JSON.stringify(result.content)).not.toBe(true);
|
|
const capture = captureOutput(result.structuredContent);
|
|
expect(capture.frames.map(({ data }) => data).join("")).toContain(
|
|
"injected-value|scenario-value",
|
|
);
|
|
},
|
|
);
|