Files
reverse-engineering-anything/tests/boundary/filesystem/mcpAddClientLifecycle.test.ts
T
morluto cb49383269 refactor(browser): take the host in Chromium, Electron and capture adapters (#11130)
* refactor(browser): take the host in Chromium, Electron and capture adapters

chromiumSandboxEnabled and safeElectronEnvironment take a required platform. The scenario browser, native module resolver, Electron provider and capture cleanup, mitmproxy adapter and web script artifacts take an optional platform seam (default HOST_PLATFORM); Electron fuses read the host architecture from HOST_PLATFORM. Their tests inject the platform instead of redefining process.platform. src/browser no longer reads process.platform or process.arch.

* refactor(pcap): take the host platform in the packet tools, native bound and batch launch (#11141)

* refactor(pcap): take the host platform in the packet tools, native bound and batch launch

* test(pcap): cover the selected host in batch launch and capture-host support

* refactor(apktool): take the host platform in launcher selection, version probe and decode (#11164)

* refactor(apktool): take the host platform in launcher selection, version probe and decode

* test(apktool): cover the provider's selected host in launcher selection

* refactor(hopper): give the launcher the provider's host platform

* refactor(native): inject the host into the pwntools decoders and native capability defaults (#11173)

* refactor(native): inject the host into the pwntools decoders and native capability defaults

* fix(native): give composed pwntools services the capability host and drop empty test hooks

* refactor(adb): take the host platform in binary selection and pulled file names (#11176)

* refactor(adb): take the host platform in binary selection and pulled file names

* refactor(cli): pass the resolved host platform through rea mcp add (#11179)

* refactor(cli): pass the resolved host platform through rea mcp add

* refactor(composition): default composition entry points to HOST_PLATFORM and pass it to Hopper and IDA (#11183)

* refactor(kuna): require the host platform in admission and installation, and give it to the session client (#11186)

* refactor(kuna): require the host platform in admission and installation, and give it to the session client

* fix(kuna): pass the host platform from the JavaScript installation callers

verify-real-kuna, both native oracle helpers and the command-lifetime child script call inspectKunaInstallation from dist, where TypeScript cannot see the now-required platform. They passed undefined and got unsupported_host on supported hosts.

* refactor(server): give createServer a host option and pass it to session tools, sigrok and EVM (#11203)

* refactor(server): give createServer a host option and pass it to session tools, sigrok and EVM

createServer read process.platform and process.arch live for its capability services, and registerSessionTools read process.platform for process capture. A host option (default HOST_PLATFORM) now selects both, and composition passes the capability host to the sigrok provider and the EVM provider (execution gate, limiter probe and environment rules), as it does for pwntools.

hostRestrictedAvailabilityMcp selects its host through the option instead of redefining process.platform and process.arch, and moves to the MCP boundary lane since it no longer touches process-global state. evmCleanupRetry passes SIMULATED_LINUX_X64 to the provider instead of 52 property redefinitions; the fixture moves to tests/fixtures/simulatedHost.ts.

* refactor(desktop): require the host platform in build computer and secret folder paths (#11210)

* refactor(desktop): require the host platform in build computer and secret folder paths

buildComputerArchiveDirectory, defaultBuildComputerArchive, resolveBuildComputerEnvironment and buildSecretsDirectory defaulted to process.platform. Every caller but DesktopBuildComputers' secret folder already passed the platform; it now passes HOST_PLATFORM.platform, which it reads for the build environment.

* refactor(filesystem): default caller paths and ChangeTime settling to HOST_PLATFORM (#11212)

appendCallerPath, absoluteCallerPath and the ChangeTime settle seams defaulted to a live process.platform read. Their defaults now come from HOST_PLATFORM: the 59 callers that omit the platform join or settle paths they open on this host, and callers describing another host's layout already pass it.

The webScriptExport case that redefined process.platform to reach the Windows settle budget moves to its own file, which selects Windows by mocking HostPlatform.

* fix(server): report process capture availability for the server's host

sessionAvailabilityPolicy imported platform from node:process, so with a selected host binary_session reported capture_process_scenario from the running host while planning and capture used the selected one. It takes the platform from createServer and registerSessionTools. hostRestrictedAvailabilityMcp checks discovery against a dry-run plan on win32 and linux hosts.

* build(guards): count platform and arch imports from node:process and node:os as ambient host reads

sessionAvailabilityPolicy's named import of platform from node:process escaped the ambient-host-platform guard. The guards now also match named platform/arch imports from node:process or node:os and os.platform()/os.arch(). The one other site, DesktopApplicationCapture's os.arch(), records the capturing host and now reads HOST_PLATFORM.architecture.

* refactor(config): take the host platform in parseConfig and give debugger managers the capability host (#11237)

* refactor(config): take the host platform in parseConfig and give debugger managers the capability host

parseConfig read process.platform for the default Hopper launcher; it takes the platform (default HOST_PLATFORM). GdbSessionManager and RizinDebugSessionManager default their platform to HOST_PLATFORM, and composition passes the capability host that already decides their availability. GDB's /proc descriptor check reads HOST_PLATFORM explicitly: it inspects this host's /proc whatever host the manager models.

* refactor(firmware): give the firmware and Wakaru providers the capability host (#11239)

* refactor(firmware): give the firmware and Wakaru providers the capability host

resolveFirmwareCommand and resolveWakaruCommand read process.platform (and process.arch) for their host gate. They take the platform or host as a parameter; FirmwareProvider and WakaruProvider take a host (default HOST_PLATFORM) for the gate and their environment snapshots, and composition passes the capability host that already decides their availability. The JavaScript analysis source transfer defaults to HOST_PLATFORM.

artifactSelectedCommand passes SIMULATED_LINUX_X64 to both providers instead of redefining process.platform and process.arch, and the Wakaru limiter configuration cases now run on any POSIX host.

* refactor(captures): give tshark the capability host and default Cutter to HOST_PLATFORM (#11240)

* refactor(captures): give tshark the capability host and default Cutter to HOST_PLATFORM

TsharkCaptureProvider and CutterBridgeClient defaulted their platform to process.platform. Both default to HOST_PLATFORM now, and composition passes the capability host to the packet capture service, which already decides its availability.

* refactor(platform): read HOST_PLATFORM explicitly in host-only path checks (#11241)

* refactor(platform): read HOST_PLATFORM explicitly in host-only path checks

Electron's readlink target split, IDA's host path comparison, the inspector's reported runtime path, the server identity check and WABT's executable names all describe paths on the running host. They read HOST_PLATFORM instead of process.platform.

* test(native): select the native UI verifier's host through HostPlatform (#11242)

verify-native-ui.mjs reads its macOS requirement from HOST_PLATFORM, and its action test resolves the script's HostPlatform import to a simulated darwin host instead of redefining process.platform in the child preload. Simulating linux there fails the macOS requirement, so the redirect is effective.

* test(mcp): check that composition forwards the selected host to execution

hostRestrictedAvailabilityMcp ran execution only on darwin, where the runner's own host also refuses. The new case calls the firmware, Wakaru, tshark, sigrok and EVM tools under a darwin and a linux x64 host and requires different outcomes, so dropping a provider's host forwarding fails on any runner (checked by removing the firmware forwarding).

* fix(composition): give the pcap, Apktool and ADB providers the capability host

createServer's selected host decided pcap capture discovery, but the default pcap, Apktool and ADB factories built their providers with HOST_PLATFORM. The factories take the platform (default HOST_PLATFORM) and capabilityServices passes its context platform, so availability and execution follow one host.

* test: import moved resources owners in host-platform fixtures

* refactor(config): drop the unused host parameter from parseConfig
2026-11-16 04:27:07 +08:00

1008 lines
37 KiB
TypeScript

import { registerMcpClientsOnHost } from "../../fixtures/mcpRegistration.js";
import { HOST_PLATFORM } from "../../../src/platform/HostPlatform.js";
import {
chmod,
copyFile,
lstat,
mkdir,
readFile,
readdir,
realpath,
rename,
rm,
stat,
symlink,
writeFile,
} from "node:fs/promises";
import { constants as fsConstants } from "node:fs";
import { dirname, join } from "node:path";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import {
clientConfigurationRecordSchema,
parseClientConfiguration,
} from "../../../src/application/ClientConfigurationDocument.js";
import { parseGooseConfigurationYaml } from "../../../src/application/GooseConfigurationYaml.js";
import { currentClientRegistrationCommand } from "../../../src/application/ClientRegistrationIdentity.js";
import {
readClientRegistrationInspections,
readClientRegistrationStatuses,
} from "../../../src/application/ClientRegistrationStatus.js";
import { resolveMcpRegistrationClients } from "../../../src/application/McpRegistrationCatalog.js";
import {
clientConfigurationAligned,
configureClientConfiguration,
inspectClientConfiguration,
} from "../../../src/application/SetupClientConfiguration.js";
import { supportedClients } from "../../../src/application/SupportedClients.js";
import { detectClients } from "../../../src/application/SetupHost.js";
import {
runUninstall,
systemUninstallHost,
type UninstallFileSystem,
} from "../../../src/application/Uninstall.js";
import { ClientConfigurationPublication } from "../../../src/application/ClientConfigurationPublication.js";
import { existingMaintenanceScope } from "../../../src/application/UpdateMaintenance.js";
import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceScope.js";
import { OwnedFileHandle } from "../../../src/filesystem/OwnedFileHandle.js";
import { clearClientLocationEnvironment } from "../../fixtures/clientEnvironment.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const platform = process.platform;
const command = currentClientRegistrationCommand({ platform });
beforeEach(clearClientLocationEnvironment);
afterEach(() => vi.unstubAllEnvs());
// Each client REA registers through `rea mcp add` is one client-table row, so
// doctor, update and uninstall see what mcp add wrote (#7181).
it.each([
{ agent: "cline", client: "cline_vscode" },
{ agent: "kiro-cli", client: "kiro" },
{ agent: "mastracode", client: "mastracode" },
{ agent: "zed", client: "zed" },
{ agent: "amp", client: "amp" },
{ agent: "mcporter", client: "mcporter" },
{ agent: "fx", client: "fx" },
{ agent: "junie", client: "junie" },
{ agent: "goose", client: "goose" },
{ agent: "kilo", client: "kilo" },
])(
"inspects, refreshes and removes the $agent registration that mcp add wrote as $client",
async ({ agent, client }) => {
const home = await createTestTempDirectory(`rea-mcp-add-${client}-`);
const environment = {};
// Goose's system layer stays inside the temporary home.
const systemPaths = { gooseSystemConfig: join(home, "goose-system.yaml") };
const resources = new ArtifactResourceScope();
const selected = await resolveMcpRegistrationClients(
{
agents: [agent],
global: true,
cwd: home,
home,
platform,
environment,
systemPaths,
},
resources,
);
if (!selected.ok) throw selected.error;
const row = supportedClients(home, platform, environment, systemPaths).find(
({ name }) => name === client,
);
if (row === undefined) throw new Error(`${client} has no table row`);
expect(selected.value.map(({ configPath }) => configPath)).toEqual([
row.configPath,
]);
// An older, still REA-owned launcher: the package runner form that
// current setup migrates to the selected installation.
const legacy = { command: "npx", args: ["-y", "rea-agents@1.0.0", "mcp"] };
const [outcome] = await registerMcpClientsOnHost(
selected.value,
legacy,
undefined,
resources,
);
expect(outcome).toMatchObject({
status: "completed",
result: { ok: true, value: { status: "configured" } },
});
const status = async () =>
(
await readClientRegistrationStatuses(home, command[1], {
platform,
environment,
systemPaths,
})
).find((entry) => entry.client === client);
// Doctor reads the registration from the same row.
expect(await status()).toMatchObject({
config_path: row.configPath,
command: [legacy.command, ...legacy.args],
command_identity: "owned",
state: "stale",
});
// Update selects it, and setup refreshes it to the current launcher.
await expect(
existingMaintenanceScope(home, command[1] ?? "", environment, {
platform: HOST_PLATFORM.platform,
systemPaths,
}),
).resolves.toMatchObject({ clients: [client] });
await expect(
inspectClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "update" });
await expect(
configureClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "configured" });
expect(await status()).toMatchObject({
command: [...command],
command_identity: "owned",
});
// Uninstall removes only REA's row and keeps the file.
const uninstall = await runUninstall(
false,
systemUninstallHost(home, undefined, environment, platform, systemPaths),
);
expect(uninstall.items).toContainEqual(
expect.objectContaining({ name: client, status: "removed" }),
);
const remaining = parseClientConfiguration(
await readFile(row.configPath, "utf8"),
row.format,
);
expect(remaining.servers).not.toHaveProperty("rea");
if (row.privateProfile !== undefined && platform !== "win32")
expect((await stat(row.configPath)).mode & 0o777).toBe(0o600);
},
);
it("registers Amp in settings.jsonc when that is Amp's only settings file", async () => {
const home = await createTestTempDirectory("rea-amp-jsonc-");
const directory = join(home, ".config", "amp");
const jsonc = join(directory, "settings.jsonc");
await mkdir(directory, { recursive: true });
await writeFile(jsonc, '{\n // keep\n "amp.theme": "dark"\n}\n');
const resources = new ArtifactResourceScope();
const selected = await resolveMcpRegistrationClients(
{
agents: ["amp"],
global: true,
cwd: home,
home,
platform,
environment: {},
},
resources,
);
if (!selected.ok) throw selected.error;
expect(selected.value[0]?.configPath).toBe(jsonc);
await registerMcpClientsOnHost(
selected.value,
{ command: command[0] ?? "", args: command.slice(1) },
undefined,
resources,
);
const text = await readFile(jsonc, "utf8");
expect(text).toContain("// keep");
expect(parseClientConfiguration(text, "amp").servers).toHaveProperty("rea");
// With both files present, which one Amp reads is unknown.
await writeFile(join(directory, "settings.json"), "{}\n");
const row = supportedClients(home, platform, {}).find(
({ name }) => name === "amp",
);
expect(row?.configPathError).toContain("Both");
const ambiguous = await resolveMcpRegistrationClients(
{
agents: ["amp"],
global: true,
cwd: home,
home,
platform,
environment: {},
},
resources,
);
expect(ambiguous.ok).toBe(false);
});
it.each([
{ client: "cline_vscode_insiders", folder: "Code - Insiders" },
{ client: "cline_vscodium", folder: "VSCodium" },
{ client: "cline_cursor", folder: "Cursor" },
{ client: "cline_windsurf", folder: "Windsurf" },
])(
"manages Cline's registration in $folder's global storage",
async ({ client, folder }) => {
const home = await createTestTempDirectory(`rea-${client}-`);
const row = supportedClients(home, "linux", {}).find(
({ name }) => name === client,
);
if (row === undefined) throw new Error(`${client} has no table row`);
const settings = join(
home,
".config",
folder,
"User",
"globalStorage",
"saoudrizwan.claude-dev",
"settings",
);
expect(row.configPath).toBe(join(settings, "cline_mcp_settings.json"));
// VSCODE_PORTABLE names VS Code's own portable data, not this editor's.
expect(
supportedClients(home, "linux", {
VSCODE_PORTABLE: join(home, "portable"),
}).find(({ name }) => name === client)?.configPath,
).toBe(row.configPath);
expect(
supportedClients(home, "linux", {
VSCODE_PORTABLE: join(home, "portable"),
}).find(({ name }) => name === "cline_vscode")?.configPath,
).toBe(
join(
home,
"portable",
"user-data",
"User",
"globalStorage",
"saoudrizwan.claude-dev",
"settings",
"cline_mcp_settings.json",
),
);
// Detected only where that editor's Cline extension has run.
expect(
(await detectClients(home, "linux", {})).map(({ name }) => name),
).not.toContain(client);
await mkdir(settings, { recursive: true });
expect(
(await detectClients(home, "linux", {})).map(({ name }) => name),
).toContain(client);
await expect(
configureClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "configured" });
const status = (
await readClientRegistrationStatuses(home, command[1], {
platform: "linux",
environment: {},
})
).find(({ client: name }) => name === client);
expect(status).toMatchObject({ command_identity: "owned" });
const uninstall = await runUninstall(
false,
systemUninstallHost(home, undefined, {}, "linux"),
);
expect(uninstall.items).toContainEqual(
expect.objectContaining({ name: client, status: "removed" }),
);
},
);
it("selects the Zed configuration a Linux Flatpak Zed reads", async () => {
const home = await createTestTempDirectory("rea-zed-flatpak-");
const zed = (env: NodeJS.ProcessEnv = {}) =>
supportedClients(home, "linux", env).find(({ name }) => name === "zed");
const native = join(home, ".config", "zed");
const sandboxed = join(home, ".var", "app", "dev.zed.Zed", "config", "zed");
expect(zed()?.configPath).toBe(join(native, "settings.json"));
await mkdir(sandboxed, { recursive: true });
expect(zed()?.configPath).toBe(join(sandboxed, "settings.json"));
await mkdir(native, { recursive: true });
expect(zed()?.configPathError).toContain("cannot tell which Zed");
// A native folder linked to the Flatpak folder is one configuration.
await rm(native, { recursive: true });
await symlink(sandboxed, native, "dir");
expect(zed()?.configPath).toBe(join(native, "settings.json"));
await rm(native);
await mkdir(native, { recursive: true });
// An XDG_CONFIG_HOME naming the Flatpak folder is one folder, not two.
expect(
zed({ XDG_CONFIG_HOME: join(home, ".var", "app", "dev.zed.Zed", "config") })
?.configPath,
).toBe(join(sandboxed, "settings.json"));
expect(
zed({ FLATPAK_XDG_CONFIG_HOME: join(home, "selected") })?.configPath,
).toBe(join(home, "selected", "zed", "settings.json"));
expect(
zed({ FLATPAK_XDG_CONFIG_HOME: "relative" })?.configPathError,
).toContain("FLATPAK_XDG_CONFIG_HOME is relative");
});
it("keeps MCPorter out of setup detection and doctor until REA registers it", async () => {
const home = await createTestTempDirectory("rea-mcporter-applicability-");
const environment = {};
const row = supportedClients(home, platform, environment).find(
({ name }) => name === "mcporter",
);
if (row === undefined) throw new Error("mcporter has no table row");
await mkdir(join(home, ".mcporter"), { recursive: true });
await writeFile(
row.configPath,
`${JSON.stringify({ mcpServers: { peer: { command: "peer" } } })}\n`,
);
expect(
(await detectClients(home, platform, environment)).map(({ name }) => name),
).not.toContain("mcporter");
const statuses = await readClientRegistrationStatuses(home, command[1], {
platform,
environment,
});
expect(statuses.map(({ client }) => client)).not.toContain("mcporter");
// An explicit selection still reports the missing registration.
const selected = await readClientRegistrationStatuses(home, command[1], {
platform,
environment,
selectedClientIds: ["mcporter"],
});
expect(selected.find(({ client }) => client === "mcporter")).toMatchObject({
state: "missing",
});
});
it("stops uninstall with MCPorter's recovery while its location is unknown", async () => {
const home = await createTestTempDirectory("rea-mcporter-unknown-uninstall-");
const environment = { MCPORTER_CONFIG: "config/mcporter.json" };
const claude = join(home, ".claude.json");
const original = `${JSON.stringify({ mcpServers: { rea: { command: command[0], args: command.slice(1) } } })}\n`;
await writeFile(claude, original);
const uninstall = await runUninstall(
false,
systemUninstallHost(home, undefined, environment, platform),
);
// REA cannot tell whether MCPorter's file holds its row, so nothing is
// removed and the result names MCPorter's own recovery.
expect(uninstall.items).toContainEqual(
expect.objectContaining({
name: "mcporter",
status: "failed",
detail: expect.stringContaining("MCPORTER_CONFIG is relative"),
}),
);
expect(await readFile(claude, "utf8")).toBe(original);
});
it.skipIf(platform === "win32")(
"setup publishes fx's profile privately and repairs its modes",
async () => {
const home = await createTestTempDirectory("rea-fx-private-");
const row = supportedClients(home, platform, {}).find(
({ name }) => name === "fx",
);
if (row === undefined) throw new Error("fx has no table row");
const modes = async () => ({
directory: (await stat(dirname(row.configPath))).mode & 0o777,
file: (await stat(row.configPath)).mode & 0o777,
});
await expect(
configureClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "configured" });
expect(await modes()).toEqual({ directory: 0o700, file: 0o600 });
await expect(inspectClientConfiguration(row, {}, command)).resolves.toEqual(
{ status: "already_current" },
);
// A current row in a readable profile still needs its modes restored.
await chmod(row.configPath, 0o644);
await chmod(dirname(row.configPath), 0o755);
await expect(
inspectClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "update" });
// Re-selection after a Hopper install uses the same modes rule.
await expect(clientConfigurationAligned(row, {}, command)).resolves.toBe(
false,
);
await expect(
configureClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "configured" });
expect(await modes()).toEqual({ directory: 0o700, file: 0o600 });
await expect(clientConfigurationAligned(row, {}, command)).resolves.toBe(
true,
);
// A linked profile is judged by the folder and file it names.
const target = join(home, "fx-profile");
await rename(dirname(row.configPath), target);
await symlink(target, dirname(row.configPath));
await expect(inspectClientConfiguration(row, {}, command)).resolves.toEqual(
{ status: "already_current" },
);
},
);
it.skipIf(platform === "win32")(
"setup surfaces the private folder handle it could not close",
async () => {
const home = await createTestTempDirectory("rea-fx-private-close-");
const row = supportedClients(home, platform, {}).find(
({ name }) => name === "fx",
);
if (row === undefined) throw new Error("fx has no table row");
const originalClose = OwnedFileHandle.prototype.close;
const close = vi
.spyOn(OwnedFileHandle.prototype, "close")
.mockImplementation(async function (this: OwnedFileHandle) {
if ((await this.handle.stat()).isDirectory())
throw new Error("fixture private directory close denied");
return originalClose.call(this);
});
try {
// The failed close keeps its owner on the error instead of becoming a
// write failure that drops it.
await expect(
configureClientConfiguration(row, {}, command),
).rejects.toMatchObject({ cleanupIncomplete: true });
} finally {
close.mockRestore();
}
},
);
it.skipIf(platform === "win32")(
"keeps fx's backups owner-only when its profile file is a link",
async () => {
const home = await createTestTempDirectory("rea-fx-private-backup-");
const row = supportedClients(home, platform, {}).find(
({ name }) => name === "fx",
);
if (row === undefined) throw new Error("fx has no table row");
// ~/.fx/mcp.json links to a readable file inside a private folder, so the
// backups beside the link are not protected by the folder setup secures.
const target = join(home, "private", "mcp.json");
await mkdir(dirname(target), { recursive: true, mode: 0o700 });
await writeFile(target, `${JSON.stringify({ mcpServers: {} })}\n`, {
mode: 0o644,
});
await mkdir(dirname(row.configPath), { recursive: true });
await chmod(dirname(row.configPath), 0o755);
await symlink(target, row.configPath);
await expect(
configureClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "configured" });
const backupMode = async () =>
(await stat(`${row.configPath}.rea.backup`)).mode & 0o777;
expect(await backupMode()).toBe(0o600);
const uninstall = await runUninstall(
false,
systemUninstallHost(home, undefined, {}, platform),
);
expect(uninstall.items).toContainEqual(
expect.objectContaining({ name: "fx", status: "removed" }),
);
// Uninstall's backup of the registered file is owner-only too.
const backups = (await readdir(dirname(row.configPath))).filter((name) =>
name.startsWith("mcp.json.rea"),
);
expect(backups.length).toBeGreaterThan(1);
for (const name of backups)
expect(
(await stat(join(dirname(row.configPath), name))).mode & 0o777,
).toBe(0o600);
},
);
it("reports a Junie registration Junie would not load in doctor and setup", async () => {
const home = await createTestTempDirectory("rea-junie-policy-");
const environment = { JUNIE_MCP_DEFAULT_LOCATIONS: "false" };
const row = supportedClients(home, platform, environment).find(
({ name }) => name === "junie",
);
if (row === undefined) throw new Error("junie has no table row");
await mkdir(dirname(row.configPath), { recursive: true });
await writeFile(
row.configPath,
`${JSON.stringify({ mcpServers: { rea: { command: command[0], args: command.slice(1) } } })}\n`,
);
const status = (
await readClientRegistrationStatuses(home, command[1], {
platform,
environment,
})
).find(({ client }) => client === "junie");
expect(status).toMatchObject({
state: "stale",
remediation: expect.stringContaining("JUNIE_MCP_DEFAULT_LOCATIONS"),
});
await expect(
inspectClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "invalid" });
// Setup would refuse Junie, so update reports it instead of failing the
// other clients' maintenance plan.
const scope = await existingMaintenanceScope(
home,
command[1] ?? "",
environment,
{ platform: HOST_PLATFORM.platform },
);
expect(scope.clients).not.toContain("junie");
expect(scope.unresolved).toEqual([
expect.objectContaining({
client: "junie",
remediation: expect.stringContaining("JUNIE_MCP_DEFAULT_LOCATIONS"),
}),
]);
});
it.skipIf(platform === "win32")(
"reports Goose's effective registration across its read-only layers",
async () => {
const home = await createTestTempDirectory("rea-goose-layers-");
const additional = join(home, "team", "goose.yaml");
await mkdir(dirname(additional), { recursive: true });
const lower = "extensions:\n rea:\n enabled: false\n";
await writeFile(additional, lower);
const environment = { GOOSE_ADDITIONAL_CONFIG_FILES: additional };
// The system layer is injected so the host's /etc/goose is never read.
const systemPaths = { gooseSystemConfig: join(home, "system.yaml") };
const resources = new ArtifactResourceScope();
const selected = await resolveMcpRegistrationClients(
{
agents: ["goose"],
global: true,
cwd: home,
home,
platform,
environment,
systemPaths,
},
resources,
);
if (!selected.ok) throw selected.error;
const row = supportedClients(home, platform, environment, systemPaths).find(
({ name }) => name === "goose",
);
expect(row?.configPaths).toEqual([
systemPaths.gooseSystemConfig,
additional,
row?.configPath,
]);
expect(selected.value[0]).toMatchObject({
configPaths: row?.configPaths,
});
const [outcome] = await registerMcpClientsOnHost(
selected.value,
{ command: command[0] ?? "", args: command.slice(1) },
undefined,
resources,
);
expect(outcome).toMatchObject({
status: "completed",
result: { ok: true },
});
// The user file does not set `enabled`, so the lower layer's false holds.
const inspections = await readClientRegistrationInspections(
home,
command[1],
{ platform, environment, systemPaths },
);
expect(
inspections.find(({ status }) => status.client === "goose"),
).toMatchObject({ activation: "disabled", status: { state: "stale" } });
const uninstall = await runUninstall(
false,
systemUninstallHost(home, undefined, environment, platform, systemPaths),
);
expect(uninstall.items).toContainEqual(
expect.objectContaining({ name: "goose", status: "removed" }),
);
// Uninstall changes only the user file Goose writes.
expect(await readFile(additional, "utf8")).toBe(lower);
},
);
it("orders Goose's layers as Goose loads them", () => {
const home = "/fixture/home";
const user = "/fixture/home/.config/goose/config.yaml";
const unix = supportedClients(home, "linux", {
GOOSE_ADDITIONAL_CONFIG_FILES: `${user}:/fixture/team.yaml`,
}).find(({ name }) => name === "goose");
// Goose loads the user file again last, so that load decides precedence.
expect(unix?.configPaths).toEqual([
"/etc/goose/config.yaml",
"/fixture/team.yaml",
user,
]);
const windows = supportedClients("C:\\Users\\fixture", "win32", {
APPDATA: "C:\\Users\\fixture\\AppData\\Roaming",
PROGRAMDATA: "C:\\ProgramData",
GOOSE_ADDITIONAL_CONFIG_FILES:
'"C:\\Team Config\\a;b.yaml";C:\\Team\\goose.yaml',
}).find(({ name }) => name === "goose");
expect(windows?.configPathError).toBeUndefined();
expect(windows?.configPaths?.slice(1, 3)).toEqual([
"C:\\Team Config\\a;b.yaml",
"C:\\Team\\goose.yaml",
]);
// A drive-less Windows entry depends on Goose's current drive.
const driveless = supportedClients("C:\\Users\\fixture", "win32", {
APPDATA: "C:\\Users\\fixture\\AppData\\Roaming",
GOOSE_ADDITIONAL_CONFIG_FILES: "\\team\\goose.yaml",
}).find(({ name }) => name === "goose");
expect(driveless?.configPathError).toContain(
"\\team\\goose.yaml, which has no drive",
);
});
it.skipIf(platform === "win32")(
"refreshes Goose's environment through setup without losing inherited or aliased settings",
async () => {
const { team, row } = await gooseFixture("rea-goose-environment-");
await mkdir(dirname(row.configPath), { recursive: true });
const userEntry = async () =>
(
clientConfigurationRecordSchema.parse(
parseGooseConfigurationYaml(await readFile(row.configPath, "utf8")),
).extensions as Record<string, Record<string, unknown>>
).rea;
const launcher = ` type: stdio\n cmd: ${JSON.stringify(command[0])}\n args: ${JSON.stringify(command.slice(1))}\n`;
// A lower map stays in effect when the user entry declares none.
await writeFile(
team,
'extensions:\n rea:\n envs:\n TEAM_SETTING: "1"\n',
);
await writeFile(row.configPath, `extensions:\n rea:\n${launcher}`);
await expect(
configureClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "configured" });
expect(await userEntry()).not.toHaveProperty("envs");
// The env alias is read and written back as envs.
await writeFile(team, "{}\n");
await writeFile(
row.configPath,
`extensions:\n rea:\n${launcher} env:\n KEEP: "1"\n`,
);
await expect(
configureClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "configured" });
expect(await userEntry()).toMatchObject({ envs: { KEEP: "1" } });
expect(await userEntry()).not.toHaveProperty("env");
// A requested setting replaces the recorded value.
await expect(
configureClientConfiguration(
row,
{ REA_ANALYSIS_PROVIDER: "ghidra" },
command,
),
).resolves.toMatchObject({ status: "configured" });
expect(await userEntry()).toMatchObject({
envs: { KEEP: "1", REA_ANALYSIS_PROVIDER: "ghidra" },
});
// A requested setting over an inherited map keeps the inherited values.
await writeFile(
team,
'extensions:\n rea:\n envs:\n TEAM_SETTING: "1"\n',
);
await writeFile(row.configPath, `extensions:\n rea:\n${launcher}`);
await expect(
configureClientConfiguration(
row,
{ REA_ANALYSIS_PROVIDER: "ghidra" },
command,
),
).resolves.toMatchObject({ status: "configured" });
expect((await userEntry())?.envs).toEqual({
TEAM_SETTING: "1",
REA_ANALYSIS_PROVIDER: "ghidra",
});
// An inherited map under the env alias is replaced under that spelling,
// so the merged entry never declares both env and envs.
await writeFile(team, 'extensions:\n rea:\n env:\n KEEP: "1"\n');
await writeFile(row.configPath, `extensions:\n rea:\n${launcher}`);
await expect(
configureClientConfiguration(
row,
{ REA_ANALYSIS_PROVIDER: "ghidra" },
command,
),
).resolves.toMatchObject({ status: "configured" });
expect(await userEntry()).toMatchObject({
env: { KEEP: "1", REA_ANALYSIS_PROVIDER: "ghidra" },
});
expect(await userEntry()).not.toHaveProperty("envs");
await expect(
inspectClientConfiguration(
row,
{ REA_ANALYSIS_PROVIDER: "ghidra" },
command,
),
).resolves.toEqual({ status: "already_current" });
},
);
it.skipIf(platform === "win32")(
"mcp add leaves an inherited Goose env alias in force",
async () => {
const { home, team, environment, systemPaths } = await gooseFixture(
"rea-goose-add-alias-",
);
await writeFile(team, 'extensions:\n rea:\n env:\n KEEP: "1"\n');
const resources = new ArtifactResourceScope();
const selected = await resolveMcpRegistrationClients(
{
agents: ["goose"],
global: true,
cwd: home,
home,
platform,
environment,
systemPaths,
},
resources,
);
if (!selected.ok) throw selected.error;
const [outcome] = await registerMcpClientsOnHost(
selected.value,
{ command: command[0] ?? "", args: command.slice(1) },
undefined,
resources,
);
expect(outcome).toMatchObject({
status: "completed",
result: { ok: true },
});
// Writing envs: {} beside the inherited alias would make the merged entry
// ambiguous; the registration reads back as aligned instead.
const inspections = await readClientRegistrationInspections(
home,
command[1],
{ platform, environment, systemPaths },
);
expect(
inspections.find(({ status }) => status.client === "goose"),
).toMatchObject({ activation: "enabled", status: { state: "aligned" } });
},
);
it.skipIf(platform === "win32")(
"keeps Goose's user registration while a read-only layer would stay enabled",
async () => {
const { home, team, row, environment, systemPaths } = await gooseFixture(
"rea-goose-uninstall-",
);
await writeFile(
team,
`extensions:\n rea:\n type: stdio\n cmd: ${JSON.stringify(command[0])}\n args: ${JSON.stringify(command.slice(1))}\n`,
);
await expect(
configureClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "configured" });
const before = await readFile(row.configPath, "utf8");
const uninstall = await runUninstall(
false,
systemUninstallHost(home, undefined, environment, platform, systemPaths),
);
expect(uninstall.items).toContainEqual(
expect.objectContaining({ name: "goose", status: "failed" }),
);
expect(await readFile(row.configPath, "utf8")).toBe(before);
},
);
it.skipIf(platform === "win32")(
"reads an administrator's system layer without requiring the caller to own it",
async () => {
const { home, row, environment, systemPaths } = await gooseFixture(
"rea-goose-system-owner-",
);
const system = systemPaths.gooseSystemConfig;
await writeFile(
system,
`extensions:\n rea:\n type: stdio\n cmd: ${JSON.stringify(command[0])}\n args: ${JSON.stringify(command.slice(1))}\n enabled: false\n`,
);
await expect(
configureClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "configured" });
// The system layer belongs to another user; only REA's write target must
// be the caller's.
const fileSystem: UninstallFileSystem = {
readText: (path) => readFile(path, "utf8"),
copy: (source, destination) =>
copyFile(source, destination, fsConstants.COPYFILE_EXCL),
publishConfiguration: (path, contents, original) =>
ClientConfigurationPublication.write(path, contents, original),
stat: async (path) => {
const stats = await lstat(path);
if (path === system) stats.uid += 1;
return stats;
},
realpath: (path) => realpath(path),
remove: (path) => rm(path, { recursive: true }),
};
const uninstall = await runUninstall(
false,
systemUninstallHost(home, fileSystem, environment, platform, systemPaths),
);
expect(uninstall.items).toContainEqual(
expect.objectContaining({ name: "goose", status: "removed" }),
);
},
);
it.skipIf(platform === "win32")(
"keeps a timeout and activation only a lower Goose layer declares",
async () => {
const { team, row } = await gooseFixture("rea-goose-inherited-policy-");
await writeFile(
team,
"extensions:\n rea:\n timeout: 900\n enabled: false\n",
);
await mkdir(dirname(row.configPath), { recursive: true });
await writeFile(
row.configPath,
`extensions:\n rea:\n type: stdio\n cmd: ${JSON.stringify(command[0])}\n args: ${JSON.stringify(command.slice(1))}\n`,
);
await expect(
configureClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "configured" });
const entry = (
clientConfigurationRecordSchema.parse(
parseGooseConfigurationYaml(await readFile(row.configPath, "utf8")),
).extensions as Record<string, Record<string, unknown>>
).rea;
expect(entry).not.toHaveProperty("timeout");
expect(entry).not.toHaveProperty("enabled");
// Setup is idempotent over the inherited settings.
await expect(inspectClientConfiguration(row, {}, command)).resolves.toEqual(
{ status: "already_current" },
);
},
);
it.skipIf(platform === "win32")(
"ignores an invalid environment map a higher Goose layer replaces",
async () => {
const { home, team, row, environment, systemPaths } = await gooseFixture(
"rea-goose-shadowed-environment-",
);
// Goose replaces envs per layer, so the lower invalid value never runs.
await writeFile(
team,
"extensions:\n rea:\n envs:\n REA_LOG_LEVEL: verbose\n",
);
await mkdir(dirname(row.configPath), { recursive: true });
await writeFile(
row.configPath,
`extensions:\n rea:\n type: stdio\n cmd: ${JSON.stringify(command[0])}\n args: ${JSON.stringify(command.slice(1))}\n envs:\n REA_LOG_LEVEL: info\n`,
);
const inspections = await readClientRegistrationInspections(
home,
command[1],
{ platform, environment, systemPaths },
);
expect(
inspections.find(({ status }) => status.client === "goose"),
).toMatchObject({ activation: "enabled", status: { state: "aligned" } });
},
);
it.skipIf(platform === "win32")(
"reports REA's registration that only a read-only Goose layer defines",
async () => {
const { home, team, row, environment, systemPaths } = await gooseFixture(
"rea-goose-lower-only-",
);
await writeFile(
team,
`extensions:\n rea:\n type: stdio\n cmd: ${JSON.stringify(command[0])}\n args: ${JSON.stringify(command.slice(1))}\n`,
);
const uninstall = () =>
runUninstall(
false,
systemUninstallHost(
home,
undefined,
environment,
platform,
systemPaths,
),
);
// Without a user file, and with one that has no REA entry, Goose still
// loads the lower registration, so uninstall is not complete.
const retained = expect.objectContaining({
name: "goose",
status: "failed",
detail: expect.stringContaining("read-only configuration layer"),
});
await expect(uninstall()).resolves.toMatchObject({
status: "failed",
items: expect.arrayContaining([retained]),
});
await mkdir(dirname(row.configPath), { recursive: true });
await writeFile(row.configPath, "extensions: {}\n");
await expect(uninstall()).resolves.toMatchObject({
status: "failed",
items: expect.arrayContaining([retained]),
});
// A partial user entry inherits REA's launcher from the lower layer.
await writeFile(
row.configPath,
"extensions:\n rea:\n envs:\n KEEP: '1'\n",
);
await expect(uninstall()).resolves.toMatchObject({
status: "failed",
items: expect.arrayContaining([retained]),
});
expect(await readFile(team, "utf8")).toContain("rea:");
// A disabled lower entry is not loaded, so nothing is retained.
await writeFile(row.configPath, "extensions: {}\n");
await writeFile(team, "extensions:\n rea:\n enabled: false\n");
await expect(uninstall()).resolves.toMatchObject({ status: "complete" });
},
);
it.skipIf(platform === "win32")(
"stops uninstall before any removal when a lower Goose layer is unreadable",
async () => {
const { home, team, environment, systemPaths } = await gooseFixture(
"rea-goose-lower-unreadable-",
);
await writeFile(team, "extensions: [\n");
const codex = join(home, ".codex", "config.toml");
await mkdir(dirname(codex), { recursive: true });
const registration = `[mcp_servers.rea]\ncommand = ${JSON.stringify(command[0])}\nargs = ${JSON.stringify(command.slice(1))}\n`;
await writeFile(codex, registration);
const uninstall = await runUninstall(
false,
systemUninstallHost(home, undefined, environment, platform, systemPaths),
);
expect(uninstall).toMatchObject({
status: "failed",
items: expect.arrayContaining([
expect.objectContaining({ name: "goose", status: "failed" }),
]),
});
expect(await readFile(codex, "utf8")).toBe(registration);
},
);
const gooseFixture = async (prefix: string) => {
const home = await createTestTempDirectory(prefix);
const team = join(home, "team.yaml");
const environment = { GOOSE_ADDITIONAL_CONFIG_FILES: team };
const systemPaths = { gooseSystemConfig: join(home, "system.yaml") };
const row = supportedClients(home, platform, environment, systemPaths).find(
({ name }) => name === "goose",
);
if (row === undefined) throw new Error("Goose has no table row");
return { home, team, row, environment, systemPaths };
};
it.skipIf(HOST_PLATFORM.platform === "win32")(
"secures a private profile on this host when setup targets another platform",
async () => {
const home = await createTestTempDirectory("rea-private-profile-host-");
const row = supportedClients(home, HOST_PLATFORM.platform, {
HOME: home,
}).find(({ name }) => name === "fx");
if (row?.privateProfile === undefined)
throw new Error("fx has no private profile row");
await expect(
configureClientConfiguration(row, {}, command),
).resolves.toMatchObject({ status: "configured" });
const directory = dirname(row.configPath);
await chmod(row.configPath, 0o644);
await chmod(directory, 0o755);
// The selected platform shapes the registration; the file's modes stay
// this host's, so a readable private profile is still repaired.
await expect(
configureClientConfiguration(row, {}, command, undefined, "win32"),
).resolves.toMatchObject({ status: "configured" });
expect((await stat(row.configPath)).mode & 0o777).toBe(0o600);
expect((await stat(directory)).mode & 0o777).toBe(0o700);
},
);