Files
magpie/internal/provider/codex_apart_test.go
T
yetone ac15036ab4 provider: a catalog entry points at its provider instead of copying it
Entry.Provider was a Provider value, 1,328 of an Entry's 1,760 bytes,
copied into every model of every provider each time a catalog, an
agent's list or a routing group's entries were built. It is now a
*Provider: entriesOf and decide's list give every model of one provider
the same one (sharedEntry), and a group's entry points at its first
member's. It is read only; every place that hands it on as a value
(Resolve, CanFast, ownAgent, modelAllowed, …) dereferences it, so what
they get is a copy as before. entryFor keeps its signature for the one
model ServedEntryOf builds; offEntries' entries now carry their provider
too.

Measured on mote with the owner's real accounts (acctp sandbox, page
open for 2m32s, w40 = before, w41 = this, alternating):
- catalogOf's allocations 106 MB -> 67 MB, providerEntries' 88 -> 78 MB
- CPU at the page's settle mark 3.42/3.33 s -> 3.18/3.30 s
- live heap unchanged (76.3/76.1 -> 76.7/75.9 MB): the lists are
  rebuilt, not kept, so the copies were churn for the GC, not held

Run: go build and go vet ./... and the tests compiled (go test -run ^$)
on mote; the full suite at this tree (d1e823d5f) on mica: 99 packages ok, 0 FAIL (agent 317s, gateway 972s, gui 188s, provider 220s, usage 56s).
2026-11-13 05:12:30 +08:00

58 lines
2.4 KiB
Go

package provider
import (
"encoding/json"
"strings"
"testing"
"github.com/yetone/magpie/internal/codexcat"
)
// Two providers both called Codex, each serving GPT-6.1-Sol (nb5p on
// Discord), are named apart in Codex's picker by their provider's id, and
// two of one provider by their own id. Only display_name says it: the
// description and Name, which go upstream in spawn_agent's list of models,
// stay as they were, and an entry no other shares a name with is shown as
// before.
func TestCodexPickerNamesSameNamesApart(t *testing.T) {
a := Provider{ID: "codex", Name: "Codex", Account: &Account{Agent: "codex", User: "a@example.com"}}
b := Provider{ID: "codex-b", Name: "Codex", Account: &Account{Agent: "codex", User: "b@example.com"}}
r := Provider{ID: "relay", Name: "Relay"}
ms := codexListed([]Entry{
{ID: "codex/gpt-6.1-sol", Model: "gpt-6.1-sol", Name: "GPT-6.1-Sol", Provider: &a},
{ID: "codex-b/gpt-6.1-sol", Model: "gpt-6.1-sol", Name: "GPT-6.1-Sol", Provider: &b},
{ID: "relay/glm-5.3", Model: "glm-5.3", Name: "GLM-5.3", Provider: &r},
{ID: "relay/glm-5.3-old", Model: "glm-5.3-old", Name: "GLM-5.3", Provider: &r},
{ID: "relay/kimi-k3", Model: "kimi-k3", Name: "Kimi K3", Provider: &r},
}, func(string) []Member { return nil }, true)
type shown struct{ name, desc string }
want := []shown{
{"GPT-6.1-Sol · Codex · codex", "GPT-6.1-Sol · Codex via magpie"},
{"GPT-6.1-Sol · Codex · codex-b", "GPT-6.1-Sol · Codex via magpie"},
{"GLM-5.3 · Relay · relay/glm-5.3", "GLM-5.3 · Relay via magpie"},
{"GLM-5.3 · Relay · relay/glm-5.3-old", "GLM-5.3 · Relay via magpie"},
{"Kimi K3 · Relay", "Kimi K3 · Relay via magpie"},
}
es := codexcat.Entries(ms, 0)
if len(es) != len(want) {
t.Fatalf("%d entries, want %d", len(es), len(want))
}
for i, e := range es {
b, _ := json.Marshal(e)
var got struct {
Name string `json:"display_name"`
Desc string `json:"description"`
}
json.Unmarshal(b, &got)
// the text-only note is beside the point here
got.Desc = strings.Replace(got.Desc, " (text only: Accepts images is in its provider's Model settings in magpie)", "", 1)
if (shown{got.Name, got.Desc}) != want[i] {
t.Errorf("%s: shown as %q, %q; want %q, %q", ms[i].ID, got.Name, got.Desc, want[i].name, want[i].desc)
}
// what goes upstream in spawn_agent's list (spawnChoices) is Name
if ms[i].Name != strings.TrimSuffix(want[i].desc, " via magpie") {
t.Errorf("%s: Name %q changed", ms[i].ID, ms[i].Name)
}
}
}