@ZhenLuoTTAWDTT on X: with Codex on magpie the model list stops short, and the models past it can't be chosen. Cause, from the clients' own code: the Codex app (ChatGPT.app 26.1013.40934, app-initial-90eb42395b7d.js) and the VS Code extension (openai.chatgpt 26.51021.22027, model-queries-667cf4bd657d.js) both ask their app-server for model/list with includeHidden, cursor null and limit 100 (e?.limit??100) and never ask for nextCursor. codex-rs's list_models pages the list sorted by priority, hidden entries counted, so anything past the 100th is in no picker there. The CLI's /model (tui model_popups.rs) lists every preset in a scrolling popup and has no cap. magpie truncated nothing; its models are numbered after the account's own, so a user with many models lost everything past ~100 in the app, while hidden entries (codex-auto-review and the like) took slots at the top. Fix: codexcat.Fit ranks the list last of all, on every route magpie hands Codex a list (/backend-api/codex/models after the user's own order, /v1/codex/models before its 1 MiB cut, and the model_catalog_json file via CatalogUsed): hidden entries go after every listed one, and with more than 100 listed, the models Codex used in the last 30 days (usage.CodexUsed, most recent first, at most 100; on /v1/codex/models only the asking key's, CodexUsedBy) are brought into the first 100 in the order they stood. The rest keep their order. The list's ETag and X-Models-Etag carry a tag of the used set (codexcat.UsedTag), so Codex asks again when it uses a model it hadn't; CodexUsed is read at most once a minute, or at once when Append writes a Codex call to a model it doesn't hold. Documented in docs/subsystems/gateway-routing.md. Tests (each red on the old code, broken by hand: Fit a no-op, the gateway calls and the used tag removed, Append's codexUsedNew removed): TestFitKeepsUsedModelsWhereTheCodexAppReads, TestFitLeavesAShortListsOrder, TestCatalogUsedFits, TestUsedTagIsTheSet (codexcat), TestCodexUsed (usage), TestCodexModelListKeepsUsedModelsInTheAppsHundred and TestCodexCatalogKeepsTheKeysUsedModelsFirst (gateway). Real run: codex-cli 0.162.1's app-server in a sandbox CODEX_HOME (never ~/.codex) with a model_catalog_json of 3 hidden entries at priority 1-3 and 150 listed models, driven over stdio with the app's own request (model/list includeHidden, limit 100). Before: page 1 held 100 of 153, 3 of them hidden, the last many/model-096, nextCursor "100". The same catalog put through Fit with many/model-140 used: page 1 has no hidden entry and ends with many/model-140. The TUI's /model scrolled to all 150 (tmux). Run with this change one4ca47b0f: gofmt -l clean; go vet -tags nogui with GOOS=linux and GOOS=windows on codexcat, usage, gateway and agent clean. go test -count=1 -timeout 45m: codexcat ok, usage ok; -run 'Codex|Fit|Used|ModelList|Catalog' on gateway, usage and agent ok (122s, 11s, 80s). The whole gateway and agent packages ran too, but the machine's disk was full under other sessions' runs (209M to 2.5G free), and 959 lines of that log are "no space left on device". Of the reds, three are not that: TestFolderVarsAreListed is #3884 (CARGO_HOME, red on main sincee4552cca9); TestHistoryKeepsDaysAndDropsOld (gateway) and TestSuffixModesReachAgentFiles (agent) failed beside half-written settings files and pass alone 3 of 3 at this commit. Not run: the full ./... suite (no room on the disk); the Codex app and VS Code extension themselves against a magpie with this change (their request was read from their bundles and replayed against a real app-server). Rebased onca8a6f346(agent rename and Codex app route, claude_runs in gateway) and run again: GOOS=windows go vet on agent and gateway clean; -run 'Codex|Fit|Used|ModelList|Catalog' ok on codexcat, usage and gateway (75s), and with |Rename ok on agent (68s). Then rebased on94c2aaecb(usage rename) and usage run whole again: ok (88s).
110 lines
3.7 KiB
Go
110 lines
3.7 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")
|
|
}
|
|
}
|