@devries-cameron20 / composio Mirror · read-only / commits / b9740b8c3
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, 18:30 UTC

fix(cli): bind analytics identity to the caller's credential; propagate spawn failure

Two bugbot findings on the previous fix. - The credential fingerprint was read back via getUserApiKey, but every login path resolves identity BEFORE persisting the new credential (link at 327/512/720 vs ctx.login at 342/560/756). So the stored fingerprint described the previous key — or none on a fresh install — and could never match, leaving every post-login event anonymous. That defeated the identity stitch entirely. The credential is now passed in explicitly by the caller (each already holds the key it just validated), so identity no longer depends on write ordering. - spawnWorker swallowed DetachedProcessSpawnError and enqueue returned true regardless, so a failed spawn still marked the alias as sent and it was never retried — defeating the field added for exactly that purpose. Spawn failure now propagates through enqueue. Added a regression test that reproduces the login ordering (no readable credential at link time); verified it fails without the fix, producing the install_id instead of the Apollo id. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Kshitij Jhunjhunwala committed Jul 23, 2026 at 17:05 UTC b9740b8c300803f45168d09dbbeba364170a2e1c
6 files changed +47 -13
ts/packages/cli/src/analytics/dispatch.ts
+12 -6
@@ -357,8 +357,9 @@ const getWorkerSpawnArgs = (workerFlag: string, encodedPayload: string) =>
357 const spawnWorker = (command: string, args: ReadonlyArray<string>) =>
358 Effect.gen(function* () {
359 const debugEnabled = yield* telemetryDebugEnabled;
360 - yield* spawnDetached(command, args, { inheritStderr: debugEnabled }).pipe(
361 - Effect.catchTag('services/DetachedProcessSpawnError', () => Effect.void)
360 + return yield* spawnDetached(command, args, { inheritStderr: debugEnabled }).pipe(
361 + Effect.as(true),
362 + Effect.catchTag('services/DetachedProcessSpawnError', () => Effect.succeed(false))
363 );
364 });
365
@@ -519,8 +520,7 @@ const enqueuePostHogEnvelope = (envelope: AnalyticsEnvelope) =>
520 INTERNAL_ANALYTICS_WORKER_FLAG,
521 encodedPayload
522 );
522 - yield* spawnWorker(command, args);
523 - return true;
523 + return yield* spawnWorker(command, args);
524 });
525
526 export const trackCliEventEffect = (event: TrackEvent) =>
@@ -585,7 +585,7 @@ export const emitPostHogAlias = (installId: string, apolloUserId: string) =>
585 * anonymous install into it. Safe to call from any command that already resolved
586 * a session, which is how already-authenticated installs get backfilled.
587 */
588 -export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
588 +export const linkApolloIdentityForAnalytics = (apolloUserId: string, apiKey?: string) =>
589 Effect.gen(function* () {
590 const resolved = typeof apolloUserId === 'string' ? apolloUserId.trim() : '';
591 if (resolved.length === 0) {
@@ -600,7 +600,13 @@ export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
600
601 const state = yield* readAnalyticsState;
602 const installId = yield* getOrCreateInstallId;
603 - const fingerprint = yield* getApiKeyFingerprint;
603 + // Callers pass the credential they just validated: login resolves identity
604 + // before persisting it, so reading it back here would fingerprint the
605 + // previous key and never match.
606 + const fingerprint =
607 + typeof apiKey === 'string' && apiKey.length > 0
608 + ? fingerprintApiKey(apiKey)
609 + : yield* getApiKeyFingerprint;
610 if (state?.apollo_user_id !== resolved || state?.api_key_fingerprint !== fingerprint) {
611 yield* mergeAnalyticsState(installId, {
612 apollo_user_id: resolved,
ts/packages/cli/src/commands/connected-accounts/commands/connected-accounts.link.cmd.ts
+1 -1
@@ -242,7 +242,7 @@ const handleNoManagedAuth = (ui: TerminalUI, toolkitSlug: string, noBrowser: boo
242 }).pipe(Effect.catchAll(() => Effect.succeed(null)));
243
244 if (sessionInfo) {
245 - yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id);
245 + yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id, apiKey);
246 }
247
248 if (sessionInfo?.project.org.name) {
ts/packages/cli/src/commands/init.cmd.ts
+1 -1
@@ -126,7 +126,7 @@ const ensureProjectApiKeyInEnv = (params: { cwd: string; selected: ProjectKeys }
126 orgId: selected.orgId,
127 projectId: selected.projectId,
128 });
129 - yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id);
129 + yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id, uakApiKey);
130
131 let projectApiKey = sessionInfo.api_key?.api_key ?? sessionInfo.api_key?.key ?? null;
132 if (!projectApiKey && !hasProjectApiKey) {
ts/packages/cli/src/commands/login.cmd.ts
+7 -4
@@ -324,7 +324,7 @@ const directLogin = (params: { userApiKey: string; org?: string }) =>
324 userApiKey: params.userApiKey,
325 });
326
327 - yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id);
327 + yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id, params.userApiKey);
328
329 const selectedOrg = yield* resolveDirectLoginOrganization({
330 apiKey: params.userApiKey,
@@ -509,7 +509,7 @@ const loginWithKey = (params: {
509 userApiKey: uakApiKey,
510 });
511
512 - yield* linkApolloIdentityForAnalytics(uakSessionInfo.org_member.id);
512 + yield* linkApolloIdentityForAnalytics(uakSessionInfo.org_member.id, uakApiKey);
513
514 const organizations = params.defaultToFirstOrg
515 ? yield* listOrganizations({
@@ -717,7 +717,7 @@ export const browserLogin = (params: {
717 userApiKey: uakApiKey,
718 });
719
720 - yield* linkApolloIdentityForAnalytics(uakSessionInfo.org_member.id);
720 + yield* linkApolloIdentityForAnalytics(uakSessionInfo.org_member.id, uakApiKey);
721
722 // e.g., "pr_xlSR6oN5jIlk"
723 const xProjectId = uakSessionInfo.project.nano_id;
@@ -899,7 +899,10 @@ export const loginCmd = Command.make(
899 orgId: identity.composio?.org_id ?? undefined,
900 }).pipe(Effect.catchAll(() => Effect.succeed(null)));
901 if (agentSessionInfo) {
902 - yield* linkApolloIdentityForAnalytics(agentSessionInfo.org_member.id);
902 + yield* linkApolloIdentityForAnalytics(
903 + agentSessionInfo.org_member.id,
904 + agentUserApiKey
905 + );
906 }
907 }
908 const summary = safeAgentSummary(identity);
ts/packages/cli/src/commands/whoami.cmd.ts
+1 -1
@@ -37,7 +37,7 @@ export const whoamiCmd = Command.make('whoami', {}).pipe(
37 }).pipe(Effect.option);
38 yield* Option.match(sessionInfo, {
39 onNone: () => Effect.void,
40 - onSome: info => linkApolloIdentityForAnalytics(info.org_member.id),
40 + onSome: info => linkApolloIdentityForAnalytics(info.org_member.id, apiKey),
41 });
42 const email = Option.map(sessionInfo, info => info.org_member.email).pipe(
43 Option.getOrUndefined
ts/packages/cli/test/src/analytics.dispatch.test.ts
+25
@@ -679,6 +679,31 @@ describe('CLI analytics dispatch', () => {
679 }).pipe(Effect.provide(makePlatformLayer(home)));
680 });
681
682 + it.effect('binds identity to the credential the caller passes, not one read back', () => {
683 + const home = tempy.temporaryDirectory();
684 + const scriptPath = `${home}/composio.ts`;
685 + // Mirrors real login: identity is resolved BEFORE the credential is persisted,
686 + // so nothing readable from disk/env identifies the user yet.
687 + enableTelemetry('');
688 + process.argv[1] = scriptPath;
689 +
690 + return Effect.gen(function* () {
691 + const fs = yield* FileSystem.FileSystem;
692 + yield* fs.writeFileString(scriptPath, '');
693 +
694 + yield* linkApolloIdentityForAnalytics('om_login_user', 'uak_fresh');
695 + childProcessMocks.spawn.mockClear();
696 +
697 + // The credential is active from here on.
698 + vi.stubEnv('COMPOSIO_USER_API_KEY', 'uak_fresh');
699 + yield* trackCliEventEffect({ name: 'producer_event' });
700 +
701 + const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
702 + const payload = decodeWorkerPayload<{ distinctId: string }>(args[2]!);
703 + expect(payload.distinctId).toBe('om_login_user');
704 + }).pipe(Effect.provide(makePlatformLayer(home)));
705 + });
706 +
707 it.effect('does not create the identity file when logging out with telemetry disabled', () => {
708 const home = tempy.temporaryDirectory();
709 enableTelemetry();