@devries-cameron20 / composio-1 / commits / 2208ed654
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): tie analytics identity to the active credential; harden logout + alias

Review follow-ups on the direct-to-PostHog telemetry. - Cross-user attribution (the serious one). getDistinctId trusted the persisted apollo_user_id regardless of which credential was actually in use, while getUserApiKey gives COMPOSIO_USER_API_KEY precedence over the stored config. A script running under user B's env key therefore emitted events attributed to whoever last logged in — and a backfilling command under B's key would overwrite the stored id, flipping later events the other way. The pre-rewrite code derived identity from the key itself and did not have this problem. Now a fingerprint of the resolving credential is persisted alongside the id, and the id is only used while that fingerprint still matches; otherwise we fall back to the anonymous install_id. Unsure attribution is anonymous rather than wrong. - clearApolloIdentityForAnalytics ignored the opt-out and called getOrCreateInstallId, so `composio logout` with telemetry disabled created ~/.composio/analytics.json and minted an install id — contradicting the invariant asserted elsewhere in these tests. It now reads state and does nothing when there is nothing to clear. - An install is aliased at most once, ever. Logging in as a second user on the same device no longer emits a second $create_alias for an install PostHog has already identified. - Dropped the now-unused readApolloUserId. Tests: stale-identity fallback under a differing env key, logout-while-disabled creating no file, and no re-alias across logout + login as a different user. The post-login test now establishes identity through the public API rather than hand-writing state, so it exercises the credential binding. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Kshitij Jhunjhunwala committed Jul 23, 2026 at 16:47 UTC 2208ed65415f1f8233f43282d4ff072e9d5e5fbc
2 files changed +107 -22
ts/packages/cli/src/analytics/dispatch.ts
+40 -12
@@ -175,6 +175,10 @@ type AnalyticsState = {
175 // Tracked separately from apollo_user_id so an alias that was never enqueued
176 // (no project key, telemetry off) is retried instead of blocked forever.
177 readonly aliased_apollo_user_id?: string;
178 + // Fingerprint of the credential that apollo_user_id was resolved from. The id
179 + // is only trusted while the active credential still matches, so an env-var key
180 + // override can never attribute one user's events to another.
181 + readonly api_key_fingerprint?: string;
182 readonly created_at?: string;
183 };
184
@@ -209,12 +213,6 @@ const getOrCreateInstallId = Effect.gen(function* () {
213 return installId;
214 }).pipe(Effect.catchAll(() => makeInstallId));
215
212 -const readApolloUserId = Effect.map(readAnalyticsState, state =>
213 - typeof state?.apollo_user_id === 'string' && state.apollo_user_id.length > 0
214 - ? state.apollo_user_id
215 - : null
216 -);
217 -
216 const mergeAnalyticsState = (installId: string, patch: Partial<AnalyticsState>) =>
217 Effect.gen(function* () {
218 const fs = yield* FileSystem.FileSystem;
@@ -253,8 +251,26 @@ const getUserApiKey = Effect.gen(function* () {
251 : null;
252 });
253
254 +const fingerprintApiKey = (apiKey: string): string =>
255 + `${djb2Hash(apiKey).toString(36)}.${apiKey.length.toString(36)}`;
256 +
257 +const getApiKeyFingerprint = Effect.map(getUserApiKey, apiKey =>
258 + apiKey ? fingerprintApiKey(apiKey) : null
259 +);
260 +
261 const getDistinctId = (installId: string) =>
257 - Effect.map(readApolloUserId, apolloUserId => apolloUserId ?? installId);
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;
267 + }
268 +
269 + const fingerprint = yield* getApiKeyFingerprint;
270 + return fingerprint !== null && fingerprint === state?.api_key_fingerprint
271 + ? apolloUserId
272 + : installId;
273 + });
274
275 const cwdHash = (cwd: string): string => djb2Hash(cwd).toString(36);
276
@@ -584,11 +600,17 @@ export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
600
601 const state = yield* readAnalyticsState;
602 const installId = yield* getOrCreateInstallId;
587 - if (state?.apollo_user_id !== resolved) {
588 - yield* mergeAnalyticsState(installId, { apollo_user_id: resolved });
603 + const fingerprint = yield* getApiKeyFingerprint;
604 + if (state?.apollo_user_id !== resolved || state?.api_key_fingerprint !== fingerprint) {
605 + yield* mergeAnalyticsState(installId, {
606 + apollo_user_id: resolved,
607 + ...(fingerprint === null ? {} : { api_key_fingerprint: fingerprint }),
608 + });
609 }
610
591 - if (state?.aliased_apollo_user_id === resolved) {
611 + // PostHog refuses to merge an install that is already identified, so alias
612 + // an install at most once — a second user on this device stays unaliased.
613 + if (typeof state?.aliased_apollo_user_id === 'string') {
614 return;
615 }
616 if (yield* emitPostHogAlias(installId, resolved)) {
@@ -598,8 +620,14 @@ export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
620
621 /** Drops the Apollo identity on logout so later events revert to `install_id`. */
622 export const clearApolloIdentityForAnalytics = Effect.gen(function* () {
601 - const installId = yield* getOrCreateInstallId;
602 - yield* mergeAnalyticsState(installId, { apollo_user_id: undefined });
623 + const state = yield* readAnalyticsState;
624 + if (!state?.install_id || !state.apollo_user_id) {
625 + return;
626 + }
627 + yield* mergeAnalyticsState(state.install_id, {
628 + apollo_user_id: undefined,
629 + api_key_fingerprint: undefined,
630 + });
631 }).pipe(Effect.catchAllCause(() => Effect.void));
632
633 export const trackCliCodactFailureEffect = (failure: CliCodactFailure) =>
ts/packages/cli/test/src/analytics.dispatch.test.ts
+67 -10
@@ -427,27 +427,24 @@ describe('CLI analytics dispatch', () => {
427 it.effect('keys post-login events on the persisted apollo_user_id', () => {
428 const home = tempy.temporaryDirectory();
429 const scriptPath = `${home}/composio.ts`;
430 - enableTelemetry('');
430 + enableTelemetry('uak_persisted');
431 process.argv[1] = scriptPath;
432
433 return Effect.gen(function* () {
434 const fs = yield* FileSystem.FileSystem;
435 - const path = yield* Path.Path;
435 yield* fs.writeFileString(scriptPath, '');
437 - const composioDir = path.join(home, '.composio');
438 - yield* fs.makeDirectory(composioDir, { recursive: true });
439 - yield* fs.writeFileString(
440 - path.join(composioDir, 'analytics.json'),
441 - JSON.stringify({ install_id: 'install_persisted', apollo_user_id: 'om_apollo_123' })
442 - );
436 +
437 + // Establish the identity under this credential; the id is only trusted
438 + // while that same credential is active.
439 + yield* linkApolloIdentityForAnalytics('om_apollo_123');
440 + childProcessMocks.spawn.mockClear();
441
442 yield* trackCliEventEffect({ name: CLI_ANALYTICS_EVENTS.CLI_EXECUTE_SUCCEEDED });
443
444 expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
445 const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
448 - const payload = decodeWorkerPayload<{ distinctId: string; installId: string }>(args[2]!);
446 + const payload = decodeWorkerPayload<{ distinctId: string }>(args[2]!);
447 expect(payload.distinctId).toBe('om_apollo_123');
450 - expect(payload.installId).toBe('install_persisted');
448 }).pipe(Effect.provide(makePlatformLayer(home)));
449 });
450
@@ -657,4 +654,64 @@ describe('CLI analytics dispatch', () => {
654 expect(fetchSpy).not.toHaveBeenCalled();
655 });
656 });
657 +
658 + describe('CLI analytics identity safety', () => {
659 + it.effect('does not attribute events to a stale identity when the active key differs', () => {
660 + const home = tempy.temporaryDirectory();
661 + const scriptPath = `${home}/composio.ts`;
662 + enableTelemetry('uak_user_a');
663 + process.argv[1] = scriptPath;
664 +
665 + return Effect.gen(function* () {
666 + const fs = yield* FileSystem.FileSystem;
667 + yield* fs.writeFileString(scriptPath, '');
668 + yield* linkApolloIdentityForAnalytics('om_user_a');
669 + childProcessMocks.spawn.mockClear();
670 +
671 + // A script running under a different credential must not inherit user A.
672 + vi.stubEnv('COMPOSIO_USER_API_KEY', 'uak_user_b');
673 + yield* trackCliEventEffect({ name: 'producer_event' });
674 +
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);
679 + }).pipe(Effect.provide(makePlatformLayer(home)));
680 + });
681 +
682 + it.effect('does not create the identity file when logging out with telemetry disabled', () => {
683 + const home = tempy.temporaryDirectory();
684 + enableTelemetry();
685 + vi.stubEnv('COMPOSIO_DISABLE_TELEMETRY', 'true');
686 +
687 + return Effect.gen(function* () {
688 + yield* clearApolloIdentityForAnalytics;
689 +
690 + const fs = yield* FileSystem.FileSystem;
691 + const path = yield* Path.Path;
692 + const exists = yield* fs.exists(path.join(home, '.composio', 'analytics.json'));
693 + expect(exists).toBe(false);
694 + }).pipe(Effect.provide(makePlatformLayer(home)));
695 + });
696 +
697 + it.effect('never re-aliases one install onto a second Apollo user', () => {
698 + const home = tempy.temporaryDirectory();
699 + const scriptPath = `${home}/composio.ts`;
700 + enableTelemetry();
701 + process.argv[1] = scriptPath;
702 +
703 + return Effect.gen(function* () {
704 + const fs = yield* FileSystem.FileSystem;
705 + yield* fs.writeFileString(scriptPath, '');
706 +
707 + yield* linkApolloIdentityForAnalytics('om_first_user');
708 + expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
709 +
710 + // Logout, then log in as a different user on the same device.
711 + yield* clearApolloIdentityForAnalytics;
712 + yield* linkApolloIdentityForAnalytics('om_second_user');
713 + expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
714 + }).pipe(Effect.provide(makePlatformLayer(home)));
715 + });
716 + });
717 });