Skip to content

impr: made analytics run in background for optimal performance#68

Merged
SiebeBaree merged 3 commits into
mainfrom
t3code/3f71fdab
May 17, 2026
Merged

impr: made analytics run in background for optimal performance#68
SiebeBaree merged 3 commits into
mainfrom
t3code/3f71fdab

Conversation

@SiebeBaree
Copy link
Copy Markdown
Member

@SiebeBaree SiebeBaree commented May 17, 2026

Summary by CodeRabbit

  • Chores

    • Version bumped to 0.3.5
  • Refactor

    • Telemetry now queues events locally and sends them from a detached background worker so commands exit faster and telemetry won't block CLI completion.
    • Telemetry respects config-level opt-out and preserves anonymous IDs when available.
  • Chores

    • macOS dev build step added to attempt re-signing the dev binary and warn if signing fails.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bfd3d30f-522f-4de0-a59a-14c68c05ab8c

📥 Commits

Reviewing files that changed from the base of the PR and between d25801f and 531f453.

📒 Files selected for processing (1)
  • scripts/build-dev.sh

Walkthrough

This PR replaces direct PostHog usage with an in-memory event queue (pendingEvents). analytics.identify/track now enqueue payloads; analytics.shutdown synchronously serializes queued events and spawns the current executable in a new __analytics mode to deliver them. A new analytics-worker module parses the payload and posts events with a 5s timeout, suppressing errors. The CLI adds a __analytics branch to run the worker and switches to a process.on("exit") handler to call analytics.shutdown. package.json version bumped to 0.3.5.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • Enkryptify/cli#42: Related prior work that introduced PostHog-based telemetry which this PR refactors away.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main architectural change: refactoring analytics to run in a background process instead of inline, which is the core purpose of the changes across cli.ts, analytics.ts, and the new analytics-worker.ts.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch t3code/3f71fdab

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/lib/analytics.ts`:
- Around line 236-241: The code is leaking queued analytics into the child
process environment by setting EK_ANALYTICS_PAYLOAD to JSON; instead, send the
payload over stdin (or another non-environment channel) when spawning the child.
In the block that builds payload from pendingEvents and calls
Bun.spawn([...getSelfSpawnCommand(), "__analytics"], { env: {...}, ... }),
remove the EK_ANALYTICS_PAYLOAD entry and configure the spawn to use a piped
stdin, then write the JSON payload to the child's stdin (and close it) after
spawn; reference symbols: pendingEvents, payload, getSelfSpawnCommand,
Bun.spawn, and EK_ANALYTICS_PAYLOAD (remove usage).
- Around line 236-248: The code currently clears pendingEvents before attempting
to spawn the analytics worker, risking lost events if Bun.spawn(...) fails;
change the logic in the block that builds payload/starts the worker (the code
using pendingEvents, getSelfSpawnCommand(), Bun.spawn(...), proc.unref(), and
EK_ANALYTICS_PAYLOAD) so you only set pendingEvents.length = 0 after Bun.spawn
completes successfully (and proc.unref() is called); if Bun.spawn throws, catch
the error, log it, and leave pendingEvents intact (or requeue the payload) so
the batch is not lost.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 62869a57-6927-4d77-8aa8-5a44492f55ca

📥 Commits

Reviewing files that changed from the base of the PR and between 3470a65 and a471395.

⛔ Files ignored due to path filters (1)
  • bun.lock is excluded by !**/*.lock
📒 Files selected for processing (4)
  • package.json
  • src/cli.ts
  • src/lib/analytics-worker.ts
  • src/lib/analytics.ts

Comment thread src/lib/analytics.ts Outdated
Comment thread src/lib/analytics.ts
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/lib/analytics.ts`:
- Around line 238-245: The code currently exposes PII by passing the JSON
payload as a command-line argument to Bun.spawn; change the spawn usage in the
analytics sending path (where Bun.spawn([...cmd, "__analytics", payload], { ...,
detached: true }) and proc.unref() are used) to omit the payload from argv and
instead open stdin for the child (set stdin to "pipe"), write the payload JSON
into proc.stdin and close it, then unref the child; also update the
analytics-worker to read the payload from stdin (e.g., via Bun.stdin.text())
instead of process.argv[3].
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4aa43cd9-f9fb-4daa-8b63-aed2cab77b26

📥 Commits

Reviewing files that changed from the base of the PR and between a471395 and d25801f.

📒 Files selected for processing (2)
  • src/lib/analytics-worker.ts
  • src/lib/analytics.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/lib/analytics-worker.ts

Comment thread src/lib/analytics.ts
@SiebeBaree SiebeBaree merged commit 80cccfc into main May 17, 2026
10 of 11 checks passed
@SiebeBaree SiebeBaree deleted the t3code/3f71fdab branch May 17, 2026 16:16
@coderabbitai coderabbitai Bot mentioned this pull request May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant