diff --git a/src/application/McpClientRegistration.layers.test.ts b/src/application/McpClientRegistration.layers.test.ts index 576fd823ff..21c3cbbe6e 100644 --- a/src/application/McpClientRegistration.layers.test.ts +++ b/src/application/McpClientRegistration.layers.test.ts @@ -2,14 +2,16 @@ import { registerMcpClientOnHost } from "../../tests/fixtures/mcpRegistration.js import { HOST_PLATFORM } from "../platform/HostPlatform.js"; import { lstat, mkdir, readFile, writeFile } from "node:fs/promises"; import { dirname, join } from "node:path"; -import { afterEach, beforeEach, expect, it } from "vitest"; +import { afterEach, beforeEach, expect, it, vi } from "vitest"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; +import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; import { ArtifactResourceScope } from "../resources/ArtifactResourceScope.js"; import { effectiveClientServer, parseClientConfiguration, } from "./ClientConfigurationDocument.js"; +import { ClientConfigurationPublication } from "./ClientConfigurationPublication.js"; import { mergeClientConfigurationDocuments } from "./ClientConfigurationFiles.js"; import { mcpClientRegistrationProfile, @@ -21,6 +23,7 @@ beforeEach(() => { resources = new ArtifactResourceScope(); }); afterEach(async () => { + vi.restoreAllMocks(); await resources.close(); }); const launcher = { command: "/fixture/server path", args: ["", "--mcp"] }; @@ -135,6 +138,48 @@ it("refuses a higher Kilo file launcher before creating the lower selected targe }); }); +it("reports the retained backup when a higher layer invalidates publication", async () => { + const root = await createTestTempDirectory( + "rea-registration-layer-readback-", + ); + const target = join(root, "project", "kilo.json"); + const higher = join(root, "higher", "kilo.json"); + const original = await write(target, { theme: "target" }); + const higherDocument = { + mcp: { rea: { type: "local", command: ["higher-server"] } }, + }; + const publish = ClientConfigurationPublication.write; + let higherText: string | undefined; + vi.spyOn(ClientConfigurationPublication, "write").mockImplementation( + async (...args) => { + const publication = await publish(...args); + higherText = await write(higher, higherDocument); + return publication; + }, + ); + const result = await registerMcpClientOnHost( + clientFor(target, [target, higher]), + launcher, + undefined, + resources, + ); + if (result.ok) + throw new Error("Registration should refuse the changed launcher"); + const backup = `${target}.rea.backup`; + expect(result.error.message).toContain( + "higher-priority configuration overrides", + ); + expect(projectAnalysisError(result.error).details?.diagnostics).toMatchObject( + { + settings_path: target, + backup_path: backup, + }, + ); + expect(await readFile(target, "utf8")).toBe(original); + expect(await readFile(backup, "utf8")).toBe(original); + expect(await readFile(higher, "utf8")).toBe(higherText); +}); + it("refuses a conflicting KILO_CONFIG_CONTENT launcher without mutating the inline source or creating a target", async () => { const root = await createTestTempDirectory( "rea-registration-inline-conflict-", diff --git a/src/application/McpClientRegistration.ts b/src/application/McpClientRegistration.ts index 92fbb513d4..b14766b000 100644 --- a/src/application/McpClientRegistration.ts +++ b/src/application/McpClientRegistration.ts @@ -255,7 +255,11 @@ const registrationFailure = async ( }, ): Promise => { const { backupPath, configured } = progress; - if (cause instanceof ConfigurationError) return cause; + if (cause instanceof ConfigurationError) { + if (backupPath !== undefined) + cause.retainPrimaryDiagnostics({ backup_path: backupPath }); + return cause; + } const cleanup = isClientConfigurationCleanupFailure(cause) ? await retryClientConfigurationCleanup(cause, resources) : undefined;