Skip to content

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

Check the account limit again right before an app-event turn is sent

Bugfixer 28cb9d26ff5f7
diff --git a/server/app-trigger-host.ts b/server/app-trigger-host.ts
index 90f49e3dfa..bdf7492bc4 100644
--- a/server/app-trigger-host.ts
+++ b/server/app-trigger-host.ts
@@ -221,6 +221,7 @@ export function createAppTriggerHost(deps: AppTriggerHostDeps) {
}
}
+ const runRefusal = (run: { webhookId?: string; eventSource?: string }) => (run.eventSource === "app" && run.webhookId ? manager.runRefusal(run.webhookId) : null);
return {
manager,
routes,
@@ -229,7 +230,12 @@ export function createAppTriggerHost(deps: AppTriggerHostDeps) {
undo: (args: { botId: string; threadId: string; requestId: string }) => requests.undo(args),
isApp: (id: string) => manager.get(id) !== null,
/** For the run scheduler, right before a queued run starts: why this app-event run may not. */
- runRefusal: (run: { webhookId?: string; eventSource?: string }) => (run.eventSource === "app" && run.webhookId ? manager.runRefusal(run.webhookId) : null),
+ runRefusal,
+ /** The same check right before the run's turn is sent, since its setup awaits and the bot can lose the account meanwhile. */
+ turnRefusal: (threadId: string, runId: string | undefined) => {
+ const run = runId ? deps.routines().runForThread(threadId) : null;
+ return run && run.id === runId ? runRefusal(run) : null;
+ },
start: () => manager.start(),
stop: () => manager.stop(),
/** Access to the service changed (the desktop handed its token over, or a Cloud registered). */
diff --git a/server/app-triggers.test.ts b/server/app-triggers.test.ts
index 72138cf5b2..b2f4076eb8 100644
--- a/server/app-triggers.test.ts
+++ b/server/app-triggers.test.ts
@@ -593,6 +593,38 @@ describe("a bot limited to some accounts of an app", () => {
expect(triggers.runRefusal("no-such-automation")).toBeNull();
});
+ it("refuses the run's turn right before it is sent when the bot lost the account during its setup", async () => {
+ const { createAppTriggerHost } = await import("./app-trigger-host.ts");
+ const bot: Record<string, unknown> = { id: "bot-1", name: "Scout", color: "#000" };
+ const seen: Array<string | null> = [];
+ let host: ReturnType<typeof createAppTriggerHost> | undefined;
+ const starting = new RoutineManager({
+ file: join(dir, "turn-routines.json"), botState: () => "ready", createTask: () => ({ threadId: "thread-turn" }),
+ startTurn: async (_botId, threadId, _prompt, _runOn, _source, _onDispatchError, runId) => {
+ seen.push(host!.turnRefusal(threadId, runId));
+ bot.connectorScopes = { apps: { gmail: "read" }, accounts: { gmail: [support] } };
+ seen.push(host!.turnRefusal(threadId, runId));
+ },
+ });
+ host = createAppTriggerHost({
+ file: join(dir, "turn-app-triggers.json"), store: { bot: (id: string) => (id === bot.id ? bot : undefined) } as never,
+ connectionMode: () => "managed", access: () => ({ url: broker.url, token: owner.token }),
+ connectedApps: async () => connected, routines: () => starting, webhooks: { list: () => [] } as never,
+ webhookBase: () => "http://127.0.0.1:8800", botState: () => "ready", timeZone: () => "UTC", visibleBot: () => true,
+ emit: () => {}, audit: () => {},
+ });
+ try {
+ const trigger = await host.manager.create({ botId: "bot-1", name: "Sales", prompt: "Draft a reply.", app: "gmail", event: "GMAIL_NEW_GMAIL_MESSAGE", account: "sales" });
+ await broker.deliver(trigger.remote.triggerId!, { subject: "Hello", sender: "[email protected]" }, "msg_turn");
+ await drain(host.manager);
+ await starting.tick();
+ expect(seen).toEqual([null, expect.stringContaining("may no longer use this account")]);
+ expect(host.turnRefusal("thread-turn", undefined)).toBeNull();
+ } finally {
+ starting.close();
+ }
+ });
+
it("lists only the accounts the bot may use in its event list", async () => {
const client = brokerClient(() => ({ url: broker.url, token: owner.token }));
const bot = { connectorScopes: { apps: { gmail: "read" as const }, accounts: { gmail: [support] } } };
diff --git a/server/index.ts b/server/index.ts
index a29562a3cd..c52f349125 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -8235,6 +8235,7 @@ async function beginTurn(
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);
// 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];