Skip to content

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): bind analytics identity to the caller's credential; propagate spawn failure

Kshitij Jhunjhunwalab9740b8c3008
diff --git a/ts/packages/cli/src/analytics/dispatch.ts b/ts/packages/cli/src/analytics/dispatch.ts
index d0f387c2d..56a59395a 100644
--- a/ts/packages/cli/src/analytics/dispatch.ts
+++ b/ts/packages/cli/src/analytics/dispatch.ts
@@ -357,8 +357,9 @@ const getWorkerSpawnArgs = (workerFlag: string, encodedPayload: string) =>
const spawnWorker = (command: string, args: ReadonlyArray<string>) =>
Effect.gen(function* () {
const debugEnabled = yield* telemetryDebugEnabled;
- yield* spawnDetached(command, args, { inheritStderr: debugEnabled }).pipe(
- Effect.catchTag('services/DetachedProcessSpawnError', () => Effect.void)
+ return yield* spawnDetached(command, args, { inheritStderr: debugEnabled }).pipe(
+ Effect.as(true),
+ Effect.catchTag('services/DetachedProcessSpawnError', () => Effect.succeed(false))
);
});
@@ -519,8 +520,7 @@ const enqueuePostHogEnvelope = (envelope: AnalyticsEnvelope) =>
INTERNAL_ANALYTICS_WORKER_FLAG,
encodedPayload
);
- yield* spawnWorker(command, args);
- return true;
+ return yield* spawnWorker(command, args);
});
export const trackCliEventEffect = (event: TrackEvent) =>
@@ -585,7 +585,7 @@ export const emitPostHogAlias = (installId: string, apolloUserId: string) =>
* anonymous install into it. Safe to call from any command that already resolved
* a session, which is how already-authenticated installs get backfilled.
*/
-export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
+export const linkApolloIdentityForAnalytics = (apolloUserId: string, apiKey?: string) =>
Effect.gen(function* () {
const resolved = typeof apolloUserId === 'string' ? apolloUserId.trim() : '';
if (resolved.length === 0) {
@@ -600,7 +600,13 @@ export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
const state = yield* readAnalyticsState;
const installId = yield* getOrCreateInstallId;
- const fingerprint = yield* getApiKeyFingerprint;
+ // Callers pass the credential they just validated: login resolves identity
+ // before persisting it, so reading it back here would fingerprint the
+ // previous key and never match.
+ const fingerprint =
+ typeof apiKey === 'string' && apiKey.length > 0
+ ? fingerprintApiKey(apiKey)
+ : yield* getApiKeyFingerprint;
if (state?.apollo_user_id !== resolved || state?.api_key_fingerprint !== fingerprint) {
yield* mergeAnalyticsState(installId, {
apollo_user_id: resolved,
diff --git a/ts/packages/cli/src/commands/connected-accounts/commands/connected-accounts.link.cmd.ts b/ts/packages/cli/src/commands/connected-accounts/commands/connected-accounts.link.cmd.ts
index 732326e4e..df8767bcc 100644
--- a/ts/packages/cli/src/commands/connected-accounts/commands/connected-accounts.link.cmd.ts
+++ b/ts/packages/cli/src/commands/connected-accounts/commands/connected-accounts.link.cmd.ts
@@ -242,7 +242,7 @@ const handleNoManagedAuth = (ui: TerminalUI, toolkitSlug: string, noBrowser: boo
}).pipe(Effect.catchAll(() => Effect.succeed(null)));
if (sessionInfo) {
- yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id);
+ yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id, apiKey);
}
if (sessionInfo?.project.org.name) {
diff --git a/ts/packages/cli/src/commands/init.cmd.ts b/ts/packages/cli/src/commands/init.cmd.ts
index bd51c40b7..0febc7d9a 100644
--- a/ts/packages/cli/src/commands/init.cmd.ts
+++ b/ts/packages/cli/src/commands/init.cmd.ts
@@ -126,7 +126,7 @@ const ensureProjectApiKeyInEnv = (params: { cwd: string; selected: ProjectKeys }
orgId: selected.orgId,
projectId: selected.projectId,
});
- yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id);
+ yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id, uakApiKey);
let projectApiKey = sessionInfo.api_key?.api_key ?? sessionInfo.api_key?.key ?? null;
if (!projectApiKey && !hasProjectApiKey) {
diff --git a/ts/packages/cli/src/commands/login.cmd.ts b/ts/packages/cli/src/commands/login.cmd.ts
index ed1bafb86..79ba3e92a 100644
--- a/ts/packages/cli/src/commands/login.cmd.ts
+++ b/ts/packages/cli/src/commands/login.cmd.ts
@@ -324,7 +324,7 @@ const directLogin = (params: { userApiKey: string; org?: string }) =>
userApiKey: params.userApiKey,
});
- yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id);
+ yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id, params.userApiKey);
const selectedOrg = yield* resolveDirectLoginOrganization({
apiKey: params.userApiKey,
@@ -509,7 +509,7 @@ const loginWithKey = (params: {
userApiKey: uakApiKey,
});
- yield* linkApolloIdentityForAnalytics(uakSessionInfo.org_member.id);
+ yield* linkApolloIdentityForAnalytics(uakSessionInfo.org_member.id, uakApiKey);
const organizations = params.defaultToFirstOrg
? yield* listOrganizations({
@@ -717,7 +717,7 @@ export const browserLogin = (params: {
userApiKey: uakApiKey,
});
- yield* linkApolloIdentityForAnalytics(uakSessionInfo.org_member.id);
+ yield* linkApolloIdentityForAnalytics(uakSessionInfo.org_member.id, uakApiKey);
// e.g., "pr_xlSR6oN5jIlk"
const xProjectId = uakSessionInfo.project.nano_id;
@@ -899,7 +899,10 @@ export const loginCmd = Command.make(
orgId: identity.composio?.org_id ?? undefined,
}).pipe(Effect.catchAll(() => Effect.succeed(null)));
if (agentSessionInfo) {
- yield* linkApolloIdentityForAnalytics(agentSessionInfo.org_member.id);
+ yield* linkApolloIdentityForAnalytics(
+ agentSessionInfo.org_member.id,
+ agentUserApiKey
+ );
}
}
const summary = safeAgentSummary(identity);
diff --git a/ts/packages/cli/src/commands/whoami.cmd.ts b/ts/packages/cli/src/commands/whoami.cmd.ts
index 6a7f72d97..2a7d3f3bb 100644
--- a/ts/packages/cli/src/commands/whoami.cmd.ts
+++ b/ts/packages/cli/src/commands/whoami.cmd.ts
@@ -37,7 +37,7 @@ export const whoamiCmd = Command.make('whoami', {}).pipe(
}).pipe(Effect.option);
yield* Option.match(sessionInfo, {
onNone: () => Effect.void,
- onSome: info => linkApolloIdentityForAnalytics(info.org_member.id),
+ onSome: info => linkApolloIdentityForAnalytics(info.org_member.id, apiKey),
});
const email = Option.map(sessionInfo, info => info.org_member.email).pipe(
Option.getOrUndefined
diff --git a/ts/packages/cli/test/src/analytics.dispatch.test.ts b/ts/packages/cli/test/src/analytics.dispatch.test.ts
index b1b148458..7ad259d17 100644
--- a/ts/packages/cli/test/src/analytics.dispatch.test.ts
+++ b/ts/packages/cli/test/src/analytics.dispatch.test.ts
@@ -679,6 +679,31 @@ describe('CLI analytics dispatch', () => {
}).pipe(Effect.provide(makePlatformLayer(home)));
});
+ it.effect('binds identity to the credential the caller passes, not one read back', () => {
+ const home = tempy.temporaryDirectory();
+ const scriptPath = `${home}/composio.ts`;
+ // Mirrors real login: identity is resolved BEFORE the credential is persisted,
+ // so nothing readable from disk/env identifies the user yet.
+ enableTelemetry('');
+ process.argv[1] = scriptPath;
+
+ return Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ yield* fs.writeFileString(scriptPath, '');
+
+ yield* linkApolloIdentityForAnalytics('om_login_user', 'uak_fresh');
+ childProcessMocks.spawn.mockClear();
+
+ // The credential is active from here on.
+ vi.stubEnv('COMPOSIO_USER_API_KEY', 'uak_fresh');
+ yield* trackCliEventEffect({ name: 'producer_event' });
+
+ const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
+ const payload = decodeWorkerPayload<{ distinctId: string }>(args[2]!);
+ expect(payload.distinctId).toBe('om_login_user');
+ }).pipe(Effect.provide(makePlatformLayer(home)));
+ });
+
it.effect('does not create the identity file when logging out with telemetry disabled', () => {
const home = tempy.temporaryDirectory();
enableTelemetry();