Files
magpie/internal/agent/claude_main_alias_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

112 lines
3.9 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// TestClaudeMainModelIsAnAlias (iryan_sun on X): Claude Desktop's Code tab
// runs Claude Code on the user's own subscription with the provider
// routing managed by the host, so it drops the settings.json env (the
// gateway's address, the tiers) but still takes the top-level "model".
// magpie's provider-prefixed id there went to Anthropic, which doesn't
// know it, and every request failed (WebFetch among them). The main model
// is written as the alias of the tier that carries it, which the env gives
// magpie's model and Anthropic's own gives the real tier.
func TestClaudeMainModelIsAnAlias(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
for _, p := range []provider.Provider{
{ID: "anth", Name: "Anthropic", Chat: "https://api.anthropic.com/v1", Key: "k", Models: []string{"claude-opus-4-7", "claude-sonnet-4-6", "claude-haiku-4-5"}},
{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
path := filepath.Join(home, ".claude", "settings.json")
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(`{}`), 0o644); err != nil {
t.Fatal(err)
}
a := claude(home)
for _, main := range []string{"anth/claude-opus-4-7", "anth/claude-sonnet-4-6", "deepseek/pro"} {
if err := a.Field("model").Set(main); err != nil {
t.Fatal(err)
}
m, _ := edit.GetJSON(path, "model")
if strings.Contains(m, "/") || !contains(claudeAliases, strings.TrimSuffix(m, "[1m]")) {
t.Fatalf("%s: settings.json model is %q, which Anthropic's API doesn't know", main, m)
}
if got, _ := edit.GetJSON(path, "env."+tierEnv(strings.TrimSuffix(m, "[1m]"))); strings.TrimSuffix(got, "[1m]") != main {
t.Fatalf("%s: the alias %q is on %q", main, m, got)
}
if got := a.Field("model").Get(); got != main {
t.Fatalf("%s: shown as %q", main, got)
}
if got := claudeMain(path); got != main {
t.Fatalf("%s: claudeMain %q", main, got)
}
}
// a Sync leaves it as it is, and a settings.json an older magpie wrote
// with the prefixed id is brought to the alias
if err := edit.SetJSON(path, edit.KV{Path: "model", Value: "deepseek/pro"}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if m, _ := edit.GetJSON(path, "model"); strings.Contains(m, "/") {
t.Fatalf("after Sync: %q", m)
}
if got := a.Field("model").Get(); got != "deepseek/pro" {
t.Fatalf("after Sync shown as %q", got)
}
// a model the user set themselves is never rewritten
for _, own := range []string{"claude-haiku-9", "own/xyz", "opusplan"} {
if err := edit.SetJSON(path, edit.KV{Path: "model", Value: own}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if m, _ := edit.GetJSON(path, "model"); m != own {
t.Fatalf("the user's %q became %q", own, m)
}
}
if err := edit.SetJSON(path, edit.KV{Path: "model", Value: "deepseek/pro"}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
// Disconnect leaves no alias on magpie's tiers behind
if err := a.Unwire(); err != nil {
t.Fatal(err)
}
if m, has := edit.GetJSON(path, "model"); has && m != "" {
t.Fatalf("after Unwire: model %q", m)
}
}
// writtenModel is settings.json's main model as the id it stands for: an
// alias magpie wrote resolved through its tier's env, anything else as is.
func writtenModel(path string) string {
m, _ := edit.GetJSON(path, "model")
if t := strings.TrimSuffix(m, "[1m]"); contains(claudeTiers, t) {
v, _ := edit.GetJSON(path, "env."+tierEnv(t))
return v
}
return m
}