diff --git a/.nextchanges/cli/aitools-marketplace-refresh.md b/.nextchanges/cli/aitools-marketplace-refresh.md new file mode 100644 index 00000000000..88d6e399d11 --- /dev/null +++ b/.nextchanges/cli/aitools-marketplace-refresh.md @@ -0,0 +1 @@ +* `databricks aitools install` and `databricks aitools update` refresh only the marketplace the Databricks plugin is listed in, so another unreachable marketplace no longer blocks them. When that refresh fails, both commands warn, install or update the plugin from the local copy of the marketplace, and record the plugin version as unknown. ([#7473](https://github.com/databricks/cli/pull/7473)) diff --git a/cmd/aitools/list.go b/cmd/aitools/list.go index 008bce4d0b0..86c273e6bfc 100644 --- a/cmd/aitools/list.go +++ b/cmd/aitools/list.go @@ -379,15 +379,17 @@ func renderSkillTable(skills []skillEntry, bothScopes bool) string { // are skipped: this line reports the plugin, and their versions would otherwise // decide the status of an agent that also has a plugin in another scope. func agentStatusLabel(a agentEntry, release string) string { - version, upToDate := "", true + // picked is separate from version because an empty version ("version + // unknown") is a real pick, not "nothing picked yet". + version, picked, upToDate := "", false, true for _, scope := range []string{installer.ScopeProject, installer.ScopeGlobal} { info, ok := a.Installed[scope] if !ok || info.Delivery != deliveryPlugin.String() { continue } stale := info.Version != release - if version == "" || (upToDate && stale) { - version = info.Version + if !picked || (upToDate && stale) { + version, picked = info.Version, true } if stale { upToDate = false diff --git a/cmd/aitools/list_test.go b/cmd/aitools/list_test.go index e1c8b42e033..257300846c2 100644 --- a/cmd/aitools/list_test.go +++ b/cmd/aitools/list_test.go @@ -348,6 +348,17 @@ func TestBuildAgentEntriesRecordsPerScopeVersions(t *testing.T) { assert.Equal(t, "databricks plugin · v0.2.5 · update available", agentStatusLabel(cc, "0.2.6")) } +func TestAgentStatusLabelKeepsUnknownStaleScope(t *testing.T) { + // The project record has no version (its last install could not refresh the + // marketplace), the global one is current. The unknown scope is stale and + // must not be hidden behind the up-to-date version. + a := agentEntry{Installed: map[string]installInfo{ + installer.ScopeProject: {Delivery: deliveryPlugin.String(), Version: ""}, + installer.ScopeGlobal: {Delivery: deliveryPlugin.String(), Version: "0.2.7"}, + }} + assert.Equal(t, "databricks plugin · version unknown · update available", agentStatusLabel(a, "0.2.7")) +} + func TestRenderListJSONScopeFiltersSummary(t *testing.T) { out := listOutput{ Release: "0.1.0", diff --git a/cmd/aitools/update.go b/cmd/aitools/update.go index dba5668bf58..784a7137293 100644 --- a/cmd/aitools/update.go +++ b/cmd/aitools/update.go @@ -300,7 +300,7 @@ func printPluginCheckResults(ctx context.Context, state *installer.InstallState, rec := state.Plugins[name] status := "up to date" if rec.Version == "" { - status = "version unknown" + status = "update may be available" } else if rec.Version != latest { status = "update available" } diff --git a/cmd/aitools/update_test.go b/cmd/aitools/update_test.go index 78e27d0d71b..3f6dc11e539 100644 --- a/cmd/aitools/update_test.go +++ b/cmd/aitools/update_test.go @@ -59,6 +59,24 @@ func TestUpdateCheckPrintsNoChanges(t *testing.T) { } func TestUpdateCheckPrintsPluginState(t *testing.T) { + for _, tc := range []struct { + name string + version string + want string + }{ + {"current", "0.2.6", "Claude Code databricks plugin v0.2.6 (up to date)"}, + {"older", "0.2.5", "Claude Code databricks plugin v0.2.5 (update available)"}, + // A record without a version (the last install or update could not + // refresh the marketplace) may be behind, and `aitools update` would fix it. + {"unknown", "", "Claude Code databricks plugin version unknown (update may be available)"}, + } { + t.Run(tc.name, func(t *testing.T) { + testUpdateCheckPrintsPluginState(t, tc.version, tc.want) + }) + } +} + +func testUpdateCheckPrintsPluginState(t *testing.T, version, want string) { noTelemetryAuth(t) setupTestAgents(t) t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") @@ -85,7 +103,7 @@ func TestUpdateCheckPrintsPluginState(t *testing.T) { SchemaVersion: 2, Release: "v0.2.6", Plugins: map[string]installer.PluginRecord{ - agents.NameClaudeCode: {Marketplace: "claude-plugins-official", Plugin: "databricks", Version: "0.2.6"}, + agents.NameClaudeCode: {Marketplace: "claude-plugins-official", Plugin: "databricks", Version: version}, }, })) @@ -94,7 +112,7 @@ func TestUpdateCheckPrintsPluginState(t *testing.T) { cmd.SetArgs([]string{"--scope", "global", "--check"}) require.NoError(t, cmd.Execute()) - assert.Contains(t, stderr.String(), "Claude Code databricks plugin v0.2.6 (up to date)") + assert.Contains(t, stderr.String(), want) } func TestUpdateMigratesManagedRawSkillsToPlugins(t *testing.T) { diff --git a/cmd/version/version.go b/cmd/version/version.go index bc9807e625f..d0456cac9d5 100644 --- a/cmd/version/version.go +++ b/cmd/version/version.go @@ -44,7 +44,7 @@ func renderUpdateCheck(ctx context.Context, result *versioncheck.Result, output return err } if output == flags.OutputText { - agents.MaybeVersionCheckTip(ctx, installer.InstalledTools) + agents.MaybeVersionCheckTip(ctx, installer.InstalledPluginAgents) } return nil } diff --git a/libs/aitools/agents/recommend.go b/libs/aitools/agents/recommend.go index b5f7c93d605..14a4da5b00b 100644 --- a/libs/aitools/agents/recommend.go +++ b/libs/aitools/agents/recommend.go @@ -25,9 +25,10 @@ func PrintSkillsTip(ctx context.Context) { // when a coding agent is detected without Databricks tooling. Claude Code // callers are skipped because MaybeHint already covers them, with its // per-session throttle. installedPlugins is a parameter because this package -// cannot import the installer; callers pass installer.InstalledTools. No -// telemetry is logged: `databricks version` never resolves auth, so the event -// would not be uploaded (see logHintShown). +// cannot import the installer; callers pass installer.InstalledPluginAgents, +// which counts a plugin recorded in the CLI install state with or without a +// known version. No telemetry is logged: `databricks version` never resolves +// auth, so the event would not be uploaded (see logHintShown). func MaybeVersionCheckTip(ctx context.Context, installedPlugins func(context.Context) []string) { if useragent.AgentProvider() == claudeAgentProvider { return diff --git a/libs/aitools/installer/detect.go b/libs/aitools/installer/detect.go index ce4312dca28..5a00d5df515 100644 --- a/libs/aitools/installer/detect.go +++ b/libs/aitools/installer/detect.go @@ -13,7 +13,10 @@ import ( // agent's own plugin cache (ground truth, catches installs made directly through // the agent), falling back to the version recorded in the CLI install state when // the plugin is not found on disk. An agent with no resolvable version is -// omitted rather than reported without one. +// omitted rather than reported without one. That includes an agent without a +// plugin cache reader (Codex) whose record has no version because its last +// update ran from a marketplace copy that could not be refreshed; use +// InstalledPluginAgents to ask whether a plugin is installed at all. func InstalledTools(ctx context.Context) []string { recorded := recordedPluginVersions(ctx) @@ -34,10 +37,33 @@ func InstalledTools(ctx context.Context) []string { return tools } +// InstalledPluginAgents returns the names of the coding tools that have the +// Databricks plugin installed, sorted: found in the agent's own plugin cache or +// recorded in the CLI install state, whether or not a version is known. +func InstalledPluginAgents(ctx context.Context) []string { + recorded := recordedPluginVersions(ctx) + + var names []string + for _, a := range agents.Registry { + if a.Plugin == nil { + continue + } + _, inCache := a.DatabricksPluginVersion(ctx) + _, inState := recorded[a.Name] + if inCache || inState { + names = append(names, a.Name) + } + } + slices.Sort(names) + return names +} + // recordedPluginVersions maps agent name to the databricks plugin version // recorded in the CLI install state, merged across global and project scope. -// When both scopes record a version, the project scope wins (it is the more -// specific, closer-to-the-invocation install). +// An agent whose record has no version maps to "". When both scopes record a +// version, the project scope wins (it is the more specific, +// closer-to-the-invocation install); a scope without a version never replaces +// one with a version. func recordedPluginVersions(ctx context.Context) map[string]string { versions := map[string]string{} // Load global first, then project, so project overwrites on conflict. @@ -55,7 +81,7 @@ func recordedPluginVersions(ctx context.Context) map[string]string { continue } for name, rec := range state.Plugins { - if rec.Version != "" { + if _, seen := versions[name]; !seen || rec.Version != "" { versions[name] = rec.Version } } diff --git a/libs/aitools/installer/detect_test.go b/libs/aitools/installer/detect_test.go index 5ef20ae2359..b5ab58f3191 100644 --- a/libs/aitools/installer/detect_test.go +++ b/libs/aitools/installer/detect_test.go @@ -54,6 +54,20 @@ func TestInstalledTools(t *testing.T) { projectPlugins: map[string]string{"codex": "0.2.7"}, want: []string{"codex_0.2.7"}, }, + { + // A record without a version (its last update ran from a marketplace + // copy that could not be refreshed) yields no token. + name: "state without version omitted", + globalPlugins: map[string]string{"codex": ""}, + want: nil, + }, + { + // A project record without a version does not hide the global one. + name: "project state without version keeps global", + globalPlugins: map[string]string{"codex": "0.2.7"}, + projectPlugins: map[string]string{"codex": ""}, + want: []string{"codex_0.2.7"}, + }, { // Manifest (claude) and state (codex) contribute; output is sorted. name: "mixed sources sorted", @@ -82,6 +96,57 @@ func TestInstalledTools(t *testing.T) { } } +// TestInstalledPluginAgents counts a plugin as installed whether or not its +// version is known, so `databricks version --check` does not suggest installing +// Databricks tooling to a user who has the plugin. +func TestInstalledPluginAgents(t *testing.T) { + tests := []struct { + name string + claudeManifestVersion string + globalPlugins map[string]string + projectPlugins map[string]string + want []string + }{ + { + name: "none installed", + want: nil, + }, + { + name: "state with version", + globalPlugins: map[string]string{"codex": "0.2.7"}, + want: []string{"codex"}, + }, + { + name: "state without version", + projectPlugins: map[string]string{"codex": ""}, + want: []string{"codex"}, + }, + { + name: "manifest and state sorted", + claudeManifestVersion: "0.2.9", + globalPlugins: map[string]string{"codex": ""}, + want: []string{"claude-code", "codex"}, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + tmp := t.TempDir() + t.Setenv("HOME", tmp) + t.Setenv("USERPROFILE", tmp) + t.Chdir(t.TempDir()) + + if tc.claudeManifestVersion != "" { + writeClaudeManifest(t, filepath.Join(tmp, ".claude"), tc.claudeManifestVersion) + } + savePluginState(t, GlobalSkillsDir, tc.globalPlugins) + savePluginState(t, ProjectSkillsDir, tc.projectPlugins) + + assert.Equal(t, tc.want, InstalledPluginAgents(t.Context())) + }) + } +} + // writeClaudeManifest writes a Claude installed_plugins.json recording the // databricks plugin at the given version under configDir. func writeClaudeManifest(t *testing.T, configDir, version string) { diff --git a/libs/aitools/installer/plugin.go b/libs/aitools/installer/plugin.go index 107b0b5b6e5..cb8e1d78428 100644 --- a/libs/aitools/installer/plugin.go +++ b/libs/aitools/installer/plugin.go @@ -105,11 +105,7 @@ func recordedInstallTarget(agent *agents.Agent, rec PluginRecord) string { if plugin == "" { plugin = agent.Plugin.ID } - marketplace := rec.Marketplace - if marketplace == "" { - marketplace = agent.Plugin.Marketplace - } - return plugin + "@" + marketplace + return plugin + "@" + recordedMarketplace(agent, rec) } // marketplaceAddArgs builds the `plugin marketplace add ` argv (sans binary). @@ -137,19 +133,27 @@ func marketplaceRemoveArgs(spec *agents.PluginSpec) []string { } func marketplaceRemoveArgsForRecord(agent *agents.Agent, rec PluginRecord) []string { - marketplace := rec.Marketplace - if marketplace == "" { - marketplace = agent.Plugin.Marketplace + return []string{"plugin", "marketplace", "remove", recordedMarketplace(agent, rec)} +} + +// recordedMarketplace is the marketplace a recorded install came from, or the +// agent's current one for records written before it was stored. +func recordedMarketplace(agent *agents.Agent, rec PluginRecord) string { + if rec.Marketplace != "" { + return rec.Marketplace } - return []string{"plugin", "marketplace", "remove", marketplace} + return agent.Plugin.Marketplace } // marketplaceUpdateArgs builds the marketplace refresh command for agents whose // plugin CLI supports it. Claude's official marketplace can be stale locally, // causing install/update to report that a published plugin does not exist. -func marketplaceUpdateArgs(agent *agents.Agent) []string { +// The refresh names the marketplace: without a name Claude refreshes every +// marketplace the user has added, and one unreachable third-party marketplace +// then fails the databricks install. +func marketplaceUpdateArgs(agent *agents.Agent, marketplace string) []string { if agent.Name == agents.NameClaudeCode { - return []string{"plugin", "marketplace", "update"} + return []string{"plugin", "marketplace", "update", marketplace} } return nil } @@ -172,26 +176,62 @@ func pluginInstallArgs(agent *agents.Agent, scope string) []string { } } -// pluginUpdateSteps builds the ordered per-agent update argv sets (sans binary). -// Codex updates in two steps: refresh the marketplace, then re-add. -func pluginUpdateSteps(agent *agents.Agent, rec PluginRecord) [][]string { +// pluginRefreshArgs builds the marketplace refresh that runs before an update +// (sans binary), or nil for agents without one. Both agents name the +// marketplace, so an unreachable marketplace the user added for another plugin +// does not fail the update. +func pluginRefreshArgs(agent *agents.Agent, rec PluginRecord) []string { + if agent.Name == agents.NameCodex { + return []string{"plugin", "marketplace", "upgrade", recordedMarketplace(agent, rec)} + } + return marketplaceUpdateArgs(agent, recordedMarketplace(agent, rec)) +} + +// pluginUpdateArgs builds the per-agent update argv (sans binary). Codex +// updates by re-adding the plugin. +func pluginUpdateArgs(agent *agents.Agent, rec PluginRecord) []string { target := recordedInstallTarget(agent, rec) switch agent.Name { case agents.NameCodex: - return [][]string{ - {"plugin", "marketplace", "upgrade"}, - {"plugin", "add", target}, - } + return []string{"plugin", "add", target} case agents.NameClaudeCode: - return [][]string{ - marketplaceUpdateArgs(agent), - appendScopeArg([]string{"plugin", "update", target}, rec.Scope), - } + return appendScopeArg([]string{"plugin", "update", target}, rec.Scope) default: - return [][]string{{"plugin", "update", target}} + return []string{"plugin", "update", target} } } +// refreshMarketplace runs the marketplace refresh (args may be nil for agents +// without one). A failed refresh is not fatal: the local copy of the marketplace +// usually still lists the plugin, so the caller goes on with it, and either +// warns with warnLocalMarketplaceCopy when that succeeds and the recorded +// version is cleared, or adds the refresh error to its own error with +// withRefreshError. +func refreshMarketplace(ctx context.Context, bin string, args []string) error { + if args == nil { + return nil + } + _, err := runAgentCmd(ctx, pluginCmdTimeout, prepend(bin, args)) + return err +} + +// warnLocalMarketplaceCopy tells the user that a successful install or update +// used a local marketplace copy that could not be refreshed, so the plugin may +// be out of date. retry names what to run later; it differs between install +// and update so an update is not told to run the command that just ran. +func warnLocalMarketplaceCopy(ctx context.Context, agent *agents.Agent, marketplace, retry string, refreshErr error) { + log.Warnf(ctx, "Could not refresh the %s marketplace, so the %s plugin came from its local copy and may be out of date; %s: %s", marketplace, agent.DisplayName, retry, stderrOf(refreshErr)) +} + +// withRefreshError appends a failed refresh to the detail of a failed install +// or update, since a stale local marketplace copy may be the cause. +func withRefreshError(blocked *BlockedError, refreshErr error) *BlockedError { + if refreshErr != nil { + blocked.Detail += " (refreshing the marketplace also failed: " + stderrOf(refreshErr) + ")" + } + return blocked +} + // pluginUninstallArgs builds the per-agent uninstall argv (sans binary). // Codex removes with `plugin remove`; the others use `plugin uninstall`. func pluginUninstallArgs(agent *agents.Agent, rec PluginRecord) []string { @@ -258,16 +298,7 @@ func InstallPluginForAgent(ctx context.Context, agent *agents.Agent, nativeScope _, addErr := runAgentCmd(ctx, pluginCmdTimeout, prepend(bin, marketplaceAddArgs(agent.Plugin))) installedMarketplace = addErr == nil && !alreadyPresent } - if args := marketplaceUpdateArgs(agent); args != nil { - if _, err := runAgentCmd(ctx, pluginCmdTimeout, prepend(bin, args)); err != nil { - if installedMarketplace { - if _, rmErr := runAgentCmd(ctx, pluginCmdTimeout, prepend(bin, marketplaceRemoveArgs(agent.Plugin))); rmErr != nil { - log.Warnf(ctx, "%s plugin marketplace refresh failed and the marketplace could not be de-registered: %v", agent.DisplayName, rmErr) - } - } - return PluginRecord{}, &BlockedError{Agent: agent.Name, Reason: ReasonInstallFailed, Detail: stderrOf(err)} - } - } + refreshErr := refreshMarketplace(ctx, bin, marketplaceUpdateArgs(agent, agent.Plugin.Marketplace)) if _, err := runAgentCmd(ctx, pluginCmdTimeout, prepend(bin, pluginInstallArgs(agent, nativeScope))); err != nil { // Roll back a marketplace we just added so a failed install doesn't @@ -277,35 +308,46 @@ func InstallPluginForAgent(ctx context.Context, agent *agents.Agent, nativeScope log.Warnf(ctx, "%s plugin install failed and the marketplace could not be de-registered: %v", agent.DisplayName, rmErr) } } - return PluginRecord{}, &BlockedError{Agent: agent.Name, Reason: ReasonInstallFailed, Detail: stderrOf(err)} + return PluginRecord{}, withRefreshError(&BlockedError{Agent: agent.Name, Reason: ReasonInstallFailed, Detail: stderrOf(err)}, refreshErr) } + // A marketplace we just added is a fresh copy, so a failed refresh right + // after it changes nothing. Otherwise we can't tell which version the local + // copy held, so the record says "version unknown" until the next successful + // update. + version := DisplaySkillsVersion(ref) + if refreshErr != nil && !installedMarketplace { + warnLocalMarketplaceCopy(ctx, agent, agent.Plugin.Marketplace, "run `databricks aitools update` later", refreshErr) + version = "" + } return PluginRecord{ Marketplace: agent.Plugin.Marketplace, Plugin: agent.Plugin.ID, Scope: nativeScope, - Version: DisplaySkillsVersion(ref), + Version: version, InstalledMarketplace: installedMarketplace, }, nil } // UpdatePluginForAgent updates the plugin through the agent's own CLI. The // plugin's own update handles content the release dropped, so there is no -// per-skill prune for plugin agents. -func UpdatePluginForAgent(ctx context.Context, agent *agents.Agent, rec PluginRecord) error { +// per-skill prune for plugin agents. When the update succeeded after a failed +// marketplace refresh, it returns that refresh error: the update ran from the +// local copy, and the caller decides whether the recorded version still holds +// and whether to warn. +func UpdatePluginForAgent(ctx context.Context, agent *agents.Agent, rec PluginRecord) (refreshErr, err error) { if agent.Plugin == nil { - return &BlockedError{Agent: agent.Name, Reason: ReasonNoPlugin} + return nil, &BlockedError{Agent: agent.Name, Reason: ReasonNoPlugin} } bin, err := resolveAgentBinary(agent) if err != nil { - return &BlockedError{Agent: agent.Name, Reason: ReasonCLINotOnPath, Detail: err.Error()} + return nil, &BlockedError{Agent: agent.Name, Reason: ReasonCLINotOnPath, Detail: err.Error()} } - for _, args := range pluginUpdateSteps(agent, rec) { - if _, err := runAgentCmd(ctx, pluginCmdTimeout, prepend(bin, args)); err != nil { - return &BlockedError{Agent: agent.Name, Reason: ReasonInstallFailed, Detail: stderrOf(err)} - } + refreshErr = refreshMarketplace(ctx, bin, pluginRefreshArgs(agent, rec)) + if _, err := runAgentCmd(ctx, pluginCmdTimeout, prepend(bin, pluginUpdateArgs(agent, rec))); err != nil { + return nil, withRefreshError(&BlockedError{Agent: agent.Name, Reason: ReasonInstallFailed, Detail: stderrOf(err)}, refreshErr) } - return nil + return refreshErr, nil } // UninstallPluginForAgent removes the plugin through the agent's own CLI, and diff --git a/libs/aitools/installer/plugin_test.go b/libs/aitools/installer/plugin_test.go index 4ce12d5934e..5de1d563650 100644 --- a/libs/aitools/installer/plugin_test.go +++ b/libs/aitools/installer/plugin_test.go @@ -1,19 +1,31 @@ package installer import ( + "bytes" "context" "errors" + "log/slog" "os/exec" "path/filepath" "testing" "github.com/databricks/cli/libs/aitools/agents" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/process" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) +// captureWarnings routes warnings and errors logged through ctx into a buffer. +func captureWarnings(ctx context.Context) (context.Context, *bytes.Buffer) { + var buf bytes.Buffer + logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelWarn})) + return log.NewContext(ctx, logger), &buf +} + +const localCopyWarning = "plugin came from its local copy and may be out of date" + func stubAgentLookPath(t *testing.T, found bool) { t.Helper() orig := lookPath @@ -62,7 +74,7 @@ func TestInstallPluginForAgentClaudeSuccess(t *testing.T) { cmds := stub.Commands() assert.Contains(t, cmds, "claude plugin --help") assert.Contains(t, cmds, "claude plugin marketplace add databricks/databricks-agent-skills") - assert.Contains(t, cmds, "claude plugin marketplace update") + assert.Contains(t, cmds, "claude plugin marketplace update databricks-agent-skills") assert.Contains(t, cmds, "claude plugin install databricks@databricks-agent-skills --scope user") } @@ -83,7 +95,7 @@ func TestInstallPluginForAgentSharedMarketplace(t *testing.T) { cmds := stub.Commands() assert.Contains(t, cmds, "claude plugin marketplace add anthropics/claude-plugins-official") - assert.Contains(t, cmds, "claude plugin marketplace update") + assert.Contains(t, cmds, "claude plugin marketplace update claude-plugins-official") assert.Contains(t, cmds, "claude plugin install databricks@claude-plugins-official --scope user") } @@ -163,18 +175,75 @@ func TestInstallPluginRollsBackMarketplaceOnInstallFailure(t *testing.T) { assert.Contains(t, stub.Commands(), "claude plugin marketplace remove databricks-agent-skills") } -func TestInstallPluginRollsBackMarketplaceOnRefreshFailure(t *testing.T) { +func TestInstallPluginContinuesWhenRefreshFails(t *testing.T) { + // The plugin's own marketplace can't be refreshed (e.g. it is unreachable), + // but the local copy still lists the plugin, so the install goes on. + for _, tc := range []struct { + name string + // listed is the `plugin marketplace list` output before the install. + listed string + wantInstalledByUs bool + wantVersion string + wantWarning bool + }{ + { + // We just added the marketplace, so its local copy is fresh. + name: "just added", + wantInstalledByUs: true, + wantVersion: "0.2.6", + }, + { + // Registered before, so the local copy may be old, and the + // marketplace is not ours to remove or record. + name: "already registered", + listed: "databricks-agent-skills\n", + wantVersion: "", + wantWarning: true, + }, + } { + t.Run(tc.name, func(t *testing.T) { + stubAgentLookPath(t, true) + ctx, stub := process.WithStub(t.Context()) + ctx, logs := captureWarnings(ctx) + stub.WithCallback(func(*exec.Cmd) error { return nil }) + stub.WithStdoutFor("plugin marketplace list", tc.listed) + stub.WithStderrFor("plugin marketplace update", "could not reach github.com"). + WithFailureFor("plugin marketplace update", errors.New("exit status 1")) + + rec, err := InstallPluginForAgent(ctx, claudeAgent(), "user", "v0.2.6") + require.NoError(t, err) + assert.Equal(t, tc.wantInstalledByUs, rec.InstalledMarketplace) + assert.Equal(t, tc.wantVersion, rec.Version) + assert.Contains(t, stub.Commands(), "claude plugin install databricks@databricks-agent-skills --scope user") + assert.NotContains(t, stub.Commands(), "claude plugin marketplace remove databricks-agent-skills") + if tc.wantWarning { + assert.Contains(t, logs.String(), localCopyWarning) + assert.Contains(t, logs.String(), "could not reach github.com") + } else { + assert.NotContains(t, logs.String(), localCopyWarning) + } + }) + } +} + +func TestInstallPluginRollsBackMarketplaceWhenRefreshAndInstallFail(t *testing.T) { stubAgentLookPath(t, true) ctx, stub := process.WithStub(t.Context()) stub.WithCallback(func(*exec.Cmd) error { return nil }) - // Marketplace absent (empty list) so we add it; then the refresh fails. - stub.WithFailureFor("plugin marketplace update", errors.New("boom")) + // Marketplace absent (empty list) so we add it; then refresh and install fail. + stub.WithFailureFor("plugin marketplace update", errors.New("refresh boom")) + stub.WithFailureFor("plugin install", errors.New("install boom")) + ctx, logs := captureWarnings(ctx) _, err := InstallPluginForAgent(ctx, claudeAgent(), "user", "v0.2.6") var be *BlockedError require.ErrorAs(t, err, &be) assert.Equal(t, ReasonInstallFailed, be.Reason) - // We added the marketplace, so a failed refresh must de-register it again. + assert.Contains(t, be.Detail, "install boom (refreshing the marketplace also failed: ") + assert.Contains(t, be.Detail, "refresh boom)") + // The refresh error is in the detail; the "local copy" advice would be wrong. + assert.NotContains(t, logs.String(), localCopyWarning) + // We added the marketplace, so a failed install must de-register it again. assert.Contains(t, stub.Commands(), "claude plugin marketplace remove databricks-agent-skills") } @@ -183,21 +252,79 @@ func TestUpdatePluginForAgentCodexTwoStep(t *testing.T) { ctx, stub := process.WithStub(t.Context()) stub.WithCallback(func(*exec.Cmd) error { return nil }) - require.NoError(t, UpdatePluginForAgent(ctx, codexAgent(), PluginRecord{Marketplace: "databricks-agent-skills", Plugin: "databricks", Scope: "user"})) + refreshErr, err := UpdatePluginForAgent(ctx, codexAgent(), PluginRecord{Marketplace: "databricks-agent-skills", Plugin: "databricks", Scope: "user"}) + require.NoError(t, err) + assert.NoError(t, refreshErr) cmds := stub.Commands() - assert.Contains(t, cmds, "codex plugin marketplace upgrade") + assert.Contains(t, cmds, "codex plugin marketplace upgrade databricks-agent-skills") assert.Contains(t, cmds, "codex plugin add databricks@databricks-agent-skills") } func TestUpdatePluginForAgentClaudeOneStep(t *testing.T) { + for _, tc := range []struct { + name string + rec PluginRecord + }{ + {"recorded marketplace", PluginRecord{Marketplace: "databricks-agent-skills", Plugin: "databricks", Scope: "user"}}, + // A record without a marketplace falls back to the agent's current one. + {"record without marketplace", PluginRecord{Plugin: "databricks", Scope: "user"}}, + } { + t.Run(tc.name, func(t *testing.T) { + stubAgentLookPath(t, true) + ctx, stub := process.WithStub(t.Context()) + stub.WithCallback(func(*exec.Cmd) error { return nil }) + + refreshErr, err := UpdatePluginForAgent(ctx, claudeAgent(), tc.rec) + require.NoError(t, err) + assert.NoError(t, refreshErr) + assert.Equal(t, []string{ + "claude plugin marketplace update databricks-agent-skills", + "claude plugin update databricks@databricks-agent-skills --scope user", + }, stub.Commands()) + }) + } +} + +func TestUpdatePluginForAgentContinuesWhenRefreshFails(t *testing.T) { + for _, tc := range []struct { + agent *agents.Agent + refresh string + update string + }{ + {claudeAgent(), "claude plugin marketplace update databricks-agent-skills", "claude plugin update databricks@databricks-agent-skills --scope user"}, + {codexAgent(), "codex plugin marketplace upgrade databricks-agent-skills", "codex plugin add databricks@databricks-agent-skills"}, + } { + t.Run(tc.agent.Name, func(t *testing.T) { + stubAgentLookPath(t, true) + ctx, stub := process.WithStub(t.Context()) + stub.WithCallback(func(*exec.Cmd) error { return nil }) + stub.WithStderrFor("plugin marketplace", "could not reach github.com"). + WithFailureFor("plugin marketplace", errors.New("exit status 1")) + + refreshErr, err := UpdatePluginForAgent(ctx, tc.agent, PluginRecord{Marketplace: "databricks-agent-skills", Plugin: "databricks", Scope: "user"}) + require.NoError(t, err) + require.Error(t, refreshErr) + assert.Equal(t, "could not reach github.com", stderrOf(refreshErr)) + assert.Equal(t, []string{tc.refresh, tc.update}, stub.Commands()) + }) + } +} + +func TestUpdatePluginForAgentReportsRefreshWhenUpdateFails(t *testing.T) { stubAgentLookPath(t, true) ctx, stub := process.WithStub(t.Context()) stub.WithCallback(func(*exec.Cmd) error { return nil }) + stub.WithFailureFor("plugin marketplace update", errors.New("refresh boom")) + stub.WithFailureFor("plugin update", errors.New("update boom")) + ctx, logs := captureWarnings(ctx) - require.NoError(t, UpdatePluginForAgent(ctx, claudeAgent(), PluginRecord{Marketplace: "databricks-agent-skills", Plugin: "databricks", Scope: "user"})) - cmds := stub.Commands() - assert.Contains(t, cmds, "claude plugin marketplace update") - assert.Contains(t, cmds, "claude plugin update databricks@databricks-agent-skills --scope user") + _, err := UpdatePluginForAgent(ctx, claudeAgent(), PluginRecord{Marketplace: "databricks-agent-skills", Plugin: "databricks", Scope: "user"}) + var be *BlockedError + require.ErrorAs(t, err, &be) + assert.Equal(t, ReasonInstallFailed, be.Reason) + assert.Contains(t, be.Detail, "update boom (refreshing the marketplace also failed: ") + assert.Contains(t, be.Detail, "refresh boom)") + assert.NotContains(t, logs.String(), localCopyWarning) } func TestUninstallPluginClaudeAlreadyDisabledContinues(t *testing.T) { @@ -441,6 +568,60 @@ func TestUpdateInstalledPlugins(t *testing.T) { assert.Equal(t, "0.2.7", state.Plugins[agents.NameCopilot].Version) } +func TestUpdateInstalledPluginsRecordsUnknownVersionWhenRefreshFails(t *testing.T) { + for _, tc := range []struct { + name string + recorded string + want string + wantWarning bool + }{ + // The update ran from a local copy of unknown age. + {"older release recorded", "0.2.6", "", true}, + // The record already holds this release, and the local copy can only + // have moved forward since the refresh that wrote it, so there is + // nothing to warn about. + {"current release recorded", "0.2.7", "0.2.7", false}, + } { + t.Run(tc.name, func(t *testing.T) { + setupTestHome(t) + stubAgentLookPath(t, true) + ctx, stub := process.WithStub(t.Context()) + ctx, logs := captureWarnings(ctx) + stub.WithCallback(func(*exec.Cmd) error { return nil }) + stub.WithStderrFor("plugin marketplace update", "could not reach github.com"). + WithFailureFor("plugin marketplace update", errors.New("exit status 1")) + + dir, err := GlobalSkillsDir(ctx) + require.NoError(t, err) + require.NoError(t, SaveState(dir, &InstallState{ + SchemaVersion: schemaVersionV2, + Release: "v" + tc.recorded, + Plugins: map[string]PluginRecord{ + agents.NameClaudeCode: {Marketplace: "claude-plugins-official", Plugin: "databricks", Scope: "user", Version: tc.recorded}, + }, + })) + + updated, err := UpdateInstalledPlugins(ctx, ScopeGlobal, "v0.2.7") + require.NoError(t, err) + require.Len(t, updated, 1) + require.NoError(t, updated[0].Err) + assert.Equal(t, tc.want, updated[0].Version) + assert.Contains(t, stub.Commands(), "claude plugin update databricks@claude-plugins-official --scope user") + + state, err := LoadState(dir) + require.NoError(t, err) + assert.Equal(t, tc.want, state.Plugins[agents.NameClaudeCode].Version) + if tc.wantWarning { + assert.Contains(t, logs.String(), localCopyWarning) + assert.Contains(t, logs.String(), "rerun `databricks aitools update` later") + assert.Contains(t, logs.String(), "could not reach github.com") + } else { + assert.NotContains(t, logs.String(), localCopyWarning) + } + }) + } +} + func TestUpdateInstalledPluginsUsesRecordedClaudeScope(t *testing.T) { for _, scope := range []string{"project", "user", "local"} { t.Run(scope, func(t *testing.T) { @@ -464,7 +645,7 @@ func TestUpdateInstalledPluginsUsesRecordedClaudeScope(t *testing.T) { require.Len(t, updated, 1) cmds := stub.Commands() - assert.Contains(t, cmds, "claude plugin marketplace update") + assert.Contains(t, cmds, "claude plugin marketplace update claude-plugins-official") assert.Contains(t, cmds, "claude plugin update databricks@claude-plugins-official --scope "+scope) }) } diff --git a/libs/aitools/installer/update.go b/libs/aitools/installer/update.go index 695edfb9e90..e8a5cc7b88d 100644 --- a/libs/aitools/installer/update.go +++ b/libs/aitools/installer/update.go @@ -434,7 +434,9 @@ type PluginUpdate struct { } // UpdateInstalledPlugins runs the plugin update for every plugin recorded in the -// given scope's state, bumping each record's version to ref. A plugin that can't +// given scope's state, bumping each record's version to ref, or clearing it +// (version unknown) when the marketplace refresh failed and the record did not +// already hold ref. A plugin that can't // be updated (CLI missing, etc.) is skipped with a warning, never failed, to // keep the non-interactive update prompt-free and exit-0 on partial success; // it is returned after the updated ones with Err set. @@ -461,14 +463,24 @@ func UpdateInstalledPlugins(ctx context.Context, scope, ref string) ([]PluginUpd continue } rec := state.Plugins[name] - if err := UpdatePluginForAgent(ctx, agent, rec); err != nil { + refreshErr, err := UpdatePluginForAgent(ctx, agent, rec) + if err != nil { log.Warnf(ctx, "Skipped %s: %v", agent.DisplayName, err) failed = append(failed, PluginUpdate{Agent: agent.DisplayName, Err: err}) continue } - rec.Version = version + // After a failed refresh the update ran from the local copy, so its + // version is unknown, unless the record already holds this release: the + // local copy can only have moved forward since the refresh that wrote it. + // Warn only when the version is cleared, as install does. + if refreshErr == nil || rec.Version == version { + rec.Version = version + } else { + warnLocalMarketplaceCopy(ctx, agent, recordedMarketplace(agent, rec), "rerun `databricks aitools update` later", refreshErr) + rec.Version = "" + } state.Plugins[name] = rec - updated = append(updated, PluginUpdate{Agent: agent.DisplayName, Version: version}) + updated = append(updated, PluginUpdate{Agent: agent.DisplayName, Version: rec.Version}) } if len(updated) > 0 {