Claude Code's model field appended its own models to the list it keeps
of magpie's (claudeViaShared), a copy of the whole catalog on every
call, as withVia did for the other agents until cdfdb62c6. With real
accounts, 40 window polls spent 9 MB flat in that append.
withVia's cache is now joinedVia, keyed by the caller, and the field
reads through it, here and in a distro asleep (wsl.go). withVia still
gives the shared list itself when there are no own models; joinedVia
always gives a list apart from via, so a write to the model field's
options never reaches the list kept (TestClaudeViaMagpieKept).
Run on mote (macOS), at this tree:
- gofmt clean; go vet ./internal/agent for darwin, windows and linux.
- TestClaudeModelFieldKeepsTheJoinedList, TestWithVia*, TestClaudeVia*
and TestSameOption* alone 3/3 ok.
- Broken by hand: the plain append back -> "Claude Code's own models
joined its list from magpie again"; joinedVia handing out via with no
own models -> TestClaudeViaMagpieKept "a write to the model field's
options reached the options kept (folded true)".
- go test ./internal/agent ./internal/tui ./internal/cli: ok.
- go test ./internal/gui: red only on #4514 and #3949, as before.
- Real-account sandbox, 40 window polls against the GroupFinder tree:
Agent.Picks 56.4 -> 42.6 MB, gui.state 138.0 -> 130.0 MB; /api/state
and drift output byte-identical; phys_footprint_peak 139 MB.
Not run: the whole ./... suite; a real WSL distro.
120 lines
4.7 KiB
Go
120 lines
4.7 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"slices"
|
|
"testing"
|
|
"unsafe"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A model field's list is its own models then the catalog's: the append
|
|
// copied the whole catalog for each field of each agent on every poll.
|
|
// withVia keeps the list joined while the own models read the same and the
|
|
// catalog's list is the one kept, and joins it again when either changed.
|
|
func TestWithViaKeepsTheJoinedList(t *testing.T) {
|
|
syncHome(t)
|
|
if err := provider.Save(provider.Provider{ID: "oa", Name: "OA", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-5.5", "gpt-5.4"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
own := func(v string) []Option { return []Option{{Value: v, Note: "own"}} }
|
|
want := func(o []Option) []Option { return append(slices.Clone(o), viaMagpieShared("pi", magpieID+"/")...) }
|
|
equal := func(a, b []Option) bool { return slices.EqualFunc(a, b, sameOption) }
|
|
|
|
first := withVia(own("m1"), "pi", magpieID+"/")
|
|
if len(first) < 3 || !equal(first, want(own("m1"))) {
|
|
t.Fatalf("the joined list is not own then the catalog: %+v", first)
|
|
}
|
|
again := withVia(own("m1"), "pi", magpieID+"/")
|
|
if &again[0] != &first[0] {
|
|
t.Fatal("the same own models joined the catalog again")
|
|
}
|
|
if n := testing.AllocsPerRun(20, func() { withVia(own("m1"), "pi", magpieID+"/") }); n > 60 {
|
|
t.Fatalf("a list already joined took %.0f allocations", n)
|
|
}
|
|
if cap(again) != len(again) {
|
|
t.Fatal("an append onto the kept list would write into it")
|
|
}
|
|
|
|
// own models that read otherwise, an icon among them, are joined anew
|
|
changed := own("m1")
|
|
changed[0].Icons = []string{"x"}
|
|
if got := withVia(changed, "pi", magpieID+"/"); &got[0] == &first[0] || !equal(got, want(changed)) {
|
|
t.Fatalf("own models changed since are not what is listed: %+v", got[0])
|
|
}
|
|
if got := withVia(own("m2"), "pi", magpieID+"/"); got[0].Value != "m2" || !equal(got, want(own("m2"))) {
|
|
t.Fatalf("own models changed since are not what is listed: %+v", got[0])
|
|
}
|
|
|
|
// a provider saved since: the catalog's list is built again, and so
|
|
// is the joined one
|
|
if err := provider.Save(provider.Provider{ID: "nb", Name: "NB", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"x"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := withVia(own("m2"), "pi", magpieID+"/")
|
|
if !slices.ContainsFunc(got, func(o Option) bool { return o.Value == magpieID+"/nb/x" }) || !equal(got, want(own("m2"))) {
|
|
t.Fatalf("a provider saved since is not listed: %d options", len(got))
|
|
}
|
|
|
|
if got := withVia(nil, "pi", magpieID+"/"); !sameList(got, viaMagpieShared("pi", magpieID+"/")) {
|
|
t.Fatal("with no own models the catalog's list is not the one kept")
|
|
}
|
|
}
|
|
|
|
// sameOption reads every field of an Option: one added and left out of it
|
|
// would keep a joined list whose own models changed only in that field.
|
|
func TestSameOptionReadsEveryField(t *testing.T) {
|
|
typ := reflect.TypeFor[Option]()
|
|
for i := range typ.NumField() {
|
|
var a, b Option
|
|
f := reflect.ValueOf(&b).Elem().Field(i)
|
|
f = reflect.NewAt(f.Type(), unsafe.Pointer(f.UnsafeAddr())).Elem()
|
|
switch f.Kind() {
|
|
case reflect.String:
|
|
f.SetString("x")
|
|
case reflect.Bool:
|
|
f.SetBool(true)
|
|
case reflect.Int:
|
|
f.SetInt(1)
|
|
case reflect.Float64:
|
|
f.SetFloat(1)
|
|
case reflect.Slice:
|
|
f.Set(reflect.ValueOf([]string{"x"}))
|
|
default:
|
|
t.Fatalf("Option.%s is a %s, which this test can't set", typ.Field(i).Name, f.Kind())
|
|
}
|
|
if sameOption(a, b) {
|
|
t.Errorf("sameOption doesn't read Option.%s", typ.Field(i).Name)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Claude Code's model field joins its own models with the list it keeps of
|
|
// its own (claudeViaShared), on this machine and in a distro asleep, and
|
|
// keeps that joined list too.
|
|
func TestClaudeModelFieldKeepsTheJoinedList(t *testing.T) {
|
|
syncHome(t)
|
|
if err := provider.Save(provider.Provider{ID: "oa", Name: "OA", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-5.5", "gpt-5.4"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
home := os.Getenv("HOME")
|
|
writeFile(t, filepath.Join(home, ".claude", "settings.json"), `{}`)
|
|
field := claude(home).Field("model")
|
|
first := field.Options(map[string]string{})
|
|
if !slices.ContainsFunc(first, func(o Option) bool { return o.Value == "oa/gpt-5.4" }) {
|
|
t.Fatal("the model field doesn't offer oa's models")
|
|
}
|
|
if again := field.Options(map[string]string{}); &again[0] != &first[0] {
|
|
t.Fatal("Claude Code's own models joined its list from magpie again")
|
|
}
|
|
if err := provider.Save(provider.Provider{ID: "nb", Name: "NB", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"y"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := field.Options(map[string]string{}); !slices.ContainsFunc(got, func(o Option) bool { return o.Value == "nb/y" }) {
|
|
t.Fatal("a provider saved since is not offered")
|
|
}
|
|
}
|