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.
457 lines
16 KiB
TypeScript
457 lines
16 KiB
TypeScript
import { artifactMcpCall } from "../../../scripts/lib/artifact-e2e.mjs";
|
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
import { dirname, join } from "node:path";
|
|
import { Client } from "@modelcontextprotocol/client";
|
|
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
|
import { expect } from "vitest";
|
|
import { parse } from "yaml";
|
|
import { z } from "zod";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import { cliTest } from "../../support/cli/cliFixture.js";
|
|
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
|
|
|
|
cliTest.for(["setup", "uninstall"] as const)(
|
|
"DSH CLI %s refuses a multi-document patch without losing caller content",
|
|
async (operation, { cli }) => {
|
|
const home = await createTestTempDirectory("rea-dsh-multiple-documents-");
|
|
const profile = join(home, ".dsh");
|
|
const config = join(profile, "cordis.patch.yml");
|
|
await mkdir(profile);
|
|
const original = `# first document
|
|
- insert:
|
|
- id: rea-mcp
|
|
name: '@deepseek-ai/dsh-mcp-client'
|
|
config: {serverName: rea, transport: stdio, command: stale, args: [mcp]}
|
|
---
|
|
# preserve the second document and its comment
|
|
- insert:
|
|
- id: caller
|
|
name: caller-plugin
|
|
`;
|
|
await writeFile(config, original);
|
|
const result = await cli.run({
|
|
arguments:
|
|
operation === "setup"
|
|
? ["setup", "--client", "dsh", "--skill=false", "--yes", "--json"]
|
|
: ["uninstall", "--json"],
|
|
cwd: home,
|
|
environment: { HOME: home, USERPROFILE: home, DSH_HOME: profile },
|
|
});
|
|
expect(await readFile(config, "utf8")).toBe(original);
|
|
expect(result.exitCode, result.stdout).toBe(1);
|
|
expect(result.json).toMatchObject(
|
|
operation === "setup"
|
|
? { status: "needs_human", plannedActions: [], appliedActions: [] }
|
|
: {
|
|
status: "failed",
|
|
items: expect.arrayContaining([
|
|
expect.objectContaining({ name: "dsh", status: "failed" }),
|
|
]),
|
|
},
|
|
);
|
|
await expect(readFile(`${config}.rea.backup`)).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
},
|
|
);
|
|
|
|
cliTest.for([
|
|
{
|
|
label: "blank defaults",
|
|
harness: "",
|
|
agents: " \t ",
|
|
dshRoot: "home/.dsh",
|
|
agentsRoot: "home/.agents",
|
|
},
|
|
{
|
|
label: "tilde roots",
|
|
harness: "~/selected harness",
|
|
agents: "~\\shared agents",
|
|
dshRoot: "home/selected harness",
|
|
agentsRoot: "home/shared agents",
|
|
},
|
|
{
|
|
label: "relative roots",
|
|
harness: "./selected harness",
|
|
agents: "../shared agents",
|
|
dshRoot: "workspace/selected harness",
|
|
agentsRoot: "shared agents",
|
|
},
|
|
])(
|
|
"DSH CLI uses Harness path semantics through setup, doctor and uninstall for $label",
|
|
async ({ harness, agents, dshRoot, agentsRoot }, { cli }) => {
|
|
const root = await createTestTempDirectory("rea-dsh-paths-");
|
|
const home = join(root, "home");
|
|
const cwd = join(root, "workspace");
|
|
const config = join(root, dshRoot, "cordis.patch.yml");
|
|
const skillRoot = join(
|
|
root,
|
|
agentsRoot,
|
|
"skills",
|
|
"reverse-engineer-anything",
|
|
);
|
|
const skill = join(skillRoot, "SKILL.md");
|
|
await mkdir(home);
|
|
await mkdir(cwd);
|
|
await mkdir(dirname(config), { recursive: true });
|
|
const original =
|
|
"# caller patch\n- insert: [{id: caller, name: caller-plugin}]\n";
|
|
await writeFile(config, original);
|
|
const run = (args: readonly string[]) =>
|
|
cli.run({
|
|
arguments: [...args, "--json"],
|
|
cwd,
|
|
environment: {
|
|
HOME: home,
|
|
USERPROFILE: home,
|
|
DSH_HOME: harness,
|
|
DSH_AGENTS_HOME: agents,
|
|
},
|
|
});
|
|
const planned = await run(["setup", "--client", "dsh", "--dry-run"]);
|
|
expect(planned.exitCode).toBe(0);
|
|
expect(planned.json).toMatchObject({
|
|
status: "planned",
|
|
appliedActions: [],
|
|
plannedActions: expect.arrayContaining([
|
|
expect.objectContaining({ id: "configure_client:dsh", target: config }),
|
|
expect.objectContaining({ kind: "install_skill", target: skillRoot }),
|
|
]),
|
|
});
|
|
expect(await readFile(config, "utf8")).toBe(original);
|
|
await expect(readFile(skill)).rejects.toMatchObject({ code: "ENOENT" });
|
|
const configured = await run(["setup", "--client", "dsh", "--yes"]);
|
|
expect(configured.exitCode).toBe(0);
|
|
expect(configured.json).toMatchObject({
|
|
status: "ready",
|
|
clients: { dsh: { status: "configured" } },
|
|
});
|
|
const text = await readFile(config, "utf8");
|
|
expect(text).toContain("# caller patch");
|
|
expect(text).toContain("rea-mcp");
|
|
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original);
|
|
expect(await readFile(skill, "utf8")).toContain(
|
|
"name: reverse-engineer-anything",
|
|
);
|
|
const doctor = await run(["doctor", "--client", "dsh", "--skill"]);
|
|
expect(doctor.exitCode).toBe(0);
|
|
expect(doctor.json).toMatchObject({ healthy: true });
|
|
const repeated = await run(["setup", "--client", "dsh", "--yes"]);
|
|
expect(repeated.exitCode).toBe(0);
|
|
expect(repeated.json).toMatchObject({
|
|
status: "ready",
|
|
plannedActions: [],
|
|
appliedActions: [],
|
|
});
|
|
expect(await readFile(config, "utf8")).toBe(text);
|
|
const removed = await run(["uninstall"]);
|
|
expect(removed.exitCode).toBe(0);
|
|
expect(removed.json).toMatchObject({
|
|
status: "complete",
|
|
items: expect.arrayContaining([
|
|
expect.objectContaining({ name: "dsh", status: "removed" }),
|
|
]),
|
|
});
|
|
expect(parse(await readFile(config, "utf8"))).toEqual([
|
|
{ insert: [{ id: "caller", name: "caller-plugin" }] },
|
|
]);
|
|
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original);
|
|
await expect(readFile(skill)).rejects.toMatchObject({ code: "ENOENT" });
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"DSH CLI previews, approves, repairs, launches MCP, repeats and uninstalls while preserving caller YAML",
|
|
async ({ cli }) => {
|
|
const home = await createTestTempDirectory("rea-dsh-lifecycle-");
|
|
const profile = join(home, "selected harness");
|
|
const config = join(profile, "cordis.patch.yml");
|
|
const skill = join(
|
|
home,
|
|
".agents/skills/reverse-engineer-anything/SKILL.md",
|
|
);
|
|
await mkdir(profile);
|
|
const original = `# caller home patch
|
|
- insert:
|
|
- id: rea-mcp
|
|
name: '@deepseek-ai/dsh-mcp-client'
|
|
config: {serverName: rea, transport: stdio, command: stale, args: [mcp]}
|
|
- holder: &op
|
|
insert:
|
|
- id: unrelated
|
|
name: caller-plugin
|
|
disabled: true
|
|
config: {cwd: !!js process.cwd()}
|
|
- id: rea-mcp
|
|
name: '@deepseek-ai/dsh-mcp-client'
|
|
config:
|
|
serverName: rea
|
|
transport: stdio
|
|
command: stale
|
|
args: [mcp]
|
|
cwd: !!js process.cwd()
|
|
env:
|
|
REA_LOG_LEVEL: !!js process.env.REA_LOG_LEVEL
|
|
- *op
|
|
`;
|
|
await writeFile(config, original);
|
|
const environment = { HOME: home, USERPROFILE: home, DSH_HOME: profile };
|
|
const run = (args: readonly string[]) =>
|
|
cli.run({
|
|
arguments: [...args, "--json"],
|
|
cwd: home,
|
|
environment,
|
|
});
|
|
const plan = await run(["setup", "--client", "dsh", "--dry-run"]);
|
|
expect(plan.exitCode).toBe(0);
|
|
expect(plan.json).toMatchObject({
|
|
status: "planned",
|
|
appliedActions: [],
|
|
plannedActions: expect.arrayContaining([
|
|
expect.objectContaining({ id: "configure_client:dsh", target: config }),
|
|
expect.objectContaining({ id: "install_skill" }),
|
|
]),
|
|
});
|
|
expect(await readFile(config, "utf8")).toBe(original);
|
|
await expect(readFile(`${config}.rea.backup`)).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
const unapproved = await run(["setup", "--client", "dsh"]);
|
|
expect(unapproved.json).toMatchObject({
|
|
status: "needs_confirmation",
|
|
appliedActions: [],
|
|
});
|
|
expect(await readFile(config, "utf8")).toBe(original);
|
|
const configured = await run(["setup", "--client", "dsh", "--yes"]);
|
|
expect(configured.exitCode).toBe(0);
|
|
expect(configured.json).toMatchObject({
|
|
status: "ready",
|
|
clients: { dsh: { status: "configured" } },
|
|
});
|
|
const text = await readFile(config, "utf8");
|
|
expect(text).toContain("# caller home patch");
|
|
const patches = parse(text, {
|
|
customTags: [
|
|
{
|
|
tag: "tag:yaml.org,2002:js",
|
|
resolve: (source: string) => ({ __jsExpr: source }),
|
|
},
|
|
],
|
|
}) as { insert?: { id: string; config: unknown }[] }[];
|
|
const rows = patches.flatMap((patch) => patch.insert ?? []);
|
|
expect(rows.filter(({ id }) => id === "unrelated")).toEqual([
|
|
{
|
|
id: "unrelated",
|
|
name: "caller-plugin",
|
|
disabled: true,
|
|
config: { cwd: { __jsExpr: "process.cwd()" } },
|
|
},
|
|
]);
|
|
expect(rows.filter(({ id }) => id === "rea-mcp")).toHaveLength(1);
|
|
const registration = z
|
|
.object({
|
|
command: z.string(),
|
|
args: z.array(z.string()),
|
|
cwd: z.unknown(),
|
|
env: z.unknown(),
|
|
})
|
|
.parse(rows.find(({ id }) => id === "rea-mcp")?.config);
|
|
expect(registration.cwd).toEqual({ __jsExpr: "process.cwd()" });
|
|
expect(registration.env).toMatchObject({
|
|
REA_LOG_LEVEL: { __jsExpr: "process.env.REA_LOG_LEVEL" },
|
|
});
|
|
const doctor = await run(["doctor", "--client", "dsh", "--skill"]);
|
|
expect(doctor.exitCode).toBe(0);
|
|
expect(doctor.json).toMatchObject({
|
|
healthy: true,
|
|
identity: {
|
|
registrations: expect.arrayContaining([
|
|
expect.objectContaining({ client: "dsh", state: "aligned" }),
|
|
]),
|
|
},
|
|
});
|
|
await verifyGeneratedLauncher(registration, home, environment);
|
|
const repeated = await run(["setup", "--client", "dsh", "--yes"]);
|
|
expect(repeated.json).toMatchObject({
|
|
status: "ready",
|
|
appliedActions: [],
|
|
plannedActions: [],
|
|
});
|
|
expect(await readFile(config, "utf8")).toBe(text);
|
|
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original);
|
|
const removed = await run(["uninstall"]);
|
|
expect(removed.exitCode).toBe(0);
|
|
expect(removed.json).toMatchObject({
|
|
status: "complete",
|
|
items: expect.arrayContaining([
|
|
expect.objectContaining({ name: "dsh", status: "removed" }),
|
|
]),
|
|
});
|
|
const remaining = parse(await readFile(config, "utf8"), {
|
|
logLevel: "silent",
|
|
}) as { insert?: { id: string }[] }[];
|
|
expect(
|
|
remaining.flatMap((patch) => patch.insert ?? []).map(({ id }) => id),
|
|
).toEqual(["unrelated"]);
|
|
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original);
|
|
await expect(readFile(skill)).rejects.toMatchObject({ code: "ENOENT" });
|
|
},
|
|
);
|
|
|
|
async function verifyGeneratedLauncher(
|
|
registration: { command: string; args: string[] },
|
|
home: string,
|
|
environment: Record<string, string>,
|
|
): Promise<void> {
|
|
// Exercise the generated launcher. Native Cordis expression evaluation belongs
|
|
// to verify:deepseek-client, not this SDK transport journey.
|
|
const transport = new StdioClientTransport({
|
|
command: registration.command,
|
|
args: registration.args,
|
|
cwd: home,
|
|
env: {
|
|
PATH: process.env.PATH ?? "",
|
|
...environment,
|
|
},
|
|
stderr: "pipe",
|
|
});
|
|
const client = new Client(
|
|
{ name: "dsh-setup-lifecycle", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
try {
|
|
await client.connect(transport);
|
|
expect((await client.listTools()).tools.map(({ name }) => name)).toContain(
|
|
"binary_session",
|
|
);
|
|
const session = await artifactMcpCall(client, {
|
|
name: "binary_session",
|
|
arguments: {},
|
|
});
|
|
expect(session.isError).not.toBe(true);
|
|
expect(session.structuredContent).toMatchObject({
|
|
result: { open: false },
|
|
});
|
|
} finally {
|
|
await client.close();
|
|
await transport.close();
|
|
}
|
|
}
|
|
|
|
cliTest.for(["null", "caller-value", "[caller-value]"])(
|
|
"DSH CLI refuses to edit config %s and plain uninstall removes its owned row",
|
|
async (configValue, { cli }) => {
|
|
const home = await createTestTempDirectory("rea-dsh-malformed-config-");
|
|
const profile = join(home, ".dsh");
|
|
const config = join(profile, "cordis.patch.yml");
|
|
await mkdir(profile);
|
|
const initial = `# caller patch
|
|
- insert:
|
|
- id: other
|
|
name: caller-plugin
|
|
- id: rea-mcp
|
|
name: '@deepseek-ai/dsh-mcp-client'
|
|
config: ${configValue}
|
|
`;
|
|
await writeFile(config, initial);
|
|
const run = (args: readonly string[]) =>
|
|
cli.run({
|
|
arguments: [...args, "--json"],
|
|
cwd: home,
|
|
environment: { HOME: home, USERPROFILE: home, DSH_HOME: profile },
|
|
});
|
|
const configured = await run(["setup", "--client", "dsh", "--yes"]);
|
|
expect(configured.exitCode).not.toBe(0);
|
|
expect(configured.json).toMatchObject({
|
|
status: "needs_human",
|
|
appliedActions: [],
|
|
remediation: expect.stringContaining("config: Use a literal mapping"),
|
|
});
|
|
expect(await readFile(config, "utf8")).toBe(initial);
|
|
await expect(readFile(`${config}.rea.backup`)).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
const removed = await run(["uninstall"]);
|
|
expect(removed.exitCode).toBe(0);
|
|
expect(removed.json).toMatchObject({
|
|
status: "complete",
|
|
items: expect.arrayContaining([
|
|
expect.objectContaining({ name: "dsh", status: "removed" }),
|
|
]),
|
|
});
|
|
const remaining = await readFile(config, "utf8");
|
|
expect(remaining).toContain("# caller patch");
|
|
expect(parse(remaining)).toEqual([
|
|
{ insert: [{ id: "other", name: "caller-plugin" }] },
|
|
]);
|
|
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(initial);
|
|
},
|
|
);
|
|
|
|
cliTest.for([
|
|
{
|
|
label: "group-owned rows",
|
|
patch: `- id: tools
|
|
insert:
|
|
- id: other
|
|
name: caller-plugin
|
|
- id: rea-mcp
|
|
name: '@deepseek-ai/dsh-mcp-client'
|
|
config: {serverName: rea, transport: stdio, command: stale, args: [mcp]}
|
|
`,
|
|
remediation: "group with unknown activity",
|
|
removal: "removed",
|
|
},
|
|
{
|
|
label: "expression-backed inserts",
|
|
patch: "- insert: !!js process.env.DSH_ROWS\n",
|
|
remediation: "unknown structure",
|
|
removal: "retained",
|
|
},
|
|
])(
|
|
"DSH CLI rejects setup and preserves ownership for $label",
|
|
async ({ patch, remediation, removal }, { cli }) => {
|
|
const home = await createTestTempDirectory("rea-dsh-ownership-");
|
|
const profile = join(home, ".dsh");
|
|
const config = join(profile, "cordis.patch.yml");
|
|
await mkdir(profile);
|
|
await writeFile(config, patch);
|
|
const run = (args: readonly string[]) =>
|
|
cli.run({
|
|
arguments: [...args, "--json"],
|
|
cwd: home,
|
|
environment: { HOME: home, USERPROFILE: home, DSH_HOME: profile },
|
|
});
|
|
const setup = await run(["setup", "--client", "dsh", "--yes"]);
|
|
expect(setup.exitCode).not.toBe(0);
|
|
expect(setup.json).toMatchObject({
|
|
status: "needs_human",
|
|
appliedActions: [],
|
|
remediation: expect.stringContaining(remediation),
|
|
});
|
|
expect(await readFile(config, "utf8")).toBe(patch);
|
|
await expect(readFile(`${config}.rea.backup`)).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
const uninstall = await run(["uninstall"]);
|
|
expect(uninstall.exitCode).toBe(0);
|
|
expect(uninstall.json).toMatchObject({
|
|
status: "complete",
|
|
items: expect.arrayContaining([
|
|
expect.objectContaining({ name: "dsh", status: removal }),
|
|
]),
|
|
});
|
|
if (removal === "removed") {
|
|
expect(parse(await readFile(config, "utf8"))).toEqual([
|
|
{ id: "tools", insert: [{ id: "other", name: "caller-plugin" }] },
|
|
]);
|
|
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(patch);
|
|
} else {
|
|
expect(await readFile(config, "utf8")).toBe(patch);
|
|
await expect(readFile(`${config}.rea.backup`)).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
}
|
|
},
|
|
);
|