Skip to content

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

45.9 KB
Raw Blame
import { randomUUID } from "node:crypto";
import { afterEach, describe, expect, it, vi } from "vitest";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { LentComputers, provenRequestPerson, SharedComputers, sharedComputerOperation, sharedComputerScopeRefusal } from "./shared-computers.ts";
import { hostScreenBusyText, SharedComputerControl } from "./shared-computer-control.ts";
import { TurnResources } from "./turn-resources.ts";

const secret = "a".repeat(64);
const other = "b".repeat(64);
const registration = (scopes: Partial<{ folders: { id: string; name: string; write: boolean }[]; terminal: boolean; computer: boolean; fileEdits: true }> = {}) =>
  ({ id: randomUUID(), name: "Desktop", environmentId: randomUUID(), folders: [], terminal: false, computer: false, ...scopes });
const alice = { session: "owner", person: "p_alice" };
const bob = { session: "intruder", person: "p_bob" };
afterEach(() => vi.useRealTimers());
const nothingQueued = async (broker: SharedComputers, id: string) => {
  vi.useFakeTimers();
  const poll = broker.poll(id, alice.session, secret);
  await vi.advanceTimersByTimeAsync(20_000);
  return poll;
};
describe("shared computer authority and lifecycle", () => {
  it("a computer whose person changed is found under its new key after it connects again", () => {
    const broker = new SharedComputers(id => id === "owner");
    const computer = registration(); broker.register(computer, alice, secret);
    broker.register(computer, { session: alice.session, person: "p_lender" }, secret);
    expect(broker.list(alice.person)).toEqual([]);
    expect(broker.list("p_lender").map(entry => entry.id)).toEqual([computer.id]);
    broker.close();
  });
  it("a computer whose person changed is no longer lent to its former person", () => {
    const dir = mkdtempSync(join(tmpdir(), "omb-lent-rekey-"));
    const lent = new LentComputers(join(dir, "lent.json"));
    const broker = new SharedComputers(id => id === "owner", lent);
    const computer = registration(); broker.register(computer, alice, secret);
    broker.register(computer, { session: alice.session, person: "p_lender" }, secret);
    expect(lent.list(alice.person).map(record => [record.id, record.stoppedAt !== null])).toEqual([[computer.id, true]]);
    expect(lent.list("p_lender").map(record => [record.id, record.stoppedAt])).toEqual([[computer.id, null]]);
    broker.close();
    rmSync(dir, { recursive: true, force: true });
  });
  it("a poll from a session that is now someone else drops the computer so it connects again", async () => {
    const broker = new SharedComputers(id => id === "owner");
    const computer = registration(); broker.register(computer, alice, secret);
    await expect(broker.poll(computer.id, alice.session, secret, undefined, "p_lender")).rejects.toMatchObject({ status: 403 });
    expect(broker.list(alice.person)).toEqual([]);
    broker.register(computer, { session: alice.session, person: "p_lender" }, secret);
    expect(broker.list("p_lender").map(entry => entry.id)).toEqual([computer.id]);
    broker.close();
  });
  it("requires both the owning paired session and the connector secret", async () => {
    const broker = new SharedComputers(id => id === "owner");
    const computer = registration(); broker.register(computer, alice, secret);
    for (const [session, key] of [["intruder", secret], ["owner", "wrong"], ["owner", "é".repeat(64)]]) {
      await expect(broker.poll(computer.id, session, key)).rejects.toMatchObject({ status: 403 });
    }
    expect(JSON.stringify(broker.list(alice.person))).not.toContain(secret);
    broker.close();
  });
  it("chromeLender names the newest computer that lends Chrome, and whether it is online", () => {
    const dir = mkdtempSync(join(tmpdir(), "omb-chrome-"));
    let live = true;
    const broker = new SharedComputers(() => live, new LentComputers(join(dir, "lent.json")));
    const a = { ...registration(), name: "A", platform: "darwin" as const, chrome: true };
    const b = { ...registration(), name: "B", platform: "darwin" as const, chrome: false };
    broker.register(a, alice, secret);
    broker.register(b, { session: "owner2", person: alice.person }, secret);
    expect(broker.chromeLender(alice.person)).toEqual({ name: "A", device: "Mac", online: true });
    expect(broker.chromeLender(bob.person)).toBeNull();
    live = false;
    expect(broker.chromeLender(alice.person)).toEqual({ name: "A", device: "Mac", online: false });
    live = true;
    broker.disconnect(a.id, alice.session, secret);
    expect(broker.chromeLender(alice.person)).toBeNull();
    broker.close();
    rmSync(dir, { recursive: true, force: true });
  });
  it("delivers a job once, refuses concurrent work, and never replays after disconnect", async () => {
    const broker = new SharedComputers(() => true);
    const computer = registration(); broker.register(computer, alice, secret);
    const operation = { computer_id: computer.id, action: "computer_tools" as const };
    const lent = registration({ computer: true }); lent.id = computer.id; broker.register(lent, alice, secret);
    const result = broker.request(operation, alice.person, () => true);
    await expect(broker.request(operation, alice.person, () => true)).rejects.toThrow(/busy/);
    const job = await broker.poll(computer.id, "owner", secret);
    expect(job?.operation).toEqual(operation);
    expect(broker.liveJob(computer.id, "owner", secret, job!.id)).toBe(true);
    broker.complete(computer.id, "owner", secret, job!.id, { ok: true });
    await expect(result).resolves.toEqual({ ok: true });
    expect(() => broker.complete(computer.id, "owner", secret, job!.id, {})).toThrow(/expired/);
    const pending = broker.request(operation, alice.person, () => true);
    const rejection = expect(pending).rejects.toThrow(/in-flight/);
    await broker.poll(computer.id, "owner", secret);
    broker.disconnect(computer.id, "owner", secret); await rejection;
    expect(broker.list(alice.person)).toEqual([]);
    await expect(broker.request(operation, alice.person, () => true)).rejects.toThrow(/offline/);
    broker.close();
  });
  it("tells the computer which bot an action is for: its name and colour only", async () => {
    const broker = new SharedComputers(() => true);
    const computer = registration({ computer: true }); broker.register(computer, alice, secret);
    const operation = { computer_id: computer.id, action: "computer_tools" as const };
    const bot = { id: "b1", name: "IT Support", color: "teal", soul: "private" };
    const result = broker.request(operation, alice.person, () => true, bot);
    const job = await broker.poll(computer.id, "owner", secret);
    expect(job).toEqual({ id: job!.id, operation, by: { name: "IT Support", color: "teal" } });
    broker.complete(computer.id, "owner", secret, job!.id, { ok: true });
    await expect(result).resolves.toEqual({ ok: true });
    const unnamed = broker.request(operation, alice.person, () => true);
    const plain = await broker.poll(computer.id, "owner", secret);
    expect(plain).toEqual({ id: plain!.id, operation });
    broker.complete(computer.id, "owner", secret, plain!.id, {}); await unnamed;
    broker.close();
  });
  it("waits for a command as long as the command may run, and a non-command action still 45 seconds", async () => {
    vi.useFakeTimers();
    const lent = () => {
      const broker = new SharedComputers(() => true);
      const computer = { ...registration({ terminal: true, computer: true }), commandTimeouts: true as const }; broker.register(computer, alice, secret);
      const run = async (operation: Omit<Parameters<SharedComputers["request"]>[0], "computer_id">) => {
        const result = broker.request({ computer_id: computer.id, ...operation }, alice.person, () => true);
        result.catch(() => {});
        const job = await broker.poll(computer.id, alice.session, secret);
        broker.liveJob(computer.id, alice.session, secret, job!.id);
        return { result, job: job!, complete: (value: unknown) => broker.complete(computer.id, alice.session, secret, job!.id, value), close: () => broker.close() };
      };
      return run;
    };
    const long = await lent()({ action: "run_command", command: "./weekly-scan", timeout_seconds: 200 });
    expect(long.job.operation).toMatchObject({ timeout_seconds: 200 });
    await vi.advanceTimersByTimeAsync(199_000);
    long.complete({ exitCode: 0 });
    await expect(long.result).resolves.toEqual({ exitCode: 0 });
    long.close();
    const defaulted = await lent()({ action: "run_command", command: "./scan" });
    await vi.advanceTimersByTimeAsync(134_000);
    defaulted.complete({ exitCode: 0 });
    await expect(defaulted.result).resolves.toEqual({ exitCode: 0 });
    defaulted.close();
    const tools = await lent()({ action: "computer_tools" });
    await vi.advanceTimersByTimeAsync(45_000);
    await expect(tools.result).rejects.toThrow(/timed out/);
    tools.close();
  });
  it("runs a command up to 900 seconds on a desktop that takes long commands, and refuses past 240 on one that does not", async () => {
    expect(sharedComputerOperation.safeParse({ computer_id: randomUUID(), action: "run_command", command: "./scan", timeout_seconds: 900 }).success).toBe(true);
    expect(sharedComputerOperation.safeParse({ computer_id: randomUUID(), action: "run_command", command: "./scan", timeout_seconds: 901 }).success).toBe(false);
    vi.useFakeTimers();
    const broker = new SharedComputers(() => true);
    const computer = { ...registration({ terminal: true }), commandTimeouts: true as const, longCommands: true as const }; broker.register(computer, alice, secret);
    const result = broker.request({ computer_id: computer.id, action: "run_command", command: "./evals", timeout_seconds: 900 }, alice.person, () => true);
    result.catch(() => {});
    const job = await broker.poll(computer.id, alice.session, secret);
    expect(job!.operation).toMatchObject({ timeout_seconds: 900 });
    broker.liveJob(computer.id, alice.session, secret, job!.id);
    await vi.advanceTimersByTimeAsync(899_000);
    broker.complete(computer.id, alice.session, secret, job!.id, { exitCode: 0 });
    await expect(result).resolves.toEqual({ exitCode: 0 });
    broker.close();
    const older = new SharedComputers(() => true);
    const capped = { ...registration({ terminal: true }), commandTimeouts: true as const }; older.register(capped, alice, secret);
    await expect(older.request({ computer_id: capped.id, action: "run_command", command: "./evals", timeout_seconds: 241 }, alice.person, () => true))
      .rejects.toThrow(/at most 240 seconds/);
    older.close();
  });
  it("never sends a timeout to a desktop that did not say it takes one, and waits for it as before", async () => {
    const broker = new SharedComputers(() => true);
    const computer = registration({ terminal: true }); broker.register(computer, alice, secret);
    vi.useFakeTimers();
    const command = broker.request({ computer_id: computer.id, action: "run_command", command: "./scan", timeout_seconds: 200 }, alice.person, () => true);
    const rejection = expect(command).rejects.toThrow(/timed out/);
    const job = await broker.poll(computer.id, alice.session, secret);
    expect(job?.operation).toEqual({ computer_id: computer.id, action: "run_command", command: "./scan" });
    broker.liveJob(computer.id, alice.session, secret, job!.id);
    await vi.advanceTimersByTimeAsync(45_000);
    await rejection;
    broker.close();
  });
  it("never sends old_text or new_text to a desktop that did not say it takes passage edits", async () => {
    const folder = { id: randomUUID(), name: "Drafts", write: true };
    for (const [scopes, sent] of [[{}, false], [{ fileEdits: true as const }, true]] as const) {
      const broker = new SharedComputers(() => true);
      const computer = registration({ folders: [folder], ...scopes }); broker.register(computer, alice, secret);
      const write = broker.request({ computer_id: computer.id, action: "write_file", folder_id: folder.id, path: "a.md", content: "x", new_text: "y" }, alice.person, () => true);
      write.catch(() => {});
      const job = await broker.poll(computer.id, alice.session, secret);
      expect("new_text" in job!.operation).toBe(sent);
      broker.close();
    }
  });
  it("sends an MCP call's caller only to a desktop that said it takes one, so an older desktop keeps running MCP calls", async () => {
    const server = { name: "local", url: "http://127.0.0.1:19501/mcp", type: "http" as const, headers: {} };
    const caller = { botId: "kiwi", threadId: "t-1" };
    for (const [scopes, sent] of [[{ mcpServers: true as const }, false], [{ mcpServers: true as const, mcpCaller: true as const }, true]] as const) {
      const broker = new SharedComputers(() => true);
      const computer = { ...registration(), ...scopes }; broker.register(computer, alice, secret);
      const call = broker.request({ computer_id: computer.id, action: "mcp_call", server, tool_name: "search", arguments: {}, caller }, alice.person, () => true);
      call.catch(() => {});
      const job = await broker.poll(computer.id, alice.session, secret);
      expect(job!.operation.caller, JSON.stringify(scopes)).toEqual(sent ? caller : undefined);
      expect("caller" in job!.operation).toBe(sent);
      broker.close();
    }
    const broker = new SharedComputers(() => true);
    const computer = { ...registration(), mcpServers: true as const, mcpCaller: true as const }; broker.register(computer, alice, secret);
    const list = broker.request({ computer_id: computer.id, action: "mcp_tools", server, caller }, alice.person, () => true);
    list.catch(() => {});
    expect("caller" in (await broker.poll(computer.id, alice.session, secret))!.operation, "only a tool call names its caller").toBe(false);
    broker.close();
    expect(sharedComputerOperation.safeParse({ computer_id: computer.id, action: "mcp_call", server, tool_name: "x", caller }).success, "a bot's own operation can't name a caller").toBe(false);
  });
  it("turn cancellation rejects queued and already delivered actions", async () => {
    vi.useFakeTimers();
    const broker = new SharedComputers(() => true);
    const computer = registration({ computer: true }); broker.register(computer, alice, secret);
    let active = true;
    const pending = broker.request({ computer_id: computer.id, action: "computer_tools" }, alice.person, () => active);
    const rejection = expect(pending).rejects.toThrow(/turn ended/);
    const job = await broker.poll(computer.id, "owner", secret);
    active = false;
    expect(broker.liveJob(computer.id, "owner", secret, job!.id)).toBe(false);
    broker.complete(computer.id, "owner", secret, job!.id, {}); await rejection;
    active = true;
    const queued = broker.request({ computer_id: computer.id, action: "computer_tools" }, alice.person, () => active);
    const stopped = expect(queued).rejects.toThrow(/turn ended/);
    active = false;
    const poll = broker.poll(computer.id, "owner", secret);
    await vi.advanceTimersByTimeAsync(20_000);
    await expect(poll).resolves.toBeNull(); await stopped;
    broker.close();
  });
  it("expires offline registrations and rejects revoked sessions", async () => {
    vi.useFakeTimers(); let valid = true;
    const broker = new SharedComputers(() => valid);
    const computer = registration(); broker.register(computer, alice, secret);
    valid = false; expect(broker.list(alice.person)).toEqual([]);
    await expect(broker.poll(computer.id, "owner", secret)).rejects.toThrow(/not authorized/);
    valid = true; await vi.advanceTimersByTimeAsync(40_001);
    expect(broker.list(alice.person)).toEqual([]); broker.close();
  });
});

describe("a dropped channel recovers by itself", () => {
  const screen = () => { const broker = new SharedComputers(() => true); const computer = registration({ computer: true }); broker.register(computer, alice, secret); return { broker, computer }; };
  it("a reconnecting desktop's poll retires the one whose client dropped, which takes no job", async () => {
    const { broker, computer } = screen();
    const dropped = broker.poll(computer.id, alice.session, secret);
    const fresh = broker.poll(computer.id, alice.session, secret);
    await expect(dropped).resolves.toBeNull();
    const result = broker.request({ computer_id: computer.id, action: "computer_tools" }, alice.person, () => true);
    const job = await fresh;
    expect(job?.operation.action).toBe("computer_tools");
    expect(broker.liveJob(computer.id, alice.session, secret, job!.id)).toBe(true);
    broker.complete(computer.id, alice.session, secret, job!.id, { ok: true });
    await expect(result).resolves.toEqual({ ok: true });
    broker.close();
  });
  it("a server that stops answers its held poll and every later call as restarting, never as a lost sign-in", async () => {
    const { broker, computer } = screen();
    const held = broker.poll(computer.id, alice.session, secret);
    broker.close();
    await expect(held).rejects.toMatchObject({ status: 503, message: expect.stringMatching(/restarting/) });
    await expect(broker.poll(computer.id, alice.session, secret)).rejects.toMatchObject({ status: 503 });
    expect(() => broker.liveJob(computer.id, alice.session, secret, randomUUID())).toThrow(expect.objectContaining({ status: 503 }));
    expect(() => broker.register(computer, alice, secret)).toThrow(expect.objectContaining({ status: 503 }));
    expect(broker.list(alice.person)).toEqual([]);
  });
  it("a poll whose client went away answers nothing, so its job waits for the next poll", async () => {
    const { broker, computer } = screen();
    const gone = new AbortController();
    const dropped = broker.poll(computer.id, alice.session, secret, gone.signal);
    gone.abort();
    await expect(dropped).resolves.toBeNull();
    const result = broker.request({ computer_id: computer.id, action: "computer_tools" }, alice.person, () => true);
    const job = await broker.poll(computer.id, alice.session, secret);
    broker.complete(computer.id, alice.session, secret, job!.id, "done");
    await expect(result).resolves.toBe("done");
    broker.close();
  });
  it("a job never leased is offered again under a new id, and the lost delivery can never start it", async () => {
    vi.useFakeTimers();
    const { broker, computer } = screen();
    const result = broker.request({ computer_id: computer.id, action: "computer_tools" }, alice.person, () => true);
    const lost = await broker.poll(computer.id, alice.session, secret);
    await vi.advanceTimersByTimeAsync(10_000);
    const again = await broker.poll(computer.id, alice.session, secret);
    expect(again?.operation).toEqual(lost?.operation);
    expect(again!.id).not.toBe(lost!.id);
    expect(broker.liveJob(computer.id, alice.session, secret, lost!.id)).toBe(false);
    expect(broker.liveJob(computer.id, alice.session, secret, again!.id)).toBe(true);
    await vi.advanceTimersByTimeAsync(10_000);
    const nothing = broker.poll(computer.id, alice.session, secret);
    await vi.advanceTimersByTimeAsync(20_000);
    await expect(nothing).resolves.toBeNull();
    broker.complete(computer.id, alice.session, secret, again!.id, "once");
    await expect(result).resolves.toBe("once");
    broker.close();
  });
  it("waits for a computer that is reconnecting, and names one that stays offline", async () => {
    vi.useFakeTimers();
    const { broker, computer } = screen();
    await vi.advanceTimersByTimeAsync(40_001);
    const waiting = broker.request({ computer_id: computer.id, action: "computer_tools" }, alice.person, () => true);
    await vi.advanceTimersByTimeAsync(3_000);
    broker.register(computer, alice, secret);
    await vi.advanceTimersByTimeAsync(250);
    const job = await broker.poll(computer.id, alice.session, secret);
    broker.complete(computer.id, alice.session, secret, job!.id, "back");
    await expect(waiting).resolves.toBe("back");
    await vi.advanceTimersByTimeAsync(45_000);
    const offline = expect(broker.request({ computer_id: computer.id, action: "computer_tools" }, alice.person, () => true)).rejects.toThrow(/^Desktop is lent but has been offline for 1 min: OpenMausBot on it is closed/);
    await vi.advanceTimersByTimeAsync(15_000);
    await offline;
    broker.close();
  });
});
describe("owner partition (audit H1, H2)", () => {
  it("another person's bot turn cannot list or use a lent computer", async () => {
    const broker = new SharedComputers(() => true);
    const computer = registration({ terminal: true, computer: true, folders: [{ id: randomUUID(), name: "Docs", write: true }] });
    broker.register(computer, alice, secret);
    expect(broker.list(alice.person)).toHaveLength(1);
    expect(broker.list(bob.person)).toEqual([]);
    expect(broker.list(null)).toEqual([]);
    await expect(broker.request({ computer_id: computer.id, action: "run_command", command: "id" }, bob.person, () => true)).rejects.toThrow(/offline/);
    await expect(broker.request({ computer_id: computer.id, action: "run_command", command: "id" }, null, () => true)).rejects.toMatchObject({ status: 403 });
    await expect(nothingQueued(broker, computer.id)).resolves.toBeNull();
    broker.close();
  });
  it("a desktop that pairs again is used while its old sign-in is still live", async () => {
    vi.useFakeTimers({ now: Date.now() });
    const dir = mkdtempSync(join(tmpdir(), "omb-repaired-"));
    const broker = new SharedComputers(id => id === "old" || id === "new", new LentComputers(join(dir, "lent.json")));
    const computer = registration({ computer: true });
    broker.register(computer, { session: "old", person: alice.person }, secret);
    await vi.advanceTimersByTimeAsync(40_001);
    broker.register(computer, { session: "new", person: alice.person }, other);
    expect(broker.known(alice.person)).toMatchObject([{ id: computer.id, online: true }]);
    expect(broker.status(alice.person)).toMatchObject([{ id: computer.id, online: true }]);
    const operation = { computer_id: computer.id, action: "computer_tools" as const };
    const result = broker.request(operation, alice.person, () => true);
    const job = await broker.poll(computer.id, "new", other);
    expect(job?.operation).toEqual(operation);
    broker.complete(computer.id, "new", other, job!.id, { ok: true });
    await expect(result).resolves.toEqual({ ok: true });
    broker.close();
    rmSync(dir, { recursive: true, force: true });
  });
  it("a poll that ends after the desktop pairs again does not hand the next job to the old registration", async () => {
    vi.useFakeTimers({ now: Date.now() });
    const broker = new SharedComputers(id => id === "old" || id === "new");
    const computer = registration({ computer: true });
    broker.register(computer, { session: "old", person: alice.person }, secret);
    const gone = new AbortController();
    const dropped = broker.poll(computer.id, "old", secret, gone.signal);
    await vi.advanceTimersByTimeAsync(5_000);
    broker.register(computer, { session: "new", person: alice.person }, other);
    await vi.advanceTimersByTimeAsync(1);
    gone.abort();
    await expect(dropped).resolves.toBeNull();
    const operation = { computer_id: computer.id, action: "computer_tools" as const };
    const result = broker.request(operation, alice.person, () => true);
    const job = await broker.poll(computer.id, "new", other);
    expect(job?.operation).toEqual(operation);
    broker.complete(computer.id, "new", other, job!.id, { ok: true });
    await expect(result).resolves.toEqual({ ok: true });
    broker.close();
  });
  it("a second session cannot squat a lent computer's id, even after its owner goes offline", async () => {
    vi.useFakeTimers();
    const broker = new SharedComputers(() => true);
    const computer = registration({ folders: [{ id: randomUUID(), name: "Docs", write: true }] });
    broker.register(computer, alice, secret);
    await vi.advanceTimersByTimeAsync(40_001);
    broker.register(computer, bob, other);
    const refused = expect(broker.request({ computer_id: computer.id, action: "write_file", folder_id: computer.folders[0]!.id, path: "a.txt", content: "secret" }, alice.person, () => true)).rejects.toThrow(/offline/);
    await vi.advanceTimersByTimeAsync(15_000);
    await refused;
    expect(broker.list(alice.person)).toEqual([]);
    broker.register(computer, alice, secret);
    expect(broker.list(alice.person)).toHaveLength(1);
    await expect(broker.poll(computer.id, bob.session, secret)).rejects.toMatchObject({ status: 403 });
    broker.close();
  });
  it("a new grant from the same desktop replaces its earlier one; other desktops are untouched", async () => {
    const broker = new SharedComputers(() => true);
    const first = registration({ computer: true }); broker.register(first, alice, secret);
    const bobs = registration({ computer: true }); broker.register(bobs, bob, other);
    const pending = broker.request({ computer_id: first.id, action: "computer_tools" }, alice.person, () => true);
    const replaced = expect(pending).rejects.toThrow(/access changed/);
    const second = registration({ computer: true }); broker.register(second, alice, other);
    await replaced;
    expect(broker.list(alice.person).map(entry => entry.id)).toEqual([second.id]);
    expect(broker.list(bob.person).map(entry => entry.id)).toEqual([bobs.id]);
    await expect(broker.poll(first.id, alice.session, secret)).rejects.toMatchObject({ status: 403 });
    broker.close();
  });
  it("one session cannot fill the registry for everyone else", () => {
    const broker = new SharedComputers(() => true);
    for (let index = 0; index < 50; index++) broker.register(registration(), alice, secret);
    expect(() => broker.register(registration(), bob, other)).not.toThrow();
    expect(broker.list(bob.person)).toHaveLength(1);
    broker.close();
  });
});

describe("a bot turn's principal fails closed (audit H1)", () => {
  const people: Record<string, string | undefined> = { "from-alice": "p_alice", "from-routine": undefined };
  const personOf = (id: string) => people[id];
  it("names the person whose own message started this exact turn", () => {
    expect(provenRequestPerson({ messageId: "from-alice", generations: new Set(["g1", "g2"]) }, "g2", personOf)).toBe("p_alice");
  });
  it("names nobody for another generation, a stopped or unproven request, or a line no person sent", () => {
    expect(provenRequestPerson({ messageId: "from-alice", generations: new Set(["g1"]) }, "g-later", personOf)).toBeNull();
    expect(provenRequestPerson({ messageId: "from-alice", generations: new Set(["g1"]), stopped: true }, "g1", personOf)).toBeNull();
    expect(provenRequestPerson({ generations: new Set(["g1"]) }, "g1", personOf)).toBeNull();
    expect(provenRequestPerson({ messageId: "from-routine", generations: new Set(["g1"]) }, "g1", personOf)).toBeNull();
    expect(provenRequestPerson(undefined, "g1", personOf)).toBeNull();
  });
});

describe("status for the next step's placement", () => {
  it("reports one person's lent computers, online or asleep, with scopes and no secrets", async () => {
    vi.useFakeTimers();
    let live = true;
    const broker = new SharedComputers(session => session !== alice.session || live);
    const docs = { id: randomUUID(), name: "Docs", write: true };
    const mac = registration({ computer: true, folders: [docs] }); broker.register(mac, alice, secret);
    broker.register(registration({ terminal: true }), bob, other);
    expect(broker.status(alice.person)).toEqual([{ id: mac.id, name: "Desktop", online: true, busy: false, lastSeenAt: expect.any(Number), scopes: { folders: [docs], terminal: false, screen: true, internet: false, chrome: false } }]);
    expect(broker.status(null)).toEqual([]);
    expect(JSON.stringify(broker.status(alice.person))).not.toContain(secret);
    await vi.advanceTimersByTimeAsync(40_001);
    expect(broker.status(alice.person)).toMatchObject([{ id: mac.id, online: false }]);
    expect(broker.list(alice.person)).toEqual([]);
    live = false;
    expect(broker.status(alice.person)).toEqual([]);
    broker.close();
  });
});

describe("server-side scope check (audit M1)", () => {
  const readOnly = { id: randomUUID(), name: "Docs", write: false };
  const writable = { id: randomUUID(), name: "Drafts", write: true };
  const lent = registration({ folders: [readOnly, writable] });
  it("refuses shell, screen and writes a read-only grant never lent", () => {
    expect(sharedComputerScopeRefusal(lent, { action: "run_command" })).toMatch(/terminal/);
    expect(sharedComputerScopeRefusal(lent, { action: "computer_call" })).toMatch(/screen/);
    expect(sharedComputerScopeRefusal(lent, { action: "computer_tools" })).toMatch(/screen/);
    expect(sharedComputerScopeRefusal(lent, { action: "write_file", folder_id: readOnly.id })).toMatch(/read-only/);
    expect(sharedComputerScopeRefusal(lent, { action: "read_file", folder_id: randomUUID() })).toMatch(/not been shared/);
    expect(sharedComputerScopeRefusal(lent, { action: "read_file", folder_id: readOnly.id })).toBeNull();
    expect(sharedComputerScopeRefusal(lent, { action: "write_file", folder_id: writable.id })).toBeNull();
  });
  it("on the person's own Cloud, a folder that isn't lent says where it is lent from, and never asks a card that can't lend one", () => {
    const refusal = sharedComputerScopeRefusal(lent, { action: "read_file", folder_id: randomUUID() }, "ask_for_computer");
    expect(refusal).toMatch(/^That folder isn't lent to this Cloud: use a folder_id list_shared_computers gives\./u);
    expect(refusal).toContain("share it from the OpenMausBot desktop app, under Files");
    expect(refusal).not.toContain("ask_for_computer");
    expect(sharedComputerScopeRefusal(lent, { action: "read_file", folder_id: readOnly.id }, "ask_for_computer")).toBeNull();
  });
  it("edits, appends and deletes only in a folder lent with editing, and only on a desktop that takes them", () => {
    const current = registration({ folders: [readOnly, writable], fileEdits: true });
    for (const action of ["edit_file", "append_file", "delete_file"] as const) {
      expect(sharedComputerScopeRefusal(current, { action, folder_id: readOnly.id })).toMatch(/read-only/);
      expect(sharedComputerScopeRefusal(current, { action, folder_id: randomUUID() })).toMatch(/not been shared/);
      expect(sharedComputerScopeRefusal(current, { action, folder_id: writable.id })).toBeNull();
      expect(sharedComputerScopeRefusal(lent, { action, folder_id: writable.id })).toMatch(/older OpenMausBot .* write_file/);
    }
  });
  it("names the computer when its terminal isn't lent, sends the bot to no switch, and tells it not to run the command another way", () => {
    const refusal = sharedComputerScopeRefusal({ ...lent, name: "Ubuntu NUC", platform: "linux" }, { action: "run_command" });
    expect(refusal).toContain("Ubuntu NUC");
    expect(refusal).not.toMatch(/Settings|turn it on/u);
    expect(refusal).toMatch(/Do not try to run the command another way or anywhere else/);
    expect(sharedComputerScopeRefusal({ ...lent, terminal: true }, { action: "run_command" })).toBeNull();
  });
  it("says where the person lends apps and screen, in the refusal and in the list a bot reads", () => {
    const steps = "OpenMausBot on that Mac lends them by itself once macOS allows it Accessibility and Screen Recording: tell the person in one sentence to allow both for OpenMausBot in System Settings → Privacy & Security on that Mac, and to update OpenMausBot there if it is an older version.";
    expect(sharedComputerScopeRefusal({ ...lent, platform: "darwin" }, { action: "computer_call" })).toBe(`Apps and screen are not lent to this server. ${steps}`);
    expect(sharedComputerScopeRefusal({ ...lent, platform: "win32" }, { action: "computer_call" })).toContain("OpenMausBot on that PC lends them by itself once its computer control works");
    expect(sharedComputerScopeRefusal(lent, { action: "computer_call" })).not.toMatch(/Settings|use-my-computer/u);
    const broker = new SharedComputers(() => true);
    const mac = { ...registration(), platform: "darwin" as const };
    broker.register(mac, alice, secret);
    expect(broker.screenNotLent(alice.person)).toEqual({ screen: `Desktop does not lend its apps and screen. ${steps}` });
    expect(broker.screenNotLent(bob.person)).toEqual({});
    expect(broker.screenNotLent(null)).toEqual({});
    const lending = new SharedComputers(() => true);
    lending.register(registration({ computer: true }), alice, secret);
    expect(lending.screenNotLent(alice.person)).toEqual({});
    broker.close(); lending.close();
  });
  it("on the person's own Cloud, every computer that can't give what's needed sends the bot to ask with a card, never to explain the fix", async () => {
    const ask = 'call ask_for_computer with need "screen"';
    expect(sharedComputerScopeRefusal({ ...lent, platform: "win32" }, { action: "computer_call" }, "ask_for_computer"))
      .toBe("Apps and screen are not lent to this Cloud right now. Call ask_for_computer with need \"screen\" and what you want to do: it asks them with a one-tap Turn on, and you carry on once it's on. Don't explain the fix in chat, and don't use your own cloud computer instead unless they ask.");
    expect(sharedComputerScopeRefusal({ ...lent, name: "HOME-PC" }, { action: "run_command" }, "ask_for_computer")).toContain('Call ask_for_computer with need "terminal"');
    const broker = new SharedComputers(() => true);
    broker.askTool = "ask_for_computer";
    const changes: number[] = [];
    broker.changed = () => changes.push(Date.now());
    const pc = { ...registration(), platform: "win32" as const };
    broker.register(pc, alice, secret);
    expect(broker.screenNotLent(alice.person).screen).toBe(`Desktop doesn't lend its apps and screen right now. To use them, ${ask} and what you want to do: it asks them with a one-tap Turn on, and you carry on once it's on. Don't explain the fix in chat, and don't use your own cloud computer instead unless they ask.`);
    expect(broker.lendingNow(alice.person)).toEqual([{ id: pc.id, name: "Desktop", platform: "win32", computer: false, terminal: false }]);
    expect(broker.lendingNow(bob.person)).toEqual([]);
    broker.disconnect(pc.id, alice.session, secret);
    expect(changes, "connecting and stopping both tell the asks").toHaveLength(2);
    await expect(broker.request({ computer_id: pc.id, action: "computer_call", tool_name: "list_apps" }, alice.person, () => false)).rejects.toThrow(/Call ask_for_computer with need "screen"/);
    broker.close();
  });
  it("never queues an out-of-scope operation for the desktop", async () => {
    const broker = new SharedComputers(() => true);
    broker.register(lent, alice, secret);
    await expect(broker.request({ computer_id: lent.id, action: "run_command", command: "id" }, alice.person, () => true)).rejects.toMatchObject({ status: 403 });
    await expect(broker.request({ computer_id: lent.id, action: "write_file", folder_id: readOnly.id, path: "x", content: "y" }, alice.person, () => true)).rejects.toThrow(/read-only/);
    await expect(nothingQueued(broker, lent.id)).resolves.toBeNull();
    broker.close();
  });
});

it("shared screen calls respect local turns, lease expiry and release, and say which bot holds the screen", async () => {
  vi.useFakeTimers();
  const resources = new TurnResources();
  const control = new SharedComputerControl(resources, (owner) => (owner.threadId === "local" ? "Researcher" : undefined));
  const local = { threadId: "local", generation: "first" };
  resources.claim("computer:host", local);
  expect(control.acquire("remote")).toBe(hostScreenBusyText("Researcher"));
  expect(control.acquire("remote")).toMatch(/^Another bot on this computer, Researcher, is using its screen right now, so this call was not performed\. It is not a person\./);
  resources.release(local); expect(control.acquire("remote")).toBeUndefined();
  expect(resources.claim("computer:host", local)).toBe(false);
  await vi.advanceTimersByTimeAsync(40_001);
  expect(resources.claim("computer:host", local)).toBe(true);
  resources.release(local); expect(control.acquire("remote")).toBeUndefined(); control.close();
  expect(resources.claim("computer:host", local)).toBe(true);
  resources.release(local);
  // A turn of a bot this server does not name (a waiting one, say) is still another bot, never a person.
  resources.claim("computer:host", { threadId: "gone", generation: "g" });
  expect(control.acquire("remote")).toBe(hostScreenBusyText());
});

it("a lent call's screen lock left behind by an earlier lent call frees itself for the next one", () => {
  const resources = new TurnResources();
  const control = new SharedComputerControl(resources);
  const earlier = randomUUID(), next = randomUUID();
  expect(control.acquire(earlier)).toBeUndefined();
  // The desktop never released it (it crashed, or the release timed out): the next lent call still gets the screen.
  expect(control.acquire(next)).toBeUndefined();
  expect(resources.holder("computer:host")).toEqual({ threadId: `shared:${next}`, generation: next });
  expect(control.acquire(next)).toBeUndefined();
  control.release(next);
  expect(resources.free("computer:host")).toBe(true);
});

describe("MCP servers a lent computer runs for this server", () => {
  const home = randomUUID();
  const command = { name: "notes", command: "npx", args: ["-y", "notes-mcp"], env: { NOTES_TOKEN: "saved" } };
  const address = { name: "local", url: "http://127.0.0.1:19501/mcp", type: "http" as const, headers: {} };
  const lender = (scopes: Record<string, unknown> = {}) => ({ ...registration(), name: "Ada's Mac", platform: "darwin" as const, mcpServers: true as const, environments: [home], ...scopes });

  it("takes the new fields from a desktop that runs them, and an MCP job only with the server it names", () => {
    expect(sharedComputerOperation.safeParse({ computer_id: randomUUID(), action: "mcp_tools", server: command }).success).toBe(true);
    expect(sharedComputerOperation.safeParse({ computer_id: randomUUID(), action: "mcp_call", server: address, tool_name: "x".repeat(200), arguments: { q: 1 } }).success).toBe(true);
    for (const server of [{ ...command, extra: true }, { ...address, url: undefined }, { ...command, name: "Bad Name" }]) {
      expect(sharedComputerOperation.safeParse({ computer_id: randomUUID(), action: "mcp_tools", server }).success).toBe(false);
    }
  });

  it("refuses a desktop too old to run them, and a command server where the terminal isn't lent; an address needs no terminal", () => {
    expect(sharedComputerScopeRefusal({ ...lender(), mcpServers: undefined }, { action: "mcp_tools", server: address })).toContain("older OpenMausBot");
    expect(sharedComputerScopeRefusal(lender(), { action: "mcp_call", server: command })).toBe("notes starts a program on Ada's Mac, which doesn't let this server run programs right now. Tell the person in one sentence that they can turn on \"Let Clouds run commands on this Mac\" from the OpenMausBot icon in its menu bar.");
    expect(sharedComputerScopeRefusal(lender(), { action: "mcp_call", server: command }, "ask_for_computer")).toContain('Call ask_for_computer with need "terminal"');
    expect(sharedComputerScopeRefusal(lender({ terminal: true }), { action: "mcp_call", server: command })).toBeNull();
    expect(sharedComputerScopeRefusal(lender(), { action: "mcp_tools", server: address })).toBeNull();
    expect(sharedComputerScopeRefusal(lender(), { action: "mcp_tools" })).toBe("Name the MCP server.");
  });

  it("finds the computer an MCP server runs on by its installs, online or as last known, and never another person's", () => {
    const dir = mkdtempSync(join(tmpdir(), "omb-lent-mcp-"));
    let live = true;
    const broker = new SharedComputers(() => live, new LentComputers(join(dir, "lent.json")));
    const computer = lender(); broker.register(computer, alice, secret);
    const runsOn = { environmentId: home, label: "Old name" };
    expect(broker.runsOn(alice.person, runsOn)).toEqual({ online: { id: computer.id, name: "Ada's Mac", platform: "darwin", terminal: false, mcpServers: true }, known: { name: "Ada's Mac", platform: "darwin", lent: true } });
    expect(broker.runsOn(bob.person, runsOn)).toEqual({ online: null, known: null });
    expect(broker.runsOn(alice.person, { environmentId: randomUUID(), label: "Ada's Mac" }).online, "its installs are known, so its name alone is not it").toBeNull();
    expect(broker.mcpLenders(alice.person)).toEqual([expect.objectContaining({ id: computer.id, name: "Ada's Mac", session: alice.session, environmentId: home })]);
    expect(broker.mcpLenders(bob.person)).toEqual([]);
    live = false;
    expect(broker.runsOn(alice.person, runsOn)).toEqual({ online: null, known: { name: "Ada's Mac", platform: "darwin", lent: true } });
    expect(broker.mcpLenders(alice.person)).toEqual([]);
    live = true;
    broker.disconnect(computer.id, alice.session, secret);
    expect(broker.runsOn(alice.person, runsOn).known).toEqual({ name: "Ada's Mac", platform: "darwin", lent: false });
    const restarted = new SharedComputers(() => true, new LentComputers(join(dir, "lent.json")));
    expect(restarted.runsOn(alice.person, runsOn).known, "remembered across restarts").toEqual({ name: "Ada's Mac", platform: "darwin", lent: false });
    broker.close(); restarted.close();
    rmSync(dir, { recursive: true, force: true });
  });

  it("an older desktop that never said its installs is known by its name", () => {
    const broker = new SharedComputers(() => true);
    const computer = { ...registration(), name: "Ada's Mac", platform: "darwin" as const }; broker.register(computer, alice, secret);
    expect(broker.runsOn(alice.person, { environmentId: home, label: "Ada's Mac" }).online).toMatchObject({ id: computer.id, mcpServers: false });
    expect(broker.mcpLenders(alice.person), "it can't run them, so nothing added here is marked as running on it").toEqual([]);
    broker.close();
  });

  it("an MCP call waits for the computer's current action instead of failing as busy, and gets its own deadline", async () => {
    vi.useFakeTimers();
    const broker = new SharedComputers(() => true);
    const computer = lender(); broker.register(computer, alice, secret);
    const first = broker.request({ computer_id: computer.id, action: "mcp_tools", server: address }, alice.person, () => true);
    const second = broker.request({ computer_id: computer.id, action: "mcp_call", server: address, tool_name: "search" }, alice.person, () => true);
    const job = await broker.poll(computer.id, alice.session, secret);
    expect(job?.operation.action).toBe("mcp_tools");
    broker.liveJob(computer.id, alice.session, secret, job!.id);
    broker.complete(computer.id, alice.session, secret, job!.id, { tools: [{ name: "search" }] });
    await expect(first).resolves.toEqual({ tools: [{ name: "search" }] });
    await vi.advanceTimersByTimeAsync(300);
    const next = await broker.poll(computer.id, alice.session, secret);
    expect(next?.operation).toMatchObject({ action: "mcp_call", tool_name: "search" });
    broker.liveJob(computer.id, alice.session, secret, next!.id);
    await vi.advanceTimersByTimeAsync(300_000);
    broker.complete(computer.id, alice.session, secret, next!.id, { content: [{ type: "text", text: "found" }] });
    await expect(second, "a tool call has five minutes, not 45 seconds").resolves.toEqual({ content: [{ type: "text", text: "found" }] });
    broker.register(computer, alice, secret);
    const blocked = broker.request({ computer_id: computer.id, action: "mcp_tools", server: address }, alice.person, () => true);
    const late = broker.request({ computer_id: computer.id, action: "mcp_tools", server: address }, alice.person, () => true);
    const stuck = expect(late).rejects.toThrow(/busy/);
    await vi.advanceTimersByTimeAsync(31_000);
    await stuck;
    const closed = expect(blocked).rejects.toThrow(/Workspace stopped/);
    broker.close();
    await closed;
  });
});

describe("the computer the person types on", () => {
  it("is the online computer lent by the session that sent the line, put first; no other session or person sees it", async () => {
    vi.useFakeTimers({ now: Date.now() });
    const dir = mkdtempSync(join(tmpdir(), "omb-typing-on-"));
    const broker = new SharedComputers(() => true, new LentComputers(join(dir, "lent.json")));
    const pc = { ...registration(), name: "WIN-1", platform: "win32" as const };
    const laptop = { ...registration(), name: "UBUNTU-1", platform: "linux" as const };
    broker.register(pc, { session: "pc", person: alice.person }, secret);
    await vi.advanceTimersByTimeAsync(1_000);
    broker.register(laptop, { session: "laptop", person: alice.person }, other);
    const typing = (session?: string) => broker.known(alice.person, session).map(computer => [computer.name, computer.typing === true]);
    expect(typing()).toEqual([["UBUNTU-1", false], ["WIN-1", false]]);
    expect(typing("pc")).toEqual([["WIN-1", true], ["UBUNTU-1", false]]);
    expect(typing("laptop")).toEqual([["UBUNTU-1", true], ["WIN-1", false]]);
    expect(typing("phone")).toEqual([["UBUNTU-1", false], ["WIN-1", false]]);
    expect(broker.known(bob.person, "pc")).toEqual([]);
    await vi.advanceTimersByTimeAsync(40_001);
    expect(typing("pc")).toEqual([["UBUNTU-1", false], ["WIN-1", false]]);
    broker.close();
    rmSync(dir, { recursive: true, force: true });
  });

  it("is listed live when another computer with the same name holds the one record kept for that name", async () => {
    vi.useFakeTimers({ now: Date.now() });
    const dir = mkdtempSync(join(tmpdir(), "omb-typing-same-name-"));
    const broker = new SharedComputers(() => true, new LentComputers(join(dir, "lent.json")));
    const first = { ...registration(), name: "PC", platform: "win32" as const };
    const second = { ...registration(), name: "PC", platform: "win32" as const };
    broker.register(first, { session: "first", person: alice.person }, secret);
    await vi.advanceTimersByTimeAsync(1_000);
    broker.register(second, { session: "second", person: alice.person }, other);
    expect(broker.known(alice.person).map(computer => computer.id)).toEqual([second.id]);
    expect(broker.known(alice.person, "first").map(computer => [computer.id, computer.typing === true, computer.online]))
      .toEqual([[first.id, true, true], [second.id, false, true]]);
    expect(broker.known(alice.person, "second").map(computer => [computer.id, computer.typing === true])).toEqual([[second.id, true]]);
    broker.close();
    rmSync(dir, { recursive: true, force: true });
  });
});