Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

3.8 KB
Raw Blame
export const BOOT_PROBE_FAST_INTERVAL_MS = 25;
export const BOOT_PROBE_FAST_WINDOW_MS = 5_000;
export const BOOT_PROBE_INTERVAL_MS = 500;

export const SERVER_BOOT_TIMEOUT_MS = 60_000;
export const SERVER_LISTENING_GRACE_MS = 180_000;
const LISTENING_LINE = "openmausbot server on ";

export function listeningSignal() {
  let tail = "";
  let heard = false;
  let announce;
  const announced = new Promise((resolve) => { announce = resolve; });
  return {
    hear(chunk) {
      if (heard) return;
      const text = tail + chunk;
      tail = text.slice(-LISTENING_LINE.length);
      heard = text.includes(LISTENING_LINE);
      if (heard) announce();
    },
    heard: () => heard,
    /** Settles when the line is heard. */
    announced,
  };
}

/** @param {number} port @param {{ outcome: string, waitedMs?: number }} identity */
export function bootFailureMessage(port, identity) {
  if (identity.outcome === "exited") return `child on port ${port} exited before answering /api/health`;
  if (identity.outcome === "foreign-owner") return `port ${port} answered health checks from another process`;
  if (identity.outcome === "ready") return `child on port ${port} answered after it stopped being the current server`;
  return `child on port ${port} did not answer /api/health within ${Math.round((identity.waitedMs ?? 0) / 1000)}s`;
}

const defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

/**
 * @param {{
 *   port: number,
 *   pid: () => number | undefined,
 *   bootTimeoutMs: number,
 *   isExited?: () => boolean,
 *   isListening?: () => boolean,
 *   listeningGraceMs?: number,
 *   listened?: Promise<void>,
 *   now?: () => number,
 *   sleep?: (ms: number) => Promise<void>,
 *   fetchImpl?: typeof fetch,
 * }} options
 * @returns {Promise<{ outcome: "ready" | "foreign-owner" | "timeout" | "exited", latencyMs?: number, waitedMs?: number }>}
*/
export async function pollServerIdentity({
  port,
  pid,
  bootTimeoutMs,
  isExited = () => false,
  isListening = () => false,
  listeningGraceMs = 0,
  listened,
  now = Date.now,
  sleep = defaultSleep,
  fetchImpl = globalThis.fetch,
}) {
  const startedAt = now();
  // The child's "listening" line cuts one wait short, so the probe that follows it is not up to an interval late.
  let wake = listened;
  const deadline = () => startedAt + bootTimeoutMs + (isListening() ? listeningGraceMs : 0);
  for (;;) {
    if (isExited()) return { outcome: "exited" };
    const remainingMs = Math.max(0, deadline() - now());
    if (remainingMs <= 0) return { outcome: "timeout", waitedMs: now() - startedAt };

    let res;
    try {
      res = await fetchImpl(`http://127.0.0.1:${port}/api/health`, {
        signal: AbortSignal.timeout(remainingMs),
      });
    } catch {
      const intervalMs =
        now() - startedAt < BOOT_PROBE_FAST_WINDOW_MS ? BOOT_PROBE_FAST_INTERVAL_MS : BOOT_PROBE_INTERVAL_MS;
      const wait = sleep(Math.min(intervalMs, Math.max(1, deadline() - now())));
      await (wake ? Promise.race([wait, wake.then(() => { wake = null; })]) : wait);
      continue;
    }
    const body = await res.json().catch(() => null);
    // Past the deadline a null body is a timeout, not another process answering.
    if (now() >= deadline()) return { outcome: "timeout", waitedMs: now() - startedAt };
    // Read now: Electron's utilityProcess sets proc.pid only on the async `spawn` event, so undefined is not our child.
    const expectedPid = pid();
    const identified =
      res.ok &&
      expectedPid !== undefined &&
      body?.app === "openmausbot" &&
      body.pid === expectedPid &&
      body.static;
    if (!identified) return { outcome: "foreign-owner" };
    if (now() >= deadline()) return { outcome: "timeout", waitedMs: now() - startedAt };
    return { outcome: "ready", latencyMs: now() - startedAt };
  }
}