Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
A turn parked behind a person's hold on the shared Local VM stays parked until the hand-back
Xiaolei Shi652e43ce0eee
diff --git a/server/group-local-vm.e2e.test.ts b/server/group-local-vm.e2e.test.tsindex e2fb6bcec7..f6e6a30f69 100644--- a/server/group-local-vm.e2e.test.ts+++ b/server/group-local-vm.e2e.test.ts@@ -4,6 +4,7 @@ import { createServer, type Server } from "node:http";import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { dirname, join } from "node:path";+import { DatabaseSync } from "node:sqlite";import { fileURLToPath, pathToFileURL } from "node:url";import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";import { writeFileAtomic } from "./atomic.ts";@@ -54,6 +55,8 @@ const dump = () => until(() => {if (!existsSync(dumpFile)) return null;try { return JSON.parse(readFileSync(dumpFile, "utf8")); } catch { return null; }}, Boolean);+const activityRows = async (threadId: string, text: string) => (await api("GET", `/api/threads/${threadId}/messages`)).messages+ .filter((message: any) => message.tool?.name?.includes(text)).length;// The seeded Chief answers a failed run with a Problems to look at turn on this same engine, and that turn writes the dump.async function stepDownSeededChief() {const seeded = (await api("GET", "/api/bots?messages=0")).bots.find((bot: any) => bot.chiefOfStaff);@@ -1017,6 +1020,8 @@ describe("Group Local VM ownership on the real isolated server", () => {});const task = parked.bots.find((bot: any) => bot.id === auto.id).tasks.find((task: any) => task.threadId === auto.threadId);expect(task).toMatchObject({ busy: false, activity: "parked.computer" });+ await new Promise(resolve => setTimeout(resolve, 2_500));+ expect(await activityRows(auto.threadId, "Parked — it continues automatically")).toBe(1);expect(existsSync(dumpFile)).toBe(false);expect(JSON.parse(readFileSync(stateFile, "utf8")).actions ?? []).toEqual([]);await api("POST", `/api/bots/${holder.id}/interrupt`, { threadId: holder.threadId });@@ -1038,6 +1043,40 @@ describe("Group Local VM ownership on the real isolated server", () => {}}, 60_000);+ it("keeps a turn parked while a person holds the shared VM, and resumes it on the hand-back", async () => {+ await waitForExit(child, { signal: "SIGTERM" });+ await startServer(2_000);+ vmState({ containers: ["shared"], stopped: ["shared"] });+ rmSync(dumpFile, { force: true }); rmSync(finishFile, { force: true });+ const { bot } = await api("POST", "/api/bots", { name: "Parked behind a person", computer: "vm", browser: false });+ try {+ await api("GET", "/api/local-computer");+ await api("POST", `/api/bots/${bot.id}/computer/control`, { action: "take" });+ await api("POST", `/api/bots/${bot.id}/messages`, { text: "Use the shared VM after the person" });+ await until(() => activityRows(bot.threadId, "Parked — it continues automatically"), count => count === 1);+ await new Promise(resolve => setTimeout(resolve, 5_000));+ expect(await activityRows(bot.threadId, "Waiting for its turn on this computer")).toBe(1);+ expect(await activityRows(bot.threadId, "Parked — it continues automatically")).toBe(1);+ expect(existsSync(dumpFile)).toBe(false);+ const root = readHandoffs(join(fixtureHome, "data")).findLast(node => node.threadId === bot.threadId)!;+ const db = new DatabaseSync(join(fixtureHome, "data", "runtime.db"), { readOnly: true });+ try {+ expect((db.prepare("SELECT detail FROM assignment_events WHERE assignment_id = ? AND kind = 'held'").all(root.id) as Array<{ detail: string }>).at(-1)?.detail)+ .toBe("waiting for the computer it needs, which another turn or a person is using");+ } finally { db.close(); }+ await api("POST", `/api/bots/${bot.id}/computer/control`, { action: "release" });+ expect(await dump()).toMatchObject({ prompt: { message: { content: expect.stringContaining("Continue the task that parked waiting for it") } } });+ } finally {+ await api("POST", `/api/bots/${bot.id}/computer/control`, { action: "release" });+ await api("POST", `/api/bots/${bot.id}/interrupt`, { threadId: bot.threadId });+ await idle(bot.id);+ await api("DELETE", `/api/bots/${bot.id}`);+ vmState();+ await waitForExit(child, { signal: "SIGTERM" });+ await startServer();+ }+ }, 60_000);+it("runs a screen-less Auto turn to completion while another thread holds the Local VM (issue #1361 AC1)", async () => {vmState(); rmSync(dumpFile, { force: true }); rmSync(finishFile, { force: true });const { bot: holder } = await api("POST", "/api/bots", { name: "VM holder" });diff --git a/server/host/queued-reason.test.ts b/server/host/queued-reason.test.tsindex 08c0a22dae..f639facbd1 100644--- a/server/host/queued-reason.test.ts+++ b/server/host/queued-reason.test.ts@@ -25,7 +25,7 @@ describe("why a queued handoff has not started", () => {});it("says when it waits for a computer, an earlier turn in its thread, or a room turn", () => {- expect(reason({ waitsForComputer: () => true })(node())).toBe("waiting for the computer it needs, which another turn is using");+ expect(reason({ waitsForComputer: () => true })(node())).toBe("waiting for the computer it needs, which another turn or a person is using");expect(reason({ threadBusy: (_bot, threadId) => threadId === "t-new" })(node())).toBe("waiting for an earlier turn in that thread to finish");expect(reason({ slots: () => ({ busy: [], limit: 2 }), groupTurn: () => true })(node())).toBe("waiting for Ops to finish its turn in a room");});diff --git a/server/host/queued-reason.ts b/server/host/queued-reason.tsindex 57365a7732..91279b8275 100644--- a/server/host/queued-reason.ts+++ b/server/host/queued-reason.ts@@ -3,7 +3,7 @@ import type { HandoffView } from "../coordinator.ts";export interface QueuedReasonDeps {botName(botId: string): string | undefined;- /** The handoff already ran and is parked until a computer another turn uses is free. */+ /** The handoff already ran and is parked until the computer it needs is free of other turns and of a person's hold. */waitsForComputer(node: HandoffView): boolean;threadBusy(botId: string, threadId: string): boolean;/** The bot's threads that hold a slot now, and how many it may run at once when a bot starts the work. */@@ -20,7 +20,7 @@ export function createQueuedReason(deps: QueuedReasonDeps) {// A room's handoff waits on the room, not on these.if (node.groupId) return "waiting for that teammate to be free";const name = deps.botName(node.botId) ?? "that teammate";- if (deps.waitsForComputer(node)) return "waiting for the computer it needs, which another turn is using";+ if (deps.waitsForComputer(node)) return "waiting for the computer it needs, which another turn or a person is using";if (deps.threadBusy(node.botId, node.threadId)) return "waiting for an earlier turn in that thread to finish";const { busy, limit } = deps.slots(node.botId);if (busy.length >= limit) {diff --git a/server/index.ts b/server/index.tsindex 1fc7c3e634..813f428801 100644--- a/server/index.ts+++ b/server/index.ts@@ -2093,6 +2093,7 @@ function claimTurnResource(owner: TurnOwner, resource: string): boolean {turnResourceOwners.set(owner.threadId, owner);return true;}+const parkedSeatTaken = (nodeId: string): boolean => { const seat = parkedSeats.get(nodeId); return seat !== undefined && (!turnResources.free(seat) || sharedVm.personHolds(seat)); };function releaseTurnResources(owner: TurnOwner | undefined): void {if (!owner) return;@@ -3764,7 +3765,7 @@ function coordinationSystemInstructions(): string {const threadReadableFrom = (threadId: string) => (other: string): boolean => !proactive.fence.confines(other) && !botApiThread(other) && (CLOUD_TEAM ? teamHost.readableFrom(threadId)(other) : !CLOUD_HOME || cloudOwnerOnlyThread(other));const queuedDeps = { botName: (id: string) => store.bot(id)?.name, threadBusy, groupTurn: (id: string) => Boolean(activeGroupTurnForBot(id)), behindPerson: queuedBehindPerson,- waitsForComputer: (node: HandoffView) => { const seat = parkedSeats.get(node.id); return Boolean(seat && !turnResources.free(seat)); },+ waitsForComputer: (node: HandoffView) => parkedSeatTaken(node.id),slots: (id: string) => ({ busy: store.tasks(id).filter((task) => threadBusy(id, task.threadId)).map(({ threadId, title }) => ({ threadId, title })), limit: maxConcurrentChildThreads(cfg, botThreadsHost(cloudPlanTier.starter())) }) }; const queuedStatus = createQueuedReason(queuedDeps), queuedNotes = createQueuedNotes({ ...queuedDeps, now: Date.now, language: (threadId) => threadLanguage(threadId), parent: (node) => node.parentId ? coordinator.node(node.parentId) : undefined, node: (id) => coordinator.node(id), post: (parent, node, line) => { const bot = store.bot(node.botId), title = store.taskByThread(node.botId, node.threadId)?.title; store.appendMessage(parent.threadId, { role: "bot", kind: "activity", ...(bot ? { from: { botId: bot.id, name: bot.name, color: bot.color } } : {}), tool: { name: line, ok: true }, ...(title ? { threadRef: { botId: node.botId, threadId: node.threadId, title } } : {}) }); } });const asksActingPerson = createAsksActingPerson({ team: Boolean(CLOUD_TEAM), requester: (threadId, generation) => actingPeople.requester(threadId, generation), askedPerson: (botId, threadId) => teamOwners.routing.askedPerson(botId, threadId) });const teammatePrompts = createTeammatePrompts({ store, asksActingPerson, outstandingDirect: (threadId) => coordinator.outstandingDirect(threadId), openPersonCard, queuedStatus, isUnattended, fullAccess: fullAccessForSource, startedOutside, guestDriven: cloudGuestDriven, confined: (threadId) => proactive.fence.confines(threadId), readableFrom: threadReadableFrom, waitingForPerson: (chief, teammates, readable) => questionRouting.waitingForPerson(chief, teammates, readable) });@@ -3911,8 +3912,7 @@ const handoffTicks = createHandoffTicks(), coordinator: Coordinator = createCoor: !node.parentId && node.groupId ? roomRootProblem(node): roomHandoffProblem(node, parent),busy: n => {- const seat = parkedSeats.get(n.id);- if (seat && !turnResources.free(seat)) return true;+ if (parkedSeatTaken(n.id)) return true;if (n.groupId) return Boolean(store.bot(n.botId)?.busy || groupThreadIsWorking(n.groupId, n.threadId));if (canAdmitDirectTurn(n.botId, n.threadId)) return false;noteQueuedBehindPerson(n); queuedNotes(n);