Skip to content

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

Cloud browser: a turn's first call opens the sandbox instead of being refused for changed launch settings

milind-soni5a9842d08b5e
diff --git a/server/browser-runtime.ts b/server/browser-runtime.ts
index c056544322..40319aeb16 100644
--- a/server/browser-runtime.ts
+++ b/server/browser-runtime.ts
@@ -770,7 +770,7 @@ export class BrowserRuntime {
}
heldBy(session: string): string | null { return this.gates.get(session)?.owner ?? null; }
-
+ /** Stops the session's engine client when it was started under other launch settings and nothing uses it now (the place the browser runs changed between a turn's tools/list and its first call), so the next call starts one under these. */ async closeIfLaunchChanged(session: string, spec: BrowserSpawnSpec): Promise<boolean> { const entry = this.clients.get(session); if (!entry || entry.key === launchSettingsKey(spec) || this.working(session)) return false; await this.close(session); return true; }
/** An interrupted action left the browser's state unknown until the engine answers again or the browser restarts. */
interrupted(session: string): boolean { return this.gates.get(session)?.uncertain === true; }
diff --git a/server/host/browser-cloud-sandbox.test.ts b/server/host/browser-cloud-sandbox.test.ts
index 7101f0a00e..8042b77b58 100644
--- a/server/host/browser-cloud-sandbox.test.ts
+++ b/server/host/browser-cloud-sandbox.test.ts
@@ -1,6 +1,6 @@
// The bots' browser host on a personal Cloud, where a bot's browser opens in the Cloud's browser sandbox, against a fake
// provider (cloud-browser/testing/fake-computer.ts): the switch's lever, a desktop install, and the rules kept for it.
-import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -173,3 +173,56 @@ describe("a personal Cloud's bot browser opens in the Cloud's browser sandbox",
expect(host.cloudBrowserReport()).toMatchObject({ sessions: { sandbox: 0, home: 1 }, fallbacks: { "start-failed": 1 } });
});
});
+
+/** A stand-in for agent-browser's MCP server: one process per engine client, as the runtime starts it. It lists the same tools
+ * wherever the browser runs, and says in each answer where its browser is (attached to the sandbox, or the home's own). */
+const FAKE_ENGINE = `#!/usr/bin/env node
+const lines = require("node:readline").createInterface({ input: process.stdin });
+const where = process.env.AGENT_BROWSER_CDP ? "sandbox" : "home";
+lines.on("line", (line) => {
+ const m = JSON.parse(line);
+ if (m.id === undefined) return;
+ let result;
+ if (m.method === "initialize") result = { protocolVersion: "2024-11-05", capabilities: { tools: {} } };
+ else if (m.method === "tools/list") result = { tools: [{ name: "agent_browser_get_url", inputSchema: { type: "object" } }, { name: "agent_browser_open", inputSchema: { type: "object" } }] };
+ else result = { content: [{ type: "text", text: where + ":" + m.params.name }] };
+ process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id: m.id, result }) + "\\n");
+});
+`;
+
+describe.skipIf(process.platform === "win32")("a turn's first browser call after its tools/list, in a fresh process", () => {
+ let engineDir: string;
+ beforeEach(() => {
+ vi.mocked(BrowserRuntime.prototype.agentRpc).mockRestore();
+ engineDir = mkdtempSync(join(tmpdir(), "fake-engine-"));
+ const binary = join(engineDir, "agent-browser");
+ writeFileSync(binary, FAKE_ENGINE);
+ chmodSync(binary, 0o755);
+ vi.mocked(browserEngineStatus).mockReturnValue({ kind: "ready", binaryPath: binary, version: "0.37.0", managed: false });
+ });
+ afterEach(() => { rmSync(engineDir, { recursive: true, force: true }); });
+
+ it("opens the sandbox on the first call, under the same tools as the list, and goes back to the home's browser after the sandbox's closed", async () => {
+ const { host } = setup();
+ // The turn starts, and its browser MCP server lists the tools: no tool call yet, so nothing opens anywhere.
+ const atStart = (await host.browserIntegration(botId, undefined, turn))!;
+ const listed = await host.browserRuntime.agentRpc(atStart.session, atStart.spec, "tools/list", {});
+ expect(atStart.spec.env.AGENT_BROWSER_CDP).toBeUndefined();
+ // The first tool call opens the sandbox; it must reach the engine, not be refused for the changed launch settings.
+ const first = (await host.browserIntegration(botId, undefined, undefined, "team", { opening: "agent_browser_get_url" }))!;
+ expect(first.spec.env.AGENT_BROWSER_CDP).toBeDefined();
+ const answer = await host.browserRuntime.agentRpc(first.session, first.spec, "tools/call", { name: "agent_browser_get_url", arguments: {} });
+ expect(answer).not.toHaveProperty("isError", true);
+ expect(JSON.stringify(answer)).toContain("sandbox:agent_browser_get_url");
+ // The tool list is the same in the sandbox as in the home.
+ expect(await host.browserRuntime.agentRpc(first.session, first.spec, "tools/list", {})).toEqual(listed);
+ // Back in the home (the operator's lever, or the sandbox unavailable): the next call is not refused either.
+ vi.stubEnv("OMB_CLOUD_BROWSER", "home");
+ // The sandbox's session ends (here with every door at once); the engine client started for it is left behind.
+ host.browserSweep.closeAll();
+ const back = (await host.browserIntegration(botId, undefined, undefined, "team", { opening: "agent_browser_get_url" }))!;
+ expect(back.spec.env.AGENT_BROWSER_CDP).toBeUndefined();
+ const again = await host.browserRuntime.agentRpc(back.session, back.spec, "tools/call", { name: "agent_browser_get_url", arguments: {} });
+ expect(JSON.stringify(again)).toContain("home:agent_browser_get_url");
+ });
+});
diff --git a/server/host/browser.ts b/server/host/browser.ts
index b7e608c858..d5b648f5e6 100644
--- a/server/host/browser.ts
+++ b/server/host/browser.ts
@@ -284,7 +284,12 @@ export function createBrowserHost(deps: BrowserHostDeps) {
// Only through MausBot Cloud's relay, which keeps the sandbox outside the plan under its own limits: never on a person's own key.
ops: () => (boatAccount(cfg)?.included === true ? computerOps : null),
used: (owner, windowMs) => deps.cloudBrowser?.used(owner, windowMs),
- closeEngine: (session) => { const spec = usedBrowserSessions.get(session); return spec ? closeBrowser(session, spec) : Promise.resolve(true); },
+ // The engine's own client of the session goes too: the next call, in the home, starts one under the home's settings.
+ closeEngine: async (session) => {
+ const spec = usedBrowserSessions.get(session);
+ if (spec) await closeBrowser(session, spec);
+ await agentBrowserRuntime.close(session);
+ },
held: (session) => !turnResources.free(`browser:${session}`),
folder: (botId) => deps.cloudBrowser?.folder?.(botId) ?? null,
// The home's saved sign-ins: agent-browser's encrypted restore file, under this server's key only.
@@ -584,6 +589,9 @@ export function createBrowserHost(deps: BrowserHostDeps) {
const cdpUrl = lent ? await lentChromeDoor(session) : undefined;
const cloudUrl = lent ? undefined : await cloudBrowserUrl(botId, profile, session, audience, options.opening);
const spec = browserSessionSpec(status.binaryPath, session, browserProfileId, usesMyChrome(browserProfileId) || cloudUrl ? undefined : await browserEgress.open(session), cdpUrl, cloudUrl);
+ // On a Cloud the place a session's browser runs can change between a turn's tools/list (which starts the engine's client)
+ // and its first call, or after the sandbox's browser closed: an unused client of the other place goes, never a running browser.
+ if (cloudBrowser && !browserRunning(session, usedBrowserSessions.get(session))) await agentBrowserRuntime.closeIfLaunchChanged(session, spec);
usedBrowserSessions.set(session, spec);
await prepareBrowserSessionState(status.binaryPath, session, { env: spec.env, persistent: profile !== "guest" && !attachesToChrome(spec.env), isCurrent: () => {
const current = store.bot(botId);