mirror of
https://github.com/morluto/rea.git
synced 2026-11-15 19:23:24 +00:00
* feat(setup): manage fx registrations with its private profile modes fx becomes a client-table row, so doctor, update and uninstall see the registration rea mcp add wrote, and setup offers fx when ~/.fx or FX_HOME exists. Setup now publishes a private profile through the same owner as mcp add (folder 0700, file 0600) and repairs those modes when the row is otherwise current. Doctor parses fx's array launcher instead of reporting it invalid. * fix(setup): judge fx's private profile by its targets and keep its cleanup owner Address review: privateProfileModesCurrent reads the modes of the canonical file and folder, so a linked profile becomes current; clientConfigurationAligned applies the same modes rule as plan and apply; a private-folder close failure reaches the caller with its cleanup owner instead of becoming a write failure; and the setup plan names the folder and file modes it sets. * fix(setup): keep fx's recovery backups owner-only beside a linked profile Setup and uninstall copied a private profile's original bytes with copyFile, so a backup beside a linked ~/.fx/mcp.json kept the target's readable mode outside the folder setup secures. Both now use the owner-only backup owner that mcp add already used. * feat(setup): manage Junie registrations and apply its location policy (#10712) * feat(setup): manage Junie registrations and apply its location policy Junie becomes a client-table row, so doctor, update and uninstall see the registration rea mcp add wrote, and setup offers Junie when its home exists. Whether Junie loads its default user MCP file now has one owner, read through the client policy block that setup, doctor and mcp add already consult: setup and mcp add refuse, and doctor reports, a registration Junie would not load. * fix(setup): read Junie's user configuration as strict JSON Address review: Junie loads config.json as strict JSON, so comments, trailing commas or an empty file leave its MCP location choice unknown, as before the move into the row. Filesystem tests also clear JUNIE_*, FX_HOME and MCPORTER_CONFIG so a developer's own client locations cannot be inspected or removed. * test(setup): keep the detected client order out of the lifecycle suite body The expected detection order grew with each folded client until the suite callback exceeded the max-lines-per-function lint limit. * fix(update): report a client whose policy setup refuses instead of failing maintenance Junie's disabled default locations (and Gemini's settings policy) make doctor report the registration stale, but setup refuses that client, so selecting it failed every other client's maintenance. The registration inspection now carries the setup blocker, and maintenance lists that client with its reason instead of selecting it. * refactor(setup): read fx's private-profile host through HOST_PLATFORM The fx private-profile mode checks and permissions describe the host REA runs on, so they take it from the host-platform owner instead of reading process.platform (refused by the ratchet from #10911). * feat(setup): manage Goose registrations across its configuration layers (#10715) * feat(setup): manage Goose registrations across its configuration layers Goose becomes a client-table row, so doctor, update and uninstall see the registration rea mcp add wrote, and setup offers Goose when its user file or system file exists. Goose loads its system file and GOOSE_ADDITIONAL_CONFIG_FILES beneath the user file and overlays each extension one field deep; doctor now reports that effective registration, while setup, update and uninstall change only the user file. Doctor also parses Goose's cmd launcher instead of reporting it invalid. * fix(setup): keep Goose's layered environment and load order through setup and uninstall Address review: read Goose's env alias and apply requested settings over envs; keep an inherited lower envs map instead of masking it with an empty default; keep each layer's last load so a repeated user file keeps its precedence; split GOOSE_ADDITIONAL_CONFIG_FILES with Windows quoting; validate only the effective environment across read-only layers; refuse uninstall while a read-only layer keeps REA enabled; and clear Goose's location variables in tests. * fix(setup): apply requested Goose settings over the inherited environment * fix(setup): leave lower-layer Goose settings in force and read admin layers read-only Setup no longer writes its default timeout, enabled, name or description into Goose's user entry when only a lower layer declares that setting, so the inherited value keeps applying and a repeat setup stays current. Uninstall reads Goose's read-only lower layers where Goose reads them, without the write target's ownership check, so an administrator-owned system file no longer blocks removal. The client table accepts an injected Goose system-config path so tests no longer read or skip on the host's /etc/goose/config.yaml; adds a shadowed-invalid-lower-layer regression. * fix(setup): keep an inherited Goose env alias's spelling when writing the user entry Goose overlays each extension field separately, so writing envs into the user file beside a lower layer's env alias left both in force, which readers reject as ambiguous. Setup now writes the user map under the alias a lower layer declares, and mcp add neither adds a default envs map beside an inherited alias nor renames a user alias the lower layer shares. * test(setup): inject Goose's system layer into mcp add selection resolveMcpRegistrationClients accepts the caller's system configuration paths, so the Goose layers test no longer reads the host's /etc/goose/config.yaml. * fix(setup): report REA's Goose registration that only a read-only layer defines * test(setup): pass an isolated Goose system path through update maintenance and the mcp add table * fix(setup): judge Goose uninstall by the merged entry and stop on an unreadable lower layer * fix(setup): select Goose's env alias by presence so a null alias stays malformed * fix(setup): use the shared record predicate for Goose's layered merge after #10588 * feat(setup): give Kilo a client-table row with an async layered location (#10823) * feat(setup): give Kilo a client-table row with an async layered location The client table gains a layeredLocation hook and resolveSupportedClients, which awaits it. Kilo's row resolves its user-scope layers through the same owner rea mcp add uses (KiloClientLocation), so both pick the same write target; the synchronous row keeps kilo.json in Kilo's config root with every candidate layer. The table refuses a relative KILO_CONFIG, KILO_CONFIG_DIR or XDG_CONFIG_HOME because Kilo's working directory is unknown. Refs #7181 * fix(setup): treat every folder Kilo reads as evidence that it is installed * fix(setup): normalize Kilo's absolute table paths and parse its command array * feat(setup): resolve the client table's layered locations in setup, doctor, update and uninstall (#10832) SetupHost (detection and the selectable table), ClientRegistrationStatus, uninstall, doctor's selected-registration environment and the mcp add catalog now read resolveSupportedClients, so each sees the Kilo layer Kilo writes rather than the synchronous default. Refs #7181 * docs(adr): record the one-client-table decision for setup and mcp add * fix(setup): refuse to read a client row whose location is unresolved Doctor's selected-client settings read a Kilo row whose relative KILO_CONFIG left only path evidence, so it inspected a file relative to REA's folder. The shared configuration reader now refuses such a row, so every reader reports the registration as unreadable. * docs(adr): name the row's layeredLocation hook * feat(setup): read KILO_CONFIG_CONTENT as Kilo's top read-only layer (#10851) * feat(setup): read KILO_CONFIG_CONTENT as Kilo's top read-only layer * fix(setup): refuse a Kilo uninstall that would expose or keep another layer's rea entry (#10852) * fix(setup): recheck KILO_CONFIG_CONTENT at write time and name it in doctor's recovery * refactor(mcp): register Kilo through its table row and remove the registration-only catalog kind (#10862) * refactor(mcp): register Kilo through its table row and remove the registration-only catalog kind * refactor(mcp): resolve user-scope targets in the catalog so location modules never import the client table * test(setup): cover Kilo's KILO_CONFIG, home-folder and inline-only layers * fix(setup): report REA's Kilo entry that only KILO_CONFIG_CONTENT sets during uninstall Doctor reports a registration that KILO_CONFIG_CONTENT alone defines, but uninstall found no file entry and reported Kilo as skipped. Uninstall now reports it once as failed and names the variable to edit. * docs(adr): record Kilo's inline layer in the client-table decision * fix(setup): compare Windows environment names without case when a higher layer overrides REA's entry Kilo's inline-layer check and Gemini's system-override check compared environment maps by exact key, so an equal Path beside PATH on Windows was reported as an override and setup refused the client. Both compare the entry as the client launches it. * fix(update): leave a stale Kilo registration KILO_CONFIG_CONTENT sets out of maintenance Setup refuses a refresh the inline layer would override, and one refused client failed every other client's maintenance. The registration inspection now names the inline layer as the setup blocker of a stale entry, and maintenance reports Kilo with that recovery instead of selecting it. * fix(setup): refuse relative JUNIE_HOME and Cline CLI overrides like the other client roots (#10909) * fix(setup): refuse relative JUNIE_HOME and Cline CLI overrides like the other client roots * fix(setup): refuse Windows client paths without a drive that follow the client's current drive * fix(update): select maintenance clients through setup's own refresh refusal check Maintenance predicted setup's refusal with its own rule: any inline rea entry blocked a stale registration, while an aligned one was never checked. A harmless KILO_CONFIG_CONTENT entry (enabled: true) kept a stale Kilo launcher pinned, and an inline value setup would not write (an extra environment variable) let an aligned Kilo registration block every client's maintenance. Setup's refusal checks (client policy, then the inline override comparison of the entry it would write) now live in one reader that setup's alignment check and maintenance share. Maintenance asks it for every owned enabled candidate and reports a refused client with setup's remediation and its real state. * fix(update): keep failed refresh-check reads owned by the supplied maintenance scope clientRefreshRefusal read the client's layers, target file and policy without the caller's resource scope, so a close that failed during those reads threw a cleanup failure no scope retained; maintenance then reported it as text and the scope's close could not retry the descriptor. The refresh reader now takes the scope and passes it to every read. * fix(setup): take the Kilo layer readers' default host from HOST_PLATFORM and the shared record predicate After rebasing on main (#10911, #10588): the new inlineRegistrationOverride and resolveSupportedClients defaults read the running host through the host-platform owner, and the layered launcher merge uses isRecord. * fix(update): keep an unreadable layer's reason in the refresh refusal check readClientRefresh returned undefined for an unreadable or malformed layer, so clientRefreshRefusal reported no refusal and maintenance selected a client whose setup preflight then failed. The reader returns the typed file failure, and the refusal check reports its detail like setup's preflight does. * fix(setup): count REA's entry in KILO_CONFIG_CONTENT as Kilo lifecycle evidence Doctor, update and uninstall inspected a client only when one of its folders existed, so a registration set only by KILO_CONFIG_CONTENT was skipped. A read-only inline layer that sets REA's entry is now evidence on its own. * fix(update): report a client whose only conflict is a discovered provider path as needing setup * fix(update): match discovered settings by the platform's environment identity * fix(update): preflight maintenance with setup's write-target checks and treat discovered inline paths as unknown
1106 lines
38 KiB
TypeScript
1106 lines
38 KiB
TypeScript
import { currentClientRegistrationCommand } from "../../../src/application/ClientRegistrationIdentity.js";
|
|
import {
|
|
lstat as fsLstat,
|
|
mkdir,
|
|
open as fsOpen,
|
|
readFile,
|
|
realpath as fsRealpath,
|
|
rename,
|
|
writeFile,
|
|
type FileHandle,
|
|
} from "node:fs/promises";
|
|
import { dirname, join } from "node:path";
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
readClientRegistrationInspections,
|
|
readClientRegistrationStatuses,
|
|
} from "../../../src/application/ClientRegistrationStatus.js";
|
|
import {
|
|
existingMaintenanceScope,
|
|
planIntegrationMaintenance,
|
|
} from "../../../src/application/UpdateMaintenance.js";
|
|
import { ArtifactResourceScope } from "../../../src/artifacts/ArtifactResourceScope.js";
|
|
import { ok } from "../../../src/domain/result.js";
|
|
import type { DoctorHost } from "../../../src/application/Doctor.js";
|
|
import { ConfigurationError } from "../../../src/domain/configurationErrors.js";
|
|
import {
|
|
filterClientsNeedingConfigure,
|
|
systemSetupHost,
|
|
} from "../../../src/application/SetupHost.js";
|
|
import { RegularFileCleanupFailure } from "../../../src/filesystem/OwnedRegularFile.js";
|
|
import { configureDetectedClients } from "../../../src/application/SetupClients.js";
|
|
import { configureClientConfiguration } from "../../../src/application/SetupClientConfiguration.js";
|
|
import {
|
|
systemUninstallHost,
|
|
runUninstall,
|
|
} from "../../../src/application/Uninstall.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js";
|
|
import { withVerifierCleanup } from "../../../scripts/lib/verifier-cleanup.mjs";
|
|
import { supportedClientRegistrationProfile } from "../../../src/application/SupportedClients.js";
|
|
|
|
vi.mock("node:fs/promises", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("node:fs/promises")>();
|
|
return {
|
|
...actual,
|
|
open: vi.fn(actual.open),
|
|
lstat: vi.fn(actual.lstat),
|
|
realpath: vi.fn(actual.realpath),
|
|
};
|
|
});
|
|
|
|
const openMock = vi.mocked(fsOpen);
|
|
const lstatMock = vi.mocked(fsLstat);
|
|
const realpathMock = vi.mocked(fsRealpath);
|
|
let actualFs: typeof import("node:fs/promises");
|
|
|
|
beforeEach(async () => {
|
|
actualFs =
|
|
await vi.importActual<typeof import("node:fs/promises")>(
|
|
"node:fs/promises",
|
|
);
|
|
openMock.mockImplementation((...args) => actualFs.open(...args));
|
|
lstatMock.mockImplementation((...args) => actualFs.lstat(...args));
|
|
realpathMock.mockImplementation((...args) => actualFs.realpath(...args));
|
|
});
|
|
|
|
afterEach(() => {
|
|
openMock.mockReset();
|
|
lstatMock.mockReset();
|
|
realpathMock.mockReset();
|
|
});
|
|
|
|
const holdReadHandle = (
|
|
path: string,
|
|
reason: string,
|
|
initiallyHeld = true,
|
|
maxFailures = Number.POSITIVE_INFINITY,
|
|
) => {
|
|
const closeAttempts: FileHandle[] = [];
|
|
let released = false;
|
|
let held = initiallyHeld;
|
|
let remainingFailures = maxFailures;
|
|
openMock.mockImplementation(async (...args) => {
|
|
const handle = await actualFs.open(...args);
|
|
if (String(args[0]) !== path) return handle;
|
|
const nativeClose = handle.close.bind(handle);
|
|
vi.spyOn(handle, "close").mockImplementation(async () => {
|
|
closeAttempts.push(handle);
|
|
if (held && !released && remainingFailures > 0) {
|
|
remainingFailures -= 1;
|
|
throw new Error(reason);
|
|
}
|
|
await nativeClose();
|
|
});
|
|
return handle;
|
|
});
|
|
return {
|
|
closeAttempts,
|
|
hold: () => {
|
|
held = true;
|
|
},
|
|
release: () => {
|
|
released = true;
|
|
},
|
|
};
|
|
};
|
|
|
|
/** Linux setup host with its VS Code client configuration directory prepared. */
|
|
const preparedVsCodeSetupHost = async (prefix: string) => {
|
|
const home = await createTestTempDirectory(prefix);
|
|
const host = systemSetupHost(
|
|
{ platform: "linux", homeDirectory: home } as DoctorHost,
|
|
{ HOME: home, XDG_CONFIG_HOME: join(home, "xdg") },
|
|
);
|
|
const client = (await host.supportedClients()).find(
|
|
({ name }) => name === "vscode",
|
|
);
|
|
if (client === undefined) throw new Error("VS Code setup client missing");
|
|
await mkdir(dirname(client.configPath), { recursive: true });
|
|
return { home, host, client };
|
|
};
|
|
|
|
/** Release the held read handle and verify host cleanup retries that same owner. */
|
|
const expectCloseReclaimsHandle = async (
|
|
host: { close?: () => Promise<unknown> },
|
|
closeAttempts: readonly object[],
|
|
release: () => void,
|
|
) => {
|
|
const attemptsBeforeClose = closeAttempts.length;
|
|
release();
|
|
expect(await host.close?.()).toBeUndefined();
|
|
expect(closeAttempts).toHaveLength(attemptsBeforeClose + 1);
|
|
expect(closeAttempts.at(-1)).toBe(closeAttempts[0]);
|
|
};
|
|
|
|
/** Give up on the assertion path: release the handle and let cleanup close it. */
|
|
const cleanupHeldHandle = async (
|
|
host: { close?: () => Promise<unknown> },
|
|
release: () => void,
|
|
) => {
|
|
release();
|
|
await host.close?.();
|
|
};
|
|
|
|
it("retains invalid UTF-8 diagnostics and the uninstall read owner when close fails", async () => {
|
|
const home = await createTestTempDirectory("rea-uninstall-utf8-close-");
|
|
const path = join(home, "client.json");
|
|
const original = Buffer.from([0xff]);
|
|
await writeFile(path, original);
|
|
const { closeAttempts, release } = holdReadHandle(
|
|
path,
|
|
"held uninstall handle",
|
|
);
|
|
const host = systemUninstallHost(home, undefined, { HOME: home }, "linux");
|
|
try {
|
|
expect(
|
|
await host.removeClient({
|
|
name: "cursor",
|
|
registrationProfile: supportedClientRegistrationProfile("cursor"),
|
|
format: "json",
|
|
configPath: path,
|
|
}),
|
|
).toMatchObject({
|
|
status: "failed",
|
|
detail: expect.stringContaining("not valid UTF-8"),
|
|
});
|
|
expect(Buffer.compare(await readFile(path), original)).toBe(0);
|
|
expect(closeAttempts).toHaveLength(3);
|
|
expect(closeAttempts.every((handle) => handle === closeAttempts[0])).toBe(
|
|
true,
|
|
);
|
|
await expectCloseReclaimsHandle(host, closeAttempts, release);
|
|
} finally {
|
|
await cleanupHeldHandle(host, release);
|
|
}
|
|
});
|
|
|
|
it("stops uninstall and retains the backup read owner when its close fails", async () => {
|
|
const home = await createTestTempDirectory("rea-uninstall-backup-close-");
|
|
const path = join(home, "client.json");
|
|
const client = {
|
|
name: "cursor",
|
|
registrationProfile: supportedClientRegistrationProfile("cursor"),
|
|
format: "json",
|
|
configPath: path,
|
|
} as const;
|
|
await writeFile(path, '{"keep":"before setup"}\n');
|
|
expect(
|
|
await configureClientConfiguration(client, {}, ["rea", "mcp"]),
|
|
).toMatchObject({ status: "configured" });
|
|
const configured = await readFile(path, "utf8");
|
|
const { closeAttempts, release } = holdReadHandle(
|
|
`${path}.rea.backup`,
|
|
"held backup handle",
|
|
);
|
|
const host = systemUninstallHost(home, undefined, { HOME: home }, "linux");
|
|
try {
|
|
expect(await host.removeClient(client)).toEqual({
|
|
name: "cursor",
|
|
status: "failed",
|
|
detail: expect.stringMatching(
|
|
/could not be backed up.*no change.*permissions.*uninstall/i,
|
|
),
|
|
});
|
|
expect(await readFile(path, "utf8")).toBe(configured);
|
|
expect(closeAttempts).toHaveLength(2);
|
|
await expectCloseReclaimsHandle(host, closeAttempts, release);
|
|
} finally {
|
|
await cleanupHeldHandle(host, release);
|
|
}
|
|
});
|
|
|
|
it("keeps shared-ownership proof when the ownership read completes but its close fails", async () => {
|
|
// FreeBuff without a profile marker leaves its shared storage applicability-
|
|
// unverified. The completed ownership read is the proof that later read
|
|
// failures are blocking; discarding it softens every failure to retained,
|
|
// so purge runs while the active shared registration remains.
|
|
const home = await createTestTempDirectory("rea-freebuff-ownership-close-");
|
|
const setupHost = systemSetupHost(
|
|
{ platform: "linux", homeDirectory: home } as DoctorHost,
|
|
{ HOME: home },
|
|
);
|
|
const client = (await setupHost.supportedClients()).find(
|
|
({ name }) => name === "freebuff",
|
|
);
|
|
if (client === undefined) throw new Error("freebuff client missing");
|
|
await mkdir(dirname(client.configPath), { recursive: true });
|
|
await writeFile(
|
|
client.configPath,
|
|
JSON.stringify({ mcpServers: { other: { command: "other" } } }),
|
|
);
|
|
expect(
|
|
await configureClientConfiguration(client, {}, ["rea", "mcp"]),
|
|
).toMatchObject({ status: "configured" });
|
|
const state = join(home, ".rea", "state.json");
|
|
await mkdir(dirname(state), { recursive: true });
|
|
await writeFile(state, "{}\n");
|
|
const { release } = holdReadHandle(
|
|
client.configPath,
|
|
"held ownership read handle",
|
|
);
|
|
const host = systemUninstallHost(home, undefined, { HOME: home }, "linux");
|
|
try {
|
|
const result = await runUninstall(true, host);
|
|
expect(result.status).toBe("failed");
|
|
expect(result.items).toContainEqual(
|
|
expect.objectContaining({ name: "freebuff", status: "failed" }),
|
|
);
|
|
expect(result.items).toContainEqual(
|
|
expect.objectContaining({
|
|
name: "uninstall",
|
|
status: "skipped",
|
|
detail: expect.stringContaining(
|
|
"No REA registration, skill, or data was removed",
|
|
),
|
|
}),
|
|
);
|
|
// Data must not be purged while the shared registration is still active.
|
|
await expect(readFile(state, "utf8")).resolves.toBe("{}\n");
|
|
} finally {
|
|
await cleanupHeldHandle(host, release);
|
|
}
|
|
});
|
|
|
|
it("keeps shared-ownership proof when removal refuses a shadowed duplicate", async () => {
|
|
// The effective row is REA-owned while an earlier duplicate is foreign, so
|
|
// removal refuses the edit. That refusal must not erase ownership: without
|
|
// the positive row fact, recurring close failures soften to retained and
|
|
// purge runs while the owned row remains active.
|
|
const home = await createTestTempDirectory("rea-freebuff-shadowed-close-");
|
|
const setupHost = systemSetupHost(
|
|
{ platform: "linux", homeDirectory: home } as DoctorHost,
|
|
{ HOME: home },
|
|
);
|
|
const client = (await setupHost.supportedClients()).find(
|
|
({ name }) => name === "freebuff",
|
|
);
|
|
if (client === undefined) throw new Error("freebuff client missing");
|
|
await mkdir(dirname(client.configPath), { recursive: true });
|
|
await writeFile(
|
|
client.configPath,
|
|
'{"mcpServers":{"rea":{"command":"foreign-server","args":[]},"rea":{"command":"rea","args":["mcp"]}}}',
|
|
);
|
|
const state = join(home, ".rea", "state.json");
|
|
await mkdir(dirname(state), { recursive: true });
|
|
await writeFile(state, "{}\n");
|
|
const { release } = holdReadHandle(
|
|
client.configPath,
|
|
"held ownership read handle",
|
|
);
|
|
const host = systemUninstallHost(home, undefined, { HOME: home }, "linux");
|
|
try {
|
|
const result = await runUninstall(true, host);
|
|
expect(result.status).toBe("failed");
|
|
expect(result.items).toContainEqual(
|
|
expect.objectContaining({ name: "freebuff", status: "failed" }),
|
|
);
|
|
expect(result.items).toContainEqual(
|
|
expect.objectContaining({
|
|
name: "uninstall",
|
|
status: "skipped",
|
|
detail: expect.stringContaining(
|
|
"No REA registration, skill, or data was removed",
|
|
),
|
|
}),
|
|
);
|
|
await expect(readFile(state, "utf8")).resolves.toBe("{}\n");
|
|
} finally {
|
|
await cleanupHeldHandle(host, release);
|
|
}
|
|
});
|
|
|
|
it("stops removal when a completed reread leaves its handle unreleased", async () => {
|
|
// Greptile P1: preflight and reread both complete with a foreign-only row,
|
|
// but the reread handle cannot be closed even by the cleanup retry. REA
|
|
// still owns that open handle, so removal must stop before purge: serving
|
|
// the completed text would let removeAll delete managed data while the
|
|
// incomplete cleanup is only reported later, by close().
|
|
const home = await createTestTempDirectory("rea-freebuff-reread-leak-");
|
|
const setupHost = systemSetupHost(
|
|
{ platform: "linux", homeDirectory: home } as DoctorHost,
|
|
{ HOME: home },
|
|
);
|
|
const client = (await setupHost.supportedClients()).find(
|
|
({ name }) => name === "freebuff",
|
|
);
|
|
if (client === undefined) throw new Error("freebuff client missing");
|
|
await mkdir(dirname(client.configPath), { recursive: true });
|
|
await writeFile(
|
|
client.configPath,
|
|
'{"mcpServers":{"rea":{"command":"foreign-server","args":[]}}}',
|
|
);
|
|
const state = join(home, ".rea", "state.json");
|
|
await mkdir(dirname(state), { recursive: true });
|
|
await writeFile(state, "{}\n");
|
|
const { hold, release } = holdReadHandle(
|
|
client.configPath,
|
|
"held removal reread handle",
|
|
false,
|
|
);
|
|
const base = systemUninstallHost(home, undefined, { HOME: home }, "linux");
|
|
const host = {
|
|
...base,
|
|
inspectSkill: async () => {
|
|
hold();
|
|
return base.inspectSkill?.();
|
|
},
|
|
};
|
|
try {
|
|
const result = await runUninstall(true, host);
|
|
expect(result.status).toBe("failed");
|
|
expect(result.items).toContainEqual(
|
|
expect.objectContaining({
|
|
name: "freebuff",
|
|
status: "failed",
|
|
detail: expect.stringContaining("its resources could not be released"),
|
|
}),
|
|
);
|
|
expect(result.items).toContainEqual(
|
|
expect.objectContaining({
|
|
name: "uninstall",
|
|
status: "skipped",
|
|
detail: expect.stringContaining("Uninstall stopped at this failure"),
|
|
}),
|
|
);
|
|
// Managed data must survive an unreleased read handle.
|
|
await expect(readFile(state, "utf8")).resolves.toBe("{}\n");
|
|
} finally {
|
|
await cleanupHeldHandle(host, release);
|
|
}
|
|
});
|
|
|
|
it("keeps the shared registration fail-closed when ZCode's probe is unresolved", async () => {
|
|
// A denied profile probe leaves ZCode's applicability unresolved, never
|
|
// absent: the shared file stays fail-closed so its row is inspected and
|
|
// removed instead of leaving an installed ZCode wired to purged data.
|
|
// Definitive absence still leaves the file to FreeBuff, ZCode's owner.
|
|
const home = await createTestTempDirectory("rea-zcode-fallback-unresolved-");
|
|
const fallback = join(home, ".agents", "mcp.json");
|
|
const profile = join(home, ".zcode", "cli");
|
|
lstatMock.mockImplementation((path) =>
|
|
String(path) === profile
|
|
? Promise.reject(
|
|
Object.assign(new Error("EACCES: permission denied, lstat"), {
|
|
code: "EACCES",
|
|
}),
|
|
)
|
|
: actualFs.lstat(path),
|
|
);
|
|
const unresolved = systemUninstallHost(
|
|
home,
|
|
undefined,
|
|
{ HOME: home },
|
|
"linux",
|
|
);
|
|
try {
|
|
const rows = (await unresolved.clients()).filter(
|
|
(candidate) => candidate.configPath === fallback,
|
|
);
|
|
// One physical file, one owner: FreeBuff edits the shared file once and
|
|
// ZCode reports that same outcome instead of racing it to "absent".
|
|
expect(rows).toHaveLength(1);
|
|
expect(rows[0]).toMatchObject({ name: "freebuff", sharedWith: ["zcode"] });
|
|
// Fail-closed: the unresolved reader carries no unverified softening, so a
|
|
// failed read blocks removal instead of retaining around the purge.
|
|
expect(rows[0]?.applicability).toBeUndefined();
|
|
} finally {
|
|
await unresolved.close?.();
|
|
}
|
|
lstatMock.mockImplementation((...args) => actualFs.lstat(...args));
|
|
const absent = systemUninstallHost(home, undefined, { HOME: home }, "linux");
|
|
try {
|
|
const rows = (await absent.clients()).filter(
|
|
(candidate) => candidate.configPath === fallback,
|
|
);
|
|
expect(rows).toHaveLength(1);
|
|
// Definitive absence keeps ZCode out of another product's primary storage.
|
|
expect(rows[0]?.sharedWith).toBeUndefined();
|
|
} finally {
|
|
await absent.close?.();
|
|
}
|
|
});
|
|
|
|
it("removes a shared registration once and reports that outcome to both clients", async () => {
|
|
// ZCode and FreeBuff both load ~/.agents/mcp.json. Queuing it per client
|
|
// removed the row as ZCode and then reported FreeBuff as "absent", losing
|
|
// the outcome for the client that owns the file, and it read and edited one
|
|
// shared resource twice.
|
|
const home = await createTestTempDirectory("rea-shared-removal-once-");
|
|
const shared = join(home, ".agents", "mcp.json");
|
|
await mkdir(join(home, ".zcode", "cli"), { recursive: true });
|
|
await mkdir(join(home, ".config", "freebuff-desktop"), { recursive: true });
|
|
expect(
|
|
await configureClientConfiguration(
|
|
{
|
|
name: "freebuff",
|
|
registrationProfile: supportedClientRegistrationProfile("freebuff"),
|
|
format: "json",
|
|
configPath: shared,
|
|
},
|
|
{},
|
|
["rea", "mcp"],
|
|
),
|
|
).toMatchObject({ status: "configured" });
|
|
const host = systemUninstallHost(home, undefined, { HOME: home }, "linux");
|
|
try {
|
|
const result = await runUninstall(false, host);
|
|
expect(result.status).toBe("complete");
|
|
expect(result.items).toContainEqual(
|
|
expect.objectContaining({
|
|
name: "freebuff",
|
|
status: "removed",
|
|
configuration_path: shared,
|
|
}),
|
|
);
|
|
// The fallback reader reports the owner's removal, never "absent".
|
|
const zcode = result.items.find(({ name }) => name === "zcode");
|
|
expect(zcode?.actions).toContainEqual(
|
|
expect.objectContaining({
|
|
status: "removed",
|
|
configuration_path: shared,
|
|
}),
|
|
);
|
|
expect(zcode?.actions).not.toContainEqual(
|
|
expect.objectContaining({
|
|
status: "skipped",
|
|
configuration_path: shared,
|
|
}),
|
|
);
|
|
expect(JSON.parse(await readFile(shared, "utf8"))).toEqual({
|
|
mcpServers: {},
|
|
});
|
|
} finally {
|
|
await host.close?.();
|
|
}
|
|
});
|
|
|
|
it("keeps one row per client when OpenCode is pointed at FreeBuff's file", async () => {
|
|
// OPENCODE_CONFIG can make OpenCode and FreeBuff edit one file with different
|
|
// tables (`mcp.rea` and `mcpServers.rea`). Sharing one decision by filename
|
|
// would remove only OpenCode's row and report it for FreeBuff too, leaving
|
|
// FreeBuff registered while uninstall reports complete.
|
|
const home = await createTestTempDirectory("rea-shared-dialects-");
|
|
const shared = join(home, ".agents", "mcp.json");
|
|
await mkdir(dirname(shared), { recursive: true });
|
|
await mkdir(join(home, ".config", "freebuff-desktop"), { recursive: true });
|
|
expect(
|
|
await configureClientConfiguration(
|
|
{
|
|
name: "freebuff",
|
|
registrationProfile: supportedClientRegistrationProfile("freebuff"),
|
|
format: "json",
|
|
configPath: shared,
|
|
},
|
|
{},
|
|
["rea", "mcp"],
|
|
),
|
|
).toMatchObject({ status: "configured" });
|
|
const host = systemUninstallHost(
|
|
home,
|
|
undefined,
|
|
{ HOME: home, OPENCODE_CONFIG: shared },
|
|
"linux",
|
|
);
|
|
try {
|
|
const rows = (await host.clients()).filter(
|
|
(candidate) => candidate.configPath === shared,
|
|
);
|
|
expect(rows.map(({ name }) => name).sort()).toEqual([
|
|
"freebuff",
|
|
"opencode",
|
|
]);
|
|
expect(await runUninstall(false, host)).toMatchObject({
|
|
status: "complete",
|
|
});
|
|
expect(JSON.parse(await readFile(shared, "utf8"))).toEqual({
|
|
mcpServers: {},
|
|
});
|
|
} finally {
|
|
await host.close?.();
|
|
}
|
|
});
|
|
|
|
it.each([true, false])(
|
|
"projects only the shared table's outcome to ZCode (shared row owned: %s)",
|
|
async (sharedOwned) => {
|
|
const home = await createTestTempDirectory("rea-shared-table-outcome-");
|
|
const shared = join(home, ".agents", "mcp.json");
|
|
const zcodePath = join(home, ".zcode", "cli", "config.json");
|
|
await mkdir(dirname(shared), { recursive: true });
|
|
await mkdir(dirname(zcodePath), { recursive: true });
|
|
await writeFile(zcodePath, '{"mcp":{"servers":{}}}');
|
|
await writeFile(
|
|
shared,
|
|
JSON.stringify({
|
|
mcp: {
|
|
rea: {
|
|
type: "local",
|
|
command: [sharedOwned ? "foreign-server" : "rea", "mcp"],
|
|
},
|
|
},
|
|
mcpServers: {
|
|
rea: {
|
|
command: sharedOwned ? "rea" : "foreign-server",
|
|
args: ["mcp"],
|
|
},
|
|
},
|
|
}),
|
|
);
|
|
const host = systemUninstallHost(
|
|
home,
|
|
undefined,
|
|
{ HOME: home, OPENCODE_CONFIG: shared },
|
|
"linux",
|
|
);
|
|
try {
|
|
const result = await runUninstall(false, host);
|
|
expect(result.status).toBe("complete");
|
|
const zcode = result.items.find(({ name }) => name === "zcode");
|
|
expect(zcode?.status).toBe(sharedOwned ? "removed" : "retained");
|
|
expect(
|
|
zcode?.actions?.filter(
|
|
({ configuration_path }) => configuration_path === shared,
|
|
),
|
|
).toEqual([
|
|
expect.objectContaining({
|
|
status: sharedOwned ? "removed" : "retained",
|
|
}),
|
|
]);
|
|
const remaining = JSON.parse(await readFile(shared, "utf8"));
|
|
expect(remaining.mcp).toEqual(
|
|
sharedOwned
|
|
? { rea: { type: "local", command: ["foreign-server", "mcp"] } }
|
|
: {},
|
|
);
|
|
expect(remaining.mcpServers).toEqual(
|
|
sharedOwned
|
|
? {}
|
|
: { rea: { command: "foreign-server", args: ["mcp"] } },
|
|
);
|
|
} finally {
|
|
await host.close?.();
|
|
}
|
|
},
|
|
);
|
|
|
|
it("does not treat a dangling FreeBuff profile link as installed", async () => {
|
|
// Setup and doctor follow ~/.config/freebuff-desktop and reject a link whose
|
|
// target is gone. Discovery must agree: counting the leftover link as an
|
|
// installed product turns an unrelated malformed shared file into a blocking
|
|
// failure that stops every other REA removal.
|
|
const home = await createTestTempDirectory("rea-freebuff-dangling-profile-");
|
|
const shared = join(home, ".agents", "mcp.json");
|
|
const profile = join(home, ".config", "freebuff-desktop");
|
|
const cursor = join(home, ".cursor", "mcp.json");
|
|
await mkdir(dirname(shared), { recursive: true });
|
|
await writeFile(shared, "{ not json");
|
|
await mkdir(dirname(cursor), { recursive: true });
|
|
await writeFile(cursor, '{"keep":"before setup"}\n');
|
|
expect(
|
|
await configureClientConfiguration(
|
|
{
|
|
name: "cursor",
|
|
registrationProfile: supportedClientRegistrationProfile("cursor"),
|
|
format: "json",
|
|
configPath: cursor,
|
|
},
|
|
{},
|
|
["rea", "mcp"],
|
|
),
|
|
).toMatchObject({ status: "configured" });
|
|
lstatMock.mockImplementation((path) =>
|
|
String(path) === profile
|
|
? Promise.resolve({
|
|
isFile: () => false,
|
|
isDirectory: () => false,
|
|
isSymbolicLink: () => true,
|
|
} as unknown as Awaited<ReturnType<typeof fsLstat>>)
|
|
: actualFs.lstat(path),
|
|
);
|
|
realpathMock.mockImplementation((path) =>
|
|
String(path) === profile
|
|
? Promise.reject(
|
|
Object.assign(
|
|
new Error("ENOENT: no such file or directory, realpath"),
|
|
{ code: "ENOENT" },
|
|
),
|
|
)
|
|
: actualFs.realpath(path),
|
|
);
|
|
const host = systemUninstallHost(home, undefined, { HOME: home }, "linux");
|
|
try {
|
|
const result = await runUninstall(false, host);
|
|
expect(result.status).toBe("complete");
|
|
// The unrelated registration is removed instead of blocked.
|
|
expect(result.items).toContainEqual(
|
|
expect.objectContaining({ name: "cursor", status: "removed" }),
|
|
);
|
|
expect(JSON.parse(await readFile(cursor, "utf8"))).not.toHaveProperty(
|
|
"mcpServers.rea",
|
|
);
|
|
expect(result.items).toContainEqual(
|
|
expect.objectContaining({ name: "freebuff", status: "retained" }),
|
|
);
|
|
} finally {
|
|
await host.close?.();
|
|
}
|
|
});
|
|
|
|
it("serves a completed removal reread when its close retry releases the handle", async () => {
|
|
// Preflight saw no owned row; another writer adds one before removal. The
|
|
// reread's first close fails but its cleanup retry releases the handle:
|
|
// the completed text is the ownership evidence that drives removal of the
|
|
// concurrently added row instead of softening to retained and leaving it
|
|
// active while data is purged.
|
|
const home = await createTestTempDirectory("rea-freebuff-reread-close-");
|
|
const setupHost = systemSetupHost(
|
|
{ platform: "linux", homeDirectory: home } as DoctorHost,
|
|
{ HOME: home },
|
|
);
|
|
const client = (await setupHost.supportedClients()).find(
|
|
({ name }) => name === "freebuff",
|
|
);
|
|
if (client === undefined) throw new Error("freebuff client missing");
|
|
await mkdir(dirname(client.configPath), { recursive: true });
|
|
await writeFile(
|
|
client.configPath,
|
|
'{"mcpServers":{"rea":{"command":"foreign-server","args":[]}}}',
|
|
);
|
|
const cacheOwned = join(home, ".rea", "cache", "owned");
|
|
await mkdir(dirname(cacheOwned), { recursive: true });
|
|
await writeFile(cacheOwned, "owned\n");
|
|
const { hold, release } = holdReadHandle(
|
|
client.configPath,
|
|
"held removal reread handle",
|
|
false,
|
|
1,
|
|
);
|
|
const base = systemUninstallHost(home, undefined, { HOME: home }, "linux");
|
|
const host = {
|
|
...base,
|
|
inspectSkill: async () => {
|
|
// Another writer registers REA between preflight and removal.
|
|
await writeFile(
|
|
client.configPath,
|
|
'{"mcpServers":{"rea":{"command":"rea","args":["mcp"]}}}',
|
|
);
|
|
hold();
|
|
return base.inspectSkill?.();
|
|
},
|
|
};
|
|
try {
|
|
const result = await runUninstall(true, host);
|
|
expect(result.status).toBe("complete");
|
|
expect(result.items).toContainEqual(
|
|
expect.objectContaining({ name: "freebuff", status: "removed" }),
|
|
);
|
|
expect(await readFile(client.configPath, "utf8")).not.toContain("rea");
|
|
await expect(readFile(cacheOwned)).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
} finally {
|
|
await cleanupHeldHandle(host, release);
|
|
}
|
|
});
|
|
|
|
describe("ZCode fallback cleanup ownership", () => {
|
|
it.each(["preflight", "configure"])(
|
|
"retains a failed ZCode fallback close during %s until host cleanup",
|
|
async (operation) => {
|
|
const home = await createTestTempDirectory("rea-zcode-fallback-close-");
|
|
const host = systemSetupHost(
|
|
{ platform: "linux", homeDirectory: home } as DoctorHost,
|
|
{ HOME: home },
|
|
);
|
|
const client = (await host.supportedClients()).find(
|
|
({ name }) => name === "zcode",
|
|
);
|
|
if (client?.zcodeFallbackPath === undefined)
|
|
throw new Error("ZCode fallback path missing");
|
|
const fallback = client.zcodeFallbackPath;
|
|
await mkdir(dirname(fallback), { recursive: true });
|
|
const original = '{"mcpServers":{"other":{"command":"other"}}}\n';
|
|
await writeFile(fallback, original);
|
|
const { closeAttempts, release } = holdReadHandle(
|
|
fallback,
|
|
"held fallback handle",
|
|
);
|
|
try {
|
|
const action =
|
|
operation === "preflight"
|
|
? host.inspectClientConfiguration?.(client, {}, ["rea", "mcp"])
|
|
: host.configureClient(client, {}, ["rea", "mcp"]);
|
|
if (action === undefined) throw new Error("setup preflight missing");
|
|
await expect(action).rejects.toMatchObject({
|
|
_tag: "ConfigurationError",
|
|
cause: expect.any(RegularFileCleanupFailure),
|
|
cleanup: { resources: [fallback] },
|
|
});
|
|
expect(closeAttempts).toHaveLength(2);
|
|
expect(closeAttempts[0]).toBe(closeAttempts[1]);
|
|
expect(await readFile(fallback, "utf8")).toBe(original);
|
|
await expect(readFile(client.configPath)).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
await expectCloseReclaimsHandle(host, closeAttempts, release);
|
|
} finally {
|
|
await cleanupHeldHandle(host, release);
|
|
}
|
|
},
|
|
);
|
|
});
|
|
|
|
describe("setup client configuration cleanup ownership", () => {
|
|
it.each([
|
|
["valid UTF-8", Buffer.from('{"servers": {"other": {}}}\n')],
|
|
["malformed UTF-8", Buffer.from([0xff])],
|
|
] as const)(
|
|
"stops configuration and retains the %s read owner until host close",
|
|
async (encoding, original) => {
|
|
const { host, client } = await preparedVsCodeSetupHost(
|
|
"rea-setup-config-close-",
|
|
);
|
|
await writeFile(client.configPath, original);
|
|
|
|
const { closeAttempts, release } = holdReadHandle(
|
|
client.configPath,
|
|
"test holds the config handle open",
|
|
);
|
|
const configure = vi.spyOn(host, "configureClient");
|
|
|
|
try {
|
|
const failure = await filterClientsNeedingConfigure(
|
|
host,
|
|
[client],
|
|
{ providerEnvironment: {} },
|
|
["rea", "mcp"],
|
|
)
|
|
.then((detectedClients) =>
|
|
configureDetectedClients({
|
|
host,
|
|
detectedClients,
|
|
providerEnvironment: {},
|
|
command: ["rea", "mcp"],
|
|
clients: {},
|
|
appliedActions: [],
|
|
}),
|
|
)
|
|
.then(
|
|
() => undefined,
|
|
(cause: unknown) => cause,
|
|
);
|
|
expect(failure).toBeInstanceOf(ConfigurationError);
|
|
if (!(failure instanceof ConfigurationError))
|
|
throw new Error(
|
|
"Expected the configuration failure to retain cleanup authority",
|
|
);
|
|
if (encoding === "malformed UTF-8")
|
|
expect(failure).toMatchObject({
|
|
cause: {
|
|
outcome: { kind: "failed", cause: expect.any(TypeError) },
|
|
},
|
|
});
|
|
expect(failure).toMatchObject({
|
|
_tag: "ConfigurationError",
|
|
cause: expect.any(RegularFileCleanupFailure),
|
|
cleanup: {
|
|
resources: [client.configPath],
|
|
reason: expect.stringContaining(
|
|
"test holds the config handle open",
|
|
),
|
|
},
|
|
});
|
|
expect(configure).not.toHaveBeenCalled();
|
|
expect(closeAttempts).toHaveLength(2);
|
|
expect(closeAttempts[0]).toBe(closeAttempts[1]);
|
|
expect(
|
|
Buffer.compare(await readFile(client.configPath), original),
|
|
).toBe(0);
|
|
|
|
await expectCloseReclaimsHandle(host, closeAttempts, release);
|
|
expect(failure.cleanupIncomplete).toBe(false);
|
|
expect(failure.cleanupResources).toEqual([]);
|
|
} finally {
|
|
await cleanupHeldHandle(host, release);
|
|
}
|
|
},
|
|
);
|
|
|
|
it("waits for an admitted configuration read and rejects later admissions", async () => {
|
|
const { host, client } = await preparedVsCodeSetupHost(
|
|
"rea-setup-config-inflight-",
|
|
);
|
|
await writeFile(client.configPath, '{"servers": {}}\n');
|
|
|
|
let beginRead!: () => void;
|
|
const readStarted = new Promise<void>((resolve) => {
|
|
beginRead = resolve;
|
|
});
|
|
let unblockRead!: () => void;
|
|
const readGate = new Promise<void>((resolve) => {
|
|
unblockRead = resolve;
|
|
});
|
|
let heldRead = false;
|
|
openMock.mockImplementation(async (...args) => {
|
|
const handle = await actualFs.open(...args);
|
|
if (String(args[0]) !== client.configPath || heldRead) return handle;
|
|
heldRead = true;
|
|
const nativeRead = handle.read.bind(handle);
|
|
vi.spyOn(handle, "read").mockImplementation(async (...readArgs) => {
|
|
beginRead();
|
|
await readGate;
|
|
return nativeRead(...readArgs);
|
|
});
|
|
return handle;
|
|
});
|
|
|
|
let hostCloseFinished = false;
|
|
const alignment = host.clientNeedsConfigure(client, {}, ["rea", "mcp"]);
|
|
let closeResult: ReturnType<NonNullable<typeof host.close>> | undefined;
|
|
await withVerifierCleanup(async () => {
|
|
await awaitAcknowledgement(readStarted, alignment, "configuration read");
|
|
|
|
const close = host.close;
|
|
if (close === undefined) throw new Error("Setup host close is missing");
|
|
closeResult = close().then((result) => {
|
|
hostCloseFinished = true;
|
|
return result;
|
|
});
|
|
|
|
const lateAdmission = await host
|
|
.clientNeedsConfigure(client, {}, ["rea", "mcp"])
|
|
.then(
|
|
() => undefined,
|
|
(cause: unknown) => cause,
|
|
);
|
|
expect(lateAdmission).toBeInstanceOf(ConfigurationError);
|
|
expect(lateAdmission).toMatchObject({
|
|
message: expect.stringContaining("Artifact resource scope is closed"),
|
|
});
|
|
expect(hostCloseFinished).toBe(false);
|
|
|
|
unblockRead();
|
|
await expect(alignment).resolves.toBe(true);
|
|
expect(await closeResult).toBeUndefined();
|
|
expect(hostCloseFinished).toBe(true);
|
|
}, [
|
|
unblockRead,
|
|
async () => {
|
|
await alignment;
|
|
},
|
|
async () => {
|
|
await closeResult;
|
|
},
|
|
async () => {
|
|
await host.close?.();
|
|
},
|
|
]);
|
|
});
|
|
});
|
|
|
|
const unscopedRegistrationReaders: readonly {
|
|
readonly name: string;
|
|
readonly read: (
|
|
home: string,
|
|
environment: NodeJS.ProcessEnv,
|
|
execute: Parameters<typeof planIntegrationMaintenance>[3],
|
|
) => Promise<unknown>;
|
|
}[] = [
|
|
{
|
|
name: "inspections",
|
|
read: (home, environment) =>
|
|
readClientRegistrationInspections(home, "/current/rea", { environment }),
|
|
},
|
|
{
|
|
name: "statuses",
|
|
read: (home, environment) =>
|
|
readClientRegistrationStatuses(home, "/current/rea", { environment }),
|
|
},
|
|
{
|
|
name: "maintenance scope",
|
|
read: (home, environment) =>
|
|
existingMaintenanceScope(home, "/current/rea", environment),
|
|
},
|
|
{
|
|
name: "maintenance plan",
|
|
read: (home, environment, execute) =>
|
|
planIntegrationMaintenance(home, "/current/rea", environment, execute),
|
|
},
|
|
];
|
|
|
|
it.each(
|
|
unscopedRegistrationReaders.flatMap((reader) =>
|
|
["configuration", "policy"].flatMap((source) =>
|
|
["completed", "failed"].map((readOutcome) => ({
|
|
...reader,
|
|
source,
|
|
readOutcome,
|
|
})),
|
|
),
|
|
),
|
|
)(
|
|
"$name transfers the exact $source owner after a $readOutcome read without a supplied scope",
|
|
async ({ read, source, readOutcome }) => {
|
|
const home = await createTestTempDirectory(
|
|
"rea-registration-unscoped-close-",
|
|
);
|
|
const config = join(
|
|
home,
|
|
source === "policy" ? ".gemini/settings.json" : ".cursor/mcp.json",
|
|
);
|
|
await mkdir(dirname(config), { recursive: true });
|
|
const registration =
|
|
'{"mcpServers":{"rea":{"command":"rea","args":["mcp"]}}}\n';
|
|
await writeFile(config, registration);
|
|
const heldPath = source === "policy" ? join(home, "system.json") : config;
|
|
const heldText = source === "policy" ? "{}\n" : registration;
|
|
await writeFile(
|
|
heldPath,
|
|
readOutcome === "failed" ? Buffer.from([0xff]) : heldText,
|
|
);
|
|
const environment = {
|
|
HOME: home,
|
|
GEMINI_CLI_SYSTEM_SETTINGS_PATH: join(home, "system.json"),
|
|
GEMINI_CLI_SYSTEM_DEFAULTS_PATH: join(home, "absent-defaults.json"),
|
|
};
|
|
const held = holdReadHandle(
|
|
heldPath,
|
|
"held standalone registration descriptor",
|
|
);
|
|
const execute = vi.fn(async () =>
|
|
ok('{"status":"planned","plannedActions":[]}'),
|
|
);
|
|
try {
|
|
const outcome = await read(home, environment, execute).then(
|
|
(value) => ({ kind: "returned" as const, value }),
|
|
(cause: unknown) => ({ kind: "threw" as const, cause }),
|
|
);
|
|
expect(outcome).toMatchObject({
|
|
kind: "threw",
|
|
cause: expect.any(RegularFileCleanupFailure),
|
|
});
|
|
if (
|
|
outcome.kind !== "threw" ||
|
|
!(outcome.cause instanceof RegularFileCleanupFailure)
|
|
)
|
|
throw new Error("Read lost its failed cleanup owner");
|
|
const failure = outcome.cause;
|
|
expect(failure.path).toBe(heldPath);
|
|
if (readOutcome === "completed")
|
|
expect(failure.outcome).toEqual({ kind: "completed", value: heldText });
|
|
else
|
|
expect(failure.outcome).toMatchObject({
|
|
kind: "failed",
|
|
cause: { _tag: "InvalidUtf8FileError", path: heldPath },
|
|
});
|
|
expect(failure.owner.handle).toBe(held.closeAttempts[0]);
|
|
await expect(failure.owner.close()).rejects.toThrow("held standalone");
|
|
await rename(heldPath, `${heldPath}.retired`);
|
|
await writeFile(heldPath, "replacement");
|
|
held.release();
|
|
await failure.owner.close();
|
|
expect(failure.owner.handle.fd).toBe(-1);
|
|
expect(new Set(held.closeAttempts).size).toBe(1);
|
|
expect(await readFile(heldPath, "utf8")).toBe("replacement");
|
|
expect(execute).not.toHaveBeenCalled();
|
|
} finally {
|
|
held.release();
|
|
for (const handle of new Set(held.closeAttempts)) await handle.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
it("keeps a supplied registration scope responsible for diagnostics and cleanup retry", async () => {
|
|
const home = await createTestTempDirectory("rea-registration-scoped-close-");
|
|
const config = join(home, ".cursor/mcp.json");
|
|
await mkdir(dirname(config), { recursive: true });
|
|
await writeFile(
|
|
config,
|
|
'{"mcpServers":{"rea":{"command":"rea","args":["mcp"]}}}',
|
|
);
|
|
const held = holdReadHandle(config, "held scoped registration descriptor");
|
|
const resources = new ArtifactResourceScope();
|
|
try {
|
|
expect(
|
|
(
|
|
await readClientRegistrationStatuses(home, "/current/rea", {
|
|
environment: { HOME: home },
|
|
resources,
|
|
})
|
|
).find((status) => status.client === "cursor"),
|
|
).toMatchObject({ state: "invalid", command: [] });
|
|
expect(resources.hasPending).toBe(true);
|
|
await expect(resources.close()).rejects.toThrow();
|
|
held.release();
|
|
await resources.close();
|
|
expect(resources.hasPending).toBe(false);
|
|
expect(new Set(held.closeAttempts).size).toBe(1);
|
|
expect(held.closeAttempts[0]!.fd).toBe(-1);
|
|
} finally {
|
|
held.release();
|
|
await resources.close();
|
|
}
|
|
});
|
|
|
|
it("keeps a supplied maintenance scope responsible for a refresh-check read whose close fails", async () => {
|
|
const home = await createTestTempDirectory("rea-maintenance-refusal-close-");
|
|
const config = join(home, ".cursor/mcp.json");
|
|
await mkdir(dirname(config), { recursive: true });
|
|
const environment = { HOME: home };
|
|
const [command, ...args] = currentClientRegistrationCommand({
|
|
entrypointPath: "/current/rea",
|
|
environment,
|
|
});
|
|
await writeFile(
|
|
config,
|
|
JSON.stringify({ mcpServers: { rea: { command, args } } }),
|
|
);
|
|
// The registration inspection reads the file first; fail only the close of
|
|
// the refresh check's later read of the same file.
|
|
const closeAttempts: FileHandle[] = [];
|
|
let opens = 0;
|
|
let released = false;
|
|
openMock.mockImplementation(async (...openArgs) => {
|
|
const handle = await actualFs.open(...openArgs);
|
|
if (String(openArgs[0]) !== config) return handle;
|
|
opens += 1;
|
|
if (opens === 1) return handle;
|
|
const nativeClose = handle.close.bind(handle);
|
|
vi.spyOn(handle, "close").mockImplementation(async () => {
|
|
closeAttempts.push(handle);
|
|
if (!released) throw new Error("held refresh-check descriptor");
|
|
await nativeClose();
|
|
});
|
|
return handle;
|
|
});
|
|
const resources = new ArtifactResourceScope();
|
|
const execute = vi.fn(async () =>
|
|
ok('{"status":"planned","plannedActions":[]}'),
|
|
);
|
|
try {
|
|
const maintenance = await planIntegrationMaintenance(
|
|
home,
|
|
"/current/rea",
|
|
environment,
|
|
execute,
|
|
{ resources },
|
|
);
|
|
expect(maintenance.status).toBe("unavailable");
|
|
expect(closeAttempts.length).toBeGreaterThan(0);
|
|
// The scope owns the failed close, so its close retries it.
|
|
expect(resources.hasPending).toBe(true);
|
|
await expect(resources.close()).rejects.toThrow();
|
|
released = true;
|
|
await resources.close();
|
|
expect(resources.hasPending).toBe(false);
|
|
expect(closeAttempts.every((handle) => handle.fd === -1)).toBe(true);
|
|
expect(execute).not.toHaveBeenCalled();
|
|
} finally {
|
|
released = true;
|
|
await resources.close();
|
|
}
|
|
});
|