provider: a partners.json that doesn't parse is kept aside before a try writes over it (#3362)
loadPartners reads a partners.json that is there but doesn't parse as
an empty cache: no list, and no Seen. Before 603408f38 only a fetch
that got the list wrote the file, so the loss waited for one. Now every
try writes it before the fetch is sent (Tried), offline too, so the
first try after a start wrote the empty cache over it: Seen holds the
presets of partners no longer listed, which providers added from them
still use (their endpoints, regions and key page), and those were gone.
A failed read is unknown, not empty (docs/code-standards.md).
savePartners now goes through lastgood.KeepUnparsed first, as
balance_history, credits_daily and codex_switch do for files their
readers take as empty when they don't parse: such a file is copied
aside as partners.json.bad-<time> before it is written over, and a
file that can't be read at all (or a copy that can't be kept) is not
written over. A file not there, or empty, writes as before.
Old data: a file already written over by 603408f38 is not in any
release (603408f38 lands in the same push), so nothing is lost before
this; a partners.json damaged earlier was written over by the next
successful fetch, as it still is, now with its copy kept.
Run (tree of this stack, on f6a96b2f0, go1.26.8, darwin/arm64):
- TestPartnersUnparsedFileKeptAside (a cut-short partners.json, the
list server answering 503, one try): with savePartners' KeepUnparsed
removed by hand, red: "partners.json that didn't parse was written
over with nothing kept aside ([])". With it: green, the .bad- copy
byte for byte the file, and the try's error recorded.
- go test -tags nogui -race -count=5 -run 'Partner|Icon'
./internal/provider: ok.
- Broken by hand in 603408f38's own code, each red in
TestPartnersKeepTheirLastTry: no save before the fetch (line 590:
"while a fetch is out the file says ... error "partners: 503 ..."");
the failure logged every try (573: "logged 3 times, want once").
- internal/gateway and internal/cli -run
'Goroutine|Heap|CPU|Help|Profile': ok. goroutinesRefused made to
return "" by hand: TestGoroutines... red ("Origin null with a key
(lanGuard true): 200 ..."), restored green. The dump's frames carry
argument words in hex, not what they point at, as 54636afa2 says.
- Full suite at this stack's head, go test -tags nogui -count=1
-timeout 45m ./... (tree 0ef74708, one suite on the machine's
other runs' load): exit 0, 70 packages ok, no red.
Not tried: a real app left running against usemagpie.ai; an unreadable
(EACCES) partners.json; Windows.
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/appdir"
|
||||
"github.com/yetone/magpie/internal/lastgood"
|
||||
"github.com/yetone/magpie/internal/source"
|
||||
)
|
||||
|
||||
@@ -298,6 +299,14 @@ func savePartners() {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// loadPartners reads a file that doesn't parse as no list and no Seen:
|
||||
// it is kept aside before it is written over, and not written over
|
||||
// when it can't be read. Every try writes the file (Tried, #3362), so
|
||||
// without this the first one, offline too, lost the presets of the
|
||||
// partners no longer listed that providers added from them still use.
|
||||
if lastgood.KeepUnparsed(partnerCacheFile(), lastgood.Into[partnerCache]) != nil {
|
||||
return
|
||||
}
|
||||
_ = os.MkdirAll(filepath.Dir(partnerCacheFile()), 0o755)
|
||||
tmp := partnerCacheFile() + ".tmp"
|
||||
if os.WriteFile(tmp, b, 0o644) == nil {
|
||||
|
||||
@@ -600,3 +600,40 @@ func TestPartnersKeepTheirLastTry(t *testing.T) {
|
||||
t.Fatalf("the list fetched again after a failure wasn't logged:\n%s", logged.String())
|
||||
}
|
||||
}
|
||||
|
||||
// A partners.json that doesn't parse is read as no list and no Seen, and
|
||||
// every try now writes the file before it is sent (#3362). The first try,
|
||||
// offline too, wrote over it: the presets of partners no longer listed,
|
||||
// which providers added from them still use, were gone. It is kept aside
|
||||
// first, as lastgood keeps every file read as empty when it doesn't parse.
|
||||
func TestPartnersUnparsedFileKeptAside(t *testing.T) {
|
||||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||||
resetPartners()
|
||||
t.Cleanup(resetPartners)
|
||||
bad := []byte(`{"at":"2026-10-20T15:40:31+08:00","seen":{"acme":{"id":"acme","name":"Acme AI","ch`)
|
||||
if err := os.MkdirAll(filepath.Dir(partnerCacheFile()), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(partnerCacheFile(), bad, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
http.Error(rw, "down", http.StatusServiceUnavailable)
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
t.Setenv("MAGPIE_PARTNERS", srv.URL)
|
||||
fetchPartnersNow()
|
||||
|
||||
aside, _ := filepath.Glob(partnerCacheFile() + ".bad-*")
|
||||
if len(aside) != 1 {
|
||||
t.Fatalf("partners.json that didn't parse was written over with nothing kept aside (%v)", aside)
|
||||
}
|
||||
if b, err := os.ReadFile(aside[0]); err != nil || !bytes.Equal(b, bad) {
|
||||
t.Fatalf("kept aside %q (%v), want the file as it was", b, err)
|
||||
}
|
||||
partnerMu.Lock()
|
||||
defer partnerMu.Unlock()
|
||||
if partnerState.Error == "" {
|
||||
t.Fatalf("the failed try isn't recorded: %+v", partnerState)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user