Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Team rules: what a running turn keeps when a rule changes, tested, and a blocked computer says so
milind-soniffc15ec78b4e
diff --git a/docs/team-cloud.md b/docs/team-cloud.mdindex 368ac5748a..20c61dee66 100644--- a/docs/team-cloud.md+++ b/docs/team-cloud.md@@ -86,9 +86,12 @@ keeps people apart on it. The bot box ([bot-box.md](bot-box.md)) is what makes tThe team's rules live on the Admin (openmaus-cloud `docs/team-cloud.md` "Rules"): one document, `{ schema: 1, limits }`, thatonly restricts. `server/cloud-team-policy.ts` fetches it at boot, every 5 minutes and when the Admin's signed `POST/api/cloud/team/policy-changed` nudges it, signed with the bootstrap secret (`POST /api/cloud/machines/team-policy`), and each-request reports the fingerprint in force here. A change applies without a restart, to the next thing each bot does: a turn-already running finishes with what it had (its engine process and the servers mounted when it began), and every turn that-starts after the change, a hand-off's included, gets the new rules.+request reports the fingerprint in force here. A change applies without a restart. A turn already running keeps its engine+process and the servers it mounted when it began, HighLevel included, and finishes with them. What it reaches for through this+machine is refused from that moment, mid-turn too: a connected-apps tool (the turn carries on without it) and the first claim on+a computer (the turn stops there, in the rule's words; a computer it already holds stays with it until it ends). Every turn that+starts after the change, a hand-off's included, gets the new rules, even in a chat whose engine process is still warm: that+process restarts without what is now blocked.- **One enforcement.** The document is projected into the one policy object (`server/managed-policy.ts`) every refusal siteand every "Managed by" screen already reads. Nothing restricted: no object at all, as on a team with no rules. Admins are not exempt.diff --git a/server/cloud-team-rules-running-turn.e2e.test.ts b/server/cloud-team-rules-running-turn.e2e.test.tsnew file mode 100644index 0000000000..50db00bee1--- /dev/null+++ b/server/cloud-team-rules-running-turn.e2e.test.ts@@ -0,0 +1,185 @@+// What a turn already running keeps when the team's rules change under it, and what it loses at once. A stand-in Admin+// holds the document; the synthetic Claude CLI waits inside its turn while the rule lands, then calls what it was launched+// with. Disposable homes, fake connected-apps service and MCP server; nothing leaves this machine.+import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";+import { createServer, type Server } from "node:http";+import type { AddressInfo } from "node:net";+import { join } from "node:path";+import { afterAll, beforeAll, expect, it } from "vitest";++import { launchCloudSendFixture, type CloudSendFixture, type CloudTeamFixture } from "../scripts/testing/cloud-send-fixture.ts";+import { TEAM_POLICY_ADMIN_PATH, teamPolicyFingerprint } from "./cloud-team-policy.ts";+import { startBrokerFake, type BrokerFake } from "./testing/broker-fake.ts";+import { startFakeHttpMcp, type FakeHttpMcp } from "./testing/fake-http-mcp-server.ts";+import { hostTimeout } from "./testing/host-timeout.ts";++const ENGINE = `process.env.FAKE_CLAUDE_DUMP = process.env.FAKE_CLAUDE_FIXTURE_DIR + "/dump.json";+process.env.FAKE_CLAUDE_TOOL_CALLS_FILE = process.env.FAKE_CLAUDE_FIXTURE_DIR + "/tool-calls.json";+process.env.FAKE_CLAUDE_RELEASE = process.env.FAKE_CLAUDE_FIXTURE_DIR + "/release";+process.env.FAKE_CLAUDE_REPLY_FILE = process.env.FAKE_CLAUDE_FIXTURE_DIR + "/reply.txt";`;+const FINISHED = "Finished with what it had.";+const WAIT = { name: "Read", input: { file_path: "notes.txt" }, hold: true, output: "the notes" };+const ORDERS = { name: "lookup_order", server: "orders", input: {} };+const GMAIL = { name: "COMPOSIO_MULTI_EXECUTE_TOOL", server: "composio", input: { tools: [{ tool_slug: "GMAIL_FETCH_EMAILS", arguments: { max_results: 1 } }] } };++let adminServer: Server, relay: Server, gateway: FakeHttpMcp, broker: BrokerFake, fx: CloudSendFixture, team: CloudTeamFixture;+let owner = "", botId = "", teamUser = "";+let answer: unknown = { policy: null }, version = 0;+const reports: { loaded: { fingerprint: string } | null }[] = [];++async function rules(limits: Record<string, unknown> | null) {+ const document = limits && { schema: 1, limits };+ const fingerprint = document && teamPolicyFingerprint(document);+ answer = { policy: document && { version: ++version, fingerprint, document } };+ const nudge = await team.signed("POST", "/api/cloud/team/policy-changed", {});+ expect(nudge.status, JSON.stringify(nudge.body)).toBe(200);+ await expect.poll(() => reports.at(-1)?.loaded?.fingerprint ?? null, { timeout: 15_000, interval: 50 }).toBe(fingerprint);+}++type Dump = { pid: number; mcpConfig?: { mcpServers?: Record<string, unknown> } };+const dumpPath = () => join(team.home, "dump.json");+const dump = () => JSON.parse(readFileSync(dumpPath(), "utf8")) as Dump;+const script = (calls: unknown[]) => writeFileSync(join(team.home, "tool-calls.json"), JSON.stringify(calls));+const orderCalls = () => (gateway.calls as { name?: string }[]).filter((call) => call.name === "lookup_order").length;+const thread = async (id: string) => JSON.stringify((await team.as(owner, "GET", `/api/threads/${id}/messages?limit=200`)).body);++async function newThread(title: string) {+ const created = await team.as(owner, "POST", `/api/bots/${botId}/tasks`, { title });+ expect(created.status, JSON.stringify(created.body)).toBe(201);+ return created.body.task.threadId as string;+}+async function send(threadId: string, text: string) {+ rmSync(dumpPath(), { force: true });+ rmSync(join(team.home, "release"), { force: true });+ return team.as(owner, "POST", `/api/bots/${botId}/messages`, { text, threadId });+}+const idle = async () => ((await team.as(owner, "GET", "/api/bots")).body.bots as { activity?: string; busy?: boolean }[]).every((bot) => bot.activity !== "working" && !bot.busy);+/** A turn that runs straight through, and what its engine was launched with. */+async function fullTurn(threadId: string, text: string) {+ script([]);+ const sent = await send(threadId, text);+ expect(sent.status, JSON.stringify(sent.body)).toBe(202);+ await expect.poll(async () => existsSync(dumpPath()) && (await thread(threadId)).includes(FINISHED) && await idle(), { timeout: 60_000, interval: 100 }).toBe(true);+ return dump();+}+/** Starts a turn that waits inside itself, with no rules in force; the rule lands while it waits; then it carries on. */+async function ruleLandsMidTurn(limits: Record<string, unknown>, calls: unknown[], title: string, ended = FINISHED) {+ await rules(null);+ const threadId = await newThread(title);+ script(calls);+ const sent = await send(threadId, title);+ expect(sent.status, JSON.stringify(sent.body)).toBe(202);+ await expect.poll(() => existsSync(dumpPath()), { timeout: 60_000, interval: 50 }).toBe(true);+ const launched = dump();+ await rules(limits);+ writeFileSync(join(team.home, "release"), "");+ await expect.poll(async () => (await thread(threadId)).includes(ended) && await idle(), { timeout: 60_000, interval: 100 }).toBe(true);+ return { threadId, launched, words: await thread(threadId) };+}++beforeAll(async () => {+ adminServer = createServer((req, res) => {+ const chunks: Buffer[] = [];+ req.on("data", (chunk) => chunks.push(chunk));+ req.on("end", () => {+ expect(req.url).toBe(TEAM_POLICY_ADMIN_PATH);+ reports.push(JSON.parse(Buffer.concat(chunks).toString("utf8")));+ res.writeHead(200, { "content-type": "application/json" });+ res.end(JSON.stringify(answer));+ });+ });+ await new Promise<void>((resolve) => adminServer.listen(0, "127.0.0.1", resolve));+ relay = createServer((_req, res) => { res.writeHead(404, { "content-type": "application/json" }); res.end("{}"); });+ await new Promise<void>((resolve) => relay.listen(0, "127.0.0.1", resolve));+ gateway = await startFakeHttpMcp({ tools: [{ name: "lookup_order", inputSchema: { type: "object", properties: {} } }], callResult: () => ({ content: [{ type: "text", text: "order 7 shipped" }] }) });+ broker = await startBrokerFake();+ fx = await launchCloudSendFixture({ onePasswordToken: "ops_fake-running-turn", broker, team: { id: "team-acme-1", name: "Acme", engine: ENGINE, env: {+ OMB_BOT_BOX: "0", FIXTURE_ADMIN_STAND_IN: `http://127.0.0.1:${(adminServer.address() as AddressInfo).port}`,+ OMB_CLOUD_BOAT_URL: `http://127.0.0.1:${(relay.address() as AddressInfo).port}/relay/api/box/v1`, OMB_CLOUD_BOAT_TOKEN: "box_omb_running",+ } } });+ team = fx.team!;+ writeFileSync(join(team.home, "reply.txt"), FINISHED);+ owner = (await team.pair({ email: "[email protected]", role: "owner" })).token!;+ botId = (await team.as(owner, "POST", "/api/bots", { name: "Scout" })).body.bot.id;+ expect((await team.as(owner, "POST", "/api/bots", { name: "Helper" })).status).toBe(201);+ teamUser = broker.userFor(broker.installationIds().filter((id) => id !== fx.desktopInstallation?.id && broker.actingUser(id) === broker.userFor(id)).at(-1)!);+ broker.account(teamUser, "gmail", "[email protected]");+ expect((await team.as(owner, "POST", "/api/mcp/servers", { name: "orders", type: "http", url: gateway.url })).status).toBe(201);+ expect((await team.as(owner, "PATCH", "/api/mcp/servers/orders", { enabled: true })).status).toBe(200);+}, hostTimeout(120_000));++afterAll(async () => {+ await fx?.close();+ await gateway?.close();+ await broker?.close();+ await new Promise((resolve) => adminServer?.close(resolve));+ await new Promise((resolve) => relay?.close(resolve));+});++it("keeps its engine and the MCP servers it began with, and finishes, though the engine and the server are now blocked", async () => {+ const before = orderCalls();+ const { launched, words, threadId } = await ruleLandsMidTurn({ allowedEngines: ["codex"], allowedMcpServers: ["approved"] }, [WAIT, ORDERS], "keeps what it had");+ expect(Object.keys(launched.mcpConfig?.mcpServers ?? {})).toContain("orders");+ expect(orderCalls()).toBe(before + 1);+ expect(words).toContain("order 7 shipped");+ expect(words).toContain(FINISHED);+ expect(words).not.toMatch(/Acme (does not allow|allows only)/);+ const next = await send(threadId, "and the next one");+ expect(next.status, JSON.stringify(next.body)).toBe(409);+ expect(next.body.error).toMatch(/^Acme does not allow the Claude engine\./);+ expect(existsSync(dumpPath())).toBe(false);+}, hostTimeout(120_000));++it("the next turn in the same chat gets the new rules, even with the engine's process still warm", async () => {+ await rules(null);+ const threadId = await newThread("stays warm");+ const first = await fullTurn(threadId, "first");+ const second = await fullTurn(threadId, "second");+ expect(second.pid).toBe(first.pid);+ expect(Object.keys(second.mcpConfig?.mcpServers ?? {})).toContain("orders");+ await rules({ allowedMcpServers: ["approved"] });+ const third = await fullTurn(threadId, "third");+ expect(third.pid).not.toBe(first.pid);+ expect(Object.keys(third.mcpConfig?.mcpServers ?? {})).not.toContain("orders");+}, hostTimeout(180_000));++it("a hand-off it starts after the change gets the new rules", async () => {+ await rules(null);+ const threadId = await newThread("hands on");+ script([WAIT, { name: "coordinate_bots", server: "agents", input: { bot_ids: ["Helper"], message: "Please check the stock.", reason: "It asked for the stock" } }]);+ const sent = await send(threadId, "hands on");+ expect(sent.status, JSON.stringify(sent.body)).toBe(202);+ await expect.poll(() => existsSync(dumpPath()), { timeout: 60_000, interval: 50 }).toBe(true);+ expect(Object.keys(dump().mcpConfig?.mcpServers ?? {})).toContain("orders");+ await rules({ allowedMcpServers: ["approved"] });+ script([]);+ rmSync(dumpPath(), { force: true });+ writeFileSync(join(team.home, "release"), "");+ await expect.poll(() => existsSync(dumpPath()), { timeout: 60_000, interval: 50 }).toBe(true);+ const helper = dump() as Dump & { prompt?: unknown };+ expect(JSON.stringify(helper.prompt)).toContain("Please check the stock.");+ expect(Object.keys(helper.mcpConfig?.mcpServers ?? {})).not.toContain("orders");+ await expect.poll(idle, { timeout: 60_000, interval: 100 }).toBe(true);+}, hostTimeout(120_000));++it("a connected-app tool is refused from the moment the rule lands, mid-turn, and the turn still finishes", async () => {+ const callsBy = () => broker.mcpCalls.filter((call) => call.method === "tools/call" && call.user === teamUser).length;+ const before = callsBy();+ const { launched, words } = await ruleLandsMidTurn({ allowedApps: ["slack"] }, [GMAIL, WAIT, GMAIL], "uses an app");+ expect(Object.keys(launched.mcpConfig?.mcpServers ?? {})).toContain("composio");+ expect(callsBy()).toBe(before + 1);+ expect(words).toMatch(/Acme does not allow bots to use Gmail\. Ask a team admin\./);+}, hostTimeout(120_000));++it("a computer it has not claimed yet is refused from the moment the rule lands: the turn stops there, in the rule's words", async () => {+ const refusal = "Acme does not allow bots to use cloud computers. Ask a team admin.";+ expect((await team.as(owner, "PATCH", `/api/bots/${botId}`, { computer: "cloud" })).status).toBe(200);+ try {+ const { launched, words } = await ruleLandsMidTurn({ allowedComputers: ["vps"] }, [WAIT, { name: "screenshot", server: "computer", input: {} }], "uses a computer", refusal);+ expect(Object.keys(launched.mcpConfig?.mcpServers ?? {})).toContain("computer");+ expect(words).not.toMatch(/didn't start/);+ expect(words).not.toContain(FINISHED);+ } finally {+ expect((await team.as(owner, "PATCH", `/api/bots/${botId}`, { computer: null })).status).toBe(200);+ }+}, hostTimeout(120_000));diff --git a/server/index.ts b/server/index.tsindex 2f9d2519f7..e28875906c 100644--- a/server/index.ts+++ b/server/index.ts@@ -1916,7 +1916,7 @@ async function asPlaceFailure(if (error instanceof DirectTurnSetupCancelled || error instanceof ComputerWaitParked || error instanceof PlaceUnavailableError) return error;const failure = error instanceof Error ? error : new Error(String(error));const cause = failure.message.trim().replace(/[\s.]+$/, "");- if (place !== "cloud" || backend === "vps") {+ if (place !== "cloud" || backend === "vps" || (failure as { code?: unknown }).code === "managed_policy") {return placeUnavailable(place, {state: "place-failed", params: { bot: bot.name, cause: `${cause.charAt(0).toUpperCase()}${cause.slice(1)}.` }, source,}, Boolean(CLOUD_HOME));diff --git a/server/testing/fake-claude-cli.ts b/server/testing/fake-claude-cli.tsindex c22687c97e..eb28c7f17b 100755--- a/server/testing/fake-claude-cli.ts+++ b/server/testing/fake-claude-cli.ts@@ -797,7 +797,8 @@ const playHeldCalls = (calls: ScriptedToolCall[], usage: object, done: () => voiout({ type: "assistant", message: { id: `msg-${id}`, content: [{ type: "tool_use", id, name: call.name, input: call.input }], usage } });event({ type: "message_stop" });const answer = () => {- out({ type: "user", message: { content: [{ type: "tool_result", tool_use_id: id, is_error: !call.ok, content: call.output ?? "" }] } });+ const reply = call.server ? mcpToolCall(call.server, call.name, call.input) : { ok: call.ok, content: call.output ?? "" };+ out({ type: "user", message: { content: [{ type: "tool_result", tool_use_id: id, is_error: !reply.ok, content: reply.content }] } });// A model takes a moment to write its next call; back-to-back frames would only test the coalescing.setTimeout(() => next(index + 1), 600);};