@devries-cameron20 / composio Mirror · read-only / commits / ce2c3a402
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.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 18:30 UTC

fix(cli): wait for analytics state locks

Kshitij Jhunjhunwala committed Jul 29, 2026 at 16:07 UTC ce2c3a40240d52d9c3f8eb1c2be13398f12727d0
2 files changed +52 -8
ts/packages/cli/src/analytics/dispatch.ts
+26 -8
@@ -25,7 +25,8 @@ 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;
28 +const ANALYTICS_STATE_LOCK_RETRY_MS = 10;
29 +const ANALYTICS_STATE_LOCK_TIMEOUT_MS = ANALYTICS_STATE_LOCK_STALE_MS + 5_000;
30 const CONSUMER_SHORT_TERM_CACHE_FILE_NAME = 'consumer-short-term-cache.json';
31 const CLI_CODACT_FAILURES_PATH = '/api/v3/cli/codact_failures';
32
@@ -203,8 +204,7 @@ const recoverStaleAnalyticsStateLock = (
204 if (!modifiedAt) {
205 return;
206 }
206 - const now = yield* Clock.currentTimeMillis;
207 - if (now - modifiedAt.getTime() <= ANALYTICS_STATE_LOCK_STALE_MS) {
207 + if (Date.now() - modifiedAt.getTime() <= ANALYTICS_STATE_LOCK_STALE_MS) {
208 return;
209 }
210
@@ -219,23 +219,41 @@ const acquireAnalyticsStateLock = Effect.gen(function* () {
219 const fs = yield* FileSystem.FileSystem;
220 const paths = yield* getAnalyticsPaths;
221 const token = crypto.randomUUID();
222 + const deadline = Date.now() + ANALYTICS_STATE_LOCK_TIMEOUT_MS;
223
224 yield* fs.makeDirectory(paths.analyticsDir, { recursive: true });
225
225 - const tryAcquire = (attemptsRemaining: number): Effect.Effect<void, PlatformError, never> =>
226 + const waitBeforeRetry = Effect.promise(
227 + () =>
228 + new Promise<void>(resolve => {
229 + setTimeout(resolve, ANALYTICS_STATE_LOCK_RETRY_MS);
230 + })
231 + );
232 + const tryAcquire = (): Effect.Effect<void, PlatformError, never> =>
233 fs.writeFileString(paths.analyticsStateLockPath, token, { flag: 'wx' }).pipe(
234 Effect.catchTag('SystemError', error => {
228 - if (error.reason !== 'AlreadyExists' || attemptsRemaining <= 0) {
235 + if (error.reason !== 'AlreadyExists') {
236 return Effect.fail(error);
237 }
238 return recoverStaleAnalyticsStateLock(fs, paths.analyticsStateLockPath).pipe(
232 - Effect.andThen(Effect.yieldNow()),
233 - Effect.andThen(tryAcquire(attemptsRemaining - 1))
239 + Effect.andThen(
240 + fs
241 + .exists(paths.analyticsStateLockPath)
242 + .pipe(Effect.catchAll(() => Effect.succeed(true)))
243 + ),
244 + Effect.flatMap(lockStillExists => {
245 + if (lockStillExists && Date.now() >= deadline) {
246 + return Effect.fail(error);
247 + }
248 + return (lockStillExists ? waitBeforeRetry : Effect.void).pipe(
249 + Effect.andThen(tryAcquire())
250 + );
251 + })
252 );
253 })
254 );
255
238 - yield* tryAcquire(ANALYTICS_STATE_LOCK_MAX_ATTEMPTS);
256 + yield* tryAcquire();
257 return { fs, lockPath: paths.analyticsStateLockPath, token };
258 });
259
ts/packages/cli/test/src/analytics.dispatch.test.ts
+26
@@ -3,6 +3,8 @@ import { afterEach, beforeEach, vi } from 'vitest';
3 import { FetchHttpClient, FileSystem, Path } from '@effect/platform';
4 import { BunFileSystem, BunPath } from '@effect/platform-bun';
5 import { Effect, Layer } from 'effect';
6 +import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
7 +import path from 'node:path';
8 import * as tempy from 'tempy';
9 import {
10 clearApolloIdentityForAnalytics,
@@ -458,6 +460,30 @@ describe('CLI analytics dispatch', () => {
460 }).pipe(Effect.provide(makePlatformLayer(home)));
461 });
462
463 + it.effect('waits for a live analytics-state lock instead of exhausting retries', () => {
464 + const home = tempy.temporaryDirectory();
465 + const scriptPath = path.join(home, 'composio.ts');
466 + const composioDir = path.join(home, '.composio');
467 + const lockPath = path.join(composioDir, 'analytics.json.lock');
468 + enableTelemetry('');
469 + process.argv[1] = scriptPath;
470 + mkdirSync(composioDir, { recursive: true });
471 + writeFileSync(scriptPath, '');
472 + writeFileSync(lockPath, 'other-process');
473 + const releaseTimer = setTimeout(() => rmSync(lockPath, { force: true }), 25);
474 +
475 + return Effect.gen(function* () {
476 + yield* trackCliEventEffect({ name: CLI_ANALYTICS_EVENTS.CLI_SETUP_SUCCEEDED });
477 +
478 + const fs = yield* FileSystem.FileSystem;
479 + expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
480 + expect(yield* fs.exists(lockPath)).toBe(false);
481 + }).pipe(
482 + Effect.ensuring(Effect.sync(() => clearTimeout(releaseTimer))),
483 + Effect.provide(makePlatformLayer(home))
484 + );
485 + });
486 +
487 it.effect('keys post-login events on the persisted apollo_user_id', () => {
488 const home = tempy.temporaryDirectory();
489 const scriptPath = `${home}/composio.ts`;