Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

3.7 KB
Raw Blame
/**
 * install-paths.ts — Rule B: installer-managed absolute-path bake helpers.
 *
 * See `CLAUDE.md` → "Spawn-Contract Resolution". Per-IDE config files that
 * claude-mem's own installers write (Cursor, Windsurf, Antigravity CLI, and
 * the MCP-only IDEs: Copilot CLI, Goose, Roo, Warp) MUST bake
 * absolute paths — those hosts perform NO `${CLAUDE_PLUGIN_ROOT}` shell
 * substitution on the `command`/`args` fields they exec. This module is the
 * single source of truth for resolving those absolute paths so each installer
 * does not re-implement (subtly divergent) probing logic.
 *
 * This is install-time resolution only. Runtime resolution (Rule C) lives in
 * `plugin/scripts/bun-runner.js` / `plugin/scripts/version-check.js` and is the
 * safety net behind both Rule A (host-managed shell templates) and Rule B.
 */
import path from 'path';
import { existsSync } from 'fs';
import { MARKETPLACE_ROOT } from '../../shared/paths.js';
import { lookupWindowsCommand } from '../../shared/spawn.js';
import { bunCommonPaths, isExecutableFile } from '../../npx-cli/install/tool-path.js';

function firstExisting(candidates: string[]): string | null {
  for (const candidate of candidates) {
    if (candidate && existsSync(candidate)) {
      return candidate;
    }
  }
  return null;
}

/**
 * Absolute path to the installed plugin root (the directory that contains
 * `scripts/`). Probes, in order: CLAUDE_PLUGIN_ROOT env, PLUGIN_ROOT env,
 * the Claude marketplace cache root, then the current working directory's
 * `plugin/` (repo/dev checkout) and the cwd itself.
 *
 * Returns null when no candidate contains `scripts/` — callers surface a
 * "could not find" install error.
 */
export function getPluginRootAbsolutePath(): string | null {
  const candidates = [
    process.env.CLAUDE_PLUGIN_ROOT,
    process.env.PLUGIN_ROOT,
    path.join(MARKETPLACE_ROOT, 'plugin'),
    path.join(process.cwd(), 'plugin'),
    process.cwd(),
  ].filter((value): value is string => Boolean(value));

  for (const candidate of candidates) {
    const absolute = path.resolve(candidate);
    if (existsSync(path.join(absolute, 'scripts'))) {
      return absolute;
    }
  }
  return null;
}

function resolvePluginScript(scriptName: string): string | null {
  const root = getPluginRootAbsolutePath();
  const candidates = [
    root ? path.join(root, 'scripts', scriptName) : '',
    path.join(MARKETPLACE_ROOT, 'plugin', 'scripts', scriptName),
    path.join(process.cwd(), 'plugin', 'scripts', scriptName),
  ];
  return firstExisting(candidates);
}

/** Absolute path to the bundled MCP server (`mcp-server.cjs`), or null. */
export function getMcpServerAbsolutePath(): string | null {
  return resolvePluginScript('mcp-server.cjs');
}

/** Absolute path to the bundled worker service (`worker-service.cjs`), or null. */
export function getWorkerServiceAbsolutePath(): string | null {
  return resolvePluginScript('worker-service.cjs');
}

/**
 * Absolute path to a Bun runtime. Falls back to the bare `bun` name (resolved
 * via PATH at exec time) when no known install location exists.
 */
export function getBunAbsolutePath(): string {
  const pathResolvedBun = process.platform === 'win32'
    ? lookupWindowsCommand('bun')
    : null;
  const candidates = [
    ...(pathResolvedBun?.toLowerCase().endsWith('.exe') ? [pathResolvedBun] : []),
    ...bunCommonPaths(),
  ];

  return candidates.find(isExecutableFile) ?? 'bun';
}

/**
 * Absolute path to the Node runtime launching this installer. MCP-only
 * integrations register `node <mcp-server.cjs>`; baking `process.execPath`
 * guarantees the same Node that ran the installer is the one that launches the
 * server.
 */
export function getNodeAbsolutePath(): string {
  return process.execPath;
}