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): serialize analytics identity state
Kshitij Jhunjhunwala committed
Jul 29, 2026 at 15:42 UTC
2be90d765fe86b653f2a8b1ccc4df7dd7e3afc3b
2 files changed
+214
-67
ts/packages/cli/src/analytics/dispatch.ts
+147
-67
@@ -1,5 +1,6 @@
1
import process from 'node:process';
2
import { FileSystem, HttpClient, HttpClientRequest, Path } from '@effect/platform';
3
+import type { PlatformError } from '@effect/platform/Error';
4
import {
5
Cause,
6
Clock,
@@ -22,6 +23,9 @@ const INTERNAL_ANALYTICS_WORKER_FLAG = '__analytics-worker';
23
const INTERNAL_CODACT_FAILURE_WORKER_FLAG = '__codact-failure-worker';
24
const COMPOSIO_DIR = '.composio';
25
const ANALYTICS_STATE_FILE_NAME = 'analytics.json';
26
+const ANALYTICS_STATE_LOCK_FILE_NAME = `${ANALYTICS_STATE_FILE_NAME}.lock`;
27
+const ANALYTICS_STATE_LOCK_STALE_MS = 30_000;
28
+const ANALYTICS_STATE_LOCK_MAX_ATTEMPTS = 10_000;
29
const CONSUMER_SHORT_TERM_CACHE_FILE_NAME = 'consumer-short-term-cache.json';
30
const CLI_CODACT_FAILURES_PATH = '/api/v3/cli/codact_failures';
31
@@ -157,6 +161,7 @@ const getAnalyticsPaths = Effect.gen(function* () {
161
return {
162
analyticsDir,
163
analyticsStatePath: path.join(analyticsDir, ANALYTICS_STATE_FILE_NAME),
164
+ analyticsStateLockPath: path.join(analyticsDir, ANALYTICS_STATE_LOCK_FILE_NAME),
165
// user_data.json and config.json live in the cache dir (setup-cache-dir.ts,
166
// cli-user-config.ts), which COMPOSIO_CACHE_DIR relocates away from ~/.composio.
167
userConfigPath: path.join(cacheDir, constants.USER_CONFIG_FILE_NAME),
@@ -188,6 +193,63 @@ const readAnalyticsState = Effect.gen(function* () {
193
return yield* readOptionalJson<AnalyticsState>(paths.analyticsStatePath);
194
});
195
196
+const recoverStaleAnalyticsStateLock = (
197
+ fs: FileSystem.FileSystem,
198
+ lockPath: string
199
+): Effect.Effect<void, never, never> =>
200
+ Effect.gen(function* () {
201
+ const info = yield* fs.stat(lockPath);
202
+ const modifiedAt = Option.getOrUndefined(info.mtime);
203
+ if (!modifiedAt) {
204
+ return;
205
+ }
206
+ const now = yield* Clock.currentTimeMillis;
207
+ if (now - modifiedAt.getTime() <= ANALYTICS_STATE_LOCK_STALE_MS) {
208
+ return;
209
+ }
210
+
211
+ // Rename claims this exact stale lock atomically. A competing process can
212
+ // then acquire the canonical path without being deleted by our cleanup.
213
+ const stalePath = `${lockPath}.stale-${crypto.randomUUID().slice(0, 8)}`;
214
+ yield* fs.rename(lockPath, stalePath);
215
+ yield* fs.remove(stalePath, { force: true });
216
+ }).pipe(Effect.catchAll(() => Effect.void));
217
+
218
+const acquireAnalyticsStateLock = Effect.gen(function* () {
219
+ const fs = yield* FileSystem.FileSystem;
220
+ const paths = yield* getAnalyticsPaths;
221
+ const token = crypto.randomUUID();
222
+
223
+ yield* fs.makeDirectory(paths.analyticsDir, { recursive: true });
224
+
225
+ const tryAcquire = (attemptsRemaining: number): Effect.Effect<void, PlatformError, never> =>
226
+ fs.writeFileString(paths.analyticsStateLockPath, token, { flag: 'wx' }).pipe(
227
+ Effect.catchTag('SystemError', error => {
228
+ if (error.reason !== 'AlreadyExists' || attemptsRemaining <= 0) {
229
+ return Effect.fail(error);
230
+ }
231
+ return recoverStaleAnalyticsStateLock(fs, paths.analyticsStateLockPath).pipe(
232
+ Effect.andThen(Effect.yieldNow()),
233
+ Effect.andThen(tryAcquire(attemptsRemaining - 1))
234
+ );
235
+ })
236
+ );
237
+
238
+ yield* tryAcquire(ANALYTICS_STATE_LOCK_MAX_ATTEMPTS);
239
+ return { fs, lockPath: paths.analyticsStateLockPath, token };
240
+});
241
+
242
+const releaseAnalyticsStateLock = (lock: Effect.Effect.Success<typeof acquireAnalyticsStateLock>) =>
243
+ Effect.gen(function* () {
244
+ const currentToken = yield* lock.fs.readFileString(lock.lockPath, 'utf8');
245
+ if (currentToken === lock.token) {
246
+ yield* lock.fs.remove(lock.lockPath, { force: true });
247
+ }
248
+ }).pipe(Effect.catchAll(() => Effect.void));
249
+
250
+const withAnalyticsStateLock = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
251
+ Effect.acquireUseRelease(acquireAnalyticsStateLock, () => effect, releaseAnalyticsStateLock);
252
+
253
// Concurrent CLI processes write this file; temp-file + rename keeps each
254
// write atomic so readers never observe partial JSON.
255
const writeAnalyticsStateFile = (contents: string) =>
@@ -201,7 +263,7 @@ const writeAnalyticsStateFile = (contents: string) =>
263
.pipe(Effect.tapError(() => fs.remove(temporaryPath).pipe(Effect.ignore)));
264
});
265
204
-const getOrCreateInstallId = Effect.gen(function* () {
266
+const getOrCreateInstallIdUnlocked = Effect.gen(function* () {
267
const fs = yield* FileSystem.FileSystem;
268
const paths = yield* getAnalyticsPaths;
269
@@ -223,9 +285,13 @@ const getOrCreateInstallId = Effect.gen(function* () {
285
});
286
yield* writeAnalyticsStateFile(contents);
287
return installId;
226
-}).pipe(Effect.catchAll(() => makeInstallId));
288
+});
289
+
290
+const getOrCreateInstallId = withAnalyticsStateLock(getOrCreateInstallIdUnlocked).pipe(
291
+ Effect.catchAll(() => makeInstallId)
292
+);
293
228
-const mergeAnalyticsState = (installId: string, patch: Partial<AnalyticsState>) =>
294
+const mergeAnalyticsStateUnlocked = (installId: string, patch: Partial<AnalyticsState>) =>
295
Effect.gen(function* () {
296
const fs = yield* FileSystem.FileSystem;
297
const paths = yield* getAnalyticsPaths;
@@ -242,7 +308,7 @@ const mergeAnalyticsState = (installId: string, patch: Partial<AnalyticsState>)
308
...patch,
309
});
310
yield* writeAnalyticsStateFile(contents);
245
- }).pipe(Effect.catchAll(() => Effect.void));
311
+ });
312
313
const readUserConfig = Effect.gen(function* () {
314
const paths = yield* getAnalyticsPaths;
@@ -682,44 +748,48 @@ export const linkApolloIdentityForAnalytics = (apolloUserId: string, apiKey?: st
748
return;
749
}
750
685
- let state = yield* readAnalyticsState;
686
- let installId = yield* getOrCreateInstallId;
687
-
688
- // A second user on this machine must not merge into the previous user's
689
- // PostHog person: rotate to a fresh install_id before linking.
690
- const previousIdentity = state?.apollo_user_id ?? state?.aliased_apollo_user_id;
691
- const identityChanged =
692
- typeof previousIdentity === 'string' &&
693
- previousIdentity.length > 0 &&
694
- previousIdentity !== resolved;
695
- if (identityChanged) {
696
- installId = yield* makeInstallId;
697
- yield* mergeAnalyticsState(installId, {
698
- install_id: installId,
699
- apollo_user_id: undefined,
700
- aliased_apollo_user_id: undefined,
701
- api_key_fingerprint: undefined,
702
- });
703
- state = yield* readAnalyticsState;
704
- }
751
+ yield* withAnalyticsStateLock(
752
+ Effect.gen(function* () {
753
+ let state = yield* readAnalyticsState;
754
+ let installId = yield* getOrCreateInstallIdUnlocked;
755
+
756
+ // A second user on this machine must not merge into the previous user's
757
+ // PostHog person: rotate to a fresh install_id before linking.
758
+ const previousIdentity = state?.apollo_user_id ?? state?.aliased_apollo_user_id;
759
+ const identityChanged =
760
+ typeof previousIdentity === 'string' &&
761
+ previousIdentity.length > 0 &&
762
+ previousIdentity !== resolved;
763
+ if (identityChanged) {
764
+ installId = yield* makeInstallId;
765
+ yield* mergeAnalyticsStateUnlocked(installId, {
766
+ install_id: installId,
767
+ apollo_user_id: undefined,
768
+ aliased_apollo_user_id: undefined,
769
+ api_key_fingerprint: undefined,
770
+ });
771
+ state = yield* readAnalyticsState;
772
+ }
773
706
- const fingerprint =
707
- typeof apiKey === 'string' && apiKey.length > 0
708
- ? fingerprintApiKey(apiKey)
709
- : yield* getApiKeyFingerprint;
710
- if (state?.apollo_user_id !== resolved || state?.api_key_fingerprint !== fingerprint) {
711
- yield* mergeAnalyticsState(installId, {
712
- apollo_user_id: resolved,
713
- ...(fingerprint === null ? {} : { api_key_fingerprint: fingerprint }),
714
- });
715
- }
774
+ const fingerprint =
775
+ typeof apiKey === 'string' && apiKey.length > 0
776
+ ? fingerprintApiKey(apiKey)
777
+ : yield* getApiKeyFingerprint;
778
+ if (state?.apollo_user_id !== resolved || state?.api_key_fingerprint !== fingerprint) {
779
+ yield* mergeAnalyticsStateUnlocked(installId, {
780
+ apollo_user_id: resolved,
781
+ ...(fingerprint === null ? {} : { api_key_fingerprint: fingerprint }),
782
+ });
783
+ }
784
717
- if (typeof state?.aliased_apollo_user_id === 'string') {
718
- return;
719
- }
720
- if (yield* emitPostHogAlias(installId, resolved)) {
721
- yield* mergeAnalyticsState(installId, { aliased_apollo_user_id: resolved });
722
- }
785
+ if (typeof state?.aliased_apollo_user_id === 'string') {
786
+ return;
787
+ }
788
+ if (yield* emitPostHogAlias(installId, resolved)) {
789
+ yield* mergeAnalyticsStateUnlocked(installId, { aliased_apollo_user_id: resolved });
790
+ }
791
+ })
792
+ );
793
}).pipe(Effect.catchAllCause(() => Effect.void));
794
795
const STITCH_ATTEMPT_INTERVAL_MS = 24 * 60 * 60 * 1000;
@@ -743,44 +813,54 @@ export const ensureAnalyticsIdentity = (params: {
813
return;
814
}
815
746
- const state = yield* readAnalyticsState;
816
const fingerprint = fingerprintApiKey(params.apiKey);
748
- const identityLinked =
749
- typeof state?.apollo_user_id === 'string' &&
750
- state.apollo_user_id.length > 0 &&
751
- state.api_key_fingerprint === fingerprint;
752
- if (identityLinked) {
753
- return;
754
- }
817
+ const shouldAttempt = yield* withAnalyticsStateLock(
818
+ Effect.gen(function* () {
819
+ const state = yield* readAnalyticsState;
820
+ const identityLinked =
821
+ typeof state?.apollo_user_id === 'string' &&
822
+ state.apollo_user_id.length > 0 &&
823
+ state.api_key_fingerprint === fingerprint;
824
+ if (identityLinked) {
825
+ return false;
826
+ }
827
+
828
+ const lastAttemptMs = Date.parse(state?.stitch_attempted_at ?? '');
829
+ const nowMs = yield* Clock.currentTimeMillis;
830
+ if (Number.isFinite(lastAttemptMs) && nowMs - lastAttemptMs < STITCH_ATTEMPT_INTERVAL_MS) {
831
+ return false;
832
+ }
833
756
- const lastAttemptMs = Date.parse(state?.stitch_attempted_at ?? '');
757
- const nowMs = yield* Clock.currentTimeMillis;
758
- if (Number.isFinite(lastAttemptMs) && nowMs - lastAttemptMs < STITCH_ATTEMPT_INTERVAL_MS) {
834
+ // Persisted before the network attempt so failures cannot retry-storm.
835
+ const installId = yield* getOrCreateInstallIdUnlocked;
836
+ yield* mergeAnalyticsStateUnlocked(installId, {
837
+ stitch_attempted_at: DateTime.formatIso(yield* DateTime.now),
838
+ });
839
+ return true;
840
+ })
841
+ );
842
+ if (!shouldAttempt) {
843
return;
844
}
845
762
- // Persisted before the network attempt so failures cannot retry-storm.
763
- const installId = yield* getOrCreateInstallId;
764
- yield* mergeAnalyticsState(installId, {
765
- stitch_attempted_at: DateTime.formatIso(yield* DateTime.now),
766
- });
767
-
846
const info = yield* params
847
.fetchSessionInfo({ baseURL: params.baseURL, userApiKey: params.apiKey })
848
.pipe(Effect.timeout(STITCH_SESSION_INFO_TIMEOUT));
849
yield* linkApolloIdentityForAnalytics(info.org_member.id, params.apiKey);
850
}).pipe(Effect.catchAllCause(() => Effect.void));
851
774
-export const clearApolloIdentityForAnalytics = Effect.gen(function* () {
775
- const state = yield* readAnalyticsState;
776
- if (!state?.install_id || !state.apollo_user_id) {
777
- return;
778
- }
779
- yield* mergeAnalyticsState(state.install_id, {
780
- apollo_user_id: undefined,
781
- api_key_fingerprint: undefined,
782
- });
783
-}).pipe(Effect.catchAllCause(() => Effect.void));
852
+export const clearApolloIdentityForAnalytics = withAnalyticsStateLock(
853
+ Effect.gen(function* () {
854
+ const state = yield* readAnalyticsState;
855
+ if (!state?.install_id || !state.apollo_user_id) {
856
+ return;
857
+ }
858
+ yield* mergeAnalyticsStateUnlocked(state.install_id, {
859
+ apollo_user_id: undefined,
860
+ api_key_fingerprint: undefined,
861
+ });
862
+ })
863
+).pipe(Effect.catchAllCause(() => Effect.void));
864
865
export const trackCliCodactFailureEffect = (failure: CliCodactFailure) =>
866
Effect.gen(function* () {
ts/packages/cli/test/src/analytics.dispatch.test.ts
+67
@@ -423,6 +423,41 @@ describe('CLI analytics dispatch', () => {
423
}).pipe(Effect.provide(makePlatformLayer(home)));
424
});
425
426
+ it.effect('uses one install_id across concurrent first-run events', () => {
427
+ const home = tempy.temporaryDirectory();
428
+ const scriptPath = `${home}/composio.ts`;
429
+ enableTelemetry('');
430
+ process.argv[1] = scriptPath;
431
+
432
+ return Effect.gen(function* () {
433
+ const fs = yield* FileSystem.FileSystem;
434
+ const path = yield* Path.Path;
435
+ yield* fs.writeFileString(scriptPath, '');
436
+
437
+ yield* Effect.all(
438
+ Array.from({ length: 16 }, (_, index) =>
439
+ trackCliEventEffect({
440
+ name: 'producer_event',
441
+ properties: { index },
442
+ })
443
+ ),
444
+ { concurrency: 'unbounded' }
445
+ );
446
+
447
+ expect(childProcessMocks.spawn).toHaveBeenCalledTimes(16);
448
+ const installIds = childProcessMocks.spawn.mock.calls.map(call => {
449
+ const args = call[1] as string[];
450
+ return decodeWorkerPayload<{ installId: string }>(args[2]!).installId;
451
+ });
452
+ expect(new Set(installIds).size).toBe(1);
453
+
454
+ const persisted = JSON.parse(
455
+ yield* fs.readFileString(path.join(home, '.composio', 'analytics.json'), 'utf8')
456
+ ) as { install_id: string };
457
+ expect(persisted.install_id).toBe(installIds[0]);
458
+ }).pipe(Effect.provide(makePlatformLayer(home)));
459
+ });
460
+
461
it.effect('keys post-login events on the persisted apollo_user_id', () => {
462
const home = tempy.temporaryDirectory();
463
const scriptPath = `${home}/composio.ts`;
@@ -499,6 +534,38 @@ describe('CLI analytics dispatch', () => {
534
}).pipe(Effect.provide(makePlatformLayer(home)));
535
});
536
537
+ it.effect('emits one alias across concurrent identity links', () => {
538
+ const home = tempy.temporaryDirectory();
539
+ const scriptPath = `${home}/composio.ts`;
540
+ enableTelemetry('uak_concurrent');
541
+ process.argv[1] = scriptPath;
542
+
543
+ return Effect.gen(function* () {
544
+ const fs = yield* FileSystem.FileSystem;
545
+ const path = yield* Path.Path;
546
+ yield* fs.writeFileString(scriptPath, '');
547
+
548
+ yield* Effect.all(
549
+ Array.from({ length: 8 }, () =>
550
+ linkApolloIdentityForAnalytics('om_concurrent', 'uak_concurrent')
551
+ ),
552
+ { concurrency: 'unbounded' }
553
+ );
554
+
555
+ expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
556
+ const persisted = JSON.parse(
557
+ yield* fs.readFileString(path.join(home, '.composio', 'analytics.json'), 'utf8')
558
+ ) as {
559
+ apollo_user_id: string;
560
+ aliased_apollo_user_id: string;
561
+ api_key_fingerprint: string;
562
+ };
563
+ expect(persisted.apollo_user_id).toBe('om_concurrent');
564
+ expect(persisted.aliased_apollo_user_id).toBe('om_concurrent');
565
+ expect(persisted.api_key_fingerprint.length).toBeGreaterThan(0);
566
+ }).pipe(Effect.provide(makePlatformLayer(home)));
567
+ });
568
+
569
it.effect('retries the alias when the first attempt could not be enqueued', () => {
570
const home = tempy.temporaryDirectory();
571
const scriptPath = `${home}/composio.ts`;