From 7e33a298b4db5dc1d71de8e041f41b3d669e13f9 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 15 Oct 2026 12:14:40 +0800 Subject: [PATCH] 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 --- internal/provider/provider.go | 17 ++++++++ internal/provider/save_unchanged_test.go | 53 ++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 internal/provider/save_unchanged_test.go diff --git a/internal/provider/provider.go b/internal/provider/provider.go index ca1128d2c..ea61706ef 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -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. diff --git a/internal/provider/save_unchanged_test.go b/internal/provider/save_unchanged_test.go new file mode 100644 index 000000000..c872fd037 --- /dev/null +++ b/internal/provider/save_unchanged_test.go @@ -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) + } +}