diff --git a/.nextchanges/cli/aitools-hint-exempt-commands.md b/.nextchanges/cli/aitools-hint-exempt-commands.md new file mode 100644 index 00000000000..6af94675aef --- /dev/null +++ b/.nextchanges/cli/aitools-hint-exempt-commands.md @@ -0,0 +1 @@ +* Stop printing the Databricks AI tooling install hint on `auth`, `configure`, `current-user me`, `version`, `help`, and shell-completion commands. ([#7452](https://github.com/databricks/cli/pull/7452)) diff --git a/acceptance/cmd/aitools/claude-config-dir/output.txt b/acceptance/cmd/aitools/claude-config-dir/output.txt index 508651018c5..8196a824be5 100644 --- a/acceptance/cmd/aitools/claude-config-dir/output.txt +++ b/acceptance/cmd/aitools/claude-config-dir/output.txt @@ -1,21 +1,34 @@ === Plugin installed only in the custom config directory ->>> CLAUDE_CONFIG_DIR=custom [CLI] version -Databricks CLI v[CLI_VERSION] +>>> CLAUDE_CONFIG_DIR=custom [CLI] clusters list +ID Name State +4321 some-test-cluster +9876 some-other-cluster === Custom directory takes precedence over the default installation ->>> CLAUDE_CONFIG_DIR=missing [CLI] version +>>> CLAUDE_CONFIG_DIR=missing [CLI] clusters list Databricks skills are not installed. To work with Databricks reliably, first run: databricks aitools install -Databricks CLI v[CLI_VERSION] +ID Name State +4321 some-test-cluster +9876 some-other-cluster + +=== Exempt commands never hint, even without the tooling + +>>> CLAUDE_CONFIG_DIR=missing [CLI] current-user me +"[USERNAME]" === Empty override uses the default installation ->>> CLAUDE_CONFIG_DIR= [CLI] version -Databricks CLI v[CLI_VERSION] +>>> CLAUDE_CONFIG_DIR= [CLI] clusters list +ID Name State +4321 some-test-cluster +9876 some-other-cluster === Unset override uses the default installation ->>> [CLI] version -Databricks CLI v[CLI_VERSION] +>>> [CLI] clusters list +ID Name State +4321 some-test-cluster +9876 some-other-cluster diff --git a/acceptance/cmd/aitools/claude-config-dir/script b/acceptance/cmd/aitools/claude-config-dir/script index 26f51369813..9dc9898f533 100644 --- a/acceptance/cmd/aitools/claude-config-dir/script +++ b/acceptance/cmd/aitools/claude-config-dir/script @@ -1,18 +1,22 @@ export CLAUDECODE=1 unset CLAUDE_CODE_SESSION_ID -# aitools commands skip the install hint; version exercises it without auth or network. +# Hint-exempt commands (aitools, auth, one-shot lookups such as version and current-user me, +# completion) skip the install hint; clusters list exercises it against the fake server. sethome ./empty-home title "Plugin installed only in the custom config directory" -trace CLAUDE_CONFIG_DIR=custom $CLI version +trace CLAUDE_CONFIG_DIR=custom $CLI clusters list sethome ./installed-home title "Custom directory takes precedence over the default installation" -trace CLAUDE_CONFIG_DIR=missing $CLI version +trace CLAUDE_CONFIG_DIR=missing $CLI clusters list + +title "Exempt commands never hint, even without the tooling" +trace CLAUDE_CONFIG_DIR=missing $CLI current-user me | jq .userName title "Empty override uses the default installation" -trace CLAUDE_CONFIG_DIR= $CLI version +trace CLAUDE_CONFIG_DIR= $CLI clusters list title "Unset override uses the default installation" unset CLAUDE_CONFIG_DIR -trace $CLI version +trace $CLI clusters list diff --git a/libs/aitools/agents/hint.go b/libs/aitools/agents/hint.go index 5cd331130f8..b2137b7a092 100644 --- a/libs/aitools/agents/hint.go +++ b/libs/aitools/agents/hint.go @@ -3,6 +3,7 @@ package agents import ( "context" "path/filepath" + "strings" "time" "github.com/databricks/cli/libs/cache" @@ -25,11 +26,6 @@ const ( // https://github.com/databricks/databricks-sdk-go/blob/main/useragent/agent.go claudeAgentProvider = "claude-code" - // aiToolsCommandName is the top-level command whose subtree is never hinted: - // telling the agent to run `databricks aitools install` while it is already - // running an aitools command makes no sense. - aiToolsCommandName = "aitools" - // claudeSessionEnv is Claude Code's per-session identifier. It keys the // per-session throttle (at most one hint per hintSessionTTL per session); // when unset (older Claude) the hint degrades to per-command. @@ -47,15 +43,15 @@ const hintSessionTTL = time.Hour // MaybeHint prints a one-line recommendation to install the Databricks AI // tooling when Claude Code drives the CLI without it installed. It is a no-op -// for human callers, other agents, aitools commands themselves, when there is -// no output stream, once the tooling is present, and when the session was -// hinted within the last hintSessionTTL. Best-effort: it writes only to stderr -// and never fails the command. +// for human callers, other agents, hint-exempt commands (isHintExemptCommand), +// when there is no output stream, once the tooling is present, and when the +// session was hinted within the last hintSessionTTL. Best-effort: it writes +// only to stderr and never fails the command. func MaybeHint(ctx context.Context, cmd *cobra.Command) { if useragent.AgentProvider() != claudeAgentProvider { return } - if isAIToolsCommand(cmd) { + if isHintExemptCommand(cmd) { return } if !cmdio.HasIO(ctx) { @@ -75,11 +71,33 @@ func MaybeHint(ctx context.Context, cmd *cobra.Command) { recordSessionHint(ctx) } -// isAIToolsCommand reports whether cmd is the aitools command or one of its -// subcommands, walking the parent chain (mirrors versioncheck.isExemptCommand). -func isAIToolsCommand(cmd *cobra.Command) bool { - for c := cmd; c != nil; c = c.Parent() { - if c.Name() == aiToolsCommandName { +// hintExemptCommands are the commands, with their subcommands, on which no +// tooling hint prints. Paths are relative to the root command. The aitools +// trees are exempt because telling the agent to run aitools while it already +// runs aitools makes no sense. The rest are plumbing or one-shot lookups (auth +// checks, "who am I", version, help, shell completion): the agent has no task to apply +// the skills to. Evaluations showed that conversion on these commands is low, +// so a hint on them wastes the prompt. +var hintExemptCommands = []string{ + "aitools", + "experimental aitools", + "auth", + "configure", + "current-user me", + "version", + "completion", + "help", + cobra.ShellCompRequestCmd, +} + +// isHintExemptCommand reports whether cmd is one of hintExemptCommands or a +// subcommand of one. +func isHintExemptCommand(cmd *cobra.Command) bool { + path := cmd.CommandPath() + root := cmd.Root().CommandPath() + for _, exempt := range hintExemptCommands { + full := root + " " + exempt + if path == full || strings.HasPrefix(path, full+" ") { return true } } diff --git a/libs/aitools/agents/hint_test.go b/libs/aitools/agents/hint_test.go index b2a16ffb8e8..e57c9a08769 100644 --- a/libs/aitools/agents/hint_test.go +++ b/libs/aitools/agents/hint_test.go @@ -6,6 +6,7 @@ import ( "log/slog" "os" "path/filepath" + "strings" "testing" "github.com/databricks/cli/libs/cmdio" @@ -60,7 +61,7 @@ func claudeCtx(t *testing.T, home string) (context.Context, *bytes.Buffer) { return ctx, stderr } -func regularCmd() *cobra.Command { return &cobra.Command{Use: "current-user"} } +func regularCmd() *cobra.Command { return commandAt("clusters list") } func writePluginManifest(t *testing.T, home, body string) { dir := filepath.Join(home, ".claude", "plugins") @@ -192,14 +193,49 @@ func TestMaybeHint_AIToolsCommandSilent(t *testing.T) { setClaudeAgent(t) ctx, stderr := claudeCtx(t, t.TempDir()) - parent := &cobra.Command{Use: aiToolsCommandName} - child := &cobra.Command{Use: "install"} - parent.AddCommand(child) - - MaybeHint(ctx, child) + MaybeHint(ctx, commandAt("aitools install")) assert.Empty(t, stderr.String()) } +// commandAt builds a command tree under a "databricks" root and returns the +// command at the space-separated path, e.g. "current-user me". +func commandAt(path string) *cobra.Command { + cmd := &cobra.Command{Use: "databricks"} + for name := range strings.FieldsSeq(path) { + child := &cobra.Command{Use: name} + cmd.AddCommand(child) + cmd = child + } + return cmd +} + +func TestIsHintExemptCommand(t *testing.T) { + tests := []struct { + path string + want bool + }{ + {"aitools install", true}, + {"experimental aitools skills install", true}, + {"auth login", true}, + {"auth token", true}, + {"configure", true}, + {"current-user me", true}, + {"completion bash", true}, + {"help", true}, + {cobra.ShellCompRequestCmd, true}, + {"version", true}, + {"authz", false}, + {"current-user", false}, + {"clusters list", false}, + {"jobs run-now", false}, + } + for _, tc := range tests { + t.Run(tc.path, func(t *testing.T) { + assert.Equal(t, tc.want, isHintExemptCommand(commandAt(tc.path))) + }) + } +} + func TestMaybeHint_NoIONoPanic(t *testing.T) { setClaudeAgent(t) // Bare context without cmdio installed: cobra resolves --help and bare