Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(cloud): the Cloud home image brings its own init (#6167)
Milind Soni08cc107218f6
diff --git a/Dockerfile b/Dockerfileindex b6c5486b67..d8d6bdaeea 100644--- a/Dockerfile+++ b/Dockerfile@@ -26,9 +26,10 @@# with no `--init`, and Node as PID 1 waits only for its own children, so the# rest stay as zombies until the container's pids limit refuses every spawn.# The compose files' `init: true` on top of it is harmless. The Cloud home-# has no entrypoint: Fly's own init is PID 1 there, and a plain `docker run`-# of that image wants `--init`. Chrome keeps its shared memory out of a small-# /dev/shm by itself (server/browser/chrome/shm.ts).+# image has the same entrypoint: a host that starts it with a plain+# `docker run` gives it no init, and under Fly's own init tini collects as a+# subreaper. Chrome keeps its shared memory out of a small /dev/shm by itself+# (server/browser/chrome/shm.ts).FROM caddy:2.10.2 AS edge# Official Caddy carries a privileged-port file capability. The Cloud home's@@ -238,6 +239,12 @@ HEALTHCHECK --interval=15s --timeout=5s --start-period=30s --retries=5 \# environment `maus` could read (server/cloud-home-start.ts). It runs only# root's code from the image, never anything on the volume.USER root+# tini is PID 1 where the host gives the container no init, and collects the+# orphans of everything beneath it: the server's, the edge's and the bots'.+# It passes signals on to the supervisor and exits with its code. -s: under+# Fly's init, or `docker run --init`, it is not PID 1 and collects as a+# subreaper instead of warning.+ENTRYPOINT ["/usr/bin/tini", "-s", "--"]CMD ["node", "/app/dist-server/cloud-home-start.js"]# The self-hosted server, last so a plain `docker build .` builds it.diff --git a/docs/browser-packaging.md b/docs/browser-packaging.mdindex e6b6645b09..99ac1594ef 100644--- a/docs/browser-packaging.md+++ b/docs/browser-packaging.md@@ -251,8 +251,9 @@ agent-browser 0.37.0 itself adds `--no-sandbox` and`/run/.containerenv`, runs as root or under CI, or finds docker, kubepodsor lxc in `/proc/1/cgroup`; a runtime that shows none of these (aKubernetes pod on cgroup v2, for one) is covered by the `/dev/shm` check.-The Cloud home image has no entrypoint of its own: on Fly Machines Fly's-init runs as PID 1, and a plain `docker run` of that image wants `--init`.+The Cloud home image has the same tini entrypoint: on Fly Machines Fly's+init runs as PID 1 and tini collects beneath it, and on a host that starts+the image with a plain `docker run` tini is PID 1.See [Running on a container platform](self-hosting.md#running-on-a-container-platform-railway-render-kubernetes-cloud-run).## Video recordingdiff --git a/scripts/docker-image.test.ts b/scripts/docker-image.test.tsindex 6f1fff0e23..5249952702 100644--- a/scripts/docker-image.test.ts+++ b/scripts/docker-image.test.ts@@ -162,8 +162,8 @@ describe("container images", () => {expect(last(home, "VOLUME")).toBe('["/data"]');expect(last(home, "EXPOSE")).toBe("8080");expect(last(home, "CMD")).toBe('["node", "/app/dist-server/cloud-home-start.js"]');- // Fly's init is PID 1 on a Cloud home, and its supervisor starts as root.- expect(last(home, "ENTRYPOINT")).toBeUndefined();+ // A host that starts a Cloud home with a plain `docker run` gives it no init either.+ expect(last(home, "ENTRYPOINT")).toBe('["/usr/bin/tini", "-s", "--"]');expect(last(home, "HEALTHCHECK")).toContain("http://127.0.0.1:8080/api/health");expect(last(chain("runtime"), "ENV")).toMatch(/^AGENT_BROWSER_EXECUTABLE_PATH=\/opt\/openmausbot-browser\/chrome .*NODE_ENV=production$/);});diff --git a/server/cloud-home-start.ts b/server/cloud-home-start.tsindex ac45992d23..473ecae4b2 100644--- a/server/cloud-home-start.ts+++ b/server/cloud-home-start.ts@@ -14,7 +14,7 @@ import {import { procHidepid } from "./bot-box/hidepid.ts";import { boxMemory, heapSizeArgs } from "./box-memory.ts";import { clearRecordsFromOtherMachines } from "./data-dir-lease.ts";-import { OOM_SCORE, protectSupervisor, spawnWithOomScore } from "./oom-score.ts";+import { OOM_SCORE, protectInit, protectSupervisor, spawnWithOomScore } from "./oom-score.ts";import { restartPolicy } from "./restart.ts";const SERVICE_USER = "maus";@@ -395,6 +395,8 @@ export function startCloudHome(processEnv: NodeJS.ProcessEnv = process.env) {const env = root ? { ...processEnv, ...readCloudSecretsFile(home) } : processEnv;const config = cloudHomeConfiguration(env);if (!config) throw new Error("This image runs a My Cloud machine for MausBot Cloud; set its boot contract (docs/cloud-pro.md).");+ // Linux hands orphans to process 1, and Node waits only for the children it started.+ if (process.pid === 1) console.warn("cloud home: this supervisor is process 1, so nothing collects the processes bots leave behind and they pile up as zombies. Keep the image's entrypoint (tini), or start the container with --init.");// Logged here once: the server child never sees what they are about.for (const warning of config.warnings) console.warn(`cloud home: ${warning}`);const { server, edge, edgeFiles, tunnelToken, secrets, dropped } = cloudHomeChildEnvironments(config, env, home);@@ -405,6 +407,7 @@ export function startCloudHome(processEnv: NodeJS.ProcessEnv = process.env) {ids = passwdIds(readFileSync("/etc/passwd", "utf8"), SERVICE_USER);if (!ids) throw new Error(`The ${SERVICE_USER} user is missing from this image.`);if (protectSupervisor()) console.log("cloud home: the supervisor is never stopped for memory");+ protectInit();// Only the mount point changes owner; anything inside keeps its owner.const stat = statSync(home);if (stat.uid !== ids.uid || stat.gid !== ids.gid) chownSync(home, ids.uid, ids.gid);diff --git a/server/oom-score-init.test.ts b/server/oom-score-init.test.tsnew file mode 100644index 0000000000..017c031998--- /dev/null+++ b/server/oom-score-init.test.ts@@ -0,0 +1,26 @@+import { describe, expect, it } from "vitest";+import { OOM_SCORE, protectInit } from "./oom-score.ts";++const linux = process.platform === "linux";++describe("the init a Cloud home's supervisor runs under", () => {+ it("gets the supervisor's score when it is tini, so memory never ends the container at its process 1", () => {+ const files = new Map([["/proc/1/comm", "tini\n"], ["/proc/1/oom_score_adj", "0\n"]]);+ const file = { read: (path: string) => files.get(path)!, write: (path: string, data: string) => { files.set(path, data); } };+ expect(protectInit(1, file)).toBe(linux);+ expect(files.get("/proc/1/oom_score_adj")!.trim()).toBe(linux ? String(OOM_SCORE.supervisor) : "0");+ });++ it("leaves any other parent alone: a host's own init, a shell, another supervisor", () => {+ for (const name of ["docker-init", "init", "sh", "node"]) {+ const written: string[] = [];+ expect(protectInit(7, { read: (path) => (path.endsWith("/comm") ? `${name}\n` : "0\n"), write: (path) => { written.push(path); } })).toBe(false);+ expect(written).toEqual([]);+ }+ });++ it("is false, not an error, where the score can't be read or set", () => {+ expect(protectInit(1, { read: () => { throw new Error("ENOENT"); }, write: () => {} })).toBe(false);+ expect(protectInit(1, { read: (path) => (path.endsWith("/comm") ? "tini\n" : "0\n"), write: () => { throw new Error("EACCES"); } })).toBe(false);+ });+});diff --git a/server/oom-score.ts b/server/oom-score.tsindex 46efab4c64..f3c43a390e 100644--- a/server/oom-score.ts+++ b/server/oom-score.ts@@ -47,3 +47,18 @@ export function protectSupervisor(file: ScoreFile = procFile): boolean {return false;}}++/** The init this supervisor runs under (the Cloud home image's tini, process 1 where the host supplies none): root gives+ * it the supervisor's score too, since the container ends with its process 1. False where the parent is no such init, or+ * its score can't be set. */+export function protectInit(parent: number = process.ppid, file: ScoreFile = procFile): boolean {+ if (process.platform !== "linux") return false;+ try {+ if (file.read(`/proc/${parent}/comm`).trim() !== "tini") return false;+ const score = `/proc/${parent}/oom_score_adj`;+ file.write(score, String(OOM_SCORE.supervisor));+ return file.read(score).trim() === String(OOM_SCORE.supervisor);+ } catch {+ return false;+ }+}