diff --git a/src/application/ClientConfigurationFiles.ts b/src/application/ClientConfigurationFiles.ts index 7c2fda145b..02ea205e16 100644 --- a/src/application/ClientConfigurationFiles.ts +++ b/src/application/ClientConfigurationFiles.ts @@ -15,7 +15,10 @@ import { clientProjectConfigurationPaths, type SetupClient, } from "./SupportedClients.js"; -import { clientRegistrationEnvironment } from "./ClientRegistrationEnvironment.js"; +import { + ClientRegistrationEnvironmentShapeError, + clientRegistrationEnvironment, +} from "./ClientRegistrationEnvironment.js"; import type { ArtifactResourceScope } from "../resources/index.js"; import type { ConfigurationError } from "../domain/configurationErrors.js"; import { mergeEnvironmentOverrides } from "../process/snapshotEnvironment.js"; @@ -1157,20 +1160,22 @@ const contributingEnvironmentSource = ( ): ClientConfigurationSource | undefined => { const normalize = (name: string): string => platform === "win32" ? name.toUpperCase() : name; - const rejected = new Set( - error.settings.flatMap(({ setting }) => { - const parts = setting.split("."); - return [normalize(setting), normalize(parts.at(-1) ?? setting)]; - }), - ); + const keys = + error instanceof ClientRegistrationEnvironmentShapeError + ? error.invalidKeys + : error.settings.map(({ setting }) => setting); + const rejected = new Set(keys.map(normalize)); + const invalidMap = + error instanceof ClientRegistrationEnvironmentShapeError && + keys.length === 0; return declarations.findLast(({ source, entry }) => { if (!isRecord(entry) || !Object.hasOwn(entry, source.environmentKey)) return false; + if (invalidMap) return true; const environment = entry[source.environmentKey]; return ( - rejected.has(normalize(source.environmentKey)) || - (isRecord(environment) && - Object.keys(environment).some((key) => rejected.has(normalize(key)))) + isRecord(environment) && + Object.keys(environment).some((key) => rejected.has(normalize(key))) ); })?.source; }; diff --git a/src/application/ClientRegistrationEnvironment.ts b/src/application/ClientRegistrationEnvironment.ts index 7a8fb23486..a724eaa8ba 100644 --- a/src/application/ClientRegistrationEnvironment.ts +++ b/src/application/ClientRegistrationEnvironment.ts @@ -85,6 +85,25 @@ const environmentSchema = clientConfigurationRecordSchema.transform( }, ); +/** Invalid recorded shape, with literal environment keys kept for source attribution. */ +export class ClientRegistrationEnvironmentShapeError extends ConfigurationError { + constructor( + field: string, + readonly invalidKeys: readonly string[], + ) { + super("Invalid REA server environment", { + settings: (invalidKeys.length === 0 + ? [field] + : invalidKeys.map((key) => `${field}.${key}`) + ).map((setting) => ({ + setting, + constraint: + "Server environment must be an object containing only string values", + })), + }); + } +} + /** * Clients that expand `$VAR` and `${VAR}` in a recorded `env` value from the * environment the client itself runs with. @@ -164,17 +183,12 @@ export const clientRegistrationEnvironmentDetails = ( const existing = environmentSchema.safeParse(raw === undefined ? {} : raw); if (!existing.success) return err( - new ConfigurationError("Invalid REA server environment", { - settings: existing.error.issues.map((issue) => ({ - setting: [ - ...(source.nested ? ["transport"] : []), - field.value, - ...issue.path, - ].join("."), - constraint: - "Server environment must be an object containing only string values", - })), - }), + new ClientRegistrationEnvironmentShapeError( + source.nested ? `transport.${field.value}` : field.value, + existing.error.issues.flatMap(({ path: [key] }) => + typeof key === "string" ? [key] : [], + ), + ), ); // Validate known settings with the runtime's parser, but retain the original // string overrides, including unknown client-specific keys and no defaults. diff --git a/tests/boundary/filesystem/clientConfigurationOverrideOrigin.test.ts b/tests/boundary/filesystem/clientConfigurationOverrideOrigin.test.ts index a76fd5e8dd..4419955408 100644 --- a/tests/boundary/filesystem/clientConfigurationOverrideOrigin.test.ts +++ b/tests/boundary/filesystem/clientConfigurationOverrideOrigin.test.ts @@ -6,87 +6,63 @@ import { readClientConfigurationFiles } from "../../../src/application/ClientCon import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { supportedClientRegistrationProfile } from "../../fixtures/clientRegistrationProfile.js"; -it("attributes case-folded Windows overrides without disclosing values", async () => { - const root = await createTestTempDirectory("rea-config-override-origin-"); - const configPath = join(root, "client.json"); - const client = { - name: "cursor", - registrationProfile: supportedClientRegistrationProfile("cursor"), - format: "json", - configPath, - } as const; - const result = await readClientConfigurationFiles( - client, - undefined, - "win32", - { - writeOverrides: { rea_asar_max_header_bytes: "0" }, - }, - ); - expect(result).toMatchObject({ - ok: false, - error: { - detail: expect.stringContaining( - "launch overrides (REA_ASAR_MAX_HEADER_BYTES)", - ), - }, - }); -}); - -it("keeps a malformed stored environment attributed to its file", async () => { - const root = await createTestTempDirectory("rea-config-file-origin-"); - const configPath = join(root, "client.json"); - await writeFile( - configPath, - JSON.stringify({ - mcpServers: { - rea: { - command: "rea", - args: ["mcp"], - env: { HOPPER_LOADER_ARGS_JSON: "not-json" }, - }, - }, - }), - ); - const result = await readClientConfigurationFiles( - { - name: "cursor", - registrationProfile: supportedClientRegistrationProfile("cursor"), - format: "json", - configPath, - }, - undefined, - HOST_PLATFORM.platform, - ); - expect(result).toMatchObject({ - ok: false, - error: { detail: expect.stringContaining(`Configuration ${configPath}`) }, - }); - if (!result.ok) expect(result.error.detail).not.toContain("not-json"); -}); - -it.each([ +it.each<{ + name: string; + platform?: NodeJS.Platform; + stored?: Record; + overrides?: Record; + reported: string; + setting?: string; + suppressed: string; +}>([ { + name: "case-folded Windows override for a missing file", + platform: "win32", + overrides: { rea_asar_max_header_bytes: "invalid-proposed-bytes" }, + reported: "launch overrides (REA_ASAR_MAX_HEADER_BYTES)", + suppressed: "invalid-proposed-bytes", + }, + { + name: "malformed stored environment", + stored: { HOPPER_LOADER_ARGS_JSON: "invalid-stored-bytes" }, + reported: "has an invalid REA server environment", + suppressed: "invalid-stored-bytes", + }, + { + name: "invalid combination of otherwise valid settings", stored: { REA_GHIDRA_LANGUAGE_ID: "x86:LE:64:default" }, overrides: { REA_GHIDRA_PROJECT_PATH: "/tmp/selected-project.gpr" }, - reported: "REA_GHIDRA_LANGUAGE_ID", + setting: "REA_GHIDRA_LANGUAGE_ID", + reported: "check the client configuration and proposed launch overrides", + suppressed: "/tmp/selected-project.gpr", }, { - stored: { HOPPER_LOADER_ARGS_JSON: "invalid-json" }, + name: "malformed stored environment beside valid overrides", + stored: { HOPPER_LOADER_ARGS_JSON: "invalid-combined-bytes" }, overrides: { REA_ASAR_MAX_HEADER_BYTES: "1024" }, - reported: "HOPPER_LOADER_ARGS_JSON", + setting: "HOPPER_LOADER_ARGS_JSON", + reported: "check the client configuration and proposed launch overrides", + suppressed: "invalid-combined-bytes", }, ])( - "recommends checking both sources when the reported setting is not an override: $reported", - async ({ stored, overrides, reported }) => { - const root = await createTestTempDirectory("rea-config-merged-origin-"); + "attributes $name without disclosing its value", + async ({ + stored, + overrides, + reported, + platform = HOST_PLATFORM.platform, + setting = reported, + suppressed, + }) => { + const root = await createTestTempDirectory("rea-config-diagnostic-origin-"); const configPath = join(root, "client.json"); - await writeFile( - configPath, - JSON.stringify({ - mcpServers: { rea: { command: "rea", args: ["mcp"], env: stored } }, - }), - ); + if (stored !== undefined) + await writeFile( + configPath, + JSON.stringify({ + mcpServers: { rea: { command: "rea", args: ["mcp"], env: stored } }, + }), + ); const result = await readClientConfigurationFiles( { name: "cursor", @@ -95,17 +71,13 @@ it.each([ configPath, }, undefined, - process.platform, - { writeOverrides: overrides }, + platform, + overrides === undefined ? undefined : { writeOverrides: overrides }, ); if (result.ok) throw new Error("Expected rejected environment"); + expect(result.error.path).toBe(configPath); expect(result.error.detail).toContain(reported); - expect(result.error.detail).toContain( - "check the client configuration and proposed launch overrides", - ); - expect(result.error.detail).toContain( - "Check the launch environment and any existing registration settings", - ); - expect(result.error.detail).not.toContain("invalid-json"); + expect(result.error.detail).toContain(setting); + expect(result.error.detail).not.toContain(suppressed); }, ); diff --git a/tests/boundary/filesystem/openCodeProjectConfiguration.test.ts b/tests/boundary/filesystem/openCodeProjectConfiguration.test.ts index 67cc52452b..cb7dbaa0d0 100644 --- a/tests/boundary/filesystem/openCodeProjectConfiguration.test.ts +++ b/tests/boundary/filesystem/openCodeProjectConfiguration.test.ts @@ -168,6 +168,31 @@ it.each(["legacy", "native"] as const)( }, ); +it("attributes a dotted environment key to its effective OpenCode source", async () => { + const { client, projectDirectory, read } = await fixture(); + const upper = join(projectDirectory, "opencode.json"); + const key = "CALLER.VALUE"; + await writeConfiguration(client.configPath, { + mcp: { rea: { ...localServer("user"), environment: { [key]: 17 } } }, + }); + await writeConfiguration(upper, { + mcp: { + rea: { ...localServer("project"), environment: { VALUE: "unrelated" } }, + }, + }); + const result = await read(); + expect(result).toMatchObject({ + ok: false, + error: { + kind: "malformed", + path: client.configPath, + detail: expect.stringContaining("environment.CALLER.VALUE"), + }, + }); + if (result.ok) throw new Error("Expected invalid environment"); + expect(result.error.scope).toBeUndefined(); +}); + it("keeps OpenCode activation tied to the authored row dialect", async () => { const { client, projectDirectory, read } = await fixture(); const project = join(projectDirectory, "opencode.json");