import { lstatSync, readlinkSync } from "node:fs"; import { homedir } from "node:os"; import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; import { realPath } from "../hub/project.ts"; export interface InstallPathOptions { env?: NodeJS.ProcessEnv; home?: string; globalFlag?: "-g" | "--global"; } // Contract: #1245 limits lexical symlink inspection to eight hops; unknown paths use the resolved fallback. const MAX_LINK_HOPS = 8; const samePath = (left: string, right: string): boolean => { try { return realPath(left) === realPath(right); } catch { return false; } }; /** Follow only the leaf links, preserving lexical parent aliases such as current/. */ function lexicalTarget(entry: string): string | undefined { let link = resolve(entry); const seen = new Set(); try { for (let hops = 0; hops <= MAX_LINK_HOPS; hops++) { if (seen.has(link)) return; seen.add(link); if (!lstatSync(link).isSymbolicLink()) return link; if (hops === MAX_LINK_HOPS) return; link = resolve(dirname(link), readlinkSync(link)); } } catch { /* Missing, unreadable and cyclic aliases are not evidence of this install. */ } } /** The PATH entry's canonical target must also be a CLI entry, not just a lookalike parent. */ function pathPackageRoot(invocation: string): string | undefined { const target = lexicalTarget(invocation); if (!target?.endsWith(`${sep}src${sep}cli${sep}main.js`)) return; try { const canonical = realPath(invocation); if (!canonical.endsWith(`${sep}src${sep}cli${sep}main.js`)) return; const candidate = dirname(dirname(dirname(target))); return samePath(candidate, dirname(dirname(dirname(canonical)))) ? candidate : undefined; } catch { return; } } /** Bun resolves argv[1], Bun.main and import.meta.dir, through both bun and a shebang (native probe, #1245). */ export function stablePackageRoot(root: string, options: { invocation?: string } = {}): string { const invocation = options.invocation ?? Bun.which("ahub", { PATH: process.env.PATH }); const candidate = invocation ? pathPackageRoot(invocation) : undefined; return candidate && samePath(candidate, root) ? candidate : root; } export function stableBunRuntime(options: { runtime?: string; pathRuntime?: string } = {}): string { const runtime = options.runtime ?? process.execPath, candidate = options.pathRuntime ?? Bun.which("bun", { PATH: process.env.PATH }); return candidate && lexicalTarget(candidate) && samePath(candidate, runtime) ? candidate : runtime; } /** The package root of the `ahub` on PATH, when it is a proven CLI entry (#1385). */ export function globalCliRoot(env: NodeJS.ProcessEnv = process.env): string | undefined { const invocation = Bun.which("ahub", { PATH: env.PATH ?? "" }); return invocation ? pathPackageRoot(invocation) : undefined; } /** Advice only; a staged daemon upgrade does not update the person's global CLI installation. */ export function globalCliUpdateStep(version?: string, options: InstallPathOptions = {}): string { const env = options.env ?? process.env; const root = globalCliRoot(env); const generic = `update the global CLI${version ? ` to ${version}` : ""} the way it was installed`; if (!root) return generic; const global = join(env.BUN_INSTALL || join(options.home ?? homedir(), ".bun"), "install/global/node_modules"); let bunGlobal = false; try { const within = relative(realPath(global), realPath(root)); bunGlobal = within !== "" && within !== ".." && !within.startsWith(`..${sep}`) && !isAbsolute(within); } catch { /* An unproven Bun prefix must not suggest updating a different CLI. */ } if (bunGlobal) return `run \`bun add ${options.globalFlag ?? "-g"} @staix/agent-hub${version ? `@${version}` : ""}\``; return `${generic} (running from ${root})`; }