Skip to content

Conversation

@gtrrz-victor
Copy link
Contributor

@gtrrz-victor gtrrz-victor commented Jan 28, 2026

Avoids blocking the main thread just to send telemetry.
Right now, depending on the region, PostHog may take around 1 second to send events.
If we spin up a new process that sends telemetry in detached mode, we make sure that:

  1. we don't block the main thread, and
  2. we always send telemetry if enabled.

Note

Medium Risk
Changes how CLI telemetry is emitted by spawning a detached process and adding a hidden command entrypoint; failures or platform differences (non-Unix no-op) could lead to missing or duplicated analytics events.

Overview
CLI telemetry is now sent asynchronously by spawning a detached subprocess instead of initializing a PostHog client in-process during PersistentPostRun.

This introduces a hidden __send_analytics command that receives a JSON payload and calls telemetry.SendEvent, plus new telemetry helpers (BuildEventPayload, TrackCommandDetached, and Unix-only detached process spawning) and associated tests; the previous synchronous telemetry.Client implementation and tests are removed.

Written by Cursor Bugbot for commit 732e342. This will update automatically on new commits. Configure here.

Entire-Checkpoint: 8b12ecd0beb5
@gtrrz-victor gtrrz-victor requested a review from a team as a code owner January 28, 2026 02:33
Copilot AI review requested due to automatic review settings January 28, 2026 02:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors CLI telemetry to be sent via a detached subprocess instead of the main process, avoiding blocking the main command execution while still sending PostHog events when enabled.

Changes:

  • Replace the old PostHog client/interface with a detached telemetry pipeline that serializes events to JSON and spawns a separate process to send them.
  • Add a hidden __send_analytics Cobra command that deserializes the payload and sends it via PostHog, and wire it into NewRootCmd’s PersistentPostRun.
  • Remove the previous synchronous telemetry implementation and its tests, and add new tests for the detached telemetry code paths and payload serialization.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
cmd/entire/cli/telemetry/telemetry.go Removed the old in-process PostHog client (Client interface, NoOpClient, PostHogClient, NewClient, and TrackCommand/Close) in favor of the new detached telemetry model.
cmd/entire/cli/telemetry/telemetry_test.go Removed tests tied to the old telemetry client types and behaviors (opt-out handling, NoOpClient, and PostHogClient guardrails).
cmd/entire/cli/telemetry/detached.go Introduces the detached telemetry pipeline: EventPayload, TrackCommandDetached (building payload, applying opt-out, and calling spawnDetachedAnalytics), and SendEvent (reconstructing the PostHog client in the subprocess and enqueueing the event).
cmd/entire/cli/telemetry/detached_unix.go Adds a Unix-only implementation of spawnDetachedAnalytics that re-invokes the CLI in a new process group with the __send_analytics command and payload JSON, so analytics sending is fully detached from the parent process.
cmd/entire/cli/telemetry/detached_test.go Adds tests for payload JSON round-tripping, basic guardrails around TrackCommandDetached (nil/hidden commands and opt-out env var), invalid JSON handling in SendEvent, and stringifyArg behavior.
cmd/entire/cli/root.go Switches PersistentPostRun to use TrackCommandDetached when settings enable telemetry, adds a guard to avoid telemetry for the __send_analytics command itself, and registers the hidden __send_analytics command that invokes telemetry.SendEvent.

Entire-Checkpoint: 6c3e73767733
Entire-Checkpoint: 8ece5fc12e98
Copilot AI review requested due to automatic review settings January 28, 2026 02:51
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

@gtrrz-victor gtrrz-victor force-pushed the gtrrz-victor/detach-posthog-emit-event branch from 251a667 to 1f49d68 Compare January 28, 2026 03:00
Entire-Checkpoint: bde5096363d6
@gtrrz-victor gtrrz-victor force-pushed the gtrrz-victor/detach-posthog-emit-event branch from 1f49d68 to 1e37755 Compare January 28, 2026 03:27
Copilot AI review requested due to automatic review settings January 28, 2026 03:27
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Copilot AI review requested due to automatic review settings January 28, 2026 13:42
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Entire-Checkpoint: e032154c0b18
@gtrrz-victor gtrrz-victor merged commit 38e8151 into main Jan 28, 2026
4 checks passed
@gtrrz-victor gtrrz-victor deleted the gtrrz-victor/detach-posthog-emit-event branch January 28, 2026 22:53
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

// Skip analytics for the analytics command itself (prevents recursion)
if cmd.Name() == "__send_analytics" {
return
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant analytics command check with misleading comment

Low Severity

The check cmd.Name() == "__send_analytics" is redundant because TrackCommandDetached already skips hidden commands, and __send_analytics is defined with Hidden: true. The comment states this "prevents recursion" but the actual recursion prevention happens in TrackCommandDetached via the cmd.Hidden check. This creates confusion about where the safeguard actually lives and adds unnecessary code. The only benefit is avoiding a LoadEntireSettings call for the analytics command, but this optimization intent isn't documented.

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants