Files
skillshare/internal/plugin/update_batch.go
Willie 2bc791ed19 perf(plugin): check every plugin at once and skip sources that did not move (#812)
* perf(plugin): check every plugin at once and skip sources that did not move

The Plugins page's update check read every Agent CLI, cloned and hashed
every source, and asked npm about every package one after another, so it
grew with each plugin added. A single Agent CLI that stopped answering
held the whole page at "Checking for updates" with no way to tell which
plugin was stuck.

The check now asks every Agent and checks every plugin at the same time.
Plugins share one read of each Agent and each source, so an Agent with
many plugins is still asked once, and a plugin waits only on its own
Agents, including when proving its own leftover installs. Before downloading a remote source with no pinned ref, the
check asks the remote which commit its default branch points at with
`git ls-remote` and reports no update when that is the installed commit.
A check of three unchanged GitHub plugins on two Agents went from 4.3s
with three clones to 0.6s with none.

The dashboard reads the check from a new stream, GET
/api/plugins/check/stream, that sends each plugin as soon as it is
checked. The page marks rows as they arrive and counts progress in the
header. The stream stops waiting after 60 seconds: a plugin bound to an
Agent that has not answered by then shows "Check failed" with the
reason, and plugins on other Agents are unaffected. There is no limit on
how many plugins run together, because plugins waiting on a hung Agent
would otherwise hold every slot and starve the rest.

A cancelled command did not return when the Agent CLI was a shim that
starts the real client, such as an npm, mise or volta wrapper: killing
the shim left the child holding its output, so the command waited until
the child exited and no timeout took effect. Commands now stop waiting
for output two seconds after they are cancelled; a command that exits on
its own while a helper keeps its output open still counts as answered.

* fix(ui): offer every update the Plugins page shows in the update dialog

The header counted updates from the per-plugin marks, but the update
dialog offered only the targets in the last completed check's plan. Two
cases left the header saying Update N with nothing to tick in the dialog:
a first check that broke off after finding an update never set the plan,
and a recheck that could not reach an Agent kept that Agent's earlier
update while the plan said blocked.

The dialog's plan is now derived from each plugin's last answer together
with the kept updates, so a kept update stands in for the blocked or
missing answer and both always agree. A broken-off check also keeps the
answers it did receive.
2026-11-09 15:06:55 +08:00

112 lines
3.9 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 {
ID string `json:"id,omitempty"`
Source string `json:"source,omitempty"`
SourceRef string `json:"sourceRef,omitempty"`
Plugin string `json:"plugin,omitempty"`
Entry string `json:"entry,omitempty"`
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 {
return !seen.matchesRecipe(b)
}
func (seen Seen) matchesRecipe(b Binding) bool {
if b.Source == "" {
// Native and npm updates identify the installation; npm still installs its latest version.
return seen.ID != "" && seen.ID == b.ID && seen.Source == "" && seen.SourceRef == "" && seen.Plugin == "" && seen.Entry == ""
}
// The digest identifies the tree, while the recipe identifies the candidate within it.
return seen.Source != "" && seen.Digest != "" && seen.Source == b.Source && seen.SourceRef == b.SourceRef &&
seen.Plugin == b.Plugin && seen.Entry == b.Entry && seen.Version == b.Version &&
seen.Commit == b.Commit && seen.Digest == b.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.reads = &reads{}
defer func() { s.reads = 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.reads = &reads{}
defer func() { s.reads = 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" && c.Action != "install" {
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
}