diff --git a/internal/gateway/codex_accounts_test.go b/internal/gateway/codex_accounts_test.go index f168a3122..7f7118f5a 100644 --- a/internal/gateway/codex_accounts_test.go +++ b/internal/gateway/codex_accounts_test.go @@ -789,7 +789,7 @@ func TestCallKind(t *testing.T) { // one that sent it, in the User-Agent and the version header alike, so a // model Codex CLI reaches on its own isn't refused through magpie // (Discord: "The 'gpt-6.1-sol' model is not supported when using Codex -// with a ChatGPT account"). 0.161.0 is newer than any installed here or +// with a ChatGPT account"). 0.199.0 is newer than any installed here or // magpie's own fallback, so only the client's own version can pass. func TestCodexPoolSaysTheClientsVersion(t *testing.T) { codexSignedIn(t, "spare@example.com") @@ -799,7 +799,7 @@ func TestCodexPoolSaysTheClientsVersion(t *testing.T) { ua := r.Header.Get("User-Agent") uas, vers = append(uas, ua), append(vers, r.Header.Get("version")) v, _, _ := strings.Cut(strings.TrimPrefix(ua, "codex_cli_rs/"), " ") - if modelOf(b) == "gpt-6.1-sol" && (!strings.HasPrefix(ua, "codex_cli_rs/") || older(v, "0.161.0")) { + if modelOf(b) == "gpt-6.1-sol" && (!strings.HasPrefix(ua, "codex_cli_rs/") || older(v, "0.199.0")) { w.WriteHeader(400) io.WriteString(w, `{"detail":"The 'gpt-6.1-sol' model is not supported when using Codex with a ChatGPT account."}`) return @@ -820,14 +820,14 @@ func TestCodexPoolSaysTheClientsVersion(t *testing.T) { req.Header.Set("Authorization", "Bearer chatgpt-token") req.Header.Set("chatgpt-account-id", "acct-1") req.Header.Set("originator", "codex_cli_rs") - req.Header.Set("User-Agent", "codex_cli_rs/0.161.0 (Mac OS 26.6.0; arm64) ghostty/1.2.0") - req.Header.Set("version", "0.161.0") + req.Header.Set("User-Agent", "codex_cli_rs/0.199.0 (Mac OS 26.6.0; arm64) ghostty/1.2.0") + req.Header.Set("version", "0.199.0") s.Handler().ServeHTTP(rec, req) if rec.Code != 200 || !strings.Contains(rec.Body.String(), "pong") { t.Fatalf("status %d: %s (User-Agent %q, version %q)", rec.Code, rec.Body.String(), uas, vers) } for i := range uas { - if !strings.HasPrefix(uas[i], "codex_cli_rs/0.161.0 (") || vers[i] != "0.161.0" { + if !strings.HasPrefix(uas[i], "codex_cli_rs/0.199.0 (") || vers[i] != "0.199.0" { t.Errorf("request %d: User-Agent %q, version %q", i, uas[i], vers[i]) } } diff --git a/internal/provider/codex_models.go b/internal/provider/codex_models.go index c8ed3132e..0625bd443 100644 --- a/internal/provider/codex_models.go +++ b/internal/provider/codex_models.go @@ -14,18 +14,25 @@ import ( "sort" "strings" "sync" + "testing" "time" "github.com/yetone/magpie/internal/catalog" "github.com/yetone/magpie/internal/codexcat" + "github.com/yetone/magpie/internal/lazyre" "github.com/yetone/magpie/internal/proc" "github.com/yetone/magpie/internal/settings" + "github.com/yetone/magpie/internal/source" ) -// codexClientVersion is the Codex CLI version the models list is asked for -// when Codex CLI has not asked itself yet: the list leaves out models newer -// than the client asking. -const codexClientVersion = "0.159.0" +// codexClientVersion is the oldest Codex CLI version the models list is +// asked for and requests say they are: npm's latest @openai/codex on +// 2026-11-18. The list leaves out models newer than the client asking +// (each entry's minimal_client_version): GPT-6.2 Sol and Astra need +// 0.166.0, and at the 0.159.0 this was before, a user with no newer Codex +// CLI never saw them (zzstar101 and m0eaki on X). A fixed version goes +// stale as Codex releases, so npm's latest counts too (codexPublished). +const codexClientVersion = "0.169.1" // codexModels asks the ChatGPT backend which Codex models the account's own // plan has — a Free account lists fewer than a Plus or Pro one, and one it @@ -128,7 +135,8 @@ var codexVersionCache struct { } // codexVersion is the client version the models list is asked for: the -// newest of the Codex CLI installed, the one Codex CLI last asked with and +// newest of the Codex CLI installed, the one Codex CLI last asked with, +// a Codex seen through the gateway, npm's latest (codexPublished) and // codexClientVersion. The list leaves out models newer than the client // asking, and Codex CLI's models_cache.json keeps the version it was written // with until Codex next asks — after an update that brought new models @@ -156,7 +164,80 @@ func codexVersion() string { } codexVersionCache.v, codexVersionCache.at = v, time.Now() } - return newerVersion(codexVersionCache.v, codexSeen.get()) + return newerVersion(newerVersion(codexVersionCache.v, codexSeen.get()), codexPublished()) +} + +// codexNpmLatest is where npm says which @openai/codex is latest, the +// release its installs and `npm i -g @openai/codex` get; a var so tests +// can serve one. +var codexNpmLatest = codexNpmRegistry + +const codexNpmRegistry = "https://registry.npmjs.org/@openai/codex/latest" + +// codexPublishedEvery is how often npm is asked again. +const codexPublishedEvery = 6 * time.Hour + +var codexPublishedCache struct { + sync.Mutex + v string // the latest read, "" before one is + at time.Time // when it was last asked + asking bool +} + +// codexPublished is npm's latest Codex CLI as last read, "" before it is: +// asked in the background at most every codexPublishedEvery, through the +// mirrors when npm doesn't answer, and never from a test binary unless a +// test points codexNpmLatest at a fake. +func codexPublished() string { + codexPublishedCache.Lock() + defer codexPublishedCache.Unlock() + v, u := codexPublishedCache.v, codexNpmLatest + if codexPublishedCache.asking || time.Since(codexPublishedCache.at) < codexPublishedEvery || testing.Testing() && u == codexNpmRegistry { + return v + } + codexPublishedCache.asking = true + go func() { + found := codexAskPublished(u) + codexPublishedCache.Lock() + if found != "" { + codexPublishedCache.v = found + } + codexPublishedCache.at, codexPublishedCache.asking = time.Now(), false + codexPublishedCache.Unlock() + }() + return v +} + +var codexReleaseRe = lazyre.New(`^\d{1,4}\.\d{1,4}\.\d{1,6}$`) + +// codexAskPublished is the version u says is latest, or "" when it can't +// be read or isn't a release version (an alpha is never named: the +// backend's lists and requests are a release's). +func codexAskPublished(u string) string { + c := &http.Client{Timeout: 8 * time.Second, Transport: http.DefaultClient.Transport} + req, err := http.NewRequest(http.MethodGet, u, nil) + if err != nil { + return "" + } + req.Header.Set("Accept", "application/json") + res, err := source.Do(c, req) + if err != nil { + return "" + } + defer res.Body.Close() + if res.StatusCode != http.StatusOK { + return "" + } + var got struct { + Version any `json:"version"` + } + if json.NewDecoder(io.LimitReader(res.Body, 4<<20)).Decode(&got) != nil { + return "" + } + if v, _ := got.Version.(string); codexReleaseRe.MatchString(v) { + return v + } + return "" } // codexCLIVersion is what the codex CLI at exe says its version is; a var diff --git a/internal/provider/codex_models_test.go b/internal/provider/codex_models_test.go index 405690fcf..6c2a1f023 100644 --- a/internal/provider/codex_models_test.go +++ b/internal/provider/codex_models_test.go @@ -8,6 +8,7 @@ import ( "path/filepath" "runtime" "strings" + "sync/atomic" "testing" "time" @@ -56,13 +57,13 @@ func TestCodexVersion(t *testing.T) { home := t.TempDir() t.Setenv("HOME", home) t.Setenv("USERPROFILE", home) - writeFile(t, filepath.Join(home, ".codex", "models_cache.json"), map[string]any{"client_version": "0.160.0"}) + writeFile(t, filepath.Join(home, ".codex", "models_cache.json"), map[string]any{"client_version": "0.170.0"}) exe := filepath.Join(t.TempDir(), "codex") - testenv.Program(t, exe, "#!/bin/sh\necho codex-cli 0.161.1\n") + testenv.Program(t, exe, "#!/bin/sh\necho codex-cli 0.171.1\n") old := codexExecutable t.Cleanup(func() { codexExecutable = old; codexVersionCache.at = time.Time{}; codexSeen = seenVersion{} }) codexSeen = seenVersion{} - for _, c := range []struct{ exe, want string }{{exe, "0.161.1"}, {"", "0.160.0"}} { + for _, c := range []struct{ exe, want string }{{exe, "0.171.1"}, {"", "0.170.0"}} { codexExecutable = func() string { return c.exe } codexVersionCache.at = time.Time{} if got := codexVersion(); got != c.want { @@ -75,9 +76,9 @@ func TestCodexVersion(t *testing.T) { if got := codexVersion(); got != codexClientVersion { t.Errorf("CODEX_HOME with no cache: %s", got) } - writeFile(t, filepath.Join(os.Getenv("CODEX_HOME"), "models_cache.json"), map[string]any{"client_version": "0.160.2"}) + writeFile(t, filepath.Join(os.Getenv("CODEX_HOME"), "models_cache.json"), map[string]any{"client_version": "0.170.2"}) codexVersionCache.at = time.Time{} - if got := codexVersion(); got != "0.160.2" { + if got := codexVersion(); got != "0.170.2" { t.Errorf("CODEX_HOME's cache: %s", got) } } @@ -111,9 +112,9 @@ func TestCodexVersionSeen(t *testing.T) { }{ {h("User-Agent", "claude-cli/2.1.300 (external, cli)"), codexClientVersion}, {h("User-Agent", "Mozilla/5.0", "version", "9.9.9"), codexClientVersion}, - {h("User-Agent", "codex_cli_rs/0.160.0 (Mac OS 26.6.0; arm64) ghostty/1.2.0"), "0.160.0"}, - {h("User-Agent", "Codex Desktop/0.162.3 (Windows 10.0.26100; x86_64) unknown", "originator", "Codex Desktop"), "0.162.3"}, - {h("User-Agent", "codex_cli_rs/0.150.0", "version", "0.150.0"), "0.162.3"}, + {h("User-Agent", "codex_cli_rs/0.170.0 (Mac OS 26.6.0; arm64) ghostty/1.2.0"), "0.170.0"}, + {h("User-Agent", "Codex Desktop/0.172.3 (Windows 10.0.26100; x86_64) unknown", "originator", "Codex Desktop"), "0.172.3"}, + {h("User-Agent", "codex_cli_rs/0.150.0", "version", "0.150.0"), "0.172.3"}, } { SawCodexClient(c.h) if got := codexVersion(); got != c.want { @@ -134,11 +135,89 @@ func TestCodexVersionSeen(t *testing.T) { if _, err := codexModels(context.Background(), sign); err != nil { t.Fatal(err) } - if asked != "0.162.3" || !strings.HasPrefix(ua, "codex_cli_rs/0.162.3 (") || ver != "0.162.3" { + if asked != "0.172.3" || !strings.HasPrefix(ua, "codex_cli_rs/0.172.3 (") || ver != "0.172.3" { t.Fatalf("models asked as client_version %q, User-Agent %q, version %q", asked, ua, ver) } } +// With no Codex CLI here, or an older one, the models list is still asked +// for as a Codex new enough for every model the account has: GPT-6.2 Sol +// and Astra, listed to 0.166.0 and later only, were never offered at the +// 0.159.0 magpie asked as (zzstar101 and m0eaki on X). npm's latest +// counts once read, and only a release version of it. +func TestCodexVersionPublished(t *testing.T) { + if compareClaudeVersion(codexClientVersion, "0.166.0") < 0 { + t.Fatalf("codexClientVersion %s is older than GPT-6.2's minimal_client_version 0.166.0", codexClientVersion) + } + t.Setenv("HOME", t.TempDir()) + t.Setenv("USERPROFILE", t.TempDir()) + t.Setenv("CODEX_HOME", "") + old, oldURL := codexExecutable, codexNpmLatest + codexExecutable = func() string { return "" } + reset := func() { + codexPublishedCache.Lock() + codexPublishedCache.v, codexPublishedCache.at = "", time.Time{} + codexPublishedCache.Unlock() + codexVersionCache.at = time.Time{} + codexSeen = seenVersion{} + } + t.Cleanup(func() { codexExecutable, codexNpmLatest = old, oldURL; reset() }) + reset() + if got := codexVersion(); got != codexClientVersion { + t.Fatalf("a test binary asked npm: %s", got) + } + // a request an older Codex sent goes on saying the floor, in its + // version header and its User-Agent ("请求头版本太低了", zzstar101) + req := httptest.NewRequest(http.MethodPost, "https://chatgpt.com/backend-api/codex/responses", nil) + req.Header.Set("version", "0.159.0") + req.Header.Set("User-Agent", "codex_cli_rs/0.159.0 (Mac OS 26.2.0; arm64)") + sign := codexSign(func(context.Context) (string, string, error) { return "tok", "acct-1", nil }) + if err := sign(context.Background(), req, []byte(`{}`)); err != nil { + t.Fatal(err) + } + if v, ua := req.Header.Get("version"), req.Header.Get("User-Agent"); v != codexClientVersion || !strings.Contains(ua, "/"+codexClientVersion+" (") { + t.Fatalf("an old Codex's request went as version %q, User-Agent %q", v, ua) + } + var answer string + var asks atomic.Int32 + fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + asks.Add(1) + w.Write([]byte(answer)) + })) + defer fake.Close() + codexNpmLatest = fake.URL + "/@openai/codex/latest" + for _, c := range []struct{ answer, want string }{ + {`{"name":"@openai/codex","version":"9.1.0"}`, "9.1.0"}, + {`{"version":"9.2.0-alpha.3"}`, codexClientVersion}, + {`{"version":"0.100.0"}`, codexClientVersion}, + {``, codexClientVersion}, + } { + reset() + answer = c.answer + asks.Store(0) + codexVersion() // asks in the background + deadline := time.Now().Add(10 * time.Second) + for { + codexPublishedCache.Lock() + done := !codexPublishedCache.at.IsZero() && !codexPublishedCache.asking + codexPublishedCache.Unlock() + if done { + break + } + if time.Now().After(deadline) { + t.Fatalf("%s: npm's answer was never read", c.answer) + } + time.Sleep(5 * time.Millisecond) + } + if got := codexVersion(); got != c.want { + t.Errorf("npm said %s: %s, want %s", c.answer, got, c.want) + } + if n := asks.Load(); n != 1 { + t.Errorf("npm said %s: asked %d times", c.answer, n) + } + } +} + // Codex CLI installed where a desktop app's PATH doesn't reach — under an // nvm Node, or an npm prefix ~/.npmrc names — is found. func TestCodexExecutableOffPath(t *testing.T) {