From a009ca4472fddd5fe71d40b7e7fb36be2ce7945b Mon Sep 17 00:00:00 2001 From: gensokyo-dev Date: Sat, 28 Nov 2026 19:45:47 +0000 Subject: [PATCH] fix(setup): compare the script before rejecting another runtime A registration that starts this installation's own script under another still-installed runtime, such as a source checkout after a Node switch, was foreign because the runtime mismatch was decided first. Runtime and script identity are now evaluated separately: when both Node's lexical and native traversal select the current script, the registration is owned but selects another installation, and setup refreshes it. A spelling only one traversal maps to the current script is unknown. Aube launchers of another runtime use the same rule. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --- src/application/ClientRegistrationIdentity.ts | 39 +++++++++++---- .../setup/registrationOwnershipSetup.test.ts | 30 ++++++++++- .../aubeBinShimRegistration.test.ts | 50 ++++++++++++------- 3 files changed, 89 insertions(+), 30 deletions(-) diff --git a/src/application/ClientRegistrationIdentity.ts b/src/application/ClientRegistrationIdentity.ts index 03fe1a9a5e..19af020c4d 100644 --- a/src/application/ClientRegistrationIdentity.ts +++ b/src/application/ClientRegistrationIdentity.ts @@ -6,7 +6,7 @@ import { readlinkSync, realpathSync, } from "node:fs"; -import { isAbsolute, posix } from "node:path"; +import { isAbsolute, posix, resolve } from "node:path"; import { CURRENT_ENTRYPOINT_PATH, normalizeEntrypointPath, @@ -253,9 +253,29 @@ const entrypointOwnership = ( return evidence === "unknown" ? { identity: "unknown" } : { identity }; }; +/** + * A launcher under another runtime never selects this installation, but its + * script may still be this installation's or another installed REA package's. + * That runtime may follow Node's lexical `..` or native traversal, so both + * spellings must select the current script. + */ +const otherRuntimeScriptOwnership = ( + script: string, + currentCommandPath: string, +): EntrypointOwnership => { + const current = [...new Set([script, resolve(script)])].map( + (spelling) => entrypointIdentity(spelling, currentCommandPath) === "owned", + ); + if (current.every(Boolean)) + return { identity: "owned", installation: "other" }; + if (current.some(Boolean)) return { identity: "unknown" }; + return entrypointOwnership("foreign", script); +}; + /** * A shim that starts another runtime cannot select this installation, but its - * target may still be another installed REA package. Undefined proves nothing. + * target may still be this installation's script or another installed REA + * package. Undefined proves nothing. */ const aubeShimOwnership = ( shim: AubeShimInspection | undefined, @@ -272,10 +292,11 @@ const aubeShimOwnership = ( entrypointIdentity(shim.target, currentCommandPath), shim.target, ); - const evidence = installedReaPackageEvidence(shim.target); - if (evidence === "verified") - return { identity: "owned", installation: "other" }; - return evidence === "unknown" ? { identity: "unknown" } : undefined; + const ownership = otherRuntimeScriptOwnership( + shim.target, + currentCommandPath, + ); + return ownership.identity === "foreign" ? undefined : ownership; }; /** A direct launcher may be a native executable or a Node shebang script. */ @@ -618,12 +639,10 @@ const inspectDirectRegistrationCommand = ( interpreter, process.execPath, ); - // Another runtime does not select this installation, but it may still - // launch an installed REA package that setup can refresh. if (interpreterIdentity !== "owned") return { - ...(currentPathAvailable - ? entrypointOwnership(interpreterIdentity, entrypoint) + ...(currentPathAvailable && interpreterIdentity === "foreign" + ? otherRuntimeScriptOwnership(entrypoint, currentCommandPath) : { identity: "unknown" }), launch: "runtime_script", entrypoint: undefined, diff --git a/tests/acceptance/setup/registrationOwnershipSetup.test.ts b/tests/acceptance/setup/registrationOwnershipSetup.test.ts index 7e069f4ac2..3f6e7335e1 100644 --- a/tests/acceptance/setup/registrationOwnershipSetup.test.ts +++ b/tests/acceptance/setup/registrationOwnershipSetup.test.ts @@ -1,5 +1,5 @@ import { mkdir, readFile, readdir, symlink, writeFile } from "node:fs/promises"; -import { dirname, join } from "node:path"; +import { dirname, join, resolve } from "node:path"; import { expect } from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; @@ -161,6 +161,34 @@ const installPackage = async ( const reaManifest = { name: "rea-agents", bin: { rea: "scripts/rea.mjs" } }; +cliTest( + "setup refreshes this installation's script under another runtime", + async ({ cli }) => { + const home = await createTestTempDirectory("rea-setup-other-runtime-"); + const config = join(home, ".claude.json"); + const runtime = join(home, "old-runtime", "node"); + await mkdir(dirname(runtime), { recursive: true }); + await writeFile(runtime, ""); + // The CLI fixture runs this checkout's script, outside any node_modules. + const original = JSON.stringify({ + mcpServers: { + rea: { command: runtime, args: [resolve("scripts/rea.mjs"), "mcp"] }, + }, + }); + await writeFile(config, original); + const result = await cli.run({ + arguments: [...setup, "--yes"], + cwd: home, + environment: { HOME: home, USERPROFILE: home, CLAUDE_CONFIG_DIR: home }, + }); + expect(result.exitCode, result.stdout).toBe(0); + expect(JSON.parse(await readFile(config, "utf8"))).toMatchObject({ + mcpServers: { rea: { command: process.execPath } }, + }); + expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original); + }, +); + for (const runtime of ["current", "other"] as const) cliTest( `setup refreshes a launcher of another installed REA package (${runtime} runtime)`, diff --git a/tests/boundary/filesystem/aubeBinShimRegistration.test.ts b/tests/boundary/filesystem/aubeBinShimRegistration.test.ts index df285988a7..b378c8b808 100644 --- a/tests/boundary/filesystem/aubeBinShimRegistration.test.ts +++ b/tests/boundary/filesystem/aubeBinShimRegistration.test.ts @@ -7,6 +7,7 @@ import { readClientRegistrationStatuses } from "../../../src/application/ClientR import { classifyClientRegistrationCommand, currentClientRegistrationCommand, + inspectClientRegistrationCommand, } from "../../../src/application/ClientRegistrationIdentity.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; @@ -348,21 +349,36 @@ it entrypoint, "mcp", ]); - expect(classifyClientRegistrationCommand([shim, "mcp"], entrypoint)).toBe( - "foreign", + // A parsed shim that starts another runtime runs REA's script, but never + // this installation; an unparsed marker proves nothing. + expect( + inspectClientRegistrationCommand([shim, "mcp"], entrypoint), + ).toMatchObject( + cause === "marker-only" + ? { identity: "foreign" } + : { identity: "owned", installation: "other" }, ); }, ); -it.skipIf(process.platform === "win32").each([true, false])( - "owns a saved Aube launcher of another runtime only for an installed REA package (manifest=%s)", - async (manifest) => { +it.skipIf(process.platform === "win32").each([ + ["this installation's script", "owned", "owned", "stale"], + ["an installed REA package", "owned", "owned", "stale"], + ["an unverified package", "unknown", "foreign", "unknown"], +] as const)( + "classifies a saved Aube launcher of another runtime whose target is %s", + async (target, saved, direct, state) => { const home = await createTestTempDirectory("rea-aube-other-runtime-"); const { entrypoint, shim } = await installWithMise( join(home, "installs", "rea"), "6.7.0", ); - if (manifest) + const current = + target === "this installation's script" + ? entrypoint + : (await installWithMise(join(home, "other", "rea"), "6.8.0")) + .entrypoint; + if (target === "an installed REA package") await writeFile( join(dirname(dirname(entrypoint)), "package.json"), JSON.stringify({ name: "rea-agents", bin: { rea: "scripts/rea.mjs" } }), @@ -371,17 +387,13 @@ it.skipIf(process.platform === "win32").each([true, false])( const runtime = join(home, "old-node", "node"); await mkdir(dirname(runtime)); await writeFile(runtime, "#!/bin/sh\nexit 44\n", { mode: 0o755 }); - const saved = oldAubeRegistration(shim, runtime, "saved"); - expect(classifyClientRegistrationCommand(saved, entrypoint)).toBe( - manifest ? "owned" : "unknown", - ); - expect(classifyClientRegistrationCommand([shim, "mcp"], entrypoint)).toBe( - manifest ? "owned" : "foreign", - ); - await registerWithClaudeCode(home, saved); - expect(await claudeCodeState(home, entrypoint)).toBe( - manifest ? "stale" : "unknown", + const command = oldAubeRegistration(shim, runtime, "saved"); + expect(classifyClientRegistrationCommand(command, current)).toBe(saved); + expect(classifyClientRegistrationCommand([shim, "mcp"], current)).toBe( + direct, ); + await registerWithClaudeCode(home, command); + expect(await claudeCodeState(home, current)).toBe(state); }, ); @@ -634,9 +646,9 @@ it.skipIf(process.platform === "win32")( const direct = [shim, "mcp"]; // The Node script argument resolves lexically to the installed script. // The shell's earlier -x test resolves its own Node path natively. - expect(classifyClientRegistrationCommand(direct, entrypoint)).not.toBe( - "owned", - ); + expect( + inspectClientRegistrationCommand(direct, entrypoint).installation, + ).not.toBe("current"); await rm(join(lexicalBin, "node")); await symlink(process.execPath, join(nativeBin, "node")); expect(classifyClientRegistrationCommand(direct, entrypoint)).toBe("owned");