plugins: a GitHub list that couldn't be read says why with Try again, instead of the section vanishing (till on Discord)

till (Windows, v0.1.1293, Belarus network) saw no "Unofficial plugins ·
GitHub" section in Plugins › Discover; a magpie in Japan shows it. On
v0.1.1293 the section shows its loading skeleton and then disappears
whenever GitHub can't be read: plugin.TaggedRepos returned the last list
(or nothing) with no error when the topic search failed, and an empty
list with no error when the search answered but every package.json read
from raw.githubusercontent.com failed or timed out. /api/plugins/github
answered 200 {repos:[]}, and the page showed ghFailed only on an HTTP
error, never in a search, and never once a list had been had.

Now:
- TaggedRepos returns the error with what it has. A failed search keeps
  the last list and says why (held a minute, like before); repositories
  the search found but that couldn't be read come back as a
  *TaggedMissing with their count and the last read error.
- /api/plugins/github sends error and missing beside repos; ?again asks
  GitHub again at once when the last answer had an error.
- The page shows the reason and Try again: in place of the section when
  nothing could be read (in Discover and in a search), and as a line over
  the cards when some were read or an older list is kept. Try again keeps
  the cards while it asks. zh, zh-TW, ja and de strings added.

No Windows-only cause was found in the code: the fetch goes through the
same netproxy on every OS (a PAC-only system proxy isn't read anywhere).
The likely cause is the network reaching api.github.com or
raw.githubusercontent.com, or the unauthenticated search limit; the page
now names which.

Run (worktree on ef655acb4, sandbox HOME):
- gofmt -l clean; go vet darwin, linux -tags nogui, windows ok; builds ok.
- go test ./internal/plugin ./internal/gui: ok. Broken by hand (failure
  path returning nil error; missing ignored): TestTaggedRepos ("down:
  <nil>") and TestTaggedReposUnreadable go red; restored, green.
- plugin-github-wait.test.cjs: 18/18 in chromium and webkit, every
  language and width; against the old assets it times out waiting for
  the reason (red). After the rebase: once 16/18 and once 17/18 with
  page.goto's 5s load timing out at load average ~280, then 18/18.
- Whole GUI suite, both engines: all pass except codex-predictions at
  420 (7 cases: Settings' stuck search head takes the click), already
  #2624, untouched by this change.
- set -o pipefail; go test -timeout 45m ./...: every package ok except
  internal/agent, which hit the 45m timeout at load average ~200 with
  other sessions' suites (2769s). Rerun after the rebase with
  internal/plugin and internal/gui: plugin ok (973s), gui ok (550s),
  agent timed out again (2704s, load ~400). Recorded on #2723; this
  change touches nothing in internal/agent.

Not tried on Windows or from the reporter's network.
This commit is contained in:
yetone
2026-10-20 02:35:14 +08:00
parent 5e30d5b5ae
commit fb101b0d57
7 changed files with 336 additions and 66 deletions
+95 -28
View File
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path"
@@ -78,6 +79,10 @@ var (
taggedMu sync.Mutex
taggedL []Tagged
taggedAt time.Time
// taggedWhy is why taggedL isn't GitHub's whole answer: the search
// failed (taggedL is what it said before), or repositories it named
// couldn't be read. nil when it is.
taggedWhy error
// owner/repo@pushed → what its package.json and npm made of it, nil
// when it was left out
readMu sync.Mutex
@@ -96,6 +101,20 @@ func taggedOnDisk() []Tagged {
return l
}
// TaggedMissing is the error TaggedRepos gives when GitHub's search
// answered but repositories it named couldn't be read: Missing of them
// aren't in the list, for Why.
type TaggedMissing struct {
Missing int
Why error
}
func (e *TaggedMissing) Error() string {
return fmt.Sprintf("%d repositories tagged %s couldn't be read from GitHub: %v", e.Missing, Topic, e.Why)
}
func (e *TaggedMissing) Unwrap() error { return e.Why }
// TaggedRepos are the repositories tagged with Topic, the most starred
// first: GitHub asked at most every ten minutes (its search lets 10 a minute
// through without a token), else what it said last, kept on disk. Archived
@@ -104,22 +123,29 @@ func taggedOnDisk() []Tagged {
// couldn't install either. A repository whose package.json didn't come in
// time is as the list had it last, and the list is asked again in
// taggedRetry. MAGPIE_PLUGIN_MARKET=off keeps to none.
func TaggedRepos(ctx context.Context) []Tagged {
//
// The error says the list isn't GitHub's whole answer, and why: the search
// failed (the list is what GitHub said before, empty when it never
// answered), or, a *TaggedMissing, repositories it named couldn't be read.
// An empty list is never a failed read passed off as there being none
// (till on Discord: GitHub unreachable from their desktop, and the
// section just wasn't there).
func TaggedRepos(ctx context.Context) ([]Tagged, error) {
taggedMu.Lock()
defer taggedMu.Unlock()
if taggedL != nil && time.Since(taggedAt) < taggedTTL {
return taggedL
return taggedL, taggedWhy
}
if os.Getenv("MAGPIE_PLUGIN_MARKET") == "off" {
return []Tagged{}
return []Tagged{}, nil
}
// a magpie started again within the ten minutes: what GitHub said then,
// without the wait of asking (a package.json read for each)
if taggedL == nil {
if fi, err := os.Stat(taggedCache()); err == nil && time.Since(fi.ModTime()) < taggedTTL {
if l := taggedOnDisk(); l != nil {
taggedL, taggedAt = l, fi.ModTime()
return l
taggedL, taggedAt, taggedWhy = l, fi.ModTime(), nil
return l, nil
}
}
}
@@ -127,33 +153,49 @@ func TaggedRepos(ctx context.Context) []Tagged {
if last == nil {
last = taggedOnDisk()
}
if l, whole, err := askTagged(ctx, last); err == nil {
taggedL, taggedAt = l, time.Now()
if l, whole, missing, err := askTagged(ctx, last); err == nil {
taggedL, taggedAt, taggedWhy = l, time.Now(), nil
if !whole {
taggedAt = time.Now().Add(-taggedTTL + taggedRetry)
}
if missing != nil {
taggedWhy = missing
}
if b, err := json.Marshal(l); err == nil {
_ = os.MkdirAll(filepath.Dir(taggedCache()), 0o755)
_ = os.WriteFile(taggedCache(), b, 0o644)
// a magpie started again meanwhile asks again too
_ = os.Chtimes(taggedCache(), taggedAt, taggedAt)
}
return l
return l, taggedWhy
} else {
// GitHub didn't answer: what it said last, asked again in a
// minute, and why it is that
l := last
if l == nil {
l = []Tagged{}
}
taggedL, taggedAt, taggedWhy = l, time.Now().Add(-taggedTTL+time.Minute), err
return l, err
}
// GitHub didn't answer: what it said last, asked again in a minute
l := last
if l == nil {
l = []Tagged{}
}
// TaggedAskAgain has the next TaggedRepos ask GitHub at once, when what
// stands now isn't its whole answer: the Plugins page's Try again. A list
// GitHub gave whole stands its ten minutes all the same.
func TaggedAskAgain() {
taggedMu.Lock()
defer taggedMu.Unlock()
if taggedWhy != nil {
taggedAt = time.Time{}
}
taggedL, taggedAt = l, time.Now().Add(-taggedTTL+time.Minute)
return l
}
// askTagged asks GitHub's search, and reads each repository it names
// that wasn't read at its last push. whole is false when a repository was
// left out for want of an answer in taggedWait: it is then as last had
// it, if it had it.
func askTagged(ctx context.Context, last []Tagged) (l []Tagged, whole bool, err error) {
// it, if it had it, and missing counts those last didn't have.
func askTagged(ctx context.Context, last []Tagged) (l []Tagged, whole bool, missing *TaggedMissing, err error) {
c, cancel := context.WithTimeout(ctx, taggedWait)
defer cancel()
type item struct {
@@ -182,14 +224,14 @@ func askTagged(ctx context.Context, last []Tagged) (l []Tagged, whole bool, err
"per_page": {strconv.Itoa(taggedPage)}, "page": {strconv.Itoa(page)}}
b, err := fetchJSON(c, githubAPI+"/search/repositories?"+v.Encode(), 4<<20)
if err != nil {
return nil, false, err
return nil, false, nil, err
}
var p struct {
Total int `json:"total_count"`
Items []item `json:"items"`
}
if err := json.Unmarshal(b, &p); err != nil {
return nil, false, err
return nil, false, nil, err
}
for _, it := range p.Items {
// a repository starred between two pages can show on both
@@ -211,8 +253,9 @@ func askTagged(ctx context.Context, last []Tagged) (l []Tagged, whole bool, err
r.URL, r.Owner, r.OwnerAvatar, r.Description, r.Stars, r.License, r.Pushed = t.URL, t.Owner, t.OwnerAvatar, t.Description, t.Stars, t.License, t.Pushed
return r
}
var mu sync.Mutex // out, keep, done; late once the page stops waiting
var mu sync.Mutex // out, keep, done, why; late once the page stops waiting
late := false
var why error // a read GitHub or npm didn't answer, the last one
out := make([]Tagged, len(r.Items))
keep := make([]bool, len(r.Items))
done := make([]bool, len(r.Items))
@@ -255,7 +298,7 @@ func askTagged(ctx context.Context, last []Tagged) (l []Tagged, whole bool, err
defer func() { <-sem }()
pc, cancel := context.WithTimeout(bg, taggedEach)
defer cancel()
got, ok, read := readTagged(pc, t, branch)
got, ok, read, rerr := readTagged(pc, t, branch)
if read {
readMu.Lock()
if ok {
@@ -270,6 +313,9 @@ func askTagged(ctx context.Context, last []Tagged) (l []Tagged, whole bool, err
out[i], keep[i] = got, ok
// a package.json GitHub didn't answer for: not known
done[i] = ok || read
if !done[i] && rerr != nil {
why = rerr
}
}
mu.Unlock()
}(i, t, it.DefaultBranch)
@@ -292,6 +338,7 @@ func askTagged(ctx context.Context, last []Tagged) (l []Tagged, whole bool, err
}
readMu.Unlock()
l, whole = []Tagged{}, true
gone := 0
for i := range out {
switch {
case keep[i]:
@@ -300,23 +347,37 @@ func askTagged(ctx context.Context, last []Tagged) (l []Tagged, whole bool, err
whole = false
if was, ok := before[strings.ToLower(asked[i].Repo)]; ok {
l = append(l, fresh(was, asked[i]))
} else {
gone++
}
}
}
return l, whole, nil
if gone > 0 {
mu.Lock()
w := why
mu.Unlock()
if w == nil {
w = fmt.Errorf("no answer in %s", taggedWait)
}
missing = &TaggedMissing{Missing: gone, Why: w}
}
return l, whole, missing, nil
}
// readTagged reads a tagged repository's package.json (on branch), and
// npm's copy of the package it names: t as it is installed, whether it is
// kept, and whether that is known (GitHub or npm not answering is not,
// and it is read again next time).
func readTagged(pc context.Context, t Tagged, branch string) (Tagged, bool, bool) {
// and it is read again next time), with the read that didn't answer.
func readTagged(pc context.Context, t Tagged, branch string) (Tagged, bool, bool, error) {
if branch == "" {
branch = "HEAD"
}
pb, err := fetchJSON(pc, githubRaw+"/"+t.Repo+"/"+url.PathEscape(branch)+"/package.json", 2<<20)
if err != nil {
return t, false, errors.Is(err, errNotFound)
if errors.Is(err, errNotFound) {
return t, false, true, nil
}
return t, false, false, err
}
var pj struct {
Name string `json:"name"`
@@ -333,7 +394,7 @@ func readTagged(pc context.Context, t Tagged, branch string) (Tagged, bool, bool
} `json:"magpie"`
}
if json.Unmarshal(pb, &pj) != nil || !pkgName.MatchString(pj.Name) {
return t, false, true
return t, false, true, nil
}
t.Package, t.Version = pj.Name, pj.Version
// a picture, as host.js's iconOf takes one: magpie checks and
@@ -354,6 +415,10 @@ func readTagged(pc context.Context, t Tagged, branch string) (Tagged, bool, bool
// built only to publish), so it is installed from npm
b, err := fetchJSON(pc, npmRegistry+"/"+npmPath(pj.Name)+"/latest", 1<<20)
known := err == nil || errors.Is(err, errNotFound)
var unknown error
if !known {
unknown = err
}
// npmNames is whether npm's copy names what it loads (main,
// exports); one that names nothing loads an index file
npmNames := false
@@ -387,12 +452,14 @@ func readTagged(pc context.Context, t Tagged, branch string) (Tagged, bool, bool
if path.Ext(entry) != "" {
_, err := fetchJSON(pc, githubRaw+"/"+t.Repo+"/"+url.PathEscape(branch)+"/"+strings.TrimPrefix(path.Clean("/"+entry), "/"), 1)
if errors.Is(err, errNotFound) {
return t, false, known
return t, false, known, unknown
}
if err != nil {
known, unknown = false, err
}
known = known && err == nil
}
}
return t, true, known
return t, true, known, unknown
}
// pkgEntry is the file importing a package loads, as Bun resolves it: its