From 19cdc1c72e4eb46fe59f254e14ba3a8fd9b1e520 Mon Sep 17 00:00:00 2001 From: Willie Date: Mon, 12 Oct 2026 17:04:52 +0800 Subject: [PATCH] fix(plugin): let an imported Claude plugin update to what its marketplace holds (#536) An imported plugin's native marketplace decides the version an update installs, so the update check now applies only to bindings with a reviewed source and an import records the version installed. Importing an imported plugin again also records the installed version; a source binding keeps the version Skillshare reviewed. Refs #535 --- internal/plugin/apply.go | 21 +++++---- internal/plugin/lifecycle.go | 2 +- internal/plugin/service.go | 4 ++ internal/plugin/service_test.go | 81 +++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 10 deletions(-) diff --git a/internal/plugin/apply.go b/internal/plugin/apply.go index a8f070911..0d55f388b 100644 --- a/internal/plugin/apply.go +++ b/internal/plugin/apply.go @@ -132,13 +132,14 @@ func (s *Service) materialize(ctx context.Context, b Binding, target string) (st return expected, nil } -func (s *Service) applyChange(ctx context.Context, c Change, b Binding) (resultErr error) { +// applyChange records in b the version an imported plugin's update installed. +func (s *Service) applyChange(ctx context.Context, c Change, b *Binding) (resultErr error) { if b.Source != "" && (c.Action == "install" || c.Action == "update") { - if _, err := os.Stat(s.snapshotPath(b, c.Target) + ".previous"); err == nil { + if _, err := os.Stat(s.snapshotPath(*b, c.Target) + ".previous"); err == nil { return fmt.Errorf("unfinished snapshot replacement; inspect previous snapshot before retrying") } defer func() { - path := s.snapshotPath(b, c.Target) + path := s.snapshotPath(*b, c.Target) backup := path + ".previous" if _, err := os.Stat(backup); err != nil { return @@ -172,7 +173,7 @@ func (s *Service) applyChange(ctx context.Context, c Change, b Binding) (resultE } agent := s.agentOf(c.Target) if !marketplaceAgent(agent) { - return s.applyAdditional(ctx, c, b) + return s.applyAdditional(ctx, c, *b) } if agent == "omp" && c.Action == "install" { if err := ompEmptyRuntime(c.ompRuntimePath); err != nil { @@ -180,7 +181,7 @@ func (s *Service) applyChange(ctx context.Context, c Change, b Binding) (resultE } } if (c.Action == "install" || c.Action == "update") && b.Source != "" { - path, err := s.materialize(ctx, b, c.Target) + path, err := s.materialize(ctx, *b, c.Target) if err != nil { return err } @@ -221,7 +222,7 @@ func (s *Service) applyChange(ctx context.Context, c Change, b Binding) (resultE if (c.Action == "remove" || c.Action == "uninstall") && b.Source != "" { // Removed natively already: only the marketplace Skillshare registered is left. if h := s.host(ctx, c.Target); h.Error == "" && !slices.ContainsFunc(h.Installed, func(i Installed) bool { return i.ID == b.ID }) { - return s.removeMarketplace(ctx, c.Target, b) + return s.removeMarketplace(ctx, c.Target, *b) } } args, err := s.nativeArgs(c.Target, c.Action, b.ID) @@ -256,15 +257,17 @@ func (s *Service) applyChange(ctx context.Context, c Change, b Binding) (resultE if c.Action == "disable" && item.Enabled { return fmt.Errorf("plugin is still enabled") } - // An upgraded marketplace sets its own version, which Skillshare never reviewed. - if c.Action == "update" && !upgrade && b.Version != "" && item.Version != b.Version { + // An import updates to whatever its native marketplace holds, which Skillshare never reviewed. + if c.Action == "update" && b.Source == "" { + b.Version = item.Version + } else if c.Action == "update" && b.Version != "" && item.Version != b.Version { return fmt.Errorf("native update returned version %s; expected %s", item.Version, b.Version) } return nil } } if c.Action == "remove" || c.Action == "uninstall" { - return s.removeMarketplace(ctx, c.Target, b) + return s.removeMarketplace(ctx, c.Target, *b) } return fmt.Errorf("native operation completed but plugin was not found; inspect the native client before retrying") } diff --git a/internal/plugin/lifecycle.go b/internal/plugin/lifecycle.go index c6d181eb9..5a8577be4 100644 --- a/internal/plugin/lifecycle.go +++ b/internal/plugin/lifecycle.go @@ -128,7 +128,7 @@ func (s *Service) applyPlan(ctx context.Context, r Request, p *Plan) (*Result, e } b := d.packages[c.Name].Bindings[c.Target] c.piRestores = piRestores - applyErr := s.applyChange(ctx, c, b) + applyErr := s.applyChange(ctx, c, &b) outcome := Outcome{Name: c.Name, Target: c.Target, Status: "installed", Message: "Native installation recorded. Reload the Agent and complete any required login or hook trust."} if applyErr != nil { outcome.Status = "failed" diff --git a/internal/plugin/service.go b/internal/plugin/service.go index 9e52e592d..fd9a837b6 100644 --- a/internal/plugin/service.go +++ b/internal/plugin/service.go @@ -399,6 +399,10 @@ func (s *Service) Preview(ctx context.Context, r Request) (*Plan, error) { if old.ID != b.ID { return nil, fmt.Errorf("package already manages a different plugin") } + // An import follows what is installed; a source binding keeps the version Skillshare reviewed. + if old.Source == "" { + old.Version = installed.Version + } b = old } change := Change{Name: name, Target: r.From, ID: b.ID, Binding: b, Action: "import", Message: "Record the existing native installation; leave its content and enabled state unchanged."} diff --git a/internal/plugin/service_test.go b/internal/plugin/service_test.go index 43ed7e901..34fb3cb5e 100644 --- a/internal/plugin/service_test.go +++ b/internal/plugin/service_test.go @@ -805,6 +805,87 @@ func TestCodexUpdateOfAnImportUpgradesItsMarketplace(t *testing.T) { } } +// importedDemo imports demo@team from Claude into a package that also has a source, as when +// another Agent was added from it, and leaves the Claude update pending at 1.0.0. +func importedDemo(t *testing.T) (*Service, *fakeAgents) { + t.Helper() + agents := &fakeAgents{version: "2.0.0", installed: map[string][]Installed{"claude": {{ID: "demo@team", PluginID: "demo@team", Installed: true, Enabled: true, Version: "1.0.0", Scope: "user"}}}, markets: map[string]map[string]string{"claude": {"team": "/team"}}} + s := agents.service(t) + applyPluginRequest(t, s, Request{Action: "import", From: "claude", Plugin: "demo@team"}) + cfg, err := s.load() + if err != nil { + t.Fatal(err) + } + pack := cfg.packages["demo"] + pack.Source = "https://example.com/demo" + b := pack.Bindings["claude"] + b.Pending = "update" + pack.Bindings["claude"] = b + cfg.packages["demo"] = pack + if err := s.save(cfg); err != nil { + t.Fatal(err) + } + return s, agents +} + +func loadBinding(t *testing.T, s *Service) Binding { + t.Helper() + cfg, err := s.load() + if err != nil { + t.Fatal(err) + } + return cfg.packages["demo"].Bindings["claude"] +} + +// An imported plugin updates to whatever its native marketplace now holds, and records it. +func TestClaudeUpdateOfAnImportRecordsTheVersionItInstalled(t *testing.T) { + s, _ := importedDemo(t) + applyPluginRequest(t, s, Request{Action: "update", Name: "demo", Targets: []string{"claude"}}) + if b := loadBinding(t, s); b.Version != "2.0.0" || b.Pending != "" { + t.Fatalf("binding: %+v", b) + } +} + +// The dashboard's batch update takes the same path for an import. +func TestBatchUpdateOfAnImportRecordsTheVersionItInstalled(t *testing.T) { + s, _ := importedDemo(t) + unlock, err := s.Lock() + if err != nil { + t.Fatal(err) + } + defer unlock() + if _, err := updateDemo(s, map[string]Seen{"claude": {Version: "1.0.0"}}); err != nil { + t.Fatal(err) + } + if b := loadBinding(t, s); b.Version != "2.0.0" { + t.Fatalf("binding: %+v", b) + } +} + +// Importing again records the version the native client has now, not the one first imported. +func TestImportAgainRecordsTheInstalledVersion(t *testing.T) { + agents := &fakeAgents{installed: map[string][]Installed{"claude": {{ID: "demo@team", PluginID: "demo@team", Installed: true, Enabled: true, Version: "1.0.0", Scope: "user"}}}, markets: map[string]map[string]string{"claude": {"team": "/team"}}} + s := agents.service(t) + applyPluginRequest(t, s, Request{Action: "import", From: "claude", Plugin: "demo@team"}) + agents.installed["claude"][0].Version = "2.0.0" + applyPluginRequest(t, s, Request{Action: "import", From: "claude", Plugin: "demo@team"}) + if cfg, _ := s.load(); cfg.packages["demo"].Bindings["claude"].Version != "2.0.0" { + t.Fatalf("binding: %+v", cfg.packages["demo"].Bindings["claude"]) + } +} + +// A binding added from a source keeps the version Skillshare reviewed when imported again. +func TestImportAgainKeepsTheReviewedVersion(t *testing.T) { + agents := &fakeAgents{version: "1.0.0"} + s := agents.service(t) + applyPluginRequest(t, s, Request{Action: "add", Source: fixture(t), Targets: []string{"claude"}}) + agents.installed["claude"][0].Version = "native-1" + applyPluginRequest(t, s, Request{Action: "import", From: "claude", Plugin: agents.installed["claude"][0].ID, Name: "demo"}) + if b := loadBinding(t, s); b.Version != "1.0.0" { + t.Fatalf("binding: %+v", b) + } +} + // A skipped update stays pending, so a later sync still knows the plugin is behind. func TestSkippedUpdateStaysPending(t *testing.T) { agents := &fakeAgents{version: "1.0.0"}