diff --git a/internal/catalog/live.go b/internal/catalog/live.go index 6e582f8be..0ddc1be9e 100644 --- a/internal/catalog/live.go +++ b/internal/catalog/live.go @@ -1,6 +1,7 @@ package catalog import ( + "bytes" "context" "encoding/json" "errors" @@ -313,15 +314,45 @@ func saveLive(provider, base, asked string, models []Model, sides map[string][]M return err } now := time.Now() - b, _ := json.MarshalIndent(liveFile{Fetched: now, Base: base, Models: models, Sides: sides, Kinded: true, - Since: liveSince(provider, models, now), Asked: asked}, "", " ") + f := liveFile{Fetched: now, Base: base, Models: models, Sides: sides, Kinded: true, + Since: liveSince(provider, models, now), Asked: asked} + same := sameLive(p, f) + b, _ := json.MarshalIndent(f, "", " ") if err := os.WriteFile(p, b, 0o644); err != nil { return err } + if same { + // the list as it was, fetched again (an account's each 45 + // minutes, KeepAccountsListed): only when it was fetched is other. + // Its next read looks at the file, and nothing else is told: + // Touched had every agent's lists built and compared again, and + // Forget moved the stamp dsh's rounds build magpie's models by, + // ~40 MB of allocations a time in a magpie at rest. + filememo.Look(p) + return nil + } Touched() return nil } +// sameLive is whether the list saved at p is f but for when it was +// fetched: the bytes on the disk are f's, written at the time they say. +func sameLive(p string, f liveFile) bool { + old, err := os.ReadFile(p) + if err != nil { + return false + } + var at struct { + Fetched time.Time `json:"fetched"` + } + if json.Unmarshal(old, &at) != nil { + return false + } + f.Fetched = at.Fetched + b, err := json.MarshalIndent(f, "", " ") + return err == nil && bytes.Equal(b, old) +} + // liveSince is Since for models about to be saved for provider at now: // what the list saved before said of each model it had too, now for one it // didn't. A list saved before Since was kept had each of its models since diff --git a/internal/catalog/live_same_test.go b/internal/catalog/live_same_test.go new file mode 100644 index 000000000..ca044653b --- /dev/null +++ b/internal/catalog/live_same_test.go @@ -0,0 +1,82 @@ +package catalog + +import ( + "os" + "path/filepath" + "testing" + "time" + + "github.com/yetone/magpie/internal/filememo" +) + +// A list fetched again as it was (an account's, each 45 minutes) moves only +// when it was fetched: the agents aren't told (Changed), as each time they +// were every agent's lists were built and compared again for nothing. A +// list that differs in anything else — a model, a part, what it was asked +// as — tells them. The held catalog isn't dropped either (Forget moves the +// stamp dsh's rounds build magpie's models again by); the file's next +// read, under a request's hold too, is the new one. +func TestLiveSavedAgainSameTellsNoAgent(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + changed, forgot := 0, 0 + oldC, oldF := Changed, Forget + Changed, Forget = func() { changed++ }, func() { forgot++ } + defer func() { Changed, Forget = oldC, oldF }() + + a := []Model{{ID: "gpt-a", Context: 200000}, {ID: "gpt-b"}} + save := func(ms []Model, asked string) { + t.Helper() + if err := SaveLiveAsked("acct", "https://a/v1", asked, ms); err != nil { + t.Fatal(err) + } + } + save(a, "1.0") + if changed != 1 || forgot != 1 { + t.Fatalf("first save: changed %d, forgot %d", changed, forgot) + } + // read as a list saved a while ago is: taken as unchanged under the + // hold, until it is looked at again + old := time.Now().Add(-time.Hour) + if err := os.Chtimes(LivePath("acct"), old, old); err != nil { + t.Fatal(err) + } + release := filememo.Hold() + defer release() + first, _ := readLive("acct") + time.Sleep(2 * time.Millisecond) + save([]Model{{ID: "gpt-a", Context: 200000}, {ID: "gpt-b"}}, "1.0") + if changed != 1 || forgot != 1 { + t.Errorf("the same list again: changed %d, forgot %d; want 1, 1", changed, forgot) + } + if again, _ := readLive("acct"); !again.Fetched.After(first.Fetched) { + t.Errorf("fetched %v, not after %v", again.Fetched, first.Fetched) + } + for _, c := range []struct { + name string + ms []Model + asked string + }{ + {"a model's window", []Model{{ID: "gpt-a", Context: 400000}, {ID: "gpt-b"}}, "1.0"}, + {"a model more", []Model{{ID: "gpt-a", Context: 400000}, {ID: "gpt-b"}, {ID: "gpt-c"}}, "1.0"}, + {"asked as another", []Model{{ID: "gpt-a", Context: 400000}, {ID: "gpt-b"}, {ID: "gpt-c"}}, "2.0"}, + } { + before := changed + save(c.ms, c.asked) + if changed != before+1 { + t.Errorf("%s: agents not told", c.name) + } + } + before := changed + if err := SaveLiveSides("acct", "https://a/v1", a, map[string][]Model{"https://a/v1": a[:1]}); err != nil { + t.Fatal(err) + } + if err := SaveLiveSides("acct", "https://a/v1", a, map[string][]Model{"https://a/v1": a}); err != nil { + t.Fatal(err) + } + if changed != before+2 { + t.Errorf("a part changed: told %d times, want 2", changed-before) + } +}