Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
4.2 KB
Raw Blame
import { execFileSync, type ExecFileSyncOptionsWithStringEncoding } from './spawn.ts';
import { readFileSync } from 'node:fs';

export type AutopilotLockHolder =
  | { state: 'dead' }
  | { state: 'self' }
  | { state: 'alive-autopilot' }
  | { state: 'alive-foreign' }
  | { state: 'alive-unknown' };

export interface AutopilotLockProbeDeps {
  isPidAlive?: (pid: number) => boolean;
  readProcessCommand?: (pid: number) => string | null;
}

export function isPidAlive(pid: number): boolean {
  if (!Number.isFinite(pid) || pid <= 0) return false;
  try {
    process.kill(pid, 0);
    return true;
  } catch (error: unknown) {
    return (error as NodeJS.ErrnoException).code === 'EPERM';
  }
}

export interface ProcessCommandProbeDeps {
  /** Injected for tests; defaults to fs.readFileSync of /proc/<pid>/cmdline. */
  readCmdlineFile?: (path: string) => Buffer | string;
  /** Injected for tests; defaults to process.platform. */
  platform?: NodeJS.Platform;
  /** Injected for tests; defaults to child_process.execFileSync (ps / powershell). */
  execFile?: (file: string, args: string[], options: ExecFileSyncOptionsWithStringEncoding) => string;
}

/**
 * Best-effort process command lookup. On Linux, /proc/<pid>/cmdline is read
 * first (no subprocess, works even when `ps` is unavailable or restricted —
 * e.g. minimal containers), falling back to `ps -o args=` elsewhere (#4300).
 * Windows has neither /proc nor `ps`, so it asks CIM via powershell instead
 * (#4563) — without this every holder classified as alive-unknown there.
 */
export function readProcessCommand(pid: number, deps: ProcessCommandProbeDeps = {}): string | null {
  if (!Number.isFinite(pid) || pid <= 0) return null;
  const exec = deps.execFile ?? execFileSync;
  if ((deps.platform ?? process.platform) === 'win32') {
    // Get-CimInstance, not wmic (removed on Win11 24H2). pid is validated
    // finite/positive above, so the WQL filter is injection-safe. Return
    // early: the POSIX probes below are guaranteed to throw on Windows.
    try {
      const out = exec('powershell.exe', [
        '-NoProfile', '-NonInteractive', '-Command',
        `Get-CimInstance Win32_Process -Filter "ProcessId=${pid}" | Select-Object -ExpandProperty CommandLine`,
      ], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: 5000, windowsHide: true }).trim();
      return out.length > 0 ? out : null;
    } catch {
      return null;
    }
  }
  const readCmdline = deps.readCmdlineFile ?? readFileSync;
  try {
    const raw = readCmdline(`/proc/${pid}/cmdline`);
    // argv is NUL-separated with a trailing NUL; normalize to a space-joined line.
    const cmd = raw.toString().split('\0').filter((part) => part.length > 0).join(' ').trim();
    if (cmd.length > 0) return cmd;
  } catch {
    // Not Linux (no /proc) or unreadable — fall through to ps.
  }
  try {
    const out = exec('ps', ['-p', String(pid), '-o', 'args='], {
      encoding: 'utf8',
      stdio: ['ignore', 'pipe', 'ignore'],
      timeout: 1000,
    }).trim();
    return out.length > 0 ? out : null;
  } catch {
    return null;
  }
}

export function looksLikeGbrainAutopilotCommand(command: string): boolean {
  const normalized = command.replace(/\\/g, '/').trim();
  if (!/(^|\s)autopilot(\s|$)/i.test(normalized)) return false;
  // Win32_Process.CommandLine quotes executables under paths with spaces: `"C:/Program Files/gbrain/gbrain.exe" autopilot`.
  if (/(^|[\/\s])"?gbrain(?:\.exe)?"?(\s|$)/i.test(normalized)) return true;
  return /(^|[\/\s])["']?cli\.(?:ts|js|mjs)["']?(\s|$)/i.test(normalized);
}

export function classifyAutopilotLockHolder(
  pid: number,
  currentPid: number = process.pid,
  deps: AutopilotLockProbeDeps = {},
): AutopilotLockHolder {
  if (!Number.isFinite(pid) || pid <= 0) return { state: 'dead' };
  if (pid === currentPid) return { state: 'self' };

  const probeAlive = deps.isPidAlive ?? isPidAlive;
  if (!probeAlive(pid)) return { state: 'dead' };

  const probeCommand = deps.readProcessCommand ?? readProcessCommand;
  const command = probeCommand(pid);
  if (command === null) return { state: 'alive-unknown' };
  return looksLikeGbrainAutopilotCommand(command)
    ? { state: 'alive-autopilot' }
    : { state: 'alive-foreign' };
}