@devries-cameron20 / composio-1 / commits / 8467efdfb
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): whoami respects the org selected via `orgs switch` (#3832)

## Summary `composio whoami` reports the wrong organization after `composio orgs switch`. It always shows the API key's home org, ignoring the switched-to org, even though `composio orgs list` correctly reflects the selection. **Root cause:** `whoami` resolves session info via `getSessionInfoByUserApiKey`, which sent only `x-user-api-key` and omitted `x-org-id`. Without that header the backend falls back to the API key's home org. Consumer commands (`search`, `execute`, …) already forward the selected org via `resolveCommandProject`, so only `whoami` was affected — a display bug, not an API bug. **Verification that the backend honors the header** (direct calls to `GET /api/v3/auth/session/info`): | `x-org-id` | returned project/org | |---|---| | org A | org A | | org B | org B | | (omitted) | API key's home org | ## Changes - `getSessionInfoByUserApiKey` accepts an optional `orgId` and forwards it as `x-org-id` when present (login-time callers pass none, so their behavior is unchanged). - `whoami` passes the selected global org (`ctx.data.orgId`). - Unit tests asserting the header is sent when an org is selected and omitted otherwise. ## Type of change - [x] Bug fix ## How Has This Been Tested? - New unit tests: `test/src/services/composio-clients.test.ts` (2 cases). - Existing `whoami.cmd.test.ts` still passes (5 tests total green). - Manual end-to-end with a real UAK against two orgs: a binary built from this branch reports the switched org correctly (`orgs switch` → `whoami`), whereas the released 0.2.31 binary always reported the home org. ## Checklist - [x] I have read the Code of Conduct and this PR adheres to it - [x] I ran linters/tests locally and they passed - [ ] I updated documentation as needed (no user-facing docs affected) - [x] I added tests or explain why not applicable - [x] I added a changeset if this change affects published packages

xHai committed Jul 16, 2026 at 22:11 UTC 8467efdfb95c27dba1a587639d8f3a8368cedb92
4 files changed +68 -4
.changeset/whoami-respect-switched-org.md new
+5
@@ -0,0 +1,5 @@
1 +---
2 +'@composio/cli': patch
3 +---
4 +
5 +Fix `composio whoami` reporting the wrong organization after `composio orgs switch`. The command resolved session info without the `x-org-id` header, so the backend fell back to the API key's home org and ignored the switch. It now forwards the selected global org, matching how consumer commands resolve org context.
ts/packages/cli/src/commands/whoami.cmd.ts
+2
@@ -31,6 +31,8 @@ export const whoamiCmd = Command.make('whoami', {}).pipe(
31 const sessionInfo = yield* getSessionInfoByUserApiKey({
32 baseURL: ctx.data.baseURL,
33 userApiKey: apiKey,
34 + // Reflect the org selected via `composio orgs switch`, not the key's home org.
35 + orgId: Option.getOrUndefined(ctx.data.orgId),
36 }).pipe(Effect.option);
37 const email = Option.map(sessionInfo, info => info.org_member.email).pipe(
38 Option.getOrUndefined
ts/packages/cli/src/services/composio-clients.ts
+11 -4
@@ -1000,15 +1000,21 @@ export const getSessionInfo = (params: {
1000 });
1001
1002 /**
1003 - * Calls GET /api/v3/auth/session/info using only the x-user-api-key header.
1004 - * Unlike getSessionInfo which requires org/project IDs, this variant resolves
1005 - * session metadata from the UAK alone — useful during login before org/project
1006 - * context is known.
1003 + * Calls GET /api/v3/auth/session/info using the x-user-api-key header.
1004 + * Unlike getSessionInfo which requires both org AND project IDs, this variant
1005 + * resolves session metadata from the UAK alone — useful during login before
1006 + * org/project context is known.
1007 + *
1008 + * When `orgId` is provided, it is forwarded as `x-org-id` so the backend
1009 + * resolves the session against the caller's currently-selected global org
1010 + * (set via `composio orgs switch`). Without it, the backend falls back to the
1011 + * API key's home org, which makes the response ignore any org switch.
1012 * Uses plain fetch since this endpoint is not available in @composio/client.
1013 */
1014 export const getSessionInfoByUserApiKey = (params: {
1015 baseURL: string;
1016 userApiKey: string;
1017 + orgId?: string;
1018 }): Effect.Effect<SessionInfoResponse, HttpServerError | HttpDecodingError> =>
1019 Effect.gen(function* () {
1020 const response = yield* Effect.tryPromise({
@@ -1018,6 +1024,7 @@ export const getSessionInfoByUserApiKey = (params: {
1024 redirect: 'error',
1025 headers: {
1026 'x-user-api-key': params.userApiKey,
1027 + ...(params.orgId ? { 'x-org-id': params.orgId } : {}),
1028 'User-Agent': '@composio/cli',
1029 Accept: '*/*',
1030 'Content-Type': 'application/json',
ts/packages/cli/test/src/services/composio-clients.test.ts new
+50
@@ -0,0 +1,50 @@
1 +import { afterEach, describe, expect, it, vi } from 'vitest';
2 +import { Effect } from 'effect';
3 +import { getSessionInfoByUserApiKey } from 'src/services/composio-clients';
4 +
5 +const emptyOkResponse = () =>
6 + new Response(JSON.stringify({}), {
7 + status: 200,
8 + headers: { 'Content-Type': 'application/json' },
9 + });
10 +
11 +describe('getSessionInfoByUserApiKey org context', () => {
12 + afterEach(() => {
13 + vi.restoreAllMocks();
14 + });
15 +
16 + // The response body does not satisfy SessionInfoResponse, so decoding fails —
17 + // but the fetch call (whose headers we assert) has already happened by then.
18 + const runIgnoringDecode = (params: { baseURL: string; userApiKey: string; orgId?: string }) =>
19 + Effect.runPromise(getSessionInfoByUserApiKey(params).pipe(Effect.ignore));
20 +
21 + it('[Given] orgId [Then] forwards it as x-org-id so the backend honors the switched org', async () => {
22 + const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(emptyOkResponse());
23 +
24 + await runIgnoringDecode({
25 + baseURL: 'https://backend.composio.dev',
26 + userApiKey: 'uak_test',
27 + orgId: 'ok_selected_org',
28 + });
29 +
30 + const [url, init] = fetchSpy.mock.calls[0]!;
31 + expect(String(url)).toContain('/api/v3/auth/session/info');
32 + const headers = new Headers((init as RequestInit).headers);
33 + expect(headers.get('x-user-api-key')).toBe('uak_test');
34 + expect(headers.get('x-org-id')).toBe('ok_selected_org');
35 + });
36 +
37 + it('[Given] no orgId [Then] omits x-org-id (login-time behavior is unchanged)', async () => {
38 + const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(emptyOkResponse());
39 +
40 + await runIgnoringDecode({
41 + baseURL: 'https://backend.composio.dev',
42 + userApiKey: 'uak_test',
43 + });
44 +
45 + const [, init] = fetchSpy.mock.calls[0]!;
46 + const headers = new Headers((init as RequestInit).headers);
47 + expect(headers.get('x-user-api-key')).toBe('uak_test');
48 + expect(headers.has('x-org-id')).toBe(false);
49 + });
50 +});