#3734: with ~20 Codex app-servers starting together, Codex logged "failed to refresh available models: request timed out" 1,906 times a day, fell back to its default metadata for magpie's groups (no supports_search_tool), and inlined ~1,075 Apps tools: ~500K input tokens a turn. Each /backend-api/codex/models asked the ChatGPT backend on its own, and gave its ask up at codexModelsWait (3 s). Measured with a minimal gateway (gateway.New().Handler() alone, sandbox HOME, no providers), 20 lists at once with a real ChatGPT sign-in's headers, as app-servers ask: -4a33cf0a9: mean 2.24 s, max 3.01 s; the one at 3.01 s was handed an empty list (16 bytes), the backend asked 20 times. Again right after: mean 1.04 s, max 1.72 s, 20 asks again. - this change: mean 1.72 s, max 1.74 s, all 20 the full list (568,401 bytes) from one ask; again right after: max 0.01 s. Now the backend's list is kept for each sign-in (account id, the backend, Codex's version): within codexListFor (a minute) it is handed out again; past it, handed out at once while one ask goes; with none, the callers wait on the one ask for up to codexModelsWait as before, and the ask goes on, up to 30 s, for the callers after, where a backend slower than the wait had never got a list through. A 401 or 403 is still Codex's to see, and no list from before is handed out in its place. Each caller reads its own copy, since the windows, picks, V1 stamp and auto-review are written into the entries. What changes for users: the account's own models (not magpie's, whose part is built per call) are up to a minute behind the backend. Not changed: Codex's own 300 s cache and the build of magpie's part under provider.Hold (#746); no model_catalog_json floor is written. Tests (codex_models_shared_test.go): - 20 lists at once, the backend held: each answered with magpie's models and Codex's last list, the backend asked once; let go, its list reaches the next caller; fresh, five more ask it no more. On4a33cf0a9: asked 20 times, then 5 times more. - a backend always 600 ms against a 200 ms wait gets its list through: red on4a33cf0a9(none in 10 s), green here. - stale: three answers at once while the backend holds its second ask. - a refused sign-in after a list: the 401 is passed on. TestCodexModelListOnlyPicked and TestCodexNativeModelsTakeSetWindows change the backend's list between calls, so they forget the kept one there (forgetCodexLists, which also waits for every ask a test began). Run at this head (cm3734 worktree, base834935149): - go test -race -count=10 on the five list tests (the four new, BackendSlow): ok. - GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gateway/: ok; gofmt -l clean. - go test -timeout 45m ./internal/gateway/: ok (429s), run before TestCodexModelListBackendAlwaysSlow was added; that test then run alone and in the -race run above: ok. - Rebased onto80857a66c(13cca8109touched codexcat and Codex's models since): go test -run 'Codex|Fast' ./internal/gateway/: ok (42s).
143 lines
4.9 KiB
Go
143 lines
4.9 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// An agent shown only the models picked for it (nianlee-official, #1337):
|
|
// with its key, /v1/models leaves out a model that came after the switch,
|
|
// of a new provider or an old one, while another agent's list has it; and
|
|
// the model asked for by name still goes through.
|
|
func TestOnlyPickedModelsList(t *testing.T) {
|
|
f := &fake{t: t, reply: sse(`data: {"choices":[{"delta":{"content":"ok"}}]}`, `data: [DONE]`)}
|
|
setup(t, provider.Chat, f)
|
|
list := func(key string) map[string]bool {
|
|
t.Helper()
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("GET", "/v1/models", nil)
|
|
req.Header.Set("Authorization", "Bearer "+key)
|
|
New().Handler().ServeHTTP(rec, req)
|
|
var got struct {
|
|
Data []struct{ ID string } `json:"data"`
|
|
}
|
|
json.Unmarshal(rec.Body.Bytes(), &got)
|
|
out := map[string]bool{}
|
|
for _, m := range got.Data {
|
|
out[m.ID] = true
|
|
}
|
|
return out
|
|
}
|
|
if err := provider.SetOnlyPicked("opencode", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := list(TokenFor("opencode")); !got["fake/m1"] {
|
|
t.Fatalf("switched on, what was shown went: %v", got)
|
|
}
|
|
// a new model of the provider it had, and a new provider
|
|
up := httptest.NewServer(f)
|
|
defer up.Close()
|
|
for _, p := range []provider.Provider{
|
|
{ID: "fake", Name: "Fake", Key: "k", Chat: up.URL + "/v1", Models: []string{"m1", "m2"}},
|
|
{ID: "fresh", Name: "Fresh", Key: "k", Chat: up.URL + "/v1", Models: []string{"x1"}},
|
|
} {
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if got := list(TokenFor("opencode")); !got["fake/m1"] || got["fake/m2"] || got["fresh/x1"] {
|
|
t.Errorf("only picked: %v", got)
|
|
}
|
|
if got := list(TokenFor("cursor")); !got["fake/m2"] || !got["fresh/x1"] {
|
|
t.Errorf("another agent's list: %v", got)
|
|
}
|
|
// asked for by name, one it isn't shown still answers
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"fresh/x1","stream":true,"messages":[{"role":"user","content":"hi"}]}`))
|
|
req.Header.Set("Authorization", "Bearer "+TokenFor("opencode"))
|
|
New().Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 || !strings.Contains(string(f.got), `"x1"`) {
|
|
t.Errorf("a model not picked, by name: %d %s; upstream got %s", rec.Code, rec.Body, f.got)
|
|
}
|
|
// ticked, it is listed
|
|
if err := provider.SetPickedModels("opencode", []string{"fake/m1", "fresh/x1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := list(TokenFor("opencode")); !got["fresh/x1"] || got["fake/m2"] {
|
|
t.Errorf("fresh/x1 ticked: %v", got)
|
|
}
|
|
}
|
|
|
|
// Codex's own ChatGPT models follow the switch too: a model the account
|
|
// gains after it is off Codex's list until ticked.
|
|
func TestCodexModelListOnlyPicked(t *testing.T) {
|
|
codexSignedIn(t)
|
|
forgetCodexLists(t)
|
|
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-5.5"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
slugs := `{"slug":"gpt-5.5","priority":1}`
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("ETag", `"v1"`)
|
|
io.WriteString(w, `{"models":[`+slugs+`]}`)
|
|
}))
|
|
defer up.Close()
|
|
was := provider.CodexBase
|
|
provider.CodexBase = up.URL + "/backend-api/codex"
|
|
defer func() { provider.CodexBase = was }()
|
|
list := func() []string {
|
|
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"`
|
|
}
|
|
json.Unmarshal(rec.Body.Bytes(), &got)
|
|
var native []string
|
|
for _, m := range got.Models {
|
|
if slug, _ := m["slug"].(string); strings.HasPrefix(slug, "gpt-") {
|
|
native = append(native, slug)
|
|
}
|
|
}
|
|
return native
|
|
}
|
|
if err := provider.SetOnlyPicked("codex", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if native := list(); len(native) != 1 || native[0] != "gpt-5.5" {
|
|
t.Fatalf("switched on: %v", native)
|
|
}
|
|
// the account gains a model: the backend's list, kept codexListFor,
|
|
// is asked again
|
|
slugs += `,{"slug":"gpt-6","priority":2}`
|
|
forgetCodexLists(t)
|
|
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-5.5", "gpt-6"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if native := list(); len(native) != 1 || native[0] != "gpt-5.5" {
|
|
t.Errorf("a model the account gained since: %v", native)
|
|
}
|
|
// off: what is shown stays (gpt-6 still off), and one that comes now shows
|
|
if err := provider.SetOnlyPicked("codex", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if native := list(); len(native) != 1 || native[0] != "gpt-5.5" {
|
|
t.Errorf("switched off: %v", native)
|
|
}
|
|
slugs += `,{"slug":"gpt-6-mini","priority":3}`
|
|
forgetCodexLists(t)
|
|
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-5.5", "gpt-6", "gpt-6-mini"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if native := list(); len(native) != 2 || native[1] != "gpt-6-mini" {
|
|
t.Errorf("switched off, a new model: %v", native)
|
|
}
|
|
}
|