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.
187 lines
7.0 KiB
TypeScript
187 lines
7.0 KiB
TypeScript
import { execFile } from "node:child_process";
|
|
import { readFile, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { promisify } from "node:util";
|
|
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import { expect, onTestFinished } from "vitest";
|
|
import { z } from "zod";
|
|
import { parseEvidence } from "../../../src/domain/evidence.js";
|
|
import { parseProcessCapture } from "../../../src/domain/process/processCaptureParsing.js";
|
|
import { compareProcessCaptures } from "../../../src/domain/process/processComparison.js";
|
|
import { digestProcessCommitment } from "../../../src/domain/process/processScenario.js";
|
|
import { createServer } from "../../../src/server/createServer.js";
|
|
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import { itWithCaptureCapability } from "./processCaptureCapability.js";
|
|
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
|
|
|
|
const exec = promisify(execFile);
|
|
const fixture = fileURLToPath(
|
|
new URL("../../fixtures/processDescendantIdentifiers.mjs", import.meta.url),
|
|
);
|
|
const identitySchema = z.strictObject({
|
|
root_pid: z.int().positive(),
|
|
child_pid: z.int().positive(),
|
|
});
|
|
|
|
itWithCaptureCapability.each([
|
|
{ adapter: "cli", pids: true },
|
|
{ adapter: "mcp", pids: true },
|
|
{ adapter: "cli", pids: false },
|
|
{ adapter: "mcp", pids: false },
|
|
] as const)(
|
|
"normalizes sampled child output and preserves original bytes with pids=$pids through $adapter",
|
|
async ({ adapter, pids }) => {
|
|
const root = await createTestTempDirectory("rea-descendant-pid-adapter-");
|
|
const identityPath = join(root, "identity.json");
|
|
const scenarioPath = join(root, "scenario.json");
|
|
const scenario = {
|
|
executable: process.execPath,
|
|
arguments: [fixture, identityPath],
|
|
working_directory: root,
|
|
timeout_ms: 5_000,
|
|
idle_timeout_ms: 5_000,
|
|
settle_ms: 100,
|
|
normalization: {
|
|
pids,
|
|
ports: false,
|
|
paths: false,
|
|
time_bucket_ms: 10_000,
|
|
},
|
|
};
|
|
await writeFile(scenarioPath, JSON.stringify(scenario));
|
|
let client: Client | undefined;
|
|
if (adapter === "mcp") {
|
|
const session = createTestBinarySession(() => {
|
|
throw new Error("Process capture must not launch a binary provider");
|
|
});
|
|
const server = createServer({ kind: "session", session });
|
|
client = new Client(
|
|
{
|
|
name: "descendant-pid-normalization",
|
|
version: "1",
|
|
},
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
const connected = client;
|
|
onTestFinished(async () => {
|
|
await connected.close();
|
|
await server.close();
|
|
await session.close();
|
|
});
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
}
|
|
const captures = [];
|
|
for (let run = 0; run < (pids ? 2 : 1); run++) {
|
|
let evidence;
|
|
if (client !== undefined) {
|
|
const response = await client.callTool({
|
|
name: "capture_process_scenario",
|
|
arguments: scenario,
|
|
});
|
|
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
|
evidence = parseEvidence(response.structuredContent);
|
|
await client.ping();
|
|
} else {
|
|
const { stdout } = await exec(
|
|
process.execPath,
|
|
[
|
|
"scripts/rea.mjs",
|
|
"capture-process",
|
|
scenarioPath,
|
|
"--format",
|
|
"json",
|
|
],
|
|
{ timeout: 20_000 },
|
|
);
|
|
evidence = parseEvidence(JSON.parse(stdout));
|
|
}
|
|
const capture = parseProcessCapture(evidence.normalized_result);
|
|
const identity = identitySchema.parse(
|
|
JSON.parse(await readFile(identityPath, "utf8")),
|
|
);
|
|
expect(
|
|
evidence.subject,
|
|
"the subject names the observed root by PID and start identity, and withholds both when the caller normalized PIDs",
|
|
).toEqual({
|
|
identity: pids
|
|
? { kind: "unknown", reason: "withheld-by-caller" }
|
|
: {
|
|
kind: "process",
|
|
pid: identity.root_pid,
|
|
start_time: expect.stringMatching(/\S/u),
|
|
executable_sha256: capture.manifest.executable_sha256,
|
|
},
|
|
name: null,
|
|
local_path: null,
|
|
});
|
|
const original = `root pid ${identity.root_pid}\r\nchild pid ${identity.child_pid}\r\ncount 9007199254740991\r\n`;
|
|
const expected = pids
|
|
? "root pid <pid>\r\nchild pid <pid>\r\ncount 9007199254740991\r\n"
|
|
: original;
|
|
const childSamples = capture.process_samples.filter(({ command }) =>
|
|
command.endsWith("processDescendantIdentifiers.mjs child"),
|
|
);
|
|
expect(
|
|
childSamples.length,
|
|
"the child must have been positively sampled",
|
|
).toBeGreaterThan(0);
|
|
expect(
|
|
childSamples.some(({ pid, parent_pid }) =>
|
|
pids
|
|
? pid !== 1 && parent_pid === 1
|
|
: pid === identity.child_pid && parent_pid === identity.root_pid,
|
|
),
|
|
).toBe(true);
|
|
expect(capture.frames.map(({ data }) => data).join("")).toBe(expected);
|
|
expect(
|
|
capture.frames.map(({ raw_data, data }) => raw_data ?? data).join(""),
|
|
).toBe(original);
|
|
expect(capture.rendered_frames.at(-1)?.lines).toEqual(
|
|
expect.arrayContaining(expected.trimEnd().split("\r\n")),
|
|
);
|
|
expect(capture.rendered_frames.at(-1)?.serialized_state).toContain(
|
|
pids ? "child pid <pid>" : `child pid ${identity.child_pid}`,
|
|
);
|
|
expect(capture.truncation_details.raw_terminal).toMatchObject({
|
|
observed_bytes: Buffer.byteLength(original),
|
|
retained_bytes: Buffer.byteLength(original),
|
|
});
|
|
expect(capture.manifest.comparison_contract).toHaveProperty(
|
|
"terminal_observation_version",
|
|
"deferred-rendered-journal-v1",
|
|
);
|
|
expect(capture.exit).toMatchObject({ code: 0, reason: "exited" });
|
|
captures.push(capture);
|
|
}
|
|
const [first, second] = captures;
|
|
if (first === undefined) throw new Error("Capture was not retained");
|
|
if (second !== undefined)
|
|
expect(compareProcessCaptures(first, second).terminal).toBe("unchanged");
|
|
const legacyContract = { ...first.manifest.comparison_contract };
|
|
delete legacyContract.terminal_observation_version;
|
|
const legacy = parseProcessCapture({
|
|
...first,
|
|
manifest: {
|
|
...first.manifest,
|
|
comparison_contract: legacyContract,
|
|
comparison_contract_sha256: digestProcessCommitment(legacyContract),
|
|
},
|
|
});
|
|
expect(() => compareProcessCaptures(legacy, first)).toThrow(
|
|
expect.objectContaining({
|
|
issues: [
|
|
expect.objectContaining({
|
|
expected: ["terminal_observation_version"],
|
|
}),
|
|
],
|
|
}),
|
|
);
|
|
},
|
|
30_000,
|
|
);
|