diff --git a/.nextchanges/notable-changes/aitools-install-nothing-installed-fails.md b/.nextchanges/notable-changes/aitools-install-nothing-installed-fails.md new file mode 100644 index 00000000000..3f2f029647f --- /dev/null +++ b/.nextchanges/notable-changes/aitools-install-nothing-installed-fails.md @@ -0,0 +1 @@ +* `databricks aitools install`, `update` and `uninstall` now exit with code 1 when they do nothing they were asked to. `install` fails when it sets up nothing: no coding agent is detected, every agent is skipped or fails to install, or no skill in the release can be installed (also with `--path`). `update` fails when every update or check it attempts fails, and `uninstall` when every removal it attempts fails. Scripts that ran these commands as a best-effort step and relied on exit code 0 should handle the new exit code. ([#7481](https://github.com/databricks/cli/pull/7481)) diff --git a/acceptance/cmd/aitools/telemetry/install-failure/output.txt b/acceptance/cmd/aitools/telemetry/install-failure/output.txt index 70aa956837d..ac884df53ba 100644 --- a/acceptance/cmd/aitools/telemetry/install-failure/output.txt +++ b/acceptance/cmd/aitools/telemetry/install-failure/output.txt @@ -68,3 +68,19 @@ Error: Cursor: does not support project-scoped skills {"command":"aitools_install","exit_code":1,"aitools_command_event":{"command":"INSTALL","stage":"STEP_COMPLETED","step":"PLAN_CONFIRMED","interactive":false,"install_mode":"SKILLS_ONLY","scope":"PROJECT"}} {"command":"aitools_install","exit_code":1,"aitools_command_event":{"command":"INSTALL","stage":"FINISHED","outcome":"FAILURE","error_category":"UNSUPPORTED_SCOPE","interactive":false,"install_mode":"SKILLS_ONLY","agents":["CURSOR"],"scope":"PROJECT"}} {"command":"aitools_install","exit_code":1,"aitools_install_event":{"agents":["CURSOR"],"scope":"PROJECT","error_category":"TYPE_UNSPECIFIED","agent_results":[{"agent":"CURSOR","error_category":"UNSUPPORTED_SCOPE"}]}} + +=== a detected agent skipped for the scope prints its recovery step and exits 1 with nothing installed + +>>> [CLI] aitools install --skills-only --scope=project +Skipped Cursor: does not support project-scoped skills + To set up Cursor in the global scope, run: databricks aitools install --scope=global --agents=cursor --skills-only +Error: nothing was installed; follow the steps above + +>>> print_telemetry +{"command":"aitools_install","exit_code":1,"aitools_command_event":{"command":"INSTALL","stage":"STARTED","interactive":false,"install_mode":"SKILLS_ONLY"}} +{"command":"aitools_install","exit_code":1,"aitools_command_event":{"command":"INSTALL","stage":"STEP_COMPLETED","step":"FLAGS_VALIDATED","interactive":false,"install_mode":"SKILLS_ONLY"}} +{"command":"aitools_install","exit_code":1,"aitools_command_event":{"command":"INSTALL","stage":"STEP_COMPLETED","step":"SCOPE_RESOLVED","interactive":false,"install_mode":"SKILLS_ONLY","scope":"PROJECT"}} +{"command":"aitools_install","exit_code":1,"aitools_command_event":{"command":"INSTALL","stage":"STEP_COMPLETED","step":"AGENTS_SELECTED","interactive":false,"install_mode":"SKILLS_ONLY","scope":"PROJECT"}} +{"command":"aitools_install","exit_code":1,"aitools_command_event":{"command":"INSTALL","stage":"STEP_COMPLETED","step":"PLAN_CONFIRMED","interactive":false,"install_mode":"SKILLS_ONLY","scope":"PROJECT"}} +{"command":"aitools_install","exit_code":1,"aitools_command_event":{"command":"INSTALL","stage":"FINISHED","outcome":"FAILURE","error_category":"UNSUPPORTED_SCOPE","interactive":false,"install_mode":"SKILLS_ONLY","agents":["CURSOR"],"scope":"PROJECT"}} +{"command":"aitools_install","exit_code":1,"aitools_install_event":{"agents":["CURSOR"],"scope":"PROJECT","error_category":"TYPE_UNSPECIFIED","agent_results":[{"agent":"CURSOR","error_category":"UNSUPPORTED_SCOPE"}]}} diff --git a/acceptance/cmd/aitools/telemetry/install-failure/script b/acceptance/cmd/aitools/telemetry/install-failure/script index 8e36f8eaf0b..6c6ce536606 100644 --- a/acceptance/cmd/aitools/telemetry/install-failure/script +++ b/acceptance/cmd/aitools/telemetry/install-failure/script @@ -1,5 +1,10 @@ # Isolate HOME so parallel aitools tests don't race on a shared ~/.databricks. sethome home +# Agent config overrides from the host would make auto-detection find agents +# outside the isolated HOME; keep every agent's config dir inside it. +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" mkdir -p "${USERPROFILE:-$HOME}/.cursor" # Only runs past the plan confirmation are here: a run that ends earlier (a @@ -22,3 +27,7 @@ trace print_telemetry title "a named agent skipped for the scope takes its category from the agent's outcome" musterr trace $CLI aitools install --skills-only --scope=project --agents=cursor trace print_telemetry + +title "a detected agent skipped for the scope prints its recovery step and exits 1 with nothing installed" +musterr trace $CLI aitools install --skills-only --scope=project +trace print_telemetry diff --git a/cmd/aitools/funnel.go b/cmd/aitools/funnel.go index 8a7dddfe38e..d82c315c5a9 100644 --- a/cmd/aitools/funnel.go +++ b/cmd/aitools/funnel.go @@ -3,6 +3,7 @@ package aitools import ( "context" "errors" + "slices" "github.com/charmbracelet/huh" "github.com/databricks/cli/libs/aitools/agents" @@ -67,15 +68,16 @@ func (f *commandFunnel) step(ctx context.Context, s protos.AitoolsCommandStep) { } // finish logs the FINISHED event for a run that returned err: SUCCESS for nil, -// NOTHING_TO_DO when update or uninstall found nothing installed (the command -// still exits non-zero), CANCELLED when the user aborted a prompt or picked no -// agents, FAILURE otherwise. No-op once the run has finished, so it is safe to -// defer after an explicit finishWith. +// NOTHING_TO_DO when update or uninstall found nothing installed, or an install +// found no skill in the release it can install (the command still exits +// non-zero), CANCELLED when the user aborted a prompt or picked no agents, +// FAILURE otherwise. No-op once the run has finished, so it is safe to defer +// after an explicit finishWith. func (f *commandFunnel) finish(ctx context.Context, err error) { switch { case err == nil: f.finishWith(ctx, protos.AitoolsCommandOutcomeSuccess, protos.AitoolsErrorCategoryUnspecified) - case errors.Is(err, installer.ErrNothingInstalled): + case errors.Is(err, installer.ErrNothingInstalled), errors.Is(err, installer.ErrNoSkillsResolved): f.finishWith(ctx, protos.AitoolsCommandOutcomeNothingToDo, protos.AitoolsErrorCategoryUnspecified) case errors.Is(err, huh.ErrUserAborted): f.finishWith(ctx, protos.AitoolsCommandOutcomeCancelled, protos.AitoolsErrorCategoryUserAborted) @@ -87,8 +89,9 @@ func (f *commandFunnel) finish(ctx context.Context, err error) { } // finishWith logs the FINISHED event with an explicit outcome, for runs whose -// returned error does not tell it: a nil return without success (declined -// confirm, nothing to act on, nothing installed) or a named agent's failure. +// returned error does not tell it: a nil return without success (a declined +// confirm, nothing to act on), a failure already reported per agent, or a run +// with no agent detected, which fails but had nothing to act on. func (f *commandFunnel) finishWith(ctx context.Context, outcome protos.AitoolsCommandOutcome, category protos.AitoolsErrorCategory) { if f.finished { return @@ -175,25 +178,9 @@ func deliverySteps(outcomes []agentOutcome) []protos.AitoolsCommandStep { return steps } -// nothingInstalledCategory reports whether a run that returned no error still -// installed nothing for any agent (e.g. the only detected agent's plugin -// install failed, which is a warning, not an error), and if so the category of -// the first agent that failed or was skipped. The funnel records such a run as -// FAILURE rather than SUCCESS. That includes a non-interactive run whose -// detected agents are all skipped for the scope (UNSUPPORTED_SCOPE): it set -// up nothing it was asked to. An interactive run never gets here for them, -// because the picker offers no skipped agent and the run finishes NOTHING_TO_DO -// before the plan. -func nothingInstalledCategory(outcomes []agentOutcome) (protos.AitoolsErrorCategory, bool) { - if len(outcomes) == 0 { - return "", false - } - for _, o := range outcomes { - if o.status == outcomeInstalled { - return "", false - } - } - return firstFailureCategory(outcomes), true +// anyInstalled reports whether at least one agent was set up. +func anyInstalled(outcomes []agentOutcome) bool { + return slices.ContainsFunc(outcomes, func(o agentOutcome) bool { return o.status == outcomeInstalled }) } // firstFailureCategory returns the category of the first agent that failed or diff --git a/cmd/aitools/funnel_test.go b/cmd/aitools/funnel_test.go index b4a09f1aab2..fe4be9dd4fc 100644 --- a/cmd/aitools/funnel_test.go +++ b/cmd/aitools/funnel_test.go @@ -9,6 +9,7 @@ import ( "maps" "net/http" "net/http/httptest" + "os" "path/filepath" "strings" "testing" @@ -187,7 +188,7 @@ func TestInstallFunnelNoAgentsDetected(t *testing.T) { forbidSkillsInstall(t) ctx, err := runInstall(t, cmdio.MockDiscard(t.Context())) - require.NoError(t, err) + require.ErrorIs(t, err, errNoAgentSetUp) assert.Equal(t, []string{ "STARTED", "FLAGS_VALIDATED", "SCOPE_RESOLVED", "FINISHED:NOTHING_TO_DO:", }, funnelShape(funnelEvents(ctx))) @@ -458,8 +459,10 @@ func TestInstallFunnelPathDumpWithNoSkillToWrite(t *testing.T) { `{"version": "2", "skills": {"databricks-preview": {"version": "1.0.0", "files": ["SKILL.md"], "repo_dir": "experimental"}}}`, nil)) + // Nothing was written, so the run exits 1, but the funnel records it as + // NOTHING_TO_DO: no skill in the release could be written. ctx, err := runInstall(t, cmdio.MockDiscard(t.Context()), "--path", t.TempDir()) - require.NoError(t, err) + require.ErrorIs(t, err, installer.ErrNoSkillsResolved) assert.Equal(t, []string{"STARTED", "FLAGS_VALIDATED", "FINISHED:NOTHING_TO_DO:"}, funnelShape(funnelEvents(ctx))) } @@ -567,8 +570,8 @@ func TestListFunnelFailure(t *testing.T) { } } -// A plugin failure for an auto-detected agent is only a warning (exit 0), but -// when nothing got installed the funnel must not report SUCCESS. +// A plugin failure for an auto-detected agent is only a warning, but when +// nothing got installed the run fails and the funnel must not report SUCCESS. func TestInstallFunnelDetectedPluginFailureIsNotSuccess(t *testing.T) { isolateAgents(t) tmp := t.TempDir() @@ -583,28 +586,27 @@ func TestInstallFunnelDetectedPluginFailureIsNotSuccess(t *testing.T) { return installer.PluginRecord{}, &installer.BlockedError{Agent: a.Name, Reason: installer.ReasonInstallFailed} } + // The run installed nothing, so it exits non-zero; the FINISHED event takes + // the category from the agent's outcome. ctx, err := runInstall(t, cmdio.MockDiscard(t.Context()), "--scope", "global") - require.NoError(t, err) + require.ErrorIs(t, err, errNoAgentSetUp) assert.Equal(t, []string{ "STARTED", "FLAGS_VALIDATED", "SCOPE_RESOLVED", "AGENTS_SELECTED", "PLAN_CONFIRMED", "FINISHED:FAILURE:PLUGIN_INSTALL_FAILED", }, funnelShape(funnelEvents(ctx))) } -func TestNothingInstalledCategory(t *testing.T) { +func TestAnyInstalledAndFirstFailureCategory(t *testing.T) { codex := &agents.Agent{Name: agents.NameCodex} cursor := &agents.Agent{Name: agents.NameCursor} failed := agentOutcome{agent: codex, delivery: deliveryPlugin, status: outcomeFailed, errorCategory: protos.AitoolsErrorCategoryPluginInstallFailed} skipped := agentOutcome{agent: cursor, delivery: deliverySkip, status: outcomeSkipped, errorCategory: protos.AitoolsErrorCategoryUnsupportedScope} installed := agentOutcome{agent: cursor, delivery: deliverySkills, status: outcomeInstalled} - _, ok := nothingInstalledCategory(nil) - assert.False(t, ok) - _, ok = nothingInstalledCategory([]agentOutcome{failed, installed}) - assert.False(t, ok, "one installed agent makes the run a success") - got, ok := nothingInstalledCategory([]agentOutcome{failed, skipped}) - assert.True(t, ok) - assert.Equal(t, protos.AitoolsErrorCategoryPluginInstallFailed, got) + assert.False(t, anyInstalled(nil)) + assert.True(t, anyInstalled([]agentOutcome{failed, installed}), "one installed agent makes the run a success") + assert.False(t, anyInstalled([]agentOutcome{failed, skipped})) + assert.Equal(t, protos.AitoolsErrorCategoryPluginInstallFailed, firstFailureCategory([]agentOutcome{failed, skipped})) uncategorized := agentOutcome{agent: codex, delivery: deliveryPlugin, status: outcomeFailed} assert.Equal(t, protos.AitoolsErrorCategoryUnsupportedScope, firstFailureCategory([]agentOutcome{installed, uncategorized, skipped})) @@ -785,8 +787,8 @@ func saveGlobalState(t *testing.T, skills map[string]string) string { return dir } -// Plugin failures are warnings (exit 0), but an update in which no plugin got -// updated and nothing else ran did nothing and must not report SUCCESS. +// Plugin failures are warnings, but an update in which no plugin got updated +// and nothing else ran did nothing: it exits 1 and reports FAILURE. func TestUpdateFunnelWhenPluginsFail(t *testing.T) { codexFailed := installer.PluginUpdate{Agent: "Codex CLI", Err: errCodexNotOnPath} cases := []struct { @@ -799,7 +801,12 @@ func TestUpdateFunnelWhenPluginsFail(t *testing.T) { refErr error checkErr error want string + // exit0 marks a FAILURE that still exits 0. + exit0 bool }{ + {name: "only an unknown agent's record", results: []installer.PluginUpdate{{Agent: "x", Err: fmt.Errorf("%w %q in state", installer.ErrUnknownAgent, "x")}}, want: "FINISHED:FAILURE:UNKNOWN_AGENT", exit0: true}, + // The unknown agent sorts first, but the failed attempt decides the run. + {name: "unknown agent's record and a failed plugin", results: []installer.PluginUpdate{{Agent: "aider", Err: fmt.Errorf("%w %q in state", installer.ErrUnknownAgent, "aider")}, codexFailed}, want: "FINISHED:FAILURE:CLI_NOT_ON_PATH"}, {name: "all plugins failed", results: []installer.PluginUpdate{codexFailed}, want: "FINISHED:FAILURE:CLI_NOT_ON_PATH"}, {name: "one of two plugins failed", results: []installer.PluginUpdate{{Agent: "Claude Code", Version: "0.2.7"}, codexFailed}, want: "FINISHED:SUCCESS:"}, {name: "plugin failed, file skills reconciled", skills: map[string]string{"databricks": "0.2.6"}, results: []installer.PluginUpdate{codexFailed}, want: "FINISHED:SUCCESS:"}, @@ -851,12 +858,25 @@ func TestUpdateFunnelWhenPluginsFail(t *testing.T) { ctx := telemetry.WithNewLogger(cmdio.MockDiscard(t.Context())) cmd := NewUpdateCmd() cmd.SetContext(ctx) + cmd.SilenceErrors = true + cmd.SilenceUsage = true args := []string{"--scope=global"} if tc.check { args = append(args, "--check") } cmd.SetArgs(args) - require.NoError(t, cmd.Execute()) + err := cmd.Execute() + // A run where every attempt failed exits 1. + if strings.HasPrefix(tc.want, "FINISHED:FAILURE:") && !tc.exit0 { + require.Error(t, err) + prefix := "nothing was updated: " + if tc.check { + prefix = "nothing was checked: " + } + assert.True(t, strings.HasPrefix(err.Error(), prefix), err.Error()) + } else { + require.NoError(t, err) + } events := funnelEvents(ctx) assert.Equal(t, []string{"STARTED", tc.want}, funnelShape(events)) assert.Equal(t, protos.AitoolsInstallScopeGlobal, events[len(events)-1].Scope) @@ -867,8 +887,8 @@ func TestUpdateFunnelWhenPluginsFail(t *testing.T) { } } -// Plugin and skill removal failures are warnings (exit 0), but an uninstall -// that removed nothing because every removal failed must not report SUCCESS. +// Plugin and skill removal failures are warnings, but an uninstall that removed +// nothing because every removal failed exits 1 and reports FAILURE. func TestUninstallFunnelWhenRemovalsFail(t *testing.T) { skillFailed := fmt.Errorf("remove skill: %w", fs.ErrPermission) cases := []struct { @@ -877,12 +897,16 @@ func TestUninstallFunnelWhenRemovalsFail(t *testing.T) { result installer.UninstallResult want string wantAgents []protos.AitoolsAgentType + // exit0 marks a FAILURE that still exits 0. + exit0 bool }{ + {name: "only an unknown agent's record", result: installer.UninstallResult{PluginFailures: []error{fmt.Errorf("%w %q in state", installer.ErrUnknownAgent, "x")}}, want: "FINISHED:FAILURE:UNKNOWN_AGENT", exit0: true}, {name: "all plugins failed", result: installer.UninstallResult{PluginFailures: []error{errCodexNotOnPath}}, want: "FINISHED:FAILURE:CLI_NOT_ON_PATH"}, {name: "nothing to remove for the targeted agent", args: []string{"--agents=cursor"}, want: "FINISHED:NOTHING_TO_DO:", wantAgents: []protos.AitoolsAgentType{protos.AitoolsAgentTypeCursor}}, - {name: "skill removed for the targeted agent", args: []string{"--agents=cursor"}, result: installer.UninstallResult{SkillsRemoved: 1}, want: "FINISHED:SUCCESS:", wantAgents: []protos.AitoolsAgentType{protos.AitoolsAgentTypeCursor}}, + {name: "skill removed for the targeted agent", args: []string{"--agents=cursor"}, result: installer.UninstallResult{SkillsChanged: 1}, want: "FINISHED:SUCCESS:", wantAgents: []protos.AitoolsAgentType{protos.AitoolsAgentTypeCursor}}, {name: "one of two plugins failed", result: installer.UninstallResult{PluginsRemoved: 1, PluginFailures: []error{errCodexNotOnPath}}, want: "FINISHED:SUCCESS:"}, - {name: "plugin failed, skills removed", result: installer.UninstallResult{SkillsRemoved: 1, PluginFailures: []error{errCodexNotOnPath}}, want: "FINISHED:SUCCESS:"}, + {name: "plugin failed, skills removed", result: installer.UninstallResult{SkillsChanged: 1, PluginFailures: []error{errCodexNotOnPath}}, want: "FINISHED:SUCCESS:"}, + {name: "a targeted agent kept its entry, another's copy removed", args: []string{"--agents=claude-code,cursor"}, result: installer.UninstallResult{SkillsChanged: 1, SkillFailures: []error{skillFailed}}, want: "FINISHED:SUCCESS:", wantAgents: []protos.AitoolsAgentType{protos.AitoolsAgentTypeClaudeCode, protos.AitoolsAgentTypeCursor}}, {name: "all skill removals failed", result: installer.UninstallResult{SkillFailures: []error{skillFailed}}, want: "FINISHED:FAILURE:PERMISSION_DENIED"}, {name: "skill removal failed, plugin removed", result: installer.UninstallResult{PluginsRemoved: 1, SkillFailures: []error{skillFailed}}, want: "FINISHED:SUCCESS:"}, } @@ -899,8 +923,17 @@ func TestUninstallFunnelWhenRemovalsFail(t *testing.T) { ctx := telemetry.WithNewLogger(cmdio.MockDiscard(t.Context())) cmd := NewUninstallCmd() cmd.SetContext(ctx) + cmd.SilenceErrors = true + cmd.SilenceUsage = true cmd.SetArgs(append([]string{"--scope=global"}, tc.args...)) - require.NoError(t, cmd.Execute()) + err := cmd.Execute() + // A run where every attempted removal failed exits 1. + if strings.HasPrefix(tc.want, "FINISHED:FAILURE:") && !tc.exit0 { + require.Error(t, err) + assert.True(t, strings.HasPrefix(err.Error(), "could not remove any skill or plugin: "), err.Error()) + } else { + require.NoError(t, err) + } events := funnelEvents(ctx) assert.Equal(t, []string{"STARTED", tc.want}, funnelShape(events)) finished := events[len(events)-1] @@ -929,3 +962,22 @@ func TestUninstallConfirmPromptFailure(t *testing.T) { assert.Empty(t, *calls) assert.Equal(t, []string{"STARTED", "FINISHED:FAILURE:PROMPT_FAILED"}, funnelShape(funnelEvents(ctx))) } + +func TestInstallFunnelNothingInstalledKeepsTheAgentCategory(t *testing.T) { + // Only Cursor is detected and it has no project-scoped skills, so the run + // sets up nothing. The FINISHED event must name why, not UNCATEGORIZED. + isolateAgents(t) + tmp := t.TempDir() + t.Setenv("HOME", tmp) + t.Setenv("USERPROFILE", tmp) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(tmp, ".config")) + require.NoError(t, os.MkdirAll(filepath.Join(tmp, ".cursor"), 0o755)) + setupInstallMock(t) + + ctx, err := runInstall(t, cmdio.MockDiscard(t.Context()), "--skills-only", "--scope", "project") + require.ErrorIs(t, err, errNoAgentSetUp) + assert.Equal(t, []string{ + "STARTED", "FLAGS_VALIDATED", "SCOPE_RESOLVED", "AGENTS_SELECTED", "PLAN_CONFIRMED", + "FINISHED:FAILURE:UNSUPPORTED_SCOPE", + }, funnelShape(funnelEvents(ctx))) +} diff --git a/cmd/aitools/install.go b/cmd/aitools/install.go index 6e84d59ed54..6a13be5f7b7 100644 --- a/cmd/aitools/install.go +++ b/cmd/aitools/install.go @@ -138,6 +138,10 @@ Output: --output json Emit a structured result instead of text. Requires --scope and --agents so the command runs without interactive prompts. +The command exits with code 1 when it sets up nothing: no agent is detected, +every agent is skipped or fails to install, or no skill in the release can be +installed (also with --path). + Supported agents: ` + strings.Join(agents.SupportedNames(), ", "), Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) (err error) { @@ -182,16 +186,12 @@ Supported agents: ` + strings.Join(agents.SupportedNames(), ", "), // --path is a dumb dump: no agents, no scope, no state. if pathFlag != "" { funnel.step(ctx, protos.AitoolsCommandStepFlagsValidated) - written, err := installer.DumpSkillsToPath(ctx, src, pathFlag, opts) - if err != nil { + // When every skill is filtered out (e.g. all experimental without + // --experimental) this fails with ErrNoSkillsResolved, so a caller + // does not take the empty directory for skills. + if _, err := installer.DumpSkillsToPath(ctx, src, pathFlag, opts); err != nil { return withRecovery(ctx, installInvocation{path: pathFlag, skills: skillsFlag, experimental: includeExperimental}, err) } - if written == 0 { - // Every skill was filtered out (e.g. all experimental without - // --experimental): there was nothing to write. - funnel.finishWith(ctx, protos.AitoolsCommandOutcomeNothingToDo, protos.AitoolsErrorCategoryUnspecified) - return nil - } funnel.step(ctx, protos.AitoolsCommandStepSkillsDumped) return nil } @@ -286,8 +286,10 @@ Supported agents: ` + strings.Join(agents.SupportedNames(), ", "), } if len(targetAgents) == 0 { printNoAgentsMessage(ctx, rerun) + // Nothing to act on, like an empty --path dump, but the run + // still fails, so a caller does not take it for a setup. funnel.finishWith(ctx, protos.AitoolsCommandOutcomeNothingToDo, protos.AitoolsErrorCategoryUnspecified) - return nil + return errNoAgentSetUp } if interactive { rerun.agents = joinAgentNames(targetAgents) @@ -326,12 +328,10 @@ Supported agents: ` + strings.Join(agents.SupportedNames(), ", "), } switch { case runErr == nil: - if category, ok := nothingInstalledCategory(outcomes); ok { - funnel.finishWith(ctx, protos.AitoolsCommandOutcomeFailure, category) - } + // The deferred funnel.finish records SUCCESS. case topLevelFailure(runErr) == nil: - // Named agents failed or were skipped. A skip has no error type of - // its own, so take the category from the outcomes, not runErr. + // Named agents failed, or no agent was set up. A skip has no error + // type of its own, so take the category from the outcomes, not runErr. funnel.finishWith(ctx, protos.AitoolsCommandOutcomeFailure, firstFailureCategory(outcomes)) default: // Finish on the real failure here: JSON mode returns the @@ -586,8 +586,14 @@ const ( outcomeFailed outcomeStatus = "failed" ) -// agentErrors wraps the failures of explicitly named agents, which are already -// reported in their per-agent outcomes. Wrapping lets the JSON and telemetry +// errNoAgentSetUp is returned when no agent was set up: none was detected, or +// every agent in the plan was skipped or failed. The reason and recovery step +// are printed above it, so the message only has to make the exit code non-zero. +var errNoAgentSetUp = errors.New("nothing was installed; follow the steps above") + +// agentErrors wraps failures that are already reported in the per-agent +// outcomes: those of agents named via --agents, and errNoAgentSetUp when no +// detected agent was set up. Wrapping lets the JSON and telemetry // layers tell a per-agent failure apart from a top-level failure that has no // per-agent entry, so each is surfaced exactly once. type agentErrors struct{ err error } @@ -607,16 +613,19 @@ func topLevelFailure(runErr error) error { // executePlan carries out the plan and returns each agent's outcome. Skills // installs go through the existing skills path. Plugin installs are reported but -// never silently fall back to skills: a blocked install is a warning (exit 0), -// unless the agent was explicitly named via --agents, which is an error. The +// never silently fall back to skills: a blocked install is a warning, unless the +// agent was explicitly named via --agents, which is an error. A run that sets up +// no agent at all is an error too, so callers never mistake it for success. The // per-agent recovery steps repeat rerun for the agent at hand. func executePlan(ctx context.Context, src installer.ManifestSource, plan []agentPlanItem, opts installer.InstallOptions, rerun installInvocation, quiet bool) ([]agentOutcome, error) { var skillsAgents []*agents.Agent var pluginItems, skipItems []agentPlanItem + skillsExplicit := false for _, it := range plan { switch it.delivery { case deliverySkills: skillsAgents = append(skillsAgents, it.agent) + skillsExplicit = skillsExplicit || it.explicit case deliveryPlugin: pluginItems = append(pluginItems, it) case deliverySkip: @@ -643,6 +652,7 @@ func executePlan(ctx context.Context, src installer.ManifestSource, plan []agent } pluginCount := 0 + pluginsRecorded := false if len(pluginItems) > 0 { // Plugins install from the agent's marketplace, so the skills ref only // labels the recorded version. Record it as unknown when unresolved. @@ -703,8 +713,14 @@ func executePlan(ctx context.Context, src installer.ManifestSource, plan []agent releaseRef = "" } if err := recordPluginInstallsFn(ctx, opts.Scope, records, releaseRef); err != nil { + // No skill to install is no failure of its own; report only the + // record failure. + if errors.Is(skillsErr, installer.ErrNoSkillsResolved) { + return outcomes, err + } return outcomes, errors.Join(skillsErr, err) } + pluginsRecorded = true } } @@ -735,6 +751,28 @@ func executePlan(ctx context.Context, src installer.ManifestSource, plan []agent cmdio.LogString(ctx, fmt.Sprintf("Installed the plugin for %d %s.", pluginCount, noun)) } + // A release with no skill this CLI can install skips the skills agents when + // the plan had plugin agents, so the plugin outcomes decide the run. A + // --skills-only run with nothing else attempted keeps the error. So does a + // run whose installed plugins were not recorded (skills without install + // state are on disk): update and uninstall could not manage them. + if errors.Is(skillsErr, installer.ErrNoSkillsResolved) && len(pluginItems) > 0 && (pluginCount == 0 || pluginsRecorded) { + recovery := installRecovery(ctx, rerun, skillsErr) + names := make([]string, len(skillsAgents)) + for i, a := range skillsAgents { + names[i] = a.DisplayName + outcomes = append(outcomes, agentOutcome{agent: a, delivery: deliverySkills, status: outcomeSkipped, errorCategory: classifyInstallError(skillsErr), message: skillsErr.Error(), recovery: recovery}) + } + if !quiet { + cmdio.LogString(ctx, cmdio.Yellow(ctx, "Skipped "+strings.Join(names, ", ")+": "+skillsErr.Error())) + cmdio.LogString(ctx, " "+recovery) + } + if skillsExplicit { + explicitErrs = append(explicitErrs, &displayError{msg: strings.Join(names, ", ") + ": " + skillsErr.Error(), err: skillsErr}) + } + skillsErr = nil + } + // The skills error has no per-agent entry, so it stays the top-level error; // explicit per-agent failures are already in their outcomes. if skillsErr != nil { @@ -743,6 +781,9 @@ func executePlan(ctx context.Context, src installer.ManifestSource, plan []agent if len(explicitErrs) > 0 { return outcomes, &agentErrors{err: errors.Join(explicitErrs...)} } + if !anyInstalled(outcomes) { + return outcomes, &agentErrors{err: errNoAgentSetUp} + } return outcomes, nil } @@ -845,7 +886,12 @@ func resolveAgentNames(_ context.Context, names string) ([]*agents.Agent, error) // printNoAgentsMessage prints the "no agents detected" message. Its commands // repeat rerun, so a named agent is set up in the scope and mode this run used. func printNoAgentsMessage(ctx context.Context, rerun installInvocation) { - cmdio.LogString(ctx, cmdio.Yellow(ctx, "No supported coding agents found on PATH.")) + // --skills-only detects an agent by its config dir only, not by its CLI. + found := "No supported coding agents found on PATH." + if rerun.skillsOnly { + found = "No supported coding agent config directories found." + } + cmdio.LogString(ctx, cmdio.Yellow(ctx, found)) cmdio.LogString(ctx, "") cmdio.LogString(ctx, "Supported: "+strings.Join(agents.SupportedNames(), ", ")+".") named, _ := commandText(runtime.GOOS, rerun, nil, "--agents=") diff --git a/cmd/aitools/install_test.go b/cmd/aitools/install_test.go index 6ef4bc51a4e..2b9ad9ea173 100644 --- a/cmd/aitools/install_test.go +++ b/cmd/aitools/install_test.go @@ -7,6 +7,7 @@ import ( "encoding/json" "errors" "io" + "io/fs" "net/http" "net/http/httptest" "os" @@ -305,26 +306,60 @@ func TestExecutePlanSkipBlockedPluginExit0(t *testing.T) { origRec := recordPluginInstallsFn t.Cleanup(func() { installPluginForAgentFn = orig; recordPluginInstallsFn = origRec }) installPluginForAgentFn = func(_ context.Context, a *agents.Agent, _, _ string) (installer.PluginRecord, error) { - return installer.PluginRecord{}, &installer.BlockedError{Agent: a.Name, Reason: installer.ReasonCLINotOnPath} + if a.Name == agents.NameClaudeCode { + return installer.PluginRecord{}, &installer.BlockedError{Agent: a.Name, Reason: installer.ReasonCLINotOnPath} + } + return installer.PluginRecord{Version: "0.2.6"}, nil } recordPluginInstallsFn = func(context.Context, string, map[string]installer.PluginRecord, string) error { return nil } + origCleanup := cleanupLegacyFn + t.Cleanup(func() { cleanupLegacyFn = origCleanup }) + cleanupLegacyFn = func(context.Context, *agents.Agent, string) error { return nil } claude := testPluginAgent(agents.NameClaudeCode, "Claude Code", "claude") + codex := testPluginAgent(agents.NameCodex, "Codex CLI", "codex") ctx := cmdio.MockDiscard(t.Context()) - // Non-explicit blocked install is a warning, not an error, but the agent's - // failure is still recorded in its outcome. - plan := buildPlan([]*agents.Agent{claude}, installer.ScopeGlobal, false, false) + // Non-explicit blocked install is a warning, not an error, as long as another + // agent was set up; the agent's failure is still recorded in its outcome. + plan := buildPlan([]*agents.Agent{claude, codex}, installer.ScopeGlobal, false, false) outcomes, err := executePlan(ctx, nil, plan, installer.InstallOptions{Scope: installer.ScopeGlobal}, installInvocation{scope: installer.ScopeGlobal}, false) require.NoError(t, err) - require.Len(t, outcomes, 1) + require.Len(t, outcomes, 2) assert.Equal(t, outcomeFailed, outcomes[0].status) assert.Equal(t, protos.AitoolsErrorCategoryCLINotOnPath, outcomes[0].errorCategory) + assert.Equal(t, outcomeInstalled, outcomes[1].status) - // Explicit (--agents) blocked install is an error. - planExplicit := buildPlan([]*agents.Agent{claude}, installer.ScopeGlobal, false, true) + // Explicit (--agents) blocked install is an error, even when another named + // agent was set up. + planExplicit := buildPlan([]*agents.Agent{claude, codex}, installer.ScopeGlobal, false, true) _, err = executePlan(ctx, nil, planExplicit, installer.InstallOptions{Scope: installer.ScopeGlobal}, installInvocation{scope: installer.ScopeGlobal}, false) require.Error(t, err) + assert.NotErrorIs(t, err, errNoAgentSetUp) +} + +func TestExecutePlanNothingInstalledIsAnError(t *testing.T) { + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") + 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, Detail: "fatal: could not read from remote repository"} + } + + // Nothing named via --agents and the only agent's plugin install fails. This + // used to exit 0 with only a warning, so an agent caller believed it worked. + claude := testPluginAgent(agents.NameClaudeCode, "Claude Code", "claude") + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + plan := buildPlan([]*agents.Agent{claude}, installer.ScopeGlobal, false, false) + outcomes, err := executePlan(ctx, nil, plan, installer.InstallOptions{Scope: installer.ScopeGlobal}, installInvocation{scope: installer.ScopeGlobal}, false) + + require.ErrorIs(t, err, errNoAgentSetUp) + // The failure is reported per agent, so it must not also be a top-level one. + assert.NoError(t, topLevelFailure(err)) + require.Len(t, outcomes, 1) + assert.Equal(t, "Run 'claude plugin marketplace add databricks/databricks-agent-skills', then 'claude plugin marketplace update databricks-agent-skills', then 'claude plugin install databricks@databricks-agent-skills --scope user' to see the full error. Once they succeed, re-run 'databricks aitools install --scope=global --agents=claude-code' so the CLI records the plugin. Or install skill files instead: databricks aitools install --scope=global --agents=claude-code --skills-only", outcomes[0].recovery) + assert.Contains(t, stderr.String(), "Skipped Claude Code: 'claude plugin' failed: fatal: could not read from remote repository") + assert.Contains(t, stderr.String(), " Run 'claude plugin marketplace add") } func TestExecutePlanPluginWithUnresolvedSkillsRelease(t *testing.T) { @@ -373,6 +408,89 @@ func TestExecutePlanPluginWithUnresolvedSkillsRelease(t *testing.T) { assert.Empty(t, recordRef) } +func TestExecutePlanNoSkillsToInstall(t *testing.T) { + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") + plugins := setupPluginMock(t) + origSkills := installSkillsForAgentsFn + origCleanup := cleanupLegacyFn + t.Cleanup(func() { installSkillsForAgentsFn = origSkills; cleanupLegacyFn = origCleanup }) + cleanupLegacyFn = func(context.Context, *agents.Agent, string) error { return nil } + installSkillsForAgentsFn = func(context.Context, installer.ManifestSource, []*agents.Agent, installer.InstallOptions) error { + return installer.ErrNoSkillsResolved + } + claude := testPluginAgent(agents.NameClaudeCode, "Claude Code", "claude") + cursor := &agents.Agent{Name: agents.NameCursor, DisplayName: "Cursor"} + inv := installInvocation{scope: installer.ScopeGlobal} + + // Only a skills agent: the run sets up nothing and fails, like an empty + // --path dump. + plan := buildPlan([]*agents.Agent{cursor}, installer.ScopeGlobal, true, false) + _, err := executePlan(cmdio.MockDiscard(t.Context()), nil, plan, installer.InstallOptions{Scope: installer.ScopeGlobal}, inv, false) + require.ErrorIs(t, err, installer.ErrNoSkillsResolved) + assert.Error(t, topLevelFailure(err)) + + // A plugin was installed too: the run succeeds and Cursor is skipped with + // the step. + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + plan = buildPlan([]*agents.Agent{cursor, claude}, installer.ScopeGlobal, false, false) + outcomes, err := executePlan(ctx, nil, plan, installer.InstallOptions{Scope: installer.ScopeGlobal}, inv, false) + require.NoError(t, err) + require.Len(t, *plugins, 1) + require.Len(t, outcomes, 2) + assert.Equal(t, outcomeInstalled, outcomes[0].status) + assert.Equal(t, agents.NameCursor, outcomes[1].agent.Name) + assert.Equal(t, outcomeSkipped, outcomes[1].status) + // Same category the run's error gets when no plugin was installed. + assert.Equal(t, classifyInstallError(installer.ErrNoSkillsResolved), outcomes[1].errorCategory) + assert.NotEmpty(t, outcomes[1].errorCategory) + assert.Contains(t, stderr.String(), "Skipped Cursor: no skills to install") + assert.Contains(t, stderr.String(), " No skill in this release can be installed by this CLI.") + + // Named via --agents, the skipped skills agent is an error even so. + plan = buildPlan([]*agents.Agent{cursor, claude}, installer.ScopeGlobal, false, true) + _, err = executePlan(cmdio.MockDiscard(t.Context()), nil, plan, installer.InstallOptions{Scope: installer.ScopeGlobal}, inv, false) + require.ErrorIs(t, err, installer.ErrNoSkillsResolved) + assert.NoError(t, topLevelFailure(err)) +} + +func TestExecutePlanNoSkillsToInstallWithPluginFailures(t *testing.T) { + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") + setupPluginMock(t) + origSkills := installSkillsForAgentsFn + t.Cleanup(func() { installSkillsForAgentsFn = origSkills }) + installSkillsForAgentsFn = func(context.Context, installer.ManifestSource, []*agents.Agent, installer.InstallOptions) error { + return installer.ErrNoSkillsResolved + } + claude := testPluginAgent(agents.NameClaudeCode, "Claude Code", "claude") + cursor := &agents.Agent{Name: agents.NameCursor, DisplayName: "Cursor"} + inv := installInvocation{scope: installer.ScopeGlobal} + plan := buildPlan([]*agents.Agent{cursor, claude}, installer.ScopeGlobal, false, false) + + // The plugin install failed: the run failed to set anything up, which is a + // FAILURE with the plugin's category, not "nothing to do". + installPluginForAgentFn = func(_ context.Context, a *agents.Agent, _, _ string) (installer.PluginRecord, error) { + return installer.PluginRecord{}, &installer.BlockedError{Agent: a.Name, Reason: installer.ReasonInstallFailed} + } + outcomes, err := executePlan(cmdio.MockDiscard(t.Context()), nil, plan, installer.InstallOptions{Scope: installer.ScopeGlobal}, inv, false) + require.ErrorIs(t, err, errNoAgentSetUp) + assert.NotErrorIs(t, err, installer.ErrNoSkillsResolved) + assert.Equal(t, protos.AitoolsErrorCategoryPluginInstallFailed, firstFailureCategory(outcomes)) + + // The plugin installed but its record could not be saved: the record + // failure is the error, without the no-skills sentinel. + installPluginForAgentFn = func(_ context.Context, _ *agents.Agent, scope, _ string) (installer.PluginRecord, error) { + return installer.PluginRecord{Plugin: "databricks", Scope: scope, Version: "0.2.6"}, nil + } + origCleanup := cleanupLegacyFn + t.Cleanup(func() { cleanupLegacyFn = origCleanup }) + cleanupLegacyFn = func(context.Context, *agents.Agent, string) error { return nil } + stateErr := &installer.StateError{Path: ".state.json", Err: fs.ErrPermission} + recordPluginInstallsFn = func(context.Context, string, map[string]installer.PluginRecord, string) error { return stateErr } + _, err = executePlan(cmdio.MockDiscard(t.Context()), nil, plan, installer.InstallOptions{Scope: installer.ScopeGlobal}, inv, false) + require.ErrorIs(t, err, stateErr) + assert.NotErrorIs(t, err, installer.ErrNoSkillsResolved) +} + func TestExecutePlanSkillsFailureStillInstallsPlugins(t *testing.T) { setupTestAgents(t) // temp HOME for the real state file below t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") @@ -807,7 +925,11 @@ func TestInstallNoAgentsDetected(t *testing.T) { cmd := newTestInstallCmd() cmd.SetContext(ctx) - require.NoError(t, cmd.Execute()) + // Nothing was set up, so the run fails; the message above it says what to do, + // so no generic recovery step is appended. + err := cmd.Execute() + require.ErrorIs(t, err, errNoAgentSetUp) + assert.Equal(t, "nothing was installed; follow the steps above", err.Error()) assert.Empty(t, *plugins) assert.Empty(t, *skills) } @@ -1278,3 +1400,16 @@ func TestNoAgentsMessageKeepsTheRunsScopeAndMode(t *testing.T) { printNoAgentsMessage(ctx, installInvocation{scope: installer.ScopeProject, skillsOnly: true}) assert.Contains(t, stderr.String(), "Install one, then re-run 'databricks aitools install --scope=project --skills-only'. To set up an agent that was not detected, name it: databricks aitools install --scope=project --skills-only --agents=") } + +// --skills-only detects agents by their config dir, so the message must not +// blame PATH. +func TestNoAgentsMessageMatchesTheDetection(t *testing.T) { + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + printNoAgentsMessage(ctx, installInvocation{scope: installer.ScopeGlobal, skillsOnly: true}) + assert.Contains(t, stderr.String(), "No supported coding agent config directories found.") + assert.NotContains(t, stderr.String(), "on PATH") + + ctx, stderr = cmdio.NewTestContextWithStderr(t.Context()) + printNoAgentsMessage(ctx, installInvocation{scope: installer.ScopeGlobal}) + assert.Contains(t, stderr.String(), "No supported coding agents found on PATH.") +} diff --git a/cmd/aitools/legacy_skills.go b/cmd/aitools/legacy_skills.go index 67ed8854dea..43ba95dc274 100644 --- a/cmd/aitools/legacy_skills.go +++ b/cmd/aitools/legacy_skills.go @@ -44,6 +44,10 @@ func newLegacySkillsInstallCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { installCmd := NewInstallCmd() installCmd.SetContext(cmd.Context()) + // Root prints the returned error; the detached command must not print + // it, or its usage, a second time. + installCmd.SilenceErrors = true + installCmd.SilenceUsage = true // This legacy alias predates the plugin-first default, so it stays on // raw skill files to preserve its behavior. diff --git a/cmd/aitools/legacy_skills_test.go b/cmd/aitools/legacy_skills_test.go index e388854559f..7f1a75b83fb 100644 --- a/cmd/aitools/legacy_skills_test.go +++ b/cmd/aitools/legacy_skills_test.go @@ -1,6 +1,9 @@ package aitools import ( + "io" + "os" + "path/filepath" "testing" "time" @@ -145,3 +148,33 @@ func TestLegacySkillsInstallKeepsTelemetryAuthConfig(t *testing.T) { require.True(t, cmdctx.HasConfigUsed(cmd.Context())) assert.Equal(t, "https://ws.test", cmdctx.ConfigUsed(cmd.Context()).Host) } + +// The alias runs a detached install command, so cobra must not print the +// error or usage itself: root prints the returned error once. +func TestLegacySkillsInstallPrintsNoErrorOrUsageItself(t *testing.T) { + isolateAgents(t) + tmp := t.TempDir() + t.Setenv("HOME", tmp) + t.Setenv("USERPROFILE", tmp) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(tmp, ".config")) + fakeBinsOnPath(t) // no agent detected + + r, w, err := os.Pipe() + require.NoError(t, err) + origStderr, origStdout := os.Stderr, os.Stdout + os.Stderr, os.Stdout = w, w + t.Cleanup(func() { os.Stderr, os.Stdout = origStderr, origStdout }) + + ctx := telemetry.WithNewLogger(cmdio.MockDiscard(t.Context())) + cmd := newLegacySkillsInstallCmd() + cmd.SetContext(ctx) + runErr := cmd.RunE(cmd, nil) + os.Stderr, os.Stdout = origStderr, origStdout + require.NoError(t, w.Close()) + out, err := io.ReadAll(r) + require.NoError(t, err) + + require.ErrorIs(t, runErr, errNoAgentSetUp) + assert.NotContains(t, string(out), "Error:") + assert.NotContains(t, string(out), "Usage:") +} diff --git a/cmd/aitools/recovery.go b/cmd/aitools/recovery.go index f8002d5761f..b64988c78c0 100644 --- a/cmd/aitools/recovery.go +++ b/cmd/aitools/recovery.go @@ -189,6 +189,13 @@ func installRecovery(ctx context.Context, inv installInvocation, err error) stri // Each agent's failure was printed with its own step. return "" } + if errors.Is(err, installer.ErrNoSkillsResolved) { + step := installer.NoSkillsResolvedMessage + " Re-run " + rerunPhrase(runtime.GOOS, inv, debugFlag) + " to see why each skill was skipped." + if !inv.experimental { + step += " To include experimental skills, add --experimental." + } + return step + } // An empty picker selection has no category of its own: the funnel // reports it as CANCELLED. if errors.Is(err, errNoAgentsSelected) { diff --git a/cmd/aitools/recovery_test.go b/cmd/aitools/recovery_test.go index 2bdd5aa933e..430306abf9c 100644 --- a/cmd/aitools/recovery_test.go +++ b/cmd/aitools/recovery_test.go @@ -160,6 +160,16 @@ func TestInstallRecovery(t *testing.T) { err: &agentErrors{err: errors.New("Cursor: does not support project-scoped skills")}, want: "", }, + { + name: "no agent set up, each agent's step is printed above", + err: &agentErrors{err: errNoAgentSetUp}, + want: "", + }, + { + name: "dump wrote no skill", + err: installer.ErrNoSkillsResolved, + want: "No skill in this release can be installed by this CLI. Re-run 'databricks aitools install --debug' to see why each skill was skipped. To include experimental skills, add --experimental.", + }, { name: "unknown error", err: errors.New("boom"), @@ -173,6 +183,12 @@ func TestInstallRecovery(t *testing.T) { } } +func TestNoSkillsWrittenRecoveryWithExperimental(t *testing.T) { + // --experimental was already given, so the step does not suggest it again. + inv := installInvocation{path: "dump", experimental: true} + assert.Equal(t, "No skill in this release can be installed by this CLI. Re-run 'databricks aitools install --path=dump --experimental --debug' to see why each skill was skipped.", installRecovery(t.Context(), inv, installer.ErrNoSkillsResolved)) +} + func TestWithRecoveryKeepsCauseAndAppendsStep(t *testing.T) { t.Setenv("DATABRICKS_SKILLS_BASE_URL", "") cause := &installer.FetchError{Stage: installer.FetchStageManifest, StatusCode: http.StatusBadGateway, Err: errors.New("failed to fetch manifest: HTTP 502")} diff --git a/cmd/aitools/telemetry_auth_test.go b/cmd/aitools/telemetry_auth_test.go index b6588d74287..82e54f09ce6 100644 --- a/cmd/aitools/telemetry_auth_test.go +++ b/cmd/aitools/telemetry_auth_test.go @@ -400,7 +400,7 @@ func TestUpdateAndUninstallWorkWaitsForTelemetryAuth(t *testing.T) { t.Cleanup(func() { uninstallSkillsFn = orig }) uninstallSkillsFn = func(context.Context, installer.UninstallOptions) (*installer.UninstallResult, error) { time.AfterFunc(200*time.Millisecond, release) - return &installer.UninstallResult{SkillsRemoved: 1}, nil + return &installer.UninstallResult{SkillsChanged: 1}, nil } ctx, _, err := runWithTelemetry(t, NewUninstallCmd(), "--scope=global") diff --git a/cmd/aitools/uninstall.go b/cmd/aitools/uninstall.go index 474c34c9840..787156ff519 100644 --- a/cmd/aitools/uninstall.go +++ b/cmd/aitools/uninstall.go @@ -2,6 +2,7 @@ package aitools import ( "context" + "errors" "fmt" "slices" "strings" @@ -24,7 +25,9 @@ func NewUninstallCmd() *cobra.Command { Short: "Uninstall Databricks skills and plugins", Long: `Remove installed Databricks skills and plugins from all coding agents. -By default, removes all skills. Use --skills to remove specific skills only.`, +By default, removes all skills. Use --skills to remove specific skills only. + +The command exits with code 1 when every removal it attempts fails.`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() @@ -104,16 +107,22 @@ By default, removes all skills. Use --skills to remove specific skills only.`, if err != nil { return err } - // A plugin or skill that could not be removed is only a warning. + // A plugin or skill that could not be removed is only a warning, unless + // no copy of any skill and no plugin was removed at all. A plugin record + // for an agent this CLI does not know is not an attempt: no command can + // act on it. failures := slices.Concat(result.PluginFailures, result.SkillFailures) - if result.PluginsRemoved == 0 && result.SkillsRemoved == 0 { + if result.PluginsRemoved == 0 && result.SkillsChanged == 0 { + if i := slices.IndexFunc(failures, func(err error) bool { return !errors.Is(err, installer.ErrUnknownAgent) }); i >= 0 { + return fmt.Errorf("could not remove any skill or plugin: %w", failures[i]) + } if len(failures) > 0 { - // Every removal failed: FAILURE, though it exits 0. + // Only unknown-agent records: warned about above. funnel.finishWith(ctx, protos.AitoolsCommandOutcomeFailure, classifyInstallError(failures[0])) - } else { - // E.g. --agents names agents that have none of the skills. - funnel.finishWith(ctx, protos.AitoolsCommandOutcomeNothingToDo, protos.AitoolsErrorCategoryUnspecified) + return nil } + // E.g. --agents names agents that have none of the skills. + funnel.finishWith(ctx, protos.AitoolsCommandOutcomeNothingToDo, protos.AitoolsErrorCategoryUnspecified) } return nil }, diff --git a/cmd/aitools/update.go b/cmd/aitools/update.go index 784a7137293..0c23f6d02e5 100644 --- a/cmd/aitools/update.go +++ b/cmd/aitools/update.go @@ -1,7 +1,9 @@ package aitools import ( + "cmp" "context" + "errors" "fmt" "maps" "os" @@ -51,7 +53,9 @@ func NewUpdateCmd() *cobra.Command { By default, updates all installed skills and auto-installs new skills from the manifest. Use --no-new to skip new skills, or --check to -preview what would change without downloading.`, +preview what would change without downloading. + +The command exits with code 1 when every update or check it attempts fails.`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() @@ -91,9 +95,12 @@ preview what would change without downloading.`, ref, _, refErr := updateGetSkillsRefFn(ctx) // A failed or skipped plugin update, or a check whose manifest could - // not be fetched, is only a warning; telemetry still needs to know when - // every attempt failed and nothing else ran. - var firstErr error + // not be fetched, is only a warning, unless every attempt failed and + // nothing else ran. A plugin record for an agent this CLI does not know + // is not an attempt: no command can act on it. + // firstErr is the first failure of any kind; firstAttemptErr the first + // one of an attempt (a record of an unknown agent is not one). + var firstErr, firstAttemptErr error updatedAnything := false for _, scope := range scopes { @@ -131,8 +138,9 @@ preview what would change without downloading.`, // Without a ref the recorded plugins are skipped (with --check, // not checked). - if refErr != nil && state != nil && len(state.Plugins) > 0 && firstErr == nil { - firstErr = refErr + if refErr != nil && state != nil && hasKnownPluginRecord(state) { + firstAttemptErr = cmp.Or(firstAttemptErr, refErr) + firstErr = cmp.Or(firstErr, refErr) } // Update plugin agents through their own CLI (check mode skips this). @@ -143,9 +151,10 @@ preview what would change without downloading.`, } for _, pu := range pluginUpdates { if pu.Err != nil { - if firstErr == nil { - firstErr = pu.Err + if !errors.Is(pu.Err, installer.ErrUnknownAgent) { + firstAttemptErr = cmp.Or(firstAttemptErr, pu.Err) } + firstErr = cmp.Or(firstErr, pu.Err) continue } updatedAnything = true @@ -192,13 +201,13 @@ preview what would change without downloading.`, return err } if result != nil && result.CheckErr != nil { - if firstErr == nil { - firstErr = result.CheckErr - } + firstAttemptErr = cmp.Or(firstAttemptErr, result.CheckErr) + firstErr = cmp.Or(firstErr, result.CheckErr) } else { updatedAnything = true } - if result != nil && (check || len(result.Updated) > 0 || len(result.Added) > 0 || len(result.Removed) > 0) { + // A check that could not fetch the manifest has nothing to report. + if result != nil && result.CheckErr == nil && (check || len(result.Updated) > 0 || len(result.Added) > 0 || len(result.Removed) > 0) { text := installer.FormatUpdateResult(result, check) if text != "No changes." || len(migrationCandidates) == 0 { cmdio.LogString(ctx, text) @@ -207,8 +216,16 @@ preview what would change without downloading.`, } } if firstErr != nil && !updatedAnything { - // Every attempt failed or was skipped: FAILURE, though it exits 0. - funnel.finishWith(ctx, protos.AitoolsCommandOutcomeFailure, classifyInstallError(firstErr)) + if firstAttemptErr == nil { + // Only unknown-agent records: warned about above, but not a run + // that failed to do what it could. + funnel.finishWith(ctx, protos.AitoolsCommandOutcomeFailure, classifyInstallError(firstErr)) + return nil + } + if check { + return fmt.Errorf("nothing was checked: %w", firstAttemptErr) + } + return fmt.Errorf("nothing was updated: %w", firstAttemptErr) } return nil }, @@ -338,3 +355,14 @@ func excludePluginAgents(installed []*agents.Agent, state *installer.InstallStat } return out } + +// hasKnownPluginRecord reports whether state records a plugin for an agent this +// CLI knows, so that skipping the recorded plugins is a failed attempt. +func hasKnownPluginRecord(state *installer.InstallState) bool { + for name := range state.Plugins { + if agents.ByName(name) != nil { + return true + } + } + return false +} diff --git a/cmd/aitools/update_test.go b/cmd/aitools/update_test.go index 3f6dc11e539..86c4aa0182e 100644 --- a/cmd/aitools/update_test.go +++ b/cmd/aitools/update_test.go @@ -463,3 +463,9 @@ func TestUpdateProjectIncludesProjectSkillAgents(t *testing.T) { assert.Contains(t, names, agents.NameGoose) assert.Contains(t, names, agents.NameKiro) } + +func TestHasKnownPluginRecord(t *testing.T) { + assert.False(t, hasKnownPluginRecord(&installer.InstallState{})) + assert.False(t, hasKnownPluginRecord(&installer.InstallState{Plugins: map[string]installer.PluginRecord{"aider": {}}})) + assert.True(t, hasKnownPluginRecord(&installer.InstallState{Plugins: map[string]installer.PluginRecord{"aider": {}, agents.NameCodex: {}}})) +} diff --git a/libs/aitools/installer/dump.go b/libs/aitools/installer/dump.go index 4d7ced91361..74b0f120550 100644 --- a/libs/aitools/installer/dump.go +++ b/libs/aitools/installer/dump.go @@ -11,7 +11,8 @@ import ( ) // DumpSkillsToPath writes resolved skill files into destDir and returns the -// number of skills written. It is a dumb dump: no agents, no .state.json, no +// number of skills written. It returns ErrNoSkillsResolved, writing nothing, +// when every skill is filtered out. It is a dumb dump: no agents, no .state.json, no // lifecycle (update/list/uninstall ignore it). It honors DATABRICKS_SKILLS_REF, // the cli-compat version pin, --skills cherry-picks, and --experimental exactly // as the agent install path does, since it reuses resolveSkills. @@ -31,6 +32,9 @@ func DumpSkillsToPath(ctx context.Context, src ManifestSource, destDir string, o if err != nil { return 0, err } + if len(targetSkills) == 0 { + return 0, ErrNoSkillsResolved + } names := slices.Sorted(maps.Keys(targetSkills)) for _, name := range names { diff --git a/libs/aitools/installer/dump_test.go b/libs/aitools/installer/dump_test.go index c73601d250b..9c837c3d927 100644 --- a/libs/aitools/installer/dump_test.go +++ b/libs/aitools/installer/dump_test.go @@ -66,3 +66,26 @@ func TestDumpSkillsToPathFallsBackWhenLatestUnknown(t *testing.T) { assert.FileExists(t, filepath.Join(dest, name, "SKILL.md")) } } + +// A dump where every skill is filtered out fails before it claims to have +// written anything. +func TestDumpSkillsToPathWithNoSkillToWrite(t *testing.T) { + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + setupFetchMock(t) + t.Setenv("DATABRICKS_SKILLS_REF", testSkillsRef) + src := &mockManifestSource{manifest: &Manifest{ + Version: "1", + UpdatedAt: "2024-01-01", + Skills: map[string]SkillMeta{ + "databricks-preview": {Version: "0.1.0", Files: []string{"SKILL.md"}, RepoDir: experimentalRepoPath}, + }, + }} + + dest := filepath.Join(t.TempDir(), "out") + n, err := DumpSkillsToPath(ctx, src, dest, InstallOptions{}) + require.ErrorIs(t, err, ErrNoSkillsResolved) + assert.Zero(t, n) + assert.NotContains(t, stderr.String(), "Wrote") + _, err = os.Stat(dest) + assert.ErrorIs(t, err, fs.ErrNotExist) +} diff --git a/libs/aitools/installer/errors.go b/libs/aitools/installer/errors.go index da28aa9906e..25e41f94e74 100644 --- a/libs/aitools/installer/errors.go +++ b/libs/aitools/installer/errors.go @@ -11,6 +11,15 @@ import ( // act on" apart from a failure. var ErrNothingInstalled = errors.New("no skills or plugins installed") +// ErrNoSkillsResolved reports that no skill in the release can be installed by +// this run: every one is experimental (without --experimental) or needs a newer +// CLI. A skills install returns it without changing anything on disk. +var ErrNoSkillsResolved = errors.New("no skills to install") + +// NoSkillsResolvedMessage is the line that explains ErrNoSkillsResolved to the +// user. +const NoSkillsResolvedMessage = "No skill in this release can be installed by this CLI." + // ErrUnknownAgent reports a plugin record in the install state for an agent // this CLI does not know (e.g. one written by a newer CLI). Update and // uninstall leave such a record alone and report it as a failed plugin. diff --git a/libs/aitools/installer/installer.go b/libs/aitools/installer/installer.go index fa23a222c80..fa30d4537ec 100644 --- a/libs/aitools/installer/installer.go +++ b/libs/aitools/installer/installer.go @@ -394,6 +394,9 @@ func InstallSkillsForAgents(ctx context.Context, src ManifestSource, targetAgent if err != nil { return err } + if len(targetSkills) == 0 { + return ErrNoSkillsResolved + } params := installParams{ baseDir: baseDir, @@ -576,7 +579,14 @@ func InstallAllSkills(ctx context.Context) error { PrintInstallingFor(ctx, installed) src := &GitHubManifestSource{} - return InstallSkillsForAgents(ctx, src, installed, InstallOptions{}) + err := InstallSkillsForAgents(ctx, src, installed, InstallOptions{}) + // An optional setup step (apps init) has nothing to install; that is no + // failure for it. + if errors.Is(err, ErrNoSkillsResolved) { + cmdio.LogString(ctx, NoSkillsResolvedMessage) + return nil + } + return err } // PrintInstallingFor prints the "Installing..." header with agent names. diff --git a/libs/aitools/installer/installer_test.go b/libs/aitools/installer/installer_test.go index 389a13bec64..959029d23c6 100644 --- a/libs/aitools/installer/installer_test.go +++ b/libs/aitools/installer/installer_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "io/fs" "log/slog" "net/http" @@ -1607,3 +1608,49 @@ func TestFetchErrorsCarryStage(t *testing.T) { require.True(t, ok, "want *FetchError, got %T", err) assert.Equal(t, FetchStageFile, ferr.Stage) } + +// A release whose every skill is filtered out installs nothing and says so, so +// the command can fail instead of reporting a successful install. +func TestInstallSkillsForAgentsWithNoSkillToInstall(t *testing.T) { + tmp := setupTestHome(t) + ctx := cmdio.MockDiscard(t.Context()) + setupFetchMock(t) + t.Setenv("DATABRICKS_SKILLS_REF", "v0.1.0") + src := &mockManifestSource{manifest: &Manifest{ + Version: "1", + UpdatedAt: "2024-01-01", + Skills: map[string]SkillMeta{ + "databricks-preview": {Version: "0.1.0", Files: []string{"SKILL.md"}, RepoDir: experimentalRepoPath}, + }, + }} + + err := InstallSkillsForAgents(ctx, src, []*agents.Agent{testAgent(tmp)}, InstallOptions{}) + require.ErrorIs(t, err, ErrNoSkillsResolved) + globalDir, err := GlobalSkillsDir(ctx) + require.NoError(t, err) + state, err := LoadState(globalDir) + require.NoError(t, err) + assert.Nil(t, state, "nothing was installed, so no state is written") +} + +// apps init installs skills as an optional step, so a release with no skill +// this CLI can install is nothing to do for it, not a failure. +func TestInstallAllSkillsWithNoSkillToInstall(t *testing.T) { + tmp := setupTestHome(t) + clearAgentDirOverrides(t) + require.NoError(t, os.MkdirAll(filepath.Join(tmp, ".claude"), 0o755)) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/test-ref/manifest.json" { + http.NotFound(w, r) + return + } + _, _ = io.WriteString(w, `{"version": "2", "skills": {"databricks-preview": {"version": "1.0.0", "files": ["SKILL.md"], "repo_dir": "experimental"}}}`) + })) + t.Cleanup(srv.Close) + t.Setenv("DATABRICKS_SKILLS_REF", "test-ref") + t.Setenv("DATABRICKS_SKILLS_BASE_URL", srv.URL) + + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + require.NoError(t, InstallAllSkills(ctx)) + assert.Contains(t, stderr.String(), NoSkillsResolvedMessage) +} diff --git a/libs/aitools/installer/plugin_test.go b/libs/aitools/installer/plugin_test.go index 35836220150..8bcfb0ded69 100644 --- a/libs/aitools/installer/plugin_test.go +++ b/libs/aitools/installer/plugin_test.go @@ -675,7 +675,7 @@ func TestPluginOperationsReportFailures(t *testing.T) { result, err := UninstallSkillsWithResult(ctx, UninstallOptions{Scope: ScopeGlobal}) require.NoError(t, err) assert.Equal(t, 0, result.PluginsRemoved) - assert.Equal(t, 0, result.SkillsRemoved) + assert.Equal(t, 0, result.SkillsChanged) require.Len(t, result.PluginFailures, 1) blocked, ok := errors.AsType[*BlockedError](result.PluginFailures[0]) require.True(t, ok, "got %v", result.PluginFailures[0]) diff --git a/libs/aitools/installer/uninstall.go b/libs/aitools/installer/uninstall.go index 6e74b90386f..8f7cf1d2c10 100644 --- a/libs/aitools/installer/uninstall.go +++ b/libs/aitools/installer/uninstall.go @@ -34,11 +34,13 @@ var removeAllFn = os.RemoveAll // nothing from a success. type UninstallResult struct { PluginsRemoved int - // SkillsRemoved counts skills whose canonical dir was removed or, when - // opts.Agents is set, whose entry was removed from one of those agents. With - // opts.Agents set, a skill whose entry could not be removed from one of them - // is not counted. - SkillsRemoved int + // SkillsChanged counts skills of which this run removed at least one copy an + // agent reads: an agent's symlink or entry, or the canonical dir. Without + // opts.Agents, a skill counts when its canonical dir is removed or was + // already gone. With opts.Agents set, removing only the canonical dir does + // not count when a targeted agent's entry could not be removed, since that + // agent keeps the skill. + SkillsChanged int PluginFailures []error // SkillFailures holds the errors from removing a skill's canonical dir or // a targeted agent's entry. @@ -142,23 +144,26 @@ func UninstallSkillsWithResult(ctx context.Context, opts UninstallOptions) (*Uni toRemove = slices.Sorted(maps.Keys(state.Skills)) } - // Remove skill directories and symlinks for each skill. skillsRemoved counts - // the skills dropped from the state on a full uninstall, and with --agents - // those whose canonical dir or a targeted agent's entry was removed. - skillsRemoved := 0 + // Remove skill directories and symlinks for each skill. skillsChanged counts + // the skills of which a copy was removed (see UninstallResult.SkillsChanged); + // skillsUninstalled also counts skills whose state record is dropped + // without a failure though no copy was left to remove, e.g. a stale entry. + skillsChanged, skillsUninstalled := 0, 0 var skillFailures []error for _, name := range toRemove { canonicalDir := filepath.Join(baseDir, name) removeCanonical := true - removed := false - var exposureFailures []error + changed := false + var failures []error if targetedAgents { - removed, exposureFailures = removeSkillExposuresFromAgents(ctx, name, canonicalDir, scope, cwd, state, opts.Agents) - skillFailures = append(skillFailures, exposureFailures...) + changed, failures = removeSkillExposuresFromAgents(ctx, name, canonicalDir, scope, cwd, state, opts.Agents) removeCanonical = !hasRemainingSkillExposure(ctx, name, canonicalDir, scope, cwd, opts.Agents) } else { - removeSymlinksFromAgents(ctx, name, canonicalDir, scope, cwd) + changed = removeSymlinksFromAgents(ctx, name, canonicalDir, scope, cwd) } + // A targeted agent that keeps its entry still has the skill, so removing + // the canonical dir alone is no change for it. + exposureFailed := len(failures) > 0 if removeCanonical { // RemoveAll succeeds on a missing dir. Without --agents the skill's // state record is dropped either way, so it counts; with --agents @@ -166,14 +171,19 @@ func UninstallSkillsWithResult(ctx context.Context, opts UninstallOptions) (*Uni _, statErr := os.Lstat(canonicalDir) if err := removeAllFn(canonicalDir); err != nil { log.Warnf(ctx, "Failed to remove %s: %v", canonicalDir, err) - skillFailures = append(skillFailures, err) - } else if statErr == nil || !targetedAgents { - removed = true + failures = append(failures, err) + } else if (statErr == nil || !targetedAgents) && !exposureFailed { + changed = true } } - // A targeted agent that keeps its entry still has the skill. - if removed && len(exposureFailures) == 0 { - skillsRemoved++ + skillFailures = append(skillFailures, failures...) + if changed { + skillsChanged++ + } + // A skill with no copy left to remove still counts when its state + // record is dropped without a failure (e.g. a stale entry). + if changed || (removeCanonical && len(failures) == 0) { + skillsUninstalled++ } if !removeCanonical { continue @@ -208,14 +218,14 @@ func UninstallSkillsWithResult(ctx context.Context, opts UninstallOptions) (*Uni } cmdio.LogString(ctx, fmt.Sprintf("Uninstalled the plugin from %d %s.", pluginCount, noun)) } - if len(toRemove) > 0 { + if skillsUninstalled > 0 { noun := "skills" - if len(toRemove) == 1 { + if skillsUninstalled == 1 { noun = "skill" } - cmdio.LogString(ctx, fmt.Sprintf("Uninstalled %d %s.", len(toRemove), noun)) + cmdio.LogString(ctx, fmt.Sprintf("Uninstalled %d %s.", skillsUninstalled, noun)) } - return &UninstallResult{PluginsRemoved: pluginCount, SkillsRemoved: skillsRemoved, PluginFailures: pluginFailures, SkillFailures: skillFailures}, nil + return &UninstallResult{PluginsRemoved: pluginCount, SkillsChanged: skillsChanged, PluginFailures: pluginFailures, SkillFailures: skillFailures}, nil } func stringSet(items []string) map[string]bool { @@ -333,7 +343,9 @@ func untrackedSkillAgents(ctx context.Context, skillName string, agentNames []st // removeSymlinksFromAgents removes a skill's symlink from all agent directories // in the registry, but only if the entry is a symlink pointing into canonicalDir. // Non-symlink directories are left untouched to avoid deleting user-managed content. -func removeSymlinksFromAgents(ctx context.Context, skillName, canonicalDir, scope, cwd string) { +// It reports whether it removed any symlink. +func removeSymlinksFromAgents(ctx context.Context, skillName, canonicalDir, scope, cwd string) bool { + removed := false for _, agent := range agents.Registry { if scope == ScopeProject && !agent.SupportsProjectScope { continue @@ -382,9 +394,11 @@ func removeSymlinksFromAgents(ctx context.Context, skillName, canonicalDir, scop if err := os.Remove(destDir); err != nil { log.Warnf(ctx, "Failed to remove symlink %s: %v", destDir, err) } else { + removed = true log.Debugf(ctx, "Removed %q from %s", skillName, agent.DisplayName) } } + return removed } // cleanOrphanedSymlinks scans all agent skill directories for symlinks pointing diff --git a/libs/aitools/installer/uninstall_test.go b/libs/aitools/installer/uninstall_test.go index 97f13b09f85..b2f70d6501a 100644 --- a/libs/aitools/installer/uninstall_test.go +++ b/libs/aitools/installer/uninstall_test.go @@ -424,11 +424,14 @@ func TestUninstallCountsRemovedSkills(t *testing.T) { claudeLink := filepath.Join(tmp, ".claude", "skills", "databricks-sql") cursorLink := filepath.Join(tmp, ".cursor", "skills", "databricks-sql") - // Only Claude Code has the skill: there is nothing to remove for Cursor. + // Only Claude Code has the skill: there is nothing to remove for Cursor, + // so the run does not claim it uninstalled anything. installSQLSkillForAgent(t, ctx, agents.NameClaudeCode) - result, err := UninstallSkillsWithResult(ctx, UninstallOptions{Agents: []string{agents.NameCursor}}) + outCtx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + result, err := UninstallSkillsWithResult(outCtx, UninstallOptions{Agents: []string{agents.NameCursor}}) require.NoError(t, err) - assert.Equal(t, 0, result.SkillsRemoved) + assert.Equal(t, 0, result.SkillsChanged) + assert.NotContains(t, stderr.String(), "Uninstalled") _, err = os.Lstat(claudeLink) assert.NoError(t, err) @@ -445,17 +448,26 @@ func TestUninstallCountsRemovedSkills(t *testing.T) { } result, err = UninstallSkillsWithResult(ctx, UninstallOptions{Agents: []string{agents.NameCursor}}) require.NoError(t, err) - assert.Equal(t, 0, result.SkillsRemoved) + assert.Equal(t, 0, result.SkillsChanged) require.Len(t, result.SkillFailures, 1) assert.ErrorIs(t, result.SkillFailures[0], fs.ErrPermission) _, err = os.Lstat(claudeLink) assert.NoError(t, err) + + // Both targeted, but Cursor's entry cannot be removed: Claude Code's copy + // is gone, so the run changed the skill. + result, err = UninstallSkillsWithResult(ctx, UninstallOptions{Agents: []string{agents.NameClaudeCode, agents.NameCursor}}) + require.NoError(t, err) + assert.Equal(t, 1, result.SkillsChanged) + _, err = os.Lstat(claudeLink) + assert.ErrorIs(t, err, fs.ErrNotExist) removeAllFn = orig + installSQLSkillForAgent(t, ctx, agents.NameClaudeCode) // Both have it: removed for Cursor, kept for Claude Code. result, err = UninstallSkillsWithResult(ctx, UninstallOptions{Agents: []string{agents.NameCursor}}) require.NoError(t, err) - assert.Equal(t, 1, result.SkillsRemoved) + assert.Equal(t, 1, result.SkillsChanged) assert.Empty(t, result.SkillFailures) _, err = os.Lstat(cursorLink) assert.ErrorIs(t, err, fs.ErrNotExist) @@ -463,11 +475,11 @@ func TestUninstallCountsRemovedSkills(t *testing.T) { assert.NoError(t, err) // Full uninstall whose canonical dir removal fails: nothing counts as - // removed and the failure is reported. + // changed and the failure is reported. removeAllFn = func(string) error { return fs.ErrPermission } result, err = UninstallSkillsWithResult(ctx, UninstallOptions{}) require.NoError(t, err) - assert.Equal(t, 0, result.SkillsRemoved) + assert.Equal(t, 0, result.SkillsChanged) require.Len(t, result.SkillFailures, 1) assert.ErrorIs(t, result.SkillFailures[0], fs.ErrPermission) removeAllFn = orig @@ -479,12 +491,12 @@ func TestUninstallCountsRemovedSkills(t *testing.T) { require.NoError(t, SaveState(globalDir, &InstallState{SchemaVersion: schemaVersionV2, Skills: map[string]string{"databricks-sql": "0.1.0"}})) result, err = UninstallSkillsWithResult(ctx, UninstallOptions{}) require.NoError(t, err) - assert.Equal(t, 1, result.SkillsRemoved) + assert.Equal(t, 1, result.SkillsChanged) assert.Empty(t, result.SkillFailures) } // A targeted agent whose entry cannot be removed keeps the skill, so the skill -// does not count as removed even though no other agent has it and its +// does not count as changed even though no other agent has it and its // canonical dir is deleted. func TestUninstallDoesNotCountSkillWithFailedTargetedRemoval(t *testing.T) { tmp := setupTestHome(t) @@ -504,11 +516,42 @@ func TestUninstallDoesNotCountSkillWithFailedTargetedRemoval(t *testing.T) { } return orig(path) } + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) result, err := UninstallSkillsWithResult(ctx, UninstallOptions{Agents: []string{agents.NameCursor}}) require.NoError(t, err) - assert.Equal(t, 0, result.SkillsRemoved) + assert.Equal(t, 0, result.SkillsChanged) require.Len(t, result.SkillFailures, 1) assert.ErrorIs(t, result.SkillFailures[0], fs.ErrPermission) + // Nothing was removed, so the run must not claim it uninstalled the skill. + assert.NotContains(t, stderr.String(), "Uninstalled") +} + +// Without --agents, removing an agent's symlink is a change even when the +// canonical dir cannot be removed, the same rule the --agents path follows. +func TestUninstallCountsRemovedSymlinkAsChange(t *testing.T) { + tmp := setupTestHome(t) + clearAgentDirOverrides(t) + globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") + canonicalDir := filepath.Join(globalDir, "databricks-sql") + require.NoError(t, os.MkdirAll(canonicalDir, 0o755)) + require.NoError(t, SaveState(globalDir, &InstallState{SchemaVersion: 1, Release: "v0.1.0", LastUpdated: time.Now(), Skills: map[string]string{"databricks-sql": "0.1.0"}})) + agentSkillsDir := filepath.Join(tmp, ".claude", "skills") + require.NoError(t, os.MkdirAll(agentSkillsDir, 0o755)) + link := filepath.Join(agentSkillsDir, "databricks-sql") + require.NoError(t, os.Symlink(canonicalDir, link)) + + orig := removeAllFn + t.Cleanup(func() { removeAllFn = orig }) + removeAllFn = func(string) error { return fs.ErrPermission } + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + result, err := UninstallSkillsWithResult(ctx, UninstallOptions{}) + require.NoError(t, err) + assert.Equal(t, 1, result.SkillsChanged) + require.Len(t, result.SkillFailures, 1) + assert.ErrorIs(t, result.SkillFailures[0], fs.ErrPermission) + _, err = os.Lstat(link) + assert.ErrorIs(t, err, fs.ErrNotExist) + assert.Contains(t, stderr.String(), "Uninstalled 1 skill.") } // installSQLSkillForAgent installs the databricks-sql test skill for one agent. diff --git a/libs/telemetry/protos/aitools_events.go b/libs/telemetry/protos/aitools_events.go index 9a39ef8c6b0..5247af6fdfb 100644 --- a/libs/telemetry/protos/aitools_events.go +++ b/libs/telemetry/protos/aitools_events.go @@ -98,7 +98,7 @@ const ( AitoolsCommandOutcomeCancelled AitoolsCommandOutcome = "CANCELLED" // The command had nothing to act on (e.g. no coding agents detected, a // --path dump where every skill was filtered out, or nothing installed to - // update or uninstall). + // update or uninstall). The command can still exit non-zero. AitoolsCommandOutcomeNothingToDo AitoolsCommandOutcome = "NOTHING_TO_DO" )