@devries-cameron20 / composio-1 / commits / 64733c3cb
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): treat empty stored api keys and list shape skew as failures (#5029)

This PR: - fixes the two behavior findings from the post-merge audits of #4994 and #4995 (both were merged before their reviews finished, so these land as follow-ups on `next`) - `orgs switch` now fails with `commands/NotLoggedInError` (exit 1, nothing written) when the stored `api_key` is `""` — `requireLogin` counts an empty key as logged in (`Option.isSome`), so the handler's `Effect.fromOption(ctx.data.apiKey, notLoggedIn)` let the command proceed where the pre-move handler warned and changed nothing; `Option.filter` rejects the empty key before any work happens - `dev connected-accounts list` now fails with `commands/ConnectedAccountsListRequestError` when the decode fallback returns a payload whose `items` is not an array — the `Arr.match` rewrite had turned that case into a silent "No connected accounts found." + exit 0 (the pre-move code crashed on `.map` instead), so a piped consumer could read "zero accounts" while data was present - restores the three `@example` JSDoc blocks the #4994 move dropped from the `connected-accounts` cmd files - adds regression tests: a stored-empty-key cmd test asserting `NotLoggedInError` and an untouched `user_data.json`, and a raw shape-skew handler test asserting the failure instead of an empty listing ## Context Found by the post-merge reviews of the CLI command-layout series: #4995's empty-key divergence (P2, validator-confirmed under `authorization-authentication`) and #4994's decode-fallback shape-skew drift (P3) plus its dropped JSDoc (P3). Validation narrowed the empty-key trigger: blank `COMPOSIO_USER_API_KEY` env values are trimmed upstream (`src/config/env.ts`), so the surviving precondition is an empty key already in `user_data.json` or the OS keyring. The empty-key failure intentionally matches the missing-key behavior (`NotLoggedInError`, exit 1) rather than the pre-move warn + exit 0, per the series' typed-failure convention; the deeper alternative (tightening `isLoggedIn` globally) was deliberately not taken. ## Verification `pnpm --filter @composio/cli test` (204 files, 2262 passed, 1 skipped; includes the two new regression tests), `pnpm --filter @composio/cli run lint:effect` (0 diagnostics), `validate:command-tests` (2 exemptions, unchanged), root `pnpm typecheck`, `pnpm lint` (no warnings in touched files), prettier clean.

Alberto Schiabel committed Oct 21, 2026 at 19:27 UTC 64733c3cbd4b3936fe1ae6bb20f41380e255f942
8 files changed +110 -11
ts/packages/cli/src/commands/connected-accounts/info/connected-accounts.info.cmd.ts
+8
@@ -6,6 +6,14 @@ const id = Argument.String('id').pipe(
6 Argument.optional
7 );
8
9 +/**
10 + * View details of a specific connected account.
11 + *
12 + * @example
13 + * ```bash
14 + * composio dev connected-accounts info "con_1a2b3c4d5e6f"
15 + * ```
16 + */
17 export const connectedAccountsCmd$Info = Command.make(
18 'info',
19 { id },
ts/packages/cli/src/commands/connected-accounts/list/connected-accounts.list.cmd.ts
+10
@@ -21,6 +21,16 @@ const limit = Flag.Int('limit').pipe(
21 Flag.withDescription('Number of results per page (1-1000)')
22 );
23
24 +/**
25 + * List connected accounts with optional filters.
26 + *
27 + * @example
28 + * ```bash
29 + * composio dev connected-accounts list
30 + * composio dev connected-accounts list --toolkits "gmail"
31 + * composio dev connected-accounts list --user-id "default" --status ACTIVE
32 + * ```
33 + */
34 export const connectedAccountsCmd$List = Command.make(
35 'list',
36 { toolkits, status, limit },
ts/packages/cli/src/commands/connected-accounts/list/connected-accounts.list.format.ts
+4
@@ -8,6 +8,10 @@ import { formatConnectedAccountsTable } from '../format';
8 export const FETCHING_CONNECTED_ACCOUNTS_MESSAGE = 'Fetching connected accounts...';
9 export const LIST_CONNECTED_ACCOUNTS_FAILED_MESSAGE = 'Failed to list connected accounts.';
10
11 +/** The failure when the response shape drifted so far that `items` is not a list. */
12 +export const CONNECTED_ACCOUNTS_LIST_SHAPE_MESSAGE =
13 + 'Connected accounts response was not the shape this CLI understands; run `composio upgrade`.';
14 +
15 /**
16 * The warning for an empty page, naming the first filter that was given: `--toolkits`, then
17 * `--user-id`, then `--status`.
ts/packages/cli/src/commands/connected-accounts/list/connected-accounts.list.handler.ts
+17 -5
@@ -9,6 +9,7 @@ import { TerminalUI } from 'src/services/terminal-ui';
9 import { clampLimit } from 'src/ui/clamp-limit';
10 import { ConnectedAccountsListRequestError } from './connected-accounts.list.errors';
11 import {
12 + CONNECTED_ACCOUNTS_LIST_SHAPE_MESSAGE,
13 FETCHING_CONNECTED_ACCOUNTS_MESSAGE,
14 LIST_CONNECTED_ACCOUNTS_FAILED_MESSAGE,
15 formatConnectedAccountsJson,
@@ -61,17 +62,28 @@ export const runConnectedAccountsList = ({
62 .pipe(failedWith(ConnectedAccountsListRequestError, LIST_CONNECTED_ACCOUNTS_FAILED_MESSAGE))
63 );
64 const result = yield* decodeConnectedAccountListWithFallback(rawResult);
65 + // The decode fallback returns the raw payload when the response shape drifts; `items`
66 + // may then not be a list at all. Fail loudly instead of reporting an empty page,
67 + // which would tell a piped consumer there are no connected accounts.
68 + const items = Array.isArray(result.items)
69 + ? result.items
70 + : yield* Effect.fail(
71 + new ConnectedAccountsListRequestError({
72 + message: CONNECTED_ACCOUNTS_LIST_SHAPE_MESSAGE,
73 + cause: `response.items was ${typeof result.items}, expected an array`,
74 + })
75 + );
76
65 - yield* Arr.match(result.items, {
77 + yield* Arr.match(items, {
78 onEmpty: () => ui.log.warn(noConnectedAccountsMessage({ toolkits, userId, status })),
67 - onNonEmpty: items =>
79 + onNonEmpty: nonEmptyItems =>
80 Effect.gen(function* () {
69 - yield* ui.log.info(listingMessage(items, result.total_items));
70 - const firstId = Arr.headNonEmpty(items).id;
81 + yield* ui.log.info(listingMessage(nonEmptyItems, result.total_items));
82 + const firstId = Arr.headNonEmpty(nonEmptyItems).id;
83 if (firstId) {
84 yield* ui.log.step(viewDetailsHint(firstId));
85 }
74 - yield* ui.output(formatConnectedAccountsJson(items));
86 + yield* ui.output(formatConnectedAccountsJson(nonEmptyItems));
87 }),
88 });
89 });
ts/packages/cli/src/commands/connected-accounts/whoami/connected-accounts.whoami.cmd.ts
+12
@@ -6,6 +6,18 @@ const id = Argument.String('id').pipe(
6 Argument.optional
7 );
8
9 +/**
10 + * Show the external account profile for a connected account.
11 + *
12 + * Retrieves the connected account details and displays a summary
13 + * of who is connected (toolkit, user, status). Warns when the
14 + * connection is in a non-ACTIVE state.
15 + *
16 + * @example
17 + * ```bash
18 + * composio dev connected-accounts whoami "con_1a2b3c4d5e6f"
19 + * ```
20 + */
21 export const connectedAccountsCmd$Whoami = Command.make(
22 'whoami',
23 { id },
ts/packages/cli/src/commands/orgs/switch/orgs.switch.handler.ts
+6 -2
@@ -28,8 +28,12 @@ export const runOrgsSwitch = ({ orgId, limit }: OrgsSwitchParams) =>
28 Effect.gen(function* () {
29 const ui = yield* TerminalUI;
30 const ctx = yield* ComposioUserContext;
31 - // `requireLogin` on the `orgs` group has already proven the key is there.
32 - const apiKey = yield* Effect.fromOption(ctx.data.apiKey, notLoggedIn);
31 + // `requireLogin` on the `orgs` group has already proven the key is there; the
32 + // filter also fails a stored empty key, which `requireLogin` counts as present.
33 + const apiKey = yield* Effect.fromOption(
34 + Option.filter(ctx.data.apiKey, key => key.length > 0),
35 + notLoggedIn
36 + );
37
38 yield* ui.intro(ORGS_SWITCH_INTRO);
39 yield* ui.note(CURRENT_ORG_NOTE, CURRENT_ORG_NOTE_TITLE);
ts/packages/cli/test/src/commands/connected-accounts/list/connected-accounts.list.handler.test.ts
+32 -4
@@ -6,7 +6,10 @@ import {
6 runConnectedAccountsList,
7 type ConnectedAccountsListParams,
8 } from 'src/commands/connected-accounts/list/connected-accounts.list.handler';
9 -import { viewDetailsHint } from 'src/commands/connected-accounts/list/connected-accounts.list.format';
9 +import {
10 + CONNECTED_ACCOUNTS_LIST_SHAPE_MESSAGE,
11 + viewDetailsHint,
12 +} from 'src/commands/connected-accounts/list/connected-accounts.list.format';
13 import { CommandSurface } from 'src/commands/surface';
14 import type { ConnectedAccountItem } from 'src/models/connected-accounts';
15 import type { ProjectKeys } from 'src/models/project-keys';
@@ -80,6 +83,8 @@ interface ListFixture {
83 readonly accounts?: ReadonlyArray<ConnectedAccountItem>;
84 readonly userId?: Option.Option<string>;
85 readonly projectName?: Option.Option<string>;
86 + /** Served raw by `connectedAccounts.list`, bypassing the well-formed mock. */
87 + readonly rawListResponse?: unknown;
88 }
89
90 /**
@@ -101,9 +106,14 @@ const listTest = (fixture: ListFixture = {}) => {
106 connectedAccounts: {
107 ...resources.connectedAccounts,
108 list: (params: Parameters<typeof resources.connectedAccounts.list>[0]) =>
104 - Effect.sync(() => requests.push({ scope, params })).pipe(
105 - Effect.andThen(resources.connectedAccounts.list(params))
106 - ),
109 + fixture.rawListResponse !== undefined
110 + ? // Record the scope, then serve the raw payload as-is.
111 + Effect.sync(() => requests.push({ scope, params })).pipe(
112 + Effect.as(fixture.rawListResponse)
113 + )
114 + : Effect.sync(() => requests.push({ scope, params })).pipe(
115 + Effect.andThen(resources.connectedAccounts.list(params))
116 + ),
117 },
118 }) as unknown as ComposioClientShape;
119
@@ -298,4 +308,22 @@ describe('runConnectedAccountsList', () => {
308 }).pipe(Effect.provide(layer));
309 }
310 );
311 +
312 + it.effect(
313 + '[Given] a raw response whose items are not an array [Then] fails instead of listing zero',
314 + () => {
315 + const { layer, outputs } = listTest({
316 + rawListResponse: { items: { pages: 2 }, total_items: 3 },
317 + });
318 + return Effect.gen(function* () {
319 + const error = yield* runConnectedAccountsList(params()).pipe(Effect.flip);
320 +
321 + expect(error).toMatchObject({
322 + _tag: 'commands/ConnectedAccountsListRequestError',
323 + message: CONNECTED_ACCOUNTS_LIST_SHAPE_MESSAGE,
324 + });
325 + expect(outputs).toEqual([]);
326 + }).pipe(Effect.provide(layer));
327 + }
328 + );
329 });
ts/packages/cli/test/src/commands/orgs/switch/orgs.switch.cmd.test.ts
+21
@@ -58,6 +58,27 @@ describe('CLI: composio orgs switch', () => {
58 );
59 });
60
61 + layer(TestLive({ fixture: 'user-config-with-empty-api-key' }))(
62 + '[Given] a stored login whose api key is empty',
63 + it => {
64 + it.effect('[Then] fails with NotLoggedInError and leaves the stored config untouched', () =>
65 + Effect.gen(function* () {
66 + const error = yield* cli(['orgs', 'switch', '--org-id', 'org_selected']).pipe(
67 + Effect.flip
68 + );
69 +
70 + expect(error).toMatchObject({ _tag: 'commands/NotLoggedInError' });
71 + expect(yield* MockConsole.getLines({ stream: 'stdout' })).toEqual([]);
72 + expect(yield* readStoredUserConfig).toEqual({
73 + api_key: '',
74 + org_id: 'org_1',
75 + project_id: 'project_1',
76 + });
77 + })
78 + );
79 + }
80 + );
81 +
82 const testConfigProvider = ConfigProvider.fromEnvRecord({
83 COMPOSIO_USER_API_KEY: 'uak_switch_test',
84 });