Skip to content

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

6.9 KB
Raw Blame
import { existsSync, mkdtempSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, it } from "vitest";
import { launchVerificationServer } from "../scripts/control-omb.ts";
import { removeTempDir } from "./testing/cleanup.ts";

it("Chief creation uses the workspace default or a validated explicit model without inheriting permissions", async () => {
  const temp = mkdtempSync(join(tmpdir(), "omb-create-model-"));
  const gate = join(temp, "finish");
  const fixture = await launchVerificationServer({ FAKE_CLAUDE_MODE: "slow", FAKE_CLAUDE_SLOW_FINISH_GATE: gate }, undefined, undefined, undefined, undefined, undefined, ["codex"]);
  const api = async (method: string, path: string, body?: unknown, expected = 200, token?: string) => {
    const response = await fetch(fixture.info.url + path, { method,
      headers: { "content-type": "application/json", origin: fixture.info.url,
        ...(token ? { authorization: `Bearer ${token}` } : {}) },
      ...(body === undefined ? {} : { body: JSON.stringify(body) }) });
    const result = await response.json() as any;
    expect(response.status, JSON.stringify(result)).toBe(expected);
    return result;
  };
  try {
    const instances = (await api("GET", "/api/instances")).instances;
    const claude = instances.find((item: any) => item.instanceId === "claude");
    const codex = instances.find((item: any) => item.instanceId === "codex");
    const defaultModel = { instanceId: "codex", model: codex.models.default, effort: "low" };
    await api("PATCH", "/api/config", { defaultModelSelection: defaultModel });
    const chiefModel = { instanceId: "claude", model: claude.models.default };
    const chief = (await api("POST", "/api/bots", { name: "Chief", section: "Design", modelSelection: chiefModel }, 201)).bot;
    // At Ask too, each creation applies at once: OpenMausBot adds no card of its own.
    await api("PATCH", `/api/bots/${chief.id}`, { chiefOfStaff: true, approvalMode: "ask", connectorScopes: { apps: { gmail: "read" } } });
    await api("POST", `/api/bots/${chief.id}/messages`, { text: "Prepare a specialist." }, 202);
    await expect.poll(() => existsSync(fixture.fixtureDumpPath), { timeout: 15_000 }).toBe(true);
    const dump = JSON.parse(readFileSync(fixture.fixtureDumpPath, "utf8"));
    let token = dump.mcpConfig.mcpServers.agents.env.OMB_COMMS_TOKEN;
    const create = (name: string, modelSelection?: unknown, expected = 201) => api("POST", "/api/internal/create-bot", {
      fromBotId: chief.id, fromThreadId: chief.threadId, name,
      role: "Specialist", instructions: "Complete assigned work.",
      ...(modelSelection === undefined ? {} : { modelSelection }),
    }, expected, token);
    const idle = () => expect.poll(async () => (await api("GET", "/api/bots")).bots.every((bot: any) => !bot.busy), { timeout: 20_000 }).toBe(true);
    const applied = (result: any) => {
      expect(result).toMatchObject({ state: "applied", applied: true });
      return { id: result.id as string, modelSelection: result.modelSelection };
    };
    // A bot whose model the installed CLI can no longer run; a copy of it falls back to the workspace default.
    const staleModel = { instanceId: "claude", model: claude.models.options.find((item: any) => item.needsCliUpdate).id };
    const stale = (await api("POST", "/api/bots", { name: "Stale specialist", section: "Design", modelSelection: staleModel }, 201)).bot;
    const before = (await api("GET", "/api/bots")).bots.length;
    const first = applied(await create("Default specialist"));
    // The installed CLI is too old for the newest model: a proposal may not name it, and the explicit pick is one it can run.
    const tooNew = claude.models.options.find((item: any) => item.needsCliUpdate);
    expect(tooNew).toBeTruthy();
    const explicit = { instanceId: "claude", model: claude.models.options.find((item: any) => item.id !== claude.models.default && !item.needsCliUpdate).id, effort: "low" };
    const second = applied(await create("Chosen specialist", explicit));
    expect((await api("GET", "/api/bots")).bots.length).toBe(before + 2);
    for (const invalid of [null, {}, { instanceId: "missing", model: "missing" },
      { ...explicit, model: "not-in-catalog" }, { ...explicit, effort: "invalid" },
      { ...explicit, variant: "a-variant" }, { instanceId: "claude", model: tooNew.id }]) {
      await create("Invalid specialist", invalid, 400);
    }
    // A replacement copies a managed bot's soul, role and model; extra instructions come after.
    const copyOf = (copyFromBotId: string, expected = 201, extra = {}) => api("POST", "/api/internal/create-bot", {
      fromBotId: chief.id, fromThreadId: chief.threadId, name: "Fourth specialist", copyFromBotId, instructions: "Pick up the open work.", ...extra,
    }, expected, token);
    const staleCopy = applied(await copyOf(stale.id, 201, { name: "Third specialist", role: "Specialist" }));
    expect(staleCopy.modelSelection).toEqual(defaultModel);
    await copyOf("missing-bot", 404);
    const copy = applied(await copyOf(second.id));
    await create("Fifth specialist", undefined, 429);
    writeFileSync(gate, "finish");
    await idle();
    await create("Expired authority", undefined, 401);
    expect(first.modelSelection).toEqual(defaultModel);
    expect(second.modelSelection).toEqual(explicit);
    const bots = (await api("GET", "/api/bots")).bots;
    expect(bots.length).toBe(before + 4);
    expect(bots.find((bot: any) => bot.id === chief.id).modelSelection).toEqual(chiefModel);
    for (const [id, modelSelection] of [[first.id, defaultModel], [second.id, explicit]]) {
      expect(bots.find((bot: any) => bot.id === id)).toMatchObject({
        section: "Design", modelSelection, approvalMode: "full", autoApprove: false, connectorScopes: { apps: { gmail: "read" } },
        soul: "Complete assigned work.",
      });
    }
    expect(bots.find((bot: any) => bot.id === copy.id)).toMatchObject({
      title: "Specialist", modelSelection: explicit, soul: "Complete assigned work.\n\nPick up the open work.",
    });
    await api("PATCH", "/api/config", { defaultModelSelection: explicit });
    unlinkSync(gate);
    unlinkSync(fixture.fixtureDumpPath);
    await api("POST", `/api/bots/${chief.id}/messages`, { text: "Prepare the next specialist." }, 202);
    await expect.poll(() => existsSync(fixture.fixtureDumpPath), { timeout: 15_000 }).toBe(true);
    token = JSON.parse(readFileSync(fixture.fixtureDumpPath, "utf8")).mcpConfig.mcpServers.agents.env.OMB_COMMS_TOKEN;
    const updated = applied(await create("Updated default specialist"));
    writeFileSync(gate, "finish");
    await idle();
    expect(updated.modelSelection).toEqual(explicit);
    expect((await api("GET", "/api/bots")).bots.find((bot: any) => bot.id === first.id).modelSelection).toEqual(defaultModel);
  } finally {
    writeFileSync(gate, "finish");
    await fixture.close();
    await removeTempDir(temp);
  }
}, 60_000);