provider: Save on a provider's editor left unchanged returns at once, not after re-writing every agent's config (#1962, witt-bit)

Opening DeepSeek's editor and pressing Save without changing anything hung
for seconds (witt-bit, still on v0.1.1246). Save stored providers.json
whatever it held, and every store calls catalog.Touched, which runs
agent.SyncCatalog: every set-up agent's config built and written again
(Claude Code, Codex, Pi, dsh, Hermes, Grok, ZCode and the rest), synchronously
in the save request. Goroutine dumps every ~100ms during a save in a sandbox
HOME with the owner's providers and agents' configs (no credentials) showed
the save handler runnable in SyncCatalog the whole time, not waiting on
network or a lock. Save now compares what it would store with what is
stored and, when they are the same bytes, writes nothing and tells no agent.
A save that changes something still does both.

Measured in that sandbox (magpie web, Playwright clicking DeepSeek > Save):
before 1616, 5407, 1843 ms for POST /api/provider/save; after 212, 132, 145,
147 ms, and providers.json's md5 the same before and after.

Tests: TestSaveUnchangedTellsNoAgent (new) fails on the old Save ("an
unchanged save told the agents 1 times; want none") and passes now.

Full suite: SUITE_LINE
This commit is contained in:
yetone
2026-10-15 23:08:00 +08:00
parent c8ed37e126
commit 7e33a298b4
2 changed files with 70 additions and 0 deletions
+17
View File
@@ -816,6 +816,13 @@ func Save(p Provider) error {
if p.Was == nil {
p.Was = was.Was
}
if sameStored(*was, p) {
// Save pressed on an editor left as it was: providers.json
// would be written byte for byte as it is, and its write
// handed the catalog to every agent again (agent.SyncCatalog),
// seconds of CPU with many agents set up (#1962, witt-bit)
return nil
}
f.Providers[i] = p
} else {
f.Providers = append(f.Providers, p)
@@ -831,6 +838,16 @@ func Save(p Provider) error {
return nil
}
// sameStored is whether a and b are written to providers.json alike.
func sameStored(a, b Provider) bool {
x, err := json.Marshal(a)
if err != nil {
return false
}
y, err := json.Marshal(b)
return err == nil && bytes.Equal(x, y)
}
// listedWith is what p.PickedFrom is saved as: none without picks; what was
// kept with the picks when they are the same picks; and the models listed
// now when the picks are new, as the user picked them from that list.
+53
View File
@@ -0,0 +1,53 @@
package provider
import (
"bytes"
"os"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// Save pressed on a provider's editor left as it was writes nothing and
// hands the agents nothing: each hand-over re-wrote every agent's config,
// seconds with many agents set up, and the DeepSeek editor's Save hung on
// it (#1962, witt-bit). A save that changes something still tells them.
func TestSaveUnchangedTellsNoAgent(t *testing.T) {
limitsHome(t)
in := Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "sk-1", Models: []string{"deepseek-v4-flash", "deepseek-v4-pro"}}
if err := Save(in); err != nil {
t.Fatal(err)
}
before, err := os.ReadFile(Path())
if err != nil {
t.Fatal(err)
}
told := 0
catalog.Changed = func() { told++ }
t.Cleanup(func() { catalog.Changed = nil })
if err := Save(in); err != nil {
t.Fatal(err)
}
if told != 0 {
t.Errorf("an unchanged save told the agents %d times; want none", told)
}
after, err := os.ReadFile(Path())
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(before, after) {
t.Errorf("an unchanged save rewrote providers.json:\n%s\nwas\n%s", after, before)
}
in.Key = "sk-2"
if err := Save(in); err != nil {
t.Fatal(err)
}
if told != 1 {
t.Errorf("a new key told the agents %d times; want once", told)
}
if p, err := Find("deepseek"); err != nil || p.Key != "sk-2" {
t.Errorf("the new key wasn't saved: %+v, %v", p, err)
}
}