From fd8c2c591277fa59a32ff851c430ed932fc51dd3 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Tue, 10 Nov 2026 14:35:39 +0100 Subject: [PATCH 1/6] aitools: report the calling agent's own Databricks tooling in the User-Agent When a coding agent runs the CLI, the SDK already puts agent/ in the User-Agent. Add aitools-agent/{plugin|skills|none} next to it: whether that agent has the Databricks plugin or Databricks skills installed in its own locations. The existing aitools/_ pairs list the plugins of every tool on the machine, so they cannot answer this. - agents.CallingAgent maps useragent.AgentProvider() to a registry agent. - (*Agent).DatabricksTooling checks the agent's own plugin manifest (Claude Code; project/local scope records count only inside their project), or the global CLI install state for agents without a manifest reader, and databricks* dirs in the agent's global and project skills dirs (looked up from the working directory up to the repository root). - The Claude Code manifest reader returns every databricks record from every marketplace entry; DatabricksPluginVersion reports the highest version instead of the first entry in random map order. - Project paths match by lexical containment after resolving symlinks, then by file identity (case-insensitive filesystems, 8.3 names, bind mounts). Co-authored-by: Isaac --- cmd/root/root.go | 1 + libs/aitools/agents/agents.go | 11 +- libs/aitools/agents/agenttest/agenttest.go | 33 ++ libs/aitools/agents/calling.go | 173 +++++++++++ libs/aitools/agents/calling_test.go | 343 +++++++++++++++++++++ libs/aitools/agents/plugins.go | 145 +++++++-- libs/aitools/agents/plugins_linux_test.go | 57 ++++ libs/aitools/agents/plugins_test.go | 68 +++- libs/aitools/installer/useragent.go | 30 ++ libs/aitools/installer/useragent_test.go | 54 ++++ 10 files changed, 874 insertions(+), 41 deletions(-) create mode 100644 libs/aitools/agents/agenttest/agenttest.go create mode 100644 libs/aitools/agents/calling.go create mode 100644 libs/aitools/agents/calling_test.go create mode 100644 libs/aitools/agents/plugins_linux_test.go 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/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/agenttest/agenttest.go b/libs/aitools/agents/agenttest/agenttest.go new file mode 100644 index 00000000000..e6ca20bfd0d --- /dev/null +++ b/libs/aitools/agents/agenttest/agenttest.go @@ -0,0 +1,33 @@ +// Package agenttest controls which coding agent useragent.AgentProvider() +// reports in tests. +package agenttest + +import ( + "testing" + + "github.com/databricks/cli/internal/testutil" + "github.com/databricks/databricks-sdk-go/useragent" +) + +// SetAgentEnv clears the environment (the agent running the test binary would +// otherwise leak in), then sets the given key/value pairs, and resets the SDK's +// process-wide AgentProvider cache before and after the test. With no +// arguments AgentProvider() reports no agent. Unlike testutil.SetAgent, which +// sets only AGENT, it sets the agents' own env vars (e.g. CLAUDECODE=1), and +// several at once for the "multiple" case. Tests using it must set any other +// env they need (HOME, ...) afterwards and must not run in parallel. A test +// that uses t.TempDir must call it once before SetAgentEnv: the first call +// picks the temp root from TMPDIR / TEMP, which SetAgentEnv clears (on Windows +// the fallback is not writable for a standard user). +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)) + } + testutil.NullEnvironment(t) + for i := 0; i < len(kv); i += 2 { + t.Setenv(kv[i], kv[i+1]) + } + useragent.ClearCache() + t.Cleanup(useragent.ClearCache) +} diff --git a/libs/aitools/agents/calling.go b/libs/aitools/agents/calling.go new file mode 100644 index 00000000000..b83d5ddf205 --- /dev/null +++ b/libs/aitools/agents/calling.go @@ -0,0 +1,173 @@ +package agents + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + + "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/folders" + "github.com/databricks/cli/libs/log" + "github.com/databricks/databricks-sdk-go/useragent" +) + +// SDK agent products (useragent.AgentProvider values) whose names differ from +// the registry name. +// https://github.com/databricks/databricks-sdk-go/blob/main/useragent/agent.go +const ( + providerCopilot = "copilot-cli" + providerGemini = "gemini-cli" +) + +// 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, +} + +// 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 { + 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 +) + +// DatabricksTooling reports the Databricks AI tooling installed for this agent +// in the agent's own locations, so tooling installed for one agent never counts +// for another: +// - the databricks plugin in the agent's own plugin manifest, for agents with +// a verified manifest reader (Claude Code), counting a project or local +// scope record only inside its project. That manifest is authoritative: +// a plugin removed with the agent's own CLI leaves a stale CLI state record +// behind, so the record is not consulted for these agents. A plugin that +// is installed but disabled in the agent's settings still counts; +// - for agents without a reader (Codex, Copilot), the plugin recorded for +// this agent in the global CLI install state; +// - a databricks* skill directory in the agent's global skills dir, or in its +// project skills dir. This covers `aitools install`, `--skills-only` and +// `apps init`, which all place skills there (as symlinks or copies). +// +// Project skills dirs are looked up in the working directory and its parents +// up to the repository root (see projectDirs), because the agent's project is +// often a parent of the directory the CLI runs in. +// +// The shared ~/.databricks/aitools/skills store is deliberately not consulted: +// it holds the skills of every agent installed into, so it cannot tell which +// agent has them. Installs the CLI did not perform and that leave no trace in +// these locations (e.g. a Codex plugin installed with `codex` directly) read as +// ToolingNone. Read errors read as "not installed". +func (a *Agent) DatabricksTooling(ctx context.Context) Tooling { + cwd, cwdErr := os.Getwd() + if cwdErr != nil { + cwd = "" + } + if a.pluginInstalls != nil { + if a.databricksPluginActiveIn(ctx, cwd) { + return ToolingPlugin + } + } else if pluginRecorded(ctx, a.Name) { + 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. Outside a repository only cwd is returned. cwd is resolved +// first (see resolvePath), so a working directory reached through a symlink +// walks the parents of the directory it points to. +func projectDirs(cwd string) []string { + cwd = resolvePath(cwd) + 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 +} + +// HasDatabricksTooling reports whether the agent has the databricks plugin or +// Databricks skills in its own locations (see DatabricksTooling). +func (a *Agent) HasDatabricksTooling(ctx context.Context) bool { + return a.DatabricksTooling(ctx) != ToolingNone +} + +// installStateFile is the CLI install state written by libs/aitools/installer +// (InstallState) in each scope's canonical skills dir. Only its plugins map is +// read here; the installer package imports this one, so it cannot be reused. +const installStateFile = ".state.json" + +// pluginRecorded 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 pluginRecorded(ctx context.Context, agentName string) bool { + home, err := env.UserHomeDir(ctx) + if err != nil { + return false + } + dir := filepath.Join(home, CanonicalSkillsDir) + data, err := os.ReadFile(filepath.Join(dir, installStateFile)) + if err != nil { + return false + } + var state struct { + Plugins map[string]json.RawMessage `json:"plugins"` + } + if err := json.Unmarshal(data, &state); err != nil { + log.Debugf(ctx, "Could not parse AI tools install state in %s: %v", dir, err) + return false + } + _, ok := state.Plugins[agentName] + return ok +} diff --git a/libs/aitools/agents/calling_test.go b/libs/aitools/agents/calling_test.go new file mode 100644 index 00000000000..7aaf89e1e4a --- /dev/null +++ b/libs/aitools/agents/calling_test.go @@ -0,0 +1,343 @@ +package agents_test + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/agents/agenttest" + "github.com/databricks/cli/libs/env" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestAgentForProvider(t *testing.T) { + assert.Equal(t, agents.NameCopilot, agents.AgentForProvider("copilot-cli").Name) + assert.Equal(t, agents.NameGemini, agents.AgentForProvider("gemini-cli").Name) + assert.Equal(t, agents.NameClaudeCode, agents.AgentForProvider("claude-code").Name) + assert.Nil(t, agents.AgentForProvider("windsurf")) + assert.Nil(t, agents.AgentForProvider("multiple")) + assert.Nil(t, agents.AgentForProvider("")) + assert.Nil(t, agents.AgentForProvider("copilot")) +} + +// Every registry agent must be reachable from some SDK product, or it could +// never be the calling agent. +func TestAgentForProviderCoversRegistry(t *testing.T) { + products := []string{"antigravity", "claude-code", "codex", "copilot-cli", "cursor", "gemini-cli", "goose", "kiro", "opencode", "pi"} + reached := map[string]bool{} + for _, p := range products { + if a := agents.AgentForProvider(p); a != nil { + reached[a.Name] = true + } + } + for _, a := range agents.Registry { + assert.True(t, reached[a.Name], "registry agent %q has no SDK product mapping", a.Name) + } +} + +func TestCallingAgentFromEnv(t *testing.T) { + agenttest.SetAgentEnv(t, "COPILOT_CLI", "1") + assert.Equal(t, agents.NameCopilot, agents.CallingAgent().Name) +} + +func TestCallingAgentFreeText(t *testing.T) { + agenttest.SetAgentEnv(t, "AGENT", "some-internal-tool") + assert.Nil(t, agents.CallingAgent()) +} + +func TestCallingAgentNone(t *testing.T) { + agenttest.SetAgentEnv(t) + assert.Nil(t, agents.CallingAgent()) +} + +func TestCallingAgentMultiple(t *testing.T) { + agenttest.SetAgentEnv(t, "CLAUDECODE", "1", "CURSOR_AGENT", "1") + assert.Nil(t, agents.CallingAgent()) +} + +// 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 requires 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) + 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":{}}`) + writeFile(t, filepath.Join(home, ".databricks", "aitools", "skills", ".state.json"), + `{"schema_version":2,"plugins":{"claude-code":{"marketplace":"claude-plugins-official","plugin":"databricks"}}}`) + }, + 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) { + writeFile(t, filepath.Join(home, ".databricks", "aitools", "skills", ".state.json"), + `{"schema_version":2,"plugins":{"claude-code":{"plugin":"databricks"}}}`) + }, + 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) { + writeFile(t, filepath.Join(home, ".databricks", "aitools", "skills", ".state.json"), + `{"schema_version":2,"plugins":{"codex":{"marketplace":"databricks-agent-skills","plugin":"databricks"}}}`) + }, + want: agents.ToolingPlugin, + }, + { + name: "copilot plugin recorded in the CLI state", + agent: agents.NameCopilot, + setup: func(t *testing.T, home, cwd string) { + writeFile(t, filepath.Join(home, ".databricks", "aitools", "skills", ".state.json"), + `{"schema_version":2,"plugins":{"copilot":{"plugin":"databricks"}}}`) + }, + 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"}]}}`) + writeFile(t, filepath.Join(home, ".databricks", "aitools", "skills", ".state.json"), + `{"plugins":{"claude-code":{"plugin":"databricks"}}}`) + }, + 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, + }, + { + name: "corrupt state reads as not installed", + agent: agents.NameCodex, + setup: func(t *testing.T, home, cwd string) { + writeFile(t, filepath.Join(home, ".databricks", "aitools", "skills", ".state.json"), `{not json`) + }, + want: agents.ToolingNone, + }, + } + 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)) + assert.Equal(t, tc.want != agents.ToolingNone, a.HasDatabricksTooling(ctx)) + }) + } +} + +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) + // Go randomizes map iteration (for a two-entry map the second entry comes + // first only about one time in eight), so a reader that keeps only the + // first databricks entry fails some of these reads. + got := map[agents.Tooling]int{} + for range 200 { + got[a.DatabricksTooling(ctx)]++ + } + assert.Equal(t, map[agents.Tooling]int{agents.ToolingPlugin: 200}, got) +} diff --git a/libs/aitools/agents/plugins.go b/libs/aitools/agents/plugins.go index ce8d2eeef6a..22fcd2b15d3 100644 --- a/libs/aitools/agents/plugins.go +++ b/libs/aitools/agents/plugins.go @@ -16,63 +16,144 @@ 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. 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 the record applies to. Claude Code requires it + // for the project and local 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. +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. +func isSameOrUnder(root, dir string) bool { + root, dir = resolvePath(root), resolvePath(dir) + 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 +} + +// databricksPluginActiveIn reports whether the agent's own manifest has a +// databricks plugin record that applies in cwd (see pluginInstall.appliesTo). +func (a *Agent) databricksPluginActiveIn(ctx context.Context, cwd string) bool { + if a.pluginInstalls == nil { + return false + } + installs, _ := a.pluginInstalls(ctx, a) + for _, install := range installs { + if install.appliesTo(cwd) { + return true + } + } + return false } -// claudePluginVersion reads the databricks plugin version from Claude Code's +// 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_linux_test.go b/libs/aitools/agents/plugins_linux_test.go new file mode 100644 index 00000000000..1f16c697b71 --- /dev/null +++ b/libs/aitools/agents/plugins_linux_test.go @@ -0,0 +1,57 @@ +package agents + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "syscall" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const bindMountTestChildEnv = "DATABRICKS_CLI_TEST_BIND_MOUNT_CHILD" + +// A bind mount names the same directory under a second path that no symlink +// resolution can map back, the way a case-insensitive filesystem does on macOS +// and Windows, so it exercises the same identity check on Linux. Mounting needs +// a private mount namespace, so the test re-runs itself under `unshare -rm` +// (an unprivileged user namespace) and skips where those are not allowed. +func TestPluginInstallAppliesToBindMountedProjectPath(t *testing.T) { + if os.Getenv(bindMountTestChildEnv) == "" { + unshare, err := exec.LookPath("unshare") + if err != nil { + t.Skip("unshare not found") + } + if err := exec.Command(unshare, "-rm", "true").Run(); err != nil { + t.Skipf("unprivileged mount namespaces are not available: %v", err) + } + cmd := exec.Command(unshare, "-rm", os.Args[0], "-test.run=^"+t.Name()+"$", "-test.count=1", "-test.v") + cmd.Env = append(os.Environ(), bindMountTestChildEnv+"=1") + out, err := cmd.CombinedOutput() + require.NoError(t, err, "%s", out) + if strings.Contains(string(out), "--- SKIP: "+t.Name()) { + t.Skipf("%s", out) + } + assert.Contains(t, string(out), "--- PASS: "+t.Name()) + return + } + + project, sub := projectFixture(t) + alias := filepath.Join(filepath.Dir(project), "alias") + require.NoError(t, os.Mkdir(alias, 0o755)) + // Some hosts (e.g. AppArmor-restricted user namespaces) allow the + // namespace but deny the mount. + if err := syscall.Mount(project, alias, "", syscall.MS_BIND, ""); err != nil { + t.Skipf("bind mount not permitted: %v", err) + } + t.Cleanup(func() { assert.NoError(t, syscall.Unmount(alias, 0)) }) + + for _, cwd := range []string{project, sub} { + assert.True(t, pluginInstall{Scope: "project", ProjectPath: alias}.appliesTo(cwd), cwd) + } + // The mount does not make an unrelated directory count. + assert.False(t, pluginInstall{Scope: "project", ProjectPath: alias}.appliesTo(filepath.Dir(project))) +} diff --git a/libs/aitools/agents/plugins_test.go b/libs/aitools/agents/plugins_test.go index 74308d5aa80..c970beb1f3d 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,66 @@ 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"}]}}`) + // Go randomizes map iteration (for a two-entry map the second entry comes + // first only about one time in eight), so a reader that keeps only the + // first databricks entry fails some of these reads. + got := map[string]int{} + for range 200 { + version, ok := agentWithConfigDir(configDir).DatabricksPluginVersion(t.Context()) + require.True(t, ok) + got[version]++ + } + assert.Equal(t, map[string]int{"0.3.0": 200}, got) +} + +// 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 +} + +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..8cc22cb7253 100644 --- a/libs/aitools/installer/useragent.go +++ b/libs/aitools/installer/useragent.go @@ -3,6 +3,7 @@ package installer import ( "context" + "github.com/databricks/cli/libs/aitools/agents" "github.com/databricks/databricks-sdk-go/useragent" ) @@ -15,6 +16,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 +53,25 @@ 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 + } + return useragent.InContext(ctx, callingAgentToolingUserAgentKey, toolingUserAgentValue(a.DatabricksTooling(ctx))) +} + +func toolingUserAgentValue(t agents.Tooling) string { + switch t { + case agents.ToolingPlugin: + return "plugin" + case agents.ToolingSkills: + return "skills" + default: + return "none" + } +} diff --git a/libs/aitools/installer/useragent_test.go b/libs/aitools/installer/useragent_test.go index 9c8aeb9c575..0e2dac80783 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/libs/aitools/agents/agenttest" "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,54 @@ 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 + 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: "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) { + // The first t.TempDir picks the temp root, so create both before + // SetAgentEnv clears TMPDIR / TEMP. + tmp := t.TempDir() + cwd := t.TempDir() + agenttest.SetAgentEnv(t, tc.agentEnv...) + 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.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) + }) + } +} From 9c0e30dc66be21d89ae17511522ff2616012c1ab Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Tue, 10 Nov 2026 16:40:35 +0000 Subject: [PATCH 2/6] aitools: read the calling agent's CLI plugin record through installer DatabricksTooling no longer parses the CLI install state itself. It takes pluginRecorded, which installer implements with LoadState and GlobalSkillsDir, so a future state change cannot silently miss this reader. pluginRecorded is called only for agents with a plugin and no manifest reader (Codex, Copilot), so agents without a plugin no longer read the state file. The working directory is resolved once per call instead of again for each project-scope record and in projectDirs. Remove HasDatabricksTooling (no production caller) and the nil guard in databricksPluginActiveIn that its only caller already checks. Trim the DatabricksTooling doc to the facts the code does not show. Co-authored-by: Isaac --- libs/aitools/agents/calling.go | 92 +++++------------------- libs/aitools/agents/calling_test.go | 77 ++++++++++---------- libs/aitools/agents/plugins.go | 12 ++-- libs/aitools/installer/useragent.go | 25 ++++++- libs/aitools/installer/useragent_test.go | 9 +++ 5 files changed, 97 insertions(+), 118 deletions(-) diff --git a/libs/aitools/agents/calling.go b/libs/aitools/agents/calling.go index b83d5ddf205..352860e87cd 100644 --- a/libs/aitools/agents/calling.go +++ b/libs/aitools/agents/calling.go @@ -2,13 +2,10 @@ package agents import ( "context" - "encoding/json" "os" "path/filepath" - "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/folders" - "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go/useragent" ) @@ -62,41 +59,27 @@ const ( ToolingPlugin ) -// DatabricksTooling reports the Databricks AI tooling installed for this agent -// in the agent's own locations, so tooling installed for one agent never counts -// for another: -// - the databricks plugin in the agent's own plugin manifest, for agents with -// a verified manifest reader (Claude Code), counting a project or local -// scope record only inside its project. That manifest is authoritative: -// a plugin removed with the agent's own CLI leaves a stale CLI state record -// behind, so the record is not consulted for these agents. A plugin that -// is installed but disabled in the agent's settings still counts; -// - for agents without a reader (Codex, Copilot), the plugin recorded for -// this agent in the global CLI install state; -// - a databricks* skill directory in the agent's global skills dir, or in its -// project skills dir. This covers `aitools install`, `--skills-only` and -// `apps init`, which all place skills there (as symlinks or copies). -// -// Project skills dirs are looked up in the working directory and its parents -// up to the repository root (see projectDirs), because the agent's project is -// often a parent of the directory the CLI runs in. -// -// The shared ~/.databricks/aitools/skills store is deliberately not consulted: -// it holds the skills of every agent installed into, so it cannot tell which -// agent has them. Installs the CLI did not perform and that leave no trace in -// these locations (e.g. a Codex plugin installed with `codex` directly) read as -// ToolingNone. Read errors read as "not installed". -func (a *Agent) DatabricksTooling(ctx context.Context) Tooling { - cwd, cwdErr := os.Getwd() - if cwdErr != nil { - cwd = "" +// 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 includes plugins later removed with the agent's own +// CLI. The shared skills store is not consulted: it cannot attribute skills. +func (a *Agent) DatabricksTooling(ctx context.Context, pluginRecorded func() bool) Tooling { + // Getwd returns "" on error, which skips the project lookups below. + cwd, err := os.Getwd() + if err == nil { + cwd = resolvePath(cwd) } - if a.pluginInstalls != nil { + switch { + case a.pluginInstalls != nil: if a.databricksPluginActiveIn(ctx, cwd) { return ToolingPlugin } - } else if pluginRecorded(ctx, a.Name) { - return ToolingPlugin + case a.Plugin != nil: + if pluginRecorded() { + return ToolingPlugin + } } if dir, err := a.SkillsDir(ctx); err == nil && HasDatabricksSkillsIn(dir) { return ToolingSkills @@ -115,11 +98,10 @@ func (a *Agent) DatabricksTooling(ctx context.Context) Tooling { // 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. Outside a repository only cwd is returned. cwd is resolved -// first (see resolvePath), so a working directory reached through a symlink +// in a subdirectory. 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 { - cwd = resolvePath(cwd) dirs := []string{cwd} root, err := folders.FindDirWithLeaf(cwd, ".git") if err != nil { @@ -135,39 +117,3 @@ func projectDirs(cwd string) []string { } return dirs } - -// HasDatabricksTooling reports whether the agent has the databricks plugin or -// Databricks skills in its own locations (see DatabricksTooling). -func (a *Agent) HasDatabricksTooling(ctx context.Context) bool { - return a.DatabricksTooling(ctx) != ToolingNone -} - -// installStateFile is the CLI install state written by libs/aitools/installer -// (InstallState) in each scope's canonical skills dir. Only its plugins map is -// read here; the installer package imports this one, so it cannot be reused. -const installStateFile = ".state.json" - -// pluginRecorded 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 pluginRecorded(ctx context.Context, agentName string) bool { - home, err := env.UserHomeDir(ctx) - if err != nil { - return false - } - dir := filepath.Join(home, CanonicalSkillsDir) - data, err := os.ReadFile(filepath.Join(dir, installStateFile)) - if err != nil { - return false - } - var state struct { - Plugins map[string]json.RawMessage `json:"plugins"` - } - if err := json.Unmarshal(data, &state); err != nil { - log.Debugf(ctx, "Could not parse AI tools install state in %s: %v", dir, err) - return false - } - _, ok := state.Plugins[agentName] - return ok -} diff --git a/libs/aitools/agents/calling_test.go b/libs/aitools/agents/calling_test.go index 7aaf89e1e4a..3282da48e27 100644 --- a/libs/aitools/agents/calling_test.go +++ b/libs/aitools/agents/calling_test.go @@ -104,7 +104,9 @@ func TestDatabricksTooling(t *testing.T) { name string agent string setup func(t *testing.T, home, cwd string) - want agents.Tooling + // recorded is what pluginRecorded (the CLI install state) reports. + recorded bool + want agents.Tooling }{ { name: "nothing installed", @@ -126,19 +128,16 @@ func TestDatabricksTooling(t *testing.T) { agent: agents.NameClaudeCode, setup: func(t *testing.T, home, cwd string) { writeFile(t, filepath.Join(home, ".claude", "plugins", "installed_plugins.json"), `{"plugins":{}}`) - writeFile(t, filepath.Join(home, ".databricks", "aitools", "skills", ".state.json"), - `{"schema_version":2,"plugins":{"claude-code":{"marketplace":"claude-plugins-official","plugin":"databricks"}}}`) }, - want: agents.ToolingNone, + 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) { - writeFile(t, filepath.Join(home, ".databricks", "aitools", "skills", ".state.json"), - `{"schema_version":2,"plugins":{"claude-code":{"plugin":"databricks"}}}`) - }, - 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", @@ -189,22 +188,18 @@ func TestDatabricksTooling(t *testing.T) { want: agents.ToolingPlugin, }, { - name: "codex plugin recorded in the CLI state", - agent: agents.NameCodex, - setup: func(t *testing.T, home, cwd string) { - writeFile(t, filepath.Join(home, ".databricks", "aitools", "skills", ".state.json"), - `{"schema_version":2,"plugins":{"codex":{"marketplace":"databricks-agent-skills","plugin":"databricks"}}}`) - }, - 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) { - writeFile(t, filepath.Join(home, ".databricks", "aitools", "skills", ".state.json"), - `{"schema_version":2,"plugins":{"copilot":{"plugin":"databricks"}}}`) - }, - 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", @@ -212,8 +207,6 @@ func TestDatabricksTooling(t *testing.T) { 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"}]}}`) - writeFile(t, filepath.Join(home, ".databricks", "aitools", "skills", ".state.json"), - `{"plugins":{"claude-code":{"plugin":"databricks"}}}`) }, want: agents.ToolingNone, }, @@ -295,14 +288,6 @@ func TestDatabricksTooling(t *testing.T) { }, want: agents.ToolingSkills, }, - { - name: "corrupt state reads as not installed", - agent: agents.NameCodex, - setup: func(t *testing.T, home, cwd string) { - writeFile(t, filepath.Join(home, ".databricks", "aitools", "skills", ".state.json"), `{not json`) - }, - want: agents.ToolingNone, - }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { @@ -311,8 +296,7 @@ func TestDatabricksTooling(t *testing.T) { ctx := env.WithUserHomeDir(t.Context(), home) a := agents.ByName(tc.agent) require.NotNil(t, a) - assert.Equal(t, tc.want, a.DatabricksTooling(ctx)) - assert.Equal(t, tc.want != agents.ToolingNone, a.HasDatabricksTooling(ctx)) + assert.Equal(t, tc.want, a.DatabricksTooling(ctx, func() bool { return tc.recorded })) }) } } @@ -337,7 +321,24 @@ func TestDatabricksToolingClaudePluginInSeveralMarketplaces(t *testing.T) { // first databricks entry fails some of these reads. got := map[agents.Tooling]int{} for range 200 { - got[a.DatabricksTooling(ctx)]++ + got[a.DatabricksTooling(ctx, func() bool { return false })]++ } assert.Equal(t, map[agents.Tooling]int{agents.ToolingPlugin: 200}, got) } + +// 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/plugins.go b/libs/aitools/agents/plugins.go index 22fcd2b15d3..e7ac9fa6c89 100644 --- a/libs/aitools/agents/plugins.go +++ b/libs/aitools/agents/plugins.go @@ -30,7 +30,8 @@ type pluginInstall struct { // 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. +// 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": @@ -43,9 +44,10 @@ func (p pluginInstall) appliesTo(cwd string) bool { } } -// isSameOrUnder reports whether dir is root or a directory under it. +// 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, dir = resolvePath(root), resolvePath(dir) + root = resolvePath(root) rel, err := filepath.Rel(root, dir) if err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) { return true @@ -111,10 +113,8 @@ func (a *Agent) DatabricksPluginVersion(ctx context.Context) (string, bool) { // 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 { - if a.pluginInstalls == nil { - return false - } installs, _ := a.pluginInstalls(ctx, a) for _, install := range installs { if install.appliesTo(cwd) { diff --git a/libs/aitools/installer/useragent.go b/libs/aitools/installer/useragent.go index 8cc22cb7253..a4360c125cc 100644 --- a/libs/aitools/installer/useragent.go +++ b/libs/aitools/installer/useragent.go @@ -4,6 +4,7 @@ import ( "context" "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go/useragent" ) @@ -62,7 +63,29 @@ func WithCallingAgentToolingInUserAgent(ctx context.Context) context.Context { if a == nil { return ctx } - return useragent.InContext(ctx, callingAgentToolingUserAgentKey, toolingUserAgentValue(a.DatabricksTooling(ctx))) + tooling := a.DatabricksTooling(ctx, func() bool { return globalPluginRecorded(ctx, a.Name) }) + return useragent.InContext(ctx, callingAgentToolingUserAgentKey, toolingUserAgentValue(tooling)) +} + +// 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 } func toolingUserAgentValue(t agents.Tooling) string { diff --git a/libs/aitools/installer/useragent_test.go b/libs/aitools/installer/useragent_test.go index 0e2dac80783..3e35ec1cee2 100644 --- a/libs/aitools/installer/useragent_test.go +++ b/libs/aitools/installer/useragent_test.go @@ -130,6 +130,7 @@ func TestWithCallingAgentToolingInUserAgent(t *testing.T) { 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 }{ @@ -139,6 +140,8 @@ func TestWithCallingAgentToolingInUserAgent(t *testing.T) { {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"}, } @@ -158,6 +161,12 @@ func TestWithCallingAgentToolingInUserAgent(t *testing.T) { 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)) } From 6825641834f340e4669885a5c60deb366730800f Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Tue, 10 Nov 2026 16:41:00 +0000 Subject: [PATCH 3/6] aitools: map SDK agent products with a two-entry alias map Only copilot-cli and gemini-cli differ from their registry names, so AgentForProvider maps those two and looks every other product up with ByName. New registry agents no longer need a second list. "multiple", "" and products without a registry entry still return nil. Replace the free toolingUserAgentValue function with a documented String method on agents.Tooling. Co-authored-by: Isaac --- libs/aitools/agents/calling.go | 45 ++++++++++++++--------------- libs/aitools/agents/calling_test.go | 1 - libs/aitools/installer/useragent.go | 13 +-------- 3 files changed, 22 insertions(+), 37 deletions(-) diff --git a/libs/aitools/agents/calling.go b/libs/aitools/agents/calling.go index 352860e87cd..4dcf903a863 100644 --- a/libs/aitools/agents/calling.go +++ b/libs/aitools/agents/calling.go @@ -9,38 +9,23 @@ import ( "github.com/databricks/databricks-sdk-go/useragent" ) -// SDK agent products (useragent.AgentProvider values) whose names differ from -// the registry name. +// 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 -const ( - providerCopilot = "copilot-cli" - providerGemini = "gemini-cli" -) - -// 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, +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 { - name, ok := providerToRegistry[provider] - if !ok { - return nil + if name, ok := providerAliases[provider]; ok { + provider = name } - return ByName(name) + return ByName(provider) } // CallingAgent returns the registry agent driving the CLI, or nil when no @@ -59,6 +44,18 @@ const ( 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 diff --git a/libs/aitools/agents/calling_test.go b/libs/aitools/agents/calling_test.go index 3282da48e27..276baabd7a2 100644 --- a/libs/aitools/agents/calling_test.go +++ b/libs/aitools/agents/calling_test.go @@ -20,7 +20,6 @@ func TestAgentForProvider(t *testing.T) { assert.Nil(t, agents.AgentForProvider("windsurf")) assert.Nil(t, agents.AgentForProvider("multiple")) assert.Nil(t, agents.AgentForProvider("")) - assert.Nil(t, agents.AgentForProvider("copilot")) } // Every registry agent must be reachable from some SDK product, or it could diff --git a/libs/aitools/installer/useragent.go b/libs/aitools/installer/useragent.go index a4360c125cc..95a7cab6492 100644 --- a/libs/aitools/installer/useragent.go +++ b/libs/aitools/installer/useragent.go @@ -64,7 +64,7 @@ func WithCallingAgentToolingInUserAgent(ctx context.Context) context.Context { return ctx } tooling := a.DatabricksTooling(ctx, func() bool { return globalPluginRecorded(ctx, a.Name) }) - return useragent.InContext(ctx, callingAgentToolingUserAgentKey, toolingUserAgentValue(tooling)) + return useragent.InContext(ctx, callingAgentToolingUserAgentKey, tooling.String()) } // globalPluginRecorded reports whether the global CLI install state records a @@ -87,14 +87,3 @@ func globalPluginRecorded(ctx context.Context, agentName string) bool { _, ok := state.Plugins[agentName] return ok } - -func toolingUserAgentValue(t agents.Tooling) string { - switch t { - case agents.ToolingPlugin: - return "plugin" - case agents.ToolingSkills: - return "skills" - default: - return "none" - } -} From c28c0836395c1a7c251c53aa59242c7bc48ef82e Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Tue, 10 Nov 2026 16:42:55 +0000 Subject: [PATCH 4/6] aitools: drop agenttest for a testutil.SetAgentEnv helper Replace the agenttest package with testutil.SetAgentEnv(t, kv...), which SetAgent now builds on. It keeps TMPDIR, TEMP and TMP, so tests no longer have to call t.TempDir before clearing the environment. Make TestCallingAgent table-driven through the agents' own env vars (CLAUDECODE, COPILOT_CLI, ...), so the test exercises the SDK's real product names, fails via require.NotNil instead of panicking, and checks that every registry agent is reachable. Replace the 200-read map-order loops with a deterministic check that claudePluginInstalls returns the records of both marketplace entries. Delete the Linux bind-mount test: it needs unprivileged mount namespaces, which the CI Linux runners do not allow, so it never ran there. The case-folding test still covers the file-identity path on macOS and Windows. Co-authored-by: Isaac --- internal/testutil/agent.go | 31 +++++++- internal/testutil/agent_test.go | 29 +++++++ libs/aitools/agents/agenttest/agenttest.go | 33 -------- libs/aitools/agents/calling_test.go | 89 +++++++++++----------- libs/aitools/agents/plugins_linux_test.go | 57 -------------- libs/aitools/agents/plugins_test.go | 17 ++--- libs/aitools/installer/useragent_test.go | 6 +- 7 files changed, 110 insertions(+), 152 deletions(-) create mode 100644 internal/testutil/agent_test.go delete mode 100644 libs/aitools/agents/agenttest/agenttest.go delete mode 100644 libs/aitools/agents/plugins_linux_test.go 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/agenttest/agenttest.go b/libs/aitools/agents/agenttest/agenttest.go deleted file mode 100644 index e6ca20bfd0d..00000000000 --- a/libs/aitools/agents/agenttest/agenttest.go +++ /dev/null @@ -1,33 +0,0 @@ -// Package agenttest controls which coding agent useragent.AgentProvider() -// reports in tests. -package agenttest - -import ( - "testing" - - "github.com/databricks/cli/internal/testutil" - "github.com/databricks/databricks-sdk-go/useragent" -) - -// SetAgentEnv clears the environment (the agent running the test binary would -// otherwise leak in), then sets the given key/value pairs, and resets the SDK's -// process-wide AgentProvider cache before and after the test. With no -// arguments AgentProvider() reports no agent. Unlike testutil.SetAgent, which -// sets only AGENT, it sets the agents' own env vars (e.g. CLAUDECODE=1), and -// several at once for the "multiple" case. Tests using it must set any other -// env they need (HOME, ...) afterwards and must not run in parallel. A test -// that uses t.TempDir must call it once before SetAgentEnv: the first call -// picks the temp root from TMPDIR / TEMP, which SetAgentEnv clears (on Windows -// the fallback is not writable for a standard user). -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)) - } - testutil.NullEnvironment(t) - for i := 0; i < len(kv); i += 2 { - t.Setenv(kv[i], kv[i+1]) - } - useragent.ClearCache() - t.Cleanup(useragent.ClearCache) -} diff --git a/libs/aitools/agents/calling_test.go b/libs/aitools/agents/calling_test.go index 276baabd7a2..38df26a2501 100644 --- a/libs/aitools/agents/calling_test.go +++ b/libs/aitools/agents/calling_test.go @@ -6,55 +6,57 @@ import ( "path/filepath" "testing" + "github.com/databricks/cli/internal/testutil" "github.com/databricks/cli/libs/aitools/agents" - "github.com/databricks/cli/libs/aitools/agents/agenttest" "github.com/databricks/cli/libs/env" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -func TestAgentForProvider(t *testing.T) { - assert.Equal(t, agents.NameCopilot, agents.AgentForProvider("copilot-cli").Name) - assert.Equal(t, agents.NameGemini, agents.AgentForProvider("gemini-cli").Name) - assert.Equal(t, agents.NameClaudeCode, agents.AgentForProvider("claude-code").Name) - assert.Nil(t, agents.AgentForProvider("windsurf")) - assert.Nil(t, agents.AgentForProvider("multiple")) - assert.Nil(t, agents.AgentForProvider("")) -} +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: "no agent"}, + {name: "several agents", env: []string{"CLAUDECODE", "1", "CURSOR_AGENT", "1"}}, + } -// Every registry agent must be reachable from some SDK product, or it could -// never be the calling agent. -func TestAgentForProviderCoversRegistry(t *testing.T) { - products := []string{"antigravity", "claude-code", "codex", "copilot-cli", "cursor", "gemini-cli", "goose", "kiro", "opencode", "pi"} - reached := map[string]bool{} - for _, p := range products { - if a := agents.AgentForProvider(p); a != nil { - reached[a.Name] = true - } + // 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, reached[a.Name], "registry agent %q has no SDK product mapping", a.Name) + assert.True(t, covered[a.Name], "no case reaches registry agent %q", a.Name) } -} - -func TestCallingAgentFromEnv(t *testing.T) { - agenttest.SetAgentEnv(t, "COPILOT_CLI", "1") - assert.Equal(t, agents.NameCopilot, agents.CallingAgent().Name) -} - -func TestCallingAgentFreeText(t *testing.T) { - agenttest.SetAgentEnv(t, "AGENT", "some-internal-tool") - assert.Nil(t, agents.CallingAgent()) -} - -func TestCallingAgentNone(t *testing.T) { - agenttest.SetAgentEnv(t) - assert.Nil(t, agents.CallingAgent()) -} -func TestCallingAgentMultiple(t *testing.T) { - agenttest.SetAgentEnv(t, "CLAUDECODE", "1", "CURSOR_AGENT", "1") - assert.Nil(t, agents.CallingAgent()) + 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 @@ -315,14 +317,9 @@ func TestDatabricksToolingClaudePluginInSeveralMarketplaces(t *testing.T) { ctx := env.WithUserHomeDir(t.Context(), home) a := agents.ByName(agents.NameClaudeCode) require.NotNil(t, a) - // Go randomizes map iteration (for a two-entry map the second entry comes - // first only about one time in eight), so a reader that keeps only the - // first databricks entry fails some of these reads. - got := map[agents.Tooling]int{} - for range 200 { - got[a.DatabricksTooling(ctx, func() bool { return false })]++ - } - assert.Equal(t, map[agents.Tooling]int{agents.ToolingPlugin: 200}, got) + // 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 diff --git a/libs/aitools/agents/plugins_linux_test.go b/libs/aitools/agents/plugins_linux_test.go deleted file mode 100644 index 1f16c697b71..00000000000 --- a/libs/aitools/agents/plugins_linux_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package agents - -import ( - "os" - "os/exec" - "path/filepath" - "strings" - "syscall" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -const bindMountTestChildEnv = "DATABRICKS_CLI_TEST_BIND_MOUNT_CHILD" - -// A bind mount names the same directory under a second path that no symlink -// resolution can map back, the way a case-insensitive filesystem does on macOS -// and Windows, so it exercises the same identity check on Linux. Mounting needs -// a private mount namespace, so the test re-runs itself under `unshare -rm` -// (an unprivileged user namespace) and skips where those are not allowed. -func TestPluginInstallAppliesToBindMountedProjectPath(t *testing.T) { - if os.Getenv(bindMountTestChildEnv) == "" { - unshare, err := exec.LookPath("unshare") - if err != nil { - t.Skip("unshare not found") - } - if err := exec.Command(unshare, "-rm", "true").Run(); err != nil { - t.Skipf("unprivileged mount namespaces are not available: %v", err) - } - cmd := exec.Command(unshare, "-rm", os.Args[0], "-test.run=^"+t.Name()+"$", "-test.count=1", "-test.v") - cmd.Env = append(os.Environ(), bindMountTestChildEnv+"=1") - out, err := cmd.CombinedOutput() - require.NoError(t, err, "%s", out) - if strings.Contains(string(out), "--- SKIP: "+t.Name()) { - t.Skipf("%s", out) - } - assert.Contains(t, string(out), "--- PASS: "+t.Name()) - return - } - - project, sub := projectFixture(t) - alias := filepath.Join(filepath.Dir(project), "alias") - require.NoError(t, os.Mkdir(alias, 0o755)) - // Some hosts (e.g. AppArmor-restricted user namespaces) allow the - // namespace but deny the mount. - if err := syscall.Mount(project, alias, "", syscall.MS_BIND, ""); err != nil { - t.Skipf("bind mount not permitted: %v", err) - } - t.Cleanup(func() { assert.NoError(t, syscall.Unmount(alias, 0)) }) - - for _, cwd := range []string{project, sub} { - assert.True(t, pluginInstall{Scope: "project", ProjectPath: alias}.appliesTo(cwd), cwd) - } - // The mount does not make an unrelated directory count. - assert.False(t, pluginInstall{Scope: "project", ProjectPath: alias}.appliesTo(filepath.Dir(project))) -} diff --git a/libs/aitools/agents/plugins_test.go b/libs/aitools/agents/plugins_test.go index c970beb1f3d..5adb3633508 100644 --- a/libs/aitools/agents/plugins_test.go +++ b/libs/aitools/agents/plugins_test.go @@ -62,16 +62,13 @@ 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"}]}}`) - // Go randomizes map iteration (for a two-entry map the second entry comes - // first only about one time in eight), so a reader that keeps only the - // first databricks entry fails some of these reads. - got := map[string]int{} - for range 200 { - version, ok := agentWithConfigDir(configDir).DatabricksPluginVersion(t.Context()) - require.True(t, ok) - got[version]++ - } - assert.Equal(t, map[string]int{"0.3.0": 200}, got) + 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 diff --git a/libs/aitools/installer/useragent_test.go b/libs/aitools/installer/useragent_test.go index 3e35ec1cee2..916d3209f0a 100644 --- a/libs/aitools/installer/useragent_test.go +++ b/libs/aitools/installer/useragent_test.go @@ -5,7 +5,7 @@ import ( "path/filepath" "testing" - "github.com/databricks/cli/libs/aitools/agents/agenttest" + "github.com/databricks/cli/internal/testutil" "github.com/databricks/databricks-sdk-go/useragent" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -147,11 +147,9 @@ func TestWithCallingAgentToolingInUserAgent(t *testing.T) { } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - // The first t.TempDir picks the temp root, so create both before - // SetAgentEnv clears TMPDIR / TEMP. + testutil.SetAgentEnv(t, tc.agentEnv...) tmp := t.TempDir() cwd := t.TempDir() - agenttest.SetAgentEnv(t, tc.agentEnv...) t.Setenv("HOME", tmp) t.Setenv("USERPROFILE", tmp) // A .git in cwd keeps the project lookup out of the host's directories. From b0cc7305d8cfce1badc9495bb630b95f6bfd04b8 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Tue, 10 Nov 2026 16:44:29 +0000 Subject: [PATCH 5/6] aitools: link the Claude Code docs the tooling check relies on Link the Claude Code skills doc for the project skills lookup that stops at the repository root (the worktree root in a linked worktree), and the plugin docs for the installed_plugins.json scope and version fields. The projectPath field is not in Claude Code's public docs; say so instead of claiming it is required. Cross-reference the install hint's claudeToolingInstalled and DatabricksTooling, so the different definitions read as deliberate. Co-authored-by: Isaac --- libs/aitools/agents/calling.go | 6 +++++- libs/aitools/agents/hint.go | 3 +++ libs/aitools/agents/plugins.go | 10 +++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/libs/aitools/agents/calling.go b/libs/aitools/agents/calling.go index 4dcf903a863..dbaa4cf1f87 100644 --- a/libs/aitools/agents/calling.go +++ b/libs/aitools/agents/calling.go @@ -62,6 +62,8 @@ func (t Tooling) String() string { // record. For Codex and Copilot, pluginRecorded (the CLI install state) is the // only plugin source, so it includes plugins later removed with the agent's own // CLI. 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 { // Getwd returns "" on error, which skips the project lookups below. cwd, err := os.Getwd() @@ -95,7 +97,9 @@ func (a *Agent) DatabricksTooling(ctx context.Context, pluginRecorded func() boo // 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. Outside a repository only cwd is returned. cwd must be +// 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 { 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 e7ac9fa6c89..794d5c7f4d2 100644 --- a/libs/aitools/agents/plugins.go +++ b/libs/aitools/agents/plugins.go @@ -16,14 +16,18 @@ 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. +// 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 the record applies to. Claude Code requires it - // for the project and local scopes. + // 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"` } From 9af9b82a60f06e647c92f3c0a8dc99de56aa316c Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Tue, 10 Nov 2026 16:49:15 +0000 Subject: [PATCH 6/6] aitools: cover the free-text alias and every manifest record in tests Add a TestCallingAgent case for AGENT=copilot, which now maps to the registry agent through ByName, and a deterministic test that databricksPluginActiveIn checks every manifest record, not only the first. Stop relying on os.Getwd returning "" on error, say that Codex and Copilot miss plugins installed with their own CLI, and align a test comment with the projectPath doc. Co-authored-by: Isaac --- libs/aitools/agents/calling.go | 12 ++++++------ libs/aitools/agents/calling_test.go | 3 ++- libs/aitools/agents/plugins_test.go | 12 ++++++++++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/libs/aitools/agents/calling.go b/libs/aitools/agents/calling.go index dbaa4cf1f87..6e91d7fc9f1 100644 --- a/libs/aitools/agents/calling.go +++ b/libs/aitools/agents/calling.go @@ -60,15 +60,15 @@ func (t Tooling) String() string { // 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 includes plugins later removed with the agent's own -// CLI. The shared skills store is not consulted: it cannot attribute skills. +// 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 { - // Getwd returns "" on error, which skips the project lookups below. - cwd, err := os.Getwd() - if err == nil { - cwd = resolvePath(cwd) + // 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: diff --git a/libs/aitools/agents/calling_test.go b/libs/aitools/agents/calling_test.go index 38df26a2501..9a4b01f815c 100644 --- a/libs/aitools/agents/calling_test.go +++ b/libs/aitools/agents/calling_test.go @@ -31,6 +31,7 @@ func TestCallingAgent(t *testing.T) { {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"}}, } @@ -85,7 +86,7 @@ func writeFile(t *testing.T, path, body string) { } // writeClaudeRecord writes a Claude manifest with one databricks plugin record -// in the given scope and project path (the field Claude Code requires for the +// 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"} diff --git a/libs/aitools/agents/plugins_test.go b/libs/aitools/agents/plugins_test.go index 5adb3633508..893449ccfba 100644 --- a/libs/aitools/agents/plugins_test.go +++ b/libs/aitools/agents/plugins_test.go @@ -82,6 +82,18 @@ func projectFixture(t *testing.T) (project, sub string) { 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")