Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Renew the current VPS idle timer when an old viewer tunnel ends
Linab63a412a33a
diff --git a/server/index.ts b/server/index.tsindex 60062d1a9a..d52010b08a 100644--- a/server/index.ts+++ b/server/index.ts@@ -15012,9 +15012,8 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {}if (m[2] === "join") {const remote = auth.kind !== "loopback";- const result = await vps.vpsComputerJoin(vpsConfigForBot(cfg, bot), botId, undefined, remote, () => {- if (vpsIdles.has(botId)) touchVpsIdle(bot);- });+ const result = await vps.vpsComputerJoin(vpsConfigForBot(cfg, bot), botId, undefined, remote,+ () => vpsIdles.get(botId)?.touch());touchVpsIdle(bot);return json(res, 200, { ...result, joinUrl: remote? desktopViewerUrl(`vps/${botId}`, bot.threadId) : result.joinUrl });diff --git a/server/vps-routing.test.ts b/server/vps-routing.test.tsindex df6761c283..f178aa322c 100644--- a/server/vps-routing.test.ts+++ b/server/vps-routing.test.ts@@ -15,8 +15,9 @@ import {IMAGE_LAYER_VERSION,MANAGED_LABEL,} from "./container-computer.ts";-import { VPS_CONTAINER_LABEL, VPS_IMAGE, VPS_MANAGED_LABEL, VPS_VIEWER_LABEL, vpsContainerName } from "./vps-computer.ts";+import { VPS_CONTAINER_LABEL, VPS_ENVIRONMENT_LABEL, VPS_IMAGE, VPS_MANAGED_LABEL, VPS_VIEWER_LABEL, vpsContainerName } from "./vps-computer.ts";import { CONTROL_REFUSAL_PLAIN, controlRefusal, createControlClient } from "./control-client.ts";+import { loadEnvironmentId } from "./environment.ts";import { removeTempDir, waitForExit } from "./testing/cleanup.ts";import type { RoutineSchedule } from "../shared/routines.ts";import { workerPort } from "./testing/worker-port.ts";@@ -29,6 +30,8 @@ const IMAGE_ID = `sha256:${"a".repeat(64)}`;const CONTAINER_ID = "b".repeat(64);/** Shortened, but past the 2 s after which the bridges' old control client told the model a person had taken control. */const CALL_WAIT_MS = 3_000;+const TEST_IDLE_MINUTES = 137;+const TEST_IDLE_MS = 4_000;// the fake docker is a POSIX shell script, like every process fixture hereconst posixOnly = describe.skipIf(process.platform === "win32");@@ -112,6 +115,7 @@ if [ -f "$FAKE_DOCKER_DIR/hold" ]; thenficase "$*" in*" run "*) rm -f "$FAKE_DOCKER_DIR/container-missing" ;;+ *" stop "*) sed 's/"Running":true/"Running":false/' "$FAKE_DOCKER_DIR/container.json.tpl" > "$FAKE_DOCKER_DIR/container.next"; mv "$FAKE_DOCKER_DIR/container.next" "$FAKE_DOCKER_DIR/container.json.tpl" ;;*" start "*) sed 's/"Running":false/"Running":true/' "$FAKE_DOCKER_DIR/container.json.tpl" > "$FAKE_DOCKER_DIR/container.next"; mv "$FAKE_DOCKER_DIR/container.next" "$FAKE_DOCKER_DIR/container.json.tpl" ;;*" container ls "*)alias=$(printf '%s' "$2" | sed 's|ssh://||')@@ -163,7 +167,7 @@ posixOnly("VPS turn routing e2e (fake ACP fleet + fake docker over SSH)", () =>let askerDump: string;let dockerLog: string;- type ApiBody = Record<string, string | boolean | null | RoutineSchedule | { instanceId: string; model: string } | { sshAlias: string }>;+ type ApiBody = Record<string, string | boolean | null | RoutineSchedule | { instanceId: string; model: string } | { sshAlias: string } | { timeoutMinutes: number }>;const api = async (method: string, path: string, body?: ApiBody): Promise<{ status: number; body: any }> => {const res = await fetch(`${BASE}${path}`, {@@ -205,6 +209,7 @@ posixOnly("VPS turn routing e2e (fake ACP fleet + fake docker over SSH)", () =>import { createServer } from 'node:net';const args = process.argv.slice(2);if (!args.includes('-L')) process.exit(0);+(await import('node:fs')).writeFileSync(process.env.FAKE_DOCKER_DIR + '/viewer.pid', String(process.pid));const forward = args[args.indexOf('-L') + 1];const port = Number(forward.split(':')[1]);createServer(socket => socket.end()).listen(port, '127.0.0.1');@@ -248,7 +253,11 @@ createServer(socket => socket.end()).listen(port, '127.0.0.1');};if (process.env.PATH) env.PATH = process.env.PATH;if (process.env.SystemRoot) env.SystemRoot = process.env.SystemRoot;- child = spawn(process.execPath, [join(SERVER_DIR, "index.ts")], {+ const idleClock = join(home, "idle-clock.cjs");+ writeFileSync(idleClock, `const schedule = global.setTimeout;+global.setTimeout = (fn, ms, ...args) => schedule(fn, ms === ${TEST_IDLE_MINUTES * 60_000} ? ${TEST_IDLE_MS} : ms, ...args);+`);+ child = spawn(process.execPath, ["--require", idleClock, join(SERVER_DIR, "index.ts")], {cwd: join(SERVER_DIR, ".."),env,stdio: ["ignore", "pipe", "pipe"],@@ -272,6 +281,37 @@ createServer(socket => socket.end()).listen(port, '127.0.0.1');await removeTempDir(home);});+ it("renews the current server's idle window when the previous server's viewer drops", async () => {+ const dir = dirname(dockerLog);+ const template = join(dir, "container.json.tpl");+ const original = readFileSync(template, "utf8");+ const container = JSON.parse(original);+ container[0].Config.Labels[VPS_ENVIRONMENT_LABEL] = loadEnvironmentId(join(home, ".openmausbot"));+ const unreachable = join(dir, "unreachable.production-vps");+ try {+ writeFileSync(template, JSON.stringify(container));+ expect((await api("PUT", "/api/config", { vps: { sshAlias: "production-vps" } })).status).toBe(200);+ expect((await api("PATCH", "/api/config", { vpsIdle: { timeoutMinutes: TEST_IDLE_MINUTES } })).status).toBe(200);+ const { bot } = (await api("POST", "/api/bots")).body;+ const path = `/api/bots/${bot.id}`;+ expect((await api("PATCH", path, { computer: "cloud", cloudBackend: "vps" })).status).toBe(200);+ expect((await api("POST", `${path}/computer/join?threadId=${bot.threadId}`, {})).status).toBe(200);+ writeFileSync(unreachable, "");+ expect((await api("PATCH", path, { vpsAlias: "alpha-vps" })).status).toBe(200);+ expect((await api("POST", `${path}/computer/provision`, {})).status).toBe(200);+ const stopped = () => readFileSync(dockerLog, "utf8").includes(`ssh://alpha-vps stop ${CONTAINER_ID}`);+ await new Promise(resolve => setTimeout(resolve, 1500));+ const droppedAt = performance.now();+ process.kill(Number(readFileSync(join(dir, "viewer.pid"), "utf8")));+ await until(async () => stopped(), "idle stop on the new server", 8000);+ expect(performance.now() - droppedAt).toBeGreaterThanOrEqual(TEST_IDLE_MS);+ } finally {+ rmSync(unreachable, { force: true });+ writeFileSync(template, original);+ await api("PATCH", "/api/config", { vpsIdle: { timeoutMinutes: 480 } });+ }+ }, 20_000);+it("recovers a transient VPS inspection failure through the real status route without provisioning", async () => {expect((await api("PUT", "/api/config", { vps: { sshAlias: "production-vps" } })).status).toBe(200);const bot = (await api("POST", "/api/bots")).body.bot;