Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion src/__tests__/unit/agents/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
readdirSync,
readFileSync,
rmSync,
symlinkSync,
writeFileSync
} from "node:fs"
import { tmpdir } from "node:os"
Expand All @@ -21,7 +22,8 @@ import {
disconnectAgents,
type ExecResult,
execRunner,
qualithmServer
qualithmServer,
stableExecutable
} from "../../../agents/connect.ts"

const QUALITHM = "/opt/qualithm/bin/qualithm"
Expand Down Expand Up @@ -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()
Expand Down
56 changes: 53 additions & 3 deletions src/agents/connect.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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)
}
}
Expand Down