agent: Claude Code's model field joins its own models with magpie's list once

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.
This commit is contained in:
yetone
2026-11-01 08:35:37 +08:00
parent 7e3cfa3442
commit b62b914964
5 changed files with 44 additions and 5 deletions
+5 -1
View File
@@ -1755,7 +1755,11 @@ func claudeIn(at place) *Agent {
// time, folded in the picker (#496). Only this machine's: a
// distro's Claude Code has a sign-in of its own, which magpie
// doesn't read
return append(group(name, own), claudeViaShared(name == "Claude Code" && at.id == "")...)
fold, key := name == "Claude Code" && at.id == "", "\x00claude-via"
if fold {
key += "-folded"
}
return joinedVia(group(name, own), claudeViaShared(fold), [2]string{key, at.id})
},
}, {
// the effort Claude Code starts with, as its /effort saves it: under
+2 -2
View File
@@ -45,8 +45,8 @@ func TestClaudeViaMagpieKept(t *testing.T) {
if got := values(); !got["oa/gpt-5.4"] {
t.Fatalf("a caller's write reached the options kept: %v", got)
}
// the model field appends the list kept to its own models: its
// options are a list of their own too
// the model field joins the list kept to its own models: its
// options are never the kept list itself, even with no own models
home := os.Getenv("HOME")
writeFile(t, filepath.Join(home, ".claude", "settings.json"), `{}`)
field := claude(home).Field("model").Options(map[string]string{})
+8 -1
View File
@@ -51,7 +51,14 @@ func withVia(own []Option, agent, prefix string) []Option {
if len(own) == 0 {
return via
}
key := [2]string{agent, prefix}
return joinedVia(own, via, [2]string{agent, prefix})
}
// joinedVia is append(own, via...) for a field whose via is a shared list,
// kept under key while own reads the same and via is the list kept (see
// withVia). Read only; with no own models it is still a list apart from
// via, as Claude Code's model field has always given.
func joinedVia(own, via []Option, key [2]string) []Option {
joinedOptions.Lock()
defer joinedOptions.Unlock()
if j, ok := joinedOptions.m[key]; ok && sameList(j.via, via) && slices.EqualFunc(j.own, own, sameOption) {
+28
View File
@@ -1,6 +1,8 @@
package agent
import (
"os"
"path/filepath"
"reflect"
"slices"
"testing"
@@ -89,3 +91,29 @@ func TestSameOptionReadsEveryField(t *testing.T) {
}
}
}
// 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")
}
}
+1 -1
View File
@@ -508,7 +508,7 @@ var wslKinds = []wslKind{
case "model":
// whether it has a base URL of its own is the distro's file
return func(cur map[string]string) []Option {
return append(group("Claude Code", claudeOwn(cur["model"], nil, catalog.Provider("anthropic"))), claudeViaShared(false)...)
return joinedVia(group("Claude Code", claudeOwn(cur["model"], nil, catalog.Provider("anthropic"))), claudeViaShared(false), [2]string{"\x00claude-via", "\x00asleep"})
}
case "effort", "ultracode":
// their own options read only what was last seen