Skip to content

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

fix(composio): bound the earlier-installation lookup to a few pages

Bugfixer 2e94c6ceb8ab9
diff --git a/server/composio-reuse.test.ts b/server/composio-reuse.test.ts
index 41393f3707..aedfbab9fb 100644
--- a/server/composio-reuse.test.ts
+++ b/server/composio-reuse.test.ts
@@ -76,6 +76,13 @@ describe("a fresh installation connecting a project an earlier installation used
expect(accountQueries.every((query) => !query.has("user_ids"))).toBe(true);
});
+ it("reads only a few pages, and starts a new user when the project is too big to tell", async () => {
+ accounts = Array.from({ length: 30 }, (_, n) => account("openmausbot_earlier", `app${n}`));
+ const prepared = await prepareProjectSession("ak_reuse");
+ expect(prepared.userId).toMatch(NEW_USER);
+ expect(accountQueries.length).toBe(4);
+ });
+
it("starts a new user when the project has no earlier installation's live connections", async () => {
accounts = [account("someone_elses_app", "notion"), account("openmausbot_gone", "gmail", "EXPIRED")];
const prepared = await prepareProjectSession("ak_reuse");
diff --git a/server/composio-session.ts b/server/composio-session.ts
index 4e3a6e506c..2708d0ff71 100644
--- a/server/composio-session.ts
+++ b/server/composio-session.ts
@@ -157,18 +157,24 @@ async function createProjectSession(apiKey: string, userId: string, authConfigs:
return session.session_id;
}
+/** Pages of connected accounts read when looking for an earlier installation's user, which bounds how long setup waits. */
+const EARLIER_USER_PAGES = 4;
+
/** The one OpenMausBot Composio user in this project, when it still has a live connection, so a fresh installation (a
* reinstall, another computer) gets that installation's apps back; null when there is none, more than one (even with
* only expired connections, since it can't tell whose is whose), or Composio can't say. */
async function earlierInstallationUser(apiKey: string): Promise<string | null> {
const users = new Set<string>();
- const sawSeveralUsers = (page: ConnectedAccountResponse[]) => {
+ let pages = 0;
+ const enough = (page: ConnectedAccountResponse[]) => {
for (const { user_id } of page) if (user_id?.startsWith("openmausbot_")) users.add(user_id);
- return users.size > 1;
+ pages += 1;
+ return users.size > 1 || pages >= EARLIER_USER_PAGES;
};
try {
- const accounts = await listConnectedAccounts(apiKey, null, [], sawSeveralUsers);
- if (users.size !== 1) return null;
+ const accounts = await listConnectedAccounts(apiKey, null, [], enough);
+ // A project too big to read in a few pages can't say there's only one: setup goes on as before, with a new user.
+ if (users.size !== 1 || pages >= EARLIER_USER_PAGES) return null;
return accounts.find((account) => account.status === "ACTIVE" && users.has(account.user_id ?? ""))?.user_id ?? null;
} catch (error) {
console.warn(`[composio] Could not read the project's earlier connections: ${error instanceof Error ? error.message : String(error)}`);