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): never reuse a stored agent identity the agents API rejected
getStoredReadyAgent collapsed every whoami failure into Option.none via Effect.option and fell back to the on-disk identity, so a revoked agent key (HTTP 401/403) still completed headless login. Auth rejections now return no agent; only transport-shaped failures keep the offline fallback. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Kshitij Jhunjhunwala committed
Jul 28, 2026 at 13:33 UTC
0c6b09df195f59abecb9c36d3b26bdcc8dcaba5b
2 files changed
+60
-4
ts/packages/cli/src/services/agents.ts
+14
-4
@@ -1,5 +1,5 @@
1
import { FileSystem, Path } from '@effect/platform';
2
-import { Data, Effect, Option, Predicate, Schema } from 'effect';
2
+import { Data, Effect, Either, 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';
@@ -365,6 +365,9 @@ export const resolveStoredAgentKey = Effect.gen(function* () {
365
return agentKey;
366
});
367
368
+const isAgentKeyRejection = (error: AgentRequestError | AgentResponseDecodeError): boolean =>
369
+ error instanceof AgentRequestError && (error.status === 401 || error.status === 403);
370
+
371
/**
372
* Reuse-only counterpart of {@link getOrSignupReadyAgent}: refreshes and
373
* returns a stored READY identity but never signs up a new agent.
@@ -376,9 +379,16 @@ export const getStoredReadyAgent = Effect.gen(function* () {
379
const agentKey = getAgentKey(stored.value);
380
if (!agentKey) return Option.none<AgentIdentity>();
381
379
- const remote = yield* fetchAgentWhoami(agentKey).pipe(Effect.option);
380
- const identity = Option.isSome(remote)
381
- ? yield* writeStoredAgentIdentity(remote.value)
382
+ const remote = yield* fetchAgentWhoami(agentKey).pipe(Effect.either);
383
+ // An auth rejection means the API examined and refused this key — the stored
384
+ // identity is revoked, not unreachable. Only transport-shaped failures may
385
+ // fall back to the on-disk identity.
386
+ if (Either.isLeft(remote) && isAgentKeyRejection(remote.left)) {
387
+ return Option.none<AgentIdentity>();
388
+ }
389
+
390
+ const identity = Either.isRight(remote)
391
+ ? yield* writeStoredAgentIdentity(remote.right)
392
: stored.value;
393
394
const ready =
ts/packages/cli/test/src/commands/login.cmd.test.ts
+46
@@ -182,6 +182,52 @@ describe('CLI: composio login', () => {
182
);
183
});
184
185
+ layer(TestLive({ terminalUI: headlessStdinUI }))(it => {
186
+ it.scoped(
187
+ '[Given] a stored READY agent identity the API rejects [When] login runs headlessly [Then] does not reuse the revoked identity',
188
+ () =>
189
+ Effect.gen(function* () {
190
+ yield* writeStoredAgentIdentity(storedAgentIdentity);
191
+ vi.spyOn(globalThis, 'fetch').mockImplementation(async requestInput =>
192
+ requestUrl(requestInput).includes('/api/whoami')
193
+ ? mockFetchResponse({ message: 'Invalid agent key' }, 401)
194
+ : mockFetchResponse({})
195
+ );
196
+
197
+ yield* cli(['login']);
198
+
199
+ const ctx = yield* ComposioUserContext;
200
+ expect(Option.getOrUndefined(ctx.data.apiKey)).toBeUndefined();
201
+
202
+ const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
203
+ expect(output).not.toContain('"logged_in":true');
204
+ expect(output).toContain('Open this URL in your browser to log in:');
205
+ })
206
+ );
207
+ });
208
+
209
+ layer(TestLive({ terminalUI: headlessStdinUI }))(it => {
210
+ it.scoped(
211
+ '[Given] a stored READY agent identity and an unreachable agents API [When] login runs headlessly [Then] still reuses the on-disk identity',
212
+ () =>
213
+ Effect.gen(function* () {
214
+ yield* writeStoredAgentIdentity(storedAgentIdentity);
215
+ vi.spyOn(globalThis, 'fetch').mockImplementation(async requestInput => {
216
+ if (requestUrl(requestInput).includes('/api/whoami')) {
217
+ throw new Error('network unreachable');
218
+ }
219
+ return mockFetchResponse({});
220
+ });
221
+
222
+ yield* cli(['login']);
223
+
224
+ const ctx = yield* ComposioUserContext;
225
+ expect(Option.getOrUndefined(ctx.data.apiKey)).toBe('uak_agent');
226
+ expect(Option.getOrUndefined(ctx.data.orgId)).toBe('org_agent');
227
+ })
228
+ );
229
+ });
230
+
231
layer(TestLive({ terminalUI: headlessStdinUI }))(it => {
232
it.scoped(
233
'[Given] a stored PENDING agent identity [When] login runs headlessly [Then] prints instructions without logging in',