From 24b61a7f95167720f30ccd4653b77bdce60bae00 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:40:12 +0000 Subject: [PATCH 1/3] aitools: don't hint an install that auto-install is about to make ## Changes When a coding agent runs the CLI, the CLI can print the "Databricks skills are not installed" hint before the command. On the same command, the auto-install that runs afterwards can install those skills for that agent. The agent then sees two lines that disagree: one tells it to run `databricks aitools install`, and the next says the tools were just installed. With this change, the hint is skipped when the auto-install after the command is due to give that agent its first tools. ```diff MaybeHint(ctx, cmd) if hint.installed(ctx) return + if autoinstall.InstallsFor(cmd, hint.agentName) + return // no event, no throttle marker claim throttle, print hint, log hint event ``` - `libs/aitools/autoinstall/hook.go`: new hook `InstallsFor(cmd, agentName)`, next to `Run`. `main.go` sets it to `aitools.AutoInstallsFor`, the same way `Run` is set, so `libs/aitools/agents` does not import `cmd/aitools`. - `cmd/aitools/autoinstall_hint.go`: `AutoInstallsFor` makes the same decision as `autoInstall`. It runs the state-free skips, loads the state and checks that `Decide` reaches the gate lookup. Then it plans the install and reports whether this agent is one of the plan's new agents. It reads the state but never writes it, and it does not fetch the compatibility manifest. - `cmd/aitools/autoinstall_run.go`: moves the building of `autoinstall.Inputs` into `autoInstallInputs` so both callers use it. Behavior is unchanged. - `libs/aitools/agents`: each `agentHint` now carries the agent's registry name so the hint can ask about it. This covers Claude Code and the registry agents. ## Why The hint runs before the command, but the gate is only fetched afterwards, so the check stops at the point where `autoInstall` would fetch it. A run that gets that far counts as installing, and its hint is withheld. If that run then installs nothing (for example, because the gate is off), the hint was not printed and no throttle marker was written. The next command prints the hint as usual. This costs one command of delay and avoids a network call before every command. An agent that the install skips, such as Claude Code with no config dir, still gets the hint on the same command. ## Tests - `TestAutoInstallsFor` (`cmd/aitools`) covers: - first run, retry and fresh state, which install; - an agent without a config dir; - a settled version, exhausted retries, opt-out in state and opt-out in env; - CI, a development build and a command that skips auto-install; - a corrupt install state, where planning fails. Each case also checks that the gate is never fetched, the state file is unchanged and nothing is installed. - `libs/aitools/agents`: - `TestMaybeHint_WithheldWhileAutoInstallInstallsForTheAgent`: both hint lines are withheld and no event is logged. When the next command does not install, it prints the hint. - `TestMaybeHint_RegistryAgentAskedByItsName`: a registry agent is asked about by its own name. - `TestMaybeHint_InstalledToolingSkipsTheAutoInstallCheck`: when the tooling is already installed, the hook is not called. - New acceptance test `acceptance/cmd/aitools/auto-install/agent-hint` covers four cases: - the first run prints only the install notice; - the second run prints nothing; - with the gate off, the first run prints nothing and the next prints the hint; - an agent the install skips still gets the hint next to the install notice. Changelog fragment: `.nextchanges/cli/aitools-auto-install-hint-contradiction.md`. Co-authored-by: Isaac --- ...aitools-auto-install-hint-contradiction.md | 1 + .../auto-install/agent-hint/out.test.toml | 1 + .../auto-install/agent-hint/output.txt | 38 ++++++++ .../aitools/auto-install/agent-hint/script | 30 +++++++ cmd/aitools/autoinstall_hint.go | 36 ++++++++ cmd/aitools/autoinstall_hint_test.go | 88 +++++++++++++++++++ cmd/aitools/autoinstall_run.go | 14 ++- libs/aitools/agents/hint.go | 11 +++ libs/aitools/agents/hint_agents.go | 1 + libs/aitools/agents/hint_test.go | 59 +++++++++++++ libs/aitools/autoinstall/hook.go | 9 ++ main.go | 1 + 12 files changed, 285 insertions(+), 4 deletions(-) create mode 100644 .nextchanges/cli/aitools-auto-install-hint-contradiction.md create mode 100644 acceptance/cmd/aitools/auto-install/agent-hint/out.test.toml create mode 100644 acceptance/cmd/aitools/auto-install/agent-hint/output.txt create mode 100644 acceptance/cmd/aitools/auto-install/agent-hint/script create mode 100644 cmd/aitools/autoinstall_hint.go create mode 100644 cmd/aitools/autoinstall_hint_test.go diff --git a/.nextchanges/cli/aitools-auto-install-hint-contradiction.md b/.nextchanges/cli/aitools-auto-install-hint-contradiction.md new file mode 100644 index 00000000000..fb32654c731 --- /dev/null +++ b/.nextchanges/cli/aitools-auto-install-hint-contradiction.md @@ -0,0 +1 @@ +* A coding agent no longer gets the "Databricks skills are not installed" hint on a command after which the automatic installation of Databricks AI tools installs them for that agent, so its output no longer tells it to install what the same command installs. When that installation does not happen, the hint appears on the next command. diff --git a/acceptance/cmd/aitools/auto-install/agent-hint/out.test.toml b/acceptance/cmd/aitools/auto-install/agent-hint/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/auto-install/agent-hint/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/auto-install/agent-hint/output.txt b/acceptance/cmd/aitools/auto-install/agent-hint/output.txt new file mode 100644 index 00000000000..6d60407db1b --- /dev/null +++ b/acceptance/cmd/aitools/auto-install/agent-hint/output.txt @@ -0,0 +1,38 @@ + +=== first run of a new version: the install notice, no install hint + +>>> CLAUDECODE=1 [CLI] jobs list +Installed Databricks AI tools for Claude Code, Cursor. Disable: databricks aitools auto-install disable + +>>> print_telemetry +{"command":"jobs_list","exit_code":0,"aitools_install_event":{"agents":["CLAUDE_CODE","CURSOR"],"scope":"GLOBAL","error_category":"TYPE_UNSPECIFIED","agent_results":[{"agent":"CLAUDE_CODE","error_category":"CLI_NOT_ON_PATH"}],"trigger":"AUTO_FIRST_RUN"}} +{"command":"jobs_list","exit_code":0,"aitools_auto_install_event":{"outcome":"INSTALLED","holdout_arm":"TREATMENT","interactive":false}} + +=== second run: the tools are installed, nothing + +>>> CLAUDECODE=1 [CLI] jobs list + +>>> print_telemetry + +=== gate off: nothing on the first run, the hint on the next + +>>> CLAUDECODE=1 [CLI] jobs list + +>>> print_telemetry + +>>> CLAUDECODE=1 [CLI] jobs list +Databricks skills are not installed. To work with Databricks reliably, first run: databricks aitools install + +>>> print_telemetry +{"command":"jobs_list","exit_code":0,"aitools_hint_event":{"hint_kind":"INSTALL_TEXT"}} + +=== an agent the install skips is still hinted: Claude Code has no config dir + +>>> CLAUDECODE=1 [CLI] jobs list +Databricks skills are not installed. To work with Databricks reliably, first run: databricks aitools install +Installed Databricks AI tools for Cursor. Disable: databricks aitools auto-install disable + +>>> print_telemetry +{"command":"jobs_list","exit_code":0,"aitools_hint_event":{"hint_kind":"INSTALL_TEXT"}} +{"command":"jobs_list","exit_code":0,"aitools_install_event":{"agents":["CURSOR"],"scope":"GLOBAL","error_category":"TYPE_UNSPECIFIED","trigger":"AUTO_FIRST_RUN"}} +{"command":"jobs_list","exit_code":0,"aitools_auto_install_event":{"outcome":"INSTALLED","holdout_arm":"TREATMENT","interactive":false}} diff --git a/acceptance/cmd/aitools/auto-install/agent-hint/script b/acceptance/cmd/aitools/auto-install/agent-hint/script new file mode 100644 index 00000000000..2bf9e7494fc --- /dev/null +++ b/acceptance/cmd/aitools/auto-install/agent-hint/script @@ -0,0 +1,30 @@ +# Claude Code calls the CLI: its config dir exists and its CLI is not on PATH, +# so auto-install gives it skill files. The install hint must not tell it to +# install what the same run installs. +unset AGENT CLAUDE_CODE_SESSION_ID CLAUDE_CODE_CHILD_SESSION +use_home home +"$mkdir_bin" -p "${USERPROFILE:-$HOME}/.claude" +gate true + +title "first run of a new version: the install notice, no install hint" +trace CLAUDECODE=1 $CLI jobs list +trace print_telemetry + +title "second run: the tools are installed, nothing" +trace CLAUDECODE=1 $CLI jobs list +trace print_telemetry + +title "gate off: nothing on the first run, the hint on the next" +use_home no-agents-home +"$mkdir_bin" -p "${USERPROFILE:-$HOME}/.claude" +gate false +trace CLAUDECODE=1 $CLI jobs list +trace print_telemetry +trace CLAUDECODE=1 $CLI jobs list +trace print_telemetry + +title "an agent the install skips is still hinted: Claude Code has no config dir" +use_home holdout-home +gate true +trace CLAUDECODE=1 $CLI jobs list +trace print_telemetry diff --git a/cmd/aitools/autoinstall_hint.go b/cmd/aitools/autoinstall_hint.go new file mode 100644 index 00000000000..46f05721c86 --- /dev/null +++ b/cmd/aitools/autoinstall_hint.go @@ -0,0 +1,36 @@ +package aitools + +import ( + "slices" + + "github.com/databricks/cli/libs/aitools/autoinstall" + "github.com/spf13/cobra" +) + +// AutoInstallsFor reports whether the auto-install after cmd is due to give +// the agent agentName its first tools (see autoinstall.InstallsFor). It decides +// as autoInstall does up to the gate lookup, which it skips: a run that would +// look the gate up counts as installing, so on a version whose gate is off the +// install hint waits one command. +func AutoInstallsFor(cmd *cobra.Command, agentName string) bool { + ctx := cmd.Context() + in := autoInstallInputs(ctx, skipsAutoInstall(cmd)) + if slices.Contains(stateFreeSkips, autoinstall.Decide(in)) { + return false + } + var err error + in.State, err = autoinstall.Load(ctx) + if err != nil || autoinstall.Decide(in) != autoinstall.ReasonGateOff { + return false + } + tools, err := planTools(quietContext(ctx), in.State) + if err != nil { + return false + } + for _, it := range tools.plan { + if it.agent.Name == agentName { + return slices.Contains(tools.newAgents(), it.agent.DisplayName) + } + } + return false +} diff --git a/cmd/aitools/autoinstall_hint_test.go b/cmd/aitools/autoinstall_hint_test.go new file mode 100644 index 00000000000..cba068ba9e0 --- /dev/null +++ b/cmd/aitools/autoinstall_hint_test.go @@ -0,0 +1,88 @@ +package aitools + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/autoinstall" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/dbr" + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestAutoInstallsFor(t *testing.T) { + tests := []struct { + name string + state string + // env is set on top of setupAutoInstallHome's environment. + env map[string]string + // installState replaces the global install state file. + installState string + agent string + command string + want bool + }{ + {name: "first run of a version installs for a detected agent", want: true}, + {name: "a retry installs too", state: `{"id": "treatment-machine", "last_version": "1.30.0", "attempts": 2}`, want: true}, + {name: "a fresh machine with no state installs", state: "{}", want: true}, + {name: "an agent without a config dir is not installed for", agent: agents.NameClaudeCode}, + {name: "a settled version installs nothing", state: `{"id": "treatment-machine", "last_version": "1.30.0"}`}, + {name: "a version whose retries ran out installs nothing", state: `{"id": "treatment-machine", "last_version": "1.30.0", "attempts": 10}`}, + {name: "an opt-out installs nothing", state: `{"id": "treatment-machine", "opted_out": true}`}, + {name: "the env opt-out installs nothing", env: map[string]string{"DATABRICKS_AITOOLS_AUTO_INSTALL": "false"}}, + {name: "CI installs nothing", env: map[string]string{"CI": "true"}}, + {name: "a development build installs nothing", env: map[string]string{"DATABRICKS_AITOOLS_AUTO_INSTALL_CLI_VERSION": ""}}, + {name: "a command that skips auto-install installs nothing", command: "aitools list"}, + {name: "a failed plan installs nothing", installState: "not json"}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + home := setupAutoInstallHome(t, tc.state, "CLAUDECODE", "1") + for k, v := range tc.env { + t.Setenv(k, v) + } + if tc.installState != "" { + dir := filepath.Join(home, ".databricks", "aitools", "skills") + require.NoError(t, os.MkdirAll(dir, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(dir, ".state.json"), []byte(tc.installState), 0o600)) + } + t.Cleanup(func() { gateOn = autoinstall.GateOn }) + gateOn = func(context.Context, string) (bool, error) { + t.Error("the gate was looked up") + return true, nil + } + agent, command := tc.agent, tc.command + if agent == "" { + agent = agents.NameCursor + } + if command == "" { + command = "jobs list" + } + ctx := dbr.MockRuntime(cmdio.MockDiscard(t.Context()), dbr.Environment{}) + stateBefore, _ := os.ReadFile(filepath.Join(home, ".databricks", "aitools", "auto-install.json")) + assert.Equal(t, tc.want, AutoInstallsFor(hintTestCommand(ctx, command), agent)) + stateAfter, _ := os.ReadFile(filepath.Join(home, ".databricks", "aitools", "auto-install.json")) + assert.Equal(t, string(stateBefore), string(stateAfter), "the state is only read") + assert.NoDirExists(t, filepath.Join(home, ".cursor", "skills")) + }) + } +} + +// hintTestCommand returns the cobra command at path (two words) under a +// databricks root, with ctx set. +func hintTestCommand(ctx context.Context, path string) *cobra.Command { + group, name, _ := strings.Cut(path, " ") + root := &cobra.Command{Use: "databricks"} + parent := &cobra.Command{Use: group} + cmd := &cobra.Command{Use: name} + root.AddCommand(parent) + parent.AddCommand(cmd) + cmd.SetContext(ctx) + return cmd +} diff --git a/cmd/aitools/autoinstall_run.go b/cmd/aitools/autoinstall_run.go index 8138881157e..27e37d22168 100644 --- a/cmd/aitools/autoinstall_run.go +++ b/cmd/aitools/autoinstall_run.go @@ -135,18 +135,24 @@ func skipsAutoInstall(cmd *cobra.Command) bool { return false } -func autoInstall(ctx context.Context, skippedCommand bool) os.Signal { - version := autoinstall.Version(ctx) - in := autoinstall.Inputs{ +// autoInstallInputs returns the decision inputs that need neither the state +// nor the gate. +func autoInstallInputs(ctx context.Context, skippedCommand bool) autoinstall.Inputs { + return autoinstall.Inputs{ SkippedCommand: skippedCommand, OnRuntime: dbr.RunsOnRuntime(ctx), CacheDisabled: autoinstall.CacheDisabled(ctx), - Version: version, + Version: autoinstall.Version(ctx), RootInUserHome: rootInUserHome(ctx), CI: agents.InCI(ctx), // A "no" earlier in this process counts even if storing it failed. EnvOptOut: autoinstall.EnvOptedOut(ctx) || autoinstall.Declined(), } +} + +func autoInstall(ctx context.Context, skippedCommand bool) os.Signal { + in := autoInstallInputs(ctx, skippedCommand) + version := in.Version // What never auto-installs skips before the state is read, and without a // log line: these are most runs. Root in a user's home is rare, and a user // who always runs the CLI under sudo needs a way to see why nothing diff --git a/libs/aitools/agents/hint.go b/libs/aitools/agents/hint.go index f3c49839721..dd6dbbcc60b 100644 --- a/libs/aitools/agents/hint.go +++ b/libs/aitools/agents/hint.go @@ -5,6 +5,7 @@ import ( "strings" "time" + "github.com/databricks/cli/libs/aitools/autoinstall" "github.com/databricks/cli/libs/cache" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/env" @@ -109,6 +110,9 @@ var ( // agentHint describes how one calling agent is nudged to install the // Databricks AI tooling. type agentHint struct { + // agentName is the registry name of the hinted agent. + agentName string + // installed reports whether the agent already has the tooling; when it // does, nothing is printed. installed func(ctx context.Context) bool @@ -159,6 +163,7 @@ func hintForAgent(provider string, pluginRecorded PluginRecordedFunc) *agentHint switch provider { case claudeAgentProvider: return &agentHint{ + agentName: NameClaudeCode, installed: claudeToolingInstalled, text: hintText, textKind: protos.AitoolsHintKindInstallText, @@ -238,6 +243,12 @@ func MaybeHint(ctx context.Context, cmd *cobra.Command, pluginRecorded PluginRec if hint.installed(ctx) { return } + // The auto-install after this command prints its own notice; neither line + // may tell the agent to install what it installs. No marker is written, so + // a run that ends up not installing leaves the hint to the next command. + if autoinstall.InstallsFor != nil && autoinstall.InstallsFor(cmd, hint.agentName) { + return + } // Only agents without a native line have a durable throttle, so a failed // claim leaves nothing to print. if textDue && !hint.claim(ctx, key) { diff --git a/libs/aitools/agents/hint_agents.go b/libs/aitools/agents/hint_agents.go index 68a3935b8af..9552ea62af0 100644 --- a/libs/aitools/agents/hint_agents.go +++ b/libs/aitools/agents/hint_agents.go @@ -35,6 +35,7 @@ type PluginRecordedFunc func(ctx context.Context, agentName string) bool // hinted; the throttle bounds that to once per session or per TTL. func agentInstallHint(a *Agent, pluginRecorded PluginRecordedFunc) *agentHint { return &agentHint{ + agentName: a.Name, installed: func(ctx context.Context) bool { recorded := func() bool { return pluginRecorded(ctx, a.Name) } return a.DatabricksTooling(ctx, recorded) != ToolingNone diff --git a/libs/aitools/agents/hint_test.go b/libs/aitools/agents/hint_test.go index c3f092e1eae..85be1a7e746 100644 --- a/libs/aitools/agents/hint_test.go +++ b/libs/aitools/agents/hint_test.go @@ -13,6 +13,7 @@ import ( "time" "github.com/databricks/cli/internal/testutil" + "github.com/databricks/cli/libs/aitools/autoinstall" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/flags" @@ -806,3 +807,61 @@ func TestMaybeHint_ProjectSkillsInstalledSilent(t *testing.T) { }) } } + +// setInstallsFor replaces autoinstall.InstallsFor for the test and returns the +// agent names it was asked about. +func setInstallsFor(t *testing.T, installs bool) *[]string { + var asked []string + prev := autoinstall.InstallsFor + autoinstall.InstallsFor = func(_ *cobra.Command, agentName string) bool { + asked = append(asked, agentName) + return installs + } + t.Cleanup(func() { autoinstall.InstallsFor = prev }) + return &asked +} + +func TestMaybeHint_WithheldWhileAutoInstallInstallsForTheAgent(t *testing.T) { + setClaudeAgent(t) + home, cacheDir := t.TempDir(), t.TempDir() + asked := setInstallsFor(t, true) + + // Both lines are withheld, and no event or throttle marker is written. + ctx1, stderr1 := sessionCtx(t, home, cacheDir, true) + MaybeHint(ctx1, regularCmd(), noPluginRecorded) + assert.Empty(t, stderr1.String()) + assert.Empty(t, hintKinds(t, ctx1)) + assert.Equal(t, []string{NameClaudeCode}, *asked) + + // That run did not install after all: the session's next command hints. + setInstallsFor(t, false) + ctx2, stderr2 := sessionCtx(t, home, cacheDir, true) + MaybeHint(ctx2, regularCmd(), noPluginRecorded) + assert.Contains(t, stderr2.String(), wantNativeHint) + assert.Contains(t, stderr2.String(), hintText) + assert.Equal(t, []protos.AitoolsHintKind{protos.AitoolsHintKindClaudeCodeNative, protos.AitoolsHintKindInstallText}, hintKinds(t, ctx2)) +} + +func TestMaybeHint_RegistryAgentAskedByItsName(t *testing.T) { + testutil.SetAgentEnv(t, "CURSOR_AGENT", "1") + cwd := t.TempDir() + require.NoError(t, os.Mkdir(filepath.Join(cwd, ".git"), 0o755)) + t.Chdir(cwd) + asked := setInstallsFor(t, true) + ctx, stderr := claudeCtx(t, t.TempDir()) + ctx = telemetry.WithNewLogger(ctx) + MaybeHint(ctx, regularCmd(), noPluginRecorded) + assert.Empty(t, stderr.String()) + assert.Empty(t, hintKinds(t, ctx)) + assert.Equal(t, []string{NameCursor}, *asked) +} + +func TestMaybeHint_InstalledToolingSkipsTheAutoInstallCheck(t *testing.T) { + setClaudeAgent(t) + home := t.TempDir() + writeClaudeSkill(t, home, "databricks-core") + asked := setInstallsFor(t, true) + ctx, _ := claudeCtx(t, home) + MaybeHint(ctx, regularCmd(), noPluginRecorded) + assert.Empty(t, *asked) +} diff --git a/libs/aitools/autoinstall/hook.go b/libs/aitools/autoinstall/hook.go index e22746c2caf..b1fdb6b29c1 100644 --- a/libs/aitools/autoinstall/hook.go +++ b/libs/aitools/autoinstall/hook.go @@ -16,3 +16,12 @@ import ( // command ends the CLI by it once it uploaded the telemetry and closed the log // file. var Run func(cmd *cobra.Command) os.Signal + +// InstallsFor, when set, reports whether the Run after cmd is due to give +// the agent with this registry name its first Databricks AI tools. The install +// hint, printed before the command runs, is withheld then so the agent is not +// told to install what the same run installs. It reads only local state: the +// gate is looked up after the command, so a run it reports may still end +// without installing, and the hint then waits for the next command. main.go +// sets it with Run. +var InstallsFor func(cmd *cobra.Command, agentName string) bool diff --git a/main.go b/main.go index a08ab8c2ec9..e562ee8a16c 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ func main() { // which imports it. It is wired here, not in a package init, for the same // reason as DATABRICKS_CLI_PATH above. autoinstall.Run = aitools.AutoInstall + autoinstall.InstallsFor = aitools.AutoInstallsFor ctx := context.Background() err := cmd.New(ctx).Execute(ctx, os.Args[1:]) From b11724535236d47d847fef0f553f33442e50f70f Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:48:28 +0000 Subject: [PATCH 2/3] Add the PR link to the changelog fragment Co-authored-by: Isaac --- .nextchanges/cli/aitools-auto-install-hint-contradiction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.nextchanges/cli/aitools-auto-install-hint-contradiction.md b/.nextchanges/cli/aitools-auto-install-hint-contradiction.md index fb32654c731..b9400cfb2d2 100644 --- a/.nextchanges/cli/aitools-auto-install-hint-contradiction.md +++ b/.nextchanges/cli/aitools-auto-install-hint-contradiction.md @@ -1 +1 @@ -* A coding agent no longer gets the "Databricks skills are not installed" hint on a command after which the automatic installation of Databricks AI tools installs them for that agent, so its output no longer tells it to install what the same command installs. When that installation does not happen, the hint appears on the next command. +* A coding agent no longer gets the "Databricks skills are not installed" hint on a command after which the automatic installation of Databricks AI tools installs them for that agent, so its output no longer tells it to install what the same command installs. When that installation does not happen, the hint appears on the next command. ([#7890](https://github.com/databricks/cli/pull/7890)) From cb5377dad4d1305434ecba5134ebaaa1a627e788 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Mon, 23 Nov 2026 20:47:03 +0000 Subject: [PATCH 3/3] aitools: print a hint withheld for auto-install once the command ends Auto-install runs only after a command succeeded, and its gate lookup or install can still end without tools. The hint withheld for it now prints when the command ends unless the agent has the tools by then, so a failing command still gets it. A holdout machine is not withheld from, and an update counts as installing for every agent with a config directory. Co-authored-by: Isaac --- ...aitools-auto-install-hint-contradiction.md | 2 +- .../auto-install/agent-hint/output.txt | 15 +++++- .../aitools/auto-install/agent-hint/script | 6 ++- cmd/aitools/autoinstall_hint.go | 23 +++++++-- cmd/aitools/autoinstall_hint_test.go | 6 +++ cmd/root/root.go | 20 +++++++- libs/aitools/agents/hint.go | 50 +++++++++++++------ libs/aitools/agents/hint_test.go | 48 ++++++++++++++---- libs/aitools/autoinstall/hook.go | 14 +++--- 9 files changed, 141 insertions(+), 43 deletions(-) diff --git a/.nextchanges/cli/aitools-auto-install-hint-contradiction.md b/.nextchanges/cli/aitools-auto-install-hint-contradiction.md index b9400cfb2d2..80150c43ad6 100644 --- a/.nextchanges/cli/aitools-auto-install-hint-contradiction.md +++ b/.nextchanges/cli/aitools-auto-install-hint-contradiction.md @@ -1 +1 @@ -* A coding agent no longer gets the "Databricks skills are not installed" hint on a command after which the automatic installation of Databricks AI tools installs them for that agent, so its output no longer tells it to install what the same command installs. When that installation does not happen, the hint appears on the next command. ([#7890](https://github.com/databricks/cli/pull/7890)) +* A coding agent no longer gets the "Databricks skills are not installed" hint on a command after which the automatic installation of Databricks AI tools installs them for that agent, so its output no longer tells it to install what the same command installs. When that installation does not happen, for example because the command failed, the hint appears once the command ends. ([#7890](https://github.com/databricks/cli/pull/7890)) diff --git a/acceptance/cmd/aitools/auto-install/agent-hint/output.txt b/acceptance/cmd/aitools/auto-install/agent-hint/output.txt index 6d60407db1b..62ec35240df 100644 --- a/acceptance/cmd/aitools/auto-install/agent-hint/output.txt +++ b/acceptance/cmd/aitools/auto-install/agent-hint/output.txt @@ -1,4 +1,15 @@ +=== a failing command: no install, the hint once the command ends + +>>> CLAUDECODE=1 [CLI] jobs get abc +Error: invalid JOB_ID: abc +Databricks skills are not installed. To work with Databricks reliably, first run: databricks aitools install + +Exit code: 1 + +>>> print_telemetry +{"command":"jobs_get","exit_code":1,"aitools_hint_event":{"hint_kind":"INSTALL_TEXT"}} + === first run of a new version: the install notice, no install hint >>> CLAUDECODE=1 [CLI] jobs list @@ -14,11 +25,13 @@ Installed Databricks AI tools for Claude Code, Cursor. Disable: databricks aitoo >>> print_telemetry -=== gate off: nothing on the first run, the hint on the next +=== gate off: no install, the hint once the command ends, and on the next >>> CLAUDECODE=1 [CLI] jobs list +Databricks skills are not installed. To work with Databricks reliably, first run: databricks aitools install >>> print_telemetry +{"command":"jobs_list","exit_code":0,"aitools_hint_event":{"hint_kind":"INSTALL_TEXT"}} >>> CLAUDECODE=1 [CLI] jobs list Databricks skills are not installed. To work with Databricks reliably, first run: databricks aitools install diff --git a/acceptance/cmd/aitools/auto-install/agent-hint/script b/acceptance/cmd/aitools/auto-install/agent-hint/script index 2bf9e7494fc..080d464f2aa 100644 --- a/acceptance/cmd/aitools/auto-install/agent-hint/script +++ b/acceptance/cmd/aitools/auto-install/agent-hint/script @@ -6,6 +6,10 @@ use_home home "$mkdir_bin" -p "${USERPROFILE:-$HOME}/.claude" gate true +title "a failing command: no install, the hint once the command ends" +errcode trace CLAUDECODE=1 $CLI jobs get abc +trace print_telemetry + title "first run of a new version: the install notice, no install hint" trace CLAUDECODE=1 $CLI jobs list trace print_telemetry @@ -14,7 +18,7 @@ title "second run: the tools are installed, nothing" trace CLAUDECODE=1 $CLI jobs list trace print_telemetry -title "gate off: nothing on the first run, the hint on the next" +title "gate off: no install, the hint once the command ends, and on the next" use_home no-agents-home "$mkdir_bin" -p "${USERPROFILE:-$HOME}/.claude" gate false diff --git a/cmd/aitools/autoinstall_hint.go b/cmd/aitools/autoinstall_hint.go index 46f05721c86..8c67da9ebc2 100644 --- a/cmd/aitools/autoinstall_hint.go +++ b/cmd/aitools/autoinstall_hint.go @@ -3,15 +3,20 @@ package aitools import ( "slices" + "github.com/databricks/cli/libs/aitools/agents" "github.com/databricks/cli/libs/aitools/autoinstall" "github.com/spf13/cobra" ) -// AutoInstallsFor reports whether the auto-install after cmd is due to give -// the agent agentName its first tools (see autoinstall.InstallsFor). It decides -// as autoInstall does up to the gate lookup, which it skips: a run that would +// AutoInstallsFor reports whether the auto-install after cmd may give the +// agent agentName tools (see autoinstall.InstallsFor). It decides as +// autoInstall does up to the gate lookup, which it skips: a run that would // look the gate up counts as installing, so on a version whose gate is off the -// install hint waits one command. +// install hint moves to the end of the command. A machine whose stored ID puts +// it in the holdout never installs. A first install gives tools to the agents +// its plan names; an update gives skills to every agent with a config +// directory when a skill changed, which is only known after the manifest is +// fetched, so it counts as installing for each of them. func AutoInstallsFor(cmd *cobra.Command, agentName string) bool { ctx := cmd.Context() in := autoInstallInputs(ctx, skipsAutoInstall(cmd)) @@ -23,10 +28,18 @@ func AutoInstallsFor(cmd *cobra.Command, agentName string) bool { if err != nil || autoinstall.Decide(in) != autoinstall.ReasonGateOff { return false } + if in.State.ID != "" && autoinstall.InHoldout(autoinstall.Bucket(in.State.ID)) { + return false + } tools, err := planTools(quietContext(ctx), in.State) - if err != nil { + if err != nil || tools.skip != "" { return false } + if tools.existing != nil { + return slices.ContainsFunc(agents.DetectInstalled(ctx), func(a *agents.Agent) bool { + return a.Name == agentName + }) + } for _, it := range tools.plan { if it.agent.Name == agentName { return slices.Contains(tools.newAgents(), it.agent.DisplayName) diff --git a/cmd/aitools/autoinstall_hint_test.go b/cmd/aitools/autoinstall_hint_test.go index cba068ba9e0..369d633415a 100644 --- a/cmd/aitools/autoinstall_hint_test.go +++ b/cmd/aitools/autoinstall_hint_test.go @@ -16,6 +16,9 @@ import ( "github.com/stretchr/testify/require" ) +// globalInstallState is a global install state file of an earlier install. +const globalInstallState = `{"schema_version": 2, "release": "v0.1.0", "skills": {"databricks-test": "0.1.0"}}` + func TestAutoInstallsFor(t *testing.T) { tests := []struct { name string @@ -40,6 +43,9 @@ func TestAutoInstallsFor(t *testing.T) { {name: "a development build installs nothing", env: map[string]string{"DATABRICKS_AITOOLS_AUTO_INSTALL_CLI_VERSION": ""}}, {name: "a command that skips auto-install installs nothing", command: "aitools list"}, {name: "a failed plan installs nothing", installState: "not json"}, + {name: "a holdout machine installs nothing", state: `{"id": "holdout-machine"}`}, + {name: "an update may install for an agent with a config dir", installState: globalInstallState, want: true}, + {name: "an update installs nothing for an agent without a config dir", installState: globalInstallState, agent: agents.NameClaudeCode}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { diff --git a/cmd/root/root.go b/cmd/root/root.go index 9f8886dd25b..56c199f2d25 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -99,9 +99,14 @@ func newRootCommand(ctx context.Context) *cobra.Command { // when it drives the CLI without the tooling installed (best-effort: a // stderr line and a telemetry event that is uploaded at exit). Only the // global install state is read, as for the calling-agent User-Agent. - agents.MaybeHint(ctx, cmd, func(ctx context.Context, agentName string) bool { + deferred := agents.MaybeHint(ctx, cmd, func(ctx context.Context, agentName string) bool { return installer.PluginRecorded(ctx, installer.ScopeGlobal, agentName) }) + // A hint withheld for auto-install prints once the command has ended. + // Commands run without execute have no slot, and then it is dropped. + if slot, ok := ctx.Value(deferredHintKey{}).(*func()); ok { + *slot = deferred + } return nil } @@ -131,6 +136,11 @@ func newRootCommand(ctx context.Context) *cobra.Command { // the context. It holds the signal that stopped auto-install, if one did. type caughtSignalKey struct{} +// deferredHintKey is the context key of the *func() that execute puts in the +// context. It holds the agent hint withheld for auto-install, if one was (see +// agents.MaybeHint). +type deferredHintKey struct{} + // raise is process.Raise; tests replace it. var raise = process.Raise @@ -194,6 +204,8 @@ Stack Trace: ctx = context.WithValue(ctx, logFileKey{}, &logFile) var caught os.Signal ctx = context.WithValue(ctx, caughtSignalKey{}, &caught) + var deferredHint func() + ctx = context.WithValue(ctx, deferredHintKey{}, &deferredHint) // slog.SetDefault also redirects the standard log package, so both are restored. prevLogger := slog.Default() prevLogWriter, prevLogFlags := stdlog.Writer(), stdlog.Flags() @@ -228,6 +240,12 @@ Stack Trace: fmt.Fprintf(cmd.ErrOrStderr(), "Error: %s\n", err.Error()) } + // After auto-install, which runs only when the command succeeded, and + // before the telemetry upload, which sends its hint-shown event. + if deferredHint != nil { + deferredHint() + } + exitCode := ExitStatus(err) // Log exit status and error diff --git a/libs/aitools/agents/hint.go b/libs/aitools/agents/hint.go index dd6dbbcc60b..66dda383a97 100644 --- a/libs/aitools/agents/hint.go +++ b/libs/aitools/agents/hint.go @@ -210,22 +210,28 @@ func (h *agentHint) nativeLine(ctx context.Context) string { // Best-effort: it never fails the command. Besides the stderr lines, it logs a // telemetry event for each hint line shown, which makes a command that resolved // auth upload telemetry at exit. -func MaybeHint(ctx context.Context, cmd *cobra.Command, pluginRecorded PluginRecordedFunc) { +// +// When the auto-install after cmd may give the agent its tools (see +// autoinstall.InstallsFor), the hint is withheld: MaybeHint returns a function +// to call once the command has ended, which prints the hint unless the tooling +// is installed by then. A failed command, a gate that turns out off or a +// failed install then still gets the hint. Otherwise it returns nil. +func MaybeHint(ctx context.Context, cmd *cobra.Command, pluginRecorded PluginRecordedFunc) (deferred func()) { hint := hintForAgent(useragent.AgentProvider(), pluginRecorded) if hint == nil { - return + return nil } if isHintExemptCommand(cmd) { - return + return nil } if IsCredentialHelperCommand(cmd) { - return + return nil } if IsSelfExec(ctx) { - return + return nil } if !cmdio.HasIO(ctx) { - return + return nil } // Check the throttles before the filesystem probe, so a throttled session // skips the plugin/skills detection. A marker is written only once its @@ -238,20 +244,32 @@ func MaybeHint(ctx context.Context, cmd *cobra.Command, pluginRecorded PluginRec } textDue := !textHintThrottle.seen(ctx, key) if native == "" && !textDue { - return + return nil } if hint.installed(ctx) { - return + return nil } // The auto-install after this command prints its own notice; neither line - // may tell the agent to install what it installs. No marker is written, so - // a run that ends up not installing leaves the hint to the next command. + // may tell the agent to install what it installs. No marker is written + // until the hint prints. if autoinstall.InstallsFor != nil && autoinstall.InstallsFor(cmd, hint.agentName) { - return + return func() { + if hint.installed(ctx) { + return + } + hint.show(ctx, key, native, textDue) + } } + hint.show(ctx, key, native, textDue) + return nil +} + +// show prints the lines MaybeHint found due, records their throttle markers +// and logs their hint-shown events. +func (h *agentHint) show(ctx context.Context, key, native string, textDue bool) { // Only agents without a native line have a durable throttle, so a failed // claim leaves nothing to print. - if textDue && !hint.claim(ctx, key) { + if textDue && !h.claim(ctx, key) { return } if native != "" { @@ -260,15 +278,15 @@ func MaybeHint(ctx context.Context, cmd *cobra.Command, pluginRecorded PluginRec // A text print starts a new text window, in which this is the first // native print. if textDue || !nativeEventThrottle.seen(ctx, key) { - logHintShown(ctx, hint.nativeKind) + logHintShown(ctx, h.nativeKind) nativeEventThrottle.set(ctx, key, true) } - } else if textDue && hint.native != nil { + } else if textDue && h.native != nil { nativeEventThrottle.set(ctx, key, false) } if textDue { - cmdio.LogString(ctx, hint.text) - logHintShown(ctx, hint.textKind) + cmdio.LogString(ctx, h.text) + logHintShown(ctx, h.textKind) } } diff --git a/libs/aitools/agents/hint_test.go b/libs/aitools/agents/hint_test.go index 85be1a7e746..1884d296150 100644 --- a/libs/aitools/agents/hint_test.go +++ b/libs/aitools/agents/hint_test.go @@ -826,20 +826,46 @@ func TestMaybeHint_WithheldWhileAutoInstallInstallsForTheAgent(t *testing.T) { home, cacheDir := t.TempDir(), t.TempDir() asked := setInstallsFor(t, true) - // Both lines are withheld, and no event or throttle marker is written. + // The run installs: both lines are withheld, and no event or throttle + // marker is written. ctx1, stderr1 := sessionCtx(t, home, cacheDir, true) - MaybeHint(ctx1, regularCmd(), noPluginRecorded) + deferred := MaybeHint(ctx1, regularCmd(), noPluginRecorded) + require.NotNil(t, deferred) + assert.Equal(t, []string{NameClaudeCode}, *asked) + writeClaudeSkill(t, home, "databricks-core") + deferred() assert.Empty(t, stderr1.String()) assert.Empty(t, hintKinds(t, ctx1)) - assert.Equal(t, []string{NameClaudeCode}, *asked) +} - // That run did not install after all: the session's next command hints. - setInstallsFor(t, false) +func TestMaybeHint_WithheldHintPrintsWhenTheRunDidNotInstall(t *testing.T) { + setClaudeAgent(t) + home, cacheDir := t.TempDir(), t.TempDir() + setInstallsFor(t, true) + + // Nothing prints before the command; once it ended without the tools + // (it failed, the gate was off or the install failed), both lines do. + ctx1, stderr1 := sessionCtx(t, home, cacheDir, true) + deferred := MaybeHint(ctx1, regularCmd(), noPluginRecorded) + require.NotNil(t, deferred) + assert.Empty(t, stderr1.String()) + deferred() + assert.Contains(t, stderr1.String(), wantNativeHint) + assert.Contains(t, stderr1.String(), hintText) + assert.Equal(t, []protos.AitoolsHintKind{protos.AitoolsHintKindClaudeCodeNative, protos.AitoolsHintKindInstallText}, hintKinds(t, ctx1)) + + // The markers were written then: the session's next command is throttled. ctx2, stderr2 := sessionCtx(t, home, cacheDir, true) - MaybeHint(ctx2, regularCmd(), noPluginRecorded) - assert.Contains(t, stderr2.String(), wantNativeHint) - assert.Contains(t, stderr2.String(), hintText) - assert.Equal(t, []protos.AitoolsHintKind{protos.AitoolsHintKindClaudeCodeNative, protos.AitoolsHintKindInstallText}, hintKinds(t, ctx2)) + assert.Nil(t, MaybeHint(ctx2, regularCmd(), noPluginRecorded)) + assert.Empty(t, stderr2.String()) +} + +func TestMaybeHint_NotWithheldWithoutAutoInstall(t *testing.T) { + setClaudeAgent(t) + setInstallsFor(t, false) + ctx, stderr := sessionCtx(t, t.TempDir(), t.TempDir(), true) + assert.Nil(t, MaybeHint(ctx, regularCmd(), noPluginRecorded)) + assert.Contains(t, stderr.String(), hintText) } func TestMaybeHint_RegistryAgentAskedByItsName(t *testing.T) { @@ -850,7 +876,7 @@ func TestMaybeHint_RegistryAgentAskedByItsName(t *testing.T) { asked := setInstallsFor(t, true) ctx, stderr := claudeCtx(t, t.TempDir()) ctx = telemetry.WithNewLogger(ctx) - MaybeHint(ctx, regularCmd(), noPluginRecorded) + assert.NotNil(t, MaybeHint(ctx, regularCmd(), noPluginRecorded)) assert.Empty(t, stderr.String()) assert.Empty(t, hintKinds(t, ctx)) assert.Equal(t, []string{NameCursor}, *asked) @@ -862,6 +888,6 @@ func TestMaybeHint_InstalledToolingSkipsTheAutoInstallCheck(t *testing.T) { writeClaudeSkill(t, home, "databricks-core") asked := setInstallsFor(t, true) ctx, _ := claudeCtx(t, home) - MaybeHint(ctx, regularCmd(), noPluginRecorded) + assert.Nil(t, MaybeHint(ctx, regularCmd(), noPluginRecorded)) assert.Empty(t, *asked) } diff --git a/libs/aitools/autoinstall/hook.go b/libs/aitools/autoinstall/hook.go index b1fdb6b29c1..8883d24d035 100644 --- a/libs/aitools/autoinstall/hook.go +++ b/libs/aitools/autoinstall/hook.go @@ -17,11 +17,11 @@ import ( // file. var Run func(cmd *cobra.Command) os.Signal -// InstallsFor, when set, reports whether the Run after cmd is due to give -// the agent with this registry name its first Databricks AI tools. The install -// hint, printed before the command runs, is withheld then so the agent is not -// told to install what the same run installs. It reads only local state: the -// gate is looked up after the command, so a run it reports may still end -// without installing, and the hint then waits for the next command. main.go -// sets it with Run. +// InstallsFor, when set, reports whether the Run after cmd may give the agent +// with this registry name Databricks AI tools. The install hint, printed before +// the command runs, is withheld then so the agent is not told to install what +// the same run installs. It reads only local state: the gate is looked up +// after the command, Run is skipped when the command fails, and an install can +// fail, so the withheld hint prints when the command ends unless the agent has +// the tools by then. main.go sets it with Run. var InstallsFor func(cmd *cobra.Command, agentName string) bool