@devries-cameron20 / composio-1 / commits / ecbd1a33c
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.

fix(cli): address PR review — identity coverage, logout, alias retry; trim comments

Review feedback (KJ): - Strip the added explanatory comments across dispatch.ts, constants.ts, _shared.ts, build-binary.ts and the workflow; keep only pre-existing ones. - Shorten the CHANGELOG entry to a single line. - Revert the .gitignore addition. Bugbot findings: - Already-authenticated installs never stitched (events stayed anonymous after upgrade, since only `login` persisted the id). Backfill now runs from any command that already resolves a session: whoami, init, connected-accounts link, plus the existing login paths. - `composio login --agent` skipped the stitch; the agent branch now resolves session info best-effort and links. - `composio logout` left `apollo_user_id` behind, so later events kept attributing to the previous person. Added clearApolloIdentityForAnalytics. - A failed alias was never retried, because any persisted `apollo_user_id` counted as "already aliased" — including when nothing was ever enqueued (no project key / telemetry off). Track `aliased_apollo_user_id` separately and only record it once the enqueue actually happened. Tests: alias retry after a skipped enqueue, and logout reverting to install_id. Full suite 956 passed. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Kshitij Jhunjhunwala committed Jul 23, 2026 at 13:07 UTC ecbd1a33ce9f0e4c0436e0d30a34567e93a9984c
13 files changed +128 -52
.github/workflows/build-cli-binaries.yml
-3
@@ -133,9 +133,6 @@ jobs:
133 - name: Cross-compile CLI binary
134 working-directory: ts/packages/cli
135 env:
136 - # Public, write-only PostHog project key (project 196278), baked into
137 - # the released binary via `--define`. Not a secret — an Actions
138 - # *variable* so only official builds carry it and forks stay no-op.
136 COMPOSIO_POSTHOG_KEY: ${{ vars.COMPOSIO_POSTHOG_KEY }}
137 run: pnpm build:binary:cross --target ${{ matrix.target }}
138
.gitignore
-1
@@ -73,4 +73,3 @@ docs/.workflow-data/*
73 # Local agent / tooling state (regenerated per machine)
74 .pi/
75 .playwright-mcp/
76 -.claude/plans/
ts/packages/cli/CHANGELOG.md
+1 -1
@@ -2,7 +2,7 @@
2
3 ## Unreleased
4
5 -- Telemetry now delivers directly to PostHog instead of through the Apollo analytics endpoint. This fixes the dark top-of-funnel: pre-login `install`/`setup`/`plugin-setup` events no longer require a logged-in API base URL, so they are captured under the anonymous `install_id`. At login the CLI persists the Apollo `org_member.id` and fires a single `$create_alias`, so post-login events (and all later CLI activity) resolve to the same PostHog person as web onboarding. Existing opt-outs (`COMPOSIO_CLI_TELEMETRY_DISABLED`, `TELEMETRY_DISABLED`, `COMPOSIO_DISABLE_TELEMETRY`, `CI`, `NODE_ENV=test`) are unchanged, and no secrets or tool-argument values are ever sent.
5 +- Telemetry now delivers directly to PostHog, so pre-login `install`/`setup` events are captured. Opt-outs are unchanged.
6
7 ## 0.3.0
8
ts/packages/cli/scripts/_shared.ts
+1 -4
@@ -51,10 +51,7 @@ const copyBundledAcpAdapters = async (outputDir: string): Promise<void> => {
51
52 export const LOCAL_TOOLS_BINARY_ASSET_DIRNAME = 'local-tools-binaries';
53
54 -// Bakes the public PostHog write key from the build-time COMPOSIO_POSTHOG_KEY
55 -// env var into the compiled binary (mirrors the dashboard's NEXT_PUBLIC baking).
56 -// Empty when unset -> telemetry is a no-op. Kept out of --env so it doesn't
57 -// clobber the DEBUG_OVERRIDE_* inline (Bun honors only the last --env).
54 +// Kept out of --env: Bun honors only the last --env, which would clobber DEBUG_OVERRIDE_*.
55 export const posthogBakeArgs = (): ReadonlyArray<string> => {
56 const key = process.env.COMPOSIO_POSTHOG_KEY?.trim();
57 return key ? ['--define', `COMPOSIO_POSTHOG_KEY_BAKED=${JSON.stringify(key)}`] : [];
ts/packages/cli/scripts/build-binary.ts
-2
@@ -18,8 +18,6 @@ export function buildBinary() {
18 const cwd = process.cwd();
19 yield* Effect.logDebug(`Building binary in ${cwd}`);
20
21 - // `--env DEBUG_OVERRIDE_*` inlines matching env vars; the PostHog key is
22 - // baked via `--define` instead (Bun honors only the last `--env`).
21 const args: ReadonlyArray<string> = [
22 'build',
23 './src/bin.ts',
ts/packages/cli/src/analytics/dispatch.ts
+33 -32
@@ -100,8 +100,6 @@ const analyticsDisabled = environmentProvider.load(
100 )
101 );
102
103 -// Direct-to-PostHog target; env overrides fall back to the embedded constants.
104 -// No dependency on the API base URL, so pre-login events can send.
103 const getPostHogConfig = environmentProvider
104 .load(
105 Config.all({
@@ -171,11 +169,12 @@ const readOptionalJson = <A>(filePath: string) =>
169 return (yield* decodeJson(raw)) as A;
170 }).pipe(Effect.catchAll(() => Effect.succeed(undefined)));
171
174 -// `install_id` = anonymous device UUID; `apollo_user_id` = Apollo `org_member.id`
175 -// persisted at login, used as the PostHog identity post-login.
172 type AnalyticsState = {
173 readonly install_id?: string;
174 readonly apollo_user_id?: string;
175 + // Tracked separately from apollo_user_id so an alias that was never enqueued
176 + // (no project key, telemetry off) is retried instead of blocked forever.
177 + readonly aliased_apollo_user_id?: string;
178 readonly created_at?: string;
179 };
180
@@ -216,8 +215,7 @@ const readApolloUserId = Effect.map(readAnalyticsState, state =>
215 : null
216 );
217
219 -// Persists the Apollo `org_member.id`, preserving `install_id`/`created_at`.
220 -const persistApolloUserId = (installId: string, apolloUserId: string) =>
218 +const mergeAnalyticsState = (installId: string, patch: Partial<AnalyticsState>) =>
219 Effect.gen(function* () {
220 const fs = yield* FileSystem.FileSystem;
221 const paths = yield* getAnalyticsPaths;
@@ -227,12 +225,11 @@ const persistApolloUserId = (installId: string, apolloUserId: string) =>
225 .pipe(Effect.catchAll(() => Effect.void));
226
227 const state = yield* readOptionalJson<AnalyticsState>(paths.analyticsStatePath);
230 - const createdAt = state?.created_at ?? DateTime.formatIso(yield* DateTime.now);
228 const contents = yield* encodeJson({
229 ...(state ?? {}),
230 install_id: state?.install_id ?? installId,
234 - apollo_user_id: apolloUserId,
235 - created_at: createdAt,
231 + created_at: state?.created_at ?? DateTime.formatIso(yield* DateTime.now),
232 + ...patch,
233 });
234 yield* fs.writeFileString(paths.analyticsStatePath, contents);
235 }).pipe(Effect.catchAll(() => Effect.void));
@@ -256,8 +253,6 @@ const getUserApiKey = Effect.gen(function* () {
253 : null;
254 });
255
259 -// Post-login: the Apollo `org_member.id`. Pre-login: the raw `install_id` (the
260 -// login-time `$create_alias` merges this exact value into the Apollo person).
256 const getDistinctId = (installId: string) =>
257 Effect.map(readApolloUserId, apolloUserId => apolloUserId ?? installId);
258
@@ -351,8 +346,6 @@ const spawnWorker = (command: string, args: ReadonlyArray<string>) =>
346 );
347 });
348
354 -// Shapes an envelope into a PostHog capture body; the public project key
355 -// authenticates the ingest in-band (no user API key attached).
349 const buildPostHogBody = (envelope: AnalyticsEnvelope, projectKey: string) => ({
350 api_key: projectKey,
351 event: envelope.event,
@@ -492,9 +485,6 @@ const captureToComposioCodactFailures = (failure: CliCodactFailure) =>
485 });
486 });
487
495 -// Hands an envelope to the detached best-effort worker for PostHog delivery.
496 -// Short-circuits when no project key is configured (the default for OSS forks /
497 -// local builds) so we never spawn a worker that would only no-op.
488 const enqueuePostHogEnvelope = (envelope: AnalyticsEnvelope) =>
489 Effect.gen(function* () {
490 const { projectKey } = yield* getPostHogConfig;
@@ -503,7 +493,7 @@ const enqueuePostHogEnvelope = (envelope: AnalyticsEnvelope) =>
493 reason: 'missing_project_key',
494 eventName: envelope.event,
495 });
506 - return;
496 + return false;
497 }
498
499 yield* telemetryDebugLog('enqueue', { envelope });
@@ -514,6 +504,7 @@ const enqueuePostHogEnvelope = (envelope: AnalyticsEnvelope) =>
504 encodedPayload
505 );
506 yield* spawnWorker(command, args);
507 + return true;
508 });
509
510 export const trackCliEventEffect = (event: TrackEvent) =>
@@ -548,13 +539,12 @@ export const trackCliEventEffect = (event: TrackEvent) =>
539 yield* enqueuePostHogEnvelope(envelope);
540 }).pipe(Effect.catchAllCause(() => Effect.void));
541
551 -// Fires one `$create_alias` merging the `install_id` device into the Apollo person.
542 export const emitPostHogAlias = (installId: string, apolloUserId: string) =>
543 Effect.gen(function* () {
544 const disabled = yield* analyticsDisabled;
545 if (disabled) {
546 yield* telemetryDebugLog('alias_skip', { reason: 'disabled', installId });
557 - return;
547 + return false;
548 }
549
550 const sentAt = DateTime.formatIso(yield* DateTime.now);
@@ -571,11 +561,14 @@ export const emitPostHogAlias = (installId: string, apolloUserId: string) =>
561 distinctId: apolloUserId,
562 installId,
563 };
574 - yield* enqueuePostHogEnvelope(envelope);
575 - }).pipe(Effect.catchAllCause(() => Effect.void));
576 -
577 -// Called at login: persist the Apollo identity and, on first association, alias
578 -// the anonymous install into it. Non-fatal so login never fails on telemetry.
564 + return yield* enqueuePostHogEnvelope(envelope);
565 + }).pipe(Effect.catchAllCause(() => Effect.succeed(false)));
566 +
567 +/**
568 + * Records the Apollo `org_member.id` as the analytics identity and stitches the
569 + * anonymous install into it. Safe to call from any command that already resolved
570 + * a session, which is how already-authenticated installs get backfilled.
571 + */
572 export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
573 Effect.gen(function* () {
574 const resolved = typeof apolloUserId === 'string' ? apolloUserId.trim() : '';
@@ -583,24 +576,32 @@ export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
576 return;
577 }
578
586 - // Disabled telemetry writes nothing — not even the local identity file.
579 const disabled = yield* analyticsDisabled;
580 if (disabled) {
581 yield* telemetryDebugLog('alias_skip', { reason: 'disabled' });
582 return;
583 }
584
593 - const existing = yield* readApolloUserId;
585 + const state = yield* readAnalyticsState;
586 const installId = yield* getOrCreateInstallId;
595 - yield* persistApolloUserId(installId, resolved);
596 - // Alias only the first time this install is tied to an Apollo user: a repeat
597 - // login is redundant, and PostHog rejects re-aliasing one install into a
598 - // second account, so skip once an id has already been persisted.
599 - if (existing === null) {
600 - yield* emitPostHogAlias(installId, resolved);
587 + if (state?.apollo_user_id !== resolved) {
588 + yield* mergeAnalyticsState(installId, { apollo_user_id: resolved });
589 + }
590 +
591 + if (state?.aliased_apollo_user_id === resolved) {
592 + return;
593 + }
594 + if (yield* emitPostHogAlias(installId, resolved)) {
595 + yield* mergeAnalyticsState(installId, { aliased_apollo_user_id: resolved });
596 }
597 }).pipe(Effect.catchAllCause(() => Effect.void));
598
599 +/** Drops the Apollo identity on logout so later events revert to `install_id`. */
600 +export const clearApolloIdentityForAnalytics = Effect.gen(function* () {
601 + const installId = yield* getOrCreateInstallId;
602 + yield* mergeAnalyticsState(installId, { apollo_user_id: undefined });
603 +}).pipe(Effect.catchAllCause(() => Effect.void));
604 +
605 export const trackCliCodactFailureEffect = (failure: CliCodactFailure) =>
606 Effect.gen(function* () {
607 const endpoint = yield* getCliCodactFailuresEndpoint;
ts/packages/cli/src/commands/connected-accounts/commands/connected-accounts.link.cmd.ts
+5
@@ -9,6 +9,7 @@ import { resolveToolRouterSession } from 'src/effects/create-tool-router-session
9 import { extractMessage, extractSlug } from 'src/utils/api-error-extraction';
10 import { ProjectContext } from 'src/services/project-context';
11 import { ComposioClientSingleton, getSessionInfoByUserApiKey } from 'src/services/composio-clients';
12 +import { linkApolloIdentityForAnalytics } from 'src/analytics/dispatch';
13 import {
14 formatResolveCommandProjectError,
15 resolveCommandProject,
@@ -240,6 +241,10 @@ const handleNoManagedAuth = (ui: TerminalUI, toolkitSlug: string, noBrowser: boo
241 userApiKey: apiKey,
242 }).pipe(Effect.catchAll(() => Effect.succeed(null)));
243
244 + if (sessionInfo) {
245 + yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id);
246 + }
247 +
248 if (sessionInfo?.project.org.name) {
249 orgName = sessionInfo.project.org.name;
250 }
ts/packages/cli/src/commands/init.cmd.ts
+2
@@ -11,6 +11,7 @@ import {
11 listOrgProjects,
12 type OrgProject,
13 } from 'src/services/composio-clients';
14 +import { linkApolloIdentityForAnalytics } from 'src/analytics/dispatch';
15 import * as constants from 'src/constants';
16 import { TerminalUI } from 'src/services/terminal-ui';
17 import { browserLogin, noBrowser as noBrowserOpt } from 'src/commands/login.cmd';
@@ -125,6 +126,7 @@ const ensureProjectApiKeyInEnv = (params: { cwd: string; selected: ProjectKeys }
126 orgId: selected.orgId,
127 projectId: selected.projectId,
128 });
129 + yield* linkApolloIdentityForAnalytics(sessionInfo.org_member.id);
130
131 let projectApiKey = sessionInfo.api_key?.api_key ?? sessionInfo.api_key?.key ?? null;
132 if (!projectApiKey && !hasProjectApiKey) {
ts/packages/cli/src/commands/login.cmd.ts
+11
@@ -891,6 +891,17 @@ export const loginCmd = Command.make(
891 yield* ensureAgentSignupAllowed;
892 const identity = yield* getOrSignupReadyAgent();
893 yield* loginWithAgentIdentity(identity);
894 + const agentUserApiKey = identity.composio?.user_api_key;
895 + if (agentUserApiKey) {
896 + const agentSessionInfo = yield* getSessionInfoByUserApiKey({
897 + baseURL: ctx.data.baseURL,
898 + userApiKey: agentUserApiKey,
899 + orgId: identity.composio?.org_id ?? undefined,
900 + }).pipe(Effect.catchAll(() => Effect.succeed(null)));
901 + if (agentSessionInfo) {
902 + yield* linkApolloIdentityForAnalytics(agentSessionInfo.org_member.id);
903 + }
904 + }
905 const summary = safeAgentSummary(identity);
906 yield* ui.log.success(
907 `Logged in as Composio agent ${summary.email ?? summary.slug ?? ''}`
ts/packages/cli/src/commands/logout.cmd.ts
+2
@@ -7,6 +7,7 @@ import {
7 } from 'src/services/agents';
8 import { ComposioUserContext } from 'src/services/user-context';
9 import { TerminalUI } from 'src/services/terminal-ui';
10 +import { clearApolloIdentityForAnalytics } from 'src/analytics/dispatch';
11
12 /**
13 * CLI command to log out from the Composio CLI.
@@ -54,6 +55,7 @@ export const logoutCmd = Command.make('logout', { force }, ({ force }) =>
55 }
56
57 yield* ctx.logout;
58 + yield* clearApolloIdentityForAnalytics;
59
60 if (Option.isSome(loggedInAgent)) {
61 yield* removeStoredAgentIdentity;
ts/packages/cli/src/commands/whoami.cmd.ts
+5
@@ -1,6 +1,7 @@
1 import { Command } from '@effect/cli';
2 import { Effect, Option } from 'effect';
3 import { getSessionInfoByUserApiKey } from 'src/services/composio-clients';
4 +import { linkApolloIdentityForAnalytics } from 'src/analytics/dispatch';
5 import { ComposioUserContext } from 'src/services/user-context';
6 import { TerminalUI } from 'src/services/terminal-ui';
7 import { commandHintStep } from 'src/services/command-hints';
@@ -34,6 +35,10 @@ export const whoamiCmd = Command.make('whoami', {}).pipe(
35 // Reflect the org selected via `composio orgs switch`, not the key's home org.
36 orgId: Option.getOrUndefined(ctx.data.orgId),
37 }).pipe(Effect.option);
38 + yield* Option.match(sessionInfo, {
39 + onNone: () => Effect.void,
40 + onSome: info => linkApolloIdentityForAnalytics(info.org_member.id),
41 + });
42 const email = Option.map(sessionInfo, info => info.org_member.email).pipe(
43 Option.getOrUndefined
44 );
ts/packages/cli/src/constants.ts
+2 -9
@@ -78,21 +78,14 @@ export const PROJECT_ENV_FILE_NAME = '.env';
78 export const PROJECT_COMPOSIO_DIR = '.composio';
79
80 /**
81 - * PostHog single-event capture endpoint for direct CLI telemetry. Overridable
82 - * via `COMPOSIO_POSTHOG_INGEST_URL` (tests / staging).
81 + * PostHog capture endpoint for CLI telemetry.
82 */
83 export const COMPOSIO_POSTHOG_INGEST_URL = 'https://us.i.posthog.com/i/v0/e/';
84
86 -// Injected at build time from the COMPOSIO_POSTHOG_KEY env var via `--define`
87 -// (see scripts/_shared.ts `posthogBakeArgs`); undefined in dev/test.
85 declare const COMPOSIO_POSTHOG_KEY_BAKED: string | undefined;
86
87 /**
91 - * Public, write-only PostHog project write key (project 196278). Client-
92 - * embeddable by design — it can only ingest events, never read them — same as
93 - * the dashboard's `NEXT_PUBLIC_POSTHOG_TOKEN`. Sourced from the build-time
94 - * bake, overridable at runtime via `COMPOSIO_POSTHOG_KEY`. Empty -> telemetry
95 - * is a no-op.
88 + * Public write-only PostHog project key, baked at build from `COMPOSIO_POSTHOG_KEY`.
89 */
90 export const COMPOSIO_POSTHOG_PROJECT_KEY =
91 typeof COMPOSIO_POSTHOG_KEY_BAKED === 'string' ? COMPOSIO_POSTHOG_KEY_BAKED : '';
ts/packages/cli/test/src/analytics.dispatch.test.ts
+66
@@ -5,6 +5,7 @@ import { BunFileSystem, BunPath } from '@effect/platform-bun';
5 import { Effect, Layer } from 'effect';
6 import * as tempy from 'tempy';
7 import {
8 + clearApolloIdentityForAnalytics,
9 emitPostHogAlias,
10 getCurrentCwdSessionId,
11 linkApolloIdentityForAnalytics,
@@ -502,6 +503,71 @@ describe('CLI analytics dispatch', () => {
503 }).pipe(Effect.provide(makePlatformLayer(home)));
504 });
505
506 + it.effect('retries the alias when the first attempt could not be enqueued', () => {
507 + const home = tempy.temporaryDirectory();
508 + const scriptPath = `${home}/composio.ts`;
509 + enableTelemetry();
510 + vi.stubEnv('COMPOSIO_POSTHOG_KEY', '');
511 + process.argv[1] = scriptPath;
512 +
513 + return Effect.gen(function* () {
514 + const fs = yield* FileSystem.FileSystem;
515 + const path = yield* Path.Path;
516 + yield* fs.writeFileString(scriptPath, '');
517 +
518 + // No key -> nothing enqueued, so the alias must not be marked as sent.
519 + yield* linkApolloIdentityForAnalytics('om_apollo_retry');
520 + expect(childProcessMocks.spawn).not.toHaveBeenCalled();
521 + const afterSkip = JSON.parse(
522 + yield* fs.readFileString(path.join(home, '.composio', 'analytics.json'), 'utf8')
523 + ) as { apollo_user_id?: string; aliased_apollo_user_id?: string };
524 + expect(afterSkip.apollo_user_id).toBe('om_apollo_retry');
525 + expect(afterSkip.aliased_apollo_user_id).toBeUndefined();
526 +
527 + // Once a key is configured the alias is attempted again.
528 + vi.stubEnv('COMPOSIO_POSTHOG_KEY', 'phc_test_key');
529 + yield* linkApolloIdentityForAnalytics('om_apollo_retry');
530 + expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
531 + const afterRetry = JSON.parse(
532 + yield* fs.readFileString(path.join(home, '.composio', 'analytics.json'), 'utf8')
533 + ) as { aliased_apollo_user_id?: string };
534 + expect(afterRetry.aliased_apollo_user_id).toBe('om_apollo_retry');
535 + }).pipe(Effect.provide(makePlatformLayer(home)));
536 + });
537 +
538 + it.effect('clears the Apollo identity on logout so events revert to install_id', () => {
539 + const home = tempy.temporaryDirectory();
540 + const scriptPath = `${home}/composio.ts`;
541 + enableTelemetry('');
542 + process.argv[1] = scriptPath;
543 +
544 + return Effect.gen(function* () {
545 + const fs = yield* FileSystem.FileSystem;
546 + const path = yield* Path.Path;
547 + yield* fs.writeFileString(scriptPath, '');
548 + const composioDir = path.join(home, '.composio');
549 + yield* fs.makeDirectory(composioDir, { recursive: true });
550 + yield* fs.writeFileString(
551 + path.join(composioDir, 'analytics.json'),
552 + JSON.stringify({ install_id: 'install_logout', apollo_user_id: 'om_apollo_gone' })
553 + );
554 +
555 + yield* clearApolloIdentityForAnalytics;
556 +
557 + const persisted = JSON.parse(
558 + yield* fs.readFileString(path.join(composioDir, 'analytics.json'), 'utf8')
559 + ) as { install_id: string; apollo_user_id?: string };
560 + expect(persisted.install_id).toBe('install_logout');
561 + expect(persisted.apollo_user_id).toBeUndefined();
562 +
563 + // Later events fall back to the anonymous install identity.
564 + yield* trackCliEventEffect({ name: 'producer_event' });
565 + const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
566 + const payload = decodeWorkerPayload<{ distinctId: string }>(args[2]!);
567 + expect(payload.distinctId).toBe('install_logout');
568 + }).pipe(Effect.provide(makePlatformLayer(home)));
569 + });
570 +
571 it.effect('delivers codact worker failures with the user key and session body', () => {
572 const home = tempy.temporaryDirectory();
573 const failureBody = {