From 841bcf8da7f415a68a54c690dfd6c2f779602cab Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 27 Oct 2026 18:54:26 +0800 Subject: [PATCH] usage: a reply under the name its provider's list gives the model is not a swap jzing.draw (Discord #general, 10-27) had a Usage row for Qoder's free Qwen model, sent as qfmodel, tagged "actual Qwen3.8-Flash". Since plugins#412/#413 (qoder 0.2.38) the plugin answers under the model list's display name, and Qoder's list names qfmodel Qwen3.8-Flash, so it is the model asked for. Swapped compares names alone and can't know that. usage.ListedAs(provider, model, served) reads the provider's own list (Provider.Available, once a minute per provider, only once a reply already reads as swapped) and holds when served is the name it gives the model, spelled however (SameSpelled). Every place a swap is set or read again asks it: the gateway's try, the ledger and request-page rows, block pricing, a kept row read back (request_page unpack) and a kept route (history routedAgain), so old rows lose the false tag too. Run at this tree (base ca8a6f346): - go vet ./internal/usage ./internal/gateway on darwin, linux (nogui) and windows; gofmt -l clean - go test -count=1 -timeout 45m -p 1 ./internal/usage ./internal/gateway: usage ok 146s, gateway ok 887s - the new tests (TestListedNameIsTheModelAskedFor, TestListedNamesReadOnceAMinute, TestKeptRowListedNameNotSwapped, TestBlockPricerListedNameNotSwapped, TestRouteKeptListedNameNotSwapped) pass Broken by hand: with the call sites put back (served.go's ListedAs kept), TestKeptRowListedNameNotSwapped, TestBlockPricerListedNameNotSwapped and TestRouteKeptListedNameNotSwapped fail. Real use: a binary built from this tree, under a sandbox HOME holding a copy of the owner's ~/.config/magpie, reads the real Qoder list: qoder/qfmodel answered as Qwen3.8-Flash and dmodel as DeepSeek-V4-Pro are listed (not swaps), qfmodel answered as GLM-5.3 is not. Not tried: a live gateway call through the Qoder plugin; the full go test ./... (other packages don't call ListedAs); the gateway try's line has no test of its own beyond the kept-route read, which uses the same ListedAs. --- internal/gateway/gateway.go | 2 +- internal/gateway/history.go | 5 +- internal/gateway/history_listed_test.go | 24 +++++++ internal/usage/block_price.go | 2 +- internal/usage/ledger.go | 2 +- internal/usage/request_page.go | 8 ++- internal/usage/served.go | 80 +++++++++++++++++++++ internal/usage/served_listed_test.go | 96 +++++++++++++++++++++++++ 8 files changed, 211 insertions(+), 8 deletions(-) create mode 100644 internal/gateway/history_listed_test.go create mode 100644 internal/usage/served_listed_test.go diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 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.Pro try.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.go index 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.go new file mode 100644 index 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.go index 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.go index 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 []sessi if 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.go index 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.Model if 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 = s diff --git a/internal/usage/served.go b/internal/usage/served.go index 3130b530e..6019d2c2b 100644 --- a/internal/usage/served.go +++ b/internal/usage/served.go @@ -3,6 +3,8 @@ package usage import ( "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 sells diff --git a/internal/usage/served_listed_test.go b/internal/usage/served_listed_test.go new file mode 100644 index 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) + } +}