@devries-cameron20 / composio-1 / commits / 52071ed5a
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.

fix(cli): resolve selected-org analytics identity

Kshitij Jhunjhunwala committed Jul 29, 2026 at 19:23 UTC 52071ed5a09e27670dd814d9e03de8989ab046f1
2 files changed +129 -9
ts/packages/cli/src/commands/login.cmd.ts
+7 -8
@@ -384,8 +384,6 @@ const storeCredentials = (params: {
384 initialOrgId: string;
385 initialProjectId: string;
386 fallbackEmail: string;
387 - /** Fallback Apollo org_member id for analytics linking when session/info fails here. */
388 - orgMemberId?: string;
387 /** When true, skip the init/switch hints and outro (shown later after org picker). */
388 skipHints?: boolean;
389 /** When true, skip JSON output (emitted later after org picker with final selection). */
@@ -402,7 +400,6 @@ const storeCredentials = (params: {
400 initialOrgId,
401 initialProjectId,
402 fallbackEmail,
405 - orgMemberId,
403 skipHints = false,
404 skipOutput = false,
405 deferAnalyticsIdentity = false,
@@ -447,13 +444,17 @@ const storeCredentials = (params: {
444
445 yield* ctx.login(uakApiKey, orgId, testUserId);
446 // Linked only after the credential persists, so stitching cannot outlive a failed login.
450 - const linkedOrgMemberId = sessionInfo?.org_member.id ?? orgMemberId;
451 - if (linkedOrgMemberId && !deferAnalyticsIdentity) {
447 + if (!deferAnalyticsIdentity) {
448 yield* linkAnalyticsIdentityForOrg({
449 apiKey: uakApiKey,
450 baseURL,
451 orgId,
456 - knownIdentity: { orgId, orgMemberId: linkedOrgMemberId },
452 + knownIdentity: sessionInfo
453 + ? {
454 + orgId: sessionInfo.project.org.id,
455 + orgMemberId: sessionInfo.org_member.id,
456 + }
457 + : undefined,
458 });
459 }
460 yield* primeConsumerConnectedToolkitsCacheInBackground({
@@ -572,7 +573,6 @@ const loginWithKey = (params: {
573 initialOrgId: xOrgId,
574 initialProjectId: xProjectId,
575 fallbackEmail: linkedSession.account.email,
575 - orgMemberId: uakSessionInfo.org_member.id,
576 skipHints: willRunPicker,
577 skipOutput: true,
578 deferAnalyticsIdentity: willRunPicker,
@@ -777,7 +777,6 @@ export const browserLogin = (params: {
777 initialOrgId: xOrgId,
778 initialProjectId: xProjectId,
779 fallbackEmail: linkedSession.account.email,
780 - orgMemberId: uakSessionInfo.org_member.id,
780 skipHints: willRunPicker,
781 skipOutput: willRunPicker,
782 deferAnalyticsIdentity: willRunPicker,
ts/packages/cli/test/src/commands/login.cmd.test.ts
+122 -1
@@ -1,6 +1,6 @@
1 import { describe, expect, layer } from '@effect/vitest';
2 import { vi, afterEach } from 'vitest';
3 -import { Console, Effect, Exit, Option } from 'effect';
3 +import { Console, DateTime, Effect, Exit, Option } from 'effect';
4 import { HelpDoc, ValidationError } from '@effect/cli';
5 import path from 'node:path';
6 import { FileSystem } from '@effect/platform';
@@ -11,6 +11,7 @@ import { setupCacheDir } from 'src/effects/setup-cache-dir';
11 import { getTerminalCapabilities, TerminalUI } from 'src/services/terminal-ui';
12 import { writeStoredAgentIdentity } from 'src/services/agents';
13 import { ComposioUserContext } from 'src/services/user-context';
14 +import { ComposioSessionRepository } from 'src/services/composio-clients';
15
16 vi.mock('open', () => ({
17 default: vi.fn(async () => undefined),
@@ -422,4 +423,124 @@ describe('CLI: composio login', () => {
423 })
424 );
425 });
426 +
427 + layer(TestLive())(it => {
428 + it.scoped(
429 + '[Given] selected-org enrichment fails [When] completing --poll [Then] links the selected org membership',
430 + () =>
431 + Effect.gen(function* () {
432 + const fs = yield* FileSystem.FileSystem;
433 + const cacheDir = yield* setupCacheDir;
434 + const now = yield* DateTime.now;
435 + const expiresAt = DateTime.add(now, { minutes: 10 });
436 + const sessionId = 'poll-session-id';
437 + const sessionRepository = new ComposioSessionRepository({
438 + createSession: () =>
439 + Effect.succeed({
440 + id: sessionId,
441 + code: '001122',
442 + expiresAt,
443 + status: 'pending',
444 + }),
445 + getSession: () =>
446 + Effect.succeed({
447 + id: sessionId,
448 + code: '001122',
449 + expiresAt,
450 + status: 'linked',
451 + api_key: 'uak_poll_key',
452 + account: {
453 + id: 'account_id',
454 + name: 'Poll User',
455 + email: 'poll@example.com',
456 + },
457 + }),
458 + getRealtimeCredentials: () =>
459 + Effect.succeed({
460 + project_id: 'proj_test',
461 + pusher_key: 'pusher_test_key',
462 + pusher_cluster: 'mt1',
463 + }),
464 + authRealtimeChannel: () =>
465 + Effect.succeed({
466 + auth: 'mock:auth',
467 + channel_data: undefined,
468 + }),
469 + });
470 +
471 + yield* fs.writeFileString(
472 + path.join(cacheDir, 'pending-login-session.json'),
473 + `${JSON.stringify(
474 + {
475 + key: sessionId,
476 + loginUrl: `https://dashboard.composio.dev/?cliKey=${sessionId}`,
477 + expiresAt: DateTime.formatIso(expiresAt),
478 + cachedAt: new Date().toISOString(),
479 + },
480 + null,
481 + 2
482 + )}\n`
483 + );
484 +
485 + vi.spyOn(globalThis, 'fetch').mockImplementation(
486 + async (requestInput: RequestInfo | URL, init?: RequestInit) => {
487 + const url = requestUrl(requestInput);
488 + const headers = new Headers(init?.headers);
489 +
490 + if (url.includes('/api/v3/auth/session/info')) {
491 + const selectedOrgId = headers.get('x-org-id');
492 + if (headers.has('x-project-id')) {
493 + return mockFetchResponse({ message: 'Selected-org enrichment failed' }, 500);
494 + }
495 + return mockFetchResponse({
496 + project: {
497 + name: 'Default Project',
498 + id: 'project_id_default',
499 + org_id: selectedOrgId ?? 'org_home',
500 + nano_id: 'project_default',
501 + email: 'project@example.com',
502 + created_at: '2026-01-01T00:00:00.000Z',
503 + updated_at: '2026-01-01T00:00:00.000Z',
504 + org: {
505 + id: selectedOrgId ?? 'org_home',
506 + name: selectedOrgId ? 'Selected Org' : 'Home Org',
507 + plan: 'enterprise',
508 + },
509 + },
510 + org_member: {
511 + id: selectedOrgId ? 'member_selected' : 'member_home',
512 + user_id: 'user_123',
513 + email: 'poll@example.com',
514 + name: 'Poll User',
515 + role: 'admin',
516 + },
517 + api_key: null,
518 + });
519 + }
520 +
521 + if (url.includes('/api/v3/org/list?limit=50')) {
522 + return mockFetchResponse({
523 + organizations: [
524 + { id: 'org_selected', name: 'Selected Org' },
525 + { id: 'org_home', name: 'Home Org' },
526 + ],
527 + });
528 + }
529 +
530 + return mockFetchResponse({});
531 + }
532 + );
533 +
534 + yield* cli(['login', '--poll', '--no-skill-install']).pipe(
535 + Effect.provideService(ComposioSessionRepository, sessionRepository)
536 + );
537 +
538 + const ctx = yield* ComposioUserContext;
539 + expect(Option.getOrUndefined(ctx.data.orgId)).toBe('org_selected');
540 + expect(analyticsMocks.linkCalls).toEqual([
541 + { apolloUserId: 'member_selected', loggedInAtLinkTime: true },
542 + ]);
543 + })
544 + );
545 + });
546 });