Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Still link when the request to raise an old Session's account cap cannot reach Composio
Bugfixer 21e585b9bbf91
diff --git a/cloudflare/composio-broker/src/index.test.ts b/cloudflare/composio-broker/src/index.test.tsindex d67eb9c104..5c449bdc47 100644--- a/cloudflare/composio-broker/src/index.test.ts+++ b/cloudflare/composio-broker/src/index.test.ts@@ -379,6 +379,27 @@ describe("connected-apps broker boundaries", () => {expect(fetchCalls.some((call) => call.init?.method === "POST" && call.url.endsWith("/tool_router/session"))).toBe(false);});+ it("still links when the request to raise the old cap cannot reach Composio", async () => {+ const fetchCalls: Array<{ url: string; init?: RequestInit }> = [];+ const { env, ctx } = testEnv(fetchCalls, "offline-cap-user");+ const installation = { id: "offline-cap-install", composio_user_id: "offline-cap-user", session_id: "trs_cap_offline", disabled_at: null };+ vi.stubGlobal("fetch", async (input: string | URL | Request, init?: RequestInit) => {+ const url = String(input);+ fetchCalls.push({ url, init });+ if (init?.method === "PATCH") throw new TypeError("fetch failed");+ if (url.includes("/connected_accounts")) return Response.json({ items: [{ id: "ca_0", alias: "sub-0", status: "ACTIVE", toolkit: { slug: "gmail" } }] });+ if (url.endsWith("/link")) return Response.json({ redirect_url: "https://connect.composio.dev/link/gmail" });+ if (url.includes("/auth_configs")) return Response.json({ items: [] });+ const old = session("trs_cap_offline", "offline-cap-user");+ return Response.json({ ...old, config: { ...old.config, multi_account: { ...multiAccount, max_accounts_per_toolkit: 5 } } });+ });++ const response = await authorize("gmail", "sub-1", installation, env as never, ctx as never);+ expect(response.status).toBe(200);+ expect(fetchCalls.some((call) => call.init?.method === "PATCH")).toBe(true);+ expect(fetchCalls.some((call) => call.url.endsWith("/trs_cap_offline/link"))).toBe(true);+ });+it("links a toolkit whose only account failed to sign in", async () => {const fixture = accountFixture("failed-user");fixture.accounts = [{ id: "ca_failed", status: "FAILED", toolkit: { slug: "gmail" } }];diff --git a/cloudflare/composio-broker/src/index.ts b/cloudflare/composio-broker/src/index.tsindex 43ca83809f..6eaa9a9f39 100644--- a/cloudflare/composio-broker/src/index.ts+++ b/cloudflare/composio-broker/src/index.ts@@ -260,15 +260,19 @@ const multiAccountUpgradeAttempted = new Set<string>();const accountCapRaised = new Set<string>();/** A Session made under a lower account cap gets the current one, in place, so- * its MCP endpoint stays. If Composio refuses, the old cap stays and the link- * goes ahead. */+ * its MCP endpoint stays. If Composio refuses or can't be reached, the old cap+ * stays, the next link tries again, and this link goes ahead. */async function raiseAccountCap(session: ComposioSession, env: Env) {if (session.maxAccounts === undefined || session.maxAccounts >= MULTI_ACCOUNT_CONFIG.max_accounts_per_toolkit|| accountCapRaised.has(session.sessionId)) return;const response = await composioRequest(env, `/tool_router/session/${encodeURIComponent(session.sessionId)}`, {method: "PATCH",body: JSON.stringify({ multi_account: MULTI_ACCOUNT_CONFIG }),+ }).catch((error: unknown) => {+ console.warn(JSON.stringify({ message: "account cap not raised", error: error instanceof Error ? error.message : String(error) }));+ return null;});+ if (!response) return;if (!response.ok) {console.warn(JSON.stringify({ message: "account cap not raised", status: response.status }));return;diff --git a/server/composio-session.ts b/server/composio-session.tsindex f51c8c588d..27feeaa096 100644--- a/server/composio-session.ts+++ b/server/composio-session.ts@@ -260,7 +260,7 @@ async function nameAuthConfigsInPlace(apiKey: string, session: SessionResponse,}/** A Session made under a lower account cap gets the current one, in place, so its MCP endpoint stays. If Composio- * refuses, the old cap stays and the link goes ahead. */+ * refuses or can't be reached, the old cap stays, the next link tries again, and this link goes ahead. */export async function raiseAccountCap(apiKey: string, session: SessionResponse): Promise<void> {const cap = session.config?.multi_account?.max_accounts_per_toolkit;if (cap === undefined || cap >= MULTI_ACCOUNT_CONFIG.max_accounts_per_toolkit || accountCapRaised.has(session.session_id)) return;@@ -269,7 +269,11 @@ export async function raiseAccountCap(apiKey: string, session: SessionResponse):headers: projectHeaders(apiKey, true),body: JSON.stringify({ multi_account: MULTI_ACCOUNT_CONFIG }),signal: AbortSignal.timeout(30_000),+ }).catch((error: unknown) => {+ console.warn(`[composio] Could not reach Composio to raise the Session's account cap (${error instanceof Error ? error.message : String(error)})`);+ return null;});+ if (!res) return;if (!res.ok) {console.warn(`[composio] Composio would not raise the Session's account cap (${await responseError(res, `HTTP ${res.status}`)})`);return;diff --git a/server/composio.test.ts b/server/composio.test.tsindex e119254b45..e1df3e0bf6 100644--- a/server/composio.test.ts+++ b/server/composio.test.ts@@ -1412,6 +1412,29 @@ describe.sequential("Composio Sessions", () => {}});+ it("still links when the request to raise the old cap cannot reach Composio, and tries again next time", async () => {+ const cfg: AppConfig = {+ composio: { apiKey: "ak_test", userId: "openmausbot_existing", sessionId: "trs_test" },+ };+ sessionAccountCap = 5;+ connectedAccountsOverride = [{ id: "ca_sub_0", alias: "sub-0", status: "ACTIVE", toolkit: { slug: "slack" } }];+ const realFetch = globalThis.fetch;+ const fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => {+ if (init?.method === "PATCH") throw new TypeError("fetch failed");+ return realFetch(input, init);+ });+ const before = calls.length;+ try {+ await expect(authorizeService(cfg, "slack", "sub-1")).resolves.toEqual({ url: "https://connect.composio.dev/link/slack" });+ expect(calls.slice(before).some((call) => call.method === "POST" && call.path.endsWith("/link"))).toBe(true);+ expect(fetchSpy.mock.calls.some(([, init]) => init?.method === "PATCH")).toBe(true);+ } finally {+ fetchSpy.mockRestore();+ sessionAccountCap = 10;+ connectedAccountsOverride = null;+ }+ });+it("links a sixth account, raising a Session made under the old cap of 5 in place first", async () => {const cfg: AppConfig = {composio: { apiKey: "ak_test", userId: "openmausbot_existing", sessionId: "trs_test" },