Files
magpie/internal/codexcat/fit_test.go
T
yetone 87a983423e codexcat: Codex's own GPT models stay in the Codex app's first 100 whatever the user's order (maiya on Discord)
maiya (Discord): after updating, the Codex app lists magpie's models and
none of Codex's own GPT ones.

Cause: the Codex app and its VS Code extension read the first 100 of the
list by priority (codexcat.AppList). codexMerged ranks the list by the
user's order from the Agents page (codexcat.Order, #855), where a model
the order doesn't name goes after every one it does, and codexcat.Fit
only brought the models Codex used back into the 100. The GUI saves the
whole shown list as the order, so an order saved while Codex's list had
no Codex section (the codex provider kept for routing groups before
a3ee00c05, Codex not yet signed in, or a GPT model OpenAI added since)
names 100+ of magpie's models and none of the account's: every unused
GPT model then landed at 101+ and was in no picker of the app. Since
add927e60 the app's desk list (CodexDesktopCatalog) is codexMerged's
too, so this reaches the app beside the ChatGPT sign-in, not only the
backend list.

Fix: codexcat.FitOwn keeps Codex's own listed models in the first 100
ahead of the used ones, in the order they stood; hidden entries still go
last, and a model the user hid in Codex's list was already taken out
before (CodexNativeHidden/CodexNativePicked). codexMerged passes the
backend's slugs; the siblings, /v1/codex/models (codexCatalog) and the
model_catalog_json file (CatalogUsed), pass a ChatGPT account's codex/...
entries (codexcat.ChatGPTOwn). Fit is FitOwn with none. Documented in
docs/subsystems/gateway-routing.md.

Tests:
- gateway TestCodexModelListKeepsOwnModelsInTheAppsHundred: the exact
  setup (ChatGPT sign-in, 130 magpie models, a Codex order naming all 130
  and no GPT model, the backend listing gpt-6-sol, gpt-5.5 and the hidden
  codex-auto-review): on 298bfce59 gpt-6-sol and gpt-5.5 were at 131 and
  132; now in the first 100, auto-review last, magpie's first 98 in the
  user's order.
- codexcat TestFitOwnKeepsCodexsOwnInTheAppsHundred and
  TestCatalogUsedKeepsChatGPTOwn.

Broken by hand: FitOwn with own forced to nil: all three red (131/132,
priority 131); restored, green.

Checked and not real (no change): CodexNativeHidden hiding an own model
while another ChatGPT entry for it is picked. There is one ChatGPT
provider ("codex"), one entry per slug however many accounts are saved,
and catalogOf adds an unlisted own entry once. A probe test (two
accounts, only-picked with both picked, a group that Unlists gpt-5.5, a
plain group, the provider Unlisted, back to hide mode) kept both slugs
out of CodexNativeHidden and in /backend-api/codex/models at every step
on 298bfce59.

Run (mote-run, this tree on 298bfce59): gofmt -l clean; go vet,
GOOS=linux go vet -tags nogui and GOOS=windows go vet on codexcat and
gateway: ok; go test -tags nogui -count=1 -timeout 45m ./internal/codexcat
./internal/gateway ./internal/provider ./internal/agent: all ok
(gateway 633s). Rebased onto b119f8777 (it changed codex_backend.go
elsewhere): gofmt, the three vets and go test ./internal/codexcat
./internal/gateway again: ok (gateway 627s).
Rebased again onto b5d2858f4 (internal/usage), then 40f206dff (vertex): gofmt, vets, codexcat and
usage ok, gateway -run Codex ok (54s); the whole gateway package not rerun.
Not run: the full suite (another session's full run was on mote).
Not tried: a real Codex app or VS Code extension, or maiya's own
settings (her order and provider setup are inferred, not seen).
2026-10-29 00:37:44 +08:00

174 lines
6.1 KiB
Go

package codexcat
import (
"encoding/json"
"fmt"
"os"
"slices"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// The Codex app and its VS Code extension read the first AppList of
// model/list by priority and never the next page, hidden ones counted
// (@ZhenLuoTTAWDTT on X: "未被显示的模型也无法选择"). Fit puts a hidden
// entry after every listed one and, with more listed than AppList, brings
// the models Codex used into the first AppList, keeping the order
// otherwise.
func TestFitKeepsUsedModelsWhereTheCodexAppReads(t *testing.T) {
var entries []any
// Codex's own: two listed, three hidden near the top
for i, s := range []string{"gpt-6", "codex-auto-review", "gpt-6-mini", "gpt-oss", "gpt-6-luna"} {
v := "list"
if i == 1 || i == 3 || i == 4 {
v = "hide"
}
entries = append(entries, map[string]any{"slug": s, "priority": float64(i + 1), "visibility": v})
}
// magpie's 150, after them
for i := range 150 {
entries = append(entries, map[string]any{"slug": fmt.Sprintf("many/model-%03d", i), "priority": 106 + i, "visibility": "list"})
}
Fit(entries, []string{"many/model-140", "codex/gpt-6-luna", "many/model-003", "gone/model"})
var order []string
pri := map[string]int{}
for i, e := range entries {
o := e.(map[string]any)
slug := o["slug"].(string)
order = append(order, slug)
pri[slug] = o["priority"].(int)
if pri[slug] != i+1 {
t.Fatalf("%s is at %d with priority %d", slug, i, pri[slug])
}
}
first := order[:AppList]
for _, s := range []string{"codex-auto-review", "gpt-oss", "gpt-6-luna"} {
if slices.Contains(first, s) {
t.Errorf("hidden %s is in the first %d", s, AppList)
}
}
if !slices.Contains(first, "many/model-140") {
t.Errorf("the used many/model-140 is at %d, past the first %d the Codex app reads", pri["many/model-140"], AppList)
}
// 2 of Codex's own listed and 98 of magpie's: 97 from the top, then the used one
if want := append([]string{"gpt-6", "gpt-6-mini"}, order[2:99]...); !slices.Equal(first[:99], want) || order[98] != "many/model-096" || order[99] != "many/model-140" || order[100] != "many/model-097" {
t.Errorf("the first 101: %v", order[:101])
}
if last := order[len(order)-3:]; !slices.Equal(last, []string{"codex-auto-review", "gpt-oss", "gpt-6-luna"}) {
t.Errorf("the last three %v, want the hidden ones in their order", last)
}
}
// A list the Codex app reads whole is left in its order, used or not.
func TestFitLeavesAShortListsOrder(t *testing.T) {
var entries []any
for i := range 20 {
entries = append(entries, map[string]any{"slug": fmt.Sprintf("p/m%02d", i), "priority": i + 1, "visibility": "list"})
}
Fit(entries, []string{"p/m19", "p/m10"})
for i, e := range entries {
if s := e.(map[string]any)["slug"]; s != fmt.Sprintf("p/m%02d", i) {
t.Fatalf("%d is %v", i, s)
}
}
}
// The model_catalog_json magpie writes Fits the same way.
func TestCatalogUsedFits(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
var ms []catalog.Model
for i := range 130 {
ms = append(ms, catalog.Model{ID: fmt.Sprintf("many/model-%03d", i)})
}
var got struct {
Models []map[string]any `json:"models"`
}
if err := json.Unmarshal(CatalogUsed(ms, "", []string{"many/model-120"}), &got); err != nil {
t.Fatal(err)
}
for _, m := range got.Models {
if m["slug"] == "many/model-120" {
if p := m["priority"].(float64); p > AppList {
t.Fatalf("the used model's priority is %v", p)
}
return
}
}
t.Fatal("many/model-120 is not in the catalog")
}
func TestUsedTagIsTheSet(t *testing.T) {
if UsedTag([]string{"a", "b"}) != UsedTag([]string{"b", "a"}) {
t.Error("the tag changed with the order alone")
}
if UsedTag([]string{"a"}) == UsedTag([]string{"a", "b"}) {
t.Error("the tag didn't change with the set")
}
}
// Codex's own models are kept in the first AppList wherever an order put
// them, ahead of the used ones; a hidden one of them still goes last
// (maiya on Discord: an order naming only magpie's put every GPT model
// past the 100 the Codex app reads).
func TestFitOwnKeepsCodexsOwnInTheAppsHundred(t *testing.T) {
var entries []any
for i := range 130 {
entries = append(entries, map[string]any{"slug": fmt.Sprintf("many/model-%03d", i), "priority": i + 1, "visibility": "list"})
}
for i, s := range []string{"gpt-6-sol", "gpt-5.5", "codex-auto-review"} {
v := "list"
if s == "codex-auto-review" {
v = "hide"
}
entries = append(entries, map[string]any{"slug": s, "priority": 131 + i, "visibility": v})
}
own := map[string]bool{"gpt-6-sol": true, "gpt-5.5": true, "codex-auto-review": true}
FitOwn(entries, []string{"many/model-120"}, func(s string) bool { return own[s] })
var order []string
for _, e := range entries {
order = append(order, e.(map[string]any)["slug"].(string))
}
first := order[:AppList]
for _, s := range []string{"gpt-6-sol", "gpt-5.5", "many/model-120"} {
if !slices.Contains(first, s) {
t.Errorf("%s is at %d, past the first %d", s, slices.Index(order, s)+1, AppList)
}
}
if order[len(order)-1] != "codex-auto-review" {
t.Errorf("the hidden codex-auto-review is at %d", slices.Index(order, "codex-auto-review")+1)
}
// the rest in the order they stood: 97 of magpie's from the top
if order[0] != "many/model-000" || order[96] != "many/model-096" || order[100] != "many/model-097" {
t.Errorf("the first 101: %v", order[:101])
}
}
// The model_catalog_json magpie writes keeps a ChatGPT account's own
// (codex/…) models in the first AppList the same way.
func TestCatalogUsedKeepsChatGPTOwn(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
var ms []catalog.Model
for i := range 130 {
ms = append(ms, catalog.Model{ID: fmt.Sprintf("many/model-%03d", i)})
}
ms = append(ms, catalog.Model{ID: "codex/gpt-5.5"})
var got struct {
Models []map[string]any `json:"models"`
}
if err := json.Unmarshal(CatalogUsed(ms, "", nil), &got); err != nil {
t.Fatal(err)
}
for _, m := range got.Models {
if m["slug"] == "codex/gpt-5.5" {
if p := m["priority"].(float64); p > AppList {
t.Fatalf("codex/gpt-5.5's priority is %v", p)
}
return
}
}
t.Fatal("codex/gpt-5.5 is not in the catalog")
}