Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 4 additions & 28 deletions cmd/aitools/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func logInstallEvent(ctx context.Context, plan []agentPlanItem, opts installOpts
Experimental: opts.Experimental,
ErrorCategory: errCategory,
AgentResults: agentResultsField(outcomes),
CallingAgent: agents.CallingAgentType(),
},
})
}
Expand Down Expand Up @@ -93,35 +94,10 @@ func agentsField(plan []agentPlanItem) []protos.AitoolsAgentType {
return out
}

// agentType maps a CLI agent registry name to its telemetry enum. Every agent
// in agents.Registry must have a case here; TestAgentTypeCoversRegistry fails
// if one is missing. The default maps genuinely unknown names to Unspecified
// so an older proto never drops an install.
// agentType maps a CLI agent registry name to its telemetry enum.
// TestAgentTypeCoversRegistry fails if a registry agent maps to Unspecified.
func agentType(name string) protos.AitoolsAgentType {
switch name {
case agents.NameClaudeCode:
return protos.AitoolsAgentTypeClaudeCode
case agents.NameCursor:
return protos.AitoolsAgentTypeCursor
case agents.NameCodex:
return protos.AitoolsAgentTypeCodex
case agents.NameOpenCode:
return protos.AitoolsAgentTypeOpenCode
case agents.NameCopilot:
return protos.AitoolsAgentTypeCopilot
case agents.NameAntigravity:
return protos.AitoolsAgentTypeAntigravity
case agents.NamePi:
return protos.AitoolsAgentTypePi
case agents.NameGemini:
return protos.AitoolsAgentTypeGemini
case agents.NameGoose:
return protos.AitoolsAgentTypeGoose
case agents.NameKiro:
return protos.AitoolsAgentTypeKiro
default:
return protos.AitoolsAgentTypeUnspecified
}
return agents.AgentType(name)
}

// scopeType maps a resolved install scope to its telemetry enum.
Expand Down
70 changes: 69 additions & 1 deletion cmd/aitools/telemetry_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,82 @@
package aitools

import (
"encoding/json"
"testing"

"github.com/databricks/cli/libs/aitools/agents"
"github.com/databricks/cli/libs/aitools/agents/agenttest"
"github.com/databricks/cli/libs/aitools/installer"
"github.com/databricks/cli/libs/cmdctx"
"github.com/databricks/cli/libs/env"
"github.com/databricks/cli/libs/telemetry"
"github.com/databricks/cli/libs/telemetry/protos"
"github.com/databricks/cli/libs/testserver"
"github.com/databricks/databricks-sdk-go/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// uploadInstallEvent runs logInstallEvent, uploads the buffered log to a local
// /telemetry-ext and returns the install event as the server received it.
func uploadInstallEvent(t *testing.T) map[string]any {
server := testserver.New(t)
t.Cleanup(server.Close)

var protoLogs []string
server.Handle("POST", "/telemetry-ext", func(req testserver.Request) any {
var body telemetry.RequestBody
require.NoError(t, json.Unmarshal(req.Body, &body))
protoLogs = append(protoLogs, body.ProtoLogs...)
return telemetry.ResponseBody{NumProtoSuccess: int64(len(body.ProtoLogs))}
})

// Upload honours these from the host env; an empty value restores the
// defaults (enabled, 3s timeout) so the test does not depend on the runner.
ctx := env.Set(t.Context(), "DATABRICKS_CLI_DISABLE_TELEMETRY", "")
ctx = env.Set(ctx, "DATABRICKS_CLI_TELEMETRY_TIMEOUT", "")
ctx = telemetry.WithNewLogger(ctx)
plan := []agentPlanItem{{agent: &agents.Agent{Name: agents.NameCursor}}}
logInstallEvent(ctx, plan, installOpts{Scope: installer.ScopeGlobal}, protos.AitoolsErrorCategoryUnspecified, nil)

// AuthType pins PAT so a host DATABRICKS_AUTH_TYPE cannot pick another method.
ctx = cmdctx.SetConfigUsed(ctx, &config.Config{Host: server.URL, Token: "token", AuthType: "pat"})
require.NoError(t, telemetry.Upload(ctx, protos.ExecutionContext{}))
require.Len(t, protoLogs, 1)

var frontendLog struct {
Entry struct {
DatabricksCliLog struct {
AitoolsInstallEvent map[string]any `json:"aitools_install_event"`
} `json:"databricks_cli_log"`
} `json:"entry"`
}
require.NoError(t, json.Unmarshal([]byte(protoLogs[0]), &frontendLog))
event := frontendLog.Entry.DatabricksCliLog.AitoolsInstallEvent
require.NotNil(t, event)
return event
}

func TestLogInstallEventSendsCallingAgent(t *testing.T) {
cases := []struct {
name string
env []string
want protos.AitoolsAgentType
}{
{name: "claude code", env: []string{"CLAUDECODE", "1"}, want: protos.AitoolsAgentTypeClaudeCode},
{name: "no agent", env: nil, want: protos.AitoolsAgentTypeNone},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
agenttest.SetAgentEnv(t, tc.env...)
event := uploadInstallEvent(t)
assert.Equal(t, string(tc.want), event["calling_agent"])
// The calling agent is not an install target.
assert.Equal(t, []any{string(protos.AitoolsAgentTypeCursor)}, event["agents"])
})
}
}

func TestAgentsField(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -74,7 +142,7 @@ func TestAgentsField(t *testing.T) {
func TestAgentTypeCoversRegistry(t *testing.T) {
for _, a := range agents.Registry {
assert.NotEqualf(t, protos.AitoolsAgentTypeUnspecified, agentType(a.Name),
"agent %q has no telemetry enum: add a case to agentType and a value to "+
"agent %q has no telemetry enum: add a case to agents.AgentType and a value to "+
"AitoolsAgentType in enum.proto (Universe) + aitools_install.go (CLI)", a.Name)
}
}
Expand Down
43 changes: 43 additions & 0 deletions libs/aitools/agents/agenttest/agenttest.go
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) {

Copy link
Copy Markdown
Member

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.SetAgent already clears the environment and resets the SDK's agent cache. These tests can use testutil.SetAgent(t, "") followed by the relevant t.Setenv calls, avoiding another manually maintained SDK environment-variable list.

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)
}
113 changes: 113 additions & 0 deletions libs/aitools/agents/calling.go
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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[nice to have] CallingAgent() currently has only test callers, and AgentForProvider has no production consumer outside this package. Consider dropping CallingAgent and keeping the provider lookup unexported until there is a production caller.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[nice to have] This comment overstates the guarantee: AGENT=cursor becomes CURSOR, and an unknown AGENT value still travels in the SDK's HTTP User-Agent. Please say that recognized product names map to enums and other values to UNKNOWN, so this JSON field never contains free-form text.

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())
}
Loading
Loading