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).
This commit is contained in:
yetone
2026-10-29 00:37:44 +08:00
parent 40f206dff1
commit 87a983423e
6 changed files with 174 additions and 11 deletions
File diff suppressed because one or more lines are too long
+29 -5
View File
@@ -99,10 +99,11 @@ func Catalog(ms []catalog.Model) []byte { return CatalogAt(ms, NewSessionEffort(
func CatalogAt(ms []catalog.Model, effort string) []byte { return CatalogUsed(ms, effort, nil) }
// CatalogUsed is CatalogAt with the models Codex used, most recent first,
// kept where the Codex app reads them (Fit).
// and a ChatGPT account's own (ChatGPTOwn), kept where the Codex app reads
// them (FitOwn).
func CatalogUsed(ms []catalog.Model, effort string, used []string) []byte {
entries := entriesAt(ms, 0, effort)
Fit(entries, used)
FitOwn(entries, used, ChatGPTOwn)
out := struct {
Models []any `json:"models"`
}{Models: entries}
@@ -113,6 +114,12 @@ func CatalogUsed(ms []catalog.Model, effort string, used []string) []byte {
return b
}
// ChatGPTOwn reports whether slug is a ChatGPT account's own model in
// magpie's list for Codex (codex/<slug>): Codex's own GPT models, for a
// Codex that reaches them through magpie, which FitOwn keeps in the first
// AppList.
func ChatGPTOwn(slug string) bool { return strings.HasPrefix(slug, "codex/") }
// level, tier and model are an entry of models.json as Entries writes it.
type level struct {
Effort string `json:"effort"`
@@ -561,7 +568,15 @@ const AppList = 100
// order they stood, in place of the last of the others there. The order
// is otherwise kept, and the priorities numbered 1 to n in it. The slice
// is put in that order too.
func Fit(entries []any, used []string) {
func Fit(entries []any, used []string) { FitOwn(entries, used, nil) }
// FitOwn is Fit with Codex's own models (own reports them by slug) kept
// in the first AppList ahead of the ones used, wherever the user's order
// (Order) put them: Codex lists them itself, so a user who didn't take
// them out of its list expects them in the app. An order set while the
// list had none of them names only magpie's, and put every one past the
// 100th (maiya on Discord). A hidden one still goes last.
func FitOwn(entries []any, used []string, own func(slug string) bool) {
type ranked struct {
e any
slug string
@@ -603,7 +618,17 @@ func Fit(entries []any, used []string) {
listed++
}
}
if listed > AppList && len(used) > 0 {
if listed > AppList && (len(used) > 0 || own != nil) {
n := 0
// Codex's own first, then the used ones, as room allows
if own != nil {
for i := range rs[:listed] {
if n < AppList && own(rs[i].slug) {
rs[i].promoted = true
n++
}
}
}
at := map[string]int{}
for i, r := range rs[:listed] {
at[r.slug] = i
@@ -613,7 +638,6 @@ func Fit(entries []any, used []string) {
at["codex/"+r.slug] = i
}
}
n := 0
for _, u := range used {
if i, ok := at[u]; ok && !rs[i].promoted && n < AppList {
rs[i].promoted = true
+64
View File
@@ -107,3 +107,67 @@ func TestUsedTagIsTheSet(t *testing.T) {
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")
}
+64
View File
@@ -118,3 +118,67 @@ func TestCodexCatalogKeepsTheKeysUsedModelsFirst(t *testing.T) {
t.Errorf("another key's many/model-111 was brought up, to %v", p)
}
}
// maiya (Discord): after an update the Codex app showed magpie's models
// and none of Codex's own GPT ones. With an order of Codex's models set on
// the Agents page while its list had no Codex section (the codex provider
// kept for routing groups before a3ee00c05, Codex not signed in yet, or a
// GPT model OpenAI added since), the order names 100 or more of magpie's
// and none of the account's, so Order put every own one after them, past
// the 100 the Codex app reads. The account's own listed models are kept
// in the first 100, wherever the order put them; a hidden one stays last.
func TestCodexModelListKeepsOwnModelsInTheAppsHundred(t *testing.T) {
codexSignedIn(t)
var ms, order []string
for i := range 130 {
ms = append(ms, fmt.Sprintf("model-%03d", i))
order = append(order, fmt.Sprintf("many/model-%03d", i))
}
if err := provider.Save(provider.Provider{ID: "many", Name: "Many", Key: "k", Chat: "http://127.0.0.1:9/v1", Models: ms}); err != nil {
t.Fatal(err)
}
if err := provider.SetModelOrder("codex", order); err != nil {
t.Fatal(err)
}
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("ETag", `"v1"`)
io.WriteString(w, `{"models":[{"slug":"codex-auto-review","priority":0,"visibility":"hide"},{"slug":"gpt-6-sol","priority":1,"visibility":"list"},{"slug":"gpt-5.5","priority":2,"visibility":"list"}]}`)
}))
defer up.Close()
was := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
defer func() { provider.CodexBase = was }()
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", CodexPath+"/models", nil)
req.Header.Set("Authorization", "Bearer chatgpt-token")
New().Handler().ServeHTTP(rec, req)
var got struct {
Models []map[string]any `json:"models"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
slices.SortStableFunc(got.Models, func(a, b map[string]any) int {
return int(a["priority"].(float64) - b["priority"].(float64))
})
var order2 []string
for _, m := range got.Models {
order2 = append(order2, m["slug"].(string))
}
if len(order2) != 133 {
t.Fatalf("%d models", len(order2))
}
first := order2[:codexcat.AppList]
for _, s := range []string{"gpt-6-sol", "gpt-5.5"} {
if !slices.Contains(first, s) {
t.Errorf("Codex's own %s is at %d, past the %d the Codex app reads", s, slices.Index(order2, s)+1, codexcat.AppList)
}
}
if order2[len(order2)-1] != "codex-auto-review" {
t.Errorf("the hidden codex-auto-review is at %d of %d", slices.Index(order2, "codex-auto-review")+1, len(order2))
}
// the user's order holds for the rest: magpie's first 98 as they put them
if !slices.Equal(first[:98], order[:98]) {
t.Errorf("the first 98: %v", first[:98])
}
}
+12 -2
View File
@@ -2024,13 +2024,23 @@ func codexMerged(own []any, cached bool) []any {
if !codexOwnOnly() {
ms = provider.CodexListed()
}
// Codex's own, as the user left them in its list
mine := make(map[string]bool, len(own))
for _, m := range own {
if o, ok := m.(map[string]any); ok {
if slug, _ := o["slug"].(string); slug != "" {
mine[slug] = true
}
}
}
all := append(own, codexcat.Entries(ms, len(own)+100)...)
if at, ok := provider.CodexOrder(); ok {
codexcat.Order(all, at)
}
// the Codex app and its VS Code extension read the first 100 alone:
// hidden ones go last, and the ones the user uses are among the 100
codexcat.Fit(all, usage.CodexUsed())
// hidden ones go last, and Codex's own and the ones the user uses are
// among the 100, wherever the user's order put them (maiya on Discord)
codexcat.FitOwn(all, usage.CodexUsed(), func(slug string) bool { return mine[slug] })
return all
}
+4 -3
View File
@@ -42,10 +42,11 @@ func (s *Server) codexCatalog(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer
b.Write(head)
n, out := 0, 0
// the Codex app reads the first 100 alone (codexcat.AppList): the ones
// this key's Codex used are among them, and kept when the end is cut
// the Codex app reads the first 100 alone (codexcat.AppList): a ChatGPT
// account's own models and the ones this key's Codex used are among
// them, and kept when the end is cut
entries := codexcat.Entries(ms, 0)
codexcat.Fit(entries, usage.CodexUsedBy(access.Caller(r.Context()).KeyID))
codexcat.FitOwn(entries, usage.CodexUsedBy(access.Caller(r.Context()).KeyID), codexcat.ChatGPTOwn)
for _, e := range entries {
var one bytes.Buffer
enc := json.NewEncoder(&one)