Skip to content

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

6.1 KB
Raw Blame
import { describe, expect, it } from "vitest";

import { cloudScreenSize, createBurgerDesk, jpegSize } from "./desk.ts";

/** The smallest JPEG header that names a frame of `width` x `height`. */
function jpeg(width: number, height: number): Buffer {
  const app0 = Buffer.from([0xff, 0xe0, 0x00, 0x04, 0x00, 0x00]);
  const sof = Buffer.from([0xff, 0xc0, 0x00, 0x11, 0x08, height >> 8, height & 0xff, width >> 8, width & 0xff, 0x03, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
  return Buffer.concat([Buffer.from([0xff, 0xd8]), app0, sof]);
}

const CAP = { kind: "computer", botId: "b", threadId: "t", generation: "g1" };

describe("the Skill Burger's desk", () => {
  it("reads the cloud screen's size from a screenshot or a size read", () => {
    expect(jpegSize(jpeg(1280, 800))).toEqual({ width: 1280, height: 800 });
    expect(jpegSize(Buffer.from("not a jpeg"))).toBeNull();
    expect(cloudScreenSize("screenshot", { content: [{ type: "image", mimeType: "image/jpeg", data: jpeg(1024, 768).toString("base64") }] })).toEqual({ width: 1024, height: 768 });
    expect(cloudScreenSize("get_screen_size", { content: [{ type: "text", text: JSON.stringify({ ok: true, stdout: "1920 1080\n" }) }] })).toEqual({ width: 1920, height: 1080 });
    expect(cloudScreenSize("click", { content: [] })).toBeNull();
  });

  it("records each cloud call with the screen last seen, and keeps the request's calls for its card", async () => {
    const desk = createBurgerDesk({ enabled: () => true });
    await desk.cloud({ ...CAP, boxId: null }, { method: "tools/call", params: { name: "screenshot", arguments: {} } }, Promise.resolve({ content: [{ type: "image", data: jpeg(1280, 800).toString("base64") }] }));
    await desk.cloud({ ...CAP, boxId: null }, { method: "tools/call", params: { name: "key_press", arguments: { key: "ctrl+s" } } }, Promise.resolve({ content: [] }));
    await desk.cloud(CAP, { method: "tools/list" }, Promise.resolve({ tools: [] }));
    desk.settled("t", "ask1", new Set(["g1"]));
    const snapshot = desk.snapshot("t")!;
    expect(snapshot).toMatchObject({ requestId: "ask1", complete: true, tainted: false });
    expect(snapshot.calls.map((call) => [call.tool, call.arguments, call.place, call.screen])).toEqual([
      ["screenshot", {}, "cloud", { width: 1280, height: 800 }],
      ["key_press", { key: "ctrl+s" }, "cloud", { width: 1280, height: 800 }],
    ]);
    expect(desk.snapshot("t")).toEqual(snapshot);
    desk.ended("t", new Set(["g2"]));
    expect(desk.snapshot("t")).toBeNull();
  });

  it("marks a request incomplete when a call it counted never answered, and records nothing while it is off", async () => {
    const desk = createBurgerDesk({ enabled: () => true });
    await desk.cloud(CAP, { method: "tools/call", params: { name: "click", arguments: { x: 1, y: 1 } } }, Promise.reject(new Error("lost"))).catch(() => {});
    desk.settled("t", "ask1", new Set(["g1"]));
    expect(desk.snapshot("t")?.complete).toBe(false);
    const off = createBurgerDesk({ enabled: () => false });
    off.gateChecked(CAP);
    off.record(CAP, { tool: "click", arguments: {}, result: { isError: false } }, "yourComputer");
    off.settled("t", "ask1", new Set(["g1"]));
    expect(off.snapshot("t")).toBeNull();
  });

  it("knows a driver turn's place and refuses team and cloud computers", () => {
    const desk = createBurgerDesk({ enabled: () => true });
    expect(desk.driverPlace(CAP)).toBe("yourComputer");
    expect(desk.driverPlace(CAP, "vm")).toBe("localVm");
    expect(desk.driverPlace({ ...CAP, localVmTarget: { key: "x" } })).toBe("localVm");
    expect(desk.driverPlace({ ...CAP, boxId: null })).toBeNull();
    expect(desk.driverPlace({ ...CAP, teamComputerId: "c" })).toBeNull();
    expect(desk.driverPlace({ ...CAP, kind: "agents" })).toBeNull();
  });

  it("hands a run only the computer its own turn mounted, with the server's replay minting, and forgets it when the request ends", () => {
    const desk = createBurgerDesk({ enabled: () => true, replayToken: (token) => `replay-of-${token}` });
    desk.remember("t", { generation: "g1", computer: { command: "node", args: [], env: {} }, approvalFull: () => true });
    expect(desk.turn("t", "g1")?.computer?.command).toBe("node");
    expect(desk.turn("t", "g1")?.replay?.("x")).toBe("replay-of-x");
    expect(desk.turn("t", "g2")).toBeNull();
    expect(desk.turn("other", "g1")).toBeNull();
    desk.settled("t", "ask1", new Set(["g1"]));
    expect(desk.turn("t", "g1")).toBeNull();
    desk.remember("t", { generation: "g2", approvalFull: () => true });
    desk.ended("t", new Set(["g2"]));
    expect(desk.turn("t", "g2")).toBeNull();
  });

  it("leaves a replay's own calls out of the count and the record", async () => {
    const desk = createBurgerDesk({ enabled: () => true });
    await desk.cloud({ ...CAP, boxId: null, replay: true }, { method: "tools/call", params: { name: "click", arguments: { x: 1, y: 1 } } }, Promise.reject(new Error("lost"))).catch(() => {});
    await desk.cloud({ ...CAP, boxId: null }, { method: "tools/call", params: { name: "key_press", arguments: { key: "a" } } }, Promise.resolve({ content: [] }));
    desk.settled("t", "ask1", new Set(["g1"]));
    expect(desk.snapshot("t")).toMatchObject({ complete: true, calls: [{ tool: "key_press" }] });
  });

  it("keeps finished requests' calls only within its byte budget, oldest dropped first", () => {
    const entry = (text: string) => ({ tool: "type_text", arguments: { text }, result: { isError: false } });
    const desk = createBurgerDesk({ enabled: () => true, snapshotBytes: 800 });
    for (const [thread, size] of [["t1", 100], ["t2", 100], ["t3", 2_000]] as const) {
      const cap = { ...CAP, threadId: thread };
      desk.gateChecked(cap);
      desk.record(cap, entry("x".repeat(size)), "yourComputer");
      desk.settled(thread, "ask", new Set(["g1"]));
    }
    expect(desk.snapshot("t3")).toBeNull();
    expect(desk.snapshot("t1")).not.toBeNull();
    const cap = { ...CAP, threadId: "t4" };
    desk.gateChecked(cap);
    desk.record(cap, entry("x".repeat(450)), "yourComputer");
    desk.settled("t4", "ask", new Set(["g1"]));
    expect([desk.snapshot("t1"), desk.snapshot("t2"), desk.snapshot("t4")].map(Boolean)).toEqual([false, true, true]);
  });
});