provider: a started app fetches the partner list at once, and hourly after

The owner's MacBook Air, on yesterday's release, listed no partners hours
after Kimi Code joined the list. The list kept on disk set the next
fetch: six hours after it was fetched, read again at each start. A list
fetched empty minutes before Kimi was added held it back for up to six
hours, and quitting and opening magpie didn't help, since the new
process read the same time from disk.

- A process fetches the list on its first ask, whatever the kept list's
  age (loadPartners no longer sets partnerNext).
- It is fetched again after an hour, not six; the feed is ~1.7KB.
- An icon kept from the same URL is used without downloading it again,
  so the hourly fetch costs one small request. A partner's new logo goes
  at a new URL (e6bed2f06 moved Kimi's to partner-kimi.png). A failed
  icon fetch still falls back to the kept one.

Not explained by this: this mac's app (v0.1.1296, running since 10-20
20:39) kept a list from 10-20 15:40 for three days, past many six-hour
marks. The same binary fetches Kimi from the CLI at once. Nothing in the
fetch path can hang past its 20s bound, no proxy is set, and the app has
no goroutine dump to look at, so the cause is open: #3362.

Run at origin/main e6bed2f06:
- TestPartnersFetchedOnStart (new): a fresh empty list kept, a start
  lists acme. Red on the old partners.go ("" want acme).
- TestPartnersFromTheFeed: an icon from one URL is fetched once over two
  list fetches. Red on the old code (fetched 3 times).
- go test ./internal/provider ok (87s); go vet on darwin, linux and
  windows ok; gofmt clean.
Not tried: a real app across an hour's refetch.
This commit is contained in:
yetone
2026-10-23 16:22:04 +08:00
parent eee9ece293
commit 97902e9383
2 changed files with 65 additions and 8 deletions
+25 -8
View File
@@ -83,8 +83,11 @@ var (
partnerFetches bool
)
// A list is fetched again after partnerEvery, so a partner added reaches
// an app left running within the hour; the list is a couple of KB and its
// icons aren't fetched again (partnerIcon).
const (
partnerEvery = 6 * time.Hour
partnerEvery = time.Hour
partnerRetry = 10 * time.Minute
)
@@ -106,8 +109,9 @@ func partnersSource() string {
}
// Partners are the partners listed now, in the list's order: what was
// fetched last, never waited for. A list older than six hours is fetched
// again behind the call, and the page asked again shows it.
// fetched last, never waited for. A list this process hasn't fetched, or
// fetched an hour ago, is fetched again behind the call, and the page
// asked again shows it.
func Partners() []Partner {
if partnersSource() == "off" {
return nil
@@ -181,8 +185,10 @@ func loadPartners() {
delete(c.Seen, id)
}
}
// partnerNext stays zero: a process started fetches on its first ask,
// whatever the kept list's age. Waiting out the kept list's six hours
// held back a partner added minutes after it was fetched, restart or not.
partnerState = &c
partnerNext = c.At.Add(partnerEvery)
}
// refreshPartners fetches the list and keeps it; on a failure the list
@@ -352,13 +358,24 @@ func partnerIcon(ctx context.Context, p Partner) string {
if p.IconURL == "" {
return "generic"
}
// an icon kept from the same URL is used as it is, so an hourly fetch
// doesn't download it again; a partner's new logo goes at a new URL
partnerMu.Lock()
loadPartners()
s, ok := partnerState.Seen[p.ID]
partnerMu.Unlock()
kept := ok && s.IconURL == p.IconURL && s.Icon != "" && s.Icon != "generic"
if kept {
if f := IconFile(strings.TrimPrefix(s.Icon, iconPrefix)); f != "" {
if _, err := os.Stat(f); err == nil {
return s.Icon
}
}
}
if icon, err := fetchPartnerIcon(ctx, p.IconURL); err == nil {
return icon
}
partnerMu.Lock()
defer partnerMu.Unlock()
loadPartners()
if s, ok := partnerState.Seen[p.ID]; ok && s.IconURL == p.IconURL {
if kept {
return s.Icon
}
return "generic"
+40
View File
@@ -2,9 +2,11 @@ package provider
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
@@ -53,7 +55,9 @@ func TestPartnersFromTheFeed(t *testing.T) {
t.Cleanup(resetPartners)
was := fetchPartnerIcon
t.Cleanup(func() { fetchPartnerIcon = was })
var iconFetches atomic.Int32
fetchPartnerIcon = func(_ context.Context, u string) (string, error) {
iconFetches.Add(1)
if u != "https://acme.example/logo.png" {
t.Errorf("icon fetched from %q", u)
}
@@ -101,6 +105,15 @@ func TestPartnersFromTheFeed(t *testing.T) {
t.Errorf("siteHost = %q, region's %q", a.SiteHost, got[1].Regions[0].SiteHost)
}
// fetched again, its icon from the same URL is the one kept
fetchPartnersNow()
if n := iconFetches.Load(); n != 1 {
t.Errorf("icon fetched %d times over two fetches of one URL, want 1", n)
}
if got := Partners(); len(got) == 0 || got[0].Icon != a.Icon {
t.Errorf("icon after a second fetch = %v, want the kept %q", got, a.Icon)
}
// a provider is added from it as from a preset
p, err := FromPreset("acme")
if err != nil || p.Chat != "https://api.acme.example/v1" || p.KeysURL != "https://acme.example/keys?ref=magpie" || p.Decide != "" {
@@ -187,6 +200,33 @@ func TestPartnersFetchedLastIsHeld(t *testing.T) {
}
}
// A process started fetches the list on its first ask, however fresh the
// list kept on disk: one fetched empty a minute before a partner was added
// held it back for six hours, restart or not.
func TestPartnersFetchedOnStart(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", t.TempDir())
resetPartners()
t.Cleanup(resetPartners)
kept, _ := json.Marshal(partnerCache{At: time.Now().Add(-time.Minute)})
if err := os.MkdirAll(filepath.Dir(partnerCacheFile()), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(partnerCacheFile(), kept, 0o644); err != nil {
t.Fatal(err)
}
var body atomic.Value
body.Store(`{"partners":[{"id":"acme","name":"Acme","chat":"https://a.example/v1"}]}`)
t.Setenv("MAGPIE_PARTNERS", partnerFeedServer(t, &body).URL)
Partners()
// the fetch it started has ended once a fetch can be claimed
claimPartnerFetch()
partnerMu.Unlock()
if ids := partnerIDs(Partners()); ids != "acme" {
t.Fatalf("partners after a start with a fresh empty list kept = %q, want acme", ids)
}
}
// MAGPIE_PARTNERS=off lists none and asks nobody, whatever was kept.
func TestPartnersOff(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", t.TempDir())