diff --git a/src/__tests__/unit/agents/connect.test.ts b/src/__tests__/unit/agents/connect.test.ts index 2a0cb47..353f055 100644 --- a/src/__tests__/unit/agents/connect.test.ts +++ b/src/__tests__/unit/agents/connect.test.ts @@ -6,6 +6,7 @@ import { readdirSync, readFileSync, rmSync, + symlinkSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" @@ -21,7 +22,8 @@ import { disconnectAgents, type ExecResult, execRunner, - qualithmServer + qualithmServer, + stableExecutable } from "../../../agents/connect.ts" const QUALITHM = "/opt/qualithm/bin/qualithm" @@ -185,6 +187,60 @@ describe("qualithmServer", () => { }) }) +describe("stableExecutable", () => { + function cellar(): { node: string; bin: string; other: string } { + const node = join(root, "Cellar", "node", "26.13.1_2", "bin", "node") + put(node, "") + const bin = join(root, "bin") + const other = join(root, "other") + mkdirSync(bin, { recursive: true }) + mkdirSync(other, { recursive: true }) + symlinkSync(node, join(bin, "node")) + return { node, bin, other } + } + + it("names the link on PATH instead of the versioned Cellar binary", () => { + const { node, bin, other } = cellar() + writeFileSync(join(other, "node"), "") + const path = ["", "relative", other, bin].join(":") + const found = stableExecutable(node, path, "darwin") + expect(found).toBe(join(bin, "node")) + expect(found).not.toContain("Cellar") + }) + + it("keeps the running path when nothing on PATH links to it", () => { + const { node, other } = cellar() + expect(stableExecutable(node, other, "darwin")).toBe(node) + expect(stableExecutable(node, undefined, "darwin")).toBe(node) + }) + + it("splits a Windows PATH on semicolons", () => { + const real = (p: string): string => { + if (p === "C:\\nodejs\\node.exe" || p === "C:\\link\\node.exe") { + return "C:\\nodejs\\node.exe" + } + throw new Error("missing") + } + const stable = stableExecutable("C:\\nodejs\\node.exe", "C:\\link;C:\\nodejs", "win32", real) + expect(stable).toBe("C:\\link\\node.exe") + }) + + it("rewrites an entry that pins the Cellar path", async () => { + const f = fake({ + desktop: true, + server: { command: "/opt/homebrew/bin/node", args: [QUALITHM, "mcp"] } + }) + const old = { + command: "/opt/homebrew/Cellar/node/26.13.1_2/bin/node", + args: [QUALITHM, "mcp"] + } + put(f.file(...DESKTOP), JSON.stringify({ mcpServers: { qualithm: old } })) + const result = await connectAgents(f.host, { dryRun: false }) + expect(result.agents[0]).toMatchObject({ status: "connected", before: old }) + expect(readFileSync(f.file(...DESKTOP), "utf8")).not.toContain("Cellar") + }) +}) + describe("detection", () => { it("reports every agent as not installed on a bare machine", async () => { const { host } = fake() diff --git a/src/agents/connect.ts b/src/agents/connect.ts index 9479a8a..4418612 100644 --- a/src/agents/connect.ts +++ b/src/agents/connect.ts @@ -1,7 +1,15 @@ import { execFile } from "node:child_process" -import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs" +import { + existsSync, + mkdirSync, + readFileSync, + realpathSync, + renameSync, + statSync, + writeFileSync +} from "node:fs" import { homedir } from "node:os" -import { basename, dirname, join, resolve } from "node:path" +import { basename, dirname, join, posix, resolve, win32 } from "node:path" /** The name every agent knows the qualithm MCP server by. */ export const SERVER_NAME = "qualithm" @@ -98,6 +106,45 @@ export function qualithmServer( : { command: execPath, args: ["mcp"] } } +/** + * A path to `execPath` that survives the runtime's upgrades: the first link on `PATH` with the same + * name that resolves to it, such as Homebrew's `/opt/homebrew/bin/node` for the versioned Cellar + * binary that `process.execPath` names. Without one, `execPath` itself. + * + * @param execPath - the running executable, already resolved + * @param path - the `PATH` to search + * @param platform - the OS, which sets the `PATH` separator + * @param realpath - resolves links; a path it can't resolve is skipped + * @returns the path to write into an agent's config + */ +export function stableExecutable( + execPath: string, + path: string | undefined, + platform: NodeJS.Platform, + realpath: (path: string) => string = realpathSync +): string { + const resolveOrNull = (p: string): string | null => { + try { + return realpath(p) + } catch { + return null + } + } + const target = resolveOrNull(execPath) ?? execPath + const paths = platform === "win32" ? win32 : posix + const name = paths.basename(execPath) + for (const dir of (path ?? "").split(paths.delimiter)) { + if (dir === "" || !paths.isAbsolute(dir)) { + continue + } + const candidate = paths.join(dir, name) + if (candidate !== execPath && resolveOrNull(candidate) === target) { + return candidate + } + } + return execPath +} + /** * Runs a program and collects what it printed, never throwing: a spawn failure exits 1. * @@ -131,7 +178,10 @@ export function defaultAgentHost(): AgentHost { home, env: process.env, applications: ["/Applications", join(home, "Applications")], - server: qualithmServer(process.execPath, process.argv[1]), + server: qualithmServer( + stableExecutable(process.execPath, process.env.PATH, process.platform), + process.argv[1] + ), exec: execRunner(process.platform) } }