-
Notifications
You must be signed in to change notification settings - Fork 32
feat(telemetry): emit cli_context property on main events #269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+215
−1
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a6b8572
feat(telemetry): add cli_context invocation detection
LorrisSaintGenez b1da8c1
feat(telemetry): emit cli_context property on all events
LorrisSaintGenez b5b4fe8
Merge branch 'main' into feat/agent-telemetry
LorrisSaintGenez dde595a
fix(telemetry): validate generic cli_context agent values
LorrisSaintGenez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package telemetry | ||
|
|
||
| import ( | ||
| "os" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/algolia/cli/pkg/utils" | ||
| ) | ||
|
|
||
| const ( | ||
| ContextHuman = "human" | ||
| ContextAgentUnknown = "agent:unknown" | ||
| ) | ||
|
|
||
| var agentEnvVars = []struct { | ||
| envVar string | ||
| name string | ||
| }{ | ||
| {"CLAUDECODE", "claude-code"}, | ||
| {"CLAUDE_CODE", "claude-code"}, | ||
| {"CLAUDE_CODE_SESSION_ID", "claude-code"}, | ||
| {"CURSOR_AGENT", "cursor"}, | ||
| {"CODEX_THREAD_ID", "codex"}, | ||
| {"CODEX_SANDBOX", "codex"}, | ||
| {"CODEX_CI", "codex"}, | ||
| {"GEMINI_CLI", "gemini"}, | ||
| {"COPILOT_CLI", "github-copilot"}, | ||
| {"OPENCODE", "opencode"}, | ||
| {"OPENCODE_CLIENT", "opencode"}, | ||
| {"AMP_CURRENT_THREAD_ID", "amp"}, | ||
| {"AUGMENT_AGENT", "augment"}, | ||
| {"GOOSE_TERMINAL", "goose"}, | ||
| {"CLINE_ACTIVE", "cline"}, | ||
| {"ANTIGRAVITY_AGENT", "antigravity"}, | ||
| {"PI_CODING_AGENT", "pi"}, | ||
| {"KIRO_AGENT_PATH", "kiro"}, | ||
| } | ||
|
|
||
| var genericAgentNameRe = regexp.MustCompile(`^[a-z][a-z0-9-]{0,31}$`) | ||
|
|
||
| var booleanishValues = map[string]bool{ | ||
| "true": true, | ||
| "false": true, | ||
| "yes": true, | ||
| "no": true, | ||
| "on": true, | ||
| "off": true, | ||
| } | ||
|
|
||
| func DetectCLIContext() string { | ||
| return detectCLIContext( | ||
| os.Getenv, | ||
| utils.IsTerminal(os.Stdin), | ||
| utils.IsTerminal(os.Stdout), | ||
| utils.IsCI(), | ||
| ) | ||
| } | ||
|
|
||
| func detectCLIContext(getenv func(string) string, stdinTTY, stdoutTTY, isCI bool) string { | ||
| for _, agent := range agentEnvVars { | ||
| if getenv(agent.envVar) != "" { | ||
| return "agent:" + agent.name | ||
| } | ||
| } | ||
| for _, key := range []string{"AI_AGENT", "AGENT"} { | ||
| v, _, _ := strings.Cut(strings.ToLower(strings.TrimSpace(getenv(key))), "_") | ||
| if genericAgentNameRe.MatchString(v) && !booleanishValues[v] { | ||
| return "agent:" + v | ||
| } | ||
| } | ||
| if !stdinTTY && !stdoutTTY && !isCI { | ||
| return ContextAgentUnknown | ||
| } | ||
| return ContextHuman | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package telemetry | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func fakeEnv(vars map[string]string) func(string) string { | ||
| return func(key string) string { return vars[key] } | ||
| } | ||
|
|
||
| func TestDetectCLIContext_KnownAgents(t *testing.T) { | ||
| cases := []struct { | ||
| envVar string | ||
| want string | ||
| }{ | ||
| {"CLAUDECODE", "agent:claude-code"}, | ||
| {"CLAUDE_CODE", "agent:claude-code"}, | ||
| {"CLAUDE_CODE_SESSION_ID", "agent:claude-code"}, | ||
| {"CURSOR_AGENT", "agent:cursor"}, | ||
| {"CODEX_THREAD_ID", "agent:codex"}, | ||
| {"CODEX_SANDBOX", "agent:codex"}, | ||
| {"CODEX_CI", "agent:codex"}, | ||
| {"GEMINI_CLI", "agent:gemini"}, | ||
| {"COPILOT_CLI", "agent:github-copilot"}, | ||
| {"OPENCODE", "agent:opencode"}, | ||
| {"OPENCODE_CLIENT", "agent:opencode"}, | ||
| {"AMP_CURRENT_THREAD_ID", "agent:amp"}, | ||
| {"AUGMENT_AGENT", "agent:augment"}, | ||
| {"GOOSE_TERMINAL", "agent:goose"}, | ||
| {"CLINE_ACTIVE", "agent:cline"}, | ||
| {"ANTIGRAVITY_AGENT", "agent:antigravity"}, | ||
| {"PI_CODING_AGENT", "agent:pi"}, | ||
| {"KIRO_AGENT_PATH", "agent:kiro"}, | ||
| } | ||
| for _, c := range cases { | ||
| t.Run(c.envVar, func(t *testing.T) { | ||
| got := detectCLIContext(fakeEnv(map[string]string{c.envVar: "1"}), true, true, false) | ||
| assert.Equal(t, c.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDetectCLIContext_GenericAIAgentVar(t *testing.T) { | ||
| got := detectCLIContext(fakeEnv(map[string]string{"AI_AGENT": "SomeAgent"}), true, true, false) | ||
| assert.Equal(t, "agent:someagent", got) | ||
| } | ||
|
|
||
| func TestDetectCLIContext_GenericAgentVar(t *testing.T) { | ||
| got := detectCLIContext(fakeEnv(map[string]string{"AGENT": "goose"}), true, true, false) | ||
| assert.Equal(t, "agent:goose", got) | ||
| } | ||
|
|
||
| func TestDetectCLIContext_AIAgentWinsOverAgent(t *testing.T) { | ||
| env := fakeEnv(map[string]string{"AI_AGENT": "v0", "AGENT": "goose"}) | ||
| assert.Equal(t, "agent:v0", detectCLIContext(env, true, true, false)) | ||
| } | ||
|
|
||
| func TestDetectCLIContext_WhitespaceOnlyGenericVar(t *testing.T) { | ||
| got := detectCLIContext(fakeEnv(map[string]string{"AI_AGENT": " "}), true, true, false) | ||
| assert.Equal(t, "human", got) | ||
| } | ||
|
|
||
| func TestDetectCLIContext_VersionStampedGenericVar(t *testing.T) { | ||
| got := detectCLIContext(fakeEnv(map[string]string{"AI_AGENT": "claude-code_2-1-210_agent"}), true, true, false) | ||
| assert.Equal(t, "agent:claude-code", got) | ||
| } | ||
|
|
||
| func TestDetectCLIContext_LeadingUnderscoreGenericVar(t *testing.T) { | ||
| got := detectCLIContext(fakeEnv(map[string]string{"AI_AGENT": "_foo"}), true, true, false) | ||
| assert.Equal(t, "human", got) | ||
| } | ||
|
|
||
| func TestDetectCLIContext_NumericGenericVar(t *testing.T) { | ||
| got := detectCLIContext(fakeEnv(map[string]string{"AGENT": "1"}), true, true, false) | ||
| assert.Equal(t, "human", got) | ||
| } | ||
|
|
||
| func TestDetectCLIContext_BooleanishGenericVar(t *testing.T) { | ||
| got := detectCLIContext(fakeEnv(map[string]string{"AGENT": "true"}), true, true, false) | ||
| assert.Equal(t, "human", got) | ||
| } | ||
|
|
||
| func TestDetectCLIContext_OverlongGenericVar(t *testing.T) { | ||
| got := detectCLIContext(fakeEnv(map[string]string{"AI_AGENT": "a-very-long-agent-name-exceeding-32-characters"}), true, true, false) | ||
| assert.Equal(t, "human", got) | ||
| } | ||
|
|
||
| func TestDetectCLIContext_NamedVarWinsOverGeneric(t *testing.T) { | ||
| env := fakeEnv(map[string]string{"CLAUDECODE": "1", "AI_AGENT": "other"}) | ||
| assert.Equal(t, "agent:claude-code", detectCLIContext(env, true, true, false)) | ||
| } | ||
|
|
||
| func TestDetectCLIContext_NoTTYNoCI(t *testing.T) { | ||
| assert.Equal(t, "agent:unknown", detectCLIContext(fakeEnv(nil), false, false, false)) | ||
| } | ||
|
|
||
| func TestDetectCLIContext_NoTTYInCI(t *testing.T) { | ||
| assert.Equal(t, "human", detectCLIContext(fakeEnv(nil), false, false, true)) | ||
| } | ||
|
|
||
| func TestDetectCLIContext_PipedOutputOnly(t *testing.T) { | ||
| assert.Equal(t, "human", detectCLIContext(fakeEnv(nil), true, false, false)) | ||
| } | ||
|
|
||
| func TestDetectCLIContext_Interactive(t *testing.T) { | ||
| assert.Equal(t, "human", detectCLIContext(fakeEnv(nil), true, true, false)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.