Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Honor turn cancellation and desktop control after VPS proxy startup
CI fixturec0e06e60e6a5
diff --git a/server/index.ts b/server/index.tsindex 9abe0ffdf7..00f3314e31 100644--- a/server/index.ts+++ b/server/index.ts@@ -12934,7 +12934,7 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {const abort = new AbortController();res.once("close", () => { if (!res.writableEnded) abort.abort(); });// A boxed turn's VPS computer is served from the host's own proxy (vps-computer-rpc.ts); no Boat is named.- if (internalCapability.boxId === undefined) return vpsComputerTurn({ body, res, json, botId: internalCapability.botId, assertActive: requireActiveInternalCapability, takeSequence: () => sharedVm.takeSequence(), gate: (since) => computerCallGate(internalCapability, abort.signal, undefined, since), signal: abort.signal });+ if (internalCapability.boxId === undefined) return await vpsComputerTurn({ body, res, json, botId: internalCapability.botId, assertActive: requireActiveInternalCapability, takeSequence: () => sharedVm.takeSequence(), gate: (since) => computerCallGate(internalCapability, callSignal, undefined, since), signal: callSignal });return json(res, 200, { result: await cloudComputerRpc(body, {cfg, signal: abort.signal, folder: () => messageFileRootsForThread(internalCapability.botId, internalCapability.threadId)[0],boxId: () => internalCapability.boxId ?? claimedBoatId(internalCapability),diff --git a/server/vps-computer-rpc.test.ts b/server/vps-computer-rpc.test.tsindex f76b7f73f9..ff5a506549 100644--- a/server/vps-computer-rpc.test.ts+++ b/server/vps-computer-rpc.test.ts@@ -194,10 +194,12 @@ describe("the boxed VPS computer's host-side proxy", () => {});it("a person who takes the desktop while a call waits for an earlier one refuses that call", async () => {- serve("slow", ["slow"]);+ const marker = join(home, "taken.txt");+ serve("slow", ["slow", marker]);const first = call("tools/call", { name: "screenshot" }, 20);const second = call("tools/call", { name: "click" }, 21);await until(() => turns.length, (arrived: number) => arrived >= 2);+ await until(() => callsReceived(marker), count => count === 1);takes.lastTake = ++takes.now;await expect(first).resolves.toMatchObject({ result: { content: [{ text: "did screenshot #1 on slow" }] } });expect((await second).result).toEqual({ content: [{ type: "text", text: HANDED_BACK }], isError: true });@@ -211,6 +213,7 @@ describe("the boxed VPS computer's host-side proxy", () => {const first = call("tools/call", { name: "screenshot" }, 30);const second = call("tools/call", { name: "click" }, 31);await until(() => turns.length, (arrived: number) => arrived >= 2);+ await until(() => callsReceived(marker), count => count === 1);turns[1]!.abort();await expect(first).resolves.toMatchObject({ result: { content: [{ text: "did screenshot #1 on slow" }] } });expect((await second).result).toMatchObject({ isError: true, content: [{ text: "The turn ended before this call reached the VPS computer, so nothing was run." }] });@@ -238,12 +241,16 @@ describe("the boxed VPS computer's host-side proxy", () => {it("a turn of a bot with no VPS computer answers 403", async () => {mountedBot = "other-bot";+ const response = await fetch(harnessUrl + "/api/internal/computer/mcp", { method: "POST", body: JSON.stringify({ method: "tools/list" }) });+ expect(response.status).toBe(403);+ await response.text();const answer = await call("tools/list", {}, 13);expect(answer.error?.code).toBe(-32603);expect(answer.error?.message).toContain("no cloud computer");});it("a re-provisioned container drops the child that pointed at the old one", async () => {+ await expect(call("tools/list")).resolves.toMatchObject({ result: { tools: [{ name: "screenshot-a" }] } });serve("b", ["b"]);await expect(call("tools/call", { name: "screenshot" }, 14)).resolves.toMatchObject({ result: { content: [{ text: "did screenshot #1 on b" }] } });await expect(call("tools/list", {}, 15)).resolves.toMatchObject({ result: { tools: [{ name: "screenshot-b" }] } });diff --git a/server/vps-computer-rpc.ts b/server/vps-computer-rpc.tsindex cfc2d4a25c..99bcffe19f 100644--- a/server/vps-computer-rpc.ts+++ b/server/vps-computer-rpc.ts@@ -96,12 +96,12 @@ export async function vpsComputerTurn(turn: {// a desktop a person took while it waited is never acted on.const previous = queues.get(turn.botId) ?? Promise.resolve();const queued = previous.then(async (): Promise<Record<string, unknown>> => {+ const child = await childFor(turn.botId, targets.get(turn.botId)!);if (method === "tools/call") {const state = await turn.gate(arrivedAt);if (state.held) return failedResult(controlRefusal(state));turn.assertActive();}- const child = await childFor(turn.botId, targets.get(turn.botId)!);try {return await child.rpc(method, turn.body.params ?? {}, turn.signal);} catch (error) {@@ -192,7 +192,7 @@ function openChild(botId: string, target: VpsTarget): Child {const timer = setTimeout(() => {pending.delete(id);reject(unanswered("The VPS computer did not answer in time. It may still have run; check its state before repeating anything."));- }, COMPUTER_PROXY_TIMEOUT_MS);+ }, COMPUTER_PROXY_TIMEOUT_MS - 5_000);timer.unref?.();const onAbort = () => {pending.delete(id);