@devries-cameron20 / composio Mirror · read-only
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, 04:45 UTC

next
ts 991 lines 34.9 KB
Raw
1 import { cliRequestHeaders } from 'src/services/client-provenance';
2 import process from 'node:process';
3 import type { PlatformError } from 'effect/PlatformError';
4 import {
5 Cause,
6 Clock,
7 DateTime,
8 Effect,
9 FileSystem,
10 Option,
11 Path,
12 Predicate,
13 Schema,
14 } from 'effect';
15 import { Base64Url } from 'effect/encoding';
16 import { HttpClient, HttpClientRequest } from 'effect/http';
17 import * as constants from 'src/constants';
18 import { APP_CONFIG, TELEMETRY_CONFIG, telemetryDisabled } from 'src/config';
19 import { resolveBackend, withoutTrailingSlashes } from 'src/config/backend';
20 import { getDetachedWorkerSpawnArgs, spawnDetached } from 'src/services/detached-process';
21 import { atomicWriteFileString } from 'src/utils/atomic-write';
22 import { sha256Hex } from 'src/utils/checksums';
23 import { djb2Hash } from 'src/utils/djb2';
24 import { NodeOs } from 'src/services/node-os';
25 import { detectPluginHost, rawHostEnvironment } from 'src/services/agent-host-env';
26 import { TerminalUI } from 'src/services/terminal-ui';
27 import { isTelemetryDebugEnabled, TELEMETRY_DEBUG_FLAG } from 'src/services/runtime-flags';
28 import { cliInvocationContext } from 'src/services/runtime-cli-context';
29 import { invocationRunId } from 'src/invocation/command-identity';
30 import type { AnalyticsEnvelope, TrackEvent } from './types';
31
32 const INTERNAL_ANALYTICS_WORKER_FLAG = '__analytics-worker';
33 const INTERNAL_CODACT_FAILURE_WORKER_FLAG = '__codact-failure-worker';
34 const COMPOSIO_DIR = '.composio';
35 const ANALYTICS_STATE_FILE_NAME = 'analytics.json';
36 const ANALYTICS_STATE_LOCK_FILE_NAME = `${ANALYTICS_STATE_FILE_NAME}.lock`;
37 const ANALYTICS_STATE_LOCK_STALE_MS = 30_000;
38 const ANALYTICS_STATE_LOCK_RETRY_MS = 10;
39 const ANALYTICS_STATE_LOCK_TIMEOUT_MS = 500;
40 const PENDING_ALIAS_RETRY_MS = 5 * 60 * 1000;
41 const CONSUMER_SHORT_TERM_CACHE_FILE_NAME = 'consumer-short-term-cache.json';
42 const CLI_CODACT_FAILURES_PATH = '/api/v3/cli/codact_failures';
43
44 const POSTHOG_ALIAS_EVENT = '$create_alias';
45 const POSTHOG_LIB = 'composio-cli';
46
47 export type CliCodactFailureType = 'wrong_tool_slug' | 'wrong_tool_input_param';
48
49 export type CliCodactFailureToolInfo = {
50 readonly toolkit?: string;
51 readonly tool?: {
52 readonly slug: string;
53 readonly version: string;
54 };
55 };
56
57 export type CliCodactFailure = {
58 readonly failureType: CliCodactFailureType;
59 readonly toolInfo?: CliCodactFailureToolInfo;
60 readonly ctx: Record<string, unknown>;
61 readonly session?: Record<string, unknown>;
62 readonly requestId?: string;
63 };
64
65 type ConsumerShortTermCacheState = Record<
66 string,
67 {
68 readonly probablyMyCliSessionsByCwdHash?: Record<
69 string,
70 {
71 readonly id: string;
72 readonly expiresAt: string;
73 }
74 >;
75 }
76 >;
77
78 const getPostHogConfig = Effect.orDie(
79 Effect.all({
80 ingestUrl: TELEMETRY_CONFIG.POSTHOG_INGEST_URL,
81 projectKey: TELEMETRY_CONFIG.POSTHOG_PROJECT_API_KEY,
82 })
83 );
84
85 const postHogEnabled = Effect.gen(function* () {
86 if (yield* telemetryDisabled) {
87 return false;
88 }
89 const { projectKey } = yield* getPostHogConfig;
90 return projectKey.length > 0;
91 });
92
93 export const analyticsIdentityLinkingEnabled = postHogEnabled;
94
95 const jsonError = (error: unknown): Error =>
96 Predicate.isError(error) ? error : new Error(String(error));
97 const decodeJson = (str: string) =>
98 Effect.try({ try: (): unknown => JSON.parse(str), catch: jsonError });
99 const encodeJson = (value: unknown) =>
100 Effect.try({ try: () => JSON.stringify(value), catch: jsonError });
101 const encodePrettyJson = (value: unknown) =>
102 Effect.try({ try: () => JSON.stringify(value, null, 2), catch: jsonError });
103
104 const telemetryDebugLog = (label: string, payload: Record<string, unknown>) =>
105 Effect.gen(function* () {
106 if (!(yield* isTelemetryDebugEnabled)) {
107 return;
108 }
109
110 const body = yield* encodePrettyJson({ label, ...payload });
111 const ui = yield* TerminalUI;
112 yield* ui.error(`[telemetry-debug] ${body}`);
113 }).pipe(Effect.ignore);
114
115 const telemetryErrorDetails = (cause: Cause.Cause<unknown>): Record<string, string> => {
116 const squashed = Cause.squash(cause);
117 const error = Cause.isUnknownError(squashed) ? squashed.cause : squashed;
118 return error instanceof Error
119 ? { name: error.name, message: error.message }
120 : { message: String(error) };
121 };
122
123 const getAnalyticsPaths = Effect.gen(function* () {
124 const path = yield* Path.Path;
125 const os = yield* NodeOs;
126 const analyticsDir = path.join(os.homedir, COMPOSIO_DIR);
127 const cacheDir =
128 (yield* Effect.orDie(APP_CONFIG.CACHE_DIR)) ??
129 path.join(os.homedir, constants.USER_COMPOSIO_DIR);
130
131 return {
132 analyticsDir,
133 analyticsStatePath: path.join(analyticsDir, ANALYTICS_STATE_FILE_NAME),
134 analyticsStateLockPath: path.join(analyticsDir, ANALYTICS_STATE_LOCK_FILE_NAME),
135 // user_data.json and config.json live in the cache dir (setup-cache-dir.ts,
136 // cli-user-config.ts), which COMPOSIO_CACHE_DIR relocates away from ~/.composio.
137 userConfigPath: path.join(cacheDir, constants.USER_CONFIG_FILE_NAME),
138 cliConfigPath: path.join(cacheDir, constants.CLI_CONFIG_FILE_NAME),
139 consumerShortTermCachePath: path.join(cacheDir, CONSUMER_SHORT_TERM_CACHE_FILE_NAME),
140 };
141 });
142
143 const readOptionalJson = <A>(filePath: string) =>
144 Effect.gen(function* () {
145 const fs = yield* FileSystem.FileSystem;
146 const raw = yield* fs.readFileString(filePath, 'utf8');
147 return (yield* decodeJson(raw)) as A;
148 }).pipe(Effect.catch(() => Effect.succeed(undefined)));
149
150 type AnalyticsState = {
151 readonly install_id?: string;
152 readonly apollo_user_id?: string;
153 readonly aliased_apollo_user_id?: string;
154 readonly pending_alias_apollo_user_id?: string;
155 readonly pending_alias_install_id?: string;
156 readonly pending_alias_attempted_at?: string;
157 readonly api_key_fingerprint?: string;
158 readonly created_at?: string;
159 };
160
161 const makeInstallId = Effect.sync(() => crypto.randomUUID());
162
163 const readAnalyticsState = Effect.gen(function* () {
164 const paths = yield* getAnalyticsPaths;
165 return yield* readOptionalJson<AnalyticsState>(paths.analyticsStatePath);
166 });
167
168 /**
169 * The stored analytics install ID, for first-party requests that should join
170 * the CLI's own events. `None` when telemetry is disabled or no ID is stored or
171 * readable. Read-only: it never creates an ID, takes no lock, and cannot fail.
172 */
173 export const readInstallIdWhenTelemetryEnabled = Effect.gen(function* () {
174 if (!(yield* postHogEnabled)) {
175 return Option.none<string>();
176 }
177 const state = yield* readAnalyticsState;
178 return typeof state?.install_id === 'string' && state.install_id.length > 0
179 ? Option.some(state.install_id)
180 : Option.none<string>();
181 }).pipe(Effect.catch(() => Effect.succeed(Option.none<string>())));
182
183 const recoverStaleAnalyticsStateLock = (
184 fs: FileSystem.FileSystem,
185 lockPath: string
186 ): Effect.Effect<void, never, never> =>
187 Effect.gen(function* () {
188 const info = yield* fs.stat(lockPath);
189 const modifiedAt = Option.getOrUndefined(info.mtime);
190 if (!modifiedAt) {
191 return;
192 }
193 if (Date.now() - modifiedAt.getTime() <= ANALYTICS_STATE_LOCK_STALE_MS) {
194 return;
195 }
196
197 // Rename claims this exact stale lock atomically. A competing process can
198 // then acquire the canonical path without being deleted by our cleanup.
199 const stalePath = `${lockPath}.stale-${crypto.randomUUID().slice(0, 8)}`;
200 yield* fs.rename(lockPath, stalePath);
201 yield* fs.remove(stalePath, { force: true });
202 }).pipe(Effect.catch(() => Effect.void));
203
204 const acquireAnalyticsStateLock = Effect.gen(function* () {
205 const fs = yield* FileSystem.FileSystem;
206 const paths = yield* getAnalyticsPaths;
207 const token = crypto.randomUUID();
208 const deadline = Date.now() + ANALYTICS_STATE_LOCK_TIMEOUT_MS;
209
210 yield* fs.makeDirectory(paths.analyticsDir, { recursive: true });
211
212 // Lock contention is wall-clock coordination between OS processes. Keeping
213 // the retry on the platform timer also prevents Effect's TestClock from
214 // freezing concurrent lock tests.
215 const waitBeforeRetry = Effect.promise(
216 () =>
217 new Promise<void>(resolve => {
218 setTimeout(resolve, ANALYTICS_STATE_LOCK_RETRY_MS);
219 })
220 );
221 const tryAcquire = (): Effect.Effect<void, PlatformError, never> =>
222 fs.writeFileString(paths.analyticsStateLockPath, token, { flag: 'wx' }).pipe(
223 Effect.catchTag('PlatformError', error => {
224 if (!Predicate.isTagged(error.reason, 'AlreadyExists')) {
225 return Effect.fail(error);
226 }
227 return recoverStaleAnalyticsStateLock(fs, paths.analyticsStateLockPath).pipe(
228 Effect.andThen(
229 fs.exists(paths.analyticsStateLockPath).pipe(Effect.catch(() => Effect.succeed(true)))
230 ),
231 Effect.flatMap(lockStillExists => {
232 if (lockStillExists && Date.now() >= deadline) {
233 return Effect.fail(error);
234 }
235 return (lockStillExists ? waitBeforeRetry : Effect.void).pipe(
236 Effect.andThen(tryAcquire())
237 );
238 })
239 );
240 })
241 );
242
243 yield* tryAcquire();
244 return { fs, lockPath: paths.analyticsStateLockPath, token };
245 });
246
247 const releaseAnalyticsStateLock = (lock: Effect.Success<typeof acquireAnalyticsStateLock>) =>
248 Effect.gen(function* () {
249 const currentToken = yield* lock.fs.readFileString(lock.lockPath, 'utf8');
250 if (currentToken === lock.token) {
251 yield* lock.fs.remove(lock.lockPath, { force: true });
252 }
253 }).pipe(Effect.catch(() => Effect.void));
254
255 const withAnalyticsStateLock = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
256 Effect.acquireUseRelease(acquireAnalyticsStateLock, () => effect, releaseAnalyticsStateLock);
257
258 // Concurrent CLI processes write this file; temp-file + rename keeps each
259 // write atomic so readers never observe partial JSON.
260 const writeAnalyticsStateFile = (contents: string) =>
261 Effect.gen(function* () {
262 const fs = yield* FileSystem.FileSystem;
263 const paths = yield* getAnalyticsPaths;
264 yield* atomicWriteFileString({ fs, target: paths.analyticsStatePath, contents });
265 });
266
267 const getOrCreateInstallIdUnlocked = Effect.gen(function* () {
268 const fs = yield* FileSystem.FileSystem;
269 const paths = yield* getAnalyticsPaths;
270
271 yield* fs
272 .makeDirectory(paths.analyticsDir, { recursive: true })
273 .pipe(Effect.catch(() => Effect.void));
274
275 const state = yield* readOptionalJson<AnalyticsState>(paths.analyticsStatePath);
276 if (typeof state?.install_id === 'string' && state.install_id.length > 0) {
277 return state.install_id;
278 }
279
280 const installId = yield* makeInstallId;
281 const createdAt = DateTime.formatIso(yield* DateTime.now);
282 const contents = yield* encodeJson({
283 ...(state ?? {}),
284 install_id: installId,
285 created_at: createdAt,
286 });
287 yield* writeAnalyticsStateFile(contents);
288 return installId;
289 });
290
291 // A process whose bounded lock wait expired (a live lock held elsewhere) must
292 // still adopt an install_id another process already persisted, so concurrent
293 // first-run events converge on one identity instead of each racer minting its
294 // own. The fallback only reads: persistence stays exclusive to the lock-holding
295 // path, and an unlocked write here could clobber a concurrent state merge.
296 const adoptOrMintInstallId = Effect.gen(function* () {
297 const state = yield* readAnalyticsState;
298 if (typeof state?.install_id === 'string' && state.install_id.length > 0) {
299 return state.install_id;
300 }
301 return yield* makeInstallId;
302 });
303
304 const getOrCreateInstallId = withAnalyticsStateLock(getOrCreateInstallIdUnlocked).pipe(
305 Effect.catch(() => adoptOrMintInstallId)
306 );
307
308 const mergeAnalyticsStateUnlocked = (installId: string, patch: Partial<AnalyticsState>) =>
309 Effect.gen(function* () {
310 const fs = yield* FileSystem.FileSystem;
311 const paths = yield* getAnalyticsPaths;
312
313 yield* fs
314 .makeDirectory(paths.analyticsDir, { recursive: true })
315 .pipe(Effect.catch(() => Effect.void));
316
317 const state = yield* readOptionalJson<AnalyticsState>(paths.analyticsStatePath);
318 const contents = yield* encodeJson({
319 ...(state ?? {}),
320 install_id: state?.install_id ?? installId,
321 created_at: state?.created_at ?? DateTime.formatIso(yield* DateTime.now),
322 ...patch,
323 });
324 yield* writeAnalyticsStateFile(contents);
325 });
326
327 const readUserConfig = Effect.gen(function* () {
328 const paths = yield* getAnalyticsPaths;
329 return yield* readOptionalJson<{ api_key?: unknown; base_url?: unknown; org_id?: unknown }>(
330 paths.userConfigPath
331 );
332 });
333
334 const getOrgId = Effect.map(readUserConfig, config =>
335 typeof config?.org_id === 'string' && config.org_id.length > 0 ? config.org_id : null
336 );
337
338 const getUserApiKey = Effect.gen(function* () {
339 const envApiKey = yield* Effect.orDie(APP_CONFIG.USER_API_KEY);
340 if (envApiKey) {
341 return envApiKey;
342 }
343
344 const userConfig = yield* readUserConfig;
345 return typeof userConfig?.api_key === 'string' && userConfig.api_key.trim().length > 0
346 ? userConfig.api_key.trim()
347 : null;
348 });
349
350 const fingerprintApiKey = (apiKey: string) =>
351 Effect.promise(() => sha256Hex(new TextEncoder().encode(apiKey)));
352
353 const getApiKeyFingerprint = Effect.flatMap(getUserApiKey, apiKey =>
354 apiKey ? fingerprintApiKey(apiKey) : Effect.succeed(null)
355 );
356
357 // The keychain* security modes strip the api key from user_data.json, so no
358 // fingerprint is computable here. A persisted org is the durable login signal:
359 // logout keeps the file but clears org_id, so a stale analytics identity can
360 // never survive a failed best-effort state cleanup.
361 const keyringBackedLoginPresent = Effect.gen(function* () {
362 const paths = yield* getAnalyticsPaths;
363 const cliConfig = yield* readOptionalJson<{ security?: unknown }>(paths.cliConfigPath);
364 if (cliConfig?.security !== 'keychain' && cliConfig?.security !== 'keychain-subprocess') {
365 return false;
366 }
367 const userConfig = yield* readUserConfig;
368 return typeof userConfig?.org_id === 'string' && userConfig.org_id.trim().length > 0;
369 });
370
371 const getDistinctId = (installId: string) =>
372 Effect.gen(function* () {
373 const state = yield* readAnalyticsState;
374 const apolloUserId = state?.apollo_user_id;
375 const fingerprint = yield* getApiKeyFingerprint;
376
377 if (typeof apolloUserId === 'string' && apolloUserId.length > 0) {
378 if (fingerprint !== null && fingerprint === state?.api_key_fingerprint) {
379 return apolloUserId;
380 }
381 if (fingerprint === null && (yield* keyringBackedLoginPresent)) {
382 return apolloUserId;
383 }
384 }
385
386 const installIdMayBeAliased =
387 typeof state?.aliased_apollo_user_id === 'string' ||
388 (state?.pending_alias_install_id === installId &&
389 typeof state.pending_alias_apollo_user_id === 'string');
390 if (!installIdMayBeAliased) {
391 return installId;
392 }
393 return fingerprint !== null ? `user_${fingerprint}` : `anon_${installId}`;
394 });
395
396 // The direct-to-PostHog path bypasses Apollo's server-side sanitizer, so
397 // secret-shaped tokens are redacted client-side before any envelope leaves.
398 const REDACTED_VALUE = '[redacted]';
399 const KNOWN_SECRET_PATTERNS: ReadonlyArray<RegExp> = [
400 /\buak_[A-Za-z0-9_-]+/gu,
401 /\bak_[A-Za-z0-9_-]+/gu,
402 /\bphc_[A-Za-z0-9]+/gu,
403 /\bsk-[A-Za-z0-9_-]+/gu,
404 /\bghp_[A-Za-z0-9]+/gu,
405 /\bBearer\s+[A-Za-z0-9._~+/=-]+/gu,
406 ];
407
408 // Mixed-case-plus-digit tokens of api-key length; single-case tokens
409 // (uuids, hex ids, tool slugs) stay untouched.
410 const isHighEntropyToken = (token: string): boolean =>
411 token.length >= 24 &&
412 /^[A-Za-z0-9+/=_-]+$/u.test(token) &&
413 /[a-z]/u.test(token) &&
414 /[A-Z]/u.test(token) &&
415 /[0-9]/u.test(token);
416
417 const scrubSecretsFromString = (value: string): string =>
418 KNOWN_SECRET_PATTERNS.reduce(
419 (scrubbed, pattern) => scrubbed.replace(pattern, REDACTED_VALUE),
420 value
421 )
422 .split(/(\s+)/u)
423 .map(part => (isHighEntropyToken(part) ? REDACTED_VALUE : part))
424 .join('');
425
426 const scrubSecrets = (value: unknown): unknown => {
427 if (typeof value === 'string') {
428 return scrubSecretsFromString(value);
429 }
430 if (Array.isArray(value)) {
431 return value.map(scrubSecrets);
432 }
433 if (value !== null && typeof value === 'object') {
434 return Object.fromEntries(
435 Object.entries(value as Record<string, unknown>).map(([key, entry]) => [
436 key,
437 scrubSecrets(entry),
438 ])
439 );
440 }
441 return value;
442 };
443
444 const cwdHash = (cwd: string): string => djb2Hash(cwd).toString(36);
445
446 export const getCurrentCwdSessionId = (cwd = process.cwd()) =>
447 Effect.gen(function* () {
448 const paths = yield* getAnalyticsPaths;
449 const state = yield* readOptionalJson<ConsumerShortTermCacheState>(
450 paths.consumerShortTermCachePath
451 );
452 if (!state) {
453 return undefined;
454 }
455
456 const currentCwdHash = cwdHash(cwd);
457 const now = yield* Clock.currentTimeMillis;
458 let best: { id: string; expiresAtMs: number } | undefined;
459
460 for (const entry of Object.values(state)) {
461 const session = entry.probablyMyCliSessionsByCwdHash?.[currentCwdHash];
462 if (!session?.id) continue;
463 const expiresAtMs = Date.parse(session.expiresAt);
464 if (!Number.isFinite(expiresAtMs) || expiresAtMs <= now) continue;
465 if (!best || expiresAtMs > best.expiresAtMs) {
466 best = { id: session.id, expiresAtMs };
467 }
468 }
469
470 return best?.id;
471 }).pipe(Effect.catchCause(() => Effect.succeed(undefined)));
472
473 const withCliSessionId = (event: NonNullable<TrackEvent>, cliSessionId?: string): TrackEvent => ({
474 ...event,
475 properties: {
476 ...(event.properties ?? {}),
477 cli_version: event.properties?.cli_version ?? constants.APP_VERSION,
478 ...(cliSessionId ? { cli_session_id: cliSessionId } : {}),
479 },
480 });
481
482 // A stored login in user_data.json: its key and the backend that issued it. The keychain
483 // modes keep the key out of the file, and then this reports no key to send either.
484 const decodeStoredLogin = Schema.decodeUnknownOption(
485 Schema.Struct({ api_key: Schema.NonEmptyString, base_url: Schema.NonEmptyString })
486 );
487
488 // The same URL the CLI itself talks to, resolved like `ComposioUserContext` does: an env var
489 // that selects a backend, else the stored login's backend, else production.
490 export const readApiBaseUrl = Effect.gen(function* () {
491 const env = yield* Effect.orDie(APP_CONFIG.BACKEND);
492 const envApiKey = yield* Effect.orDie(APP_CONFIG.USER_API_KEY);
493 const stored =
494 envApiKey === undefined
495 ? Option.map(decodeStoredLogin(yield* readUserConfig), login => ({
496 baseURL: login.base_url,
497 webURL: Option.none<string>(),
498 }))
499 : Option.none();
500 return withoutTrailingSlashes(resolveBackend(env, stored).baseURL);
501 });
502
503 const getCliCodactFailuresEndpoint = Effect.map(
504 readApiBaseUrl,
505 baseUrl => `${baseUrl}${CLI_CODACT_FAILURES_PATH}`
506 );
507
508 // Effect's Command processes are scoped and die with their scope; telemetry
509 // workers intentionally outlive the CLI process, so they go through the
510 // sanctioned detached-spawn boundary in src/services/detached-process.
511 // Delivery is best effort: a failed spawn is swallowed, never surfaced.
512 const spawnWorker = (command: string, args: ReadonlyArray<string>) =>
513 Effect.gen(function* () {
514 const debugEnabled = yield* isTelemetryDebugEnabled;
515 const workerArgs = debugEnabled ? [...args, TELEMETRY_DEBUG_FLAG] : args;
516 return yield* spawnDetached(command, workerArgs, { inheritStderr: debugEnabled }).pipe(
517 Effect.as(true),
518 Effect.catchTag('services/DetachedProcessSpawnError', () => Effect.succeed(false))
519 );
520 });
521
522 const buildPostHogBody = (envelope: AnalyticsEnvelope, projectKey: string) => ({
523 api_key: projectKey,
524 event: envelope.event,
525 distinct_id: envelope.distinctId,
526 timestamp: envelope.sentAt,
527 properties: {
528 ...(envelope.properties ?? {}),
529 install_id: envelope.installId,
530 $lib: POSTHOG_LIB,
531 $lib_version: constants.APP_VERSION,
532 },
533 });
534
535 const captureToPostHog = (envelope: AnalyticsEnvelope) =>
536 Effect.gen(function* () {
537 const disabled = yield* telemetryDisabled;
538 const { ingestUrl, projectKey } = yield* getPostHogConfig;
539 if (disabled || projectKey.length === 0) {
540 yield* telemetryDebugLog('posthog_delivery_skipped', {
541 reason: disabled ? 'disabled' : 'missing_project_key',
542 endpoint: ingestUrl,
543 eventName: envelope.event,
544 });
545 return false;
546 }
547
548 const httpClient = yield* HttpClient.HttpClient;
549 const request = yield* HttpClientRequest.post(ingestUrl).pipe(
550 HttpClientRequest.setHeader('x-composio-analytics-source', 'cli'),
551 HttpClientRequest.bodyJson(buildPostHogBody(envelope, projectKey))
552 );
553 const response = yield* httpClient.execute(request);
554 const responseOk = response.status >= 200 && response.status < 300;
555 const debugEnabled = yield* isTelemetryDebugEnabled;
556 const responseBody = !responseOk && debugEnabled ? yield* response.text : undefined;
557
558 yield* telemetryDebugLog(
559 responseOk ? 'posthog_delivery_succeeded' : 'posthog_delivery_failed',
560 {
561 endpoint: ingestUrl,
562 eventName: envelope.event,
563 status: response.status,
564 ok: responseOk,
565 responseBody: responseBody?.slice(0, 1000),
566 }
567 );
568 return responseOk;
569 });
570
571 type CliCodactFailureBody = {
572 failure_type: CliCodactFailureType;
573 tool_info?: CliCodactFailureToolInfo;
574 ctx: Record<string, unknown>;
575 session: Record<string, unknown>;
576 request_id?: string;
577 };
578
579 type CliInvocationContext = {
580 readonly origin?: string;
581 readonly parentRunId?: string;
582 };
583
584 // The COMPOSIO_CLI_PARENT_RUN_ID handshake only exists in processes `composio run` spawned. The
585 // root run process minted its id when the command began (`invocationRunId`), so failures it
586 // reports carry the same run id its children stamp from the environment.
587 const getCliInvocationContext: Effect.Effect<CliInvocationContext> = Effect.gen(function* () {
588 const { invocationOrigin, parentRunId } = yield* cliInvocationContext;
589 const mintedRunId = yield* invocationRunId;
590 return {
591 origin: invocationOrigin,
592 parentRunId: parentRunId ?? Option.getOrUndefined(mintedRunId),
593 };
594 });
595
596 export const createCliCodactFailureBody = (
597 failure: CliCodactFailure,
598 cliSessionId?: string,
599 invocation: CliInvocationContext = {}
600 ): CliCodactFailureBody => ({
601 failure_type: failure.failureType,
602 ...(failure.toolInfo ? { tool_info: failure.toolInfo } : {}),
603 ctx: failure.ctx,
604 session: {
605 source: 'cli',
606 id: cliSessionId,
607 cli_version: constants.APP_VERSION,
608 invocation_origin: invocation.origin ?? 'cli',
609 parent_run_id: invocation.parentRunId,
610 ...(failure.session ?? {}),
611 },
612 ...(failure.requestId ? { request_id: failure.requestId } : {}),
613 });
614
615 const captureToComposioCodactFailures = (failure: CliCodactFailure) =>
616 Effect.gen(function* () {
617 const endpoint = yield* getCliCodactFailuresEndpoint;
618 const disabled = yield* telemetryDisabled;
619 if (disabled) {
620 yield* telemetryDebugLog('codact_delivery_skipped', {
621 reason: 'disabled',
622 endpoint,
623 failureType: failure.failureType,
624 });
625 return;
626 }
627
628 const userApiKey = yield* getUserApiKey;
629 if (!userApiKey) {
630 yield* telemetryDebugLog('codact_delivery_skipped', {
631 reason: 'missing_user_api_key',
632 endpoint,
633 failureType: failure.failureType,
634 });
635 return;
636 }
637
638 const httpClient = yield* HttpClient.HttpClient;
639 const cliSessionId = yield* getCurrentCwdSessionId();
640 const invocation = yield* getCliInvocationContext;
641 const body = createCliCodactFailureBody(failure, cliSessionId, invocation);
642 const request = yield* HttpClientRequest.post(endpoint).pipe(
643 HttpClientRequest.setHeaders(cliRequestHeaders()),
644 HttpClientRequest.setHeader('x-user-api-key', userApiKey),
645 HttpClientRequest.bodyJson(body)
646 );
647 const response = yield* httpClient.execute(request);
648 const responseOk = response.status >= 200 && response.status < 300;
649 const debugEnabled = yield* isTelemetryDebugEnabled;
650 const responseBody = !responseOk && debugEnabled ? yield* response.text : undefined;
651
652 yield* telemetryDebugLog(responseOk ? 'codact_delivery_succeeded' : 'codact_delivery_failed', {
653 endpoint,
654 failureType: failure.failureType,
655 status: response.status,
656 ok: responseOk,
657 responseBody: responseBody?.slice(0, 1000),
658 });
659 });
660
661 const enqueuePostHogEnvelope = (envelope: AnalyticsEnvelope) =>
662 Effect.gen(function* () {
663 const { projectKey } = yield* getPostHogConfig;
664 if (projectKey.length === 0) {
665 yield* telemetryDebugLog('enqueue_skipped', {
666 reason: 'missing_project_key',
667 eventName: envelope.event,
668 });
669 return false;
670 }
671
672 yield* telemetryDebugLog('enqueue', { envelope });
673 const serializedEnvelope = yield* encodeJson(envelope);
674 const encodedPayload = Base64Url.encode(serializedEnvelope);
675 const { command, args } = yield* getDetachedWorkerSpawnArgs(
676 INTERNAL_ANALYTICS_WORKER_FLAG,
677 encodedPayload
678 );
679 return yield* spawnWorker(command, args);
680 });
681
682 export const trackCliEventEffect = (event: TrackEvent) =>
683 Effect.gen(function* () {
684 if (!event) {
685 return;
686 }
687
688 if (!(yield* postHogEnabled)) {
689 yield* telemetryDebugLog('skip', {
690 reason: (yield* telemetryDisabled) ? 'disabled' : 'missing_project_key',
691 eventName: event.name,
692 });
693 return;
694 }
695
696 const cliSessionId = yield* getCurrentCwdSessionId();
697 const enrichedEvent = withCliSessionId(event, cliSessionId);
698 if (!enrichedEvent) {
699 return;
700 }
701
702 const installId = yield* getOrCreateInstallId;
703 const distinctId = yield* getDistinctId(installId);
704 const orgId = yield* getOrgId;
705 const hostEnv = yield* rawHostEnvironment;
706 const sentAt = DateTime.formatIso(yield* DateTime.now);
707 const properties = scrubSecrets({
708 ...(enrichedEvent.properties ?? {}),
709 ...(orgId ? { org_id: orgId } : {}),
710 agent_host_env: detectPluginHost(hostEnv) ?? 'none',
711 }) as Record<string, unknown>;
712 const envelope: AnalyticsEnvelope = {
713 event: enrichedEvent.name,
714 ...(Object.keys(properties).length > 0 ? { properties } : {}),
715 sentAt,
716 source: 'cli',
717 distinctId,
718 installId,
719 };
720 yield* enqueuePostHogEnvelope(envelope);
721 }).pipe(Effect.catchCause(() => Effect.void));
722
723 export const emitPostHogAlias = (installId: string, apolloUserId: string) =>
724 Effect.gen(function* () {
725 if (!(yield* postHogEnabled)) {
726 yield* telemetryDebugLog('alias_skip', { reason: 'disabled_or_missing_key', installId });
727 return false;
728 }
729
730 const sentAt = DateTime.formatIso(yield* DateTime.now);
731 const envelope: AnalyticsEnvelope = {
732 event: POSTHOG_ALIAS_EVENT,
733 properties: {
734 distinct_id: apolloUserId,
735 alias: installId,
736 cli_version: constants.APP_VERSION,
737 },
738 sentAt,
739 source: 'cli',
740 distinctId: apolloUserId,
741 installId,
742 };
743 return yield* enqueuePostHogEnvelope(envelope);
744 }).pipe(Effect.catchCause(() => Effect.succeed(false)));
745
746 export const linkApolloIdentityForAnalytics = (apolloUserId: string, apiKey?: string) =>
747 Effect.gen(function* () {
748 const resolved = typeof apolloUserId === 'string' ? apolloUserId.trim() : '';
749 if (resolved.length === 0) {
750 return;
751 }
752
753 if (!(yield* postHogEnabled)) {
754 yield* telemetryDebugLog('alias_skip', { reason: 'disabled_or_missing_key' });
755 return;
756 }
757
758 const aliasToEmit = yield* withAnalyticsStateLock(
759 Effect.gen(function* () {
760 let state = yield* readAnalyticsState;
761 let installId = yield* getOrCreateInstallIdUnlocked;
762
763 // A second user on this machine must not merge into the previous user's
764 // PostHog person: rotate to a fresh install_id before linking.
765 const previousIdentity =
766 state?.apollo_user_id ??
767 state?.pending_alias_apollo_user_id ??
768 state?.aliased_apollo_user_id;
769 const identityChanged =
770 typeof previousIdentity === 'string' &&
771 previousIdentity.length > 0 &&
772 previousIdentity !== resolved;
773 if (identityChanged) {
774 installId = yield* makeInstallId;
775 yield* mergeAnalyticsStateUnlocked(installId, {
776 install_id: installId,
777 apollo_user_id: undefined,
778 aliased_apollo_user_id: undefined,
779 pending_alias_apollo_user_id: undefined,
780 pending_alias_install_id: undefined,
781 pending_alias_attempted_at: undefined,
782 api_key_fingerprint: undefined,
783 });
784 state = yield* readAnalyticsState;
785 }
786
787 const fingerprint =
788 typeof apiKey === 'string' && apiKey.length > 0
789 ? yield* fingerprintApiKey(apiKey)
790 : yield* getApiKeyFingerprint;
791 if (state?.apollo_user_id !== resolved || state?.api_key_fingerprint !== fingerprint) {
792 yield* mergeAnalyticsStateUnlocked(installId, {
793 apollo_user_id: resolved,
794 ...(fingerprint === null ? {} : { api_key_fingerprint: fingerprint }),
795 });
796 }
797
798 state = yield* readAnalyticsState;
799 if (state?.aliased_apollo_user_id === resolved) {
800 return undefined;
801 }
802 if (
803 state?.pending_alias_apollo_user_id === resolved &&
804 state.pending_alias_install_id === installId
805 ) {
806 const attemptedAt = Date.parse(state.pending_alias_attempted_at ?? '');
807 const now = yield* Clock.currentTimeMillis;
808 if (Number.isFinite(attemptedAt) && now - attemptedAt < PENDING_ALIAS_RETRY_MS) {
809 return undefined;
810 }
811 }
812
813 yield* mergeAnalyticsStateUnlocked(installId, {
814 pending_alias_apollo_user_id: resolved,
815 pending_alias_install_id: installId,
816 pending_alias_attempted_at: DateTime.formatIso(yield* DateTime.now),
817 });
818 return { installId, apolloUserId: resolved };
819 })
820 );
821 if (!aliasToEmit) {
822 return;
823 }
824
825 if (!(yield* emitPostHogAlias(aliasToEmit.installId, aliasToEmit.apolloUserId))) {
826 yield* withAnalyticsStateLock(
827 Effect.gen(function* () {
828 const state = yield* readAnalyticsState;
829 if (
830 state?.pending_alias_install_id !== aliasToEmit.installId ||
831 state.pending_alias_apollo_user_id !== aliasToEmit.apolloUserId
832 ) {
833 return;
834 }
835 yield* mergeAnalyticsStateUnlocked(aliasToEmit.installId, {
836 pending_alias_apollo_user_id: undefined,
837 pending_alias_install_id: undefined,
838 pending_alias_attempted_at: undefined,
839 });
840 })
841 );
842 }
843 }).pipe(Effect.catchCause(() => Effect.void));
844
845 export const clearApolloIdentityForAnalytics = withAnalyticsStateLock(
846 Effect.gen(function* () {
847 const state = yield* readAnalyticsState;
848 if (!state?.install_id || !state.apollo_user_id) {
849 return;
850 }
851 yield* mergeAnalyticsStateUnlocked(state.install_id, {
852 apollo_user_id: undefined,
853 api_key_fingerprint: undefined,
854 });
855 })
856 ).pipe(Effect.catchCause(() => Effect.void));
857
858 export const trackCliCodactFailureEffect = (failure: CliCodactFailure) =>
859 Effect.gen(function* () {
860 const endpoint = yield* getCliCodactFailuresEndpoint;
861 const userApiKey = yield* getUserApiKey;
862 const disabled = yield* telemetryDisabled;
863 if (disabled || !userApiKey) {
864 yield* telemetryDebugLog('codact_skip', {
865 reason: disabled ? 'disabled' : 'missing_user_api_key',
866 failureType: failure.failureType,
867 endpoint,
868 });
869 return;
870 }
871
872 const cliSessionId = yield* getCurrentCwdSessionId();
873 const invocation = yield* getCliInvocationContext;
874 const body = yield* encodeJson(createCliCodactFailureBody(failure, cliSessionId, invocation));
875 const encodedPayload = Base64Url.encode(body);
876 const { command, args } = yield* getDetachedWorkerSpawnArgs(
877 INTERNAL_CODACT_FAILURE_WORKER_FLAG,
878 encodedPayload
879 );
880 yield* spawnWorker(command, args);
881 }).pipe(Effect.catchCause(() => Effect.void));
882
883 const getWorkerFlagIndex = (argv: ReadonlyArray<string>, flag: string): number =>
884 argv.findIndex(token => token === flag);
885
886 export const isBackgroundWorkerInvocation = (argv: ReadonlyArray<string>): boolean =>
887 getWorkerFlagIndex(argv, INTERNAL_ANALYTICS_WORKER_FLAG) >= 0 ||
888 getWorkerFlagIndex(argv, INTERNAL_CODACT_FAILURE_WORKER_FLAG) >= 0;
889
890 const decodeWorkerPayload = <A>(encodedPayload: string) =>
891 Effect.gen(function* () {
892 const serialized = yield* Effect.fromResult(Base64Url.decodeString(encodedPayload));
893 return (yield* decodeJson(serialized)) as A;
894 });
895
896 const settlePostHogAlias = (envelope: AnalyticsEnvelope, delivered: boolean) => {
897 if (envelope.event !== POSTHOG_ALIAS_EVENT) {
898 return Effect.void;
899 }
900
901 return withAnalyticsStateLock(
902 Effect.gen(function* () {
903 const state = yield* readAnalyticsState;
904 if (
905 state?.install_id !== envelope.installId ||
906 state.pending_alias_install_id !== envelope.installId ||
907 state.pending_alias_apollo_user_id !== envelope.distinctId
908 ) {
909 return;
910 }
911
912 yield* mergeAnalyticsStateUnlocked(envelope.installId, {
913 ...(delivered ? { aliased_apollo_user_id: envelope.distinctId } : {}),
914 pending_alias_apollo_user_id: undefined,
915 pending_alias_install_id: undefined,
916 pending_alias_attempted_at: undefined,
917 });
918 })
919 ).pipe(Effect.catchCause(() => Effect.void));
920 };
921
922 const runAnalyticsWorker = (argv: ReadonlyArray<string>) => {
923 const flagIndex = getWorkerFlagIndex(argv, INTERNAL_ANALYTICS_WORKER_FLAG);
924 if (flagIndex < 0) {
925 return Effect.succeed(false);
926 }
927
928 const encodedPayload = argv[flagIndex + 1];
929 if (!encodedPayload) {
930 return Effect.succeed(true);
931 }
932
933 return Effect.gen(function* () {
934 const envelope = yield* decodeWorkerPayload<AnalyticsEnvelope>(encodedPayload);
935 if (typeof envelope?.event !== 'string' || envelope.event.length === 0) {
936 return true;
937 }
938 const delivered = yield* captureToPostHog(envelope).pipe(
939 Effect.catchCause(cause =>
940 telemetryDebugLog('delivery_error', { error: telemetryErrorDetails(cause) }).pipe(
941 Effect.as(false)
942 )
943 )
944 );
945 yield* settlePostHogAlias(envelope, delivered);
946 return true;
947 }).pipe(Effect.catchCause(() => Effect.succeed(true)));
948 };
949
950 const runCodactFailureWorker = (argv: ReadonlyArray<string>) => {
951 const flagIndex = getWorkerFlagIndex(argv, INTERNAL_CODACT_FAILURE_WORKER_FLAG);
952 if (flagIndex < 0) {
953 return Effect.void;
954 }
955
956 const encodedPayload = argv[flagIndex + 1];
957 if (!encodedPayload) {
958 return Effect.void;
959 }
960
961 return Effect.gen(function* () {
962 const body =
963 yield* decodeWorkerPayload<ReturnType<typeof createCliCodactFailureBody>>(encodedPayload);
964 if (
965 body?.failure_type !== 'wrong_tool_slug' &&
966 body?.failure_type !== 'wrong_tool_input_param'
967 ) {
968 return;
969 }
970
971 yield* captureToComposioCodactFailures({
972 failureType: body.failure_type,
973 toolInfo: body.tool_info,
974 ctx: body.ctx,
975 session: body.session,
976 requestId: body.request_id,
977 });
978 }).pipe(
979 Effect.catchCause(cause =>
980 telemetryDebugLog('codact_delivery_error', { error: telemetryErrorDetails(cause) })
981 )
982 );
983 };
984
985 export const runBackgroundWorkerFromArgv = (argv: ReadonlyArray<string>) =>
986 Effect.gen(function* () {
987 const handledAnalytics = yield* runAnalyticsWorker(argv);
988 if (!handledAnalytics) {
989 yield* runCodactFailureWorker(argv);
990 }
991 }).pipe(Effect.catchCause(() => Effect.void));