feat(telemetry): add agent detection tag for AI coding tools#687
Conversation
Semver Impact of This PR🟡 Minor (new features) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨Docs
Other
Bug Fixes 🐛Dashboard
Other
Internal Changes 🔧
🤖 This preview updates automatically when you update the PR. |
|
Codecov Results 📊✅ 134 passed | Total: 134 | Pass Rate: 100% | Execution Time: 0ms 📊 Comparison with Base Branch
✨ No test changes detected All tests are passing successfully. ✅ Patch coverage is 95.93%. Project has 1574 uncovered lines. Files with missing lines (2)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 95.30% 95.30% —%
==========================================
Files 229 230 +1
Lines 33377 33499 +122
Branches 0 0 —
==========================================
+ Hits 31808 31925 +117
- Misses 1569 1574 +5
- Partials 0 0 —Generated by Codecov Action |
|
@BYK We're missing some of the smaller ones but these should be a good list to start with. |
Detect whether the CLI is being driven by an AI coding agent and tag the telemetry span with the agent name. Uses environment variables that agents inject into child processes (e.g. CLAUDE_CODE, CURSOR_TRACE_ID). Supports a generic AI_AGENT override and an AGENT fallback so new tools work out of the box. Adapted from Vercel's @vercel/detect-agent. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ection - Convert AGENT_ENV_VARS array-of-objects to a flat Map<string, string> mapping each env var directly to its agent name (simpler lookups, one-line additions for new agents) - Add process tree walking as fallback detection: scan parent → grandparent → ... for known agent executables (up to 5 levels) - Linux: reads /proc/<pid>/status (in-memory, no subprocess overhead) - macOS: falls back to ps(1) via execFileSync - Injectable via setProcessInfoProvider() for testing (same pattern as setEnv) - 50 tests (up from 29) covering env vars, Map structure, process tree walking, depth limits, and real process info reads
Users may export COPILOT_GITHUB_TOKEN persistently in their shell config for authentication purposes, not just when Copilot is actively driving the CLI. This caused false-positive agent tagging. Keep COPILOT_MODEL and COPILOT_ALLOW_ALL which indicate active agent control.
Prevents CLI from hanging indefinitely on macOS if ps is unresponsive during agent detection at startup.
REPL_ID is set in ALL Replit workspaces, not just when the Replit AI agent is driving the CLI. Same class of false positive as COPILOT_GITHUB_TOKEN — a platform environment marker rather than an agent control signal.
Process tree detection (reading /proc/ or spawning ps) should never block CLI startup for a telemetry side-effect. Split the detection: - detectAgent() is sync — env var lookups only (instant) - detectAgentFromProcessTree() is async — fires as a background task in initSentry(), sets the Sentry tag when it resolves Uses readFile (node:fs/promises) and promisified execFile instead of their sync counterparts. Windows is explicitly unsupported for process tree detection (env var detection still works everywhere).
Prevents unhandled promise rejection if the async detection fails, particularly important in library mode where OnUnhandledRejection is excluded.
- Replace promisify(execFile) with a manual wrapper that calls
child.unref() so the spawned ps process never prevents CLI exit
- Log a warning via logger.withTag('agent') instead of silently
swallowing errors in the .catch() handler
There was a problem hiding this comment.
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.
Reviewed by Cursor Bugbot for commit e1d4849. Configure here.

Summary
Adds an
agenttag to telemetry spans when the CLI is invoked by a known AI coding tool. Detection uses two strategies:@vercel/detect-agent(Apache-2.0).Supported agents (env vars)
AI_AGENTCURSOR_TRACE_ID,CURSOR_AGENTGEMINI_CLICODEX_SANDBOX,CODEX_CI,CODEX_THREAD_IDANTIGRAVITY_AGENTAUGMENT_AGENTOPENCODE_CLIENTCLAUDE_CODE,CLAUDECODECLAUDE_CODE+CLAUDE_CODE_IS_COWORKCOPILOT_MODEL,COPILOT_ALLOW_ALLGOOSE_TERMINALAMP_THREAD_IDAGENTIntentionally excluded:
REPL_ID(set in all Replit workspaces, not just AI agent sessions) andCOPILOT_GITHUB_TOKEN(auth credential users may export persistently).New agents can be added with a single
["ENV_VAR", "agent-name"]line in theENV_VAR_AGENTSmap.Process tree detection (fallback)
When no env var matches, the CLI asynchronously walks the parent process tree (up to 5 levels) looking for known agent executables:
cursor,claude,goose,windsurf,amp,codex,augment,opencode,gemini/proc/<pid>/status(in-memory filesystem, fast)ps(1)with a 500ms timeout, child process unreffed to never block exitDetection priority
AI_AGENTenv var (explicit override)ENV_VAR_AGENTSmap)AGENTenv var (generic fallback — explicit signal beats heuristic)Known limitations
/proc/orps). Env var detection still works.Test plan
/proc/readsCLAUDE_CODE=1 bunx sentry-cli auth statusand verify span hasagent: claude