Files
magpie/internal/provider/claude_code_models_test.go
T
yetone f51ab771f6 catalog, provider: a model only models.dev's lab lists is known, and a Claude account lists the gated models Anthropic told its Claude Code of (iebb on Discord)
iebb on Discord (10-27): his account has Claude Mythos 5.1, magpie
doesn't list it. models.dev took the gated Mythos 5.1 out of
anthropic's provider list (models.dev #11327) and kept only its lab
file, models/anthropic/claude-mythos-5-1.toml (#11328).

Cause. magpie read only models.dev's api.json (providers -> models).
The Claude subscription's list was exactly catalog.Provider("anthropic"),
so Mythos 5.1 left it with that PR. And a vendor list that still
returns it (an Anthropic key's /v1/models) got no window, reply limit,
name or thinking for it, since nothing read the lab catalog.

Fix.
- catalog: Sync also keeps models.dev's lab catalog (models.json) as
  labs.json beside the cache, through temp+rename; a failed fetch
  keeps the last one, and a cache without it is Stale once a process.
  A model only a lab lists votes into the same tables as a listed one
  (ContextOf, OutputOf, NameOf, Thinks, SeesImages); a model a
  provider lists keeps what providers say. A thinking lab-only model
  with no levels takes those of the newest of its family a provider
  gives (labKinEfforts: Mythos 5.1 takes Mythos 5's). catalog.Describe
  builds a Model from an id for lists that name ids alone.
- provider: the Claude account's list is models.dev's anthropic list
  plus what Anthropic told the account's Claude Code beyond it, read
  from .claude.json's additionalModelOptionsCache / modelAccessCache /
  additionalModelCostsCache (Claude Code's own sign-in and each saved
  account's directory). Account.Lists honours modelAccessCache's
  entitled per account, ListPrice falls back to Anthropic's price
  from additionalModelCostsCache, and the agent's own Claude Code
  picker (claudeOwn) offers the signed-in account's extras. WSL keeps
  the catalog list. magpie still asks Anthropic nothing with a Claude
  sign-in; nothing is sent differently, no Claude Code mimicry change.
- docs/subsystems/providers-accounts.md: two rows, the lab catalog
  and a Claude account's models.

Testdata: models.dev's api.json and models.json as served on 10-27,
cut to the anthropic and azure providers' Claude models and the
anthropic lab. Tests never reach the network.

Run:
- TestLabOnlyModelIsKnown, TestLabsLeaveListedModels, TestSyncKeepsLabs,
  TestClaudeAccountListsGatedModels, TestAnthropicKeyGatedModelDescribed:
  pass.
- go test -count=1 -timeout 45m ./internal/catalog/ ./internal/provider/
  ./internal/agent/ ./internal/gateway/ at 2e165fe9d + this change:
  catalog, agent (362s), gateway (557s) ok; provider crashed in
  TestUnreadLoginsWriteErrorsKeepCredentials ("close of synctest
  channel from outside bubble" in filememo.Forget). It crashes the
  same alone on a fresh origin/main 19dc5b925 and is #3632, open.
- rebased onto origin/main 2afacd7d1 (this commit's parent): catalog
  and provider again with -skip TestUnreadLoginsWriteErrorsKeepCredentials,
  both ok. agent and gateway were not run again after the rebase.
- gofmt -l internal/: nothing. go vet for darwin, linux and windows on
  catalog, provider, agent, gateway at the rebased head: ok.
- Real use: magpie built from this tree (linux, nogui) in a golang:1.26.8
  container with a sandbox HOME, so the Mac's Keychain and ~/.claude are
  never read. `magpie sync` fetched models.dev's real catalogs today
  (api.json's anthropic has no claude-mythos-5-1; labs.json has it).
  With a fake Claude sign-in and a .claude.json whose bootstrap caches
  name claude-mythos-5-1, `magpie models` lists claude/claude-mythos-5-1
  "Mythos 5.1" low/medium/high/xhigh; `magpie model spec` gives 1M,
  128k, vision on; `magpie model price` gives $25/$125, cache read
  $2.5, write $31.25 / $50 (1h). A custom Anthropic-protocol provider
  against a fake /v1/models returning claude-mythos-5-1 lists
  fakeanth/claude-mythos-5-1 "Claude Mythos 5.1" with 1M / 128k /
  low..xhigh.

Broken by hand, each red:
- lab reading off in loadGen: every lab assertion in the catalog tests
  and the provider tests (no window, no levels, the API-key entry's
  context 0).
- syncLabs off: TestSyncKeepsLabs.
- labKinEfforts off: the efforts assertions.
- the Claude list back to catalog.Provider("anthropic"): "Claude
  account's list has no Mythos 5.1".
- claudeLists / claudeCodePrice off: the entitlement and price
  assertions.

Not tried:
- a real account with Mythos 5.1: none here. The .claude.json keys and
  shapes were read from Claude Code 2.1.311's own binary, not a real
  account's file.
- a custom Anthropic-protocol provider without catalog=anthropic gets
  no price for a lab-only model (labs carry none).
- the plugin host's mdev() (host.js) reads api.json only; Gemini CLI's
  own picker stays catalog-only; a saved Claude account whose Claude
  Code never ran has no .claude.json, so its gated models show once it
  has run there.
2026-10-27 12:16:33 +08:00

178 lines
6.7 KiB
Go

package provider
import (
"context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"testing"
"time"
"github.com/yetone/magpie/internal/agentenv"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/filememo"
)
// mythosCatalogs puts models.dev's catalogs as they were on 10-27, cut to
// Claude, where magpie keeps them: Claude Mythos 5.1 is in the anthropic
// lab (models.json) and in no provider's list (api.json).
func mythosCatalogs(t *testing.T) {
t.Helper()
for src, dst := range map[string]string{
"../catalog/testdata/models.dev-2026-10-27-claude.json": catalog.CachePath(),
"../catalog/testdata/models.dev-labs-2026-10-27-anthropic.json": catalog.LabsPath(),
} {
b, err := os.ReadFile(src)
if err != nil {
t.Fatal(err)
}
os.MkdirAll(filepath.Dir(dst), 0o755)
if err := os.WriteFile(dst, b, 0o644); err != nil {
t.Fatal(err)
}
}
catalog.Reset()
t.Cleanup(catalog.Reset)
filememo.Forget()
}
// what Claude Code keeps in .claude.json from Anthropic's answer to its
// bootstrap, in the shape its 2.1.311 reads it
func claudeCodeState(t *testing.T, path, email string, options []map[string]any, access []map[string]any, costs map[string]any) {
t.Helper()
writeFile(t, path, map[string]any{
"oauthAccount": map[string]any{"emailAddress": email, "accountUuid": "u-" + email},
"additionalModelOptionsCache": options,
"additionalModelOptionsAnsweredAt": time.Now().UnixMilli(),
"modelAccessCache": access,
"additionalModelCostsCache": costs,
})
}
// A Claude account lists a gated model Anthropic told its Claude Code it
// has, though models.dev's anthropic list no longer does: Claude Mythos
// 5.1, with its lab file's window and name and Anthropic's own price; an
// account not entitled to it isn't routed it (iebb on Discord).
func TestClaudeAccountListsGatedModels(t *testing.T) {
home := claudeHome(t)
claudeSignIn(t, home, time.Now().Add(time.Hour))
claudeBase = "http://127.0.0.1:1" // asked nothing
mythosCatalogs(t)
if slices.Contains(modelIDs(catalog.Provider("anthropic")), "claude-mythos-5-1") {
t.Fatal("fixture: models.dev lists Mythos 5.1 under anthropic again")
}
no := false
claudeCodeState(t, filepath.Join(home, ".claude.json"), "iebb@example.com",
[]map[string]any{
{"value": "claude-mythos-5-1[1m]", "label": "Mythos 5.1", "description": "Restricted research model"},
{"value": nil, "label": "Default", "description": "Recommended"},
{"value": "claude-opus-3", "label": "Opus 3", "description": "Retired", "disabled": true},
},
[]map[string]any{{"apiName": "claude-mythos-5-1", "entitled": true}, {"apiName": "claude-opus-4-5", "entitled": no}},
map[string]any{"claude-mythos-5-1": map[string]any{
"inputTokens": 25, "outputTokens": 125, "promptCacheWriteTokens": 31.25,
"promptCacheWrite1hTokens": 50, "promptCacheReadTokens": 2.5, "webSearchRequests": 0.01}})
filememo.Forget()
p, ok := find(All(), "claude")
if !ok || p.Account == nil {
t.Fatal("no Claude account")
}
ms := p.Available()
i := slices.IndexFunc(ms, func(m catalog.Model) bool { return m.ID == "claude-mythos-5-1" })
if i < 0 {
t.Fatalf("Claude account's list has no Mythos 5.1: %v", modelIDs(ms))
}
if m := ms[i]; m.Name != "Mythos 5.1" || m.Context != 1_000_000 || m.Output != 128_000 || !m.Reasoning || len(m.Efforts) == 0 {
t.Errorf("Mythos 5.1 = %+v", m)
}
if slices.Contains(modelIDs(ms), "claude-opus-3") {
t.Error("a disabled option listed")
}
if pr, ok := p.ListPrice("claude-mythos-5-1"); !ok || pr.Input != 25 || pr.Output != 125 || pr.CacheWrite1h != 50 {
t.Errorf("price = %+v %v", pr, ok)
}
fetched, err := p.Fetch(context.Background())
if err != nil || !slices.Contains(modelIDs(fetched), "claude-mythos-5-1") {
t.Fatalf("fetch: %v %v", modelIDs(fetched), err)
}
if e, ok := EntryOf("claude/claude-mythos-5-1"); !ok || e.Context != 1_000_000 {
t.Errorf("entry: %+v %v", e, ok)
}
// routed to the account Anthropic entitles to it, not one it doesn't
if !p.Account.Lists("claude-mythos-5-1") {
t.Error("the entitled account doesn't list Mythos 5.1")
}
if p.Account.Lists("claude-opus-4-5") {
t.Error("a model the account isn't entitled to is listed")
}
if !p.Account.Lists("claude-sonnet-5") {
t.Error("a model models.dev lists for every account isn't listed")
}
other := &Account{Agent: "claude", User: "someone@example.com"}
claudeCodeState(t, filepath.Join(claudeAccountDir(other.User), ".claude.json"), other.User,
nil, []map[string]any{{"apiName": "claude-mythos-5-1", "entitled": no}}, nil)
filememo.Forget()
if other.Lists("claude-mythos-5-1") {
t.Error("an account not entitled to Mythos 5.1 lists it")
}
if !other.Lists("claude-sonnet-5") {
t.Error("the other account lost what every account has")
}
if nobody := (&Account{Agent: "claude", User: "unknown@example.com"}); !nobody.Lists("claude-mythos-5-1") {
t.Error("an account no Claude Code has said of is taken to lack it")
}
// Claude Code's own picker on this computer offers it too
if own := modelIDs(ClaudeCodeModels()); !slices.Contains(own, "claude-mythos-5-1") || slices.Contains(own, "claude-opus-4-5") {
t.Errorf("ClaudeCodeModels = %v", own)
}
}
// A vendor's own list that has a model models.dev keeps under its lab
// alone gets the lab's window, reply limit, name and thinking: an
// Anthropic API key whose /v1/models has Claude Mythos 5.1.
func TestAnthropicKeyGatedModelDescribed(t *testing.T) {
isolate(t)
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("PATH", h)
for _, v := range agentenv.Vars {
t.Setenv(v, "")
}
mythosCatalogs(t)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/models" {
http.NotFound(w, r)
return
}
w.Write([]byte(`{"data":[{"type":"model","id":"claude-mythos-5-1","created_at":"2026-09-01T00:00:00Z"},` +
`{"type":"model","id":"claude-sonnet-5","display_name":"Claude Sonnet 5"}],"has_more":false}`))
}))
defer srv.Close()
if err := Save(Provider{ID: "anth", Name: "Anthropic key", Anthropic: srv.URL + "/v1", Key: "sk-ant-test"}); err != nil {
t.Fatal(err)
}
q, err := Find("anth")
if err != nil {
t.Fatal(err)
}
if _, err := q.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
e, ok := EntryOf("anth/claude-mythos-5-1")
if !ok {
t.Fatal("Mythos 5.1 not listed")
}
if e.Context != 1_000_000 || e.Output != 128_000 || e.Default != "Claude Mythos 5.1" && e.Name != "Claude Mythos 5.1" || len(e.Efforts) == 0 {
t.Errorf("entry = id %s name %q default %q context %d output %d efforts %v", e.ID, e.Name, e.Default, e.Context, e.Output, e.Efforts)
}
}