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.
397 lines
14 KiB
TypeScript
397 lines
14 KiB
TypeScript
import { unknownTarget } from "../../src/domain/targetIdentity.js";
|
|
import { getHeapStatistics } from "node:v8";
|
|
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import { createServer } from "../../src/server/createServer.js";
|
|
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
|
|
import { loadNativeMatchingBundleSources } from "../../src/application/native/NativeMatchingBundleSources.js";
|
|
import { NativeMatchingAdmission } from "../../src/application/native/NativeMatchingAdmission.js";
|
|
import { matchNativeFunctionsInputSchema } from "../../src/contracts/native/functionMatchingContracts.js";
|
|
import {
|
|
createEvidence,
|
|
isImmutableEvidence,
|
|
parseEvidence,
|
|
evidenceSubjectInput,
|
|
} from "../../src/domain/evidence.js";
|
|
import { createTestBinarySession } from "../fixtures/binarySession.js";
|
|
import { writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { afterEach, expect, it, vi } from "vitest";
|
|
import {
|
|
createImmutableEvidenceBundle,
|
|
safeParseImmutableEvidenceBundle,
|
|
} from "../../src/domain/evidenceBundle.js";
|
|
import { ArtifactResourceScope } from "../../src/resources/ArtifactResourceScope.js";
|
|
import { inspectAnalysisView } from "../../src/application/analysisView/AnalysisViewService.js";
|
|
import { readImmutableEvidenceBundle } from "../../src/application/EvidenceBundleFiles.js";
|
|
import { requireAuthenticationHeadroom } from "../../src/application/JsonAuthenticationHeadroom.js";
|
|
import { SOURCE_TO_BUNDLE_COMPARISON_EXAMPLE } from "../../src/contracts/javascript/javascriptApplicationWorkflowExamples.js";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
import { holdFirstFileRead } from "../support/holdFirstFileRead.js";
|
|
import { createMcpSchemaValidation } from "../fixtures/mcpSchemaValidation.js";
|
|
|
|
vi.mock("../../src/application/JsonAuthenticationHeadroom.js", { spy: true });
|
|
vi.mock("node:v8", { spy: true });
|
|
vi.mock("../../src/domain/evidenceBundle.js", { spy: true });
|
|
afterEach(() => vi.restoreAllMocks());
|
|
|
|
it.each(["compare_bundles", "match_native_functions"])(
|
|
"attributes bundle authentication refusal to the actual %s boundary",
|
|
async (operation) => {
|
|
const { path, parent } = await savedParent();
|
|
const actual = await vi.importActual<
|
|
typeof import("../../src/application/JsonAuthenticationHeadroom.js")
|
|
>("../../src/application/JsonAuthenticationHeadroom.js");
|
|
vi.mocked(requireAuthenticationHeadroom).mockImplementation(
|
|
(value, options) =>
|
|
actual.requireAuthenticationHeadroom(value, {
|
|
...options,
|
|
availableBytes: 0,
|
|
}),
|
|
);
|
|
const resources = new ArtifactResourceScope();
|
|
try {
|
|
if (operation === "match_native_functions") {
|
|
const source = {
|
|
kind: "retained-evidence",
|
|
evidence_id: parent.evidence_id,
|
|
};
|
|
await expect(
|
|
loadNativeMatchingBundleSources({
|
|
input: matchNativeFunctionsInputSchema.parse({
|
|
left: source,
|
|
right: source,
|
|
}),
|
|
paths: [path],
|
|
resources,
|
|
admission: new NativeMatchingAdmission(operation),
|
|
}),
|
|
).rejects.toMatchObject({
|
|
_tag: "AnalysisResourceConstraintError",
|
|
operation,
|
|
});
|
|
} else {
|
|
const session = createTestBinarySession(() => {
|
|
throw new Error("Comparison must not open a target");
|
|
});
|
|
const server = createServer({ kind: "session", session });
|
|
const client = new Client(
|
|
{
|
|
name: "bundle-operation-refusal",
|
|
version: "1",
|
|
},
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
try {
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
const result = await client.callTool({
|
|
name: operation,
|
|
arguments: { left_bundle_path: path, right_bundle_path: path },
|
|
});
|
|
expect(result.isError).toBe(true);
|
|
expect(parseMcpToolError(result)).toMatchObject({
|
|
error: { code: "resource_constraint", details: { operation } },
|
|
});
|
|
expect(session.exportEvidenceBundle().records).toEqual([]);
|
|
} finally {
|
|
await client.close();
|
|
await server.close();
|
|
await session.close();
|
|
}
|
|
}
|
|
} finally {
|
|
vi.mocked(requireAuthenticationHeadroom).mockImplementation(
|
|
actual.requireAuthenticationHeadroom,
|
|
);
|
|
await resources.close();
|
|
}
|
|
expect(resources.hasPending).toBe(false);
|
|
},
|
|
);
|
|
|
|
const savedParent = async () => {
|
|
const root = await createTestTempDirectory("rea-managed-bundle-guard-");
|
|
const path = join(root, "parent.json");
|
|
const parent = SOURCE_TO_BUNDLE_COMPARISON_EXAMPLE.application;
|
|
await writeFile(
|
|
path,
|
|
JSON.stringify(createImmutableEvidenceBundle([parent])),
|
|
);
|
|
return { path, parent };
|
|
};
|
|
|
|
it("keeps the owning operation when native matching reloads a needed earlier bundle", async () => {
|
|
const root = await createTestTempDirectory("rea-native-bundle-reload-");
|
|
const path = join(root, "parent.json"),
|
|
empty = join(root, "empty.json");
|
|
const parent = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
{ id: "fixture", name: "Fixture", version: "1" },
|
|
{ operation: "list_procedures", parameters: {}, result: {} },
|
|
);
|
|
await writeFile(
|
|
path,
|
|
JSON.stringify(createImmutableEvidenceBundle([parent])),
|
|
);
|
|
await writeFile(empty, JSON.stringify(createImmutableEvidenceBundle([])));
|
|
const actual = await vi.importActual<
|
|
typeof import("../../src/application/JsonAuthenticationHeadroom.js")
|
|
>("../../src/application/JsonAuthenticationHeadroom.js");
|
|
let reads = 0;
|
|
vi.mocked(requireAuthenticationHeadroom).mockImplementation(
|
|
(value, options) =>
|
|
actual.requireAuthenticationHeadroom(
|
|
value,
|
|
++reads === 3 ? { ...options, availableBytes: 0 } : options,
|
|
),
|
|
);
|
|
const resources = new ArtifactResourceScope();
|
|
const source = { kind: "retained-evidence", evidence_id: parent.evidence_id };
|
|
try {
|
|
await expect(
|
|
loadNativeMatchingBundleSources({
|
|
input: matchNativeFunctionsInputSchema.parse({
|
|
left: source,
|
|
right: source,
|
|
}),
|
|
paths: [path, empty],
|
|
resources,
|
|
admission: new NativeMatchingAdmission("match_native_functions"),
|
|
}),
|
|
).rejects.toMatchObject({
|
|
_tag: "AnalysisResourceConstraintError",
|
|
operation: "match_native_functions",
|
|
});
|
|
expect(reads).toBe(3);
|
|
} finally {
|
|
vi.mocked(requireAuthenticationHeadroom).mockImplementation(
|
|
actual.requireAuthenticationHeadroom,
|
|
);
|
|
await resources.close();
|
|
}
|
|
expect(resources.hasPending).toBe(false);
|
|
});
|
|
|
|
it("admits bundle authentication before cloning and hashing retained records", async () => {
|
|
const { path } = await savedParent();
|
|
const actual = await vi.importActual<
|
|
typeof import("../../src/application/JsonAuthenticationHeadroom.js")
|
|
>("../../src/application/JsonAuthenticationHeadroom.js");
|
|
vi.mocked(requireAuthenticationHeadroom).mockImplementationOnce(
|
|
(value, options) =>
|
|
actual.requireAuthenticationHeadroom(value, {
|
|
...options,
|
|
availableBytes: 0,
|
|
}),
|
|
);
|
|
vi.mocked(safeParseImmutableEvidenceBundle).mockClear();
|
|
const resources = new ArtifactResourceScope();
|
|
try {
|
|
expect(
|
|
await readImmutableEvidenceBundle(path, undefined, resources),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "AnalysisResourceConstraintError", resource: "memory" },
|
|
});
|
|
expect(safeParseImmutableEvidenceBundle).not.toHaveBeenCalled();
|
|
} finally {
|
|
await resources.close();
|
|
}
|
|
expect(resources.hasPending).toBe(false);
|
|
});
|
|
|
|
it("preserves the view operation and caller reason when bundle I/O is cancelled", async () => {
|
|
const { path, parent } = await savedParent();
|
|
const held = await holdFirstFileRead(path);
|
|
const controller = new AbortController();
|
|
const reason = new Error("cancel managed bundle page");
|
|
const resources = new ArtifactResourceScope();
|
|
try {
|
|
const pending = inspectAnalysisView(
|
|
{
|
|
source: { kind: "retained-evidence", evidence_id: parent.evidence_id },
|
|
view: { kind: "summary" },
|
|
},
|
|
undefined,
|
|
{ evidenceBundlePath: path, resources, signal: controller.signal },
|
|
);
|
|
await held.reached(pending, "first file read");
|
|
controller.abort(reason);
|
|
held.release();
|
|
const result = await pending;
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "AnalysisCancelledError",
|
|
operation: "inspect_analysis_view",
|
|
},
|
|
});
|
|
if (result.ok) throw new Error("Expected cancelled bundle read");
|
|
expect(result.error.cause).toBe(reason);
|
|
} finally {
|
|
held.release();
|
|
await resources.close();
|
|
}
|
|
expect(resources.hasPending).toBe(false);
|
|
});
|
|
|
|
it.each(["JSON assembly", "bundle authentication"])(
|
|
"attributes low-headroom %s to the invoking view",
|
|
async (stage) => {
|
|
const { path, parent } = await savedParent();
|
|
const actualV8 = await vi.importActual<typeof import("node:v8")>("node:v8");
|
|
if (stage === "JSON assembly") {
|
|
vi.mocked(getHeapStatistics).mockReturnValue({
|
|
...getHeapStatistics(),
|
|
total_available_size: 0,
|
|
});
|
|
} else {
|
|
const actual = await vi.importActual<
|
|
typeof import("../../src/application/JsonAuthenticationHeadroom.js")
|
|
>("../../src/application/JsonAuthenticationHeadroom.js");
|
|
vi.mocked(requireAuthenticationHeadroom).mockImplementationOnce(
|
|
(value, options) =>
|
|
actual.requireAuthenticationHeadroom(value, {
|
|
...options,
|
|
availableBytes: 0,
|
|
}),
|
|
);
|
|
}
|
|
const resources = new ArtifactResourceScope();
|
|
try {
|
|
expect(
|
|
await inspectAnalysisView(
|
|
{
|
|
source: {
|
|
kind: "retained-evidence",
|
|
evidence_id: parent.evidence_id,
|
|
},
|
|
view: { kind: "summary" },
|
|
},
|
|
undefined,
|
|
{ evidenceBundlePath: path, resources },
|
|
),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "AnalysisResourceConstraintError",
|
|
operation: "inspect_analysis_view",
|
|
},
|
|
});
|
|
} finally {
|
|
vi.mocked(getHeapStatistics).mockImplementation(
|
|
actualV8.getHeapStatistics,
|
|
);
|
|
await resources.close();
|
|
}
|
|
expect(resources.hasPending).toBe(false);
|
|
},
|
|
);
|
|
|
|
it("authenticates escaped payloads without serializing either complete bundle", async () => {
|
|
const root = await createTestTempDirectory("rea-escaped-bundle-");
|
|
const path = join(root, "parent.json");
|
|
const escaped = '\u0000\u0001\\"😀\ud800'.repeat(20000);
|
|
const example = SOURCE_TO_BUNDLE_COMPARISON_EXAMPLE.application;
|
|
const parent = createEvidence(
|
|
evidenceSubjectInput(example.subject),
|
|
example.provider,
|
|
{
|
|
predicateType: example.predicate_type,
|
|
operation: example.operation,
|
|
parameters: example.parameters,
|
|
result: example.normalized_result,
|
|
rawResult: { escaped },
|
|
},
|
|
);
|
|
await writeFile(
|
|
path,
|
|
JSON.stringify(createImmutableEvidenceBundle([parent])),
|
|
);
|
|
const stringify = JSON.stringify;
|
|
const fullDocuments: unknown[] = [];
|
|
vi.spyOn(JSON, "stringify").mockImplementation((value: unknown) => {
|
|
if (
|
|
typeof value === "object" &&
|
|
value !== null &&
|
|
Object.hasOwn(value, "records")
|
|
) {
|
|
fullDocuments.push(value);
|
|
throw new Error(
|
|
"Document-sized JSON allocation forbidden during authentication",
|
|
);
|
|
}
|
|
return stringify(value);
|
|
});
|
|
const resources = new ArtifactResourceScope();
|
|
try {
|
|
const loaded = await readImmutableEvidenceBundle(
|
|
path,
|
|
undefined,
|
|
resources,
|
|
);
|
|
if (!loaded.ok) throw loaded.error;
|
|
expect(loaded.value.records[0]?.evidence_id).toBe(parent.evidence_id);
|
|
expect(loaded.value.records[0]?.raw_result).toEqual({ escaped });
|
|
expect(fullDocuments).toEqual([]);
|
|
} finally {
|
|
await resources.close();
|
|
}
|
|
expect(resources.hasPending).toBe(false);
|
|
});
|
|
|
|
it("seals a fresh object-heavy Evidence tree without retaining a set of completed rows", () => {
|
|
const provider = { id: "fixture", name: "Fixture", version: "1" };
|
|
const row = { seal_probe: true, fields: [1, 2] };
|
|
const result = { rows: Array.from({ length: 512 }, () => row) };
|
|
const observation = {
|
|
operation: "inspect_managed_members",
|
|
parameters: {},
|
|
result,
|
|
};
|
|
const complete = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
provider,
|
|
observation,
|
|
);
|
|
const add = Set.prototype.add;
|
|
const guarded = vi.spyOn(Set.prototype, "add").mockImplementation(function (
|
|
this: Set<unknown>,
|
|
value: unknown,
|
|
) {
|
|
if (
|
|
typeof value === "object" &&
|
|
value !== null &&
|
|
Object.hasOwn(value, "seal_probe") &&
|
|
this.size > 32
|
|
)
|
|
throw new Error(
|
|
"Sealing retained completed rows beyond its ancestry budget",
|
|
);
|
|
return add.call(this, value);
|
|
});
|
|
let sealed;
|
|
try {
|
|
sealed = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
provider,
|
|
observation,
|
|
{
|
|
immutable: true,
|
|
},
|
|
);
|
|
} finally {
|
|
guarded.mockRestore();
|
|
}
|
|
expect(sealed).toEqual(complete);
|
|
expect(isImmutableEvidence(sealed)).toBe(true);
|
|
const data = sealed.normalized_result as typeof result;
|
|
expect(data.rows[0]).not.toBe(row);
|
|
expect(data.rows[0]).not.toBe(data.rows[1]);
|
|
expect(Object.isFrozen(data.rows[0]!.fields)).toBe(true);
|
|
expect(Object.isFrozen(row)).toBe(false);
|
|
expect(parseEvidence(sealed)).toBe(sealed);
|
|
});
|