diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index e781b811c..5b24ea32a 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -362,6 +362,12 @@ var ( // listed are the bare ids of every model models.dev lists, whatever it // says of them: an id listed is never taken for another (renamed) listed map[string]bool + // canonical are the bare ids of every model models.dev says an entry is + // (canonical_model_id, #1520): DeepSeek's deepseek-flash is + // deepseek/deepseek-v4.1-flash, the id WorkBuddy and the relays serve it + // under, which no catalog lists by it. One is a model as a listed one + // is, so it is never taken for another either (renamed) + canonical map[string]bool syncMu sync.Mutex ) @@ -408,9 +414,13 @@ func load() map[string]mdProvider { // a host voted := map[string]bool{} listed = map[string]bool{} + canonical = map[string]bool{} for pid, p := range m { for id, x := range p.Models { listed[bareID(id)] = true + if _, _, ok := strings.Cut(x.Canonical, "/"); ok { + canonical[bareID(x.Canonical)] = true + } OneHourFor(id, x.Cost) if x.Name != "" && x.Name != id && x.Name != bareID(id) { if named[bareID(id)] == nil { @@ -493,7 +503,7 @@ func Reset() { loadMu.Lock() defer loadMu.Unlock() loaded = false - mdev, images, windows, outputs, efforts, thinks, names, listed = nil, nil, nil, nil, nil, nil, nil, nil + mdev, images, windows, outputs, efforts, thinks, names, listed, canonical = nil, nil, nil, nil, nil, nil, nil, nil, nil } // Sync downloads the models.dev catalog into CachePath. It serializes with @@ -875,16 +885,19 @@ var relayPrefixes = []string{"claude-"} // by b (found), and the window the rename names, if any: a window mark // taken off ("deepseek-v4.1-flash[1m]", "[200k]", and Claude Code's // claude-opus-5[1m], which is claude-opus-5), then a relay's prefix -// ("claude-deepseek-v4.1-flash"). An id models.dev lists is always its own -// model, never renamed; a prefix comes off only when what is left is -// listed, and names a version ("claude-auto" is not the router "auto"). +// ("claude-deepseek-v4.1-flash"). An id models.dev lists, or knows as an +// entry's canonical_model_id, is always its own model, never renamed +// (cortecs's claude-sonnet-4 is claude-sonnet-4-0, not sonnet-4-0); a +// prefix comes off only when what is left is known — listed, or an entry's +// canonical_model_id (#1520) — and names a version ("claude-auto" is not +// the router "auto"). func renamed(b string) (string, int, bool) { - if _, ok := found(b, listed); ok { + if _, ok := found(b, listed); ok || canonical[b] { return "", 0, false } r, w := unmarked(b) if r != b { - if _, ok := found(r, listed); ok { + if knownModel(r) { return r, w, true } } @@ -893,13 +906,23 @@ func renamed(b string) (string, int, bool) { if !ok || !strings.ContainsAny(rest, "0123456789") { continue } - if _, ok := found(rest, listed); ok { + if knownModel(rest) { return rest, w, true } } return "", 0, false } +// knownModel is whether models.dev knows b as a model: listed by its id, +// or named as an entry's canonical_model_id (#1520) — a model no catalog +// lists by it, which relays serve and Claude Code asks for under a prefix. +func knownModel(b string) bool { + if _, ok := found(b, listed); ok { + return true + } + return canonical[b] +} + // unmarked is a bare id without a window mark at its end ("[1m]", // "[200k]", "[1.5m]") and the window it names, or the id and 0 without one. func unmarked(b string) (string, int) { diff --git a/internal/catalog/renamed_test.go b/internal/catalog/renamed_test.go index 0995e3d49..eae8b32ec 100644 --- a/internal/catalog/renamed_test.go +++ b/internal/catalog/renamed_test.go @@ -136,6 +136,97 @@ func TestClaude1MMarkIsTheClaudeModel(t *testing.T) { } } +// canonicalRenameCatalog is models.dev's api.json as fetched on 2026-10-11 +// for the entries in the shape this is about: OpenAI lists its cyber model +// as gpt-daybreak-red-latest, canonically openai/gpt-5.6-cyber, the id the +// relays serve it under — and no catalog lists gpt-5.6-cyber by it (one of +// the canonical-only ids, as deepseek-v4.1-flash was before DeepSeek's own +// resellers listed it). +const canonicalRenameCatalog = `{ + "openai": {"name":"OpenAI","models": { + "gpt-daybreak-red-latest": {"id":"gpt-daybreak-red-latest","name":"Daybreak Red", + "modalities":{"input":["text","image"],"output":["text"]}, + "cost":{"input":12.5,"output":75,"cache_read":1.25,"cache_write":15.625}, + "limit":{"context":400000,"input":272000,"output":128000}, + "canonical_model_id":"openai/gpt-5.6-cyber"}}} +}` + +// A model models.dev publishes only as another entry's canonical_model_id +// is a model like a listed one, so a relay's rename of it renames too, and +// its price and a window mark go with the rename (#1520 on the id +// #1383's relays put their prefix in front of). +func TestRelayRenameReachesACanonicalOnlyID(t *testing.T) { + writeCatalog(t, canonicalRenameCatalog) + + // the bare canonical id is priced by its maker's entry (#1520) + if p, ok := PricedBy([]string{"openai"}, "gpt-5.6-cyber"); !ok || p.Input != 12.5 { + t.Fatalf("PricedBy(gpt-5.6-cyber) = %+v, %v, want 12.5", p, ok) + } + for _, id := range []string{"claude-gpt-5.6-cyber", "gpt-5.6-cyber[1m]", "claude-gpt-5.6-cyber[1m]"} { + if p, ok := PricedBy([]string{"openai"}, id); !ok || p.Input != 12.5 { + t.Errorf("PricedBy(%q) = %+v, %v, want 12.5", id, p, ok) + } + } + // and the window mark on the renamed id names the window, as a listed + // model's does + if got := ContextOf("claude-gpt-5.6-cyber[1m]"); got != 1_000_000 { + t.Errorf("ContextOf(claude-gpt-5.6-cyber[1m]) = %d, want 1000000", got) + } + // a near miss of the canonical id is still no model + for _, id := range []string{"claude-gpt-5.6-cyberx", "claude-gpt-5.6", "gpt-5.6-cyber[fast]"} { + if p, ok := PricedBy([]string{"openai"}, id); ok { + t.Errorf("%q priced %+v", id, p) + } + } +} + +// canonicalPrefixCatalog is models.dev's api.json as fetched on 2026-10-11 +// for the entries in the shape the guard is about: Cortecs lists Claude's +// Sonnet 4 as claude-sonnet-4, canonically anthropic/claude-sonnet-4-0 — +// the canonical id itself starts with the prefix a relay puts in front — +// and no catalog lists claude-sonnet-4-0 by it (one of the canonical-only +// ids). A catalog naming a sonnet-4-0 of its own, here Drift's, must not +// take Claude's model for it. +const canonicalPrefixCatalog = `{ + "cortecs": {"name":"Cortecs","models": { + "claude-sonnet-4": {"id":"claude-sonnet-4","name":"Claude Sonnet 4 (latest)", + "modalities":{"input":["text","image","pdf"],"output":["text"]}, + "cost":{"input":2.989,"output":14.945,"cache_read":0.296,"cache_write":3.707}, + "limit":{"context":200000,"output":65000}, + "canonical_model_id":"anthropic/claude-sonnet-4-0"}}}, + "drift": {"name":"Drift","models": { + "sonnet-4-0": {"id":"sonnet-4-0","name":"Drift Sonnet", + "modalities":{"input":["text"],"output":["text"]}, + "cost":{"input":99,"output":99}, + "limit":{"context":32000,"output":4000}}}} +}` + +// A canonical id is a model as a listed one is, so it is never taken for +// another: claude-sonnet-4-0 starts with a relay's prefix, and what is left +// names a version, but the id is its own model, not Drift's sonnet-4-0. +func TestCanonicalIDIsNeverTakenForAnother(t *testing.T) { + writeCatalog(t, canonicalPrefixCatalog) + + if p, ok := PricedBy([]string{"drift"}, "claude-sonnet-4-0"); ok { + t.Errorf("PricedBy(drift, claude-sonnet-4-0) = %+v, want none", p) + } + // no data of its own yet: unknown, not Drift's window or name + if got := ContextOf("claude-sonnet-4-0"); got != 0 { + t.Errorf("ContextOf(claude-sonnet-4-0) = %d, want 0", got) + } + if got := NameOf("claude-sonnet-4-0"); got != "" { + t.Errorf("NameOf(claude-sonnet-4-0) = %q, want none", got) + } + // the model the id is Drift's for keeps its own price, and a listed id + // in front of the prefix is priced by its own entry + if p, ok := PricedBy([]string{"drift"}, "sonnet-4-0"); !ok || p.Input != 99 { + t.Errorf("PricedBy(drift, sonnet-4-0) = %+v, %v, want 99", p, ok) + } + if p, ok := PricedBy([]string{"cortecs"}, "claude-sonnet-4"); !ok || p.Input != 2.989 { + t.Errorf("PricedBy(cortecs, claude-sonnet-4) = %+v, %v, want 2.989", p, ok) + } +} + // A listed id is always its own model, and a near miss is no model. func TestRenameNeverTakesAModelForAnother(t *testing.T) { writeCatalog(t, renamedCatalog)