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): wait for analytics state locks

Kshitij Jhunjhunwalace2c3a40240d
diff --git a/ts/packages/cli/src/analytics/dispatch.ts b/ts/packages/cli/src/analytics/dispatch.ts
index e1f2c1656..04d7d3e1c 100644
--- a/ts/packages/cli/src/analytics/dispatch.ts
+++ b/ts/packages/cli/src/analytics/dispatch.ts
@@ -25,7 +25,8 @@ const COMPOSIO_DIR = '.composio';
const ANALYTICS_STATE_FILE_NAME = 'analytics.json';
const ANALYTICS_STATE_LOCK_FILE_NAME = `${ANALYTICS_STATE_FILE_NAME}.lock`;
const ANALYTICS_STATE_LOCK_STALE_MS = 30_000;
-const ANALYTICS_STATE_LOCK_MAX_ATTEMPTS = 10_000;
+const ANALYTICS_STATE_LOCK_RETRY_MS = 10;
+const ANALYTICS_STATE_LOCK_TIMEOUT_MS = ANALYTICS_STATE_LOCK_STALE_MS + 5_000;
const CONSUMER_SHORT_TERM_CACHE_FILE_NAME = 'consumer-short-term-cache.json';
const CLI_CODACT_FAILURES_PATH = '/api/v3/cli/codact_failures';
@@ -203,8 +204,7 @@ const recoverStaleAnalyticsStateLock = (
if (!modifiedAt) {
return;
}
- const now = yield* Clock.currentTimeMillis;
- if (now - modifiedAt.getTime() <= ANALYTICS_STATE_LOCK_STALE_MS) {
+ if (Date.now() - modifiedAt.getTime() <= ANALYTICS_STATE_LOCK_STALE_MS) {
return;
}
@@ -219,23 +219,41 @@ const acquireAnalyticsStateLock = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const paths = yield* getAnalyticsPaths;
const token = crypto.randomUUID();
+ const deadline = Date.now() + ANALYTICS_STATE_LOCK_TIMEOUT_MS;
yield* fs.makeDirectory(paths.analyticsDir, { recursive: true });
- const tryAcquire = (attemptsRemaining: number): Effect.Effect<void, PlatformError, never> =>
+ const waitBeforeRetry = Effect.promise(
+ () =>
+ new Promise<void>(resolve => {
+ setTimeout(resolve, ANALYTICS_STATE_LOCK_RETRY_MS);
+ })
+ );
+ const tryAcquire = (): Effect.Effect<void, PlatformError, never> =>
fs.writeFileString(paths.analyticsStateLockPath, token, { flag: 'wx' }).pipe(
Effect.catchTag('SystemError', error => {
- if (error.reason !== 'AlreadyExists' || attemptsRemaining <= 0) {
+ if (error.reason !== 'AlreadyExists') {
return Effect.fail(error);
}
return recoverStaleAnalyticsStateLock(fs, paths.analyticsStateLockPath).pipe(
- Effect.andThen(Effect.yieldNow()),
- Effect.andThen(tryAcquire(attemptsRemaining - 1))
+ Effect.andThen(
+ fs
+ .exists(paths.analyticsStateLockPath)
+ .pipe(Effect.catchAll(() => Effect.succeed(true)))
+ ),
+ Effect.flatMap(lockStillExists => {
+ if (lockStillExists && Date.now() >= deadline) {
+ return Effect.fail(error);
+ }
+ return (lockStillExists ? waitBeforeRetry : Effect.void).pipe(
+ Effect.andThen(tryAcquire())
+ );
+ })
);
})
);
- yield* tryAcquire(ANALYTICS_STATE_LOCK_MAX_ATTEMPTS);
+ yield* tryAcquire();
return { fs, lockPath: paths.analyticsStateLockPath, token };
});
diff --git a/ts/packages/cli/test/src/analytics.dispatch.test.ts b/ts/packages/cli/test/src/analytics.dispatch.test.ts
index 84108131a..1f8631ce2 100644
--- a/ts/packages/cli/test/src/analytics.dispatch.test.ts
+++ b/ts/packages/cli/test/src/analytics.dispatch.test.ts
@@ -3,6 +3,8 @@ import { afterEach, beforeEach, vi } from 'vitest';
import { FetchHttpClient, FileSystem, Path } from '@effect/platform';
import { BunFileSystem, BunPath } from '@effect/platform-bun';
import { Effect, Layer } from 'effect';
+import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
+import path from 'node:path';
import * as tempy from 'tempy';
import {
clearApolloIdentityForAnalytics,
@@ -458,6 +460,30 @@ describe('CLI analytics dispatch', () => {
}).pipe(Effect.provide(makePlatformLayer(home)));
});
+ it.effect('waits for a live analytics-state lock instead of exhausting retries', () => {
+ const home = tempy.temporaryDirectory();
+ const scriptPath = path.join(home, 'composio.ts');
+ const composioDir = path.join(home, '.composio');
+ const lockPath = path.join(composioDir, 'analytics.json.lock');
+ enableTelemetry('');
+ process.argv[1] = scriptPath;
+ mkdirSync(composioDir, { recursive: true });
+ writeFileSync(scriptPath, '');
+ writeFileSync(lockPath, 'other-process');
+ const releaseTimer = setTimeout(() => rmSync(lockPath, { force: true }), 25);
+
+ return Effect.gen(function* () {
+ yield* trackCliEventEffect({ name: CLI_ANALYTICS_EVENTS.CLI_SETUP_SUCCEEDED });
+
+ const fs = yield* FileSystem.FileSystem;
+ expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
+ expect(yield* fs.exists(lockPath)).toBe(false);
+ }).pipe(
+ Effect.ensuring(Effect.sync(() => clearTimeout(releaseTimer))),
+ Effect.provide(makePlatformLayer(home))
+ );
+ });
+
it.effect('keys post-login events on the persisted apollo_user_id', () => {
const home = tempy.temporaryDirectory();
const scriptPath = `${home}/composio.ts`;