Files
skillshare/internal/plugin/update_batch.go
T
Krishnaswamy Subramanian eb9b2b28f9 feat(plugin): add --all flag to plugin update and enable antigravity-cli updates (#656)
* Add --all flag to plugin update and enable agy

Add -a/--all to skillshare plugin update so users can update all
tracked plugins in a single command without tying slow target CLI runs
to skillshare update --all or sync --all.

- Reject --all outside plugin update or when combined with a plugin
  name or --source-ref
- Share source repository discoveries and discovery errors across the
  Preview and Apply lifecycle via Service.WithDiscoveryCache so multi-
  plugin marketplaces are cloned at most once per command
- Isolate source, manifest, and target failures per binding when --all
  is active so healthy plugins update while partial failures exit
  non-zero and leave failed bindings unchanged in config.yaml
- Skip unfiltered OMP bindings during --all updates so sibling targets
  on the same package still update without failing the batch
- Enable plugin update for antigravity-cli while skipping disabled
  plugins when agy extensions list reports an entry as disabled

Closes #653

* Add plugin update --all to docs and completions

Advertise -a/--all for skillshare plugin across all five shell
completion scripts and document the bulk update workflow and
antigravity-cli update support in the user and built-in skill docs.

- Add --all and -a to plugin completions in bash, zsh, fish,
  PowerShell, and Nushell with TestPluginCompletionsIncludeAllFlag
- Document update [NAME | --all], -a/--all, and antigravity-cli source
  updates in website/docs/reference/commands/plugin.md (and its
  zh-Hans, zh-Hant, ja, and ko translations), sharing-plugins.md, and
  quick-reference.md
- Document plugin update --all and antigravity-cli enablement checks in
  skills/skillshare/references/plugins.md

* Require known enabled state for native updates

Require a matching EnabledKnown && Enabled inventory entry before
updating copilot or antigravity-cli bindings, and align the localized
plugin workflow pages with the English docs.

- Skip copilot and antigravity-cli updates in Service.Preview and
  refuse reinstallation in applyNativeTarget when the binding is absent
  from native inventory or not known to be enabled
- Cover absent native inventory in
  TestAntigravityCLIUpdateRespectsEnablement
- Synchronize plugin update [name | --all] and Antigravity CLI update
  guidance across the ja, ko, zh-Hans, and zh-Hant copies of
  quick-reference.md and sharing-plugins.md
2026-10-27 20:57:43 +08:00

109 lines
3.5 KiB
Go

package plugin
import (
"context"
"errors"
)
// ErrSourceMoved means a source changed after the check the user approved: nothing was
// applied, and only a new check can show what an update would bring.
var ErrSourceMoved = errors.New("source changed since the check; check again")
// Seen is what a check showed for one Agent of a plugin.
type Seen struct {
Version string `json:"version"`
Commit string `json:"commit,omitempty"`
Digest string `json:"digest,omitempty"`
}
// movedTo says whether the source an update would install is not the one the check showed.
func (seen Seen) movedTo(b Binding) bool {
// An npm package (no source) has no reviewed snapshot to move: Pi installs whatever npm names latest.
if b.Source == "" {
return false
}
// A local directory has no commit; its digest is what moves.
return b.Version != seen.Version || seen.Commit != "" && b.Commit != seen.Commit || seen.Digest != "" && b.Digest != seen.Digest
}
// UpdateItem is one plugin of a batch update and, per Agent, what the check showed.
type UpdateItem struct {
Name string `json:"name"`
Targets []string `json:"targets"`
Seen map[string]Seen `json:"seen,omitempty"`
}
// Batch is a batch update and how its caller follows it.
type Batch struct {
Items []UpdateItem
// Stopped is asked before each plugin; the one under way always finishes.
Stopped func() bool
// Around runs each plugin's update, so the caller can hold its own lock for just that long.
Around func(update func())
Started func(name string)
Planned func(name string, p *Plan)
Ended func(name string, p *Plan, result *Result, err error)
}
// WithDiscoveryCache shares source discovery results and errors across all Preview and
// Apply calls made inside fn, so a single CLI command reads each remote source once.
func (s *Service) WithDiscoveryCache(fn func() error) error {
s.discoveries = map[string]*Discovery{}
s.discoveryErrs = map[string]error{}
defer func() {
s.discoveries = nil
s.discoveryErrs = nil
}()
return fn()
}
// UpdateBatch updates b.Items one after another under the Lock its caller holds, so one
// failure stops nothing else, and returns how many did not complete. Plugins from one
// source share a single read of it for this batch only.
func (s *Service) UpdateBatch(ctx context.Context, b Batch) (failed int) {
s.discoveries = map[string]*Discovery{}
s.discoveryErrs = map[string]error{}
defer func() {
s.discoveries = nil
s.discoveryErrs = nil
}()
for _, item := range b.Items {
if b.Stopped() {
break
}
b.Started(item.Name)
var p *Plan
var result *Result
var err error
b.Around(func() { p, result, err = s.updateLocked(ctx, item, func(p *Plan) { b.Planned(item.Name, p) }) })
if err != nil {
failed++
}
b.Ended(item.Name, p, result, err)
}
return failed
}
// updateLocked previews and applies one plugin's update. planned sees the plan before it
// is applied. A blocked plan is returned with the error that stops it.
func (s *Service) updateLocked(ctx context.Context, item UpdateItem, planned func(*Plan)) (*Plan, *Result, error) {
r := Request{Action: "update", Name: item.Name, Targets: item.Targets}
p, err := s.Preview(ctx, r)
if err != nil {
return nil, nil, err
}
for _, c := range p.Changes {
if c.Action != "update" {
continue
}
// An Agent no check showed has no version the user approved.
seen, ok := item.Seen[c.Target]
if !ok || seen.movedTo(c.Binding) {
return p, nil, ErrSourceMoved
}
}
planned(p)
result, err := s.applyPlan(ctx, r, p)
return p, result, err
}