Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
fix(cli): link the analytics identity only after credentials persist
All human login flows now call linkApolloIdentityForAnalytics after ctx.login instead of before, and every agent entry point links through the loginWithAgentIdentity chokepoint (best-effort, never fatal). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Kshitij Jhunjhunwala committed
Jul 27, 2026 at 15:08 UTC
b93a05ea823fb3be7985f2036149eeb7ed6c41a3
3 files changed
+44
-20
ts/packages/cli/src/commands/login.cmd.ts
+11
-20
@@ -324,8 +324,6 @@ const directLogin = (params: { userApiKey: string; org?: string }) =>
324
userApiKey: params.userApiKey,
325
});
326
327
- yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id, params.userApiKey);
328
-
327
const selectedOrg = yield* resolveDirectLoginOrganization({
328
apiKey: params.userApiKey,
329
baseURL: ctx.data.baseURL,
@@ -340,6 +338,7 @@ const directLogin = (params: { userApiKey: string; org?: string }) =>
338
: Option.getOrUndefined(ctx.data.testUserId);
339
340
yield* ctx.login(params.userApiKey, selectedOrg.id, testUserId);
341
+ yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id, params.userApiKey);
342
yield* primeConsumerConnectedToolkitsCacheInBackground({
343
orgId: selectedOrg.id,
344
});
@@ -363,6 +362,8 @@ const storeCredentials = (params: {
362
initialOrgId: string;
363
initialProjectId: string;
364
fallbackEmail: string;
365
+ /** Fallback Apollo org_member id for analytics linking when session/info fails here. */
366
+ orgMemberId?: string;
367
/** When true, skip the init/switch hints and outro (shown later after org picker). */
368
skipHints?: boolean;
369
/** When true, skip JSON output (emitted later after org picker with final selection). */
@@ -377,6 +378,7 @@ const storeCredentials = (params: {
378
initialOrgId,
379
initialProjectId,
380
fallbackEmail,
381
+ orgMemberId,
382
skipHints = false,
383
skipOutput = false,
384
} = params;
@@ -419,6 +421,11 @@ const storeCredentials = (params: {
421
}
422
423
yield* ctx.login(uakApiKey, orgId, testUserId);
424
+ // Linked only after the credential persists, so stitching cannot outlive a failed login.
425
+ const linkedOrgMemberId = sessionInfo?.org_member.id ?? orgMemberId;
426
+ if (linkedOrgMemberId) {
427
+ yield* linkApolloIdentityForAnalytics(linkedOrgMemberId, uakApiKey);
428
+ }
429
yield* primeConsumerConnectedToolkitsCacheInBackground({
430
orgId,
431
});
@@ -509,8 +516,6 @@ const loginWithKey = (params: {
516
userApiKey: uakApiKey,
517
});
518
512
- yield* linkApolloIdentityForAnalytics(uakSessionInfo.org_member.id, uakApiKey);
513
-
519
const organizations = params.defaultToFirstOrg
520
? yield* listOrganizations({
521
baseURL: ctx.data.baseURL,
@@ -537,6 +542,7 @@ const loginWithKey = (params: {
542
initialOrgId: xOrgId,
543
initialProjectId: xProjectId,
544
fallbackEmail: linkedSession.account.email,
545
+ orgMemberId: uakSessionInfo.org_member.id,
546
skipHints: willRunPicker,
547
skipOutput: true,
548
});
@@ -717,8 +723,6 @@ export const browserLogin = (params: {
723
userApiKey: uakApiKey,
724
});
725
720
- yield* linkApolloIdentityForAnalytics(uakSessionInfo.org_member.id, uakApiKey);
721
-
726
// e.g., "pr_xlSR6oN5jIlk"
727
const xProjectId = uakSessionInfo.project.nano_id;
728
// e.g., "k2OiqRLMdHyM"
@@ -733,6 +737,7 @@ export const browserLogin = (params: {
737
initialOrgId: xOrgId,
738
initialProjectId: xProjectId,
739
fallbackEmail: linkedSession.account.email,
740
+ orgMemberId: uakSessionInfo.org_member.id,
741
skipHints: willRunPicker,
742
skipOutput: willRunPicker,
743
});
@@ -891,20 +896,6 @@ export const loginCmd = Command.make(
896
yield* ensureAgentSignupAllowed;
897
const identity = yield* getOrSignupReadyAgent();
898
yield* loginWithAgentIdentity(identity);
894
- const agentUserApiKey = identity.composio?.user_api_key;
895
- if (agentUserApiKey) {
896
- const agentSessionInfo = yield* getSessionInfoByUserApiKey({
897
- baseURL: ctx.data.baseURL,
898
- userApiKey: agentUserApiKey,
899
- orgId: identity.composio?.org_id ?? undefined,
900
- }).pipe(Effect.catchAll(() => Effect.succeed(null)));
901
- if (agentSessionInfo) {
902
- yield* linkApolloIdentityForAnalytics(
903
- agentSessionInfo.org_member.id,
904
- agentUserApiKey
905
- );
906
- }
907
- }
899
const summary = safeAgentSummary(identity);
900
yield* ui.log.success(
901
`Logged in as Composio agent ${summary.email ?? summary.slug ?? ''}`
ts/packages/cli/src/services/agents.ts
+7
@@ -3,7 +3,9 @@ import { Data, Effect, Option, Predicate, Schema } from 'effect';
3
import { JsonRecordSchema } from 'src/effects/json';
4
import { setupCacheDir } from 'src/effects/setup-cache-dir';
5
import { ComposioUserContext } from 'src/services/user-context';
6
+import { getSessionInfoByUserApiKey } from 'src/services/composio-clients';
7
import { primeConsumerConnectedToolkitsCacheInBackground } from 'src/services/consumer-short-term-cache';
8
+import { linkApolloIdentityForAnalytics } from 'src/analytics/dispatch';
9
10
export const AGENT_CONFIG_FILE_NAME = 'agent.json';
11
export const DEFAULT_AGENTS_BASE_URL = 'https://agents.composio.dev';
@@ -398,5 +400,10 @@ export const loginWithAgentIdentity = (identity: AgentIdentity) =>
400
}
401
402
yield* ctx.login(userApiKey, orgId);
403
+ // Best-effort analytics stitch after the credential persists; must never break login.
404
+ yield* getSessionInfoByUserApiKey({ baseURL: ctx.data.baseURL, userApiKey, orgId }).pipe(
405
+ Effect.flatMap(info => linkApolloIdentityForAnalytics(info.org_member.id, userApiKey)),
406
+ Effect.catchAllCause(() => Effect.void)
407
+ );
408
yield* primeConsumerConnectedToolkitsCacheInBackground({ orgId });
409
});
ts/packages/cli/test/src/commands/login.cmd.test.ts
+26
@@ -9,6 +9,25 @@ import * as constants from 'src/constants';
9
import { setupCacheDir } from 'src/effects/setup-cache-dir';
10
import { ComposioUserContext } from 'src/services/user-context';
11
12
+const analyticsMocks = vi.hoisted(() => ({
13
+ linkCalls: [] as Array<{ apolloUserId: string; loggedInAtLinkTime: boolean }>,
14
+}));
15
+
16
+// Records each identity link and whether the credential had already been
17
+// stored via ctx.login at call time — the link-after-persistence ordering.
18
+vi.mock('src/analytics/dispatch', async importOriginal => {
19
+ const actual = await importOriginal<typeof import('src/analytics/dispatch')>();
20
+ const { Effect } = await import('effect');
21
+ const { ComposioUserContext } = await import('src/services/user-context');
22
+ return {
23
+ ...actual,
24
+ linkApolloIdentityForAnalytics: ((apolloUserId: string) =>
25
+ Effect.map(ComposioUserContext, ctx => {
26
+ analyticsMocks.linkCalls.push({ apolloUserId, loggedInAtLinkTime: ctx.isLoggedIn() });
27
+ })) as unknown as typeof actual.linkApolloIdentityForAnalytics,
28
+ };
29
+});
30
+
31
const mockFetchResponse = (body: unknown, status = 200) =>
32
new Response(JSON.stringify(body), {
33
status,
@@ -39,6 +58,7 @@ const setTtyState = (state: { stdin: boolean; stdout: boolean; stderr: boolean }
58
describe('CLI: composio login', () => {
59
afterEach(() => {
60
vi.restoreAllMocks();
61
+ analyticsMocks.linkCalls.length = 0;
62
});
63
64
describe('login --help', () => {
@@ -203,6 +223,12 @@ describe('CLI: composio login', () => {
223
224
const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
225
expect(output).toContain('Logged in as cli@example.com in "Selected Org"');
226
+
227
+ // The analytics identity is linked exactly once, and only after the
228
+ // credential was stored via ctx.login.
229
+ expect(analyticsMocks.linkCalls).toEqual([
230
+ { apolloUserId: 'member_123', loggedInAtLinkTime: true },
231
+ ]);
232
})
233
);
234
});