Repository navigation
aitools: report the calling agent's own Databricks tooling in the User-Agent #7442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fd8c2c5
aitools: report the calling agent's own Databricks tooling in the Use…
anton-107 9c0e30d
aitools: read the calling agent's CLI plugin record through installer
anton-107 6825641
aitools: map SDK agent products with a two-entry alias map
anton-107 c28c083
aitools: drop agenttest for a testutil.SetAgentEnv helper
anton-107 b0cc730
aitools: link the Claude Code docs the tooling check relies on
anton-107 9af9b82
aitools: cover the free-text alias and every manifest record in tests
anton-107 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nice to have] Stopping at the nearest
.gitundercounts in two cases: a Claude Code project that is not a git repo (only cwd is checked, so<project>/.claude/skills/databricks*is missed when the CLI runs from a subdirectory), and a nested repo or submodule inside the agent's project. The doc says this matches Claude Code's own lookup; a source link would settle that (the comments rule asks for one when depending on an external tool's behaviour). If Claude Code walks further, walking to the filesystem root is bounded and has no false positives since~/.claude/skillsis already the global dir.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keeping the stop at the repository root, now with the source link (b0cc730): https://code.claude.com/docs/en/skills says Claude Code "loads project skills from
.claude/skills/in the directory where you start it and in every parent directory up to the repository root", and in a linked worktree "only up to the worktree root". Walking to/would count skills the agent does not load, e.g. a.claude/skillsin a parent of the repo. For a nested repo or submodule, the walk stops at the inner repo, which matches the doc's "repository root" as far as it says. The doc does not describe non-git directories, so the check stays conservative there (cwd only).