Skip to content

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

Cap: a real-process Linux test of the watchdog; never close a browser on the person's own Chrome

milind-soni8267a93df381
diff --git a/server/bot-memory-cap-live.test.ts b/server/bot-memory-cap-live.test.ts
new file mode 100644
index 0000000000..6f3d65d3c5
--- /dev/null
+++ b/server/bot-memory-cap-live.test.ts
@@ -0,0 +1,65 @@
+// The memory cap on real processes (Linux): stand-in engines started as spawnCli starts them, each leading its own
+// session with two tool-server children and a child its shell left behind (adopted by init, still in the engine's
+// session); the watchdog reads /proc as the server does: stat lines, proportional set sizes, start ticks, real signals.
+import { spawn, type ChildProcess } from "node:child_process";
+import { readFileSync } from "node:fs";
+import { afterEach, describe, expect, it } from "vitest";
+
+import { BotMemoryWatchdog, pssBytes } from "./bot-memory-cap.ts";
+import { parseProcStat } from "./perf/process-buckets.ts";
+import { linuxProcessList } from "./perf/process-sampler.ts";
+
+const MB = 1024 * 1024;
+const started: ChildProcess[] = [];
+afterEach(() => { for (const child of started.splice(0)) try { process.kill(-child.pid!, "SIGKILL"); } catch {} });
+
+/** An engine that holds `holdMb` of its own pages, with two tool servers and a background `sleep` its shell left. */
+function engine(holdMb: number): ChildProcess {
+ const child = spawn(process.execPath, ["-e", `
+ const { spawn } = require("node:child_process");
+ for (let i = 0; i < 2; i++) spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: "ignore" });
+ spawn("/bin/sh", ["-c", "sleep 600 & exit 0"], { stdio: "ignore" });
+ const keep = []; for (let i = 0; i < ${holdMb}; i++) { const b = Buffer.alloc(${MB}); b.fill(1 + (i % 250)); keep.push(b); }
+ setInterval(() => keep.length, 1000);
+ process.stdout.write("ready\\n");
+ `], { detached: true, stdio: ["ignore", "pipe", "ignore"] });
+ started.push(child);
+ return child;
+}
+const ready = (child: ChildProcess) => new Promise<void>((resolve) => child.stdout!.once("data", () => resolve()));
+const alive = (pid: number) => { try { process.kill(pid, 0); return true; } catch { return false; } };
+const startOf = (pid: number) => { try { return parseProcStat(pid, readFileSync(`/proc/${pid}/stat`, "utf8"))?.start ?? null; } catch { return null; } };
+/** Polls until `done` holds or the deadline passes. */
+async function until(done: () => boolean, ms = 15_000): Promise<boolean> {
+ for (const end = Date.now() + ms; Date.now() < end; await new Promise((resolve) => setTimeout(resolve, 100))) if (done()) return true;
+ return done();
+}
+/** The processes in `sid`'s session. */
+const session = (sid: number) => linuxProcessList().rows.filter((row) => row.sid === sid && row.state !== "Z").map((row) => row.pid);
+
+describe("the memory cap on real processes", () => {
+ it.runIf(process.platform === "linux")("stops only the bot over its cap, the child its shell left included, and leaves the other bot running", async () => {
+ const big = engine(300), small = engine(10);
+ await Promise.all([ready(big), ready(small)]);
+ // Both sessions are whole: the engine, two tool servers and the left-behind sleep.
+ expect(await until(() => session(big.pid!).length === 4 && session(small.pid!).length === 4)).toBe(true);
+ const smallPids = session(small.pid!);
+ const signals: string[] = [];
+ const dog = new BotMemoryWatchdog({
+ cap: () => 200 * MB, list: () => linuxProcessList().rows, pss: (pid) => pssBytes(pid), startOf, graceMs: 2_000,
+ owners: () => [{ pid: big.pid!, botIds: ["bot_big"], kind: "engine" }, { pid: small.pid!, botIds: ["bot_small"], kind: "engine" }],
+ signal: (pid, value) => { signals.push(`${value} ${pid}`); process.kill(pid, value); }, log: () => {},
+ });
+ let stops = await dog.check();
+ // 300 MB of its own pages against 200 MB is past one and a half times the cap: one look is enough. Allow a second
+ // where the share read below that the first time.
+ if (!stops.length) stops = await dog.check();
+ expect(stops.map((stop) => stop.botId)).toEqual(["bot_big"]);
+ expect(stops[0]!.usedBytes).toBeGreaterThan(300 * MB);
+ expect(stops[0]!.pids.length).toBeGreaterThanOrEqual(4);
+ expect(await until(() => session(big.pid!).length === 0)).toBe(true);
+ expect(signals.some((line) => smallPids.includes(Number(line.split(" ")[1])))).toBe(false);
+ expect(smallPids.every(alive)).toBe(true);
+ expect(await dog.check()).toEqual([]);
+ }, 60_000);
+});
diff --git a/server/host/browser.ts b/server/host/browser.ts
index e3b2a46d6e..d796b937e7 100644
--- a/server/host/browser.ts
+++ b/server/host/browser.ts
@@ -500,7 +500,7 @@ export function createBrowserHost(deps: BrowserHostDeps) {
const shared = (session: string) => store.bots.some((other) => other.id !== botId && browserSessionsOf(other.id, other.browserProfile).includes(session));
await Promise.all(browserSessionsOf(bot.id, bot.browserProfile).flatMap((session) => {
const spec = usedBrowserSessions.get(session);
- return spec && !chromeTarget(session, spec) && !shared(session) ? [closeForMemory(session, spec)] : [];
+ return spec && !chromeTarget(session, spec) && !attachesToChrome(spec.env) && !shared(session) ? [closeForMemory(session, spec)] : [];
}));
}
/** The browser a thread's bot would start; a temporary one has no session until its first call. My Chrome is the person's own. */
diff --git a/server/idle-and-cap.test.ts b/server/idle-and-cap.test.ts
index b6353923b9..479611adbd 100644
--- a/server/idle-and-cap.test.ts
+++ b/server/idle-and-cap.test.ts
@@ -8,7 +8,10 @@ import {
} from "./bot-memory-cap.ts";
import type { ProviderInstance, RuntimeEvent, RuntimeEventListener, SendTurnInput } from "./contracts.ts";
import { armIdleClose, engineIdleMs, idleAfter, stopIdleClose, stoppedForRoom, sweepIdleEngines, type WarmEngine } from "./drivers/session-idle.ts";
-import { EngineMemory, type HostMemory } from "./engine-memory.ts";
+import { EngineMemory, memoryStopped, type HostMemory } from "./engine-memory.ts";
+import { hostFaultOf, retryCannotFix } from "./host-fault.ts";
+import { namesOwnCause } from "./process-shortage.ts";
+import { retriesTeammateThread } from "./routine-recovery.ts";
import type { FootprintOwner } from "./perf/bot-footprint.ts";
import type { ProcessRow } from "./perf/process-buckets.ts";
import { ProcessBudget } from "./process-room.ts";
@@ -339,6 +342,12 @@ describe("a memory cap per bot", () => {
+ "Send the message again to start over; if it keeps happening, give this task less to do at once, or give the server more memory.";
expect(shown()).toEqual([line, "turn.completed", "something else"]);
expect(seen[0]).toMatchObject({ text: { key: "chat.notice.botMemoryCap", params: { bot: "Researcher", used: "1700", cap: "1000" } } });
+ // A fault a retry cannot fix: no automatic retry or Chief retry starts the bot into the same cap again.
+ expect(hostFaultOf(line)).toBe("bot_memory");
+ expect(retryCannotFix("bot_memory")).toBe(true);
+ expect(retriesTeammateThread(line)).toBe(false);
+ expect(namesOwnCause(line)).toBe(true);
+ expect(memoryStopped(line)).toBe(false);
// A turn that ends without an error of its own still says why, first.
seen.length = 0;
emit({ threadId: "tc", turnId: "t", type: "turn.completed", ok: false, stopReason: "exit_before_result" });