Skip to content

Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA

TypeScript
10.3 KB
Raw Blame
import * as fs from 'node:fs';
import * as path from 'node:path';
import { spawnSync } from 'node:child_process';
import { isProcessAlive } from '../../browse/src/error-handling';
import { readPidCmdline, readPidStartTime } from '../../browse/src/xvfb';
import { isAgentRecordGone, isOurAgent, readAgentRecord } from '../../browse/src/terminal-agent-control';

export async function stopQaOnlyBrowser(directory: string, timeoutMs: number): Promise<void> {
  if (!Number.isFinite(timeoutMs) || timeoutMs <= 100) throw new Error('QA-only browser cleanup: no settlement budget remains; retaining fixture');
  // An absolute deadline keeps worker startup inside its own budget, so it reports its reason before the kill.
  const worker = Bun.spawn([process.execPath, import.meta.path, directory, String(Date.now() + timeoutMs - 100)], {
    stdout: 'ignore', stderr: 'pipe',
  });
  let timedOut = false;
  const timer = setTimeout(() => { timedOut = true; worker.kill('SIGKILL'); }, timeoutMs);
  const stderr = new Response(worker.stderr).text();
  try {
    const code = await worker.exited;
    const error = await stderr;
    if (timedOut) throw new Error('QA-only browser cleanup: owned worker settlement deadline exceeded; retaining fixture');
    if (code !== 0) throw new Error(error.trim() || `QA-only browser cleanup: worker exited ${code}; retaining fixture`);
  } finally { clearTimeout(timer); }
}

const TERMINAL_PUBLISHED = Symbol('terminal published during identification');

async function settleOwnedBrowser(directory: string, deadlineEpochMs: number): Promise<void> {
  const started = performance.now();
  const timeoutMs = deadlineEpochMs - Date.now();
  const deadline = started + timeoutMs;
  let pending = ['identity verification'];
  const fail = (message: string): never => { throw new Error(`QA-only browser cleanup: ${message}`); };
  const remaining = () => {
    const ms = Math.floor(deadline - performance.now());
    if (ms <= 0) fail(`owned process settlement deadline exceeded (${pending.join(', ')}); retaining fixture`);
    return ms;
  };
  if (fs.lstatSync(directory).isSymbolicLink()) fail('fixture directory is a link');
  const root = fs.realpathSync(directory);
  const stateDir = path.join(root, '.gstack');
  if (!fs.existsSync(stateDir)) return;
  if (fs.lstatSync(stateDir).isSymbolicLink()) fail('state directory is a link');
  const stateFile = path.join(stateDir, 'browse.json');
  const agentFile = path.join(stateDir, 'terminal-agent-pid');
  for (const file of [stateFile, agentFile]) {
    if (fs.existsSync(file) && !fs.lstatSync(file).isFile()) fail('state record is not a regular file');
  }
  if (!fs.existsSync(stateFile)) {
    if (fs.existsSync(agentFile)) fail('terminal record has no daemon state');
    return;
  }
  const raw = fs.readFileSync(stateFile, 'utf8');
  const state = JSON.parse(raw);
  if (!Number.isSafeInteger(state.pid) || state.pid <= 1
    || typeof state.instanceId !== 'string' || !state.instanceId) fail('invalid daemon identity');
  const agentRaw = fs.existsSync(agentFile) ? fs.readFileSync(agentFile, 'utf8') : undefined;
  const agent = readAgentRecord(stateDir);
  if (agent?.pid === 0) {
    // spawnTerminalAgent holds a pid-0 reservation for the instant between spawn and its real record.
    await Bun.sleep(Math.min(25, remaining()));
    throw TERMINAL_PUBLISHED;
  }
  const processCwd = (pid: number): string => {
    if (process.platform === 'linux') {
      try { return fs.realpathSync(`/proc/${pid}/cwd`); } catch { return ''; }
    }
    const result = spawnSync('lsof', ['-a', '-p', String(pid), '-d', 'cwd', '-Fn'], {
      encoding: 'utf8', timeout: Math.min(1000, remaining()),
    });
    return result.error || result.status !== 0 ? '' : result.stdout.split('\n').find(line => line.startsWith('n'))?.slice(1) ?? '';
  };
  if (!agentRaw || !agent) {
    // A headless daemon starts its terminal agent only on its first watchdog tick (60 s by default), so a record that
    // was never written is not a lost agent. Settle without one only when no agent of this fixture is running.
    const matches = spawnSync('pgrep', ['-f', '--', '--agent-gen='], { encoding: 'utf8', timeout: Math.min(1000, remaining()) });
    if (matches.error || ![0, 1].includes(matches.status ?? -1)) fail('terminal agent identities are unavailable');
    const fixtureAgents = matches.stdout.split('\n').map(Number).filter(pid => {
      if (!Number.isSafeInteger(pid) || pid <= 1 || !/terminal-agent/.test(readPidCmdline(pid))) return false;
      const cwd = processCwd(pid);
      if (!cwd && isProcessAlive(pid) && /terminal-agent/.test(readPidCmdline(pid))) fail(`terminal agent ${pid} has no readable working directory`);
      return cwd === root;
    });
    const port = fs.existsSync(path.join(stateDir, 'terminal-port'));
    if (agentRaw !== undefined || port || fixtureAgents.length) {
      // Qa-only-no-fix (census 41962523474) failed here once with nothing else to go on; name what was there instead.
      const log = path.join(stateDir, 'browse-daemon.log');
      const agentLines = fs.existsSync(log) && fs.lstatSync(log).isFile()
        ? fs.readFileSync(log, 'utf8').split('\n').filter(line => /terminal-agent/i.test(line)).slice(-3).map(line => line.slice(0, 160)) : [];
      fail(`terminal identity is unavailable (record ${agentRaw === undefined ? 'absent' : `unparsed, ${agentRaw.length} bytes`}; `
        + `terminal-port ${port ? 'present' : 'absent'}; running fixture agents: ${fixtureAgents.length ? fixtureAgents.join(', ') : 'none'}; `
        + `daemon ${state.pid} ${isProcessAlive(state.pid) ? 'alive' : 'exited'}, state written ${Math.round((Date.now() - fs.statSync(stateFile).mtimeMs) / 1000)}s ago; `
        + `daemon log terminal-agent lines: ${agentLines.length ? agentLines.join(' | ') : 'none'})`);
    }
  }
  if (!isProcessAlive(state.pid)) fail('daemon exited before its owned processes could be identified');
  const daemonStart = readPidStartTime(state.pid);
  const command = readPidCmdline(state.pid);
  if (!daemonStart || !(typeof state.serverPath === 'string' && command.includes(state.serverPath)
    || command.includes('--server') && /browse/.test(command))) fail('daemon identity is unavailable');
  const cwd = processCwd(state.pid);
  if (!cwd) fail('daemon working directory is unavailable');
  if (cwd !== root) fail('daemon belongs to another fixture');
  if (agent && (agent.ownerPid !== state.pid || agent.ownerStartTime !== daemonStart
    || !isAgentRecordGone(agent) && !isOurAgent(agent, state.pid))) fail('terminal ownership is unconfirmed');
  const processes = spawnSync('ps', ['-eo', 'pid=,ppid='], {
    encoding: 'utf8', timeout: Math.min(1000, remaining()),
  });
  if (processes.error || processes.status !== 0) fail('owned child identities are unavailable');
  const rows = processes.stdout.trim().split('\n').map(line => line.trim().split(/\s+/).map(Number));
  const descendants = new Set<number>([state.pid]);
  for (let previous = 0; previous !== descendants.size;) {
    previous = descendants.size;
    for (const [pid, parent] of rows) if (descendants.has(parent)) descendants.add(pid);
  }
  const chromium = [...descendants].filter(pid => pid !== state.pid && /chrom|headless_shell/i.test(readPidCmdline(pid)))
    .map(pid => ({ pid, start: readPidStartTime(pid) }));
  if (!chromium.length || chromium.some(child => !child.start)) fail('Chromium identity is unavailable');
  if (state.chromiumPid !== undefined && !chromium.some(child => child.pid === state.chromiumPid
    && child.start === state.chromiumStartTime)) fail('Chromium ownership is unconfirmed');
  const current = (file: string) => {
    try { return fs.readFileSync(file, 'utf8'); }
    catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') return undefined; throw error; }
  };
  const unchanged = () => {
    const state = current(stateFile);
    if (state !== undefined && state !== raw) fail('daemon state was replaced');
    const terminal = current(agentFile);
    if (terminal === undefined || terminal === agentRaw) return;
    // The first watchdog tick can publish the terminal while the daemon is still unsignalled: identify it from the top.
    if (agentRaw === undefined && !signalled) throw TERMINAL_PUBLISHED;
    fail('terminal state was replaced');
  };
  let signalled = false;
  unchanged();
  if (readPidStartTime(state.pid) !== daemonStart || readPidCmdline(state.pid) !== command) fail('daemon identity changed before stop');
  unchanged();
  remaining();
  signalled = true;
  try { process.kill(state.pid, 'SIGINT'); }
  catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ESRCH') throw error; }
  const settled = (pid: number, start: string) => {
    if (!isProcessAlive(pid)) return true;
    const actual = readPidStartTime(pid);
    if (actual && actual !== start) return true;
    if (process.platform === 'linux') {
      try { return fs.readFileSync(`/proc/${pid}/stat`, 'utf8').match(/^\d+ \(.*\) ([A-Z])/u)?.[1] === 'Z'; }
      catch { return !isProcessAlive(pid); }
    }
    const result = spawnSync('ps', ['-p', String(pid), '-o', 'stat='], {
      encoding: 'utf8', timeout: Math.min(1000, remaining()),
    });
    return result.status === 0 && result.stdout.trim().startsWith('Z');
  };
  for (;;) {
    unchanged();
    remaining();
    pending = [
      ...(!settled(state.pid, daemonStart) ? [`daemon ${state.pid}`] : []),
      ...chromium.filter(child => !settled(child.pid, child.start)).map(child => `Chromium ${child.pid}`),
      ...(agent && !isAgentRecordGone(agent) ? [`terminal ${agent.pid}`] : []),
    ];
    if (!pending.length) {
      unchanged();
      remaining();
      return;
    }
    if (pending.length === 1 && pending[0] === `daemon ${state.pid}`
      && performance.now() - started >= Math.min(1000, timeoutMs / 2)) {
      if (readPidStartTime(state.pid) !== daemonStart || readPidCmdline(state.pid) !== command) fail('daemon identity changed before final termination');
      unchanged();
      try { process.kill(state.pid, 'SIGKILL'); }
      catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ESRCH') throw error; }
    }
    await Bun.sleep(Math.min(25, remaining()));
  }
}

if (import.meta.main) {
  try {
    for (;;) {
      try { await settleOwnedBrowser(process.argv[2], Number(process.argv[3])); break; }
      catch (error) { if (error !== TERMINAL_PUBLISHED) throw error; }
    }
  } catch (error) {
    console.error(error instanceof Error ? error.message : String(error));
    process.exitCode = 1;
  }
}