Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Keep account limits when every box is ticked and when the account lookup fails
AdityaUmalea0b25257e9f1
diff --git a/server/host/proactive.test.ts b/server/host/proactive.test.tsindex e67f143df9..bd24069287 100644--- a/server/host/proactive.test.ts+++ b/server/host/proactive.test.ts@@ -17,7 +17,7 @@ afterEach(() => {type Services = Record<string, { connected: boolean; accounts?: Array<{ id: string; status: string }> }>;-function setup(options: { team?: boolean; driver?: string; configured?: () => boolean; cloud?: boolean; services?: Services } = {}) {+function setup(options: { team?: boolean; driver?: string; configured?: () => boolean; cloud?: boolean; services?: Services; connectedServices?: () => Promise<Services> } = {}) {const dir = mkdtempSync(join(tmpdir(), "omb-proactive-"));dirs.push(dir);const store = new Store(() => ({ instanceId: "claude", model: "claude-sonnet-5" }));@@ -34,7 +34,7 @@ function setup(options: { team?: boolean; driver?: string; configured?: () => bolet routines: RoutineManager | null = null;const make = () => createProactive({store, cfg: {} as AppConfig, dataDir: dir, team: options.team ?? false, routines: () => routines,- composio: { configured: options.configured ?? (() => true), connectedServices: async () => options.services ?? { gmail: { connected: true }, googlecalendar: { connected: true } }, relayMcp: relay },+ composio: { configured: options.configured ?? (() => true), connectedServices: options.connectedServices ?? (async () => options.services ?? { gmail: { connected: true }, googlecalendar: { connected: true } }), relayMcp: relay },registry: { get: () => ({ driverKind: options.driver ?? "claudeAgent", adapter: {} as never }) },mainThread: (botId) => store.bot(botId)?.threadId, threadBusy: () => false, notify: () => {}, engineName: () => "Claude", needsYou: () => [],hostTimeZone: () => "UTC", decide: () => {}, stale: () => false, release: () => {}, owned: () => {},@@ -136,6 +136,20 @@ describe("the watch's checks", () => {expect(new Set(gmail)).toEqual(new Set(["ca_gone"]));});+ it("still name an allowed account when the account lookup fails", async () => {+ let calls = 0;+ const s = setup({ connectedServices: async () => {+ if (calls++) throw new Error("composio down");+ return { gmail: { connected: true, accounts: [{ id: "ca_newest", status: "ACTIVE" }, { id: "ca_older", status: "ACTIVE" }] }, googlecalendar: { connected: false } };+ } });+ const limited: ConnectorScopes = { apps: { gmail: "read" }, accounts: { gmail: ["ca_older"] } };+ s.store.patchBot(s.chief.id, { connectorScopes: limited });+ await s.proactive.checks.check(s.proactive.ensureWatch()!, { manual: true });+ const gmail = s.reads.flatMap((slug, index) => (slug.startsWith("GMAIL_") ? [s.accounts[index]] : []));+ expect(gmail.length).toBeGreaterThan(0);+ expect(new Set(gmail)).toEqual(new Set(["ca_older"]));+ });+it("belong to the main agent only: a briefing routine of another bot reads nothing", async () => {const s = setup();const other = s.store.createBot({ name: "Scout" });diff --git a/server/host/proactive.ts b/server/host/proactive.tsindex 6f21f07686..97b3612f0b 100644--- a/server/host/proactive.ts+++ b/server/host/proactive.ts@@ -100,10 +100,13 @@ export function createProactive(deps: ProactiveDeps) {},// Several accounts of one app: read the newest active one, as Composio refuses a read that names none. A bot limited// to some accounts reads one of those, named even when it is the app's only one; with none of them active the read- // names the first anyway, so it is refused rather than run as another account.+ // names the first anyway, so it is refused rather than run as another account, also when the lookup fails.accounts: async (botId) => {- const services = await composio.connectedServices(cfg);const scopes = botId ? store.bot(botId)?.connectorScopes : undefined;+ const services = await composio.connectedServices(cfg).catch((error: unknown) => {+ if (!READ_APPS.some((app) => accountLimit(scopes, app))) throw error;+ return {} as Awaited<ReturnType<typeof composio.connectedServices>>;+ });return Object.fromEntries(READ_APPS.flatMap((app) => {const active = (services[app]?.accounts ?? []).filter((one) => /^active$/i.test(one.status)).map((one) => one.id);const allowed = accountLimit(scopes, app);diff --git a/src/components/bot-settings/AccessSection.tsx b/src/components/bot-settings/AccessSection.tsxindex 94b4508a72..b556adf5c4 100644--- a/src/components/bot-settings/AccessSection.tsx+++ b/src/components/bot-settings/AccessSection.tsx@@ -895,7 +895,7 @@ export function AccessSection({);}-function ConnectorScopesControl({ connectedSlugs, labels = {}, accounts = {}, scopes, onChange }: {+export function ConnectorScopesControl({ connectedSlugs, labels = {}, accounts = {}, scopes, onChange }: {connectedSlugs: string[];/** display names by slug; a slug without one shows as itself */labels?: Record<string, string>;@@ -918,15 +918,13 @@ function ConnectorScopesControl({ connectedSlugs, labels = {}, accounts = {}, sc}emit(apps, limits);};- // No limit means every account; ticking the last one back drops the limit again. Ids no longer connected are dropped.+ // No limit means every account, also ones connected later; once a box is changed the ticked ids stay a limit, so a+ // later account is allowed only when it is ticked too. Ids no longer connected are dropped.const connectedIds = (slug: string) => (accounts[slug] ?? []).map((account) => account.id);const allowedIds = (slug: string) => scopes?.accounts?.[slug]?.filter((id) => connectedIds(slug).includes(id)) ?? connectedIds(slug);const setAccount = (slug: string, id: string, on: boolean) => {const ids = allowedIds(slug).filter((one) => one !== id).concat(on ? [id] : []);- const limits = { ...scopes?.accounts };- if (connectedIds(slug).every((one) => ids.includes(one))) delete limits[slug];- else limits[slug] = ids;- emit({ ...scopes?.apps }, limits);+ emit({ ...scopes?.apps }, { ...scopes?.accounts, [slug]: ids });};return <div className="mt-3"><div className="flex items-center justify-between gap-4">diff --git a/src/components/bot-settings/ConnectorScopesControl.dom.test.ts b/src/components/bot-settings/ConnectorScopesControl.dom.test.tsnew file mode 100644index 0000000000..a676726156--- /dev/null+++ b/src/components/bot-settings/ConnectorScopesControl.dom.test.ts@@ -0,0 +1,45 @@+// @vitest-environment happy-dom+// The per-account boxes in a real DOM: ticking every listed account keeps them as a limit, so an account connected+// later stays off until it is ticked too.+import { createElement } from "react";+import { flushSync } from "react-dom";+import { createRoot, type Root } from "react-dom/client";+import { afterEach, beforeEach, describe, expect, it } from "vitest";+import { setLocale } from "@/lib/i18n";+import { ConnectorScopesControl } from "./AccessSection";++let host: HTMLDivElement;+let root: Root;+beforeEach(() => {+ setLocale("en");+ host = document.createElement("div");+ document.body.append(host);+ root = createRoot(host);+});+afterEach(() => {+ flushSync(() => root.unmount());+ host.remove();+});++describe("ConnectorScopesControl accounts", () => {+ const accounts = { gmail: [{ id: "ca_work", alias: "work", status: "ACTIVE" }, { id: "ca_home", alias: "home", status: "ACTIVE" }] };+ const render = (scopes: Parameters<typeof ConnectorScopesControl>[0]["scopes"], onChange: (next: unknown) => void) =>+ flushSync(() => root.render(createElement(ConnectorScopesControl, { connectedSlugs: ["gmail"], accounts, scopes, onChange })));+ const box = (alias: string) => [...host.querySelectorAll("label")].find((label) => label.textContent === alias)!.querySelector("input")!;++ it("keeps a limit when the last unticked account is ticked back", () => {+ const changes: unknown[] = [];+ render({ apps: { gmail: "read" }, accounts: { gmail: ["ca_work"] } }, (next) => changes.push(next));+ expect(box("work").checked).toBe(true);+ expect(box("home").checked).toBe(false);+ flushSync(() => box("home").click());+ expect(changes).toEqual([{ apps: { gmail: "read" }, accounts: { gmail: ["ca_work", "ca_home"] } }]);+ });++ it("starts a limit from every connected account when one is unticked", () => {+ const changes: unknown[] = [];+ render({ apps: { gmail: "read" } }, (next) => changes.push(next));+ flushSync(() => box("work").click());+ expect(changes).toEqual([{ apps: { gmail: "read" }, accounts: { gmail: ["ca_home"] } }]);+ });+});