@devries-cameron20 / composio Mirror · read-only / commits / 023088a3a
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 12:15 UTC

perf(cli): list connected accounts once per execute

`composio execute` listed the user's connected accounts twice on every call: once toolkit-filtered by the account picker, once unfiltered by session creation. The two code paths cannot see each other. The unfiltered list is now fetched once per process and shared, and the picker derives its toolkit subset from it: same slug match, server order kept, first 100, exactly what its own query returned. If the shared list is truncated (more active accounts than one page), the picker falls back to its original request, so results are identical in every case. Interleaved A/B against the parent commit, compiled binaries, 15 runs each on a small response: 1735 -> 1636ms best, 1934 -> 1845ms median. One fewer request on the critical path, ~140ms. `memoizeInProcess` memoizes an Effect per key for the process lifetime, shares one run between concurrent callers, and drops failures so the next caller retries. It also covers `get_latest_version`, which the definition refresh fetched twice with the same key on the stale path. The executor's own version lookup stays unscoped and separate; scoping it to org and project would change which definition it resolves, so that is left as is. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wx9gEjuiHux2weiHjdNcDs

DakshM on Exe (exe.dev) committed Sep 13, 2026 at 11:56 UTC 023088a3a43b30428623234970f1d2547f263323
5 files changed +271 -51
ts/packages/cli/src/services/connected-account-selection.ts
+125 -16
@@ -3,6 +3,7 @@ import { Data, Effect, Option, Schema } from 'effect';
3 import type { ConnectedAccountItem } from 'src/models/connected-accounts';
4 import { decodeConnectedAccountItems } from 'src/effects/decode-connected-account-list';
5 import type { TerminalUI } from 'src/services/terminal-ui';
6 +import { memoizeInProcess } from 'src/utils/memoize-in-process';
7
8 // `status` is an open enum, so this is shape-identical to
9 // `ConnectedAccountItem`; tool-router rows normalize statuses outside the
@@ -137,6 +138,115 @@ export const formatConnectedAccountChoices = (
138 ): ReadonlyArray<string> =>
139 items.filter(isUsableConnectedAccount).sort(compareNewestFirst).map(formatConnectedAccountChoice);
140
141 +export class ActiveConnectedAccountsListError extends Data.TaggedError(
142 + 'services/ActiveConnectedAccountsListError'
143 +)<{
144 + readonly message: string;
145 + readonly cause: unknown;
146 +}> {}
147 +
148 +// `Composio` clients come from `ComposioClientSingleton`, one instance per
149 +// org/project, so identity is a sound cache key. Ids instead of the object
150 +// itself keep the memo key a string.
151 +const clientIds = new WeakMap<Composio, number>();
152 +let nextClientId = 0;
153 +const clientId = (client: Composio): number => {
154 + let id = clientIds.get(client);
155 + if (id === undefined) {
156 + id = nextClientId++;
157 + clientIds.set(client, id);
158 + }
159 + return id;
160 +};
161 +
162 +/**
163 + * Every active connected account of `userId`, fetched once per client and
164 + * user for the lifetime of the process.
165 + *
166 + * `composio execute` needs this list twice on every call, from two code paths
167 + * that cannot see each other: `resolveConnectedAccountForToolkit` picks the
168 + * account for the tool's toolkit, then `resolveToolRouterSessionConnections`
169 + * builds the session's connection context from the full list. Both used to
170 + * issue their own `GET /connected_accounts`, one toolkit-filtered and one not.
171 + * The per-toolkit view is a subset of this list, so both read from here.
172 + *
173 + * `limit: 1000` is the session path's existing page size; a user with more
174 + * active accounts than that was already truncated there.
175 + */
176 +export const listActiveConnectedAccounts = memoizeInProcess({
177 + keyOf: (input: { readonly client: Composio; readonly userId: string }) =>
178 + `${clientId(input.client)}\u0000${input.userId}`,
179 + make: ({ client, userId }) =>
180 + Effect.tryPromise({
181 + try: () =>
182 + client.connectedAccounts.list({
183 + user_ids: [userId],
184 + statuses: ['ACTIVE'],
185 + limit: 1000,
186 + }),
187 + catch: cause =>
188 + new ActiveConnectedAccountsListError({
189 + message: `Failed to list connected accounts for user "${userId}".`,
190 + cause,
191 + }),
192 + }),
193 +});
194 +
195 +const TOOLKIT_ACCOUNTS_PAGE_SIZE = 100;
196 +
197 +// The account picker's own query: one toolkit, first 100 active accounts, in
198 +// server order. Kept verbatim as the fallback so results stay identical when
199 +// the shared list cannot stand in for it.
200 +const fetchConnectedAccountsForToolkit = (params: {
201 + readonly client: Composio;
202 + readonly userId: string;
203 + readonly toolkitSlug: string;
204 +}) =>
205 + Effect.tryPromise({
206 + try: () =>
207 + params.client.connectedAccounts.list({
208 + toolkit_slugs: [params.toolkitSlug],
209 + user_ids: [params.userId],
210 + statuses: ['ACTIVE'],
211 + limit: TOOLKIT_ACCOUNTS_PAGE_SIZE,
212 + }),
213 + // The raw rejection, so the caller's message reads as it always has.
214 + catch: cause => cause,
215 + }).pipe(Effect.map(response => response.items));
216 +
217 +/**
218 + * The active accounts of one toolkit, derived from the shared per-user list
219 + * when that list is complete, so `composio execute` does not issue a second,
220 + * toolkit-filtered `GET /connected_accounts` next to the one session creation
221 + * needs anyway.
222 + *
223 + * Derivation reproduces the server query exactly: same slug match, server
224 + * order preserved, first 100. If the shared list was truncated (more active
225 + * accounts than its page holds), the toolkit's accounts may sit past the cut,
226 + * so the original filtered request runs instead.
227 + */
228 +const listConnectedAccountsForToolkit = (params: {
229 + readonly client: Composio;
230 + readonly userId: string;
231 + readonly toolkitSlug: string;
232 +}) =>
233 + Effect.gen(function* () {
234 + const shared = yield* listActiveConnectedAccounts({
235 + client: params.client,
236 + userId: params.userId,
237 + }).pipe(Effect.mapError(error => error.cause));
238 + const items = shared.items ?? [];
239 + const complete = shared.next_cursor == null && shared.total_items <= items.length;
240 + if (!complete) {
241 + return yield* fetchConnectedAccountsForToolkit(params);
242 + }
243 +
244 + const wantedToolkit = params.toolkitSlug.toLowerCase();
245 + return items
246 + .filter(item => item.toolkit?.slug?.toLowerCase() === wantedToolkit)
247 + .slice(0, TOOLKIT_ACCOUNTS_PAGE_SIZE);
248 + });
249 +
250 export class ConnectedAccountResolutionError extends Data.TaggedError(
251 'services/ConnectedAccountResolutionError'
252 )<{
@@ -155,22 +265,21 @@ export const resolveConnectedAccountForToolkit = (params: {
265 if (!params.toolkitSlug) return undefined;
266 const toolkitSlug = params.toolkitSlug;
267
158 - const accounts = yield* Effect.tryPromise({
159 - try: () =>
160 - params.client.connectedAccounts.list({
161 - toolkit_slugs: [toolkitSlug],
162 - user_ids: [params.userId],
163 - statuses: ['ACTIVE'],
164 - limit: 100,
165 - }),
166 - catch: cause =>
167 - new ConnectedAccountResolutionError({
168 - message: `Failed to load connected accounts for toolkit "${toolkitSlug}": ${String(cause)}`,
169 - toolkitSlug,
170 - cause,
171 - }),
172 - });
173 - const selectableAccounts = yield* decodeConnectedAccountItems(accounts.items).pipe(
268 + const toolkitItems = yield* listConnectedAccountsForToolkit({
269 + client: params.client,
270 + userId: params.userId,
271 + toolkitSlug,
272 + }).pipe(
273 + Effect.mapError(
274 + cause =>
275 + new ConnectedAccountResolutionError({
276 + message: `Failed to load connected accounts for toolkit "${toolkitSlug}": ${String(cause)}`,
277 + toolkitSlug,
278 + cause,
279 + })
280 + )
281 + );
282 + const selectableAccounts = yield* decodeConnectedAccountItems(toolkitItems).pipe(
283 Effect.mapError(
284 cause =>
285 new ConnectedAccountResolutionError({
ts/packages/cli/src/services/tool-input-validation.ts
+38 -28
@@ -9,6 +9,7 @@ import {
9 import { JsonRecordSchema } from 'src/effects/json';
10 import { setupCacheDir } from 'src/effects/setup-cache-dir';
11 import { ComposioToolkitsRepository, getLatestToolVersion } from 'src/services/composio-clients';
12 +import { memoizeInProcess } from 'src/utils/memoize-in-process';
13 import { logToolDebug } from 'src/services/runtime-debug-logger';
14 import { normalizeFileUploadSchema } from 'src/services/tool-file-uploads';
15 import { ComposioUserContext } from 'src/services/user-context';
@@ -184,32 +185,39 @@ export const invalidateToolInputDefinition = (slug: string) =>
185 );
186 });
187
187 -const fetchResolvedLatestToolVersion = (
188 - slug: string,
189 - params?: { readonly orgId?: string; readonly projectId?: string }
190 -) =>
191 - Effect.gen(function* () {
192 - const userContext = yield* ComposioUserContext;
193 - const apiKey = Option.getOrUndefined(userContext.data.apiKey);
194 - if (!apiKey) {
195 - return null;
196 - }
197 -
198 - const latest = yield* getLatestToolVersion({
199 - baseURL: userContext.data.baseURL,
200 - apiKey,
201 - toolSlug: slug,
202 - orgId: params?.orgId,
203 - projectId: params?.projectId,
204 - });
205 - yield* logToolDebug('latest_tool_version', {
206 - slug,
207 - orgId: params?.orgId,
208 - projectId: params?.projectId,
209 - response: latest,
210 - });
211 - return latest.version;
212 - });
188 +// One `get_latest_version` round trip per tool per process. `composio execute`
189 +// asks twice on every call: the background version check forked from the
190 +// command, and `getOrFetchToolInputDefinition` on the executor's file-upload
191 +// path. Both want the same answer within the same second.
192 +const fetchResolvedLatestToolVersion = memoizeInProcess({
193 + keyOf: (input: {
194 + readonly slug: string;
195 + readonly params?: { readonly orgId?: string; readonly projectId?: string };
196 + }) => `${input.slug}\u0000${input.params?.orgId ?? ''}\u0000${input.params?.projectId ?? ''}`,
197 + make: ({ slug, params }) =>
198 + Effect.gen(function* () {
199 + const userContext = yield* ComposioUserContext;
200 + const apiKey = Option.getOrUndefined(userContext.data.apiKey);
201 + if (!apiKey) {
202 + return null;
203 + }
204 +
205 + const latest = yield* getLatestToolVersion({
206 + baseURL: userContext.data.baseURL,
207 + apiKey,
208 + toolSlug: slug,
209 + orgId: params?.orgId,
210 + projectId: params?.projectId,
211 + });
212 + yield* logToolDebug('latest_tool_version', {
213 + slug,
214 + orgId: params?.orgId,
215 + projectId: params?.projectId,
216 + response: latest,
217 + });
218 + return latest.version;
219 + }),
220 +});
221
222 const fetchAndCacheToolInputDefinition = (
223 slug: string,
@@ -242,7 +250,9 @@ const fetchAndCacheToolInputDefinition = (
250 const [tool, latestVersion] = yield* Effect.all(
251 [
252 repo.getToolDetailed(slug),
245 - fetchResolvedLatestToolVersion(slug, params).pipe(Effect.catch(() => Effect.succeed(null))),
253 + fetchResolvedLatestToolVersion({ slug, params }).pipe(
254 + Effect.catch(() => Effect.succeed(null))
255 + ),
256 ],
257 { concurrency: 2 }
258 );
@@ -314,7 +324,7 @@ const refreshAndFetchToolInputDefinitionIfVersionChanged = (
324 params?: { readonly orgId?: string; readonly projectId?: string }
325 ) =>
326 Effect.gen(function* () {
317 - const latestVersion = yield* fetchResolvedLatestToolVersion(slug, params);
327 + const latestVersion = yield* fetchResolvedLatestToolVersion({ slug, params });
328 yield* logToolDebug('resolved_tool_version', {
329 slug,
330 mode: 'refresh',
ts/packages/cli/src/services/tool-router-session-connections.ts
+27 -7
@@ -4,6 +4,7 @@ import { isKnownConnectedAccountStatus } from 'src/models/connected-accounts';
4 import {
5 compareNewestFirst,
6 groupCachedConnectedAccountsByToolkit,
7 + listActiveConnectedAccounts,
8 resolveDefaultConnectedAccountsByToolkit,
9 type SelectableConnectedAccount,
10 } from 'src/services/connected-account-selection';
@@ -58,20 +59,17 @@ const normalizeConnectedAccountStatus = (
59 const isNewerAccount = (candidate: RawConnectedAccount, current: RawConnectedAccount): boolean =>
60 compareNewestFirst(candidate, current) < 0;
61
61 -export const resolveToolRouterSessionConnections = (
62 +const listConnectedAccountsForToolkits = (
63 client: Composio,
64 userId: string,
64 - options?: {
65 - readonly toolkits?: ReadonlyArray<string>;
66 - }
65 + toolkits: ReadonlyArray<string>
66 ) =>
67 Effect.tryPromise({
68 try: () =>
69 client.connectedAccounts.list({
70 user_ids: [userId],
71 statuses: ['ACTIVE'],
73 - toolkit_slugs:
74 - options?.toolkits && options.toolkits.length > 0 ? [...options.toolkits] : undefined,
72 + toolkit_slugs: [...toolkits],
73 limit: 1000,
74 }),
75 catch: cause =>
@@ -79,7 +77,29 @@ export const resolveToolRouterSessionConnections = (
77 message: `Failed to list connected accounts for user "${userId}".`,
78 cause,
79 }),
82 - }).pipe(
80 + });
81 +
82 +export const resolveToolRouterSessionConnections = (
83 + client: Composio,
84 + userId: string,
85 + options?: {
86 + readonly toolkits?: ReadonlyArray<string>;
87 + }
88 +) =>
89 + (options?.toolkits && options.toolkits.length > 0
90 + ? listConnectedAccountsForToolkits(client, userId, options.toolkits)
91 + : // The unfiltered list is the one `composio execute` also needs for its
92 + // account picker, so it is fetched once per process and shared.
93 + listActiveConnectedAccounts({ client, userId }).pipe(
94 + Effect.mapError(
95 + error =>
96 + new ToolRouterSessionConnectionsError({
97 + message: `Failed to list connected accounts for user "${userId}".`,
98 + cause: error.cause,
99 + })
100 + )
101 + )
102 + ).pipe(
103 Effect.map(response => {
104 const items = (response.items ?? []) as ReadonlyArray<RawConnectedAccount>;
105 const unknownStatuses = new Set<string>();
ts/packages/cli/src/utils/memoize-in-process.ts new
+37
@@ -0,0 +1,37 @@
1 +import { Effect } from 'effect';
2 +
3 +/**
4 + * Memoizes an Effect per key for the lifetime of the process, sharing one run
5 + * between concurrent callers. A success stays cached; a failure is dropped so
6 + * the next caller retries instead of replaying the error.
7 + *
8 + * Meant for reads that cannot change within one CLI invocation (a tool's
9 + * latest version, the user's connected accounts) but were being fetched more
10 + * than once by independent code paths on the same command.
11 + */
12 +export const memoizeInProcess = <I, A, E, R>(options: {
13 + readonly keyOf: (input: I) => string;
14 + readonly make: (input: I) => Effect.Effect<A, E, R>;
15 +}): ((input: I) => Effect.Effect<A, E, R>) => {
16 + const cache = new Map<string, Effect.Effect<A, E, R>>();
17 +
18 + return input =>
19 + Effect.suspend(() => {
20 + const key = options.keyOf(input);
21 + const existing = cache.get(key);
22 + if (existing) {
23 + return existing;
24 + }
25 +
26 + // `Effect.cached` only allocates the memo cell; running it here is
27 + // synchronous, so the lookup and the insert cannot interleave with
28 + // another fiber.
29 + const cached = Effect.runSync(
30 + Effect.cached(
31 + options.make(input).pipe(Effect.tapError(() => Effect.sync(() => cache.delete(key))))
32 + )
33 + );
34 + cache.set(key, cached);
35 + return cached;
36 + });
37 +};
ts/packages/cli/test/src/utils/memoize-in-process.test.ts new
+44
@@ -0,0 +1,44 @@
1 +import { describe, expect, it } from '@effect/vitest';
2 +import { Effect, Exit } from 'effect';
3 +import { memoizeInProcess } from 'src/utils/memoize-in-process';
4 +
5 +describe('memoizeInProcess', () => {
6 + it.effect('runs the effect once per key and shares the result', () =>
7 + Effect.gen(function* () {
8 + let calls = 0;
9 + const load = memoizeInProcess({
10 + keyOf: (key: string) => key,
11 + make: key => Effect.sync(() => `${key}:${++calls}`),
12 + });
13 +
14 + const [a, b, c] = yield* Effect.all([load('x'), load('x'), load('y')], {
15 + concurrency: 'unbounded',
16 + });
17 +
18 + expect(a).toBe('x:1');
19 + expect(b).toBe('x:1');
20 + expect(c).toBe('y:2');
21 + expect(calls).toBe(2);
22 + })
23 + );
24 +
25 + it.effect('drops a failure so the next caller retries', () =>
26 + Effect.gen(function* () {
27 + let calls = 0;
28 + const load = memoizeInProcess({
29 + keyOf: (key: string) => key,
30 + make: () =>
31 + Effect.suspend(() =>
32 + ++calls === 1 ? Effect.fail(new Error('first call fails')) : Effect.succeed(calls)
33 + ),
34 + });
35 +
36 + const first = yield* Effect.exit(load('k'));
37 + expect(Exit.isFailure(first)).toBe(true);
38 +
39 + expect(yield* load('k')).toBe(2);
40 + expect(yield* load('k')).toBe(2);
41 + expect(calls).toBe(2);
42 + })
43 + );
44 +});