Files
magpie/internal/gui/agent_models_codex_group_test.go
T
yetone 88e6fb9cf7 provider: a signed-in subscription's models stay in every agent's list with a group listing alone on
士奇 on Discord (Simplified Chinese GUI, the third report of this): with
routing groups off, Hermes Agent's and WorkBuddy's model lists had the
ChatGPT subscription's GPT models. With routing groups on, they were gone.

Cause: the user's group names those models and lists itself alone
(Group.Unlists, the routing plugin's option for that, #1783). While the
group is on, catalogKeeping drops what it names from Catalog(), and every
agent's list starts from Catalog(). With groups gated off the group's
Unlists goes away, so the models came back. a3ee00c05 kept them for the
Codex agent only; its body said other agents still didn't see them.

Fix, at the one point every agent's list starts from: catalogOf(agent)
keeps a signed-in subscription's own models (SubscriptionKept: no group
entry, Provider.Account set, the provider not kept for routing groups)
for every agent, so a group is added on top of what agents had. A client
that is no agent ("" on /v1/models) still gets the group alone, as #1783
asked. Provider.Unlisted ("Only through routing groups") still hides the
provider's models from agents other than Codex. Unlisted() no longer names
a kept subscription model, so the picker's note doesn't call it kept for
groups.

Siblings: every agent writer reaches catalogOf through CatalogFor and
ListedFor. Two gateway lookups read Catalog() directly and now use the
agent's catalog (CatalogOf): /v1/models/{id} and Claude Desktop/Science's
alias lookup. agent.go and codex.go read Catalog() only for error text or
a fallback and are left alone. The routing plugin's hint for that option
says, in every language, that a signed-in subscription's models stay in
agents' lists.

Test: TestSubscriptionModelsStayInEveryAgentsListWithGroupOn signs in a
ChatGPT account in a sandbox and switches on a group naming its 5 models
that lists itself alone. For every agent that lists the group (68,
including hermes, workbuddy, claude and codex), the agent's list and
CatalogFor have the models, and a picker offering the group offers them.
Catalog() and CatalogFor("") still don't. With the provider set to
Unlisted, Hermes doesn't list them. TestCodexOwnModelsListedOnlyThroughGroups
now expects Claude Code to keep the models under a group alone.

Run (on mote):
- Broken by hand: catalogOf reverted to a3ee00c05's Codex-only version.
  The new test went red with 340 errors across 68 agents, e.g.
  "codex/gpt-5.5 not in hermes's list map[group/mygpt:true]", and the
  same for workbuddy, claude and claude-desktop. Green with the fix back.
- gofmt -l; go vet on darwin, linux (nogui) and windows; builds: clean.
- go test -timeout 45m ./... at 035d87b80 (this change on 4a4a90c23,
  before a test comment was reworded): every package ok except
  - internal/gui TestMainWindowCorners/macOS_26_SDK: #3949, red on
    origin/main accb951ab on mote too (macOS 27);
  - internal/provider TestClaudeAccountServesModelsListedSinceItsFetch:
    #3174 (red in a full run, green alone). 5 of 5 green alone and
    green in a whole internal/provider run at that head.
- At this commit on b26fa0903: gofmt -l on the
  changed Go files, go vet of provider, gateway and gui on darwin, linux
  (nogui) and windows: clean. internal/provider whole: ok.
  internal/gui and internal/gateway with -run
  "Codex|Group|Subscription|Unlist|Catalog|AgentModel": gui ok; gateway
  red on TestAntigravityTurnsCodexsGPT5ThreadAwayOnce and
  TestCodexSpentGrokFailingIsTold, both red the same way on origin/main
  b26fa0903 (3 of 3) and filed as #4362.
- GUI files the change reaches, on b26fa0903: agent-model*, group-unlists and
  i18n-keys: every Chromium case and i18n-keys pass (46); every WebKit
  case timed out launching the browser (180s, #4125: WebKit doesn't start
  on mote right now). The same files at 035d87b80 passed in both engines
  but i18n-keys, whose one red (the new test quoted a routing-plugin
  word) is fixed here.
- Rebased on 101775657 (main had moved models.go and gateway.go): gofmt,
  go build for darwin, linux (nogui) and windows, go vet of provider,
  gateway and gui on all three: clean. go test -run
  "Codex|Group|Subscription|Unlist|Catalog|AgentModel" ./internal/gui
  ./internal/provider and -run "Model|Desktop|Alias|Catalog"
  ./internal/gateway: ok.
- The whole GUI suite (1004 files) at fd50ed116 was still running when
  this was pushed (mote shared with other sessions' suites). Reds it
  shows get their own issue unless already red on main.

Not tried: no live ChatGPT account and no real GUI app run (the test signs
in a sandbox account). No separate gateway test for /v1/models/{id} or the
Desktop alias lookup. Provider.Unlisted stays honoured, on purpose.
2026-10-30 15:38:06 +08:00

281 lines
10 KiB
Go

package gui
import (
"encoding/base64"
"encoding/json"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// Codex's own models, a ChatGPT account's, stay in its model picker with a
// routing group switched on and its own models switched off (shiqi_40904 on
// Discord: "开启路由组后 … 模型列表里不会显示 codex 模型"). magpie's list shows
// them Codex as its Codex section, and its pickers offer them as Codex's own
// (codex/… would only add a hop), so the switch for the agent's own models
// never takes them out. It did once the group gave the picker one of
// magpie's models, which is what the switch waits for: the picker was the
// group alone, and with the group off every model was back.
func TestCodexOwnModelsStayWithGroupOn(t *testing.T) {
ids := codexSignedInSandbox(t)
if err := provider.SetOwnModelsHidden("codex", true); err != nil {
t.Fatal(err)
}
codex, err := agent.Find("codex")
if err != nil {
t.Fatal(err)
}
picks := func() (out []string) {
t.Helper()
for _, f := range agentFields(codex, codex.Values()) {
if f.Key == "model" {
for _, o := range f.Options {
out = append(out, o.Value)
}
}
}
return out
}
for _, id := range ids {
if got := picks(); !slices.Contains(got, id) {
t.Fatalf("group off: %s not in Codex's picker %v", id, got)
}
}
if err := provider.SaveGroup(provider.Group{ID: "mygpt", Name: "MyGPT", Members: []string{"codex/gpt-5.5", "codex/gpt-5.4"}}); err != nil {
t.Fatal(err)
}
got := picks()
for _, id := range append(slices.Clone(ids), "group/mygpt") {
if !slices.Contains(got, id) {
t.Fatalf("group on: %s not in Codex's picker %v", id, got)
}
}
// nothing of Codex's own is left for the switch to take out, so the
// list has none to show
if codex.OwnModels(codex.Values()) {
t.Fatal("Codex's list offers a switch for its own models, every one of them magpie's too")
}
// one the list takes out is the switch's again: magpie no longer shows
// it Codex
if err := provider.SetHiddenModels("codex", []string{"codex/gpt-5.4"}); err != nil {
t.Fatal(err)
}
if got := picks(); slices.Contains(got, "gpt-5.4") || !slices.Contains(got, "gpt-5.4-mini") {
t.Fatalf("gpt-5.4 taken out of the list, own models off: picker %v", got)
}
}
// codexSignedInSandbox signs a ChatGPT account in to a sandbox Codex, with
// the models its backend lists, and returns their ids.
func codexSignedInSandbox(t *testing.T) []string {
t.Helper()
home := sandboxHome(t)
codexHome := filepath.Join(home, ".codex")
t.Setenv("CODEX_HOME", codexHome)
os.MkdirAll(codexHome, 0o700)
jwt := func(c any) string {
b, _ := json.Marshal(c)
return "e30." + base64.RawURLEncoding.EncodeToString(b) + ".sig"
}
auth, _ := json.Marshal(map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{
"id_token": jwt(map[string]any{"email": "a@x.y", "https://api.openai.com/auth": map[string]any{"chatgpt_plan_type": "plus", "chatgpt_account_id": "acc"}}),
"access_token": jwt(map[string]any{"exp": time.Now().Add(24 * time.Hour).Unix()}), "refresh_token": "r", "account_id": "acc"}})
os.WriteFile(filepath.Join(codexHome, "auth.json"), auth, 0o600)
os.WriteFile(filepath.Join(codexHome, "config.toml"), []byte("model = \"gpt-5.5\"\n"), 0o600)
ids := []string{"gpt-5.5", "gpt-5.4", "gpt-5.4-mini", "gpt-5.3-codex", "gpt-5.2"}
var cache []map[string]any
var live []catalog.Model
for i, id := range ids {
cache = append(cache, map[string]any{"slug": id, "display_name": id, "visibility": "list", "priority": i, "context_window": 400000})
live = append(live, catalog.Model{ID: id, Context: 400000})
}
b, _ := json.Marshal(map[string]any{"etag": "x", "models": cache})
os.WriteFile(filepath.Join(codexHome, "models_cache.json"), b, 0o600)
provider.ForgetAccounts()
t.Cleanup(provider.ForgetAccounts)
catalog.Reset()
t.Cleanup(catalog.Reset)
if err := catalog.SaveLive("codex", provider.CodexBase, live); err != nil {
t.Fatal(err)
}
return ids
}
// The codex provider "Only through routing groups" (只通过路由分组使用) keeps
// the ChatGPT account's models out of other agents' lists but in Codex's
// own, and a group that lists itself alone keeps them in every agent's
// (士奇 on Discord, 10-28: "开启路由组以后,agent 设置页面,模型列表里就是丢失 codex 供应商和对应模型,在下拉
// 选项中虽然能显示 codex 模型但是实际codex 应用中是没有的"). Codex's list
// has its Codex section, so its picker offers them as magpie's and there is
// no own-models switch to show them by.
func TestCodexOwnModelsListedOnlyThroughGroups(t *testing.T) {
ids := codexSignedInSandbox(t)
codex, err := agent.Find("codex")
if err != nil {
t.Fatal(err)
}
check := func(when string, unlisted ...string) {
t.Helper()
own := map[string]bool{}
for _, m := range agentModelList(codex) {
if m.Own {
own[m.ID] = true
}
}
for _, id := range ids {
if !own["codex/"+id] {
t.Errorf("%s: codex/%s not in Codex's list %v", when, id, own)
}
}
if codex.OwnModels(codex.Values()) {
t.Errorf("%s: Codex's list offers a switch for its own models", when)
}
var picks []string
for _, f := range agentFields(codex, codex.Values()) {
if f.Key == "model" {
for _, o := range f.Options {
picks = append(picks, o.Value)
}
}
}
for _, id := range ids {
if !slices.Contains(picks, id) {
t.Errorf("%s: %s not in Codex's picker %v", when, id, picks)
}
}
for _, e := range func() []provider.Entry { l, _ := provider.ListedFor("claude"); return l }() {
if provider.CodexOwn(e) && slices.Contains(unlisted, e.ID) {
t.Errorf("%s: Claude Code lists %s", when, e.ID)
}
}
}
if err := provider.SaveGroup(provider.Group{ID: "mygpt", Name: "MyGPT", Members: []string{"codex/gpt-5.5", "codex/gpt-5.4"}, Unlists: true}); err != nil {
t.Fatal(err)
}
// a group listing alone no longer keeps a subscription's models from
// any agent (TestSubscriptionModelsStayInEveryAgentsListWithGroupOn):
// Claude Code lists them too
check("group lists itself alone")
p, err := provider.Find("codex")
if err != nil {
t.Fatal(err)
}
q := *p
q.Unlisted = true
if err := provider.Save(q); err != nil {
t.Fatal(err)
}
var all []string
for _, id := range ids {
all = append(all, "codex/"+id)
}
check("only through routing groups", all...)
}
// A group that lists itself alone (Group.Unlists) keeps its models out
// of a client's /v1/models (#1783), but a signed-in subscription's models
// stay in every agent's list with it switched on: the group is added on
// top (士奇 on Discord, a third time: with the group on, Hermes Agent's and
// WorkBuddy's lists lost the Codex section and every GPT model, and off
// they were back; a3ee00c05 kept them in Codex's list alone). Every agent
// is checked, its Agents-page list and its model pickers, so a fix for one
// agent can't pass for all.
func TestSubscriptionModelsStayInEveryAgentsListWithGroupOn(t *testing.T) {
ids := codexSignedInSandbox(t)
var refs []string
for _, id := range ids {
refs = append(refs, "codex/"+id)
}
listed := func(a *agent.Agent) map[string]bool {
out := map[string]bool{}
for _, m := range agentModelList(a) {
out[m.ID] = true
}
return out
}
// the group names every one of them, as the reporter's took all five
if err := provider.SaveGroup(provider.Group{ID: "mygpt", Name: "MyGPT", Members: refs, Unlists: true}); err != nil {
t.Fatal(err)
}
checked := 0
for _, a := range agent.All() {
got := listed(a)
if !got["group/mygpt"] {
continue // an agent shown no models of magpie's (or no groups)
}
checked++
for _, ref := range refs {
if !got[ref] {
t.Errorf("group on: %s not in %s's list %v", ref, a.ID, got)
}
}
if shown, _ := provider.CatalogFor(a.ListsFor()); !slices.ContainsFunc(shown, func(e provider.Entry) bool { return e.ID == refs[0] }) {
t.Errorf("group on: %s not in what %s is written and /v1/models gives it", refs[0], a.ID)
}
var picks []string
for _, f := range agentFields(a, a.Values()) {
if f.Key != "model" {
continue
}
for _, o := range f.Options {
picks = append(picks, o.Value)
}
}
if slices.ContainsFunc(picks, func(v string) bool { return strings.HasSuffix(v, "group/mygpt") }) &&
!slices.ContainsFunc(picks, func(v string) bool { return v == ids[0] || strings.HasSuffix(v, refs[0]) }) {
t.Errorf("group on: %s's picker offers the group but not %s: %v", a.ID, refs[0], picks)
}
}
for _, id := range []string{"hermes", "workbuddy", "claude", "codex"} {
a, err := agent.Find(id)
if err != nil {
t.Fatal(err)
}
if !listed(a)["group/mygpt"] {
t.Errorf("%s's list wasn't checked: it has no group/mygpt", id)
}
}
if checked < 10 {
t.Errorf("only %d agents' lists checked", checked)
}
// a client that is no agent is still given the group alone (#1783)
for _, e := range provider.Catalog() {
if slices.Contains(refs, e.ID) {
t.Errorf("the gateway's /v1/models for a client lists %s, kept for the group", e.ID)
}
}
if shown, _ := provider.CatalogFor(""); slices.ContainsFunc(shown, func(e provider.Entry) bool { return slices.Contains(refs, e.ID) }) {
t.Error("a client that is no agent is listed the group's models")
}
// the picker's "kept for routing groups" note doesn't name a model the
// agents are offered
for _, m := range unlistedModels() {
if slices.Contains(refs, m.ID) {
t.Errorf("%s said kept for routing groups, while every agent lists it", m.ID)
}
}
// the codex provider kept for routing groups itself still keeps them
// out of the other agents' lists: the user's own switch, which a group
// switched off doesn't bring back
p, err := provider.Find("codex")
if err != nil {
t.Fatal(err)
}
q := *p
q.Unlisted = true
if err := provider.Save(q); err != nil {
t.Fatal(err)
}
hermes, _ := agent.Find("hermes")
if got := listed(hermes); got[refs[0]] {
t.Errorf("codex used only through routing groups: Hermes lists %s", refs[0])
}
}