Repository navigation
aitools telemetry: attach the calling agent to the install event #7415
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Package agenttest controls which coding agent useragent.AgentProvider() | ||
| // reports in tests. | ||
| package agenttest | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/databricks/databricks-sdk-go/useragent" | ||
| ) | ||
|
|
||
| // agentEnvVars are the env vars useragent.AgentProvider() inspects: the SDK's | ||
| // listKnownAgents() (useragent/agent.go, unexported, so copied by hand) plus | ||
| // the AGENT / AI_AGENT fallbacks. | ||
| var agentEnvVars = []string{ | ||
| "AGENT", "AI_AGENT", "AMP_CURRENT_THREAD_ID", "ANTIGRAVITY_AGENT", | ||
| "AUGMENT_AGENT", "CLAUDECODE", "CLINE_ACTIVE", "CODEX_CI", "COPILOT_CLI", | ||
| "CURSOR_AGENT", "GEMINI_CLI", "GOOSE_TERMINAL", "KIRO", "OPENCLAW_SHELL", | ||
| "OPENCODE", "PI_CODING_AGENT", "VSCODE_AGENT", "WINDSURF_AGENT", | ||
| } | ||
|
|
||
| // SetAgentEnv clears every agent env var (the agent running the test binary | ||
| // would otherwise leak in), then sets the given ones, and resets the SDK's | ||
| // process-wide AgentProvider cache before and after the test. With no | ||
| // arguments AgentProvider() reports no agent. Tests using it must not run in | ||
| // parallel. | ||
| func SetAgentEnv(t *testing.T, kv ...string) { | ||
| t.Helper() | ||
| if len(kv)%2 != 0 { | ||
| t.Fatalf("SetAgentEnv needs key/value pairs, got %d values", len(kv)) | ||
| } | ||
| for _, v := range agentEnvVars { | ||
| if orig, ok := os.LookupEnv(v); ok { | ||
| t.Cleanup(func() { os.Setenv(v, orig) }) //nolint:usetesting // restoring, not setting fresh | ||
| os.Unsetenv(v) //nolint:usetesting // t.Setenv cannot unset | ||
| } | ||
| } | ||
| for i := 0; i < len(kv); i += 2 { | ||
| t.Setenv(kv[i], kv[i+1]) | ||
| } | ||
| useragent.ClearCache() | ||
| t.Cleanup(useragent.ClearCache) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package agents | ||
|
|
||
| import ( | ||
| "github.com/databricks/cli/libs/telemetry/protos" | ||
| "github.com/databricks/databricks-sdk-go/useragent" | ||
| ) | ||
|
|
||
| // SDK agent products (useragent.AgentProvider values) whose names differ from | ||
| // the registry name, plus the SDK's marker for stacked agents. | ||
| // https://github.com/databricks/databricks-sdk-go/blob/main/useragent/agent.go | ||
| const ( | ||
| providerCopilot = "copilot-cli" | ||
| providerGemini = "gemini-cli" | ||
| providerMultiple = "multiple" | ||
| ) | ||
|
|
||
| // providerToRegistry maps SDK agent products to registry agent names. Products | ||
| // missing here have no registry entry, so the CLI cannot install into them. | ||
| var providerToRegistry = map[string]string{ | ||
| NameClaudeCode: NameClaudeCode, | ||
| NameCursor: NameCursor, | ||
| NameCodex: NameCodex, | ||
| NameOpenCode: NameOpenCode, | ||
| providerCopilot: NameCopilot, | ||
| NameAntigravity: NameAntigravity, | ||
| NamePi: NamePi, | ||
| providerGemini: NameGemini, | ||
| NameGoose: NameGoose, | ||
| NameKiro: NameKiro, | ||
| } | ||
|
|
||
| // providerOnlyTypes are SDK agent products with no registry entry. They are | ||
| // reported as a calling agent only. | ||
| var providerOnlyTypes = map[string]protos.AitoolsAgentType{ | ||
| "amp": protos.AitoolsAgentTypeAmp, | ||
| "augment": protos.AitoolsAgentTypeAugment, | ||
| "cline": protos.AitoolsAgentTypeCline, | ||
| "openclaw": protos.AitoolsAgentTypeOpenClaw, | ||
| "vscode-agent": protos.AitoolsAgentTypeVSCodeAgent, | ||
| "windsurf": protos.AitoolsAgentTypeWindsurf, | ||
| } | ||
|
|
||
| // AgentForProvider returns the registry agent for an SDK agent product | ||
| // (useragent.AgentProvider value), or nil when the product has no registry | ||
| // entry, is "multiple", or is empty. | ||
| func AgentForProvider(provider string) *Agent { | ||
| name, ok := providerToRegistry[provider] | ||
| if !ok { | ||
| return nil | ||
| } | ||
| return ByName(name) | ||
| } | ||
|
|
||
| // CallingAgent returns the registry agent driving the CLI, or nil when no | ||
| // agent is detected, several are, or the agent is one the CLI cannot install | ||
| // into. | ||
| func CallingAgent() *Agent { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nice to have] |
||
| return AgentForProvider(useragent.AgentProvider()) | ||
| } | ||
|
|
||
| // AgentType maps a registry agent name to its telemetry enum. Every agent in | ||
| // Registry has a case; unknown names map to Unspecified so an older proto | ||
| // never drops the event. | ||
| func AgentType(name string) protos.AitoolsAgentType { | ||
| switch name { | ||
| case NameClaudeCode: | ||
| return protos.AitoolsAgentTypeClaudeCode | ||
| case NameCursor: | ||
| return protos.AitoolsAgentTypeCursor | ||
| case NameCodex: | ||
| return protos.AitoolsAgentTypeCodex | ||
| case NameOpenCode: | ||
| return protos.AitoolsAgentTypeOpenCode | ||
| case NameCopilot: | ||
| return protos.AitoolsAgentTypeCopilot | ||
| case NameAntigravity: | ||
| return protos.AitoolsAgentTypeAntigravity | ||
| case NamePi: | ||
| return protos.AitoolsAgentTypePi | ||
| case NameGemini: | ||
| return protos.AitoolsAgentTypeGemini | ||
| case NameGoose: | ||
| return protos.AitoolsAgentTypeGoose | ||
| case NameKiro: | ||
| return protos.AitoolsAgentTypeKiro | ||
| default: | ||
| return protos.AitoolsAgentTypeUnspecified | ||
| } | ||
| } | ||
|
|
||
| // ProviderAgentType maps an SDK agent product to the closed telemetry enum. | ||
| // Free text from the AGENT / AI_AGENT env vars, and products a newer SDK | ||
| // detects, become UNKNOWN, so the raw value never leaves the machine. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nice to have] This comment overstates the guarantee: |
||
| func ProviderAgentType(provider string) protos.AitoolsAgentType { | ||
| switch provider { | ||
| case "": | ||
| return protos.AitoolsAgentTypeNone | ||
| case providerMultiple: | ||
| return protos.AitoolsAgentTypeMultiple | ||
| } | ||
| if a := AgentForProvider(provider); a != nil { | ||
| return AgentType(a.Name) | ||
| } | ||
| if t, ok := providerOnlyTypes[provider]; ok { | ||
| return t | ||
| } | ||
| return protos.AitoolsAgentTypeUnknown | ||
| } | ||
|
|
||
| // CallingAgentType is the telemetry enum for the agent driving the CLI. | ||
| func CallingAgentType() protos.AitoolsAgentType { | ||
| return ProviderAgentType(useragent.AgentProvider()) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nice to have]
internal/testutil.SetAgentalready clears the environment and resets the SDK's agent cache. These tests can usetestutil.SetAgent(t, "")followed by the relevantt.Setenvcalls, avoiding another manually maintained SDK environment-variable list.