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): key identity stitch throttle by credential

Kshitij Jhunjhunwala0f37be91e5be
diff --git a/ts/packages/cli/src/analytics/dispatch.ts b/ts/packages/cli/src/analytics/dispatch.ts
index 04d7d3e1c..dc5923ec3 100644
--- a/ts/packages/cli/src/analytics/dispatch.ts
+++ b/ts/packages/cli/src/analytics/dispatch.ts
@@ -184,6 +184,7 @@ type AnalyticsState = {
readonly aliased_apollo_user_id?: string;
readonly api_key_fingerprint?: string;
readonly stitch_attempted_at?: string;
+ readonly stitch_attempted_api_key_fingerprint?: string;
readonly created_at?: string;
};
@@ -845,7 +846,12 @@ export const ensureAnalyticsIdentity = (params: {
const lastAttemptMs = Date.parse(state?.stitch_attempted_at ?? '');
const nowMs = yield* Clock.currentTimeMillis;
- if (Number.isFinite(lastAttemptMs) && nowMs - lastAttemptMs < STITCH_ATTEMPT_INTERVAL_MS) {
+ const sameCredentialAttempt = state?.stitch_attempted_api_key_fingerprint === fingerprint;
+ if (
+ sameCredentialAttempt &&
+ Number.isFinite(lastAttemptMs) &&
+ nowMs - lastAttemptMs < STITCH_ATTEMPT_INTERVAL_MS
+ ) {
return false;
}
@@ -853,6 +859,7 @@ export const ensureAnalyticsIdentity = (params: {
const installId = yield* getOrCreateInstallIdUnlocked;
yield* mergeAnalyticsStateUnlocked(installId, {
stitch_attempted_at: DateTime.formatIso(yield* DateTime.now),
+ stitch_attempted_api_key_fingerprint: fingerprint,
});
return true;
})
diff --git a/ts/packages/cli/test/src/analytics.dispatch.test.ts b/ts/packages/cli/test/src/analytics.dispatch.test.ts
index 1f8631ce2..e013a1ca0 100644
--- a/ts/packages/cli/test/src/analytics.dispatch.test.ts
+++ b/ts/packages/cli/test/src/analytics.dispatch.test.ts
@@ -1104,8 +1104,12 @@ describe('CLI analytics dispatch', () => {
const persisted = JSON.parse(
yield* fs.readFileString(path.join(home, '.composio', 'analytics.json'), 'utf8')
- ) as { stitch_attempted_at?: string };
+ ) as {
+ stitch_attempted_at?: string;
+ stitch_attempted_api_key_fingerprint?: string;
+ };
expect(typeof persisted.stitch_attempted_at).toBe('string');
+ expect(typeof persisted.stitch_attempted_api_key_fingerprint).toBe('string');
// Identity is still unlinked, but the attempt is throttled for 24h.
yield* ensureAnalyticsIdentity({
@@ -1116,5 +1120,40 @@ describe('CLI analytics dispatch', () => {
expect(fetchSessionInfo).toHaveBeenCalledTimes(1);
}).pipe(Effect.provide(makePlatformLayer(home)));
});
+
+ it.effect('bootstrap stitch retries immediately when the credential changes', () => {
+ const home = tempy.temporaryDirectory();
+ const scriptPath = `${home}/composio.ts`;
+ enableTelemetry('uak_first');
+ process.argv[1] = scriptPath;
+ const failedFetch = vi.fn(() => Effect.fail(new Error('offline')));
+ const changedCredentialFetch = vi.fn(() =>
+ Effect.succeed({ org_member: { id: 'om_changed' } })
+ );
+
+ return Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ yield* fs.writeFileString(scriptPath, '');
+
+ yield* ensureAnalyticsIdentity({
+ apiKey: 'uak_first',
+ baseURL: 'https://backend.example.test',
+ fetchSessionInfo: failedFetch,
+ });
+ yield* ensureAnalyticsIdentity({
+ apiKey: 'uak_second',
+ baseURL: 'https://backend.example.test',
+ fetchSessionInfo: changedCredentialFetch,
+ });
+
+ expect(failedFetch).toHaveBeenCalledTimes(1);
+ expect(changedCredentialFetch).toHaveBeenCalledTimes(1);
+ expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
+ const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
+ const payload = decodeWorkerPayload<{ event: string; distinctId: string }>(args[2]!);
+ expect(payload.event).toBe('$create_alias');
+ expect(payload.distinctId).toBe('om_changed');
+ }).pipe(Effect.provide(makePlatformLayer(home)));
+ });
});
});