Skip to content

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

Cloud browser: the Admin sizes the sandbox by plan; as many Chromes as its relay says

milind-soni8647823e3b92
diff --git a/server/cloud-browser/cloud-browser.test.ts b/server/cloud-browser/cloud-browser.test.ts
index 8c9562ea6b..cc87e13510 100644
--- a/server/cloud-browser/cloud-browser.test.ts
+++ b/server/cloud-browser/cloud-browser.test.ts
@@ -139,8 +139,9 @@ describe("a personal Cloud's bot browsers in the Cloud's browser sandbox", () =>
const create = fake.requests.findIndex((one) => one.method === "POST" && one.path === "/api/v1/sandboxes");
expect(probe).toBeGreaterThan(-1);
expect(probe).toBeLessThan(create);
- expect(fake.requests[create]?.body).toEqual({ noEnv: true, type: "small", purpose: "browser" });
- expect(browserSandboxCreateBody()).toEqual({ noEnv: true, type: "small", purpose: "browser" });
+ // No size asked for: the Admin picks it by the Cloud's plan (small, or default on the bigger plans).
+ expect(fake.requests[create]?.body).toEqual({ noEnv: true, purpose: "browser" });
+ expect(browserSandboxCreateBody()).toEqual({ noEnv: true, purpose: "browser" });
expect(fake.requests.find((one) => one.method === "PATCH")?.body?.name).toMatch(/^ogb-[a-f0-9]{12}-browser-[a-f0-9]{6}$/);
expect(cloud.report().resumes).toBe(1);
});
@@ -164,7 +165,7 @@ describe("a personal Cloud's bot browsers in the Cloud's browser sandbox", () =>
fake.exists = false;
// The probe answers, but the create comes back unmarked (an Admin rolled back between the two).
const realProbe = providerOps(() => ({}) as AppConfig);
- const ops = { ...realProbe, probe: async () => { fake.oldRelay = true; return { kind: "available" as const, boxId: null }; } };
+ const ops = { ...realProbe, probe: async () => { fake.oldRelay = true; return { kind: "available" as const, boxId: null, chromes: null }; } };
const guarded = browser({ ops: () => ops });
expect(await guarded.open(request())).toEqual({ home: "old-relay" });
expect(fake.creates()).toBe(1);
@@ -350,6 +351,15 @@ describe("a personal Cloud's bot browsers in the Cloud's browser sandbox", () =>
expect(await cloud.open(request("chief"))).toEqual({ home: "sandbox-full" });
});
+ it("carries as many bots' Chromes as the relay says this Cloud's sandbox takes", async () => {
+ fake.state = "archived";
+ fake.chromes = 3;
+ const cloud = browser();
+ // The relay says it at the wake, so from the first open on.
+ for (const bot of ["scout", "writer", "chief"]) urlOf(await cloud.open(request(bot)));
+ expect(await cloud.open(request("fourth"))).toEqual({ home: "sandbox-full" });
+ });
+
it("reuses a bot's profile after the sandbox slept", async () => {
const cloud = browser();
await cloud.open(request());
diff --git a/server/cloud-browser/index.ts b/server/cloud-browser/index.ts
index b1ee7cb28d..a1df61dcd6 100644
--- a/server/cloud-browser/index.ts
+++ b/server/cloud-browser/index.ts
@@ -98,6 +98,8 @@ export function createCloudBrowser(deps: CloudBrowserDeps) {
let awake = false;
let leverCleanupAt = -Infinity;
let ttlRefreshedAt = 0;
+ /** How many bots' Chromes this Cloud's sandbox takes at once, as the relay last said (by its size). */
+ let chromesAllowed = BROWSER_SANDBOX_CHROMES;
const home = (request: OpenRequest, reason: HomeReason): Opened => {
rails.home(reason);
@@ -135,6 +137,7 @@ export function createCloudBrowser(deps: CloudBrowserDeps) {
const said = await ops.probe();
if (said.kind === "old-relay") throw new OldRelay();
if (said.kind === "unavailable") throw new Unavailable(said.reason, said.resetsAt);
+ if (said.chromes !== null) chromesAllowed = said.chromes;
}
async function wake(ops: ComputerOps): Promise<{ id: string; started: boolean }> {
@@ -257,7 +260,7 @@ export function createCloudBrowser(deps: CloudBrowserDeps) {
if (held) return home(request, held);
// One Chrome per bot's profile: a second session of the same bot (another audience's) stays in the home.
if (bots().has(request.botId)) return home(request, "busy");
- if (bots().size >= BROWSER_SANDBOX_CHROMES) return home(request, "sandbox-full");
+ if (bots().size >= chromesAllowed) return home(request, "sandbox-full");
// The home's copy Chrome refused before, unchanged since: not worth a wake.
const known = deps.signIns?.load(request.session);
const bad = rails.carryBad(profileKey(request.botId));
diff --git a/server/cloud-browser/rails.ts b/server/cloud-browser/rails.ts
index 0507483bb8..ace91ca01d 100644
--- a/server/cloud-browser/rails.ts
+++ b/server/cloud-browser/rails.ts
@@ -7,9 +7,9 @@ import { dirname, join } from "node:path";
import type { CloudLimit } from "../../shared/place-view.ts";
/** Resumes a Cloud may spend on its bots' browsers in one UTC day; past it, every browser opens in the home. */
-export const CLOUD_BROWSER_DAILY_RESUMES = 30;
+export const CLOUD_BROWSER_DAILY_RESUMES = 100;
/** Minutes the Cloud's browser sandbox may be awake in one UTC day; past it, every browser opens in the home. */
-export const CLOUD_BROWSER_DAILY_AWAKE_MINUTES = 240;
+export const CLOUD_BROWSER_DAILY_AWAKE_MINUTES = 1_080;
/** Failed starts in a row before the back-off. */
export const CLOUD_BROWSER_FAILURES_IN_ROW = 2;
export const CLOUD_BROWSER_BACKOFF_MS = 15 * 60_000;
diff --git a/server/cloud-browser/remote.ts b/server/cloud-browser/remote.ts
index 639e9a99eb..6352bd230f 100644
--- a/server/cloud-browser/remote.ts
+++ b/server/cloud-browser/remote.ts
@@ -16,10 +16,9 @@ const READY = new Set(["idle", "ready", "running"]);
const TERMINAL = new Set(["error", "cancelled"]);
/** The sandbox's owner key: its name is `ogb-<this installation>-browser-<hash>`, so operators tell it from a bot's computer. */
export const BROWSER_SANDBOX_OWNER = "browser";
-/** The provider's smallest size that runs Chrome well: 2 vCPU, 4 GB. A heavy page measured 0.8–0.87 GB per Chrome. */
-export const BROWSER_SANDBOX_TYPE = "small";
-/** Chromes at once in one sandbox (one per bot browsing); past this a bot's browser opens in the home. One heavy page
- * measured 954 MB on `small` (4 GB), so two; the Admin's launch also refuses a Chrome when under 1 GiB is free. */
+/** Chromes at once in one sandbox (one per bot browsing) until the relay says how many this Cloud's size takes (two on
+ * `small`, where one heavy page measured 954 MB of 4 GB; three on `default`); past it a bot's browser opens in the home. The
+ * Admin's launch also refuses a Chrome when under 1 GiB is free. */
export const BROWSER_SANDBOX_CHROMES = 2;
/** The port the sandbox's small door listens on, on its loopback only; the provider's relay reaches it there. */
export const COMPUTER_DOOR_PORT = 39231;
@@ -30,7 +29,7 @@ export const profileKey = (botId: string): string => createHash("sha256").update
/** What the relay says before anything is started: whether a browser sandbox may start now, and until when it may not. */
export type Availability =
| { kind: "old-relay" }
- | { kind: "available"; boxId: string | null }
+ | { kind: "available"; boxId: string | null; chromes: number | null }
| { kind: "unavailable"; reason: string; resetsAt: string | null };
/** What the browser needs from the relay; tests pass their own. */
@@ -71,8 +70,9 @@ async function json(cfg: AppConfig, path: string, init: RequestInit = {}) {
return { ok: res.ok && body?.ok !== false, status: res.status, body };
}
-/** `purpose` tells the Admin's relay this is the Cloud's browser sandbox, outside the plan's computers and hours. */
-export const browserSandboxCreateBody = () => ({ noEnv: true, type: BROWSER_SANDBOX_TYPE, purpose: "browser" });
+/** `purpose` tells the Admin's relay this is the Cloud's browser sandbox, outside the plan's computers and hours. Its size
+ * is the Admin's to choose by the Cloud's plan (small, or default on the bigger plans), so none is asked for here. */
+export const browserSandboxCreateBody = () => ({ noEnv: true, purpose: "browser" });
/** The Cloud's relay does not know the browser sandbox (an Admin from before it): nothing is created. */
export class OldRelay extends Error {}
@@ -135,7 +135,8 @@ export function providerOps(cfg: () => AppConfig): ComputerOps {
const answer = await json(cfg(), "/browser-sandbox");
if (answer.status === 404) return { kind: "old-relay" };
if (!answer.ok || typeof answer.body?.available !== "boolean") throw boatRefusal(answer.status, "asking about the browser sandbox", answer.body, true);
- if (answer.body.available) return { kind: "available", boxId: typeof answer.body.boxId === "string" ? answer.body.boxId : null };
+ const chromes = Number.isInteger(answer.body.chromes) && answer.body.chromes >= 1 && answer.body.chromes <= 4 ? answer.body.chromes as number : null;
+ if (answer.body.available) return { kind: "available", boxId: typeof answer.body.boxId === "string" ? answer.body.boxId : null, chromes };
return { kind: "unavailable", reason: typeof answer.body.reason === "string" ? answer.body.reason : "unknown", resetsAt: typeof answer.body.resetsAt === "string" ? answer.body.resetsAt : null };
},
async find() {
diff --git a/server/cloud-browser/testing/fake-computer.ts b/server/cloud-browser/testing/fake-computer.ts
index c5c44319e1..09d21450b7 100644
--- a/server/cloud-browser/testing/fake-computer.ts
+++ b/server/cloud-browser/testing/fake-computer.ts
@@ -29,6 +29,8 @@ export interface FakeComputer {
hostRoute: boolean;
unprotected: boolean;
chromeStarts: boolean;
+ /** How many Chromes the probe says this Cloud's sandbox takes (by its plan's size). */
+ chromes: number;
/** A resume during `archiving` finds the machine already gone. */
takeBackFails: boolean;
/** Whether the launch could write the Chrome policy (sudo in the sandbox). */
@@ -67,7 +69,7 @@ export async function startFakeComputer(): Promise<FakeComputer> {
let retired = true;
const fake = {
base: "", state: "ready", exists: true, startStatus: 200, startBody: { ok: true }, oldRelay: false, available: true, unavailableReason: "fleet_starts",
- unavailableUntil: null, hostRoute: true, unprotected: false, chromeStarts: true, policy: true, takeBackFails: false, requests: [], upgrades: [], frames: [], boxId: "bx_23456789",
+ unavailableUntil: null, hostRoute: true, unprotected: false, chromeStarts: true, policy: true, takeBackFails: false, chromes: 2, requests: [], upgrades: [], frames: [], boxId: "bx_23456789",
files: new Map(), cookies: new Map(), commands: [], failing: new Set(),
} as unknown as FakeComputer;
const watchers = new Map<string, Set<import("ws").WebSocket>>();
@@ -100,7 +102,7 @@ export async function startFakeComputer(): Promise<FakeComputer> {
const rest = url.pathname.replace(/^\/api\/v1/, "");
if (rest === "/browser-sandbox") {
if (fake.oldRelay) return send(404, { ok: false, code: "not_found" });
- return send(200, { ok: true, type: "browser.sandbox", available: fake.available, boxId: fake.exists ? boxId : null,
+ return send(200, { ok: true, type: "browser.sandbox", available: fake.available, boxId: fake.exists ? boxId : null, sandboxType: fake.chromes > 2 ? "default" : "small", chromes: fake.chromes,
...(fake.available ? {} : { reason: fake.unavailableReason, ...(fake.unavailableUntil ? { resetsAt: fake.unavailableUntil } : {}) }) });
}
if (rest === "/sandboxes" && req.method === "GET") return send(200, { ok: true, sandboxes: fake.exists ? [row] : [], pageInfo: { nextCursor: null } });