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

8.1 KB
Raw Blame
import { spawn, spawnSync, type ChildProcess } from 'child_process';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { homedir } from 'os';
import { dirname, join } from 'path';
import { isCurrentDirectoryCandidate } from '../shared/spawn.js';
import { expandHome } from '../shared/expand-home.js';

export const BUN_PATH_LOOKUP_TIMEOUT_MS = 1500;

interface BunLookupOptions {
  platform?: NodeJS.Platform;
  env?: NodeJS.ProcessEnv;
  homeDirectory?: string;
  exists?: (path: string) => boolean;
  spawnProcess?: typeof spawn;
  timeoutMs?: number;
  /** Working directory where.exe results are filtered against. */
  cwd?: string;
  /**
   * Where to remember a Bun path found through where.exe. Defaults to
   * `<data dir>/run/bun-path` on Windows; null disables the cache.
   */
  cacheFilePath?: string | null;
}

interface PathLookupResult {
  status: number | null;
  stdout: string;
}

function windowsSystem32Executable(executableName: string, env: NodeJS.ProcessEnv): string {
  const systemRoot = env.SystemRoot || env.SYSTEMROOT || env.windir || env.WINDIR || 'C:\\Windows';
  return join(systemRoot, 'System32', executableName);
}

/**
 * Every hook is a separate process, so a Bun path discovered through where.exe
 * is remembered on disk. The next hook reads it back instead of paying another
 * synchronous PATH scan (#6761). The file lives under the owner-controlled
 * data directory, never the project cwd.
 */
function defaultBunPathCacheFilePath(env: NodeJS.ProcessEnv, homeDirectory: string): string {
  const configuredDataDir = typeof env.CLAUDE_MEM_DATA_DIR === 'string' ? env.CLAUDE_MEM_DATA_DIR.trim() : '';
  const dataDirectory = configuredDataDir
    ? expandHome(configuredDataDir, 'win32', homeDirectory)
    : join(homeDirectory, '.claude-mem');
  return join(dataDirectory, 'run', 'bun-path');
}

function readCachedBunPath(
  cacheFilePath: string | null,
  exists: (path: string) => boolean,
  cwd: string
): string | null {
  if (!cacheFilePath) return null;
  try {
    const cachedPath = readFileSync(cacheFilePath, 'utf-8').trim();
    if (!cachedPath || isCurrentDirectoryCandidate(cachedPath, cwd)) return null;
    return exists(cachedPath) ? cachedPath : null;
  } catch {
    return null;
  }
}

function writeCachedBunPath(cacheFilePath: string | null, bunPath: string): void {
  if (!cacheFilePath) return;
  try {
    mkdirSync(dirname(cacheFilePath), { recursive: true, mode: 0o700 });
    writeFileSync(cacheFilePath, `${bunPath}\n`, { mode: 0o600 });
  } catch {
    // A read-only or unwritable data directory only costs the next where.exe.
  }
}

function knownBunPaths(platform: NodeJS.Platform, env: NodeJS.ProcessEnv, homeDirectory: string): string[] {
  const bunInstall = typeof env.BUN_INSTALL === 'string' ? env.BUN_INSTALL.trim() : '';
  const bunEnv = typeof env.BUN === 'string' ? env.BUN.trim() : '';
  const bunPathEnv = typeof env.BUN_PATH === 'string' ? env.BUN_PATH.trim() : '';

  return platform === 'win32'
    ? [
        bunEnv,
        bunPathEnv,
        bunInstall ? join(bunInstall, 'bin', 'bun.exe') : '',
        bunInstall ? join(bunInstall, 'bin', 'bun') : '',
        bunInstall ? join(bunInstall, 'bun.exe') : '',
        join(homeDirectory, '.bun', 'bin', 'bun.exe'),
      ]
    : [
        bunEnv,
        bunPathEnv,
        bunInstall ? join(bunInstall, 'bin', 'bun') : '',
        bunInstall ? join(bunInstall, 'bun') : '',
        join(homeDirectory, '.bun', 'bin', 'bun'),
        '/usr/local/bin/bun',
        '/opt/homebrew/bin/bun',
        '/home/linuxbrew/.linuxbrew/bin/bun',
      ];
}

function runPathLookup(
  command: string,
  args: string[],
  platform: NodeJS.Platform,
  spawnProcess: typeof spawn,
  timeoutMs: number
): Promise<PathLookupResult> {
  return new Promise((resolve) => {
    let child: ChildProcess;
    try {
      child = spawnProcess(command, args, {
        stdio: ['ignore', 'pipe', 'ignore'],
        ...(platform === 'win32' ? { windowsHide: true } : {}),
      });
    } catch {
      resolve({ status: null, stdout: '' });
      return;
    }

    let stdout = '';
    let settled = false;
    const finish = (result: PathLookupResult) => {
      if (settled) return;
      settled = true;
      clearTimeout(timer);
      resolve(result);
    };
    const timer = setTimeout(() => {
      try {
        child.kill();
      } catch {
        // The resolver still needs to finish if the process already exited.
      }
      finish({ status: null, stdout: '' });
    }, timeoutMs);

    child.stdout?.on('data', (chunk: Buffer | string) => {
      stdout += chunk.toString();
    });
    child.once('error', () => finish({ status: null, stdout: '' }));
    child.once('close', (status) => finish({ status, stdout }));
  });
}

/** Resolve a Bun executable without allowing Windows PATH discovery to block a hook indefinitely. */
export async function findBun(options: BunLookupOptions = {}): Promise<string | null> {
  const platform = options.platform ?? process.platform;
  const env = options.env ?? process.env;
  const homeDirectory = options.homeDirectory ?? homedir();
  const exists = options.exists ?? existsSync;
  const knownPaths = knownBunPaths(platform, env, homeDirectory);
  const cwd = options.cwd ?? process.cwd();

  // The Windows launcher runs inside every hook. Check explicit locations first
  // so the usual ~/.bun installation does not synchronously invoke where.exe.
  if (platform === 'win32') {
    for (const bunPath of knownPaths) {
      if (bunPath && exists(bunPath)) return bunPath;
    }

    const cacheFilePath = options.cacheFilePath === undefined
      ? defaultBunPathCacheFilePath(env, homeDirectory)
      : options.cacheFilePath;
    const cachedBunPath = readCachedBunPath(cacheFilePath, exists, cwd);
    if (cachedBunPath) return cachedBunPath;

    const pathCheck = await runPathLookup(
      windowsSystem32Executable('where.exe', env),
      ['bun'],
      platform,
      options.spawnProcess ?? spawn,
      options.timeoutMs ?? BUN_PATH_LOOKUP_TIMEOUT_MS
    );
    if (pathCheck.status === 0 && pathCheck.stdout.trim()) {
      // where.exe prints in the console code page, so a non-ASCII profile path
      // can arrive garbled; keep only lines that exist on disk (#4694). It
      // also searches the current directory first: hooks run inside the user's
      // project, so a bun.exe planted there must never be returned.
      const bunPaths = pathCheck.stdout
        .split(/\r?\n/)
        .map((line) => line.trim())
        .filter(Boolean)
        .filter((line) => !isCurrentDirectoryCandidate(line, cwd))
        .filter((line) => exists(line));
      const firstBunPath = bunPaths.find((line) => {
        const lowerPath = line.toLowerCase();
        return lowerPath.endsWith('bun.exe') || lowerPath.endsWith('bun.cmd');
      });
      const firstBunDir = firstBunPath ? dirname(firstBunPath).toLowerCase() : null;
      const firstInstallPaths = firstBunDir
        ? bunPaths.filter((line) => dirname(line).toLowerCase() === firstBunDir)
        : [];
      const bunExePath = firstInstallPaths.find((line) => line.toLowerCase().endsWith('bun.exe'));
      if (bunExePath) {
        writeCachedBunPath(cacheFilePath, bunExePath);
        return bunExePath;
      }
      const bunCmdPath = firstInstallPaths.find((line) => line.toLowerCase().endsWith('bun.cmd'));
      if (bunCmdPath) {
        writeCachedBunPath(cacheFilePath, bunCmdPath);
        return bunCmdPath;
      }
      // The official installer ships bun.exe only (no bun.cmd shim). Return
      // the resolved absolute path instead of relying on the child's PATH.
      const firstWherePath = bunPaths.find(Boolean);
      if (firstWherePath) {
        writeCachedBunPath(cacheFilePath, firstWherePath);
        return firstWherePath;
      }
    }
    return null;
  }

  // Keep the existing Unix PATH-before-explicit-path precedence.
  const pathCheck = spawnSync('which', ['bun'], {
    encoding: 'utf-8',
    stdio: ['pipe', 'pipe', 'pipe'],
  });
  if (pathCheck.status === 0 && pathCheck.stdout?.trim()) return 'bun';

  for (const bunPath of knownPaths) {
    if (bunPath && exists(bunPath)) return bunPath;
  }
  return null;
}