Files
magpie/internal/agent/claude_picker_group_test.go
T
yetone aa2ea5c906 agent: Claude's main model is written as a tier alias, so Claude Desktop on a subscription no longer sends magpie's prefixed id (iryan_sun on X)
Claude Desktop's Code tab sets CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST=1. Claude
Code then ignores the env block of settings.json (base URL, token, tier model
vars) but still honours the top-level `model`. magpie wrote `model` as the
provider-prefixed id (anth/claude-opus-4-7), so the official API rejected it
and WebFetch failed.

writeTiers now writes `model` as the tier alias (opus/sonnet/haiku/fable, with
[1m]) whose ANTHROPIC_DEFAULT_<TIER>_MODEL already maps to the main model, on
this machine's ~/.claude only. A distro file, the claude-magpie launch file and
a main with no tier keep the id. The model field reads the alias back through
the tier env, also while the gateway is unreachable (drift). Unwire drops
`model` when magpie wrote the alias.

What happens to old values: a settings.json an older magpie wrote with the
prefixed id (model "up/big-x") is rewritten to the alias at the next catalog
sync, for every user, with no step on their side; the tier env already maps it
to the same model. A prefixed id picked from the modelPicker rows is brought to
the alias the same way. A model the user set themselves (claude-haiku-9,
own/xyz, opusplan) is never rewritten.

Ran:
- Real Claude Code CLI (~/.local/bin/claude) in a sandbox HOME and
  CLAUDE_CONFIG_DIR, wired by a magpie built from this tree (gateway :38760)
  to a scripted upstream that logs each request's model: main -> big-x,
  `--model haiku` -> fast-x, a subagent (Agent tool) -> sub-x, WebFetch's
  extraction of https://example.com -> fast-x (haiku's). Old prefixed file
  migrated to "sonnet" by a catalog sync; the three user models above stayed.
- TestClaudeMainModelIsAnAlias: red on the old code, red again with mainWord
  broken by hand, green with the fix; it also covers the user's own models.
- 11 existing tests read the raw `model` as an id; they read it through
  writtenModel now. TestWSLClaudePick compares the distro's file with the
  native one after normalising `model` (the distro keeps the id by design).
- go test ./internal/agent ok at the pushed head; gofmt; go vet darwin, linux
  nogui, windows for internal/agent.
- internal/cli: TestGatewayKeyLimitCLI is red on origin/main b39a793c9 too,
  untouched here: #6082.
Not tried: the real Claude Desktop UI (only Claude Code with the
host-managed flag, 2.1.312/.314/.316); a user whose Claude runs in a WSL
distro is not covered; the full suite is run after the tag.
2026-11-11 13:28:02 +08:00

94 lines
3.4 KiB
Go

package agent
import (
"encoding/json"
"fmt"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// yanjieee (#1575): with one subscription's 30 models and a routing group,
// saving the group set Claude Code's model and every tier to group/<id>,
// while /model's modelPicker held only the subscription's 30 models, so
// the model Claude Code was on wasn't in /model. Every routing group is in
// the picker, first, as Claude Code is given it; a group saved later is
// there after the next Sync.
func TestClaudePickerListsRoutingGroups(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("APPDATA", filepath.Join(home, "AppData", "Roaming"))
t.Setenv("LOCALAPPDATA", filepath.Join(home, "AppData", "Local"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
t.Setenv("CLAUDE_CONFIG_DIR", "")
models := []string{"claude-haiku-5-5", "deepseek-v4.1-flash"}
for i := len(models); i < 30; i++ {
models = append(models, fmt.Sprintf("model-%02d", i))
}
if err := provider.Save(provider.Provider{ID: "commandcode", Name: "Command Code", Chat: "http://127.0.0.1:1/v1", Key: "k", Models: models}); err != nil {
t.Fatal(err)
}
if err := provider.SaveGroup(provider.Group{ID: "auto-deepseek-v4-1-flash", Name: "Auto DeepSeek", Members: []string{"commandcode/deepseek-v4.1-flash", "commandcode/model-05"}}); err != nil {
t.Fatal(err)
}
path := filepath.Join(home, ".claude", "settings.json")
writeFile(t, path, `{"theme":"dark"}`)
a := claude(home)
picker := func() []map[string]string {
t.Helper()
var p struct{ Options []map[string]string }
raw, _ := edit.GetJSON(path, "modelPicker")
if err := json.Unmarshal([]byte(raw), &p); err != nil {
t.Fatalf("modelPicker: %v\n%s", err, readFile(path))
}
return p.Options
}
listed := func() []string {
var out []string
for _, o := range picker() {
out = append(out, o["model"])
}
return out
}
if err := a.Apply("model", "group/auto-deepseek-v4-1-flash"); err != nil {
t.Fatal(err)
}
main := writtenModel(path)
if !strings.HasPrefix(main, "auto/deepseek-v4.1-flash") {
t.Fatalf("model: %q\n%s", main, readFile(path))
}
opts := picker()
if len(opts) != 31 {
t.Fatalf("picker has %d options, want the group and 30 models: %v", len(opts), listed())
}
// the group, first, as the model Claude Code was set to is spelled
if g := opts[0]; g["model"] != main || g["label"] != "Auto DeepSeek · routing group" || g["description"] != "via magpie" {
t.Fatalf("first option %v, want the group as %q: %v", g, main, listed())
}
for _, m := range models {
if !slices.ContainsFunc(listed(), func(v string) bool {
return strings.TrimSuffix(v, "[1m]") == "commandcode/"+provider.ClaudeDashed(m) || strings.TrimSuffix(v, "[1m]") == "commandcode/"+m
}) {
t.Fatalf("picker has no commandcode/%s: %v", m, listed())
}
}
// a group saved after it is in /model once magpie syncs Claude Code
if err := provider.SaveGroup(provider.Group{ID: "second", Name: "Second", Members: []string{"commandcode/model-07"}}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if !slices.ContainsFunc(listed(), func(v string) bool { return strings.TrimSuffix(v, "[1m]") == "group/second" }) {
t.Fatalf("a group saved later isn't in the picker: %v", listed())
}
}