Files
reverse-engineering-anything/tests/boundary/filesystem/effectiveProjectRegistration.test.ts
061239a5de fix(setup): follow the registration Copilot, Codex, and Kiro load (#15230)
Follow the effective Copilot, Codex, and Kiro MCP registration. Use the
same resolved Kiro agent file for the approval plan and the write.

Fixes #15032.
Fixes #15034.
Fixes #15035.

Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)
Co-authored-by: N0zoM1z0 <161784452+N0zoM1z0@users.noreply.github.com>
Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-12-01 00:19:28 +08:00

536 lines
19 KiB
TypeScript

import { mkdir, readFile, rm, unlink, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { expect, it } from "vitest";
import {
effectiveClientServer,
type ClientConfigurationDocument,
} from "../../../src/application/ClientConfigurationDocument.js";
import { readEffectiveClientRegistrationConfiguration } from "../../../src/application/ClientConfigurationFiles.js";
import {
configureClientConfiguration,
inspectClientConfiguration,
} from "../../../src/application/SetupClientConfiguration.js";
import { clientProjectConfigurationPaths } from "../../../src/application/SupportedClientLocations.js";
import { supportedClients } from "../../../src/application/SupportedClients.js";
import { systemUninstallHost } from "../../../src/application/Uninstall.js";
import { uninstallConfigPaths } from "../../../src/application/UninstallClientConfiguration.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const writeText = async (path: string, text: string) => {
await mkdir(dirname(path), { recursive: true });
await writeFile(path, text);
};
const writeJson = async (path: string, value: unknown) => {
await writeText(path, JSON.stringify(value));
};
const reaServer = (
document: ClientConfigurationDocument | undefined,
name = "rea",
) =>
document === undefined ? undefined : effectiveClientServer(document, name);
const copilotServer = (
command: string,
extra: Record<string, unknown> = {},
) => ({
type: "stdio",
command,
args: ["mcp"],
tools: ["*"],
...extra,
});
const codexServer = (command: string) =>
`[mcp_servers.rea]\ncommand = ${JSON.stringify(command)}\nargs = ["mcp"]\n`;
const trust = (directory: string) =>
`[projects.${JSON.stringify(directory)}]\ntrust_level = "trusted"\n`;
const layout = async (git = true) => {
const root = await createTestTempDirectory("rea-effective-project-");
const home = join(root, "home");
const repository = join(root, "repository");
const projectDirectory = join(repository, "packages", "child");
if (git) await mkdir(join(repository, ".git"), { recursive: true });
await mkdir(projectDirectory, { recursive: true });
const clientFor = (name: string, env: NodeJS.ProcessEnv = {}) => {
const client = supportedClients(home, process.platform, env).find(
(item) => item.name === name,
);
if (client === undefined) throw new Error(`${name} is missing`);
return client;
};
const read = (
name: string,
directory = projectDirectory,
env: NodeJS.ProcessEnv = {},
) =>
readEffectiveClientRegistrationConfiguration(
clientFor(name, env),
undefined,
process.platform,
{ projectDirectory: directory },
);
return { root, home, repository, projectDirectory, clientFor, read };
};
it("lists Copilot project files from the Git root down to the working directory", async () => {
const { repository, projectDirectory, clientFor } = await layout();
const paths = clientProjectConfigurationPaths(
clientFor("copilot_cli"),
projectDirectory,
process.platform,
);
expect(paths.ok ? paths.value : paths.error.detail).toEqual([
join(repository, ".github", "mcp.json"),
join(repository, ".mcp.json"),
join(repository, "packages", ".github", "mcp.json"),
join(repository, "packages", ".mcp.json"),
join(projectDirectory, ".github", "mcp.json"),
join(projectDirectory, ".mcp.json"),
]);
});
it("reads only the Copilot working directory outside a Git repository", async () => {
const { projectDirectory, clientFor } = await layout(false);
const paths = clientProjectConfigurationPaths(
clientFor("copilot_cli"),
projectDirectory,
process.platform,
);
expect(paths.ok ? paths.value : paths.error.detail).toEqual([
join(projectDirectory, ".github", "mcp.json"),
join(projectDirectory, ".mcp.json"),
]);
});
it("uses the nearest Copilot project rea server and keeps unrelated user servers", async () => {
const { clientFor, projectDirectory, repository, read } = await layout();
const client = clientFor("copilot_cli");
const rootConfig = join(repository, ".mcp.json");
const childGithub = join(projectDirectory, ".github", "mcp.json");
const childConfig = join(projectDirectory, ".mcp.json");
await writeJson(client.configPath, {
mcpServers: {
rea: copilotServer("/user/node", { env: { CALLER_SETTING: "user" } }),
peer: copilotServer("/user/peer"),
},
});
await writeJson(rootConfig, {
mcpServers: { rea: copilotServer("/root/node") },
});
await writeJson(childGithub, {
mcpServers: { rea: copilotServer("/github/node") },
});
await writeJson(childConfig, {
mcpServers: { rea: copilotServer("/project/node") },
});
const result = await read("copilot_cli");
if (!result.ok) throw new Error(result.error.detail);
expect(result.value).toMatchObject({
scope: "project",
configPath: childConfig,
launcherConfigPath: childConfig,
projectInspection: "evaluated",
});
expect(reaServer(result.value.document, "rea")).toMatchObject(
copilotServer("/project/node"),
);
expect(reaServer(result.value.document, "peer")).toMatchObject(
copilotServer("/user/peer"),
);
const owned = await readEffectiveClientRegistrationConfiguration(
client,
undefined,
process.platform,
{ projectDirectory, mode: "ownership" },
);
if (!owned.ok) throw new Error(owned.error.detail);
expect(
owned.value.projectSources?.map((source) => source.sourcePath),
).toEqual(expect.arrayContaining([rootConfig, childGithub, childConfig]));
});
it("accepts a bare Copilot project server map", async () => {
const { projectDirectory, read } = await layout();
const project = join(projectDirectory, ".mcp.json");
await writeJson(project, { rea: copilotServer("/bare/node") });
const result = await read("copilot_cli");
if (!result.ok) throw new Error(result.error.detail);
expect(result.value.configPath).toBe(project);
expect(reaServer(result.value.document, "rea")).toMatchObject({
command: "/bare/node",
});
});
it("uses the nearest trusted Codex project file", async () => {
const { clientFor, repository, projectDirectory, read } = await layout();
const client = clientFor("codex");
const rootConfig = join(repository, ".codex", "config.toml");
const childConfig = join(projectDirectory, ".codex", "config.toml");
await writeText(
client.configPath,
`${codexServer("/user/node")}\n${trust(repository)}\n${trust(projectDirectory)}`,
);
await writeText(rootConfig, codexServer("/root/node"));
await writeText(childConfig, codexServer("/child/node"));
const nearest = await read("codex");
if (!nearest.ok) throw new Error(nearest.error.detail);
expect(nearest.value.configPath).toBe(childConfig);
expect(reaServer(nearest.value.document, "rea")).toMatchObject({
command: "/child/node",
});
await unlink(childConfig);
const rootOnly = await read("codex");
if (!rootOnly.ok) throw new Error(rootOnly.error.detail);
expect(rootOnly.value.configPath).toBe(rootConfig);
expect(reaServer(rootOnly.value.document, "rea")).toMatchObject({
command: "/root/node",
});
});
it("keeps the Codex user registration when the project directory is not trusted", async () => {
const { clientFor, repository, projectDirectory, read } = await layout();
const client = clientFor("codex");
const rootConfig = join(repository, ".codex", "config.toml");
const childConfig = join(projectDirectory, ".codex", "config.toml");
await writeText(
client.configPath,
`${codexServer("/user/node")}\n${trust(repository)}`,
);
await writeText(rootConfig, codexServer("/root/node"));
await writeText(childConfig, codexServer("/child/node"));
const result = await read("codex");
if (!result.ok) throw new Error(result.error.detail);
expect(result.value.configPath).toBe(rootConfig);
expect(reaServer(result.value.document, "rea")).toMatchObject({
command: "/root/node",
});
await writeText(client.configPath, codexServer("/user/node"));
const untrusted = await read("codex");
if (!untrusted.ok) throw new Error(untrusted.error.detail);
expect(untrusted.value.configPath).toBe(client.configPath);
expect(reaServer(untrusted.value.document, "rea")).toMatchObject({
command: "/user/node",
});
const owned = await readEffectiveClientRegistrationConfiguration(
clientFor("codex"),
undefined,
process.platform,
{ projectDirectory, mode: "ownership" },
);
if (!owned.ok) throw new Error(owned.error.detail);
expect(owned.value.projectSources ?? []).toEqual([]);
});
it("reports a Codex projects table that is not a directory map", async () => {
const { clientFor, projectDirectory, read } = await layout();
await writeText(
join(projectDirectory, ".codex", "config.toml"),
codexServer("/project/node"),
);
await writeText(
clientFor("codex").configPath,
`projects = "nope"\n\n${codexServer("/user/node")}`,
);
const result = await read("codex");
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.detail).toContain("must be a [projects] table");
});
it("does not load a Codex ancestor outside the Git repository", async () => {
const { clientFor, repository, projectDirectory, read } = await layout(false);
const client = clientFor("codex");
await writeText(
client.configPath,
`${codexServer("/user/node")}\n${trust(repository)}\n${trust(projectDirectory)}`,
);
await writeText(
join(repository, ".codex", "config.toml"),
codexServer("/ancestor/node"),
);
await writeText(
join(projectDirectory, ".codex", "config.toml"),
codexServer("/local/node"),
);
const result = await read("codex");
if (!result.ok) throw new Error(result.error.detail);
expect(reaServer(result.value.document, "rea")).toMatchObject({
command: "/local/node",
});
});
it("lets a Kiro agent entry replace the workspace and user servers", async () => {
const { home, projectDirectory, clientFor, read } = await layout();
const user = join(home, ".kiro", "settings", "mcp.json");
const workspace = join(projectDirectory, ".kiro", "settings", "mcp.json");
const profileAgent = join(home, ".kiro", "agents", "review.json");
await writeJson(user, {
mcpServers: { rea: { command: "/user/node", args: ["mcp"] } },
});
await writeJson(workspace, {
mcpServers: { rea: { command: "/workspace/node", args: ["mcp"] } },
});
const workspaceOnly = await read("kiro");
if (!workspaceOnly.ok) throw new Error(workspaceOnly.error.detail);
expect(workspaceOnly.value.configPath).toBe(workspace);
expect(reaServer(workspaceOnly.value.document, "rea")).toMatchObject({
command: "/workspace/node",
});
await writeJson(profileAgent, {
mcpServers: {
rea: { command: "/agent/node", args: ["mcp"], disabled: true },
},
});
const agent = await read("kiro");
if (!agent.ok) throw new Error(agent.error.detail);
expect(agent.value.configPath).toBe(profileAgent);
expect(reaServer(agent.value.document, "rea")).toMatchObject({
command: "/agent/node",
disabled: true,
});
expect(uninstallConfigPaths(clientFor("kiro"))).toContain(profileAgent);
});
it("lets one Kiro workspace agent replace the workspace settings file", async () => {
const { home, projectDirectory, read } = await layout();
const workspace = join(projectDirectory, ".kiro", "settings", "mcp.json");
const workspaceAgent = join(
projectDirectory,
".kiro",
"agents",
"local.json",
);
await writeJson(join(home, ".kiro", "settings", "mcp.json"), {
mcpServers: { rea: { command: "/user/node", args: ["mcp"] } },
});
await writeJson(join(home, ".kiro", "agents", "review.json"), {
mcpServers: { other: { command: "/profile/node", args: ["mcp"] } },
});
await writeJson(workspace, {
mcpServers: { rea: { command: "/workspace/node", args: ["mcp"] } },
});
await writeJson(workspaceAgent, {
mcpServers: { rea: { command: "/workspace-agent/node", args: ["mcp"] } },
});
const result = await read("kiro");
if (!result.ok) throw new Error(result.error.detail);
expect(result.value.configPath).toBe(workspaceAgent);
expect(reaServer(result.value.document, "rea")).toMatchObject({
command: "/workspace-agent/node",
});
});
it("names every Kiro agent file that defines rea instead of choosing one", async () => {
const { home, projectDirectory, clientFor, read } = await layout();
const first = join(home, ".kiro", "agents", "alpha.json");
const second = join(projectDirectory, ".kiro", "agents", "beta.json");
await writeJson(first, {
mcpServers: { rea: { command: "/alpha/node", args: ["mcp"] } },
});
await writeJson(second, {
mcpServers: { rea: { command: "/beta/node", args: ["mcp"] } },
});
const result = await read("kiro");
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.detail).toContain(first);
expect(result.error.detail).toContain(second);
const owned = await readEffectiveClientRegistrationConfiguration(
clientFor("kiro"),
undefined,
process.platform,
{ projectDirectory, mode: "ownership" },
);
if (!owned.ok) throw new Error(owned.error.detail);
expect(
owned.value.projectSources?.map((source) => source.sourcePath),
).toContain(second);
});
it("checks the Kiro profile agent during setup and leaves both files unchanged when its launcher is unowned", async () => {
const { home, clientFor } = await layout();
const user = join(home, ".kiro", "settings", "mcp.json");
const agent = join(home, ".kiro", "agents", "review.json");
const userText = JSON.stringify({
mcpServers: { peer: { command: "keep" } },
});
await writeText(user, userText);
await writeJson(agent, {
mcpServers: { rea: { command: "/agent/node", args: ["mcp"] } },
});
const configured = await configureClientConfiguration(clientFor("kiro"), {}, [
"rea",
"mcp",
]);
expect(configured).toMatchObject({
status: "failed",
reason: "registration_ownership",
});
if (configured.status !== "failed" || !("detail" in configured)) return;
expect(configured.detail).toContain(agent);
expect(await readFile(user, "utf8")).toBe(userText);
expect(await readFile(agent, "utf8")).toContain("/agent/node");
});
it("does not choose a Kiro agent file when setup finds more than one", async () => {
const { home, clientFor } = await layout();
const first = join(home, ".kiro", "agents", "alpha.json");
const second = join(home, ".kiro", "agents", "beta.json");
await writeJson(first, {
mcpServers: { rea: { command: "/alpha/node", args: ["mcp"] } },
});
await writeJson(second, {
mcpServers: { rea: { command: "/beta/node", args: ["mcp"] } },
});
const configured = await configureClientConfiguration(clientFor("kiro"), {}, [
"rea",
"mcp",
]);
expect(configured).toMatchObject({
status: "failed",
reason: "invalid_configuration",
});
if (configured.status !== "failed" || !("detail" in configured)) return;
expect(configured.detail).toContain(first);
expect(configured.detail).toContain(second);
expect(await readFile(first, "utf8")).toContain("/alpha/node");
expect(await readFile(second, "utf8")).toContain("/beta/node");
});
it("reads Kiro agents from KIRO_HOME and reports an unreadable agents directory", async () => {
const { root, home, read, clientFor } = await layout();
const profile = join(root, "kiro-profile");
const agent = join(profile, "agents", "review.json");
await writeJson(join(home, ".kiro", "agents", "ignored.json"), {
mcpServers: { rea: { command: "/ignored/node", args: ["mcp"] } },
});
await writeJson(join(profile, "settings", "mcp.json"), {
mcpServers: { rea: { command: "/profile-user/node", args: ["mcp"] } },
});
await writeJson(agent, {
mcpServers: { rea: { command: "/profile-agent/node", args: ["mcp"] } },
});
const selected = await read(
"kiro",
join(root, "repository", "packages", "child"),
{ KIRO_HOME: profile },
);
if (!selected.ok) throw new Error(selected.error.detail);
expect(selected.value.configPath).toBe(agent);
await rm(join(profile, "agents"), { recursive: true });
await writeText(join(profile, "agents"), "not a directory");
const broken = clientFor("kiro", { KIRO_HOME: profile });
expect(broken.configPathError).toContain("could not be inspected");
});
it("replaces overridden Kiro settings in user reads and inspects the winning agent target", async () => {
const { home, clientFor } = await layout();
const user = join(home, ".kiro", "settings", "mcp.json");
const agent = join(home, ".kiro", "agents", "review.json");
await writeJson(user, {
mcpServers: {
rea: {
command: "/user/node",
args: ["mcp"],
disabled: true,
env: { ANTHROPIC_API_KEY: "bad key" },
},
},
});
await writeJson(agent, {
mcpServers: {
rea: { command: "npx", args: ["-y", "rea-agents@5.0.0", "mcp"] },
},
});
const userOnly = await readEffectiveClientRegistrationConfiguration(
clientFor("kiro"),
undefined,
process.platform,
);
if (!userOnly.ok) throw new Error(userOnly.error.detail);
expect(reaServer(userOnly.value.document, "rea")).toEqual({
command: "npx",
args: ["-y", "rea-agents@5.0.0", "mcp"],
});
const inspection = await inspectClientConfiguration(clientFor("kiro"), {}, [
"/durable/node",
"/durable/rea",
"mcp",
]);
expect(inspection).toMatchObject({
status: "update",
targetClient: expect.objectContaining({ configPath: agent }),
});
});
it("removes a bare Copilot project server map during project uninstall", async () => {
const { home, projectDirectory, clientFor } = await layout(false);
await writeJson(clientFor("copilot_cli").configPath, { mcpServers: {} });
const projectFile = join(projectDirectory, ".mcp.json");
await writeJson(projectFile, {
rea: copilotServer("npx", { args: ["-y", "rea-agents@5.0.0", "mcp"] }),
peer: copilotServer("keep"),
});
const host = systemUninstallHost(
home,
undefined,
{},
process.platform,
{},
projectDirectory,
);
const clients = await host.clients();
const copilotProject = clients.find(
(client) =>
client.name === "copilot_cli" && client.configPath === projectFile,
);
expect(copilotProject).toBeDefined();
if (copilotProject === undefined) return;
expect(await host.removeClient(copilotProject)).toMatchObject({
status: "removed",
});
const remaining: unknown = JSON.parse(await readFile(projectFile, "utf8"));
expect(remaining).toEqual({
peer: copilotServer("keep"),
});
});
it("ignores an unrelated Codex trusted project behind a non-directory parent", async () => {
const { root, clientFor, projectDirectory, read } = await layout();
const regularParent = join(root, "not-a-directory");
await writeText(regularParent, "file");
const client = clientFor("codex");
await writeText(
client.configPath,
`${codexServer("/user/node")}\n${trust(join(regularParent, "old-project"))}\n${trust(projectDirectory)}`,
);
await writeText(
join(projectDirectory, ".codex", "config.toml"),
codexServer("/project/node"),
);
const result = await read("codex");
if (!result.ok) throw new Error(result.error.detail);
expect(reaServer(result.value.document, "rea")).toMatchObject({
command: "/project/node",
});
});