diff --git a/.nextchanges/cli/aitools-install-skills-failure-keeps-plugins.md b/.nextchanges/cli/aitools-install-skills-failure-keeps-plugins.md new file mode 100644 index 00000000000..60777a1b217 --- /dev/null +++ b/.nextchanges/cli/aitools-install-skills-failure-keeps-plugins.md @@ -0,0 +1 @@ +* `databricks aitools install` now installs plugins for the other coding agents even when installing skill files fails. ([#7479](https://github.com/databricks/cli/pull/7479)) diff --git a/cmd/aitools/install.go b/cmd/aitools/install.go index 3e4bccc5271..bb9ffe5ab8f 100644 --- a/cmd/aitools/install.go +++ b/cmd/aitools/install.go @@ -607,17 +607,18 @@ func executePlan(ctx context.Context, src installer.ManifestSource, plan []agent var outcomes []agentOutcome var explicitErrs []error + // A skills install runs as a group; its failure is the command's top-level + // error. Plugin agents don't depend on it, so they are still attempted. + var skillsErr error if len(skillsAgents) > 0 { if !quiet { installer.PrintInstallingFor(ctx, skillsAgents) } - // A skills install runs as a group; on failure the whole command fails and - // the top-level error category classifies it. - if err := installSkillsForAgentsFn(ctx, src, skillsAgents, opts); err != nil { - return outcomes, err - } - for _, a := range skillsAgents { - outcomes = append(outcomes, agentOutcome{agent: a, delivery: deliverySkills, status: outcomeInstalled}) + skillsErr = installSkillsForAgentsFn(ctx, src, skillsAgents, opts) + if skillsErr == nil { + for _, a := range skillsAgents { + outcomes = append(outcomes, agentOutcome{agent: a, delivery: deliverySkills, status: outcomeInstalled}) + } } } @@ -664,9 +665,22 @@ 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))) } } - if len(records) > 0 { - if err := recordPluginInstallsFn(ctx, opts.Scope, records, ref); err != nil { - return outcomes, 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 + // holding only these plugins would hide those skills from update and + // from the install's legacy checks, so leave recording to the next + // install whose skills step succeeds. + log.Debugf(ctx, "Not recording plugin installs: skills without install state are on disk") + } else if len(records) > 0 { + // After a skills failure the skills on disk stay at the old release, + // so record the plugins without advancing state.Release. + releaseRef := ref + if skillsErr != nil { + releaseRef = "" + } + if err := recordPluginInstallsFn(ctx, opts.Scope, records, releaseRef); err != nil { + return outcomes, errors.Join(skillsErr, err) } } } @@ -695,6 +709,11 @@ func executePlan(ctx context.Context, src installer.ManifestSource, plan []agent cmdio.LogString(ctx, fmt.Sprintf("Installed the plugin for %d %s.", pluginCount, noun)) } + // 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 { + return outcomes, skillsErr + } if len(explicitErrs) > 0 { return outcomes, &agentErrors{err: errors.Join(explicitErrs...)} } @@ -740,8 +759,9 @@ func buildInstallOutput(scope string, outcomes []agentOutcome, runErr error) ins } // A top-level failure (skills-group install, plugin recording) has no // per-agent entry, so surface it here too; otherwise the consumer sees a - // non-zero exit with an empty agents array and no reason. Per-agent failures - // stay in the agents entries above and are not repeated here. + // non-zero exit with no reason, and the agents array holds at most the + // plugin agents that were still installed. Per-agent failures stay in the + // agents entries above and are not repeated here. if e := topLevelFailure(runErr); e != nil { out.Error = e.Error() out.ErrorCategory = string(classifyInstallError(e)) diff --git a/cmd/aitools/install_test.go b/cmd/aitools/install_test.go index 1df2df8eb71..abd9a491537 100644 --- a/cmd/aitools/install_test.go +++ b/cmd/aitools/install_test.go @@ -372,6 +372,122 @@ func TestExecutePlanPluginWithUnresolvedSkillsRelease(t *testing.T) { assert.Empty(t, recordRef) } +func TestExecutePlanSkillsFailureStillInstallsPlugins(t *testing.T) { + setupTestAgents(t) // temp HOME for the real state file below + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") + plugins := setupPluginMock(t) + recordPluginInstallsFn = installer.RecordPluginInstalls + origSkills := installSkillsForAgentsFn + origCleanup := cleanupLegacyFn + t.Cleanup(func() { installSkillsForAgentsFn = origSkills; cleanupLegacyFn = origCleanup }) + cleanupLegacyFn = func(context.Context, *agents.Agent, string) error { return nil } + fetchErr := &installer.FetchError{Stage: installer.FetchStageManifest, StatusCode: 503, Err: errors.New("failed to fetch manifest: HTTP 503")} + installSkillsForAgentsFn = func(context.Context, installer.ManifestSource, []*agents.Agent, installer.InstallOptions) error { + return fetchErr + } + + ctx := cmdio.MockDiscard(t.Context()) + // Cursor's skills are at v0.2.5 from an earlier install. + 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"}, + })) + + claude := testPluginAgent(agents.NameClaudeCode, "Claude Code", "claude") + cursor := &agents.Agent{Name: agents.NameCursor, DisplayName: "Cursor"} + plan := buildPlan([]*agents.Agent{cursor, claude}, installer.ScopeGlobal, false, false) + outcomes, err := executePlan(ctx, nil, plan, installer.InstallOptions{Scope: installer.ScopeGlobal}, false) + + // The skills failure is still the command's error, but it no longer stops + // Claude Code's plugin install. + require.ErrorIs(t, err, fetchErr) + require.Len(t, *plugins, 1) + require.Len(t, outcomes, 1) + assert.Equal(t, agents.NameClaudeCode, outcomes[0].agent.Name) + assert.Equal(t, outcomeInstalled, outcomes[0].status) + + // The plugin is recorded, but the release stays at the skills' v0.2.5 so + // version and update don't report the old skills as up to date. + state, err := installer.LoadState(dir) + require.NoError(t, err) + assert.Contains(t, state.Plugins, agents.NameClaudeCode) + assert.Equal(t, "v0.2.5", state.Release) +} + +func TestExecutePlanSkillsFailureKeepsUntrackedSkillsVisible(t *testing.T) { + setupTestAgents(t) + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") + plugins := setupPluginMock(t) + recordPluginInstallsFn = installer.RecordPluginInstalls + origSkills := installSkillsForAgentsFn + origCleanup := cleanupLegacyFn + t.Cleanup(func() { installSkillsForAgentsFn = origSkills; cleanupLegacyFn = origCleanup }) + cleanupLegacyFn = func(context.Context, *agents.Agent, string) error { return nil } + fetchErr := &installer.FetchError{Stage: installer.FetchStageManifest, StatusCode: 503, Err: errors.New("failed to fetch manifest: HTTP 503")} + installSkillsForAgentsFn = func(context.Context, installer.ManifestSource, []*agents.Agent, installer.InstallOptions) error { + return fetchErr + } + + ctx := cmdio.MockDiscard(t.Context()) + // Skills from an install that predates state tracking: on disk, no state file. + dir, err := installer.GlobalSkillsDir(ctx) + require.NoError(t, err) + require.NoError(t, os.MkdirAll(filepath.Join(dir, "databricks"), 0o755)) + require.True(t, installer.HasUntrackedSkills(ctx, installer.ScopeGlobal)) + + claude := testPluginAgent(agents.NameClaudeCode, "Claude Code", "claude") + cursor := &agents.Agent{Name: agents.NameCursor, DisplayName: "Cursor"} + plan := buildPlan([]*agents.Agent{cursor, claude}, installer.ScopeGlobal, false, false) + outcomes, err := executePlan(ctx, nil, plan, installer.InstallOptions{Scope: installer.ScopeGlobal}, false) + + require.ErrorIs(t, err, fetchErr) + require.Len(t, *plugins, 1) + require.Len(t, outcomes, 1) + assert.Equal(t, outcomeInstalled, outcomes[0].status) + + // No plugin-only state file is created, so update still reports the + // untracked skills instead of treating the scope as plugin-only. + state, err := installer.LoadState(dir) + require.NoError(t, err) + assert.Nil(t, state) + assert.True(t, installer.HasUntrackedSkills(ctx, installer.ScopeGlobal)) +} + +func TestExecutePlanSkillsAndStateWriteFailuresAreJoined(t *testing.T) { + setupTestAgents(t) // temp HOME: the skills failure path reads the global skills dir + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") + setupPluginMock(t) + origSkills := installSkillsForAgentsFn + origCleanup := cleanupLegacyFn + t.Cleanup(func() { installSkillsForAgentsFn = origSkills; cleanupLegacyFn = origCleanup }) + cleanupLegacyFn = func(context.Context, *agents.Agent, string) error { return nil } + skillsErr := errors.New("failed to write skill files") + installSkillsForAgentsFn = func(context.Context, installer.ManifestSource, []*agents.Agent, installer.InstallOptions) error { + return skillsErr + } + stateErr := &installer.StateError{Path: ".state.json", Err: errors.New("permission denied")} + recordPluginInstallsFn = func(context.Context, string, map[string]installer.PluginRecord, string) error { + return stateErr + } + + claude := testPluginAgent(agents.NameClaudeCode, "Claude Code", "claude") + cursor := &agents.Agent{Name: agents.NameCursor, DisplayName: "Cursor"} + ctx := cmdio.MockDiscard(t.Context()) + plan := buildPlan([]*agents.Agent{cursor, claude}, installer.ScopeGlobal, false, false) + _, err := executePlan(ctx, nil, plan, installer.InstallOptions{Scope: installer.ScopeGlobal}, false) + + // Both failures reach the user, the skills one first. + require.ErrorIs(t, err, skillsErr) + require.ErrorIs(t, err, stateErr) + assert.Equal(t, skillsErr.Error()+"\n"+stateErr.Error(), err.Error()) + // The category is picked by error type, not by order: the typed state + // error outranks the untyped skills error. + assert.Equal(t, protos.AitoolsErrorCategoryStateInvalid, classifyInstallError(err)) +} + // --- RunE: skills-only path (config-dir detection, no plugin) --- func TestInstallSkillsOnlyAllAgents(t *testing.T) { @@ -597,12 +713,13 @@ func TestInstallOutputJSONTopLevelFailure(t *testing.T) { cmd := newTestInstallCmd() cmd.SetContext(ctx) cmd.SetOut(&out) - // Cursor is skills-only, so this fails in the skills-group path, which returns - // before appending any per-agent outcome. + // Cursor is skills-only, so this fails in the skills-group path, which records + // no per-agent outcome. cmd.SetArgs([]string{"--agents", "cursor", "--scope", "global", "--output", "json"}) // A top-level failure has no per-agent entry, so it must still be represented - // in the JSON (not just a bare non-zero exit with an empty agents array). + // in the JSON (not just a bare non-zero exit). With no plugin agent in the + // plan, the agents array is empty here. err := cmd.Execute() require.ErrorIs(t, err, root.ErrAlreadyPrinted) diff --git a/libs/aitools/installer/plugin.go b/libs/aitools/installer/plugin.go index b7a777116b9..5801cbc95d5 100644 --- a/libs/aitools/installer/plugin.go +++ b/libs/aitools/installer/plugin.go @@ -494,7 +494,8 @@ func claudeAlreadyDisabled(err error) bool { // RecordPluginInstalls persists plugin install records into the state file for // the given CLI scope (global or project), creating state if none exists. ref // is the resolved skills release the install corresponds to. An empty ref -// (release unresolved) keeps the existing state.Release. +// leaves state.Release unchanged: the release could not be resolved, or the +// skills install failed and the skills on disk are still at the old release. func RecordPluginInstalls(ctx context.Context, cliScope string, records map[string]PluginRecord, ref string) error { dir, err := skillsDir(ctx, cliScope) if err != nil { diff --git a/libs/aitools/installer/update.go b/libs/aitools/installer/update.go index e8a5cc7b88d..30f1d98ab48 100644 --- a/libs/aitools/installer/update.go +++ b/libs/aitools/installer/update.go @@ -275,6 +275,27 @@ func buildUpdateSkillSet(state *InstallState, manifest *Manifest, opts UpdateOpt return skillSet } +// HasUntrackedSkills reports whether the global scope has skills on disk but no +// state file: from an install that predates state tracking, or from a first +// install that failed before it saved state. Only the global scope is checked, +// so a first project install interrupted the same way is not detected. A state +// file that can't be read counts as no: the caller's next state load reports +// that error itself. +func HasUntrackedSkills(ctx context.Context, cliScope string) bool { + if cliScope != ScopeGlobal { + return false + } + dir, err := GlobalSkillsDir(ctx) + if err != nil { + return false + } + state, err := LoadState(dir) + if err != nil { + return false + } + return state == nil && hasLegacyInstall(ctx, dir) +} + // hasLegacyInstall checks both canonical and legacy dirs for skills on disk without state. func hasLegacyInstall(ctx context.Context, globalDir string) bool { if hasSkillsOnDisk(globalDir) {