Files
skillshare/internal/plugin/lifecycle.go
Willie 7f3b5e7b1e fix(plugin): clean up and explain Skillshare marketplaces (#321)
Managed Claude/Codex marketplaces were named skillshare-<hash>, which
says nothing in native marketplace lists, and they stayed registered
after the plugin was excluded or removed. Cleanup ran only after a
native uninstall, so a plugin already removed in the Agent, a failed
first install, or a failed cleanup left the marketplace behind for good.

Name new marketplaces skillshare-<plugin>-<hash>, and remove the one
Skillshare registered whenever its plugin is excluded or removed, also
when the plugin is already gone. Cleanup checks that the marketplace is
registered from Skillshare's own state directory, re-lists afterwards,
and stays pending for the next sync on failure or while the marketplace
list cannot be read. An update registers a missing managed marketplace
again.

An imported plugin reinstalls from its native marketplace. When that
marketplace is gone (for example when synced settings dropped it), sync
failed with a generic "command failed". Skip that target with the reason
instead, so other Agents still sync. Skillshare does not move an import
to its recorded source on its own: that would install a distribution
and version nobody reviewed for that Agent.

Failed outcomes now carry their translation key, and the dashboard
shows each distinct failure once instead of the joined English text.

Validation: make check and the full UI vitest suite in the devcontainer,
tsc and eslint; the new Go tests fail without the change. Claude and
Codex marketplace removal was checked against the real CLIs in
isolated config directories.

Release-As: 0.23.3
2026-10-02 05:08:30 +08:00

152 lines
5.0 KiB
Go

package plugin
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
)
func (s *Service) Apply(ctx context.Context, r Request, revision string) (*Result, error) {
if revision == "" {
return nil, fmt.Errorf("preview the plugin changes before applying")
}
// Serialize CLI and UI mutations across processes, not just HTTP requests.
if err := os.MkdirAll(filepath.Dir(s.ConfigPath), 0755); err != nil {
return nil, err
}
lock, err := os.OpenFile(s.ConfigPath+".plugins.lock", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
return nil, fmt.Errorf("another plugin operation is running (lock: %s.plugins.lock)", s.ConfigPath)
}
_ = lock.Close()
defer os.Remove(s.ConfigPath + ".plugins.lock")
p, err := s.Preview(ctx, r)
if err != nil {
return nil, err
}
if p.Revision != revision {
return nil, fmt.Errorf("plugin state changed since preview; preview again")
}
if p.Blocked {
return nil, fmt.Errorf("resolve blocked plugin targets before applying")
}
if r.Action == "check" {
return nil, fmt.Errorf("check is read-only")
}
d, err := s.load()
if err != nil {
return nil, err
}
result := &Result{Results: []Outcome{}}
changed := false
for _, c := range p.Changes {
if c.Action == "skip" {
// Unlike noop, a skipped update stays pending for a later sync.
result.Results = append(result.Results, Outcome{Name: c.Name, Target: c.Target, Status: "skipped", Message: c.Message, MessageKey: c.MessageKey, MessageArgs: c.MessageArgs})
continue
}
if c.Action == "noop" {
result.Results = append(result.Results, Outcome{Name: c.Name, Target: c.Target, Status: "unchanged"})
if r.Action == "sync" && d.packages[c.Name].Bindings[c.Target].Pending != "" {
changed = true
pack := d.packages[c.Name]
b := pack.Bindings[c.Target]
b.Pending = ""
pack.Bindings[c.Target] = b
d.packages[c.Name] = pack
}
continue
}
changed = true
pack := d.packages[c.Name]
if pack.Bindings == nil {
pack.Bindings = map[string]Binding{}
}
if c.Target == "" {
if c.Action == "forget" {
delete(d.packages, c.Name)
} else {
pack.Source, pack.SourceRef, pack.Plugin, pack.Entry, pack.Version = c.Binding.Source, c.Binding.SourceRef, c.Binding.Plugin, c.Binding.Entry, c.Binding.Version
d.packages[c.Name] = pack
}
continue
}
if r.Action == "add" && pack.Source == "" && c.Binding.Source != "" {
pack.Source, pack.SourceRef, pack.Plugin, pack.Entry = c.Binding.Source, c.Binding.SourceRef, c.Binding.Plugin, r.Entry
}
b := c.Binding
b.Pending = c.Action
if c.Action == "import" || c.Action == "selection" {
b.Pending = ""
}
pack.Bindings[c.Target] = b
d.packages[c.Name] = pack
}
if !changed {
return result, nil
}
if err := s.save(d); err != nil {
return result, err
}
var failures []error
for _, c := range p.Changes {
if c.Action == "noop" || c.Action == "skip" {
continue
}
if c.Target == "" {
outcome := Outcome{Name: c.Name, Status: "saved", Message: "Added to Skillshare. Choose Agents when you want to install it."}
if c.Action == "forget" {
outcome.Status, outcome.Message = "removed", "Removed from Skillshare."
}
result.Results = append(result.Results, outcome)
continue
}
b := d.packages[c.Name].Bindings[c.Target]
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"
outcome.Message = applyErr.Error()
outcome.MessageKey, outcome.MessageArgs = ErrorKey(applyErr)
failures = append(failures, applyErr)
} else {
pack := d.packages[c.Name]
if c.Action == "remove" || c.Action == "forget" {
delete(pack.Bindings, c.Target)
outcome.Status = "removed"
outcome.Message = "Native plugin removed; shared marketplaces are retained."
} else {
b.Pending = ""
pack.Bindings[c.Target] = b
if c.Action == "selection" {
outcome.Status = "saved"
outcome.Message = "Sync selection saved. Run sync to apply installation changes."
}
if c.Action == "uninstall" {
outcome.Status = "excluded"
outcome.Message = "Removed from this target; plugin definition retained."
}
if c.Action == "import" {
outcome.Status = "imported"
outcome.Message = "Existing installation adopted without changing its enabled state."
}
}
// A package with a recorded source stays managed without Agents until it is removed whole.
if len(pack.Bindings) == 0 && (pack.Source == "" || (r.Action == "remove" && len(r.Targets) == 0)) {
delete(d.packages, c.Name)
} else {
d.packages[c.Name] = pack
}
if err := s.save(d); err != nil {
outcome.Status = "failed"
outcome.Message = "Native operation completed, but recording its result failed; inspect status before retrying."
failures = append(failures, err)
}
}
result.Results = append(result.Results, outcome)
}
return result, errors.Join(failures...)
}