Skip to content

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

Browser: a started Chrome's first tab gets as long as Chrome's own start, not 15 s (#6995)

Milind Soni32b6b1ddf0d7
diff --git a/server/browser/service.test.ts b/server/browser/service.test.ts
index bd0fd8dbb9..4e5956cd9b 100644
--- a/server/browser/service.test.ts
+++ b/server/browser/service.test.ts
@@ -1,7 +1,7 @@
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
-import { afterAll, afterEach, describe, expect, it } from "vitest";
+import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
import { removeTempDir } from "../testing/cleanup.ts";
import { forbiddenCommand } from "./cdp/testing/real-chrome.ts";
@@ -479,6 +479,29 @@ describe("browsers on one computer", () => {
await viewer.close();
});
+ it("gives a slow machine's first tab as long as Chrome's own start, so a start that takes 20 s is not refused at 15 s", async () => {
+ // On a busy or CPU-capped computer Chrome answers on its pipe but its first tab runs late; refusing then made the bot
+ // start Chrome all over again. Only the timers are fake: nothing here waits on the wall clock.
+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
+ try {
+ const slow = new FakeBackend(() => {
+ const chrome = new FakeChrome({ documents });
+ chrome.delay = (method) => (method === "Page.enable" ? 20_000 : 0);
+ return chrome;
+ });
+ backends.push(slow);
+ const { call, text } = setup({ backend: () => slow });
+ let result: Awaited<ReturnType<typeof call>> | null = null;
+ void call("agent_browser_open", { url: "https://example.com/form" }).then((answer) => { result = answer; });
+ for (let waited = 0; result === null && waited < 90_000; waited += 1_000) await vi.advanceTimersByTimeAsync(1_000);
+ expect(result).not.toBeNull();
+ expect(text(result!)).not.toContain("No matching tab opened");
+ expect(text(result!)).toContain("Opened https://example.com/form in t1.");
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it("makes room by closing the least recently used idle browser, and waits, then refuses, when none is idle", async () => {
const { service, call, backend } = setup({ maxRunning: 1, capacityWaitMs: 100 });
const other = { session: "bot-b", key: "bot:b" };
diff --git a/server/browser/service.ts b/server/browser/service.ts
index 53c66ca681..355aee08d1 100644
--- a/server/browser/service.ts
+++ b/server/browser/service.ts
@@ -45,6 +45,10 @@ export const BROWSER_STUCK = `The browser engine is stuck. Press Restart in the
const RESTART_COST = "a restart closes every tab and loses page state such as form input";
const PROBE_MS = 5_000;
const CAPACITY_WAIT_MS = 30_000;
+/** How long a started Chrome's first tab may take to run: as long as Chrome itself gets to answer on its DevTools pipe
+ * (chrome/launch.ts). On a busy or CPU-capped computer the first tab can take longer than 15 s, and refusing it then only
+ * made the bot start Chrome again from nothing. */
+const FIRST_TAB_MS = 60_000;
const SERVER_TAB_LIMIT = 32;
const PROFILE_TAB_LIMIT = 8;
@@ -256,7 +260,7 @@ export class BrowserService {
}));
try {
this.#stillWanted(profile, epoch);
- await browser.waitForPage(() => true, 15_000);
+ await browser.waitForPage(() => true, FIRST_TAB_MS);
for (const page of browser.pages()) await page.ready.then(() => adopt(page, "restored"), () => undefined);
if (fresh) await this.#onlyBlankTab(profile, browser);
// Chrome gives a new window's keyboard to its address bar, and the bot's DevTools Protocol work never moves it: on a