Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(local-vm): a wedged Docker no longer costs 10 s on every runtime check
Gökhan Kösef59d495dd680
diff --git a/server/cloud-computer-lazy.e2e.test.ts b/server/cloud-computer-lazy.e2e.test.tsindex a602d04f00..313d7fca50 100644--- a/server/cloud-computer-lazy.e2e.test.ts+++ b/server/cloud-computer-lazy.e2e.test.ts@@ -240,6 +240,12 @@ describe("a cloud computer starts only when the bot uses it", () => {OMB_BOAT_API: `${relayBase}/own-key-must-not-be-used`,OMB_CLOUD_BOAT_URL: `${relayBase}/relay/api/box/v1`,OMB_CLOUD_BOAT_TOKEN: INCLUDED,+ // The host's container daemon is not this test's subject: a hung+ // Docker Desktop answers the local-VM probe (`docker info`) only at+ // its 10 s timeout, once per select_computer call. A socket nobody+ // listens on makes the probe fail at once, as on a host without one.+ DOCKER_HOST: `unix://${join(home, "no-docker.sock")}`,+ CONTAINER_HOST: `unix://${join(home, "no-podman.sock")}`,// The provider's error is still read twice before a computer is deleted; here the reads need not be half an hour apart.OMB_CLOUD_COMPUTER_DEAD_CONFIRM_MS: "0",},diff --git a/server/container-computer.test.ts b/server/container-computer.test.tsindex 97b6e7512f..20a751fc10 100644--- a/server/container-computer.test.ts+++ b/server/container-computer.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it } from "vitest";+import { describe, expect, it, vi } from "vitest";import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";@@ -305,6 +305,70 @@ function perBotReadyInspect(botId: string, viewerPort: number, targetLabel?: strreturn JSON.stringify([detail]);}+describe("containerRuntimeStatus probe of a hung runtime", () => {+ const dockerInfo = "docker info --format {{.ServerVersion}}";+ const dockerOnly = (info: string | Error) => runner({+ "/usr/bin/which docker": "/usr/local/bin/docker\n",+ "/usr/bin/which podman": new Error("missing"),+ [dockerInfo]: info,+ });+ const infoCalls = (calls: string[]) => calls.filter((call) => call === dockerInfo).length;++ it.each([+ ["killed", Object.assign(new Error("timeout"), { killed: true })],+ ["ETIMEDOUT", Object.assign(new Error("timeout"), { code: "ETIMEDOUT" })],+ ])("remembers a %s info check for 30 s instead of waiting again", async (_name, timeout) => {+ vi.useFakeTimers({ toFake: ["Date"] });+ try {+ const fake = dockerOnly(timeout);+ const down = { runtime: "docker", available: ["docker"], daemonUp: false };++ expect(await containerRuntimeStatus(fake.run, "linux")).toEqual(down);+ expect(await containerRuntimeStatus(fake.run, "linux")).toEqual(down);+ expect(infoCalls(fake.calls)).toBe(1);++ vi.advanceTimersByTime(30_001);+ expect(await containerRuntimeStatus(fake.run, "linux")).toEqual(down);+ expect(infoCalls(fake.calls)).toBe(2);+ } finally {+ vi.useRealTimers();+ }+ });++ it("checks again right away after a fast daemon error", async () => {+ const fake = dockerOnly(new Error("Cannot connect to the Docker daemon"));++ expect((await containerRuntimeStatus(fake.run, "linux")).daemonUp).toBe(false);+ expect((await containerRuntimeStatus(fake.run, "linux")).daemonUp).toBe(false);+ expect(infoCalls(fake.calls)).toBe(2);+ });++ it("does not cache a healthy check", async () => {+ const fake = dockerOnly("29.0.0\n");++ expect((await containerRuntimeStatus(fake.run, "linux")).daemonUp).toBe(true);+ expect((await containerRuntimeStatus(fake.run, "linux")).daemonUp).toBe(true);+ expect(infoCalls(fake.calls)).toBe(2);+ });++ it("shares one info check between concurrent callers", async () => {+ let infos = 0;+ const run: CommandRunner = async (command, args) => {+ const key = [command, ...args].join(" ");+ if (key === "/usr/bin/which docker") return { stdout: "/usr/local/bin/docker\n" };+ if (key !== dockerInfo) throw new Error("missing");+ infos += 1;+ await new Promise((resolve) => setTimeout(resolve, 20));+ return { stdout: "29.0.0\n" };+ };++ const both = await Promise.all([containerRuntimeStatus(run, "linux"), containerRuntimeStatus(run, "linux")]);++ expect(both.map((status) => status.daemonUp)).toEqual([true, true]);+ expect(infos).toBe(1);+ });+});+describe("containerComputerStatus", () => {it("prefers the supported Podman image store when Docker is also healthy on Windows", async () => {const fake = runner({diff --git a/server/container-computer.ts b/server/container-computer.tsindex a17628a272..c64551659b 100644--- a/server/container-computer.ts+++ b/server/container-computer.ts@@ -33,6 +33,7 @@ import {type HostUser,} from "./container-isolation.ts";import { SPAWNED_PROXIES } from "./proxy-paths.ts";+import { runtimeHealthy } from "./container-runtime-probe.ts";// Its own group: a timeout also ends the docker CLI plugins and credential helpers it started.const run = execFileInGroupAsync;@@ -252,25 +253,7 @@ export async function containerRuntimeStatus(: RUNTIMES.filter((runtime) => runtime !== "container" || platform === "darwin");const present = await Promise.all(candidates.map((runtime) => installed(runtime, runner, platform)));const available = candidates.filter((_, index) => present[index]);- const healthy = await Promise.all(- available.map(async (candidate) => {- try {- const infoArgs = candidate === "container"- ? ["system", "status"]- : candidate === "podman"- ? ["info", "--format", "json"]- : ["info", "--format", "{{.ServerVersion}}"];- await runner(- candidate,- infoArgs,- 10_000,- );- return true;- } catch {- return false;- }- }),- );+ const healthy = await Promise.all(available.map((candidate) => runtimeHealthy(runner, candidate)));const healthyIndex = healthy.indexOf(true);return {runtime: healthyIndex >= 0 ? available[healthyIndex] : (available[0] ?? null),diff --git a/server/container-runtime-probe.ts b/server/container-runtime-probe.tsnew file mode 100644index 0000000000..eb3c1dd710--- /dev/null+++ b/server/container-runtime-probe.ts@@ -0,0 +1,38 @@+import type { CommandRunner, Runtime } from "./container-computer.ts";++// A runtime whose info check timed out (a wedged Docker Desktop answers `docker info` only at the timeout) counts as+// down for HUNG_PROBE_MS without a new process, so a recovered one is seen up to that late. Fast errors and healthy+// checks are never remembered; concurrent callers share one check.+const HUNG_PROBE_MS = 30_000;+type RuntimeProbe = { hungUntil: number; inflight: Promise<boolean> | null };+const runtimeProbes = new WeakMap<CommandRunner, Map<Runtime, RuntimeProbe>>();++export function runtimeHealthy(runner: CommandRunner, runtime: Runtime): Promise<boolean> {+ let probes = runtimeProbes.get(runner);+ if (!probes) runtimeProbes.set(runner, (probes = new Map()));+ let probe = probes.get(runtime);+ if (!probe) probes.set(runtime, (probe = { hungUntil: 0, inflight: null }));+ if (Date.now() < probe.hungUntil) return Promise.resolve(false);+ if (probe.inflight) return probe.inflight;+ const state = probe;+ const infoArgs = runtime === "container"+ ? ["system", "status"]+ : runtime === "podman"+ ? ["info", "--format", "json"]+ : ["info", "--format", "{{.ServerVersion}}"];+ const check = (async () => {+ try {+ await runner(runtime, infoArgs, 10_000);+ return true;+ } catch (error) {+ const failure = error as { killed?: boolean; code?: unknown } | null;+ if (failure?.killed === true || failure?.code === "ETIMEDOUT") state.hungUntil = Date.now() + HUNG_PROBE_MS;+ return false;+ }+ })();+ state.inflight = check;+ void check.finally(() => {+ if (state.inflight === check) state.inflight = null;+ });+ return check;+}