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.

chore(cli): trim telemetry comments; bake PostHog key at build (not source)

Kshitij Jhunjhunwala96516e3dd5ed
diff --git a/.github/workflows/build-cli-binaries.yml b/.github/workflows/build-cli-binaries.yml
index e3bb9bfe5..a3c18d537 100644
--- a/.github/workflows/build-cli-binaries.yml
+++ b/.github/workflows/build-cli-binaries.yml
@@ -132,6 +132,11 @@ jobs:
- name: Cross-compile CLI binary
working-directory: ts/packages/cli
+ env:
+ # Public, write-only PostHog project key (project 196278), baked into
+ # the released binary via `--define`. Not a secret — an Actions
+ # *variable* so only official builds carry it and forks stay no-op.
+ COMPOSIO_POSTHOG_KEY: ${{ vars.COMPOSIO_POSTHOG_KEY }}
run: pnpm build:binary:cross --target ${{ matrix.target }}
- name: Verify binary
diff --git a/ts/packages/cli/scripts/_shared.ts b/ts/packages/cli/scripts/_shared.ts
index 99f743598..776460405 100644
--- a/ts/packages/cli/scripts/_shared.ts
+++ b/ts/packages/cli/scripts/_shared.ts
@@ -51,6 +51,15 @@ const copyBundledAcpAdapters = async (outputDir: string): Promise<void> => {
export const LOCAL_TOOLS_BINARY_ASSET_DIRNAME = 'local-tools-binaries';
+// Bakes the public PostHog write key from the build-time COMPOSIO_POSTHOG_KEY
+// env var into the compiled binary (mirrors the dashboard's NEXT_PUBLIC baking).
+// Empty when unset -> telemetry is a no-op. Kept out of --env so it doesn't
+// clobber the DEBUG_OVERRIDE_* inline (Bun honors only the last --env).
+export const posthogBakeArgs = (): ReadonlyArray<string> => {
+ const key = process.env.COMPOSIO_POSTHOG_KEY?.trim();
+ return key ? ['--define', `COMPOSIO_POSTHOG_KEY_BAKED=${JSON.stringify(key)}`] : [];
+};
+
const localToolsBinaryAssetsSourceDir = (): string =>
path.resolve(process.cwd(), '../cli-local-tools', LOCAL_TOOLS_BINARY_ASSET_DIRNAME);
diff --git a/ts/packages/cli/scripts/build-all-binaries.ts b/ts/packages/cli/scripts/build-all-binaries.ts
index 9636c8eba..95a092f63 100644
--- a/ts/packages/cli/scripts/build-all-binaries.ts
+++ b/ts/packages/cli/scripts/build-all-binaries.ts
@@ -15,7 +15,12 @@
import { Config, ConfigProvider, Console, Effect, Stream, Logger, Layer, LogLevel } from 'effect';
import { Command } from '@effect/platform';
import { BunContext, BunRuntime } from '@effect/platform-bun';
-import { buildCompanionModules, copyLocalToolBinaryAssets, teardown } from './_shared';
+import {
+ buildCompanionModules,
+ copyLocalToolBinaryAssets,
+ posthogBakeArgs,
+ teardown,
+} from './_shared';
import { BinaryBuildError } from './build-error';
/**
@@ -30,21 +35,21 @@ const TARGETS = [
function runBunBuild(target: string, outfile: string) {
return Effect.gen(function* () {
- const args = [
- 'bun',
+ const args: ReadonlyArray<string> = [
'build',
'./src/bin.ts',
'--env',
'DEBUG_OVERRIDE_*',
+ ...posthogBakeArgs(),
'--compile',
'--production',
'--target',
target,
'--outfile',
outfile,
- ] as const satisfies ReadonlyArray<string>;
+ ];
- const cmd = Command.make(...args);
+ const cmd = Command.make('bun', ...args);
const { exitCode } = yield* cmd.pipe(
Command.start,
diff --git a/ts/packages/cli/scripts/build-binary-cross.ts b/ts/packages/cli/scripts/build-binary-cross.ts
index 2b316385f..eedde04c5 100644
--- a/ts/packages/cli/scripts/build-binary-cross.ts
+++ b/ts/packages/cli/scripts/build-binary-cross.ts
@@ -18,7 +18,12 @@ import process from 'node:process';
import { Config, ConfigProvider, Console, Effect, Stream, Logger, Layer, LogLevel } from 'effect';
import { Command } from '@effect/platform';
import { BunContext, BunRuntime } from '@effect/platform-bun';
-import { buildCompanionModules, copyLocalToolBinaryAssets, teardown } from './_shared';
+import {
+ buildCompanionModules,
+ copyLocalToolBinaryAssets,
+ posthogBakeArgs,
+ teardown,
+} from './_shared';
import { BinaryBuildError } from './build-error';
/**
@@ -61,21 +66,21 @@ export function buildBinaryCross() {
const outfile = `./dist/binaries/${artifact}`;
- const args = [
- 'bun',
+ const args: ReadonlyArray<string> = [
'build',
'./src/bin.ts',
'--env',
'DEBUG_OVERRIDE_*',
+ ...posthogBakeArgs(),
'--compile',
'--production',
'--target',
target,
'--outfile',
outfile,
- ] as const satisfies ReadonlyArray<string>;
+ ];
- const cmd = Command.make(...args);
+ const cmd = Command.make('bun', ...args);
yield* Effect.logDebug('Running build command with', args.join(' '), '');
diff --git a/ts/packages/cli/scripts/build-binary.ts b/ts/packages/cli/scripts/build-binary.ts
index 31179a414..a62572b08 100644
--- a/ts/packages/cli/scripts/build-binary.ts
+++ b/ts/packages/cli/scripts/build-binary.ts
@@ -2,7 +2,12 @@ import process from 'node:process';
import { Config, ConfigProvider, Console, Effect, Stream, Logger, Layer, LogLevel } from 'effect';
import { Command } from '@effect/platform';
import { BunContext, BunRuntime } from '@effect/platform-bun';
-import { buildCompanionModules, copyLocalToolBinaryAssets, teardown } from './_shared';
+import {
+ buildCompanionModules,
+ copyLocalToolBinaryAssets,
+ posthogBakeArgs,
+ teardown,
+} from './_shared';
import { BinaryBuildError } from './build-error';
/**
@@ -13,34 +18,21 @@ export function buildBinary() {
const cwd = process.cwd();
yield* Effect.logDebug(`Building binary in ${cwd}`);
- const args = [
- 'bun',
- /**
- * Transpile and bundle the CLI app.
- */
+ // `--env DEBUG_OVERRIDE_*` inlines matching env vars; the PostHog key is
+ // baked via `--define` instead (Bun honors only the last `--env`).
+ const args: ReadonlyArray<string> = [
'build',
'./src/bin.ts',
-
- /**
- * Statically inline any environment variable that matches `DEBUG_OVERRIDE_*`.
- */
'--env',
'DEBUG_OVERRIDE_*',
-
- /**
- * Generate a standalone Bun executable containing your bundled code.
- */
+ ...posthogBakeArgs(),
'--compile',
'--production',
-
- /**
- * Output file destination.
- */
'--outfile',
'./dist/composio',
- ] as const satisfies ReadonlyArray<string>;
+ ];
- const cmd = Command.make(...args);
+ const cmd = Command.make('bun', ...args);
yield* Effect.logDebug('Running build command with', args.join(' '), '');
diff --git a/ts/packages/cli/src/analytics/dispatch.ts b/ts/packages/cli/src/analytics/dispatch.ts
index 11d12f4b3..c060e9f0e 100644
--- a/ts/packages/cli/src/analytics/dispatch.ts
+++ b/ts/packages/cli/src/analytics/dispatch.ts
@@ -25,8 +25,6 @@ const ANALYTICS_STATE_FILE_NAME = 'analytics.json';
const CONSUMER_SHORT_TERM_CACHE_FILE_NAME = 'consumer-short-term-cache.json';
const CLI_CODACT_FAILURES_PATH = '/api/v3/cli/codact_failures';
-// PostHog's single-event capture endpoint expects `$create_alias` to merge an
-// anonymous person into an identified one (`properties.alias` -> `distinct_id`).
const POSTHOG_ALIAS_EVENT = '$create_alias';
const POSTHOG_LIB = 'composio-cli';
@@ -102,10 +100,8 @@ const analyticsDisabled = environmentProvider.load(
)
);
-// Resolves the direct-to-PostHog transport target. Both fields fall back to the
-// embedded constants; env overrides exist for tests and staging. Crucially this
-// does NOT depend on the Composio API base URL (which is only written at login),
-// so pre-login install/setup events can be delivered.
+// Direct-to-PostHog target; env overrides fall back to the embedded constants.
+// No dependency on the API base URL, so pre-login events can send.
const getPostHogConfig = environmentProvider
.load(
Config.all({
@@ -175,9 +171,8 @@ const readOptionalJson = <A>(filePath: string) =>
return (yield* decodeJson(raw)) as A;
}).pipe(Effect.catchAll(() => Effect.succeed(undefined)));
-// Persisted `~/.composio/analytics.json`. `install_id` is the anonymous device
-// UUID; `apollo_user_id` (the Apollo `org_member.id`, persisted at login) becomes
-// the PostHog identity so CLI events join the same person as web onboarding.
+// `install_id` = anonymous device UUID; `apollo_user_id` = Apollo `org_member.id`
+// persisted at login, used as the PostHog identity post-login.
type AnalyticsState = {
readonly install_id?: string;
readonly apollo_user_id?: string;
@@ -221,9 +216,7 @@ const readApolloUserId = Effect.map(readAnalyticsState, state =>
: null
);
-// Persists the Apollo `org_member.id` into the analytics state, preserving the
-// existing `install_id`/`created_at` so the anonymous device identity is kept
-// for the login-time alias. Best-effort: never surfaces or throws.
+// Persists the Apollo `org_member.id`, preserving `install_id`/`created_at`.
const persistApolloUserId = (installId: string, apolloUserId: string) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
@@ -263,10 +256,8 @@ const getUserApiKey = Effect.gen(function* () {
: null;
});
-// Post-login events key on the Apollo `org_member.id` (persisted at login) so
-// they resolve to the same PostHog person as web onboarding. Pre-login, the raw
-// `install_id` is the distinct id (NOT wrapped as `anon_<id>` — the login-time
-// `$create_alias` merges this exact value into the Apollo person).
+// Post-login: the Apollo `org_member.id`. Pre-login: the raw `install_id` (the
+// login-time `$create_alias` merges this exact value into the Apollo person).
const getDistinctId = (installId: string) =>
Effect.map(readApolloUserId, apolloUserId => apolloUserId ?? installId);
@@ -360,11 +351,8 @@ const spawnWorker = (command: string, args: ReadonlyArray<string>) =>
);
});
-// Shapes an analytics envelope into a PostHog single-event capture body. The
-// public project write key authenticates the ingest in-band (no user API key),
-// which is what lets pre-login events send. Only allowlisted, already-shaped
-// properties from the event builders flow through here — no secrets or tool-arg
-// values (see events.ts).
+// Shapes an envelope into a PostHog capture body. The public project key
+// authenticates the ingest in-band — no user API key, so pre-login events send.
const buildPostHogBody = (envelope: AnalyticsEnvelope, projectKey: string) => ({
api_key: projectKey,
event: envelope.event,
@@ -504,8 +492,7 @@ const captureToComposioCodactFailures = (failure: CliCodactFailure) =>
});
});
-// Encodes an envelope and hands it to the detached best-effort worker, which
-// delivers it directly to PostHog. Shared by event tracking and login aliasing.
+// Hands an envelope to the detached best-effort worker for PostHog delivery.
const enqueuePostHogEnvelope = (envelope: AnalyticsEnvelope) =>
Effect.gen(function* () {
yield* telemetryDebugLog('enqueue', { envelope });
@@ -524,9 +511,6 @@ export const trackCliEventEffect = (event: TrackEvent) =>
return;
}
- // The direct PostHog transport carries a fixed endpoint + embedded public
- // key, so — unlike the old Apollo path — there is no base-URL gate here and
- // pre-login install/setup events are delivered.
const disabled = yield* analyticsDisabled;
if (disabled) {
yield* telemetryDebugLog('skip', { reason: 'disabled', eventName: event.name });
@@ -553,8 +537,7 @@ export const trackCliEventEffect = (event: TrackEvent) =>
yield* enqueuePostHogEnvelope(envelope);
}).pipe(Effect.catchAllCause(() => Effect.void));
-// Fires a single PostHog `$create_alias` merging the anonymous `install_id`
-// device person into the identified Apollo `org_member.id` person. Best-effort.
+// Fires one `$create_alias` merging the `install_id` device into the Apollo person.
export const emitPostHogAlias = (installId: string, apolloUserId: string) =>
Effect.gen(function* () {
const disabled = yield* analyticsDisabled;
@@ -567,7 +550,7 @@ export const emitPostHogAlias = (installId: string, apolloUserId: string) =>
const envelope: AnalyticsEnvelope = {
event: POSTHOG_ALIAS_EVENT,
properties: {
- // PostHog merges `properties.alias` into the event's `distinct_id`.
+ // PostHog merges `alias` into `distinct_id`.
distinct_id: apolloUserId,
alias: installId,
cli_version: constants.APP_VERSION,
@@ -580,9 +563,8 @@ export const emitPostHogAlias = (installId: string, apolloUserId: string) =>
yield* enqueuePostHogEnvelope(envelope);
}).pipe(Effect.catchAllCause(() => Effect.void));
-// Called at login: persists the Apollo `org_member.id` as the analytics identity
-// and stitches the pre-login anonymous device into that person via one alias.
-// Fully non-fatal so login never fails on telemetry.
+// Called at login: persist the Apollo identity and alias the device into it.
+// Non-fatal so login never fails on telemetry.
export const linkApolloIdentityForAnalytics = (apolloUserId: string) =>
Effect.gen(function* () {
if (typeof apolloUserId !== 'string' || apolloUserId.trim().length === 0) {
diff --git a/ts/packages/cli/src/constants.ts b/ts/packages/cli/src/constants.ts
index 92353cb50..e855a88c5 100644
--- a/ts/packages/cli/src/constants.ts
+++ b/ts/packages/cli/src/constants.ts
@@ -78,28 +78,24 @@ export const PROJECT_ENV_FILE_NAME = '.env';
export const PROJECT_COMPOSIO_DIR = '.composio';
/**
- * PostHog ingest endpoint the CLI sends telemetry directly to.
- *
- * This is the public single-event capture API. It does not depend on the
- * Composio API base URL, so pre-login install/setup events can be delivered
- * before the user has ever authenticated. Overridable via the
- * `COMPOSIO_POSTHOG_INGEST_URL` environment variable (used by tests / staging).
+ * PostHog single-event capture endpoint for direct CLI telemetry. Overridable
+ * via `COMPOSIO_POSTHOG_INGEST_URL` (tests / staging).
*/
export const COMPOSIO_POSTHOG_INGEST_URL = 'https://us.i.posthog.com/i/v0/e/';
+// Injected at build time from the COMPOSIO_POSTHOG_KEY env var via `--define`
+// (see scripts/_shared.ts `posthogBakeArgs`); undefined in dev/test.
+declare const COMPOSIO_POSTHOG_KEY_BAKED: string | undefined;
+
/**
- * Public, write-only PostHog project write key for project 196278
- * ("Composio Platform"). This is client-embeddable by design (same trust model
- * as the dashboard JS bundle): it can only ingest events, never read them.
- *
- * Overridable via the `COMPOSIO_POSTHOG_KEY` environment variable.
- *
- * TODO(kj): fill in the real public `phc_*` project-196278 write key from the
- * PostHog project settings. While this is empty, direct PostHog delivery is a
- * safe no-op (the transport skips when no key is configured). Do NOT commit a
- * real token into tests or fixtures.
+ * Public, write-only PostHog project write key (project 196278). Client-
+ * embeddable by design — it can only ingest events, never read them — same as
+ * the dashboard's `NEXT_PUBLIC_POSTHOG_TOKEN`. Sourced from the build-time
+ * bake, overridable at runtime via `COMPOSIO_POSTHOG_KEY`. Empty -> telemetry
+ * is a no-op.
*/
-export const COMPOSIO_POSTHOG_PROJECT_KEY = '';
+export const COMPOSIO_POSTHOG_PROJECT_KEY =
+ typeof COMPOSIO_POSTHOG_KEY_BAKED === 'string' ? COMPOSIO_POSTHOG_KEY_BAKED : '';
/**
* GitHub repository information for release fetching