diff --git a/cmd/root/root.go b/cmd/root/root.go index 81b341e9c5c..d304b62ed84 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -85,6 +85,7 @@ func New(ctx context.Context) *cobra.Command { ctx = withInteractiveModeInUserAgent(ctx) ctx = installer.WithAiToolsInUserAgent(ctx) ctx = installer.WithAiDevKitInUserAgent(ctx) + ctx = installer.WithCallingAgentToolingInUserAgent(ctx) ctx = InjectTestPidToUserAgent(ctx) cmd.SetContext(ctx) diff --git a/internal/testutil/agent.go b/internal/testutil/agent.go index 249f575e3f3..0d43283edd2 100644 --- a/internal/testutil/agent.go +++ b/internal/testutil/agent.go @@ -1,14 +1,41 @@ package testutil -import "github.com/databricks/databricks-sdk-go/useragent" +import ( + "os" + + "github.com/databricks/databricks-sdk-go/useragent" +) // SetAgent makes useragent.AgentProvider report agent, or no agent if agent // is empty. It clears the environment and the SDK's cached agent, so the agent // running the tests does not leak into them. // Note: use of this function is incompatible with parallel execution. func SetAgent(t TestingT, agent string) { + SetAgentEnv(t, "AGENT", agent) +} + +// SetAgentEnv clears the environment and sets the given key/value pairs, e.g. +// the agents' own env vars (CLAUDECODE=1), so useragent.AgentProvider sees +// only them; with no pairs it reports no agent. TMPDIR, TEMP and TMP are kept +// so t.TempDir keeps working. The SDK's cached agent is reset before and after +// the test. +// Note: use of this function is incompatible with parallel execution. +func SetAgentEnv(t TestingT, kv ...string) { + t.Helper() + if len(kv)%2 != 0 { + t.Fatalf("SetAgentEnv needs key/value pairs, got %d values", len(kv)) + } + var keep []string + for _, key := range []string{"TMPDIR", "TEMP", "TMP"} { + if value, ok := os.LookupEnv(key); ok { + keep = append(keep, key, value) + } + } NullEnvironment(t) - t.Setenv("AGENT", agent) + kv = append(keep, kv...) + for i := 0; i < len(kv); i += 2 { + t.Setenv(kv[i], kv[i+1]) + } useragent.ClearCache() t.Cleanup(useragent.ClearCache) } diff --git a/internal/testutil/agent_test.go b/internal/testutil/agent_test.go new file mode 100644 index 00000000000..b21f6fa6d94 --- /dev/null +++ b/internal/testutil/agent_test.go @@ -0,0 +1,29 @@ +package testutil_test + +import ( + "os" + "testing" + + "github.com/databricks/cli/internal/testutil" + "github.com/databricks/databricks-sdk-go/useragent" + "github.com/stretchr/testify/assert" +) + +func TestSetAgentEnv(t *testing.T) { + kept := t.TempDir() + t.Setenv("TMPDIR", kept) + testutil.SetAgentEnv(t, "CLAUDECODE", "1") + assert.Equal(t, "claude-code", useragent.AgentProvider()) + assert.Equal(t, kept, os.Getenv("TMPDIR")) +} + +func TestSetAgentEnvWithoutPairs(t *testing.T) { + t.Setenv("CLAUDECODE", "1") + testutil.SetAgentEnv(t) + assert.Empty(t, useragent.AgentProvider()) +} + +func TestSetAgent(t *testing.T) { + testutil.SetAgent(t, "my-agent") + assert.Equal(t, "my-agent", useragent.AgentProvider()) +} diff --git a/libs/aitools/agents/agents.go b/libs/aitools/agents/agents.go index 0c6cdfcbfb2..a8f59f037b0 100644 --- a/libs/aitools/agents/agents.go +++ b/libs/aitools/agents/agents.go @@ -56,12 +56,13 @@ type Agent struct { // Plugin describes the databricks plugin for this agent, or nil when the // agent has no plugin and skills files are its native delivery. Plugin *PluginSpec - // pluginVersion reads the installed databricks plugin version from the + // pluginInstalls reads the databricks plugin's install records from the // agent's own plugin manifest. Each agent's manifest format differs, so it is // set per agent (only for formats we have verified); when nil, - // DatabricksPluginVersion reports no version. New agents extend support by - // providing their own reader here. - pluginVersion func(ctx context.Context, a *Agent) (string, bool) + // DatabricksPluginVersion reports no version and DatabricksTooling falls + // back to the CLI install state. New agents extend support by providing + // their own reader here. + pluginInstalls func(ctx context.Context, a *Agent) ([]pluginInstall, bool) } // Detected reports whether the agent is installed: its config directory exists, @@ -179,7 +180,7 @@ var Registry = []*Agent{ ProjectConfigDir: ".claude", Binary: "claude", Plugin: claudePlugin(), - pluginVersion: claudePluginVersion, + pluginInstalls: claudePluginInstalls, }, { Name: NameCursor, diff --git a/libs/aitools/agents/calling.go b/libs/aitools/agents/calling.go new file mode 100644 index 00000000000..6e91d7fc9f1 --- /dev/null +++ b/libs/aitools/agents/calling.go @@ -0,0 +1,120 @@ +package agents + +import ( + "context" + "os" + "path/filepath" + + "github.com/databricks/cli/libs/folders" + "github.com/databricks/databricks-sdk-go/useragent" +) + +// providerAliases maps the SDK agent products (useragent.AgentProvider values) +// whose names differ from the registry name. Every other product is looked up +// by its own name. +// https://github.com/databricks/databricks-sdk-go/blob/main/useragent/agent.go +var providerAliases = map[string]string{ + "copilot-cli": NameCopilot, + "gemini-cli": NameGemini, +} + +// 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 { + if name, ok := providerAliases[provider]; ok { + provider = name + } + return ByName(provider) +} + +// 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 { + return AgentForProvider(useragent.AgentProvider()) +} + +// Tooling is what Databricks AI tooling an agent has in its own location. +type Tooling int + +const ( + ToolingNone Tooling = iota + ToolingSkills + ToolingPlugin +) + +// String returns the value reported in the aitools-agent User-Agent segment. +func (t Tooling) String() string { + switch t { + case ToolingPlugin: + return "plugin" + case ToolingSkills: + return "skills" + default: + return "none" + } +} + +// DatabricksTooling reports the Databricks plugin or skills in this agent's own +// locations; read errors count as not installed. Claude Code's manifest is +// authoritative because `claude plugin uninstall` leaves a stale CLI install +// record. For Codex and Copilot, pluginRecorded (the CLI install state) is the +// only plugin source, so it misses plugins installed with the agent's own CLI +// and includes plugins later removed with it. The shared skills store is not consulted: it cannot attribute skills. +// The install hint's claudeToolingInstalled differs on purpose: it stays quiet +// for any trace of an install, while this reports what the agent loads. +func (a *Agent) DatabricksTooling(ctx context.Context, pluginRecorded func() bool) Tooling { + // An unknown working directory skips the project lookups below. + cwd := "" + if wd, err := os.Getwd(); err == nil { + cwd = resolvePath(wd) + } + switch { + case a.pluginInstalls != nil: + if a.databricksPluginActiveIn(ctx, cwd) { + return ToolingPlugin + } + case a.Plugin != nil: + if pluginRecorded() { + return ToolingPlugin + } + } + if dir, err := a.SkillsDir(ctx); err == nil && HasDatabricksSkillsIn(dir) { + return ToolingSkills + } + if a.SupportsProjectScope && cwd != "" { + for _, dir := range projectDirs(cwd) { + if HasDatabricksSkillsIn(a.ProjectSkillsDir(dir)) { + return ToolingSkills + } + } + } + return ToolingNone +} + +// projectDirs returns cwd and each of its parents up to the repository root, +// the nearest directory holding .git (a file in a git worktree, so the walk +// stops at the worktree root). Claude Code loads project skills the same way, +// so an agent started at the repository root still has them when the CLI runs +// in a subdirectory: https://code.claude.com/docs/en/skills ("in every parent +// directory up to the repository root"; up to the worktree root in a linked +// worktree). Outside a repository only cwd is returned. cwd must be +// resolved (see resolvePath), so a working directory reached through a symlink +// walks the parents of the directory it points to. +func projectDirs(cwd string) []string { + dirs := []string{cwd} + root, err := folders.FindDirWithLeaf(cwd, ".git") + if err != nil { + return dirs + } + for dir := cwd; dir != root; { + parent := filepath.Dir(dir) + if parent == dir { + break + } + dir = parent + dirs = append(dirs, dir) + } + return dirs +} diff --git a/libs/aitools/agents/calling_test.go b/libs/aitools/agents/calling_test.go new file mode 100644 index 00000000000..9a4b01f815c --- /dev/null +++ b/libs/aitools/agents/calling_test.go @@ -0,0 +1,341 @@ +package agents_test + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/databricks/cli/internal/testutil" + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/env" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCallingAgent(t *testing.T) { + cases := []struct { + name string + env []string + want string // registry agent name; "" means no calling agent + }{ + {name: "claude code", env: []string{"CLAUDECODE", "1"}, want: agents.NameClaudeCode}, + {name: "cursor", env: []string{"CURSOR_AGENT", "1"}, want: agents.NameCursor}, + {name: "codex", env: []string{"CODEX_CI", "1"}, want: agents.NameCodex}, + {name: "opencode", env: []string{"OPENCODE", "1"}, want: agents.NameOpenCode}, + {name: "copilot", env: []string{"COPILOT_CLI", "1"}, want: agents.NameCopilot}, + {name: "antigravity", env: []string{"ANTIGRAVITY_AGENT", "1"}, want: agents.NameAntigravity}, + {name: "pi", env: []string{"PI_CODING_AGENT", "1"}, want: agents.NamePi}, + {name: "gemini", env: []string{"GEMINI_CLI", "1"}, want: agents.NameGemini}, + {name: "goose", env: []string{"GOOSE_TERMINAL", "1"}, want: agents.NameGoose}, + {name: "kiro", env: []string{"KIRO", "1"}, want: agents.NameKiro}, + {name: "agent the CLI cannot install into", env: []string{"WINDSURF_AGENT", "1"}}, + {name: "free-text agent", env: []string{"AGENT", "some-internal-tool"}}, + {name: "free-text agent named like a registry agent", env: []string{"AGENT", "copilot"}, want: agents.NameCopilot}, + {name: "no agent"}, + {name: "several agents", env: []string{"CLAUDECODE", "1", "CURSOR_AGENT", "1"}}, + } + + // Every registry agent must be reachable through its SDK env var, or it + // could never be the calling agent. + covered := map[string]bool{} + for _, tc := range cases { + covered[tc.want] = true + } + for _, a := range agents.Registry { + assert.True(t, covered[a.Name], "no case reaches registry agent %q", a.Name) + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + testutil.SetAgentEnv(t, tc.env...) + a := agents.CallingAgent() + if tc.want == "" { + assert.Nil(t, a) + return + } + require.NotNil(t, a) + assert.Equal(t, tc.want, a.Name) + }) + } +} + +// toolingFixture isolates HOME, the working directory and agent config env so +// DatabricksTooling reads only what the test writes. cwd holds a .git, so the +// project lookup never walks above it into the host's directories. +func toolingFixture(t *testing.T) (home, cwd string) { + home = t.TempDir() + cwd = t.TempDir() + require.NoError(t, os.Mkdir(filepath.Join(cwd, ".git"), 0o755)) + t.Chdir(cwd) + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("CLAUDE_CONFIG_DIR", "") + t.Setenv("PI_CODING_AGENT_DIR", "") + return home, cwd +} + +func mkdir(t *testing.T, parts ...string) { + require.NoError(t, os.MkdirAll(filepath.Join(parts...), 0o755)) +} + +func writeFile(t *testing.T, path, body string) { + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + require.NoError(t, os.WriteFile(path, []byte(body), 0o600)) +} + +// writeClaudeRecord writes a Claude manifest with one databricks plugin record +// in the given scope and project path (the field Claude Code writes for the +// project and local scopes). +func writeClaudeRecord(t *testing.T, home, scope, projectPath string) { + record := map[string]string{"scope": scope, "version": "0.2.9"} + if projectPath != "" { + record["projectPath"] = projectPath + } + body, err := json.Marshal(map[string]any{ + "version": 2, + "plugins": map[string]any{"databricks@claude-plugins-official": []any{record}}, + }) + require.NoError(t, err) + writeFile(t, filepath.Join(home, ".claude", "plugins", "installed_plugins.json"), string(body)) +} + +func TestDatabricksTooling(t *testing.T) { + cases := []struct { + name string + agent string + setup func(t *testing.T, home, cwd string) + // recorded is what pluginRecorded (the CLI install state) reports. + recorded bool + want agents.Tooling + }{ + { + name: "nothing installed", + agent: agents.NameClaudeCode, + setup: func(t *testing.T, home, cwd string) {}, + want: agents.ToolingNone, + }, + { + name: "claude plugin in its own manifest", + agent: agents.NameClaudeCode, + setup: func(t *testing.T, home, cwd string) { + writeFile(t, filepath.Join(home, ".claude", "plugins", "installed_plugins.json"), + `{"plugins":{"databricks@claude-plugins-official":[{"version":"0.2.9"}]}}`) + }, + want: agents.ToolingPlugin, + }, + { + name: "stale claude record in the CLI state, plugin gone from claude's manifest", + agent: agents.NameClaudeCode, + setup: func(t *testing.T, home, cwd string) { + writeFile(t, filepath.Join(home, ".claude", "plugins", "installed_plugins.json"), `{"plugins":{}}`) + }, + recorded: true, + want: agents.ToolingNone, + }, + { + name: "stale claude record in the CLI state, no claude manifest at all", + agent: agents.NameClaudeCode, + setup: func(t *testing.T, home, cwd string) {}, + recorded: true, + want: agents.ToolingNone, + }, + { + name: "claude project-scope plugin for another project", + agent: agents.NameClaudeCode, + setup: func(t *testing.T, home, cwd string) { + writeClaudeRecord(t, home, "project", filepath.Join(home, "repoA")) + }, + want: agents.ToolingNone, + }, + { + name: "claude project-scope plugin for a sibling with a shared prefix", + agent: agents.NameClaudeCode, + setup: func(t *testing.T, home, cwd string) { + writeClaudeRecord(t, home, "project", cwd+"x") + }, + want: agents.ToolingNone, + }, + { + name: "claude project-scope plugin for the working directory", + agent: agents.NameClaudeCode, + setup: func(t *testing.T, home, cwd string) { + writeClaudeRecord(t, home, "project", cwd) + }, + want: agents.ToolingPlugin, + }, + { + name: "claude local-scope plugin for an ancestor of the working directory", + agent: agents.NameClaudeCode, + setup: func(t *testing.T, home, cwd string) { + writeClaudeRecord(t, home, "local", filepath.Dir(cwd)) + }, + want: agents.ToolingPlugin, + }, + { + name: "claude project-scope record without a project path", + agent: agents.NameClaudeCode, + setup: func(t *testing.T, home, cwd string) { + writeClaudeRecord(t, home, "project", "") + }, + want: agents.ToolingNone, + }, + { + name: "claude managed-scope plugin", + agent: agents.NameClaudeCode, + setup: func(t *testing.T, home, cwd string) { + writeClaudeRecord(t, home, "managed", "") + }, + want: agents.ToolingPlugin, + }, + { + name: "codex plugin recorded in the CLI state", + agent: agents.NameCodex, + setup: func(t *testing.T, home, cwd string) {}, + recorded: true, + want: agents.ToolingPlugin, + }, + { + name: "copilot plugin recorded in the CLI state", + agent: agents.NameCopilot, + setup: func(t *testing.T, home, cwd string) {}, + recorded: true, + want: agents.ToolingPlugin, + }, + { + name: "another agent's plugin does not count", + agent: agents.NameCodex, + setup: func(t *testing.T, home, cwd string) { + writeFile(t, filepath.Join(home, ".claude", "plugins", "installed_plugins.json"), + `{"plugins":{"databricks@claude-plugins-official":[{"version":"0.2.9"}]}}`) + }, + want: agents.ToolingNone, + }, + { + name: "cursor skills in its own dir", + agent: agents.NameCursor, + setup: func(t *testing.T, home, cwd string) { + mkdir(t, home, ".cursor", "skills", "databricks-apps") + }, + want: agents.ToolingSkills, + }, + { + name: "skills only in the shared store do not count", + agent: agents.NameCursor, + setup: func(t *testing.T, home, cwd string) { + mkdir(t, home, ".databricks", "aitools", "skills", "databricks") + }, + want: agents.ToolingNone, + }, + { + name: "another agent's skills do not count", + agent: agents.NameCodex, + setup: func(t *testing.T, home, cwd string) { + mkdir(t, home, ".cursor", "skills", "databricks") + }, + want: agents.ToolingNone, + }, + { + name: "non-databricks skills do not count", + agent: agents.NameKiro, + setup: func(t *testing.T, home, cwd string) { + mkdir(t, home, ".kiro", "skills", "other-skill") + }, + want: agents.ToolingNone, + }, + { + name: "project-scoped skills", + agent: agents.NameGoose, + setup: func(t *testing.T, home, cwd string) { + mkdir(t, cwd, ".goose", "skills", "databricks") + }, + want: agents.ToolingSkills, + }, + { + name: "project skills at the repository root, CLI in a subdirectory", + agent: agents.NameClaudeCode, + setup: func(t *testing.T, home, cwd string) { + mkdir(t, cwd, ".claude", "skills", "databricks-apps") + mkdir(t, cwd, "jobs", "src") + t.Chdir(filepath.Join(cwd, "jobs", "src")) + }, + want: agents.ToolingSkills, + }, + { + name: "project skills above the repository root do not count", + agent: agents.NameClaudeCode, + setup: func(t *testing.T, home, cwd string) { + mkdir(t, cwd, ".claude", "skills", "databricks-apps") + // A git worktree's .git is a file; the walk stops there too. + writeFile(t, filepath.Join(cwd, "repo", ".git"), "gitdir: elsewhere\n") + mkdir(t, cwd, "repo", "jobs") + t.Chdir(filepath.Join(cwd, "repo", "jobs")) + }, + want: agents.ToolingNone, + }, + { + name: "project skills found through a symlinked working directory", + agent: agents.NameClaudeCode, + setup: func(t *testing.T, home, cwd string) { + mkdir(t, cwd, "repo", ".git") + mkdir(t, cwd, "repo", ".claude", "skills", "databricks-apps") + mkdir(t, cwd, "repo", "jobs") + alias := filepath.Join(cwd, "alias") + if err := os.Symlink(filepath.Join(cwd, "repo", "jobs"), alias); err != nil { + t.Skipf("cannot create a symlink: %v", err) + } + // t.Chdir sets PWD, so os.Getwd reports the alias. + t.Chdir(alias) + }, + want: agents.ToolingSkills, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + home, cwd := toolingFixture(t) + tc.setup(t, home, cwd) + ctx := env.WithUserHomeDir(t.Context(), home) + a := agents.ByName(tc.agent) + require.NotNil(t, a) + assert.Equal(t, tc.want, a.DatabricksTooling(ctx, func() bool { return tc.recorded })) + }) + } +} + +func TestDatabricksToolingClaudePluginInSeveralMarketplaces(t *testing.T) { + home, _ := toolingFixture(t) + // A user-scope install from one marketplace applies everywhere, even next + // to a project-scope install from another marketplace for another project. + body, err := json.Marshal(map[string]any{ + "plugins": map[string]any{ + "databricks@claude-plugins-official": []any{map[string]string{"scope": "user", "version": "0.2.9"}}, + "databricks@databricks-agent-skills": []any{map[string]string{"scope": "project", "version": "0.2.9", "projectPath": filepath.Join(home, "repoA")}}, + }, + }) + require.NoError(t, err) + writeFile(t, filepath.Join(home, ".claude", "plugins", "installed_plugins.json"), string(body)) + ctx := env.WithUserHomeDir(t.Context(), home) + a := agents.ByName(agents.NameClaudeCode) + require.NotNil(t, a) + // TestDatabricksPluginVersionAcrossMarketplaces checks that the records of + // every marketplace entry are read. + assert.Equal(t, agents.ToolingPlugin, a.DatabricksTooling(ctx, func() bool { return false })) +} + +// The CLI install state is not read for agents whose own manifest is +// authoritative (Claude Code) or that have no plugin (Cursor). +func TestDatabricksToolingSkipsInstallState(t *testing.T) { + for _, name := range []string{agents.NameClaudeCode, agents.NameCursor} { + t.Run(name, func(t *testing.T) { + home, _ := toolingFixture(t) + ctx := env.WithUserHomeDir(t.Context(), home) + a := agents.ByName(name) + require.NotNil(t, a) + a.DatabricksTooling(ctx, func() bool { + t.Error("pluginRecorded called") + return true + }) + }) + } +} diff --git a/libs/aitools/agents/hint.go b/libs/aitools/agents/hint.go index 802c04bbc90..5cd331130f8 100644 --- a/libs/aitools/agents/hint.go +++ b/libs/aitools/agents/hint.go @@ -92,6 +92,9 @@ func isAIToolsCommand(cmd *cobra.Command) bool { // `claude plugin install`), and databricks skill files in the shared canonical // skills dir (written by `aitools install --skills-only`). Any read/parse // failure reports false, so an unreadable state errs toward hinting. +// (*Agent).DatabricksTooling, which feeds the User-Agent, differs on purpose: +// it ignores the shared store and out-of-project records, and counts +// .claude/skills dirs, to report what the calling agent loads. func claudeToolingInstalled(ctx context.Context) bool { claude := ByName(NameClaudeCode) if claude == nil { diff --git a/libs/aitools/agents/plugins.go b/libs/aitools/agents/plugins.go index ce8d2eeef6a..794d5c7f4d2 100644 --- a/libs/aitools/agents/plugins.go +++ b/libs/aitools/agents/plugins.go @@ -16,63 +16,148 @@ import ( const pluginManifestFile = "installed_plugins.json" // pluginInstall is one install record in an agent's plugin manifest. A plugin -// installed in more than one scope has several records; only the version is -// consumed here. +// installed in more than one scope has several records. Claude Code documents +// the record's scope and version and what each scope covers: +// https://code.claude.com/docs/en/plugins/loading#check-which-stage-a-plugin-reached +// https://code.claude.com/docs/en/plugins/install#choose-an-install-scope type pluginInstall struct { Version string `json:"version"` + // Scope is Claude Code's install scope: "managed", "user", "project" or + // "local". + Scope string `json:"scope"` + // ProjectPath is the project a project or local scope record applies to. + // It is not in Claude Code's public docs; Claude Code writes it for those + // scopes. + ProjectPath string `json:"projectPath"` +} + +// appliesTo reports whether the agent loads this install when run in cwd: +// always for machine-wide scopes, and for project and local scopes only inside +// the recorded project (cwd is the project or a directory under it). A record +// with an unknown scope counts, so a new machine-wide scope is not missed. cwd +// must be resolved (see resolvePath). +func (p pluginInstall) appliesTo(cwd string) bool { + switch p.Scope { + case "project", "local": + if p.ProjectPath == "" || cwd == "" { + return false + } + return isSameOrUnder(p.ProjectPath, cwd) + default: + return true + } +} + +// isSameOrUnder reports whether dir is root or a directory under it. dir must +// be resolved (see resolvePath); root is resolved here. +func isSameOrUnder(root, dir string) bool { + root = resolvePath(root) + rel, err := filepath.Rel(root, dir) + if err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) { + return true + } + // Two spellings can still name one directory: a different case on macOS's + // case-insensitive APFS (Rel folds case only on Windows), a Windows 8.3 + // short name, or a bind mount. Compare file identity with dir and each of + // its ancestors; the walk is bounded by dir's depth. + rootInfo, err := os.Stat(root) + if err != nil { + return false + } + for { + if info, err := os.Stat(dir); err == nil && os.SameFile(rootInfo, info) { + return true + } + parent := filepath.Dir(dir) + if parent == dir { + return false + } + dir = parent + } +} + +// resolvePath cleans path and resolves symlinks when it can (e.g. macOS /var +// -> /private/var), so a project path and a working directory spelled +// differently still compare equal. +func resolvePath(path string) string { + if resolved, err := filepath.EvalSymlinks(path); err == nil { + return resolved + } + return filepath.Clean(path) } // DatabricksPluginVersion reports the recorded version of the databricks plugin -// in the agent's own plugin manifest and whether the plugin is installed at all. -// This catches installs done through `databricks aitools install` and a direct -// ` plugin install`, unlike the CLI's own state file, which only records -// CLI-driven installs. Each agent's manifest format differs, so parsing is -// delegated to a per-agent reader; agents without a verified reader report -// ("", false), i.e. "not installed as far as we can tell". +// in the agent's own plugin manifest and whether the plugin is installed at all, +// in any scope or project. This catches installs done through `databricks +// aitools install` and a direct ` plugin install`, unlike the CLI's own +// state file, which only records CLI-driven installs. Each agent's manifest +// format differs, so parsing is delegated to a per-agent reader; agents without +// a verified reader report ("", false), i.e. "not installed as far as we can +// tell". When the plugin is recorded for more than one scope or marketplace, +// the highest recorded version is returned (they match in the common +// single-scope case, but the cache can retain a stale scope); the version is "" +// when installed but unversioned. func (a *Agent) DatabricksPluginVersion(ctx context.Context) (string, bool) { - if a.pluginVersion == nil { + if a.pluginInstalls == nil { + return "", false + } + installs, ok := a.pluginInstalls(ctx, a) + if !ok { return "", false } - return a.pluginVersion(ctx, a) + best := "" + for _, install := range installs { + // Compare as semver (versions are unprefixed, e.g. "0.2.9"). + if install.Version != "" && (best == "" || semver.Compare("v"+install.Version, "v"+best) > 0) { + best = install.Version + } + } + return best, true } -// claudePluginVersion reads the databricks plugin version from Claude Code's +// databricksPluginActiveIn reports whether the agent's own manifest has a +// databricks plugin record that applies in cwd (see pluginInstall.appliesTo). +// A plugin disabled in the agent's settings still counts. +func (a *Agent) databricksPluginActiveIn(ctx context.Context, cwd string) bool { + installs, _ := a.pluginInstalls(ctx, a) + for _, install := range installs { + if install.appliesTo(cwd) { + return true + } + } + return false +} + +// claudePluginInstalls reads the databricks plugin's records from Claude Code's // installed_plugins.json (/plugins/), which keys each entry as // "@" (e.g. "databricks@claude-plugins-official") with one -// record per install scope. Any read/parse failure reports ("", false), so an -// unreadable manifest reads as "not installed". When the plugin is recorded for -// more than one scope, the highest recorded version is returned (they match in -// the common single-scope case, but the cache can retain a stale scope); the -// version is "" when installed but unversioned. -func claudePluginVersion(ctx context.Context, a *Agent) (string, bool) { +// record per install scope. The plugin can be installed from more than one +// marketplace, so the records of every databricks entry are returned. Any +// read/parse failure reports (nil, false), so an unreadable manifest reads as +// "not installed". +func claudePluginInstalls(ctx context.Context, a *Agent) ([]pluginInstall, bool) { configDir, err := a.ConfigDir(ctx) if err != nil { - return "", false + return nil, false } data, err := os.ReadFile(filepath.Join(configDir, "plugins", pluginManifestFile)) if err != nil { - return "", false + return nil, false } var manifest struct { Plugins map[string][]pluginInstall `json:"plugins"` } if err := json.Unmarshal(data, &manifest); err != nil { - return "", false + return nil, false } + var all []pluginInstall + found := false for key, installs := range manifest.Plugins { id, _, _ := strings.Cut(key, "@") - if id != databricksPluginID { - continue - } - // Installed. Report the highest version across scopes; - best := "" - for _, install := range installs { - // Compare as semver (versions are unprefixed, e.g. "0.2.9"). - if install.Version != "" && (best == "" || semver.Compare("v"+install.Version, "v"+best) > 0) { - best = install.Version - } + if id == databricksPluginID { + all = append(all, installs...) + found = true } - return best, true } - return "", false + return all, found } diff --git a/libs/aitools/agents/plugins_test.go b/libs/aitools/agents/plugins_test.go index 74308d5aa80..893449ccfba 100644 --- a/libs/aitools/agents/plugins_test.go +++ b/libs/aitools/agents/plugins_test.go @@ -14,10 +14,10 @@ import ( // Claude Code's manifest reader (the format these tests write). func agentWithConfigDir(dir string) *Agent { return &Agent{ - Name: "test-agent", - DisplayName: "Test Agent", - ConfigDir: func(_ context.Context) (string, error) { return dir, nil }, - pluginVersion: claudePluginVersion, + Name: "test-agent", + DisplayName: "Test Agent", + ConfigDir: func(_ context.Context) (string, error) { return dir, nil }, + pluginInstalls: claudePluginInstalls, } } @@ -59,6 +59,75 @@ func TestDatabricksPluginVersion(t *testing.T) { } } +func TestDatabricksPluginVersionAcrossMarketplaces(t *testing.T) { + configDir := t.TempDir() + writeManifest(t, configDir, `{"plugins":{"databricks@a":[{"scope":"user","version":"0.2.9"}],"databricks@b":[{"scope":"user","version":"0.3.0"}]}}`) + a := agentWithConfigDir(configDir) + installs, ok := claudePluginInstalls(t.Context(), a) + require.True(t, ok) + assert.Len(t, installs, 2) + version, ok := a.DatabricksPluginVersion(t.Context()) + require.True(t, ok) + assert.Equal(t, "0.3.0", version) +} + +// projectFixture creates /Project/sub and returns the project dir and the +// sub dir, with root resolved so the paths are the ones the OS reports. +func projectFixture(t *testing.T) (project, sub string) { + root, err := filepath.EvalSymlinks(t.TempDir()) + require.NoError(t, err) + project = filepath.Join(root, "Project") + sub = filepath.Join(project, "sub") + require.NoError(t, os.MkdirAll(sub, 0o755)) + return project, sub +} + +// Every record counts, not only the first one the reader returns. +func TestDatabricksPluginActiveInChecksEveryRecord(t *testing.T) { + project, _ := projectFixture(t) + a := &Agent{pluginInstalls: func(context.Context, *Agent) ([]pluginInstall, bool) { + return []pluginInstall{ + {Scope: "project", ProjectPath: filepath.Join(filepath.Dir(project), "Other")}, + {Scope: "user"}, + }, true + }} + assert.True(t, a.databricksPluginActiveIn(t.Context(), project)) +} + +func TestPluginInstallAppliesToProjectOutsideWorkingDir(t *testing.T) { + project, sub := projectFixture(t) + sibling := filepath.Join(filepath.Dir(project), "Other") + require.NoError(t, os.Mkdir(sibling, 0o755)) + + // Existing directories, so the comparison by file identity runs too. + for _, cwd := range []string{sibling, filepath.Dir(project)} { + assert.False(t, pluginInstall{Scope: "project", ProjectPath: project}.appliesTo(cwd), cwd) + } + assert.False(t, pluginInstall{Scope: "local", ProjectPath: sub}.appliesTo(project)) +} + +func TestPluginInstallAppliesToSymlinkedProjectPath(t *testing.T) { + project, sub := projectFixture(t) + alias := filepath.Join(filepath.Dir(project), "alias") + if err := os.Symlink(project, alias); err != nil { + t.Skipf("cannot create a symlink: %v", err) + } + for _, cwd := range []string{project, sub} { + assert.True(t, pluginInstall{Scope: "project", ProjectPath: alias}.appliesTo(cwd), cwd) + } +} + +func TestPluginInstallAppliesToProjectPathInAnotherCase(t *testing.T) { + project, sub := projectFixture(t) + upper := filepath.Join(filepath.Dir(project), "PROJECT") + if _, err := os.Stat(upper); err != nil { + t.Skip("the temp filesystem is case-sensitive") + } + for _, cwd := range []string{project, sub} { + assert.True(t, pluginInstall{Scope: "project", ProjectPath: upper}.appliesTo(cwd), cwd) + } +} + func TestDatabricksPluginVersionWithoutReader(t *testing.T) { // An agent with no per-agent manifest reader (its format is unverified) // reports no version, even when an installed_plugins.json is present, so its diff --git a/libs/aitools/installer/useragent.go b/libs/aitools/installer/useragent.go index d0c9894582a..95a7cab6492 100644 --- a/libs/aitools/installer/useragent.go +++ b/libs/aitools/installer/useragent.go @@ -3,6 +3,8 @@ package installer import ( "context" + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go/useragent" ) @@ -15,6 +17,13 @@ const aiToolsUserAgentKey = "aitools" // into databricks-agent-skills, but we track ai-dev-kit installs separately. const aiDevKitUserAgentKey = "aidevkit" +// callingAgentToolingUserAgentKey is the user-agent key for the Databricks AI +// tooling of the agent calling the CLI, read from that agent's own locations +// (see agents.Agent.DatabricksTooling). Paired with the SDK's agent/ key +// it gives a per-calling-agent install rate, which the aitools/ pairs cannot: +// they report plugins of every tool on the machine. +const callingAgentToolingUserAgentKey = "aitools-agent" + // WithAiToolsInUserAgent adds one aitools/_ pair to the user // agent for each coding tool that has the Databricks plugin installed, e.g. // "aitools/claude-code_0.2.9 aitools/codex_0.2.9". Nothing is added when no tool @@ -45,3 +54,36 @@ func WithAiDevKitInUserAgent(ctx context.Context) context.Context { } return useragent.InContext(ctx, aiDevKitUserAgentKey, version) } + +// WithCallingAgentToolingInUserAgent adds "aitools-agent/" +// for the registry agent calling the CLI. Nothing is added when no agent is +// detected, several are, or the CLI cannot install into the agent. +func WithCallingAgentToolingInUserAgent(ctx context.Context) context.Context { + a := agents.CallingAgent() + if a == nil { + return ctx + } + tooling := a.DatabricksTooling(ctx, func() bool { return globalPluginRecorded(ctx, a.Name) }) + return useragent.InContext(ctx, callingAgentToolingUserAgentKey, tooling.String()) +} + +// globalPluginRecorded reports whether the global CLI install state records a +// databricks plugin install for the agent. Only the global state is read: the +// CLI installs plugins in project scope only for agents with SupportsProjectScope +// (see mapAgentScope in cmd/aitools), and those have a manifest reader. +func globalPluginRecorded(ctx context.Context, agentName string) bool { + dir, err := GlobalSkillsDir(ctx) + if err != nil { + return false + } + state, err := LoadState(dir) + if err != nil { + log.Debugf(ctx, "Could not load AI tools install state in %s: %v", dir, err) + return false + } + if state == nil { + return false + } + _, ok := state.Plugins[agentName] + return ok +} diff --git a/libs/aitools/installer/useragent_test.go b/libs/aitools/installer/useragent_test.go index 9c8aeb9c575..916d3209f0a 100644 --- a/libs/aitools/installer/useragent_test.go +++ b/libs/aitools/installer/useragent_test.go @@ -1,11 +1,14 @@ package installer import ( + "os" "path/filepath" "testing" + "github.com/databricks/cli/internal/testutil" "github.com/databricks/databricks-sdk-go/useragent" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // TestWithAiToolsInUserAgent is an end-to-end test of the top-level call tree: @@ -117,3 +120,61 @@ func TestWithAiDevKitInUserAgent(t *testing.T) { }) } } + +// TestWithCallingAgentToolingInUserAgent checks that the segment reflects only +// the calling agent's own tooling: a plugin installed for Claude Code does not +// make Codex count as installed. +func TestWithCallingAgentToolingInUserAgent(t *testing.T) { + tests := []struct { + name string + agentEnv []string + claude string // claude-code manifest version, when set + plugins map[string]string + state string // raw global install state, when set + skill string // skill dir to create, relative to HOME, when set + want string // "" means no aitools-agent segment + }{ + {name: "no calling agent", claude: "0.2.9", want: ""}, + {name: "agent the CLI cannot install into", agentEnv: []string{"WINDSURF_AGENT", "1"}, want: ""}, + {name: "free-text agent", agentEnv: []string{"AGENT", "my-tool"}, want: ""}, + {name: "claude with its plugin", agentEnv: []string{"CLAUDECODE", "1"}, claude: "0.2.9", want: "aitools-agent/plugin"}, + {name: "codex without tooling while claude has it", agentEnv: []string{"CODEX_CI", "1"}, claude: "0.2.9", want: "aitools-agent/none"}, + {name: "codex with its recorded plugin", agentEnv: []string{"CODEX_CI", "1"}, plugins: map[string]string{"codex": "0.2.8"}, want: "aitools-agent/plugin"}, + {name: "codex while the CLI state records only claude's plugin", agentEnv: []string{"CODEX_CI", "1"}, plugins: map[string]string{"claude-code": "0.2.8"}, want: "aitools-agent/none"}, + {name: "codex with a corrupt CLI state", agentEnv: []string{"CODEX_CI", "1"}, state: "{not json", want: "aitools-agent/none"}, + {name: "claude with only a stale CLI record", agentEnv: []string{"CLAUDECODE", "1"}, plugins: map[string]string{"claude-code": "0.2.8"}, want: "aitools-agent/none"}, + {name: "claude with only skills", agentEnv: []string{"CLAUDECODE", "1"}, skill: filepath.Join(".claude", "skills", "databricks-apps"), want: "aitools-agent/skills"}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + testutil.SetAgentEnv(t, tc.agentEnv...) + tmp := t.TempDir() + cwd := t.TempDir() + t.Setenv("HOME", tmp) + t.Setenv("USERPROFILE", tmp) + // A .git in cwd keeps the project lookup out of the host's directories. + require.NoError(t, os.Mkdir(filepath.Join(cwd, ".git"), 0o755)) + t.Chdir(cwd) + if tc.claude != "" { + writeClaudeManifest(t, filepath.Join(tmp, ".claude"), tc.claude) + } + savePluginState(t, GlobalSkillsDir, tc.plugins) + if tc.state != "" { + dir, err := GlobalSkillsDir(t.Context()) + require.NoError(t, err) + require.NoError(t, os.MkdirAll(dir, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(dir, stateFileName), []byte(tc.state), 0o600)) + } + if tc.skill != "" { + require.NoError(t, os.MkdirAll(filepath.Join(tmp, tc.skill), 0o755)) + } + + ua := useragent.FromContext(WithCallingAgentToolingInUserAgent(t.Context())) + if tc.want == "" { + assert.NotContains(t, ua, "aitools-agent/") + return + } + assert.Contains(t, ua, tc.want) + }) + } +}