Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(cli): never reuse a stored agent identity the agents API rejected
Kshitij Jhunjhunwala0c6b09df195f
diff --git a/ts/packages/cli/src/services/agents.ts b/ts/packages/cli/src/services/agents.tsindex 2d76c0262..737b1de5f 100644--- a/ts/packages/cli/src/services/agents.ts+++ b/ts/packages/cli/src/services/agents.ts@@ -1,5 +1,5 @@import { FileSystem, Path } from '@effect/platform';-import { Data, Effect, Option, Predicate, Schema } from 'effect';+import { Data, Effect, Either, Option, Predicate, Schema } from 'effect';import { JsonRecordSchema } from 'src/effects/json';import { setupCacheDir } from 'src/effects/setup-cache-dir';import { ComposioUserContext } from 'src/services/user-context';@@ -365,6 +365,9 @@ export const resolveStoredAgentKey = Effect.gen(function* () {return agentKey;});+const isAgentKeyRejection = (error: AgentRequestError | AgentResponseDecodeError): boolean =>+ error instanceof AgentRequestError && (error.status === 401 || error.status === 403);+/*** Reuse-only counterpart of {@link getOrSignupReadyAgent}: refreshes and* returns a stored READY identity but never signs up a new agent.@@ -376,9 +379,16 @@ export const getStoredReadyAgent = Effect.gen(function* () {const agentKey = getAgentKey(stored.value);if (!agentKey) return Option.none<AgentIdentity>();- const remote = yield* fetchAgentWhoami(agentKey).pipe(Effect.option);- const identity = Option.isSome(remote)- ? yield* writeStoredAgentIdentity(remote.value)+ const remote = yield* fetchAgentWhoami(agentKey).pipe(Effect.either);+ // An auth rejection means the API examined and refused this key — the stored+ // identity is revoked, not unreachable. Only transport-shaped failures may+ // fall back to the on-disk identity.+ if (Either.isLeft(remote) && isAgentKeyRejection(remote.left)) {+ return Option.none<AgentIdentity>();+ }++ const identity = Either.isRight(remote)+ ? yield* writeStoredAgentIdentity(remote.right): stored.value;const ready =diff --git a/ts/packages/cli/test/src/commands/login.cmd.test.ts b/ts/packages/cli/test/src/commands/login.cmd.test.tsindex 9250ab687..433f74ee7 100644--- a/ts/packages/cli/test/src/commands/login.cmd.test.ts+++ b/ts/packages/cli/test/src/commands/login.cmd.test.ts@@ -182,6 +182,52 @@ describe('CLI: composio login', () => {);});+ layer(TestLive({ terminalUI: headlessStdinUI }))(it => {+ it.scoped(+ '[Given] a stored READY agent identity the API rejects [When] login runs headlessly [Then] does not reuse the revoked identity',+ () =>+ Effect.gen(function* () {+ yield* writeStoredAgentIdentity(storedAgentIdentity);+ vi.spyOn(globalThis, 'fetch').mockImplementation(async requestInput =>+ requestUrl(requestInput).includes('/api/whoami')+ ? mockFetchResponse({ message: 'Invalid agent key' }, 401)+ : mockFetchResponse({})+ );++ yield* cli(['login']);++ const ctx = yield* ComposioUserContext;+ expect(Option.getOrUndefined(ctx.data.apiKey)).toBeUndefined();++ const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');+ expect(output).not.toContain('"logged_in":true');+ expect(output).toContain('Open this URL in your browser to log in:');+ })+ );+ });++ layer(TestLive({ terminalUI: headlessStdinUI }))(it => {+ it.scoped(+ '[Given] a stored READY agent identity and an unreachable agents API [When] login runs headlessly [Then] still reuses the on-disk identity',+ () =>+ Effect.gen(function* () {+ yield* writeStoredAgentIdentity(storedAgentIdentity);+ vi.spyOn(globalThis, 'fetch').mockImplementation(async requestInput => {+ if (requestUrl(requestInput).includes('/api/whoami')) {+ throw new Error('network unreachable');+ }+ return mockFetchResponse({});+ });++ yield* cli(['login']);++ const ctx = yield* ComposioUserContext;+ expect(Option.getOrUndefined(ctx.data.apiKey)).toBe('uak_agent');+ expect(Option.getOrUndefined(ctx.data.orgId)).toBe('org_agent');+ })+ );+ });+layer(TestLive({ terminalUI: headlessStdinUI }))(it => {it.scoped('[Given] a stored PENDING agent identity [When] login runs headlessly [Then] prints instructions without logging in',