Files
skillshare/internal/server/handler_plugin.go
T
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

293 lines
9.4 KiB
Go

package server
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"time"
"skillshare/internal/config"
"skillshare/internal/plugin"
)
// pluginServiceSnapshot builds the plugin service under s.mu.RLock and releases it.
// Reads that run Agent CLIs or reach the network use it: holding s.mu through them
// would stall every other API request, since each one reloads config under s.mu.Lock.
func (s *Server) pluginServiceSnapshot() *plugin.Service {
s.mu.RLock()
defer s.mu.RUnlock()
return s.pluginService()
}
func (s *Server) pluginService() *plugin.Service {
service := &plugin.Service{ConfigPath: s.configPath(), ProjectRoot: s.projectRoot, StateDir: config.StateDir()}
if s.IsProjectMode() {
service.GlobalConfigPath = config.ConfigPath()
} else {
service.Accounts = pluginAccounts(s.cfg)
}
return service
}
// pluginAccounts are the configured targets that are another config directory of an
// Agent, such as a second Claude account. They are plugin targets of their own.
func pluginAccounts(cfg *config.Config) map[string]plugin.Account {
accounts := map[string]plugin.Account{}
for name, target := range cfg.Targets {
if target.Agent != "" && target.ConfigDir != "" {
accounts[name] = plugin.Account{Agent: target.Agent, Dir: target.ConfigDir, CLI: target.CLI}
}
}
return accounts
}
func (s *Server) requireLocalPlugin(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !s.mcpRequestAllowed(r) {
writeError(w, http.StatusForbidden, "Plugin management is available only from the local dashboard")
return
}
// Native downloads can exceed the dashboard's normal 30-second deadline.
// Limit this extension to the local plugin endpoints.
_ = http.NewResponseController(w).SetWriteDeadline(time.Now().Add(5 * time.Minute))
next(w, r)
}
}
type pluginRequest struct {
Request plugin.Request `json:"request"`
Revision string `json:"revision,omitempty"`
}
func decodePluginRequest(w http.ResponseWriter, r *http.Request, v any) bool {
err := decodeJSONWith(w, r, defaultJSONBodyLimit, func(d *json.Decoder) error {
d.DisallowUnknownFields()
if err := d.Decode(v); err != nil {
return err
}
if err := d.Decode(new(any)); err != io.EOF {
return errors.New("expected a single JSON document")
}
return nil
})
if err != nil {
if !errors.Is(err, errBodyTooLarge) {
writeError(w, 400, "invalid plugin request")
}
return false
}
return true
}
func (s *Server) handlePluginList(w http.ResponseWriter, r *http.Request) {
service := s.pluginServiceSnapshot()
// hosts=false skips the Agent CLIs: the page draws the plugin list from this answer
// and fills in the Agents when the full one arrives.
var inventory *plugin.Inventory
var err error
if r.URL.Query().Get("hosts") == "false" {
inventory, err = service.Packages()
} else {
inventory, err = service.Inventory(r.Context())
}
if err != nil {
writeError(w, 400, err.Error())
return
}
writeJSON(w, inventory)
}
func (s *Server) handlePluginDiscover(w http.ResponseWriter, r *http.Request) {
// Name is set for a plugin already managed here, whose snapshot can answer instead of the network.
var body struct {
Name string `json:"name,omitempty"`
Source string `json:"source"`
SourceRef string `json:"sourceRef,omitempty"`
Entry string `json:"entry,omitempty"`
}
if !decodePluginRequest(w, r, &body) {
return
}
result, err := s.pluginServiceSnapshot().DiscoverManaged(r.Context(), body.Name, body.Source, body.SourceRef, body.Entry)
if err != nil {
// A fixed failure carries its key as the error code, so the dashboard can translate it.
if key, args := plugin.ErrorKey(err); key != "" {
writeCodedError(w, 400, key, err.Error(), args)
return
}
writeError(w, 400, err.Error())
return
}
writeJSON(w, result)
}
func (s *Server) handlePluginPreview(w http.ResponseWriter, r *http.Request) {
var body pluginRequest
if !decodePluginRequest(w, r, &body) {
return
}
p, err := s.pluginServiceSnapshot().Preview(r.Context(), body.Request)
if err != nil {
if key, args := plugin.ErrorKey(err); key != "" {
writeCodedError(w, 400, key, err.Error(), args)
return
}
writeError(w, 400, err.Error())
return
}
writeJSON(w, p)
}
func (s *Server) handlePluginApply(w http.ResponseWriter, r *http.Request) {
var body pluginRequest
if !decodePluginRequest(w, r, &body) {
return
}
if body.Revision == "" {
writeError(w, 400, "preview before applying plugin changes")
return
}
s.mu.Lock()
defer s.mu.Unlock()
start := time.Now()
result, err := s.pluginService().Apply(r.Context(), body.Request, body.Revision)
status := "ok"
message := ""
if err != nil {
status = "error"
message = err.Error()
}
s.writeOpsLog("plugin "+body.Request.Action, status, start, map[string]any{"scope": "ui"}, "")
if reloadErr := s.reloadConfig(); reloadErr != nil && err == nil {
message = "Plugin operation completed, but configuration reload failed"
}
// Preserve all per-target outcomes even when only some targets succeed.
writeJSON(w, map[string]any{"result": result, "failure": message})
}
// handlePluginUpdateStream updates plugins one after another under one plugin lock and streams each:
// - "start" → {"name"} a plugin begins
// - "plan" → {"name","plan"} what its Agents are about to do
// - "result" → {"name","plan","result","failure","stale"} how it ended; stale means its source moved since the check
// - "done" → {}
//
// Plugins from one source share a single read of it, and config reloads once at the end.
func (s *Server) handlePluginUpdateStream(w http.ResponseWriter, r *http.Request) {
var body struct {
Items []plugin.UpdateItem `json:"items"`
}
if !decodePluginRequest(w, r, &body) {
return
}
service := s.pluginServiceSnapshot()
unlock, err := service.Lock()
if err != nil {
writeError(w, http.StatusConflict, err.Error())
return
}
defer unlock()
send, ok := initSSE(w)
if !ok {
return
}
s.pluginStop.Store(false)
start := time.Now()
// A plugin under way finishes even if the page goes away; the next one does not start.
ctx := context.WithoutCancel(r.Context())
failed := service.UpdateBatch(ctx, plugin.Batch{
Items: body.Items,
Stopped: func() bool { return s.pluginStop.Load() || r.Context().Err() != nil },
Around: func(update func()) {
s.mu.Lock()
defer s.mu.Unlock()
update()
},
Started: func(name string) { send("start", map[string]string{"name": name}) },
Planned: func(name string, p *plugin.Plan) { send("plan", map[string]any{"name": name, "plan": p}) },
Ended: func(name string, p *plugin.Plan, result *plugin.Result, err error) {
out := map[string]any{"name": name, "plan": p, "result": result, "failure": "", "stale": errors.Is(err, plugin.ErrSourceMoved)}
if err != nil {
out["failure"] = err.Error()
}
send("result", out)
},
})
s.mu.Lock()
status, message := "ok", ""
if failed > 0 {
status, message = "partial", fmt.Sprintf("%d plugin update(s) did not complete", failed)
}
s.writeOpsLog("plugin update", status, start, map[string]any{"scope": "ui", "plugins": len(body.Items)}, message)
failure := ""
if err := s.reloadConfig(); err != nil {
failure = "Plugin update completed, but configuration reload failed"
}
s.mu.Unlock()
send("done", map[string]string{"failure": failure})
}
// pluginCheckTimeout bounds an update check, so an Agent CLI or source that does not answer
// fails its own plugins in time instead of keeping the page waiting.
const pluginCheckTimeout = 60 * time.Second
// handlePluginCheckStream checks every plugin at once and sends each plugin's plan as soon as it
// is ready, so the page marks rows while slower plugins are still being checked.
func (s *Server) handlePluginCheckStream(w http.ResponseWriter, r *http.Request) {
send, ok := initSSE(w)
if !ok {
return
}
ctx, cancel := context.WithTimeout(r.Context(), pluginCheckTimeout)
defer cancel()
err := s.pluginServiceSnapshot().Check(ctx, plugin.Request{Action: "check"}, func(name string, p *plugin.Plan, err error) {
out := map[string]any{"name": name, "plan": p}
if err != nil {
code, params := plugin.ErrorKey(err)
out["error"], out["error_code"], out["error_params"] = err.Error(), code, params
}
send("plan", out)
})
failure := ""
if err != nil {
failure = err.Error()
}
send("done", map[string]string{"failure": failure})
}
// handlePluginUpdateStop lets the plugin under way finish and starts no other.
func (s *Server) handlePluginUpdateStop(w http.ResponseWriter, _ *http.Request) {
s.pluginStop.Store(true)
writeJSON(w, map[string]bool{"stopping": true})
}
// handlePluginFiles lists the reviewed snapshot of a plugin; an imported one has none and lists nothing.
func (s *Server) handlePluginFiles(w http.ResponseWriter, r *http.Request) {
s.mu.RLock()
defer s.mu.RUnlock()
files, err := s.pluginService().Files(r.PathValue("name"))
if err != nil {
writeError(w, 400, err.Error())
return
}
writeJSON(w, map[string]any{"files": files})
}
func (s *Server) handlePluginFile(w http.ResponseWriter, r *http.Request) {
s.mu.RLock()
defer s.mu.RUnlock()
data, err := s.pluginService().ReadFile(r.PathValue("name"), r.PathValue("filepath"))
if errors.Is(err, fs.ErrNotExist) {
writeError(w, http.StatusNotFound, "file not found: "+r.PathValue("filepath"))
return
}
if err != nil {
writeError(w, 400, err.Error())
return
}
writeJSON(w, map[string]any{"content": string(data)})
}