@devries-cameron20 / composio-1 / commits / c22c7b325
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): never reuse the aliased install_id as an anonymous identity

Once $create_alias fires, PostHog permanently merges install_id into the Apollo person. Falling back to that same install_id on logout or on a credential fingerprint mismatch therefore did NOT anonymise anything — person-level insights still resolved those events to the previous user. The guard added in b9740b8c changed the mechanism of the cross-user attribution bug rather than removing it. getDistinctId now returns: - the bare install_id only while the install has never been aliased, so the login alias still merges the pre-login funnel correctly; - user_<credential fingerprint> when a credential is active but does not match the stored identity — pseudonymous per credential, never merged; - anon_<install_id> when signed out after an alias. Verified live against a throwaway PostHog project: the bare install_id and the Apollo id both resolve to person 42ff3120, while post-logout events under anon_<install_id> resolve to a different person entirely. The earlier trace had shown both ids collapsing to one person; I read that as the merge working and missed that it also meant the logout fallback attributed to the logged-out user. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Kshitij Jhunjhunwala committed Jul 23, 2026 at 17:33 UTC c22c7b325bdfc87907a993977bf41d17a9a80c26
2 files changed +48 -7
ts/packages/cli/src/analytics/dispatch.ts
+17 -6
@@ -262,14 +262,25 @@ const getDistinctId = (installId: string) =>
262 Effect.gen(function* () {
263 const state = yield* readAnalyticsState;
264 const apolloUserId = state?.apollo_user_id;
265 - if (typeof apolloUserId !== 'string' || apolloUserId.length === 0) {
266 - return installId;
265 + const fingerprint = yield* getApiKeyFingerprint;
266 +
267 + if (
268 + typeof apolloUserId === 'string' &&
269 + apolloUserId.length > 0 &&
270 + fingerprint !== null &&
271 + fingerprint === state?.api_key_fingerprint
272 + ) {
273 + return apolloUserId;
274 }
275
269 - const fingerprint = yield* getApiKeyFingerprint;
270 - return fingerprint !== null && fingerprint === state?.api_key_fingerprint
271 - ? apolloUserId
272 - : installId;
276 + // Once the install has been aliased, PostHog has permanently merged the bare
277 + // install_id into that Apollo person — reusing it here would attribute these
278 + // events to the previous user rather than to nobody. Fall back to an id that
279 + // was never merged: pseudonymous per credential, or per device when signed out.
280 + if (typeof state?.aliased_apollo_user_id !== 'string') {
281 + return installId;
282 + }
283 + return fingerprint !== null ? `user_${fingerprint}` : `anon_${installId}`;
284 });
285
286 const cwdHash = (cwd: string): string => djb2Hash(cwd).toString(36);
ts/packages/cli/test/src/analytics.dispatch.test.ts
+31 -1
@@ -675,7 +675,10 @@ describe('CLI analytics dispatch', () => {
675 const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
676 const payload = decodeWorkerPayload<{ distinctId: string; installId: string }>(args[2]!);
677 expect(payload.distinctId).not.toBe('om_user_a');
678 - expect(payload.distinctId).toBe(payload.installId);
678 + // The install_id is already merged into user A's PostHog person, so
679 + // reusing it would still attribute to A. Must be a never-merged id.
680 + expect(payload.distinctId).not.toBe(payload.installId);
681 + expect(payload.distinctId.startsWith('user_')).toBe(true);
682 }).pipe(Effect.provide(makePlatformLayer(home)));
683 });
684
@@ -704,6 +707,33 @@ describe('CLI analytics dispatch', () => {
707 }).pipe(Effect.provide(makePlatformLayer(home)));
708 });
709
710 + it.effect('does not reuse the aliased install_id after logout', () => {
711 + const home = tempy.temporaryDirectory();
712 + const scriptPath = `${home}/composio.ts`;
713 + enableTelemetry('uak_logged_in');
714 + process.argv[1] = scriptPath;
715 +
716 + return Effect.gen(function* () {
717 + const fs = yield* FileSystem.FileSystem;
718 + yield* fs.writeFileString(scriptPath, '');
719 +
720 + yield* linkApolloIdentityForAnalytics('om_logged_in', 'uak_logged_in');
721 + yield* clearApolloIdentityForAnalytics;
722 + childProcessMocks.spawn.mockClear();
723 +
724 + // Signed out: no credential at all.
725 + vi.stubEnv('COMPOSIO_USER_API_KEY', '');
726 + yield* trackCliEventEffect({ name: 'producer_event' });
727 +
728 + const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
729 + const payload = decodeWorkerPayload<{ distinctId: string; installId: string }>(args[2]!);
730 + expect(payload.distinctId).not.toBe('om_logged_in');
731 + // Bare install_id now resolves to the logged-out user's person.
732 + expect(payload.distinctId).not.toBe(payload.installId);
733 + expect(payload.distinctId).toBe(`anon_${payload.installId}`);
734 + }).pipe(Effect.provide(makePlatformLayer(home)));
735 + });
736 +
737 it.effect('does not create the identity file when logging out with telemetry disabled', () => {
738 const home = tempy.temporaryDirectory();
739 enableTelemetry();