diff --git a/.nextchanges/cli/aitools-update-keeps-skills-only.md b/.nextchanges/cli/aitools-update-keeps-skills-only.md new file mode 100644 index 00000000000..c93acc8dc2b --- /dev/null +++ b/.nextchanges/cli/aitools-update-keeps-skills-only.md @@ -0,0 +1 @@ +* `databricks aitools update` no longer replaces skill files with the Databricks plugin for Claude Code, Codex or GitHub Copilot when you installed them with `databricks aitools install --skills-only` from this release on while the agent's CLI was on PATH. For an earlier `--skills-only` install, run `databricks aitools install --skills-only` again, with the agent's CLI on PATH, to keep the skill files, or run `databricks aitools install` without `--skills-only` to switch to the plugin. ([#7831](https://github.com/databricks/cli/pull/7831)) diff --git a/acceptance/experimental/aitools/skills/install-non-interactive/output.txt b/acceptance/experimental/aitools/skills/install-non-interactive/output.txt index 0bb5d48e863..cd23f84b81f 100644 --- a/acceptance/experimental/aitools/skills/install-non-interactive/output.txt +++ b/acceptance/experimental/aitools/skills/install-non-interactive/output.txt @@ -40,6 +40,19 @@ Using skills version test-ref Fetching skills manifest... Installed 1 skill in [TEST_TMP_DIR]/home/.databricks/aitools/skills. +=== --skills-only with the agent's CLI on PATH: update keeps the skill files + +>>> [CLI] aitools install --agents=claude-code --skills-only +Installing Databricks skills for Claude Code... +Using skills version test-ref +Fetching skills manifest... +Installed 1 skill in [TEST_TMP_DIR]/home/.databricks/aitools/skills. + +>>> [CLI] aitools update --force + updated test-stable v1.0.0 -> v1.0.0 +Updated 1 skill. +Claude Code has skill files + === project scope: a personal plugin does not replace the project's skill files >>> [CLI] aitools install --scope=project diff --git a/acceptance/experimental/aitools/skills/install-non-interactive/script b/acceptance/experimental/aitools/skills/install-non-interactive/script index bb361d77522..8535a262014 100644 --- a/acceptance/experimental/aitools/skills/install-non-interactive/script +++ b/acceptance/experimental/aitools/skills/install-non-interactive/script @@ -8,6 +8,23 @@ unset CLAUDE_CONFIG_DIR PI_CODING_AGENT_DIR GEMINI_CLI_HOME GOOSE_PATH_ROOT export XDG_CONFIG_HOME="${USERPROFILE:-$HOME}/.config" export APPDATA="${USERPROFILE:-$HOME}/AppData/Roaming" +# The harness's agent CLI stand-ins (acceptance/internal/fakeagent), for the +# runs below that need an agent CLI on PATH. They only accept the MCP commands, +# so a plugin install through one of them fails. Without them those runs would +# not test anything, so the test stops instead. Each stand-in appends its argv +# to FAKE_AGENT_LOG; Go opens it, so on msys it is in the mixed form. +if [[ -z "${FAKE_AGENT_DIR:-}" ]]; then + echo "FAKE_AGENT_DIR is empty: the agent CLI stand-ins were not built" >&2 + exit 1 +fi +export FAKE_AGENT_LOG="$PWD/agent-calls.log" +if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" || "$OSTYPE" == "win32" ]]; then + fake_agent_path=$(cygpath -u "$FAKE_AGENT_DIR") + export FAKE_AGENT_LOG="$(cygpath -m "$FAKE_AGENT_LOG")" +else + fake_agent_path=$FAKE_AGENT_DIR +fi + # Agents are also detected by their CLI binary on PATH, so drop every PATH entry # that holds one; otherwise the developer's own agents would join the run. That # can drop /usr/bin (and /bin on a merged-/usr system), so resolve the commands @@ -66,6 +83,16 @@ trace musterr $CLI aitools install --agents=claude-code title "the suggested recovery command works" trace $CLI aitools install --agents=claude-code --skills-only +title "--skills-only with the agent's CLI on PATH: update keeps the skill files" +# With Claude Code's CLI on PATH, --skills-only records that the user declined +# the plugin, so update refreshes the skill files and does not try the plugin. +path_without_agents=$PATH +export PATH="$fake_agent_path:$PATH" +trace $CLI aitools install --agents=claude-code --skills-only +trace $CLI aitools update --force +check_claude_skills +export PATH=$path_without_agents + title "project scope: a personal plugin does not replace the project's skill files" # Project skill files are shared through the repository, so a user-scope plugin # installed with Claude Code itself later must not stop the update from diff --git a/acceptance/experimental/aitools/skills/install-non-interactive/test.toml b/acceptance/experimental/aitools/skills/install-non-interactive/test.toml index 7afa69b4552..8002262ced2 100644 --- a/acceptance/experimental/aitools/skills/install-non-interactive/test.toml +++ b/acceptance/experimental/aitools/skills/install-non-interactive/test.toml @@ -4,6 +4,7 @@ Env.DATABRICKS_SKILLS_REF = "test-ref" Ignore = [ "home", + "agent-calls.log", ] [[Server]] diff --git a/cmd/aitools/install.go b/cmd/aitools/install.go index 821870f21da..a1f89a7a4e6 100644 --- a/cmd/aitools/install.go +++ b/cmd/aitools/install.go @@ -124,6 +124,15 @@ type agentChoice struct { } func NewInstallCmd() *cobra.Command { + return newInstallCmd(true) +} + +// newInstallCmd builds the install command. recordSkillsOnly says whether +// --skills-only records the user's choice of raw skills, so update keeps those +// agents off the plugin. The deprecated `experimental aitools skills install` +// alias passes false: it always adds --skills-only to keep its pre-plugin +// behavior, so its users did not decline the plugin. +func newInstallCmd(recordSkillsOnly bool) *cobra.Command { var skillsFlag, agentsFlag, scopeFlag, pathFlag string var includeExperimental, skillsOnly bool var projectFlag, globalFlag bool @@ -196,6 +205,7 @@ Supported agents: ` + strings.Join(agents.SupportedNames(), ", "), opts := installer.InstallOptions{ IncludeExperimental: includeExperimental, SpecificSkills: splitAndTrim(skillsFlag), + SkillsOnly: skillsOnly && recordSkillsOnly, } // --skills cherry-picks individual skill files, which only applies to @@ -407,10 +417,11 @@ Supported agents: ` + strings.Join(agents.SupportedNames(), ", "), } // installOutputIsJSON reports whether --output json was requested. Unlike list, -// install can run detached from root: the legacy `skills install` alias builds a -// NewInstallCmd and executes it directly (see newLegacySkillsInstallCmd), so the -// root-supplied --output flag may be absent. Treat a missing flag as text rather -// than panicking the way root.OutputType would. +// install can run detached from root: the legacy `skills install` alias builds an +// install command with newInstallCmd and executes it directly (see +// newLegacySkillsInstallCmd), so the root-supplied --output flag may be absent. +// Treat a missing flag as text rather than panicking the way root.OutputType +// would. func installOutputIsJSON(cmd *cobra.Command) bool { f := cmd.Flag("output") if f == nil { @@ -956,6 +967,7 @@ func executePlan(ctx context.Context, src installer.ManifestSource, plan []agent ref = "" } records := map[string]installer.PluginRecord{} + var failed []string for _, it := range pluginItems { if !quiet { cmdio.LogString(ctx, fmt.Sprintf("Installing databricks plugin for %s...", it.agent.DisplayName)) @@ -978,6 +990,7 @@ func executePlan(ctx context.Context, src installer.ManifestSource, plan []agent if it.explicit { explicitErrs = append(explicitErrs, &displayError{msg: it.agent.DisplayName + ": " + reason, err: err}) } + failed = append(failed, it.agent.Name) continue } records[it.agent.Name] = rec @@ -992,6 +1005,14 @@ func executePlan(ctx context.Context, src installer.ManifestSource, plan []agent cmdio.LogString(ctx, fmt.Sprintf(" %s databricks plugin %s", it.agent.DisplayName, versionToken(rec.Version))) } } + // Asking for the plugin ends an earlier --skills-only choice even when + // the install fails, so update tries the plugin again. A successful + // install ends it when the plugin is recorded. + if len(failed) > 0 { + if err := installer.ForgetSkillsOnlyChoice(ctx, opts.Scope, failed); err != nil { + log.Warnf(ctx, "Could not drop the --skills-only choice for %s: %v", strings.Join(failed, ", "), err) + } + } if len(records) > 0 && skillsErr != nil && installer.HasUntrackedSkills(ctx, opts.Scope) { // Skills with no state file are on disk (an install that predates // state tracking, or an interrupted first install). A state file diff --git a/cmd/aitools/install_test.go b/cmd/aitools/install_test.go index 02f3e3ce1f4..a89da97b96e 100644 --- a/cmd/aitools/install_test.go +++ b/cmd/aitools/install_test.go @@ -335,6 +335,8 @@ func TestMapAgentScope(t *testing.T) { } func TestExecutePlanSkipBlockedPluginExit0(t *testing.T) { + // A failed plugin install rewrites the install state; keep it off the real HOME. + setupTestAgents(t) t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") fakeBinsOnPath(t, "claude", "codex") orig := installPluginForAgentFn @@ -373,7 +375,42 @@ func TestExecutePlanSkipBlockedPluginExit0(t *testing.T) { assert.NotErrorIs(t, err, errNoAgentSetUp) } +// An install that tries the plugin for an agent the user had put on raw skills +// ends that choice even when the plugin install fails, so update tries the +// plugin again. The choice of an agent not asked for the plugin stays. +func TestExecutePlanFailedPluginForgetsSkillsOnlyChoice(t *testing.T) { + setupTestAgents(t) + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") + fakeBinsOnPath(t, "claude") + orig := installPluginForAgentFn + t.Cleanup(func() { installPluginForAgentFn = orig }) + installPluginForAgentFn = func(_ context.Context, a *agents.Agent, _, _ string) (installer.PluginRecord, error) { + return installer.PluginRecord{}, &installer.BlockedError{Agent: a.Name, Reason: installer.ReasonInstallFailed} + } + + ctx := cmdio.MockDiscard(t.Context()) + dir, err := installer.GlobalSkillsDir(ctx) + require.NoError(t, err) + require.NoError(t, installer.SaveState(dir, &installer.InstallState{ + SchemaVersion: installer.CurrentSchemaVersion, + Release: "v0.2.5", + Skills: map[string]string{"databricks": "0.2.5"}, + SkillsOnlyAgents: []string{agents.NameClaudeCode, agents.NameCodex}, + })) + + claude := testPluginAgent(agents.NameClaudeCode, "Claude Code", "claude") + plan := buildPlan(t.Context(), []*agents.Agent{claude}, installer.ScopeGlobal, false, false) + _, err = executePlan(ctx, nil, plan, installer.InstallOptions{Scope: installer.ScopeGlobal}, installInvocation{scope: installer.ScopeGlobal}, true) + require.ErrorIs(t, err, errNoAgentSetUp) + + st, err := installer.LoadState(dir) + require.NoError(t, err) + assert.Equal(t, []string{agents.NameCodex}, st.SkillsOnlyAgents) +} + func TestExecutePlanNothingInstalledIsAnError(t *testing.T) { + // A failed plugin install rewrites the install state; keep it off the real HOME. + setupTestAgents(t) t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") fakeBinsOnPath(t, "claude") orig := installPluginForAgentFn @@ -506,6 +543,8 @@ func TestExecutePlanNoSkillsToInstall(t *testing.T) { } func TestExecutePlanNoSkillsToInstallWithPluginFailures(t *testing.T) { + // A failed plugin install rewrites the install state; keep it off the real HOME. + setupTestAgents(t) t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") fakeBinsOnPath(t, "claude") setupPluginMock(t) @@ -906,6 +945,7 @@ func TestInstallSkillsOnlyAllAgents(t *testing.T) { require.Len(t, *calls, 1) assert.Len(t, (*calls)[0].agents, 2) assert.Nil(t, (*calls)[0].opts.SpecificSkills) + assert.True(t, (*calls)[0].opts.SkillsOnly) } func TestInstallSkillsOnlySpecificSkills(t *testing.T) { diff --git a/cmd/aitools/legacy_skills.go b/cmd/aitools/legacy_skills.go index 43ba95dc274..f55f4cf1736 100644 --- a/cmd/aitools/legacy_skills.go +++ b/cmd/aitools/legacy_skills.go @@ -42,7 +42,8 @@ func newLegacySkillsInstallCmd() *cobra.Command { Deprecated: `use "databricks aitools install --skills " instead.`, Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - installCmd := NewInstallCmd() + // Its --skills-only is not the user's choice, so it is not recorded. + installCmd := newInstallCmd(false) installCmd.SetContext(cmd.Context()) // Root prints the returned error; the detached command must not print // it, or its usage, a second time. diff --git a/cmd/aitools/legacy_skills_test.go b/cmd/aitools/legacy_skills_test.go index 7f1a75b83fb..f7cf36b0cba 100644 --- a/cmd/aitools/legacy_skills_test.go +++ b/cmd/aitools/legacy_skills_test.go @@ -30,6 +30,9 @@ func TestLegacySkillsInstallDelegatesToInstall(t *testing.T) { require.Len(t, *calls, 1) assert.Len(t, (*calls)[0].agents, 2) + // The alias adds --skills-only itself, so its users did not decline the + // plugin: the choice is not recorded and update still migrates them. + assert.False(t, (*calls)[0].opts.SkillsOnly) } func TestLegacySkillsInstallForwardsSkillName(t *testing.T) { diff --git a/cmd/aitools/update.go b/cmd/aitools/update.go index 8ab71a87e65..b4865afa570 100644 --- a/cmd/aitools/update.go +++ b/cmd/aitools/update.go @@ -299,6 +299,10 @@ func pluginMigrationCandidatesForScope(ctx context.Context, scope string, state if _, alreadyPlugin := state.Plugins[agent.Name]; alreadyPlugin { continue } + // The user chose raw skills over the plugin (install --skills-only). + if slices.Contains(state.SkillsOnlyAgents, agent.Name) { + continue + } } nativeScope, ok, _ := mapAgentScope(agent, scope) if !ok { diff --git a/cmd/aitools/update_test.go b/cmd/aitools/update_test.go index ad6ad104776..54a2e328887 100644 --- a/cmd/aitools/update_test.go +++ b/cmd/aitools/update_test.go @@ -356,6 +356,68 @@ func TestUpdateDoesNotMigratePluginAgentWithoutManagedRawSkills(t *testing.T) { assert.Equal(t, []string{agents.NameClaudeCode, agents.NameCursor}, rawUpdateAgents) } +// Raw skills the user chose with install --skills-only stay raw skills: update +// neither plans nor runs their migration to the plugin, and refreshes them. +func TestUpdateKeepsSkillsOnlyAgentsOnRawSkills(t *testing.T) { + noTelemetryAuth(t) + setupTestAgents(t) + fakeBinsOnPath(t, "claude") + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") + resetUpdatePluginSeams(t) + + var rawUpdateAgents []string + origUpdateSkills := updateSkillsFn + t.Cleanup(func() { updateSkillsFn = origUpdateSkills }) + updateSkillsFn = func(_ context.Context, _ installer.ManifestSource, targetAgents []*agents.Agent, opts installer.UpdateOptions) (*installer.UpdateResult, error) { + if !opts.Check { + for _, a := range targetAgents { + rawUpdateAgents = append(rawUpdateAgents, a.Name) + } + } + return &installer.UpdateResult{}, nil + } + updatePluginsFn = func(context.Context, string, string) ([]installer.PluginUpdate, error) { + return nil, nil + } + updateInstallPluginForAgentFn = func(context.Context, *agents.Agent, string, string) (installer.PluginRecord, error) { + require.Fail(t, "must not migrate an agent the user put on raw skills") + return installer.PluginRecord{}, nil + } + updateRecordPluginInstallsFn = func(context.Context, string, map[string]installer.PluginRecord, string) error { + require.Fail(t, "must not record plugin installs without migration") + return nil + } + updateCleanupLegacyFn = func(context.Context, *agents.Agent, string) error { + require.Fail(t, "must not clean up the raw skills the user chose") + return nil + } + hasManagedRawSkillsForAgentFn = func(_ context.Context, a *agents.Agent, _ string) (bool, error) { + return a.Plugin != nil, nil + } + + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + dir, err := installer.GlobalSkillsDir(ctx) + require.NoError(t, err) + require.NoError(t, installer.SaveState(dir, &installer.InstallState{ + SchemaVersion: 2, + Release: "v0.2.5", + Skills: map[string]string{"databricks": "0.2.5"}, + SkillsOnlyAgents: []string{agents.NameClaudeCode}, + })) + + check := NewUpdateCmd() + check.SetContext(ctx) + check.SetArgs([]string{"--scope", "global", "--check"}) + require.NoError(t, check.Execute()) + assert.NotContains(t, stderr.String(), "would migrate") + + update := NewUpdateCmd() + update.SetContext(ctx) + update.SetArgs([]string{"--scope", "global"}) + require.NoError(t, update.Execute()) + assert.Equal(t, []string{agents.NameClaudeCode, agents.NameCursor}, rawUpdateAgents) +} + func TestUpdateNoPruneFlag(t *testing.T) { noTelemetryAuth(t) setupTestAgents(t) @@ -490,6 +552,24 @@ func TestPluginMigrationCandidatesNeedTheCLI(t *testing.T) { assert.Equal(t, []string{agents.NameCodex}, names) } +// Only the agents the user put on raw skills with install --skills-only stay +// there; other raw skills (an older CLI, a missing agent CLI) still migrate. +func TestPluginMigrationCandidatesSkipSkillsOnlyAgents(t *testing.T) { + setupTestAgents(t) + fakeBinsOnPath(t, "claude", "codex", "copilot") + resetUpdatePluginSeams(t) + hasManagedRawSkillsForAgentFn = func(context.Context, *agents.Agent, string) (bool, error) { return true, nil } + + state := &installer.InstallState{SkillsOnlyAgents: []string{agents.NameClaudeCode, agents.NameCodex}} + candidates, err := pluginMigrationCandidatesForScope(t.Context(), installer.ScopeGlobal, state) + require.NoError(t, err) + var names []string + for _, c := range candidates { + names = append(names, c.agent.Name) + } + assert.Equal(t, []string{agents.NameCopilot}, names) +} + // Install keeps a plugin installed with Claude Code's own CLI without recording // it, so update must not write the raw skills back next to it. A project's // skill files are shared, so at project scope only a project-scope plugin of diff --git a/libs/aitools/installer/installer.go b/libs/aitools/installer/installer.go index f2603739964..ae6f67a7be3 100644 --- a/libs/aitools/installer/installer.go +++ b/libs/aitools/installer/installer.go @@ -226,6 +226,9 @@ type InstallOptions struct { IncludeExperimental bool SpecificSkills []string // empty = all skills Scope string // ScopeGlobal or ScopeProject (default: global) + // SkillsOnly records the target agents that could have had the plugin in + // InstallState.SkillsOnlyAgents: the user chose raw skills for them. + SkillsOnly bool } func fetchSkillFile(ctx context.Context, ref, repoDir, skillName, filePath string) ([]byte, error) { @@ -472,6 +475,17 @@ func InstallSkillsForAgents(ctx context.Context, src ManifestSource, targetAgent // enabled; this field does not retroactively remove them. state.IncludeExperimental = opts.IncludeExperimental state.Scope = scope + if opts.SkillsOnly { + for _, a := range targetAgents { + // Without its CLI on PATH the agent could not have had the plugin: + // install suggests --skills-only for that case too, and update + // should migrate the agent once the CLI is there. + if a.Plugin != nil && a.HasBinary(ctx) && !slices.Contains(state.SkillsOnlyAgents, a.Name) { + state.SkillsOnlyAgents = append(state.SkillsOnlyAgents, a.Name) + } + } + slices.Sort(state.SkillsOnlyAgents) + } for name, meta := range targetSkills { state.Skills[name] = meta.Version state.RepoDirs[name] = meta.RepoDir diff --git a/libs/aitools/installer/installer_test.go b/libs/aitools/installer/installer_test.go index 13013982a02..c9bca4010d7 100644 --- a/libs/aitools/installer/installer_test.go +++ b/libs/aitools/installer/installer_test.go @@ -12,6 +12,7 @@ import ( "net/http" "net/http/httptest" "os" + "os/exec" "path/filepath" "sync" "syscall" @@ -460,6 +461,50 @@ func TestInstallSkillsForAgentsWritesState(t *testing.T) { assert.Contains(t, stderr.String(), "Installed 2 skills in ") } +// --skills-only records the agents that could have had the plugin (their CLI +// is on PATH), so update keeps them on raw skills. An agent whose CLI is +// missing is not recorded: install suggests --skills-only for it, and update +// migrates it once the CLI is there. A later install without --skills-only +// keeps the record: it says nothing about the user's choice. +func TestInstallSkillsForAgentsRecordsSkillsOnlyAgents(t *testing.T) { + tmp := setupTestHome(t) + ctx := cmdio.MockDiscard(t.Context()) + setupFetchMock(t) + t.Setenv("DATABRICKS_SKILLS_REF", testSkillsRef) + src := &mockManifestSource{manifest: testManifest()} + + origLookPath := agents.LookPath + t.Cleanup(func() { agents.LookPath = origLookPath }) + agents.LookPath = func(name string) (string, error) { + if name == "plugin-agent-cli" { + return filepath.Join("/usr/bin", name), nil + } + return "", exec.ErrNotFound + } + + plain := testAgent(tmp) + withPlugin := testAgent(tmp) + withPlugin.Name = "plugin-agent" + withPlugin.Binary = "plugin-agent-cli" + withPlugin.Plugin = &agents.PluginSpec{} + cliMissing := testAgent(tmp) + cliMissing.Name = "cli-missing-agent" + cliMissing.Binary = "cli-missing-agent-cli" + cliMissing.Plugin = &agents.PluginSpec{} + targets := []*agents.Agent{withPlugin, cliMissing, plain} + globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") + + require.NoError(t, InstallSkillsForAgents(ctx, src, targets, InstallOptions{SkillsOnly: true})) + state, err := LoadState(globalDir) + require.NoError(t, err) + assert.Equal(t, []string{"plugin-agent"}, state.SkillsOnlyAgents) + + require.NoError(t, InstallSkillsForAgents(ctx, src, targets, InstallOptions{})) + state, err = LoadState(globalDir) + require.NoError(t, err) + assert.Equal(t, []string{"plugin-agent"}, state.SkillsOnlyAgents) +} + func setupSkillsFallback(t *testing.T, latestErr error) (ManifestSource, string) { t.Helper() setBuildVersion(t, "1.22.0") diff --git a/libs/aitools/installer/plugin.go b/libs/aitools/installer/plugin.go index af595e7153a..9c2a942c43a 100644 --- a/libs/aitools/installer/plugin.go +++ b/libs/aitools/installer/plugin.go @@ -7,6 +7,7 @@ import ( "maps" "path/filepath" "regexp" + "slices" "strings" "sync" "time" @@ -503,6 +504,11 @@ func RecordPluginInstalls(ctx context.Context, cliScope string, records map[stri state.Plugins = make(map[string]PluginRecord, len(records)) } maps.Copy(state.Plugins, records) + // An agent with the plugin is no longer on raw skills by choice. + state.SkillsOnlyAgents = slices.DeleteFunc(state.SkillsOnlyAgents, func(name string) bool { + _, ok := records[name] + return ok + }) // An unresolved release keeps the one recorded by earlier installs. if ref != "" { state.Release = ref @@ -558,6 +564,30 @@ func ForgetPluginInstalls(ctx context.Context, cliScope string, agentNames []str return SaveState(dir, state) } +// ForgetSkillsOnlyChoice removes the named agents from the agents the user put +// on raw skills with `install --skills-only`, in the state file for the given +// CLI scope. Install calls it for agents whose plugin install it attempted and +// that failed, so update tries the plugin again. Agents not in the list, or a +// scope without state, are left as they are. +func ForgetSkillsOnlyChoice(ctx context.Context, cliScope string, agentNames []string) error { + dir, err := skillsDir(ctx, cliScope) + if err != nil { + return err + } + state, err := LoadState(dir) + if err != nil || state == nil { + return err + } + n := len(state.SkillsOnlyAgents) + state.SkillsOnlyAgents = slices.DeleteFunc(state.SkillsOnlyAgents, func(name string) bool { + return slices.Contains(agentNames, name) + }) + if len(state.SkillsOnlyAgents) == n { + return nil + } + return SaveState(dir, state) +} + // ManualPluginInstallCommands returns the commands, in order, that install the // databricks plugin through the agent's own CLI. When the headless install // fails, running them by hand shows the agent's full output. Returns nil for an diff --git a/libs/aitools/installer/plugin_test.go b/libs/aitools/installer/plugin_test.go index fd0552cc99f..f1046dd16c4 100644 --- a/libs/aitools/installer/plugin_test.go +++ b/libs/aitools/installer/plugin_test.go @@ -820,6 +820,60 @@ func TestRecordPluginInstallsUnresolvedRefKeepsRelease(t *testing.T) { assert.Contains(t, st.Plugins, agents.NameCodex) } +// Installing the plugin for an agent the user had put on raw skills ends that +// choice; the other agents keep theirs. +func TestRecordPluginInstallsForgetsSkillsOnlyChoice(t *testing.T) { + setupTestHome(t) + ctx := cmdio.MockDiscard(t.Context()) + dir, err := GlobalSkillsDir(ctx) + require.NoError(t, err) + require.NoError(t, SaveState(dir, &InstallState{ + SchemaVersion: CurrentSchemaVersion, + Skills: map[string]string{"databricks": "0.1.0"}, + SkillsOnlyAgents: []string{agents.NameClaudeCode, agents.NameCodex}, + })) + + require.NoError(t, RecordPluginInstalls(ctx, ScopeGlobal, map[string]PluginRecord{ + agents.NameCodex: {Marketplace: "databricks-agent-skills", Plugin: "databricks"}, + }, "v0.2.6")) + + st, err := LoadState(dir) + require.NoError(t, err) + assert.Equal(t, []string{agents.NameClaudeCode}, st.SkillsOnlyAgents) +} + +// Forgetting the --skills-only choice of some agents keeps the choice of the +// others and the rest of the state. +func TestForgetSkillsOnlyChoice(t *testing.T) { + setupTestHome(t) + ctx := cmdio.MockDiscard(t.Context()) + dir, err := GlobalSkillsDir(ctx) + require.NoError(t, err) + require.NoError(t, SaveState(dir, &InstallState{ + SchemaVersion: CurrentSchemaVersion, + Skills: map[string]string{"databricks": "0.1.0"}, + SkillsOnlyAgents: []string{agents.NameClaudeCode, agents.NameCodex}, + })) + + require.NoError(t, ForgetSkillsOnlyChoice(ctx, ScopeGlobal, []string{agents.NameCodex, agents.NameCopilot})) + + st, err := LoadState(dir) + require.NoError(t, err) + assert.Equal(t, []string{agents.NameClaudeCode}, st.SkillsOnlyAgents) + assert.Equal(t, map[string]string{"databricks": "0.1.0"}, st.Skills) +} + +func TestForgetSkillsOnlyChoiceWithoutState(t *testing.T) { + setupTestHome(t) + ctx := cmdio.MockDiscard(t.Context()) + require.NoError(t, ForgetSkillsOnlyChoice(ctx, ScopeGlobal, []string{agents.NameCodex})) + dir, err := GlobalSkillsDir(ctx) + require.NoError(t, err) + st, err := LoadState(dir) + require.NoError(t, err) + assert.Nil(t, st) +} + func TestRecordPluginInstallsThenSkillsInstallNoPanic(t *testing.T) { tmp := setupTestHome(t) ctx := cmdio.MockDiscard(t.Context()) diff --git a/libs/aitools/installer/state.go b/libs/aitools/installer/state.go index f44c33921d4..599cacab00b 100644 --- a/libs/aitools/installer/state.go +++ b/libs/aitools/installer/state.go @@ -59,6 +59,17 @@ type InstallState struct { // e.g. "databricks/SKILL.md"). Added in schema v2; used to prune only the // skills we wrote that the user hasn't modified. Files map[string]FileRecord `json:"files,omitempty"` + // SkillsOnlyAgents names, sorted, the agents with a plugin that + // `install --skills-only` gave raw skills while their CLI was on PATH, so + // the plugin was declined, not unavailable. Update keeps them on raw + // skills. Raw skills of other plugin agents were written by an older CLI or + // while the agent's CLI was missing, and update migrates those to the + // plugin. + // + // Unlike Plugins, nothing on disk shows this choice again: an older CLI + // that rewrites the state drops the field, and update then migrates these + // agents to the plugin like any other raw skills. + SkillsOnlyAgents []string `json:"skills_only_agents,omitempty"` // MCP records the Databricks MCP servers the `aitools mcp` commands wrote. // Added in schema v3; omitted when none are recorded. SaveState ignores this // field and keeps the section on disk; SaveMCPState is the only writer of it. diff --git a/libs/aitools/installer/state_test.go b/libs/aitools/installer/state_test.go index feeb59d81bd..e92ba66d85d 100644 --- a/libs/aitools/installer/state_test.go +++ b/libs/aitools/installer/state_test.go @@ -199,7 +199,8 @@ func TestSaveAndLoadStateWithOptionalFields(t *testing.T) { "databricks": stableSkillsRepoPath, "sql-tools": experimentalRepoPath, }, - Scope: "project", + Scope: "project", + SkillsOnlyAgents: []string{"claude-code", "codex"}, } err := SaveState(dir, original) diff --git a/libs/aitools/installer/uninstall.go b/libs/aitools/installer/uninstall.go index ffa36c9b769..90ceb95d996 100644 --- a/libs/aitools/installer/uninstall.go +++ b/libs/aitools/installer/uninstall.go @@ -150,13 +150,20 @@ func UninstallSkillsWithResult(ctx context.Context, opts UninstallOptions) (*Uni // without a failure though no copy was left to remove, e.g. a stale entry. skillsChanged, skillsUninstalled := 0, 0 var skillFailures []error + // failedAgents names the targeted agents that keep an entry they could not + // remove. + failedAgents := map[string]bool{} for _, name := range toRemove { canonicalDir := filepath.Join(baseDir, name) removeCanonical := true changed := false var failures []error if targetedAgents { - changed, failures = removeSkillExposuresFromAgents(ctx, name, canonicalDir, scope, cwd, state, opts.Agents) + var failed []string + changed, failures, failed = removeSkillExposuresFromAgents(ctx, name, canonicalDir, scope, cwd, state, opts.Agents) + for _, agentName := range failed { + failedAgents[agentName] = true + } removeCanonical = !hasRemainingSkillExposure(ctx, name, canonicalDir, scope, cwd, opts.Agents) } else { changed = removeSymlinksFromAgents(ctx, name, canonicalDir, scope, cwd) @@ -197,6 +204,20 @@ func UninstallSkillsWithResult(ctx context.Context, opts UninstallOptions) (*Uni } } + // Uninstalling an agent ends its --skills-only choice, so raw skills it + // gets later another way are migrated to the plugin by update. An agent + // that keeps an entry it could not remove keeps the choice, since its files + // are still there; with --agents, the other named agents lose theirs. + if fullUninstall { + if targetedAgents { + state.SkillsOnlyAgents = slices.DeleteFunc(state.SkillsOnlyAgents, func(name string) bool { + return agentFilter[name] && !failedAgents[name] + }) + } else if len(skillFailures) == 0 { + state.SkillsOnlyAgents = nil + } + } + // If nothing remains in this scope, clean up orphaned symlinks and delete the // state file; otherwise persist the trimmed state. if len(state.Skills) == 0 && len(state.Plugins) == 0 { @@ -240,10 +261,11 @@ func stringSet(items []string) map[string]bool { // removeSkillExposuresFromAgents removes the skill from the named agents' skills // dirs and reports whether it removed an existing entry for any of them, plus -// the errors from entries it failed to remove. -func removeSkillExposuresFromAgents(ctx context.Context, skillName, canonicalDir, scope, cwd string, state *InstallState, agentNames []string) (bool, []error) { +// the errors from entries it failed to remove and the agents they belong to. +func removeSkillExposuresFromAgents(ctx context.Context, skillName, canonicalDir, scope, cwd string, state *InstallState, agentNames []string) (bool, []error, []string) { removed := false var failures []error + var failedAgents []string for _, name := range agentNames { agent := agents.ByName(name) if agent == nil { @@ -268,11 +290,12 @@ func removeSkillExposuresFromAgents(ctx context.Context, skillName, canonicalDir if err := removeAllFn(entry); err != nil { log.Warnf(ctx, "Failed to remove %s: %v", entry, err) failures = append(failures, err) + failedAgents = append(failedAgents, name) continue } removed = true } - return removed, failures + return removed, failures, failedAgents } func hasRemainingSkillExposure(ctx context.Context, skillName, canonicalDir, scope, cwd string, removedAgents []string) bool { diff --git a/libs/aitools/installer/uninstall_test.go b/libs/aitools/installer/uninstall_test.go index 1413cde08c2..e3bc55ea333 100644 --- a/libs/aitools/installer/uninstall_test.go +++ b/libs/aitools/installer/uninstall_test.go @@ -354,6 +354,7 @@ func TestUninstallSelectiveAllRemovesStateFile(t *testing.T) { func TestUninstallTargetedAgentKeepsRemainingRawExposure(t *testing.T) { tmp := setupTestHome(t) + clearAgentDirOverrides(t) ctx := cmdio.MockDiscard(t.Context()) setupFetchMock(t) t.Setenv("DATABRICKS_SKILLS_REF", testSkillsRef) @@ -384,6 +385,156 @@ func TestUninstallTargetedAgentKeepsRemainingRawExposure(t *testing.T) { assert.Contains(t, state.Skills, "databricks-sql") } +// Uninstalling an agent ends its --skills-only choice, so raw skills it gets +// later another way are migrated by update. Removing only some of its skills +// keeps the choice, and so does uninstalling another agent. +func TestUninstallTargetedAgentForgetsSkillsOnlyChoice(t *testing.T) { + tmp := setupTestHome(t) + clearAgentDirOverrides(t) + ctx := cmdio.MockDiscard(t.Context()) + setupFetchMock(t) + t.Setenv("DATABRICKS_SKILLS_REF", testSkillsRef) + require.NoError(t, os.MkdirAll(filepath.Join(tmp, ".claude"), 0o755)) + require.NoError(t, os.MkdirAll(filepath.Join(tmp, ".cursor"), 0o755)) + + claudeAgent := agents.ByName(agents.NameClaudeCode) + cursorAgent := agents.ByName(agents.NameCursor) + src := &mockManifestSource{manifest: testManifest()} + require.NoError(t, InstallSkillsForAgents(ctx, src, []*agents.Agent{claudeAgent, cursorAgent}, InstallOptions{})) + + globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") + state, err := LoadState(globalDir) + require.NoError(t, err) + state.SkillsOnlyAgents = []string{agents.NameClaudeCode, agents.NameCodex} + require.NoError(t, SaveState(globalDir, state)) + + _, err = UninstallSkillsWithResult(ctx, UninstallOptions{Agents: []string{agents.NameClaudeCode}, Skills: []string{"databricks-sql"}}) + require.NoError(t, err) + state, err = LoadState(globalDir) + require.NoError(t, err) + assert.Equal(t, []string{agents.NameClaudeCode, agents.NameCodex}, state.SkillsOnlyAgents) + + _, err = UninstallSkillsWithResult(ctx, UninstallOptions{Agents: []string{agents.NameClaudeCode}}) + require.NoError(t, err) + state, err = LoadState(globalDir) + require.NoError(t, err) + require.NotNil(t, state) + assert.Equal(t, []string{agents.NameCodex}, state.SkillsOnlyAgents) +} + +// A targeted uninstall that leaves one of the agent's skills in place keeps +// the agent's --skills-only choice, since it still has skill files. +func TestUninstallTargetedAgentFailureKeepsSkillsOnlyChoice(t *testing.T) { + tmp := setupTestHome(t) + clearAgentDirOverrides(t) + ctx := cmdio.MockDiscard(t.Context()) + setupFetchMock(t) + t.Setenv("DATABRICKS_SKILLS_REF", testSkillsRef) + require.NoError(t, os.MkdirAll(filepath.Join(tmp, ".claude"), 0o755)) + require.NoError(t, os.MkdirAll(filepath.Join(tmp, ".cursor"), 0o755)) + + claudeAgent := agents.ByName(agents.NameClaudeCode) + cursorAgent := agents.ByName(agents.NameCursor) + src := &mockManifestSource{manifest: testManifest()} + require.NoError(t, InstallSkillsForAgents(ctx, src, []*agents.Agent{claudeAgent, cursorAgent}, InstallOptions{})) + + globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") + state, err := LoadState(globalDir) + require.NoError(t, err) + state.SkillsOnlyAgents = []string{agents.NameClaudeCode} + require.NoError(t, SaveState(globalDir, state)) + + claudeSQL := filepath.Join(tmp, ".claude", "skills", "databricks-sql") + orig := removeAllFn + t.Cleanup(func() { removeAllFn = orig }) + removeAllFn = func(path string) error { + if path == claudeSQL { + return fs.ErrPermission + } + return orig(path) + } + + result, err := UninstallSkillsWithResult(ctx, UninstallOptions{Agents: []string{agents.NameClaudeCode}}) + require.NoError(t, err) + assert.NotEmpty(t, result.SkillFailures) + state, err = LoadState(globalDir) + require.NoError(t, err) + require.NotNil(t, state) + assert.Equal(t, []string{agents.NameClaudeCode}, state.SkillsOnlyAgents) +} + +// With several --agents, a removal failure for one agent keeps only that +// agent's --skills-only choice; the agents whose entries were all removed +// lose theirs. +func TestUninstallTargetedAgentsFailureKeepsOnlyFailedAgentChoice(t *testing.T) { + tmp := setupTestHome(t) + clearAgentDirOverrides(t) + ctx := cmdio.MockDiscard(t.Context()) + setupFetchMock(t) + t.Setenv("DATABRICKS_SKILLS_REF", testSkillsRef) + require.NoError(t, os.MkdirAll(filepath.Join(tmp, ".claude"), 0o755)) + require.NoError(t, os.MkdirAll(filepath.Join(tmp, ".cursor"), 0o755)) + require.NoError(t, os.MkdirAll(filepath.Join(tmp, ".copilot"), 0o755)) + + // Copilot is not targeted and keeps the skills, so the state file stays. + claudeAgent := agents.ByName(agents.NameClaudeCode) + cursorAgent := agents.ByName(agents.NameCursor) + copilotAgent := agents.ByName(agents.NameCopilot) + src := &mockManifestSource{manifest: testManifest()} + require.NoError(t, InstallSkillsForAgents(ctx, src, []*agents.Agent{claudeAgent, cursorAgent, copilotAgent}, InstallOptions{})) + + globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") + state, err := LoadState(globalDir) + require.NoError(t, err) + state.SkillsOnlyAgents = []string{agents.NameClaudeCode, agents.NameCodex, agents.NameCursor} + require.NoError(t, SaveState(globalDir, state)) + + cursorSQL := filepath.Join(tmp, ".cursor", "skills", "databricks-sql") + _, err = os.Lstat(cursorSQL) + require.NoError(t, err) + orig := removeAllFn + t.Cleanup(func() { removeAllFn = orig }) + removeAllFn = func(path string) error { + if path == cursorSQL { + return fs.ErrPermission + } + return orig(path) + } + + result, err := UninstallSkillsWithResult(ctx, UninstallOptions{Agents: []string{agents.NameClaudeCode, agents.NameCursor}}) + require.NoError(t, err) + assert.NotEmpty(t, result.SkillFailures) + _, err = os.Lstat(filepath.Join(tmp, ".claude", "skills", "databricks-sql")) + assert.ErrorIs(t, err, fs.ErrNotExist) + state, err = LoadState(globalDir) + require.NoError(t, err) + require.NotNil(t, state) + assert.Equal(t, []string{agents.NameCodex, agents.NameCursor}, state.SkillsOnlyAgents) +} + +// A plain uninstall removes every skill, so it ends every --skills-only +// choice, also when the state file stays for a plugin it could not remove. +func TestUninstallAllForgetsSkillsOnlyChoice(t *testing.T) { + tmp := setupTestHome(t) + globalDir := installTestSkills(t, tmp) + ctx := cmdio.MockDiscard(t.Context()) + + state, err := LoadState(globalDir) + require.NoError(t, err) + state.Plugins = map[string]PluginRecord{"unknown-agent": {Marketplace: "databricks-agent-skills", Plugin: "databricks"}} + state.SkillsOnlyAgents = []string{agents.NameClaudeCode, agents.NameCodex} + require.NoError(t, SaveState(globalDir, state)) + + _, err = UninstallSkillsWithResult(ctx, UninstallOptions{}) + require.NoError(t, err) + state, err = LoadState(globalDir) + require.NoError(t, err) + require.NotNil(t, state) + assert.Contains(t, state.Plugins, "unknown-agent") + assert.Empty(t, state.Skills) + assert.Empty(t, state.SkillsOnlyAgents) +} + func TestUninstallTargetedAgentRemovesCanonicalWhenNoExposureRemains(t *testing.T) { tmp := setupTestHome(t) ctx := cmdio.MockDiscard(t.Context())