diff --git a/.nextchanges/cli/aitools-auto-install-keeps-newer-skills.md b/.nextchanges/cli/aitools-auto-install-keeps-newer-skills.md new file mode 100644 index 00000000000..74b64d683ff --- /dev/null +++ b/.nextchanges/cli/aitools-auto-install-keeps-newer-skills.md @@ -0,0 +1 @@ +* Automatic installation of Databricks AI tools no longer moves skills you installed from a newer release (for example with `DATABRICKS_SKILLS_REF`) back to the older release this CLI version is pinned to, and no longer reports that as "Updated"; it leaves them alone. `databricks aitools update` still installs the pinned release when you run it. ([#7888](https://github.com/databricks/cli/pull/7888)) diff --git a/acceptance/cmd/aitools/auto-install/script.prepare b/acceptance/cmd/aitools/auto-install/script.prepare index ad4bd0fd397..ebc99b77818 100644 --- a/acceptance/cmd/aitools/auto-install/script.prepare +++ b/acceptance/cmd/aitools/auto-install/script.prepare @@ -57,11 +57,12 @@ use_home() { echo "{\"id\": \"${2:-treatment-machine}\"}" > "${USERPROFILE:-$HOME}/.databricks/aitools/auto-install.json" } -# gate true|false writes a compatibility manifest into the CLI cache with that -# auto_install flag. A fresh cache file replaces the remote manifest. +# gate true|false [SKILLS] writes a compatibility manifest into the CLI cache +# with that auto_install flag and skills version SKILLS (default: latest). A +# fresh cache file replaces the remote manifest. gate() { "$mkdir_bin" -p "$DATABRICKS_CACHE_DIR" - echo "{\"0.0.0\": {\"appkit\": \"0.84.0\", \"skills\": \"latest\", \"auto_install\": $1}}" > "$DATABRICKS_CACHE_DIR/compat-manifest.json" + echo "{\"0.0.0\": {\"appkit\": \"0.84.0\", \"skills\": \"${2:-latest}\", \"auto_install\": $1}}" > "$DATABRICKS_CACHE_DIR/compat-manifest.json" } # cursor_skills lists the skills installed for Cursor. diff --git a/acceptance/cmd/aitools/auto-install/upgrade/output.txt b/acceptance/cmd/aitools/auto-install/upgrade/output.txt index f608694937f..e7e2668e911 100644 --- a/acceptance/cmd/aitools/auto-install/upgrade/output.txt +++ b/acceptance/cmd/aitools/auto-install/upgrade/output.txt @@ -64,3 +64,29 @@ kept 2.0.0 "last_version": "1.32.0", "id": "treatment-machine" } + +>>> DATABRICKS_SKILLS_REF=v0.4.0 [CLI] aitools install --skills-only --scope=global --agents=cursor +Installing Databricks skills for Cursor... +Using skills version 0.4.0 +Fetching skills manifest... +Installed 2 skills in [TEST_TMP_DIR]/pinned-home/.databricks/aitools/skills. + +=== upgrade to 1.33.0 pinned to 0.3.0 over an explicit 0.4.0 install: no downgrade, nothing printed, not retried + +>>> DATABRICKS_AITOOLS_AUTO_INSTALL_CLI_VERSION=1.33.0 [CLI] jobs list + +>>> print_telemetry +{"command":"jobs_list","exit_code":0,"aitools_auto_install_event":{"outcome":"KEPT","holdout_arm":"TREATMENT","interactive":false}} + +>>> cursor_skills +databricks-edited +databricks-kept + +>>> skill_file databricks-kept +kept 2.0.0 + +>>> auto_install_state +{ + "last_version": "1.33.0", + "id": "treatment-machine" +} diff --git a/acceptance/cmd/aitools/auto-install/upgrade/script b/acceptance/cmd/aitools/auto-install/upgrade/script index 98b3705fab5..0cd0338817d 100644 --- a/acceptance/cmd/aitools/auto-install/upgrade/script +++ b/acceptance/cmd/aitools/auto-install/upgrade/script @@ -36,3 +36,18 @@ title "an older CLI on the same machine (e.g. one an IDE ships) does nothing" trace DATABRICKS_AITOOLS_AUTO_INSTALL_CLI_VERSION=1.31.0 $CLI jobs list trace print_telemetry trace auto_install_state + +# The user pinned release 0.4.0 with an explicit install. The compatibility +# manifest pins this CLI to the older 0.3.0, as it pins released CLIs. +use_home pinned-home +skills_dir="${USERPROFILE:-$HOME}/.databricks/aitools/skills" +trace DATABRICKS_SKILLS_REF=v0.4.0 $CLI aitools install --skills-only --scope=global --agents=cursor +print_telemetry > /dev/null +gate true 0.3.0 + +title "upgrade to 1.33.0 pinned to 0.3.0 over an explicit 0.4.0 install: no downgrade, nothing printed, not retried" +trace DATABRICKS_AITOOLS_AUTO_INSTALL_CLI_VERSION=1.33.0 $CLI jobs list +trace print_telemetry +trace cursor_skills +trace skill_file databricks-kept +trace auto_install_state diff --git a/acceptance/cmd/aitools/auto-install/upgrade/test.toml b/acceptance/cmd/aitools/auto-install/upgrade/test.toml index 1511d4aaad2..cd0ef11212d 100644 --- a/acceptance/cmd/aitools/auto-install/upgrade/test.toml +++ b/acceptance/cmd/aitools/auto-install/upgrade/test.toml @@ -1,3 +1,5 @@ +Ignore = ["pinned-home"] + # Skills releases: the latest release is v0.3.0, under /newer it is v0.4.0 and # under /older it is v0.2.0 (DATABRICKS_CLI_GITHUB_API_URL picks one). diff --git a/cmd/aitools/autoinstall_plan_test.go b/cmd/aitools/autoinstall_plan_test.go index 14dc7ef3411..e6f780ce6ce 100644 --- a/cmd/aitools/autoinstall_plan_test.go +++ b/cmd/aitools/autoinstall_plan_test.go @@ -1,6 +1,8 @@ package aitools import ( + "context" + "errors" "maps" "os" "path/filepath" @@ -9,6 +11,7 @@ import ( "github.com/databricks/cli/libs/aitools/agents" "github.com/databricks/cli/libs/aitools/autoinstall" "github.com/databricks/cli/libs/aitools/installer" + "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/telemetry/protos" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" @@ -161,3 +164,46 @@ func TestSkipsAutoInstall(t *testing.T) { assert.False(t, skipsAutoInstall(cmd), cmd.CommandPath()) } } + +// The update runs the plugin updates before it refuses the raw skills, so a +// refusal still reports the plugins it updated. +func TestUpdateToolsReportsPluginsUpdatedBeforeARefusal(t *testing.T) { + autoInstallHome(t) + noTelemetryAuth(t) + t.Setenv("DATABRICKS_SKILLS_REF", "v0.3.0") + resetUpdatePluginSeams(t) + dir := saveGlobalState(t, map[string]string{"databricks-sql": "0.1.0"}) + before, err := installer.LoadState(dir) + require.NoError(t, err) + + origUpdateSkills := updateSkillsFn + t.Cleanup(func() { updateSkillsFn = origUpdateSkills }) + updateSkillsFn = func(context.Context, installer.ManifestSource, []*agents.Agent, installer.UpdateOptions) (*installer.UpdateResult, error) { + return nil, &installer.RefusedError{Err: errors.New("keeping the installed skills")} + } + pluginUpdated := false + updatePluginsFn = func(context.Context, string, string) ([]installer.PluginUpdate, error) { + if !pluginUpdated { + return nil, nil + } + state, err := installer.LoadState(dir) + require.NoError(t, err) + rec := state.Plugins[agents.NameClaudeCode] + rec.Version = "0.3.0" + state.Plugins[agents.NameClaudeCode] = rec + require.NoError(t, installer.SaveState(dir, state)) + return []installer.PluginUpdate{{Agent: "Claude Code", Version: "0.3.0"}}, nil + } + ctx := cmdio.MockDiscard(t.Context()) + + // Nothing changed: the refusal is returned, which auto-install reports + // as kept. + _, err = updateTools(ctx, dir, before) + assert.ErrorAs(t, err, new(*installer.RefusedError)) + + pluginUpdated = true + result, err := updateTools(ctx, dir, before) + require.NoError(t, err) + assert.Equal(t, protos.AitoolsAutoInstallOutcomeInstalled, result.outcome) + assert.Equal(t, []string{"Claude Code"}, result.agents) +} diff --git a/cmd/aitools/autoinstall_run.go b/cmd/aitools/autoinstall_run.go index 8138881157e..9efc3608bf7 100644 --- a/cmd/aitools/autoinstall_run.go +++ b/cmd/aitools/autoinstall_run.go @@ -532,8 +532,9 @@ func (t toolsPlan) fresh() bool { // planTools plans what `databricks aitools install --scope=global` runs // without prompts, for the agents whose config directory exists. An existing // global installation is updated as `databricks aitools update --scope=global` -// does instead, which keeps its downgrade guard, keeps the skills whose files -// the user modified at their version, and does not migrate skills to plugins. +// does instead, which keeps its downgrade guard, also keeps an installed +// release newer than a pinned one, keeps the skills whose files the user +// modified at their version, and does not migrate skills to plugins. // Planning reads only local state; prior is the auto-install state. func planTools(ctx context.Context, prior autoinstall.State) (toolsPlan, error) { globalDir, err := installer.GlobalSkillsDir(ctx) @@ -627,8 +628,12 @@ func updateTools(ctx context.Context, globalDir string, before *installer.Instal uctx := telemetry.WithNewLogger(ctx) uctx = context.WithValue(uctx, funnelKey{}, &commandFunnel{command: protos.AitoolsCommandUpdate}) uctx = context.WithValue(uctx, telemetryAuthKey{}, &telemetryAuth{}) - if err := runUpdate(uctx, updateFlags{scope: installer.ScopeGlobal, keepModified: true}); err != nil { - return installResult{}, err + updateErr := runUpdate(uctx, updateFlags{scope: installer.ScopeGlobal, keepModified: true}) + // The update refuses the raw skills after it updated the plugins, so a + // refusal still reports the plugins it changed. + _, refused := errors.AsType[*installer.RefusedError](updateErr) + if updateErr != nil && !refused { + return installResult{}, updateErr } after, err := installer.LoadState(globalDir) if err != nil { @@ -638,9 +643,14 @@ func updateTools(ctx context.Context, globalDir string, before *installer.Instal if after != nil { result.agents = changedAgents(ctx, before, after) } - if len(result.agents) > 0 { - result.outcome = protos.AitoolsAutoInstallOutcomeInstalled + if len(result.agents) == 0 { + // Nothing changed: the caller reports a refusal as kept. + return result, updateErr + } + if refused { + log.Debugf(ctx, "auto-install: kept the installed skills: %v", updateErr) } + result.outcome = protos.AitoolsAutoInstallOutcomeInstalled return result, nil } diff --git a/cmd/aitools/update.go b/cmd/aitools/update.go index b4865afa570..673b00e981f 100644 --- a/cmd/aitools/update.go +++ b/cmd/aitools/update.go @@ -79,7 +79,8 @@ type updateFlags struct { project, global bool // keepModified is not a flag: auto-install sets it (see // installer.UpdateOptions.KeepModified). It also keeps the raw skills of - // agents that have, or could get, the plugin. + // agents that have, or could get, the plugin, and refuses a downgrade + // (installer.UpdateOptions.NoDowngrade). keepModified bool } @@ -219,6 +220,7 @@ func runUpdate(ctx context.Context, f updateFlags) error { Scope: scope, KeepModified: f.keepModified, + NoDowngrade: f.keepModified, } opts.Skills = skills diff --git a/internal/build/README.md b/internal/build/README.md index d82afac3751..2f60d423f81 100644 --- a/internal/build/README.md +++ b/internal/build/README.md @@ -23,7 +23,7 @@ An entry can also set `"auto_install": true` to turn on the automatic installati This skills fallback only applies to raw-file delivery. Version checks omit update availability when latest is unknown, and plugin operations don't substitute the fallback version. -Raw-skills updates and reinstalls reject automatic downgrades, including with `--force`: the fallback must be at least as new as the installed release. Existing installs without a comparable release require a successful latest-release fetch. Explicit `DATABRICKS_SKILLS_REF` overrides and compatibility pins can still select an older release. +Raw-skills updates and reinstalls reject automatic downgrades, including with `--force`: the fallback must be at least as new as the installed release. Existing installs without a comparable release require a successful latest-release fetch. Explicit `DATABRICKS_SKILLS_REF` overrides and compatibility pins can still select an older release, except in automatic installation: it keeps an installed release newer than the pinned one, which `databricks aitools update` still installs. CLIs before `1.6.0` reject the whole remote manifest when any entry uses `"latest"`, even an entry they never resolve to. They use a valid cached manifest or their embedded manifest without returning a parse error. These CLIs stop receiving remote compatibility updates for both AppKit and skills. diff --git a/libs/aitools/installer/update.go b/libs/aitools/installer/update.go index cbd3a48bd89..7bf0df4a128 100644 --- a/libs/aitools/installer/update.go +++ b/libs/aitools/installer/update.go @@ -36,6 +36,13 @@ type UpdateOptions struct { // no record to check against), instead of replacing it. Automatic updates // set it: they must never overwrite what the user changed. KeepModified bool + + // NoDowngrade refuses a release older than the installed one even when + // the ref is pinned (by DATABRICKS_SKILLS_REF or the compatibility + // manifest). A pinned branch or commit SHA is still installed. Automatic + // updates set it: a newer release the user installed is theirs to roll + // back. + NoDowngrade bool } // UpdateResult describes what UpdateSkills did (or would do in check mode). @@ -109,6 +116,12 @@ func UpdateSkills(ctx context.Context, src ManifestSource, targetAgents []*agent return &UpdateResult{Unchanged: slices.Sorted(maps.Keys(state.Skills))}, nil } + // A pinned ref is the ref installed, so it is compared before fetching its + // manifest. A branch or commit SHA has no release order to compare. + if explicit && opts.NoDowngrade && semver.IsValid(latestTag) && semver.Compare(latestTag, installedRef) < 0 { + return nil, &RefusedError{Err: fmt.Errorf("keeping the installed skills %s, which are newer than the pinned %s; run 'databricks aitools update' to install %s", DisplaySkillsVersion(installedRef), DisplaySkillsVersion(latestTag), DisplaySkillsVersion(latestTag))} + } + manifest, latestTag, err := FetchSkillsManifestWithFallback(ctx, src, latestTag, allowFallback && !explicit) if err != nil { if opts.Check { diff --git a/libs/aitools/installer/update_test.go b/libs/aitools/installer/update_test.go index 0c582d0bb8b..6f13f2df026 100644 --- a/libs/aitools/installer/update_test.go +++ b/libs/aitools/installer/update_test.go @@ -3,6 +3,7 @@ package installer import ( "bytes" "context" + "errors" "io/fs" "log/slog" "os" @@ -163,6 +164,73 @@ func TestUpdateKeepModifiedKeepsEditedSkill(t *testing.T) { assert.Equal(t, "0.2.0", state.Skills["databricks-jobs"]) } +func TestUpdateNoDowngradeRefusesAnOlderPinnedRelease(t *testing.T) { + tmp := setupTestHome(t) + ctx := cmdio.MockDiscard(t.Context()) + setupFetchMock(t) + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.0") + + src := &mockManifestSource{manifest: testManifest()} + agent := testAgent(tmp) + require.NoError(t, InstallSkillsForAgents(ctx, src, []*agents.Agent{agent}, InstallOptions{})) + + // A pinned older release, as the compatibility manifest pins one. It is + // refused before its manifest is fetched. + t.Setenv("DATABRICKS_SKILLS_REF", testSkillsRef) + unfetchable := &mockManifestSource{fetchErr: errors.New("the manifest was fetched")} + _, err := UpdateSkills(ctx, unfetchable, []*agents.Agent{agent}, UpdateOptions{NoDowngrade: true}) + assert.ErrorAs(t, err, new(*RefusedError)) + assert.ErrorContains(t, err, "keeping the installed skills 0.2.0, which are newer than the pinned 0.1.5; run 'databricks aitools update' to install 0.1.5") + globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") + state, err := LoadState(globalDir) + require.NoError(t, err) + assert.Equal(t, "v0.2.0", state.Release) + + // Without it, the pinned ref is the user's choice. + _, err = UpdateSkills(ctx, src, []*agents.Agent{agent}, UpdateOptions{}) + require.NoError(t, err) + state, err = LoadState(globalDir) + require.NoError(t, err) + assert.Equal(t, testSkillsRef, state.Release) +} + +func TestUpdateNoDowngradeInstallsANewerPinnedRelease(t *testing.T) { + tmp := setupTestHome(t) + ctx := cmdio.MockDiscard(t.Context()) + setupFetchMock(t) + t.Setenv("DATABRICKS_SKILLS_REF", testSkillsRef) + + src := &mockManifestSource{manifest: testManifest()} + agent := testAgent(tmp) + require.NoError(t, InstallSkillsForAgents(ctx, src, []*agents.Agent{agent}, InstallOptions{})) + + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.0") + _, err := UpdateSkills(ctx, src, []*agents.Agent{agent}, UpdateOptions{NoDowngrade: true}) + require.NoError(t, err) + state, err := LoadState(filepath.Join(tmp, ".databricks", "aitools", "skills")) + require.NoError(t, err) + assert.Equal(t, "v0.2.0", state.Release) +} + +func TestUpdateNoDowngradeInstallsAPinnedBranch(t *testing.T) { + tmp := setupTestHome(t) + ctx := cmdio.MockDiscard(t.Context()) + setupFetchMock(t) + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.0") + + src := &mockManifestSource{manifest: testManifest()} + agent := testAgent(tmp) + require.NoError(t, InstallSkillsForAgents(ctx, src, []*agents.Agent{agent}, InstallOptions{})) + + // A branch has no release order, so it is not a downgrade. + t.Setenv("DATABRICKS_SKILLS_REF", "main") + _, err := UpdateSkills(ctx, src, []*agents.Agent{agent}, UpdateOptions{NoDowngrade: true}) + require.NoError(t, err) + state, err := LoadState(filepath.Join(tmp, ".databricks", "aitools", "skills")) + require.NoError(t, err) + assert.Equal(t, "main", state.Release) +} + func TestUpdateCheckDryRun(t *testing.T) { tmp := setupTestHome(t) ctx := cmdio.MockDiscard(t.Context())