Skip to content

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

Train K1: keep only the idle-close wording from the browser change

milind-soni3d4aa2d6016e
diff --git a/docs/browser-packaging.md b/docs/browser-packaging.md
index f8bb392f36..8d9c49ee7c 100644
--- a/docs/browser-packaging.md
+++ b/docs/browser-packaging.md
@@ -203,11 +203,10 @@ same rules hold on the desktop, a server and Cloud:
fill_login's typing; a preview frame only holds off a close while it is
taken), no turn that has used it and is still running, and no person
holding it or using it through a Browser panel (opening the panel, a
- click, a key, an address, taking or handing back control). On desktop,
- an open Browser panel also keeps an agent-browser session open while you
- watch it. On servers, watching alone does not extend the idle time. An
- idle-closed panel offers to open the browser again. With the new engine,
- it shows the browser again once the bot's next call starts it.
+ click, a key, an address, taking or handing back control). A panel that
+ only watches does not keep it open: the panel says the browser closed
+ after it sat unused and offers to open it again, and on the new engine
+ it shows the browser again by itself once the bot's next call starts it.
It never reconnects on its own, which would start the browser again.
Saved sign-ins stay, and the bot's next browser call opens it again and is
told its pages are gone. A temporary browser saves nothing, so it starts
diff --git a/server/browser-launch-key.ts b/server/browser-launch-key.ts
index 931906ae7f..9dcda9f103 100644
--- a/server/browser-launch-key.ts
+++ b/server/browser-launch-key.ts
@@ -8,14 +8,6 @@ import type { BrowserSpawnSpec } from "./browser-runtime.ts";
const FNM_PER_SHELL = /(?:^|[\\/])fnm_multishells[\\/][^\\/]+(?:[\\/]bin)?[\\/]?$/u;
const FNM_UNRESOLVED = "fnm-multishell";
-export function attachesToChrome(env: NodeJS.ProcessEnv): boolean {
- return Boolean(env.AGENT_BROWSER_CDP || env.AGENT_BROWSER_AUTO_CONNECT);
-}
-
-export function ownsBrowserViewport(env: NodeJS.ProcessEnv): boolean {
- return env.AGENT_BROWSER_HEADLESS === "1" && !attachesToChrome(env);
-}
-
function fnmTarget(entry: string): string {
try { return realpathSync.native(entry); }
catch { return FNM_UNRESOLVED; }
diff --git a/server/browser-lifecycle-cleanup.ts b/server/browser-lifecycle-cleanup.ts
index 49bf86df6a..b16f9874e6 100644
--- a/server/browser-lifecycle-cleanup.ts
+++ b/server/browser-lifecycle-cleanup.ts
@@ -411,7 +411,7 @@ export class BrowserSweep {
engine: () => string | null;
owns: (session: string) => boolean;
live: (session: string) => boolean;
- /** The session is in use and skipped by this sweep. */
+ /** A turn holds it until it finishes: never closed while one does. */
held: (session: string) => boolean;
closeIdle: (session: string) => Promise<boolean>;
intervalMs?: number;
diff --git a/server/browser-runtime.ts b/server/browser-runtime.ts
index e3d6629e0a..1f15730030 100644
--- a/server/browser-runtime.ts
+++ b/server/browser-runtime.ts
@@ -6,8 +6,7 @@ import { PageSizes, type PageSize } from "./browser-page-size.ts";
import { SECRET_ON_PAGE, SecretHolds, type SecretHold } from "./browser-secret-hold.ts";
import type { BrowserSteps, StepCall, StepContext } from "./browser-steps.ts";
import { HARNESS_CALL_BUDGET_MS } from "./harness-budget.ts";
-import { attachesToChrome, launchSettingsKey } from "./browser-launch-key.ts";
-export { attachesToChrome, ownsBrowserViewport } from "./browser-launch-key.ts";
+import { launchSettingsKey } from "./browser-launch-key.ts";
import { refusedSlot, SharedToolLists, slotRefusal, type BrowserRoom } from "./browser-room.ts";
export interface BrowserSpawnSpec {
@@ -73,6 +72,14 @@ export function browserRuntimeEnv(overrides: Record<string, string | undefined>)
return { ...env, ...overrides };
}
+export function attachesToChrome(env: NodeJS.ProcessEnv): boolean {
+ return Boolean(env.AGENT_BROWSER_CDP || env.AGENT_BROWSER_AUTO_CONNECT);
+}
+
+export function ownsBrowserViewport(env: NodeJS.ProcessEnv): boolean {
+ return env.AGENT_BROWSER_HEADLESS === "1" && !attachesToChrome(env);
+}
+
export class TransportError extends Error {}
/** One timed browser action (ms), logged with OMB_BROWSER_TIMING=1; `inFlight` is this session's work already running. */
@@ -341,7 +348,7 @@ export class BrowserRuntime {
readonly pageSizes = new PageSizes({
apply: (session, size) => { const spec = this.gates.get(session)?.spec; return spec && this.applyViewport ? this.applyViewport(spec, size) : Promise.resolve(); },
lane: (session, work) => this.lanes.run(session, work, this.options.laneWaitMs),
- watched: (session) => this.watched(session),
+ watched: (session) => { const gate = this.gates.get(session); return Boolean(gate && gate.watchers > 0 && !gate.closing); },
});
private holds = new SecretHolds((session, clear) => this.lanes.run(session, () => this.agentAction(session, clear, false), this.options.laneWaitMs).then((ran) => ran !== null));
/** Strips held secrets (a filled password, a one-time code) from every tool result before the bot reads it. */
@@ -819,7 +826,6 @@ export class BrowserRuntime {
};
}
- watched(session: string): boolean { const gate = this.gates.get(session); return Boolean(gate && gate.watchers > 0 && !gate.closing); }
async settled(session: string): Promise<void> { await this.gates.get(session)?.idleClose; }
/** A bot call, a person's action or hold, or a close is under way in this session's browser. */
diff --git a/server/host/browser.test.ts b/server/host/browser.test.ts
index d6ec8fc32d..9d2b7473dc 100644
--- a/server/host/browser.test.ts
+++ b/server/host/browser.test.ts
@@ -695,34 +695,6 @@ describe("a running turn's agent-browser browser", () => {
}
});
- it.each([true, false])("keeps watched desktop tabs past the idle cutoff (used by a bot: %s)", async (usedByBot) => {
- ready();
- const session = "bot-scout";
- const { host } = setup(undefined);
- daemons([session]);
- const closing = vi.spyOn(BrowserRuntime.prototype, "closeIfIdle");
- await host.browserRuntime.closeIfIdle(session, HOUR * 1_000, keepsSignIns);
- const agentBrowser = closing.mock.contexts[0] as BrowserRuntime;
- closing.mockRestore();
- if (usedByBot) host.usedBrowserSessions.set(session, keepsSignIns);
- const unwatch = agentBrowser.watch(session, keepsSignIns);
- vi.useFakeTimers({ toFake: ["setInterval", "clearInterval"] });
- anHourLater();
- try {
- host.browserSweep.start();
- await vi.advanceTimersByTimeAsync(0);
- expect(runningBrowserSessionNames).toHaveBeenCalled();
- expect(closedSessions()).toEqual([]);
- unwatch();
- await vi.advanceTimersByTimeAsync(60_000);
- expect(closedSessions()).toEqual([session]);
- } finally {
- unwatch();
- await host.browserSweep.closeAll();
- vi.useRealTimers();
- }
- });
-
it("is closed for a bot waiting on a full limit only once it sat unused for the idle time and keeps its sign-ins", async () => {
ready();
const { host } = setup(undefined, { cloudHome: true, turnResources: holding(["bot-a", "bot-t"]), browsersAtOnce: () => 2 });
diff --git a/server/host/browser.ts b/server/host/browser.ts
index 23603e0b69..f0dd46d71c 100644
--- a/server/host/browser.ts
+++ b/server/host/browser.ts
@@ -383,7 +383,7 @@ export function createBrowserHost(deps: BrowserHostDeps) {
engine: () => { const engine = browserEngineStatus(); return engine.kind === "ready" ? engine.binaryPath : null; },
owns: (session) => belongs(session) || (Boolean(CLOUD_HOME) && BOT_SESSION.test(session)),
live: (session) => usedBrowserSessions.has(session) && belongs(session),
- held: (session) => !turnResources.free(`browser:${session}`) || (DESKTOP_MANAGED && agentBrowserRuntime.watched(session)),
+ held: (session) => !turnResources.free(`browser:${session}`),
closeIdle: (session) => browserRuntime.closeIfIdle(session, idleMs(), usedBrowserSessions.get(session)),
});
// A browser that has ended keeps no loopback listener: its gate opens again with it, on the same port where it can, and