Files
magpie/internal/agent/claude_bare_models_test.go
T
yetone fe8c05e0c8 agent: Claude's model variables hold Anthropic's own ids on Claude Code's own subscription, so Desktop's Code tab stops failing WebFetch (iryan_sun on X)
ANTHROPIC_DEFAULT_<TIER>_MODEL, ANTHROPIC_SMALL_FAST_MODEL,
CLAUDE_CODE_SUBAGENT_MODEL and the auto mode model stayed
"claude/claude-haiku-4-5[1m]" after the main model became an alias. A tier
variable can't be an alias of itself, so on this computer's own file a
Claude model of Claude Code's own subscription is now written as
Anthropic's bare id (provider.ClaudeBare): through magpie that id goes to the
same subscription (OwnRoute), and Anthropic knows it. Haiku has no 1M window,
so its [1m] mark is taken off. Read back, a bare Claude id is the
subscription's ref (ClaudeUnbare), so every field, the tiers' follow logic
and the gateway's stand-in read it as before.

Old files: a settings.json written with the prefixed ids is rewritten by the
next Sync (the check that already brought dashed spellings up to date), for
every user. Models of other vendors or a relay keep their ref: Anthropic has
no id for them. A distro's file and the claude-magpie launch file keep refs.

Run (wsl-run, Ubuntu, at the pushed head after rebasing on origin/main):
internal/agent -run "Claude|Tier|Folder" ok; internal/provider ok;
gofmt -l clean; go vet -tags nogui for linux, darwin and windows clean.
Before the rebase, the whole internal/agent package had two reds:
TestFolderVarsAreListed (BUN_INSTALL, fixed on main since) and
TestGrokInstalled (WSL2 binfmt, already #3548). Neither touches this change.
Real claude 2.1.316 with a fake upstream showed Desktop's
PROVIDER_MANAGED_BY_HOST sends the tier env as written, so prefixed ids go
to Anthropic.
Broken by hand: with ClaudeBare turned into a no-op,
TestClaudeModelVariablesAreAnthropicsOwnIDs fails on the bare ids and on the
Sync rewrite.
Not tried: the real Claude Desktop Code tab, a WSL distro user, real
accounts through magpie end to end. A non-Claude model or a relay-served
Claude model still has no Anthropic id, so its tier variable keeps the ref
and Desktop's Code tab still can't use it.
2026-11-11 16:02:17 +08:00

108 lines
4.2 KiB
Go

package agent
import (
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// Claude Desktop's Code tab on the user's own subscription sends the model
// variables of settings.json to Anthropic as they stand (iryan_sun on X:
// ANTHROPIC_SMALL_FAST_MODEL and the haiku tier were still
// "claude/claude-haiku-4-5[1m]", and WebFetch failed with "There's an issue
// with the selected model"). A Claude model of Claude Code's own
// subscription goes into every model variable as Anthropic's bare id; a
// model of another vendor, or a relay's, has no such id and stays.
func TestClaudeModelVariablesAreAnthropicsOwnIDs(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"))
t.Setenv("CLAUDE_CONFIG_DIR", "")
write := func(p, s string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(s), 0o600); err != nil {
t.Fatal(err)
}
}
exp := time.Now().Add(time.Hour).UnixMilli()
write(filepath.Join(home, ".claude", ".credentials.json"), `{"claudeAiOauth":{"accessToken":"sk-ant-oat01-x","refreshToken":"sk-ant-ort01-x","expiresAt":`+strconv.FormatInt(exp, 10)+`,"subscriptionType":"max","scopes":["user:inference","user:profile"]}}`)
write(filepath.Join(home, ".claude.json"), `{"oauthAccount":{"emailAddress":"own@example.com"},"additionalModelOptionsCache":[{"value":"claude-sonnet-4-6","label":"S"},{"value":"claude-haiku-4-5","label":"H"},{"value":"claude-opus-4-7","label":"O"}]}`)
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash"}}); err != nil {
t.Fatal(err)
}
provider.ForgetAccounts()
if p, err := provider.Find("claude"); err != nil || p.Account == nil {
t.Fatalf("no Claude Code account in the sandbox: %v", err)
}
path := filepath.Join(home, ".claude", "settings.json")
write(path, `{}`)
a := claude(home)
main := "claude/claude-sonnet-4-6"
if err := a.Field("model").Set(main); err != nil {
t.Fatal(err)
}
if err := a.Field("haiku").Set("claude/claude-haiku-4-5[1m]"); err != nil {
t.Fatal(err)
}
if err := a.Field("opus").Set("deepseek/pro"); err != nil {
t.Fatal(err)
}
get := func(k string) string { v, _ := edit.GetJSON(path, "env."+k); return v }
for k, want := range map[string]string{
"ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-4-6",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-haiku-4-5",
"ANTHROPIC_SMALL_FAST_MODEL": "claude-haiku-4-5",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "deepseek/pro", // no id Anthropic knows
} {
if got := strings.TrimSuffix(get(k), "[1m]"); got != want {
t.Errorf("%s is %q, want %q", k, got, want)
}
}
if got := get("ANTHROPIC_DEFAULT_HAIKU_MODEL"); strings.HasSuffix(got, "[1m]") {
t.Errorf("haiku has no 1M window to mark: %q", got)
}
// magpie reads them back as the subscription's models
if got := a.Field("haiku").Get(); got != "claude/claude-haiku-4-5" {
t.Errorf("haiku shown as %q", got)
}
if got := claudeMain(path); got != main {
t.Errorf("main read as %q", got)
}
// a settings.json an older magpie wrote is rewritten by the next sync
for k, v := range map[string]string{
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude/claude-haiku-4-5[1m]",
"ANTHROPIC_SMALL_FAST_MODEL": "claude/claude-haiku-4-5[1m]",
"CLAUDE_CODE_SUBAGENT_MODEL": "claude/claude-sonnet-4-6",
} {
if err := edit.SetJSON(path, edit.KV{Path: "env." + k, Value: v}); err != nil {
t.Fatal(err)
}
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
for _, k := range []string{"ANTHROPIC_DEFAULT_HAIKU_MODEL", "ANTHROPIC_SMALL_FAST_MODEL", "CLAUDE_CODE_SUBAGENT_MODEL"} {
if v := get(k); strings.Contains(v, "/") || strings.HasSuffix(v, "[1m]") {
t.Errorf("after Sync %s is still %q", k, v)
}
}
// the user's own bare model on a tier magpie doesn't hold stays theirs
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if got := get("ANTHROPIC_SMALL_FAST_MODEL"); got != "claude-haiku-4-5" {
t.Errorf("a second Sync moved it to %q", got)
}
}