Files
reverse-engineering-anything/tests/boundary/cli/packageRunnerBoundInstallation.test.ts
morluto 3ab956b327 fix(setup): repair npx registrations after the npx cache is pruned (#15249)
* fix(setup): refresh registrations from a removed npx cache entry

npx rea-agents setup saves REA's script in npm's _npx cache. After npm prunes that entry, setup could not read the package manifest, marked the registration unknown and asked for a manual config edit. A saved script with setup's exact npx layout that no longer exists is now REA-owned and stale, so npx rea-agents@latest setup replaces it and doctor names that command. The npx cache matcher moves to one module shared by doctor and setup.

* docs(agents): keep npx rea-agents setup as the onboarding path

* test(setup): run real npx setup through a cache prune and repair

The end-to-end run showed POSIX setup saves npm exec's node_modules/.bin/rea alias, not the package script, so removed-npx recognition now accepts both layouts setup writes.
2026-11-30 10:21:49 -05:00

329 lines
10 KiB
TypeScript

import {
access,
mkdir,
readFile,
rename,
rm,
symlink,
writeFile,
} from "node:fs/promises";
import { delimiter, join, relative, resolve, sep } from "node:path";
import { Client } from "@modelcontextprotocol/client";
import {
StdioClientTransport,
getDefaultEnvironment,
} from "@modelcontextprotocol/client/stdio";
import { expect } from "vitest";
import { z } from "zod";
import { npmInvocation } from "../../../scripts/lib/npm-invocation.mjs";
import { PRODUCT_IDENTITY } from "../../../src/identity.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
const registrationSchema = z.object({
mcpServers: z.object({
rea: z.object({ command: z.string(), args: z.array(z.string()) }),
}),
});
cliTest.skipIf(process.platform === "win32")(
"real offline npm setup binds an installation across project starts and cache restoration",
async ({ processes }) => {
const root = await createTestTempDirectory("rea-real-npm-binding-");
const home = join(root, "home"),
trusted = join(root, "trusted"),
project = join(root, "project");
for (const path of [
home,
trusted,
join(project, "node_modules", ".bin"),
join(project, "node_modules", PRODUCT_IDENTITY.packageName),
])
await mkdir(path, { recursive: true });
const environment = {
...process.env,
HOME: home,
USERPROFILE: home,
npm_config_cache: join(root, "cache"),
npm_config_prefix: join(root, "prefix"),
npm_config_userconfig: join(root, "user.npmrc"),
npm_config_globalconfig: join(root, "global.npmrc"),
npm_config_update_notifier: "false",
};
const npm = npmInvocation(
[
"exec",
"--offline",
"--ignore-scripts",
"--yes",
`--package=file:${resolve(".")}`,
"--",
"rea",
"setup",
"--client",
"omp",
"--no-skill",
"--yes",
"--json",
],
environment,
);
const setup = await processes.run(npm.command, npm.arguments, {
cwd: trusted,
env: environment,
});
expect(setup.exitCode, setup.stderr).toBe(0);
const configPath = join(home, ".omp", "agent", "mcp.json");
const original = await readFile(configPath, "utf8");
const registration = registrationSchema.parse(JSON.parse(original))
.mcpServers.rea;
const alias = registration.args[0];
expect(alias).toBeDefined();
if (alias === undefined) throw new Error("Missing registered entrypoint");
// Only remove an alias in this test's owned cache, never the source installation.
expect(alias.startsWith(join(root, "cache") + "/")).toBe(true);
expect(registration.args.slice(1)).toEqual(["mcp"]);
const marker = join(root, "project-candidate-called");
const candidate = join(
project,
"node_modules",
PRODUCT_IDENTITY.packageName,
);
await writeFile(
join(project, "package.json"),
JSON.stringify({
name: "owned-registration-project",
version: "1.0.0",
dependencies: {
[PRODUCT_IDENTITY.packageName]: PRODUCT_IDENTITY.packageVersion,
},
}),
);
await writeFile(
join(candidate, "package.json"),
JSON.stringify({
name: PRODUCT_IDENTITY.packageName,
version: PRODUCT_IDENTITY.packageVersion,
bin: { rea: "candidate.cjs" },
}),
);
await writeFile(
join(candidate, "candidate.cjs"),
`#!${process.execPath}\nrequire('node:fs').writeFileSync(${JSON.stringify(marker)}, 'unexpected');\n`,
{ mode: 0o700 },
);
await symlink(
`../${PRODUCT_IDENTITY.packageName}/candidate.cjs`,
join(project, "node_modules", ".bin", "rea"),
);
await writeFile(join(project, ".npmrc"), "registry=http://127.0.0.1:9/\n");
const clientEnvironment = {
...getDefaultEnvironment(),
HOME: home,
PATH: [
join(project, "node_modules", ".bin"),
process.env.PATH ?? "",
].join(delimiter),
};
const connect = async () => {
const transport = new StdioClientTransport({
...registration,
cwd: project,
env: clientEnvironment,
stderr: "pipe",
});
const client = new Client(
{
name: "real-npm-registration",
version: "1",
},
{ jsonSchemaValidator: createMcpSchemaValidation() },
);
try {
await client.connect(transport);
expect(client.getServerVersion()?.version).toBe(
PRODUCT_IDENTITY.packageVersion,
);
const status = await client.callTool({
name: "binary_session",
arguments: {},
});
expect(status.isError).not.toBe(true);
expect(status.structuredContent).toMatchObject({
result: { open: false },
});
} finally {
await client.close();
await transport.close();
}
};
await connect();
await rename(alias, `${alias}.removed`);
try {
const missing = await processes.run(
registration.command,
registration.args,
{ cwd: project, env: clientEnvironment },
);
expect(missing.exitCode).not.toBe(0);
await expect(access(marker)).rejects.toMatchObject({ code: "ENOENT" });
} finally {
await rename(`${alias}.removed`, alias);
}
await connect();
expect(await readFile(configPath, "utf8")).toBe(original);
await expect(access(marker)).rejects.toMatchObject({ code: "ENOENT" });
},
);
// `npx rea-agents setup` is the onboarding path; npm exec is what npx runs.
cliTest.skipIf(process.platform === "win32")(
"real npx setup starts the MCP server and repairs itself after the npx cache is pruned",
async ({ cli, processes }) => {
const root = await createTestTempDirectory("rea-real-npx-setup-");
const home = join(root, "home"),
trusted = join(root, "trusted"),
cache = join(root, "cache");
for (const path of [home, trusted]) await mkdir(path, { recursive: true });
const environment = {
...process.env,
HOME: home,
USERPROFILE: home,
npm_config_cache: cache,
npm_config_prefix: join(root, "prefix"),
npm_config_userconfig: join(root, "user.npmrc"),
npm_config_globalconfig: join(root, "global.npmrc"),
npm_config_update_notifier: "false",
};
const configPath = join(home, ".omp", "agent", "mcp.json");
const npxSetup = async () => {
const npm = npmInvocation(
[
"exec",
"--offline",
"--ignore-scripts",
"--yes",
`--package=file:${resolve(".")}`,
"--",
"rea",
"setup",
"--client",
"omp",
"--no-skill",
"--yes",
"--json",
],
environment,
);
const setup = await processes.run(npm.command, npm.arguments, {
cwd: trusted,
env: environment,
});
expect(setup.exitCode, setup.stderr).toBe(0);
return registrationSchema.parse(
JSON.parse(await readFile(configPath, "utf8")),
).mcpServers.rea;
};
const connect = async (registration: {
command: string;
args: string[];
}) => {
const transport = new StdioClientTransport({
...registration,
cwd: trusted,
env: { ...getDefaultEnvironment(), HOME: home },
stderr: "pipe",
});
const client = new Client(
{ name: "real-npx-setup", version: "1" },
{ jsonSchemaValidator: createMcpSchemaValidation() },
);
try {
await client.connect(transport);
expect(client.getServerVersion()?.version).toBe(
PRODUCT_IDENTITY.packageVersion,
);
} finally {
await client.close();
await transport.close();
}
};
const registration = await npxSetup();
await connect(registration);
const script = registration.args[0] ?? "";
const [npxRoot, key] = relative(cache, script).split(sep);
expect(npxRoot).toBe("_npx");
expect(key).toMatch(/^[0-9a-f]{16}$/u);
// npm prunes the whole cache entry; this removes only the test's own cache.
await rm(join(cache, "_npx", key ?? ""), { recursive: true, force: true });
const doctor = await cli.run({
arguments: ["doctor", "--client", "omp", "--skill=false", "--json"],
cwd: trusted,
environment: { HOME: home, USERPROFILE: home },
});
expect(doctor.json).toMatchObject({
identity: {
registrations: expect.arrayContaining([
expect.objectContaining({
client: "omp",
command_identity: "owned",
state: "stale",
remediation: expect.stringContaining(
`Run npx ${PRODUCT_IDENTITY.packageSpecifier} setup`,
),
}),
]),
},
});
await connect(await npxSetup());
},
);
cliTest.skipIf(process.platform === "win32")(
"mcp add preserves a selected entrypoint with spaces and both quote characters",
async ({ processes }) => {
const home = await createTestTempDirectory("rea-amp-command-");
const bin = join(home, `bin 'quoted' "double"`);
await mkdir(bin);
const alias = join(bin, "rea");
await symlink(resolve("scripts/rea.mjs"), alias);
const result = await processes.run(
process.execPath,
[alias, "mcp", "add", "--agent", "amp"],
{
cwd: home,
env: {
...process.env,
HOME: home,
USERPROFILE: home,
PATH: [bin, process.env.PATH ?? ""].join(delimiter),
},
},
);
expect(result.exitCode, result.stderr).toBe(0);
const settings = z
.object({
"amp.mcpServers": z.object({
rea: z.object({ command: z.string(), args: z.array(z.string()) }),
}),
})
.parse(
JSON.parse(
await readFile(join(home, ".config", "amp", "settings.json"), "utf8"),
),
);
expect(settings["amp.mcpServers"].rea.args).toEqual([alias, "mcp"]);
const start = await processes.run(
settings["amp.mcpServers"].rea.command,
[alias, "--version"],
{ cwd: home },
);
expect(start.exitCode, start.stderr).toBe(0);
expect(start.stdout).toContain(PRODUCT_IDENTITY.packageVersion);
},
);