Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
usage: a reply under the name its provider's list gives the model is not a swap
yetone841bcf8da7f4
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.goindex 674e418f9..cc1f04659 100644--- a/internal/gateway/gateway.go+++ b/internal/gateway/gateway.go@@ -2962,7 +2962,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Protry.Reset, resetFirst = resetFirst, nil}asName := provider.SentNameOnIn(wiresOf(r.Context()), c.p.ID, accountAgent(c.p), c.model, sent)- try.Swapped, try.Routed = swapped(asName, call.Usage.Served), usage.GroupRouted(asName, call.Usage.Served)+ try.Swapped, try.Routed = swapped(asName, call.Usage.Served) && !usage.ListedAs(c.p.ID, c.model, call.Usage.Served), usage.GroupRouted(asName, call.Usage.Served)try.TTFT, try.FirstText = hw.first.ms()try.FirstReasoning = hw.first.reasoningMs(call.To)try.ReasoningHidden = hw.first.hidesReasoning(call.To)diff --git a/internal/gateway/history.go b/internal/gateway/history.goindex addbdd241..b9e014860 100644--- a/internal/gateway/history.go+++ b/internal/gateway/history.go@@ -401,12 +401,13 @@ func tally(d dayFile, c counted) counted {// answered as deepseek-v4-1-flash) wasn't swapped (usage.SameSpelled), nor// was one answered under Google's name for the deployment serving its// Gemini model (gemini-3.8-flash as gemini-3.8-flash-n) or xAI's for a Grok-// one (grok-4.7 as grok-4.7-build), usage.ServingName.+// one (grok-4.7 as grok-4.7-build), usage.ServingName, nor one answered+// under the name its provider's list gives it (usage.ListedAs).func (r *Route) routedAgain() {for i := range r.Tries {if tr := &r.Tries[i]; tr.Swapped && usage.GroupRouted(tr.Model, tr.Served) {tr.Swapped, tr.Routed = false, true- } else if tr.Swapped && (usage.SameSpelled(tr.Model, tr.Served) || usage.ServingName(tr.Model, tr.Served)) {+ } else if tr.Swapped && (usage.SameSpelled(tr.Model, tr.Served) || usage.ServingName(tr.Model, tr.Served) || usage.ListedAs(tr.ID, tr.Model, tr.Served)) {tr.Swapped = false}}diff --git a/internal/gateway/history_listed_test.go b/internal/gateway/history_listed_test.gonew file mode 100644index 000000000..f90d3a574--- /dev/null+++ b/internal/gateway/history_listed_test.go@@ -0,0 +1,24 @@+package gateway++import (+ "testing"++ "github.com/yetone/magpie/internal/usage"+)++// A route kept with a try marked swapped for the name its provider's list+// gives the model (Qoder's qfmodel answered as Qwen3.8-Flash, jzing.draw+// on Discord, 10-27) is read as not swapped; another model stays swapped.+func TestRouteKeptListedNameNotSwapped(t *testing.T) {+ t.Cleanup(usage.ListedForTest(map[string]map[string]string{"qoder": {"qfmodel": "Qwen3.8-Flash"}}))+ r := &Route{Swapped: true, Served: "Qwen3.8-Flash", Tries: []Try{{ID: "qoder", Model: "qfmodel", Served: "Qwen3.8-Flash", Swapped: true}}}+ r.routedAgain()+ if r.Swapped || r.Tries[0].Swapped {+ t.Fatalf("still swapped: %+v", r)+ }+ r = &Route{Swapped: true, Served: "Qwen3.7-Flash", Tries: []Try{{ID: "qoder", Model: "qfmodel", Served: "Qwen3.7-Flash", Swapped: true}}}+ r.routedAgain()+ if !r.Swapped || !r.Tries[0].Swapped {+ t.Fatalf("a real swap was cleared: %+v", r)+ }+}diff --git a/internal/usage/block_price.go b/internal/usage/block_price.goindex 1743586e7..19d38aa17 100644--- a/internal/usage/block_price.go+++ b/internal/usage/block_price.go@@ -60,7 +60,7 @@ func (b *blockPricer) as(prov, model, served, effort string) pricedAs {}a := pricedAs{provider: prov, flags: swapKnown}sent := provider.SentNameIn(b.wires, prov, model, effort)- if served != "" && Swapped(sent, served) && !(SameSpelled(model, served) || ServingName(model, served) || RelayRenamed(model, served)) {+ if served != "" && Swapped(sent, served) && !(SameSpelled(model, served) || ServingName(model, served) || RelayRenamed(model, served) || ListedAs(prov, model, served)) {a.flags |= 2}if GroupRouted(sent, served) {diff --git a/internal/usage/ledger.go b/internal/usage/ledger.goindex 916e4389b..9524a724a 100644--- a/internal/usage/ledger.go+++ b/internal/usage/ledger.go@@ -441,7 +441,7 @@ func ledgerMatched(since, until time.Time, f Filter, recs []Record, logs []sessiif source == "" {sent = provider.SentNameIn(wires, r.Provider, r.Model, r.Effort)}- row := codexIdentityRow(Row{Record: r, Swapped: r.Served != "" && Swapped(sent, r.Served), Routed: GroupRouted(sent, r.Served), Source: source}, actor, codexIdentities)+ row := codexIdentityRow(Row{Record: r, Swapped: r.Served != "" && Swapped(sent, r.Served) && !ListedAs(r.Provider, r.Model, r.Served), Routed: GroupRouted(sent, r.Served), Source: source}, actor, codexIdentities)if !f.keepsRow(&row) {return}diff --git a/internal/usage/request_page.go b/internal/usage/request_page.goindex 433a7df8c..3342f1e83 100644--- a/internal/usage/request_page.go+++ b/internal/usage/request_page.go@@ -316,6 +316,8 @@ func (c *rowChunk) row(i int) Row {}if r.Swapped && p.Flags&swapKnown == 0 && (SameSpelled(r.Model, r.Served) || ServingName(r.Model, r.Served) || RelayRenamed(r.Model, r.Served)) {r.Swapped = false // kept before a name spelled otherwise, a vendor's serving name or a relay's rename was the same+ } else if r.Swapped && ListedAs(r.Provider, r.Model, r.Served) {+ r.Swapped = false // the name the provider's list gives the model asked for}if r.cacheNotCounted() {r.CacheUnknown = true // kept before its plugin said its vendor counts no cache@@ -860,7 +862,7 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti// reader of the session files has a pricer of its own, with its memos.pricers := func() func(Record, string) Row {price := Pricer()- swapped := map[[2]string]bool{}+ swapped := map[[4]string]bool{}return func(r Record, source string) Row {sent := r.Modelif source == "" {@@ -868,10 +870,10 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti}row := Row{Record: r, Source: source, Routed: GroupRouted(sent, r.Served)}if r.Served != "" {- k := [2]string{sent, r.Served}+ k := [4]string{r.Provider, r.Model, sent, r.Served}s, ok := swapped[k]if !ok {- s = Swapped(sent, r.Served)+ s = Swapped(sent, r.Served) && !ListedAs(r.Provider, r.Model, r.Served)swapped[k] = s}row.Swapped = sdiff --git a/internal/usage/served.go b/internal/usage/served.goindex 3130b530e..6019d2c2b 100644--- a/internal/usage/served.go+++ b/internal/usage/served.go@@ -3,6 +3,8 @@ package usageimport ("regexp""strings"+ "sync"+ "time""unicode""github.com/yetone/magpie/internal/provider"@@ -65,6 +67,84 @@ func Swapped(sent, served string) bool {return a != "" && b != "" && squash(a) != squash(b) && a != "auto" && squash(bareModel(provider.EffortFamily(a))) != squash(b) && !GroupRouted(sent, served) && !ServingName(sent, served) && !RelayRenamed(sent, served)}+// ListedAs reports whether served is the name the provider's own model+// list gives the model asked for: Qoder's plugin lists its free Qwen model+// as qfmodel, named Qwen3.8-Flash, and since 0.2.38 (plugins#412) answers+// under that name, which was marked a swap to "actual Qwen3.8-Flash"+// (jzing.draw on Discord, 10-27). The list is the provider's word that+// the two are one model. It is asked only once a reply already reads as+// swapped, and each provider's list is read at most once a minute.+func ListedAs(prov, model, served string) bool {+ if prov == "" || model == "" || served == "" {+ return false+ }+ n := listedNames(prov)[model]+ return n != "" && n != model && SameSpelled(n, served)+}++// listedFor is how long ListedAs keeps a provider's names.+const listedFor = time.Minute++var listed struct {+ sync.Mutex+ by map[string]listedEntry+}++type listedEntry struct {+ names map[string]string+ at time.Time+}++// listedModels is the provider's models as magpie lists them; a test+// replaces it.+var listedModels = func(prov string) map[string]string {+ p, err := provider.Find(prov)+ if err != nil {+ return nil+ }+ out := map[string]string{}+ for _, m := range p.Available() {+ if m.Name != "" {+ out[m.ID] = m.Name+ }+ }+ return out+}++// ListedForTest gives ListedAs these lists, provider by model id, and+// forgets every list read before, until restore.+func ListedForTest(lists map[string]map[string]string) (restore func()) {+ old := listedModels+ listedModels = func(prov string) map[string]string { return lists[prov] }+ forgetListed()+ return func() { listedModels = old; forgetListed() }+}++func forgetListed() {+ listed.Lock()+ listed.by = nil+ listed.Unlock()+}++func listedNames(prov string) map[string]string {+ listed.Lock()+ e, ok := listed.by[prov]+ listed.Unlock()+ if ok && time.Since(e.at) < listedFor {+ return e.names+ }+ // read outside the lock: a provider's list can take a moment, and+ // another reader of another provider needn't wait on it+ names := listedModels(prov)+ listed.Lock()+ if listed.by == nil {+ listed.by = map[string]listedEntry{}+ }+ listed.by[prov] = listedEntry{names: names, at: time.Now()}+ listed.Unlock()+ return names+}+// RelayRenamed reports whether sent is served's own name with a relay's// word in front of it: a relay that sells kimi-k3 as moonshot-kimi-k3 is// answered as kimi-k3 (#1498, liuweifeng), one that sellsdiff --git a/internal/usage/served_listed_test.go b/internal/usage/served_listed_test.gonew file mode 100644index 000000000..fcfb4589b--- /dev/null+++ b/internal/usage/served_listed_test.go@@ -0,0 +1,96 @@+package usage++import (+ "testing"+ "time"++ "github.com/yetone/magpie/internal/catalog"+ "github.com/yetone/magpie/internal/provider"+)++// listedAs gives ListedAs these lists, provider by model id, for the+// test's length, and forgets every list read before.+func listedAs(t *testing.T, lists map[string]map[string]string) {+ t.Helper()+ t.Cleanup(ListedForTest(lists))+}++// Qoder's plugin lists its free Qwen model as qfmodel, named+// Qwen3.8-Flash, and answers under that name (plugins#412): the model+// asked for, not another one swapped in (jzing.draw on Discord, 10-27).+// Another name, another provider's list or another model of the same+// list is still a swap.+func TestListedNameIsTheModelAskedFor(t *testing.T) {+ listedAs(t, map[string]map[string]string{"qoder": {"qfmodel": "Qwen3.8-Flash", "dfmodel": "DeepSeek-V4.1-Flash", "auto": "auto"}})+ if !Swapped("qfmodel", "Qwen3.8-Flash") {+ t.Fatal("the names alone read alike; the list is what tells them apart")+ }+ for _, c := range [][3]string{{"qoder", "qfmodel", "Qwen3.8-Flash"}, {"qoder", "qfmodel", "qwen3.8-flash"}, {"qoder", "dfmodel", "DeepSeek-V4.1-Flash"}} {+ if !ListedAs(c[0], c[1], c[2]) {+ t.Errorf("%s/%s answered as %s is the model its list names", c[0], c[1], c[2])+ }+ }+ for _, c := range [][3]string{+ {"qoder", "qfmodel", "DeepSeek-V4.1-Flash"}, // another of its models+ {"qoder", "qfmodel", "Qwen3.7-Flash"},+ {"qoder", "auto", "auto"}, // a name no list gives apart from the id+ {"other", "qfmodel", "Qwen3.8-Flash"}, // a list that doesn't name it+ {"qoder", "nomodel", "Qwen3.8-Flash"},+ {"", "qfmodel", "Qwen3.8-Flash"},+ } {+ if ListedAs(c[0], c[1], c[2]) {+ t.Errorf("%s/%s answered as %s is another model", c[0], c[1], c[2])+ }+ }+}++// A provider's list is read once a minute at most, however many rows ask.+func TestListedNamesReadOnceAMinute(t *testing.T) {+ n := 0+ old := listedModels+ listedModels = func(string) map[string]string { n++; return map[string]string{"qfmodel": "Qwen3.8-Flash"} }+ forgetListed()+ t.Cleanup(func() { listedModels = old; forgetListed() })+ for range 1000 {+ ListedAs("qoder", "qfmodel", "Qwen3.8-Flash")+ }+ if n != 1 {+ t.Fatalf("read the list %d times", n)+ }+ listed.Lock()+ e := listed.by["qoder"]+ e.at = time.Now().Add(-listedFor - time.Second)+ listed.by["qoder"] = e+ listed.Unlock()+ ListedAs("qoder", "qfmodel", "Qwen3.8-Flash")+ if n != 2 {+ t.Fatalf("a list a minute old wasn't read again (%d reads)", n)+ }+}++// A row kept marked swapped for the name its provider's list gives the+// model is read back as not swapped; another model stays swapped.+func TestKeptRowListedNameNotSwapped(t *testing.T) {+ listedAs(t, map[string]map[string]string{"qoder": {"qfmodel": "Qwen3.8-Flash"}})+ c := &rowChunk{}+ c.add(Row{Record: Record{Provider: "qoder", Model: "qfmodel", Served: "Qwen3.8-Flash"}, Swapped: true}, "", 0, false)+ c.add(Row{Record: Record{Provider: "qoder", Model: "qfmodel", Served: "Qwen3.7-Flash"}, Swapped: true}, "", 1, false)+ for _, chunk := range []*rowChunk{c, c.pack().unpack()} {+ if chunk.row(0).Swapped || !chunk.row(1).Swapped {+ t.Fatalf("listed name %v, other model %v", chunk.row(0).Swapped, chunk.row(1).Swapped)+ }+ }+}++// A gateway call priced again reads the listed name as the model asked+// for, as the row does.+func TestBlockPricerListedNameNotSwapped(t *testing.T) {+ listedAs(t, map[string]map[string]string{"qoder": {"qfmodel": "Qwen3.8-Flash"}})+ b := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil })+ if a := b.as("qoder", "qfmodel", "Qwen3.8-Flash", ""); a.flags&2 != 0 {+ t.Fatalf("listed name priced as a swap: %b", a.flags)+ }+ if a := b.as("qoder", "qfmodel", "Qwen3.7-Flash", ""); a.flags&2 == 0 {+ t.Fatalf("another model priced as not swapped: %b", a.flags)+ }+}