Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Let a teammate's hand-off move to the built-in browser
AdityaUmalede84eeac861f
diff --git a/server/host/handoff-browser.test.ts b/server/host/handoff-browser.test.tsnew file mode 100644index 0000000000..5d8eb2565b--- /dev/null+++ b/server/host/handoff-browser.test.ts@@ -0,0 +1,8 @@+import { expect, it } from "vitest";+import { handoffSurface } from "./handoff-browser.ts";++it("lets a hand-off ask only for the built-in browser, auto included", () => {+ expect(handoffSurface("browser")).toBe("browser");+ expect(handoffSurface("auto")).toBe("browser");+ for (const asked of ["cloud", "vm", "local", undefined, 3]) expect(handoffSurface(asked)).toBeNull();+});diff --git a/server/host/handoff-browser.ts b/server/host/handoff-browser.tsnew file mode 100644index 0000000000..100d06c095--- /dev/null+++ b/server/host/handoff-browser.ts@@ -0,0 +1,10 @@+// A teammate's hand-off may move only to the built-in browser with select_computer, never to another computer: no person+// watches a hand-off's Screen panel, and fill_login needs the bot's own browser. The turn that continues there owns the result.+export const HANDOFF_BROWSER_ONLY = "A teammate's hand-off can move only to the built-in browser: call select_computer with surface browser, or carry on with the computer already in use.";++export const HANDOFF_BROWSER_NOTE = " This is a teammate's hand-off: when it needs the built-in browser (for example to sign in with a saved login), call select_computer with surface browser and end this turn; the hand-off continues there and its result still returns. It cannot move to any other computer.";++/** The place a hand-off's select_computer asks for: auto means the built-in browser; any other computer is refused. */+export function handoffSurface(asked: unknown): "browser" | null {+ return asked === "auto" || asked === "browser" ? "browser" : null;+}diff --git a/server/index.ts b/server/index.tsindex 2aee79bbd3..17ede31629 100644--- a/server/index.ts+++ b/server/index.ts@@ -271,7 +271,7 @@ import {SendSequencer,} from "./send-idempotency.ts";import { EventBus } from "./harness/bus.ts";-import { ENGINE_LIST_REUSE_MS, engineMemory, enginesByUse, ProviderRegistry } from "./harness/registry.ts"; import { afterFirstEngineRead, holdUntilFirstEngineRead } from "./first-engine-read.ts";+import { ENGINE_LIST_REUSE_MS, engineMemory, enginesByUse, ProviderRegistry } from "./harness/registry.ts"; import { afterFirstEngineRead, holdUntilFirstEngineRead } from "./first-engine-read.ts"; import { HANDOFF_BROWSER_NOTE, HANDOFF_BROWSER_ONLY, handoffSurface } from "./host/handoff-browser.ts";import { ManagedDesktopProviders } from "./managed-desktop.ts";import { mainThread, wasOldestThread } from "./main-thread.ts";import { computerKindForResource, ManagedDesktopPolicy, type ComputerKind } from "./managed-policy.ts"; import { nudgeCloudTeamPolicy, remoteAccessPaths, startCloudTeamPolicy, stopCloudTeamPolicy } from "./cloud-team-policy.ts";@@ -1631,9 +1631,9 @@ const internalGenerationByProviderTurn = new ProviderTurnGenerationRegistry();const computerSelectionTurns = new Map<string, {generation: string;botId: string;- source: Message;+ source?: Message; // none for a teammate's hand-off, which moves only to the browser and continues with `handoff.opts`text: string;- dataContext?: DataContext;+ dataContext?: DataContext; handoff?: { opts: Parameters<typeof startTurn>[2]; outcome?: DirectTurnOutcome };mounted?: Surface;selected?: Surface;previousSurface?: Surface;@@ -2011,6 +2011,7 @@ function hostTurnResult(node: HandoffView | undefined, generation: string, outcofunction settleDirectCoordination(generation: string | undefined, outcome: DirectTurnOutcome) {if (!generation) return;+ const moving = [...computerSelectionTurns.values()].find(turn => turn.generation === generation && turn.handoff && turn.selected); if (moving && directCoordinationSettlers.has(generation)) { moving.handoff!.outcome ??= outcome; return; } // kept for the turn continuing in the browserconst settler = directCoordinationSettlers.get(generation);directCoordinationSettlers.delete(generation);const node = coordinator.node(settler?.nodeId ?? generation);@@ -6103,7 +6104,7 @@ function browserSelectable(bot: BotRecord): boolean {function selectTurnPlace(turn: { botId: string; threadId: string; generation: string }, surface: Surface): boolean {const source = computerSelectionTurns.get(turn.threadId);const bot = store.projectBotForTask(turn.botId, turn.threadId);- if (!source || source.generation !== turn.generation || !bot || bot.computer === "off") return false;+ if (!source || source.generation !== turn.generation || !bot || bot.computer === "off" || (source.handoff && surface !== "browser")) return false;if (source.selected) return source.selected === surface;source.selected = surface;source.previousSurface = store.taskByThread(bot.id, bot.threadId)?.surface;@@ -6113,26 +6114,24 @@ function selectTurnPlace(turn: { botId: string; threadId: string; generation: stfunction continueComputerSelection(threadId: string, generation: string | undefined, succeeded: boolean): boolean {const selection = computerSelectionTurns.get(threadId);if (!selection || selection.generation !== generation) return false;- if (!succeeded || !selection.selected || hasQueuedSteeredMessages(selection.botId, threadId)) {- computerSelectionTurns.delete(threadId);- return false;- }+ // A hand-off that does not continue still returns the result it kept back.+ const giveUp = () => { if (computerSelectionTurns.get(threadId) === selection) computerSelectionTurns.delete(threadId); if (selection.handoff?.outcome) settleDirectCoordination(generation, selection.handoff.outcome); };+ if (!succeeded || !selection.selected || hasQueuedSteeredMessages(selection.botId, threadId)) return giveUp(), false;const surface = selection.selected;setImmediate(() => {// Stop, deletion, a new user send, or any replacement generation wins.- if (computerSelectionTurns.get(threadId) !== selection || directTurnGenerationByThread.get(threadId) !== generation) return;+ if (computerSelectionTurns.get(threadId) !== selection || directTurnGenerationByThread.get(threadId) !== generation) return giveUp();computerSelectionTurns.delete(threadId);const bot = store.projectBotForTask(selection.botId, threadId);- if (!bot || bot.computer === "off" || threadBusy(bot.id, threadId)) return;- if (store.taskByThread(bot.id, threadId)?.surface !== selection.previousSurface) return;- if (hasQueuedSteeredMessages(bot.id, threadId)) { drainQueuedSends(); return; }- if (store.activePath(threadId).findLast(message => message.role === "user" && message.kind === "text")?.id !== selection.source.id) return;+ if (!bot || bot.computer === "off" || threadBusy(bot.id, threadId)) return giveUp();+ if (store.taskByThread(bot.id, threadId)?.surface !== selection.previousSurface) return giveUp();+ if (hasQueuedSteeredMessages(bot.id, threadId)) { giveUp(); drainQueuedSends(); return; }+ if (selection.source && store.activePath(threadId).findLast(message => message.role === "user" && message.kind === "text")?.id !== selection.source.id) return;store.patchTask(bot.id, threadId, { surface, surfaceSource: "auto" });- const text = `The computer selection is now ${surfaceLabel(surface)}. Continue the user's original request using the tools mounted for this turn; verify the result before claiming success.\n\n${selection.text}`;- void startTurn(bot.id, text, { threadId, userMessage: selection.source, computerSelectionContinuation: true, dataContext: selection.dataContext }).catch(error => {- if (store.taskByThread(bot.id, threadId)) store.appendMessage(threadId, { role: "bot", kind: "activity",- tool: { name: `Could not continue on ${surfaceLabel(surface)}: ${error instanceof Error ? error.message : String(error)}`, ok: false } });- });+ const text = `The computer selection is now ${surfaceLabel(surface)}. Continue the ${selection.handoff ? "handed-off" : "user's original"} request using the tools mounted for this turn; verify the result before claiming success.\n\n${selection.text}`;+ if (selection.handoff) directCoordinationSettlers.delete(generation!); /* the continuing turn owns the hand-off's result */ void startTurn(bot.id, text, selection.handoff ? { ...selection.handoff.opts, threadId, computerSelectionContinuation: true } : { threadId, userMessage: selection.source, computerSelectionContinuation: true, dataContext: selection.dataContext }).catch(error => {+ const why = `Could not continue on ${surfaceLabel(surface)}: ${error instanceof Error ? error.message : String(error)}`; selection.handoff?.opts?.coordination?.settle({ ok: false, text: why });+ if (store.taskByThread(bot.id, threadId)) store.appendMessage(threadId, { role: "bot", kind: "activity", tool: { name: why, ok: false } }); });});return true;}@@ -7768,7 +7767,7 @@ async function beginTurn(!opts?.commsDepth && !opts?.coordination && !inheritedTeamComputer(bot) && bot.computer !== "off" && agentsMounted) {const source = store.activePath(threadId).findLast(message => message.id === userMessage?.id && message.role === "user" && !message.peerAsk);if (source) computerSelectionTurns.set(threadId, { generation: dispatchClaimId, botId: bot.id, source, text, dataContext: opts?.dataContext });- }+ } else if (opts?.coordination && opts.commsDepth && !opts.computerSelectionContinuation && !opts.goalRunId && !opts.automationSource && !inheritedTeamComputer(bot) && bot.computer !== "off" && agentsMounted && browserSelectable(bot)) computerSelectionTurns.set(threadId, { generation: dispatchClaimId, botId: bot.id, text, handoff: { opts } });store.setTaskActivity(bot.id, threadId, "working");// Watch from admission, not dispatch: a turn can wedge in setup long before any provider event exists.watchdog.watch(threadId, bot.id);@@ -8380,7 +8379,7 @@ async function beginTurn({ id: "shell", label: "Long-running commands", text: worksInWorkspace ? longCommandsPrompt(computerPromptKind) : "" },{ id: "computer", label: "Computer", text: computerPrompt(computerPromptKind) },{ id: "team-computer", label: "Team computer", text: teamComputerPrompt(teamComputer) },- { id: "plan", label: "Surface", text: surfacePrompt({ computer: mountedComputer, browser: Boolean(integrations.browser) }, { pinned: plan.pinned, note: plan.note + vpsUnavailableNote + vmUpdateNote + (computerSkipped && plan.computer !== "off" ? computerSkippedNote(redactSecretsInText(computerSkipped)) : ""), canSelect: computerSelectionTurns.has(threadId), cloudHome: Boolean(CLOUD_HOME), botOpened: plan.botOpened, ownComputer: CLOUD_HOME && integrations.agents && lendingEnabled() ? sharedComputers.known(CLOUD_HOME_LENDER) : undefined }) },+ { id: "plan", label: "Surface", text: surfacePrompt({ computer: mountedComputer, browser: Boolean(integrations.browser) }, { pinned: plan.pinned, note: plan.note + vpsUnavailableNote + vmUpdateNote + (computerSkipped && plan.computer !== "off" ? computerSkippedNote(redactSecretsInText(computerSkipped)) : "") + (computerSelectionTurns.get(threadId)?.handoff && !integrations.browser ? HANDOFF_BROWSER_NOTE : ""), canSelect: computerSelectionTurns.has(threadId) && !computerSelectionTurns.get(threadId)?.handoff, cloudHome: Boolean(CLOUD_HOME), botOpened: plan.botOpened, ownComputer: CLOUD_HOME && integrations.agents && lendingEnabled() ? sharedComputers.known(CLOUD_HOME_LENDER) : undefined }) },{ id: "cloud-home", label: "My Cloud", text: CLOUD_HOME ? cloudHomePrompt(Boolean(integrations.agents) && lendingEnabled(), syncedFolders(DATA_DIR), sharedComputers.known(CLOUD_HOME_LENDER), CLOUD_PERSONAL) + firstHourHooks.prompt(bot.id, threadId) : "" },{ id: "composio", label: "Connected apps", text: integrations.composio ? composioSystemPrompt(liveBot?.connectorTools ?? bot.connectorTools) + describeConnectorScopes((liveBot ?? bot).connectorScopes) : "" },{ id: "mcp", label: "MCP servers", text: customMcpPrompt(Object.keys(integrations.custom ?? {}), mcpServersHost.off(bot)) }, { id: "to-connect", label: "Connections to make", text: cloudConnections.prompt(liveBot ?? bot, lentMcp.lines(liveBot ?? bot, { botId: bot.id, threadId, generation: dispatchClaimId })) + teamConnections.prompt(threadId, dispatchClaimId, integrations) },@@ -13014,7 +13013,8 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {const bot = store.projectBotForTask(internalSender.id, internalCapability.threadId);const canSelect = Boolean(source && source.generation === internalCapability.generation && bot && bot.computer !== "off");if (!bot) return json(res, 403, { error: "Computer selection belongs to a direct bot conversation." });- const requested = method === "POST" ? (await readInternalBody()).surface : undefined;+ const asked = method === "POST" ? (await readInternalBody()).surface : undefined, browserOnly = canSelect && Boolean(source?.handoff);+ if (browserOnly && method === "POST" && !handoffSurface(asked)) return json(res, 403, { error: HANDOFF_BROWSER_ONLY }); const requested = browserOnly && method === "POST" ? handoffSurface(asked) : asked;const request = directRequestOwners.get(internalCapability.threadId);const refusal = canSelect ? undefined : bot.computer === "off" ? COMPUTER_OFF_REFUSAL : computerPickRefusal(request?.generation === internalCapability.generation ? request.kind : undefined);if (method === "POST" && refusal) return json(res, 403, { error: refusal });@@ -13022,10 +13022,10 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {return json(res, 400, { error: "surface must be auto, cloud, vm, local, or browser" });}const unoffered = CLOUD_HOME && method === "POST" && requested !== "auto" ? cloudHomePlaceRefusal(parseSurface(requested)!) : undefined;- const options = await selectableComputers(bot), chosen = chosenPlace(store.taskByThread(bot.id, internalCapability.threadId), bot.computer);+ const options = (await selectableComputers(bot)).filter(option => !browserOnly || option.surface === "browser"), chosen = browserOnly ? undefined : chosenPlace(store.taskByThread(bot.id, internalCapability.threadId), bot.computer);const current = source ? source.mounted ?? "off" : await computerPreviewSurface(bot, bot.threadId);requireActiveInternalCapability();- if (method === "GET") return json(res, 200, { current, canSelect, ...(refusal ? { why: refusal } : {}), ...(chosen ? { chosen, chosenNote: `The person chose ${surfaceLabel(chosen)} for this conversation, and auto keeps it while it is available. Select another place only when they ask for one or it is unavailable.` } : {}), options });+ if (method === "GET") return json(res, 200, { current, canSelect, ...(refusal ? { why: refusal } : browserOnly ? { why: HANDOFF_BROWSER_ONLY } : {}), ...(chosen ? { chosen, chosenNote: `The person chose ${surfaceLabel(chosen)} for this conversation, and auto keeps it while it is available. Select another place only when they ask for one or it is unavailable.` } : {}), options });if (computerSelectionTurns.get(internalCapability.threadId) !== source) return json(res, 409, { error: "The user request ended before its computer was selected." });if (unoffered) return json(res, 409, { error: unoffered, options });const claim = autoVmClaims.get(internalCapability.threadId);diff --git a/server/surface-pins.e2e.test.ts b/server/surface-pins.e2e.test.tsindex fa716246c2..b80f1e4fe2 100644--- a/server/surface-pins.e2e.test.ts+++ b/server/surface-pins.e2e.test.ts@@ -141,6 +141,9 @@ describe("surface pin provenance against the real server", () => {driver: "claudeAgent", config: { cli: join(ROOT, "server/testing/fake-claude-cli.ts") },// The first computer call is what reaches the Boat, as with a real model.environment: { FAKE_CLAUDE_MODE: "slow", FAKE_CLAUDE_DUMP: dumpFile, FAKE_CLAUDE_SLOW_FINISH_GATE: finishFile, FAKE_CLAUDE_USES_CLOUD_COMPUTER: "1" },+ }, teammate: {+ driver: "claudeAgent", config: { cli: join(ROOT, "server/testing/fake-claude-cli.ts") },+ environment: { FAKE_CLAUDE_MODE: "slow", FAKE_CLAUDE_DUMP: join(home, "teammate-dump.json"), FAKE_CLAUDE_SLOW_FINISH_GATE: join(home, "teammate-finish"), FAKE_CLAUDE_USES_CLOUD_COMPUTER: "1" },} } }));});afterAll(async () => {@@ -460,4 +463,55 @@ describe("surface pin provenance against the real server", () => {await apiOk("DELETE", `/api/bots/${bot.id}`);await stop();});++ it("lets a teammate's hand-off move to the built-in browser only, and returns the result from there", async () => {+ // The teammate runs on its own fixture engine, so its turn waits on its own gate while the asker's turn ends.+ const teammateDump = join(home, "teammate-dump.json"), teammateFinish = join(home, "teammate-finish");+ rmSync(teammateDump, { force: true });+ rmSync(teammateFinish, { force: true });+ await start(true);+ const asker = (await apiOk("POST", "/api/bots", { name: "Asker" })).bot;+ const tester = (await apiOk("POST", "/api/bots", { name: "Tester" })).bot;+ await apiOk("PATCH", `/api/bots/${tester.id}`, { modelSelection: { ...tester.modelSelection, instanceId: "teammate" } });+ await stop();+ editSavedBot(tester.id, saved => { saved.browserProfile = "guest"; });+ await start(true);+ const { task: own } = await apiOk("POST", `/api/bots/${asker.id}/tasks`, {});+ resetTurn();+ await apiOk("POST", `/api/bots/${asker.id}/messages`, { text: "Ask Tester to check the site.", threadId: own.threadId });+ const askerToken = (await dump()).mcpConfig.mcpServers.agents.env.OMB_COMMS_TOKEN;+ const handed = await fetch(base + "/api/internal/coordinate-bots", { method: "POST",+ headers: { authorization: `Bearer ${askerToken}`, "content-type": "application/json" },+ body: JSON.stringify({ botIds: [tester.id], message: "Sign in to the shop and check the order page." }) });+ expect(handed.status).toBeLessThan(400);+ writeFileSync(finishFile, "finish");+ const teammateTurn = (): Promise<any> => until((): any => {+ if (!existsSync(teammateDump)) return null;+ try { return JSON.parse(readFileSync(teammateDump, "utf8")); } catch { return null; }+ }, Boolean);+ const token = (await teammateTurn()).mcpConfig.mcpServers.agents.env.OMB_COMMS_TOKEN;+ const select = (body?: unknown) => fetch(base + "/api/internal/computer/select", { method: body ? "POST" : "GET",+ headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, ...(body ? { body: JSON.stringify(body) } : {}) });+ const offered = await (await select()).json() as any;+ expect(offered).toMatchObject({ canSelect: true });+ expect(offered.options.map((option: any) => option.surface)).toEqual(["browser"]);+ const refused = await select({ surface: "vm" });+ expect(refused.status).toBe(403);+ expect((await refused.json() as any).error).toMatch(/only to the built-in browser/);+ expect(await (await select({ surface: "browser" })).json()).toMatchObject({ status: "pending", surface: "browser" });+ const workThread = (await apiOk("GET", "/api/bots?messages=0")).bots.find((bot: any) => bot.id === tester.id)+ .tasks.find((task: any) => task.openedBy?.botId === asker.id).threadId as string;+ rmSync(teammateDump, { force: true });+ writeFileSync(teammateFinish, "finish");+ const moved = await teammateTurn();+ expect(JSON.stringify(moved.prompt)).toContain("Continue the handed-off request");+ expect(moved.mcpConfig.mcpServers.browser).toBeDefined();+ expect(savedTask(tester.id, workThread)).toMatchObject({ surface: "browser" });+ // The asker hears back from the browser turn, never from the turn that only asked to move.+ const replies = async () => JSON.stringify((await apiOk("GET", `/api/threads/${own.threadId}/messages?limit=60`)).messages);+ expect(await until(replies, (text) => text.includes("Continue the handed-off request"))).toBeTruthy();+ await idle(tester.id, workThread);+ await idle(asker.id, own.threadId);+ await stop();+ }, 60_000);});