Skip to content

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

Bot access: count room turns as busy when rechecking account-list widening

AdityaUmale294616fe58e9
diff --git a/server/routes/bot-patch.test.ts b/server/routes/bot-patch.test.ts
index 1410e78001..9c561a8836 100644
--- a/server/routes/bot-patch.test.ts
+++ b/server/routes/bot-patch.test.ts
@@ -4,6 +4,7 @@ import { rmSync } from "node:fs";
import type { IncomingMessage, ServerResponse } from "node:http";
import { beforeEach, describe, expect, it, vi } from "vitest";
+import type { ConnectorScopes } from "../../shared/connector-scopes.ts";
import type { WireBot } from "../../shared/wire.ts";
import { VPS_BOT_ALIAS_CHANGE_ERROR, VPS_BOT_ALIAS_LIFECYCLE_ERROR, VPS_BOT_ALIAS_RESOURCES_ERROR } from "../cloud-backend.ts";
import { DATA_DIR, type AppConfig } from "../config.ts";
@@ -40,7 +41,7 @@ beforeEach(() => {
lifecycleBusy.mockReturnValue(false);
});
-function fixture() {
+function fixture(extra: Partial<BotPatchRouteDeps> = {}) {
const store = new Store(() => sonnet);
const bot = store.createBot({ name: "Scout" });
const cfg = { vps: { sshAlias: "production-vps" } } as AppConfig;
@@ -60,6 +61,8 @@ function fixture() {
// A member's session may chat with this bot: the level that still can't move it or change what it may do.
levelOf: () => "chat",
mainThreadTasks: (botId: string) => store.tasks(botId).filter((task) => !privateThreads.has(task.threadId)),
+ activeGroupTurnForBot: () => null,
+ ...extra,
} as unknown as BotPatchRouteDeps;
const handler = createBotPatchRoutes(deps);
const patch = async (botId: string, body: Record<string, unknown>, as: { auth?: RequestAuth; headers?: Record<string, string> } = {}) => {
@@ -389,6 +392,27 @@ describe("PATCH /api/bots/:id phoneApps", () => {
});
});
+describe("PATCH /api/bots/:id connectorScopes", () => {
+ it("refuses a local-API widening while another bot works in a room, and still allows narrowing", async () => {
+ let roomBotId = "";
+ const { store, bot, patch } = fixture({
+ activeGroupTurnForBot: (botId: string) => (botId === roomBotId ? { group: {} as never, threadId: "room-thread" } : null),
+ });
+ roomBotId = store.createBot({ name: "Room worker" }).id;
+ const firstOnly: ConnectorScopes = { apps: { gmail: "read" }, accounts: { gmail: ["first"] } };
+ store.patchBot(bot.id, { connectorScopes: firstOnly });
+
+ const widened = await patch(bot.id, { connectorScopes: { apps: { gmail: "read" }, accounts: { gmail: ["first", "second"] } } });
+
+ expect(widened.status).toBe(409);
+ expect(store.bot(bot.id)?.connectorScopes).toEqual(firstOnly);
+
+ const narrowed = await patch(bot.id, { connectorScopes: firstOnly });
+
+ expect(narrowed.status).toBe(200);
+ });
+});
+
describe("PATCH /api/bots/:id mainThreadChoice", () => {
it("stores the conversation the person chose, clears it with null, and refuses a routine run or another bot's thread", async () => {
const { store, bot, patch } = fixture();
diff --git a/server/routes/bot-patch.ts b/server/routes/bot-patch.ts
index f294025c7d..afc65bab94 100644
--- a/server/routes/bot-patch.ts
+++ b/server/routes/bot-patch.ts
@@ -661,7 +661,8 @@ export function createBotPatchRoutes(deps: BotPatchRouteDeps): RouteHandler {
}
}
if (body.connectorScopes !== undefined && widensConnectorScopes(freshBrowserBot?.connectorScopes, trustPatch.connectorScopes) &&
- auth.kind === "loopback" && !DESKTOP_MANAGED && !browserOrigin && store.bots.some((candidate) => candidate.busy)) {
+ auth.kind === "loopback" && !DESKTOP_MANAGED && !browserOrigin &&
+ store.bots.some((candidate) => candidate.busy || activeGroupTurnForBot(candidate.id))) {
return json(res, 409, { error: "A bot is working right now, so this change has to come from the desktop app or a paired device. Try again once every bot is idle." });
}
// A running turn cannot unmount custom servers; rechecked after the awaits above.