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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
31 changes: 29 additions & 2 deletions internal/testutil/agent.go
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)
}
29 changes: 29 additions & 0 deletions internal/testutil/agent_test.go
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())
}
11 changes: 6 additions & 5 deletions libs/aitools/agents/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -179,7 +180,7 @@ var Registry = []*Agent{
ProjectConfigDir: ".claude",
Binary: "claude",
Plugin: claudePlugin(),
pluginVersion: claudePluginVersion,
pluginInstalls: claudePluginInstalls,
},
{
Name: NameCursor,
Expand Down
120 changes: 120 additions & 0 deletions libs/aitools/agents/calling.go
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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[nice to have] Stopping at the nearest .git undercounts 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/skills is already the global dir.

Copy link
Copy Markdown
Contributor Author

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/skills in 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).

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