Skip to content

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

App-event runs carried on later check the bot's account limit before the turn is sent

Bugfixer 2488ee674fcb4
diff --git a/server/app-event-account-limit.e2e.test.ts b/server/app-event-account-limit.e2e.test.ts
new file mode 100644
index 0000000000..e9de3a367a
--- /dev/null
+++ b/server/app-event-account-limit.e2e.test.ts
@@ -0,0 +1,87 @@
+import { existsSync, readFileSync, writeFileSync } from "node:fs";
+import { join } from "node:path";
+import { afterAll, beforeAll, expect, it, vi } from "vitest";
+import { launchVerificationServer, runControlOmb } from "../scripts/control-omb.ts";
+import { startBrokerFake, type BrokerFake } from "./testing/broker-fake.ts";
+
+// An app-event run that is carried on later (here by a teammate's reply) checks the bot's account limit again right
+// before that turn is sent, on a real server with a fake Composio behind the connected-apps service: once the owner
+// takes the account away from the bot, the reply never reaches the engine and the run fails with the plain refusal.
+let broker: BrokerFake;
+let owner: { id: string; token: string; userId: string };
+let sales = "";
+let support = "";
+let fixture: Awaited<ReturnType<typeof launchVerificationServer>>;
+
+beforeAll(async () => {
+ vi.spyOn(console, "log").mockImplementation(() => {});
+ vi.spyOn(console, "warn").mockImplementation(() => {});
+ broker = await startBrokerFake();
+ owner = await broker.install();
+ sales = broker.account(owner.userId, "gmail", "sales");
+ support = broker.account(owner.userId, "gmail", "support");
+ fixture = await launchVerificationServer({ ...process.env, OMB_COMPOSIO_BROKER_URL: broker.url, OMB_COMPOSIO_BROKER_TOKEN: owner.token }, undefined, undefined, undefined, undefined, { scripted: true });
+}, 60_000);
+
+afterAll(async () => {
+ console.info(JSON.stringify({ logPath: fixture?.info.logPath }));
+ await fixture?.close();
+ await broker?.close();
+ vi.restoreAllMocks();
+});
+
+it("does not carry an app-event run on after its bot loses the account, even when the turn continues a reply", async () => {
+ const planPath = join(fixture.info.dataDir, "room-plan.json");
+ const turns = () => existsSync(`${planPath}.evidence.jsonl`)
+ ? readFileSync(`${planPath}.evidence.jsonl`, "utf8").trim().split("\n").filter(Boolean).map((line) => JSON.parse(line) as { planKey?: string })
+ : [];
+ const api = async (method: string, path: string, body?: unknown) => {
+ const response = await fetch(`${fixture.info.url}${path}`, {
+ method, headers: { "content-type": "application/json", origin: fixture.info.url },
+ ...(body === undefined ? {} : { body: JSON.stringify(body) }),
+ });
+ const value = await response.json() as any;
+ expect(response.status, `${method} ${path}: ${JSON.stringify(value)}`).toBeLessThan(300);
+ return value;
+ };
+ const cli = (...args: string[]) => runControlOmb([...args, "--url", fixture.info.url]) as Promise<any>;
+ const { bot } = await cli("new-bot", "--name", "Scout");
+ const { bot: sam } = await cli("new-bot", "--name", "Sam");
+ const threadId = bot.activeTaskId as string;
+ const samGate = join(fixture.info.dataDir, "sam-counts");
+ writeFileSync(planPath, JSON.stringify({
+ [bot.id]: { script: [
+ { key: "setup", match: ["When I get an email from Acme"], reply: "Set up.", steps: [
+ { tool: "list_trigger_events", arguments: { app: "gmail" } },
+ { tool: "create_trigger", arguments: { when: "app_event", name: "Acme orders", app: "gmail", account: sales, event: "GMAIL_NEW_GMAIL_MESSAGE",
+ filter: { from: "acme.com" }, instructions: "Ask Sam to count the boxes for the order in the email." } },
+ ] },
+ { key: "run", match: ["Ask Sam to count the boxes"], reply: "Asked Sam to count the boxes.",
+ steps: [{ tool: "coordinate_bots", arguments: { bot_ids: [sam.id], message: "Count the boxes for order 1042." } }] },
+ { key: "resume", match: ["42 boxes"], reply: "Replied to Acme: 42 boxes are ready." },
+ ] },
+ [sam.id]: { script: [{ key: "count", match: ["Count the boxes for order 1042."], reply: "Counted 42 boxes.", gateFile: samGate }] },
+ }));
+ await cli("send", "--bot", bot.id, "--task", threadId, "--text", "When I get an email from Acme, ask Sam to count the boxes.");
+ await expect.poll(() => turns().some((turn) => turn.planKey === "setup"), { timeout: 20_000 }).toBe(true);
+ await cli("wait", "--bot", bot.id, "--task", threadId, "--timeout", "20");
+ const trigger = async () => (await api("GET", "/api/app-triggers")).triggers.find((candidate: any) => candidate.botId === bot.id);
+ expect(await trigger()).toMatchObject({ app: "gmail", accountId: sales, enabled: true, remote: { state: "active" } });
+
+ // An Acme email starts the run; it hands the count to Sam and waits for the reply.
+ expect((await broker.deliver((await trigger()).remote.triggerId, { sender: "[email protected]", subject: "Order 1042", message_text: "How many boxes?" }, "msg_limit_1")).status).toBe(200);
+ await expect.poll(async () => (await trigger())?.lastRunId, { timeout: 30_000 }).toBeTruthy();
+ const runId = (await trigger()).lastRunId as string;
+ const run = async () => (await api("GET", "/api/routines")).runs.find((candidate: any) => candidate.id === runId);
+ await expect.poll(async () => (await run())?.status, { timeout: 30_000 }).toBe("waiting");
+ expect(turns().filter((turn) => turn.planKey === "run")).toHaveLength(1);
+
+ // While it waits, the owner limits Scout to the support account in its Access settings.
+ await api("PATCH", `/api/bots/${bot.id}`, { connectorScopes: { apps: { gmail: "read" }, accounts: { gmail: [support] } } });
+ writeFileSync(samGate, "done");
+
+ // Sam's reply would carry the run on; the turn is refused before it is sent, and the run says why.
+ await expect.poll(async () => (await run())?.status, { timeout: 30_000 }).toBe("failed");
+ expect((await run()).error).toContain("Its bot may no longer use this account");
+ expect(turns().filter((turn) => turn.planKey === "resume")).toEqual([]);
+}, 180_000);
diff --git a/server/index.ts b/server/index.ts
index 8407fe8c31..96ec2394d9 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -7472,7 +7472,7 @@ async function beginTurn(
const onDispatchError = opts?.onDispatchError;
opts = {
...opts,
- runOn: continuingRoutine.runOn,
+ runOn: continuingRoutine.runOn, automationRunId: continuingRoutine.id,
automationSource: continuingRoutine.triggerSource ?? (continuingRoutine.manual ? "manual" : "schedule"),
onDispatchError: (message) => {
routines?.failThread(threadId, message, { cause: "refused" });
@@ -8242,12 +8242,11 @@ async function beginTurn(
kind: computerKind,
vmPrivate: localVmMode(cfg) === "per-bot",
});
- const coordinationNode = opts?.coordination ? coordinator.node(opts.coordination.id) : undefined;
+ const coordinationNode = opts?.coordination ? coordinator.node(opts.coordination.id) : undefined, accountRefusal = appTriggers.turnRefusal(threadId, opts?.automationRunId);
if (opts?.coordination && (!coordinationNode || coordinationNode.status !== "running" || roomHandoffProblem(coordinationNode,
coordinationNode.parentId ? coordinator.node(coordinationNode.parentId) : undefined))) {
throw new DirectTurnSetupCancelled("Coordination access changed before dispatch");
- }
- const accountRefusal = appTriggers.turnRefusal(threadId, opts?.automationRunId); if (accountRefusal) throw new DirectTurnSetupCancelled(accountRefusal);
+ } else if (accountRefusal) throw new DirectTurnSetupCancelled(accountRefusal);
// The tool bridge is decided here: the folder the turn is launched in is known, and the prompt is not built yet.
applyAgentsBridge(threadId, integrations, cfg, bot, turnEngine(instance, providerConfigs(), registry.lastSnapshot(instance.instanceId), cwd));
const confined = proactive.fence.confines(threadId), apiFenced = botApiThread(threadId); if (confined || apiFenced) for (const key of Object.keys(integrations) as Array<keyof typeof integrations>) if (!(apiFenced ? BOT_API_TOOLS : READ_AND_DRAFT_TOOLS).has(key)) delete integrations[key];