codex: a cached entry keeps the cache's own max when magpie's list has none, and Codex's models_cache.json is read under CODEX_HOME everywhere (follow-up to #674, from reviewing #679) — restoring a cached native entry (backend away) set max_context_window to max(MaxContext, Context) from magpie's live list, so a list saved without the max (before magpie kept it) wrote the window over the cache's own (872000 became 272000); now a list with no max leaves the cache's, and codexcat.Window still raises it to a window past it. codexcat.CacheEntries and catalog.Codex read ~/.codex/models_cache.json even with CODEX_HOME set, while codexVersion and codexInstructions followed it; all four now take catalog.CodexModelsCache() (CODEX_HOME, else ~/.codex), and provider's codexCLIHome and the copy in codexInstructions are gone. TestCodexReadsCacheUnderCodexHome, TestCodexCachedListUnderCodexHomeKeepsItsMax and TestCodexCatalogEntryFromCodexHome fail on the old code (the ~/.codex cache read, none listed from CODEX_HOME); TestCodexNativeModelsTakeSetWindows now wants a cached gpt-5.5 with no live max to keep the cache's 400000 (old code: 272000). go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass but TestProvidersAnswerWhileListsComeIn, a timing test that passes alone.

This commit is contained in:
yetone
2026-10-03 21:39:13 +08:00
parent 7b5c3cb797
commit 9f7b6bd86c
7 changed files with 136 additions and 23 deletions
+13 -2
View File
@@ -769,11 +769,22 @@ func Providers() []string {
return ids
}
// CodexHome is where Codex CLI keeps its state: $CODEX_HOME, else ~/.codex.
func CodexHome() string {
if dir := os.Getenv("CODEX_HOME"); dir != "" {
return dir
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".codex")
}
// CodexModelsCache is the model list Codex CLI keeps, under CodexHome.
func CodexModelsCache() string { return filepath.Join(CodexHome(), "models_cache.json") }
// Codex returns the models Codex itself lists, straight from the cache the
// Codex CLI writes; there is no compiled-in list to fall back to.
func Codex() []Model {
home, _ := os.UserHomeDir()
out, _ := filememo.Read("codex models", filepath.Join(home, ".codex", "models_cache.json"), parseCodex)
out, _ := filememo.Read("codex models", CodexModelsCache(), parseCodex)
return slices.Clone(out)
}
+22
View File
@@ -0,0 +1,22 @@
package catalog
import (
"os"
"path/filepath"
"testing"
)
// Codex's own models are read from the cache under CODEX_HOME when it is
// set, as Codex CLI writes it there, not from ~/.codex.
func TestCodexReadsCacheUnderCodexHome(t *testing.T) {
home, codexHome := t.TempDir(), t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("CODEX_HOME", codexHome)
os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"), []byte(`{"models":[{"slug":"in-home","visibility":"list"}]}`), 0o644)
os.WriteFile(filepath.Join(codexHome, "models_cache.json"), []byte(`{"models":[{"slug":"in-codex-home","visibility":"list"}]}`), 0o644)
if ms := Codex(); len(ms) != 1 || ms[0].ID != "in-codex-home" {
t.Errorf("Codex() = %+v, want in-codex-home alone", ms)
}
}
+1 -2
View File
@@ -164,8 +164,7 @@ func Entries(ms []catalog.Model, after int) []any {
// CacheEntries is Codex's own models, as models_cache.json describes them
// for the ChatGPT account it last asked with, by slug.
func CacheEntries() map[string]map[string]any {
home, _ := os.UserHomeDir()
b, err := os.ReadFile(filepath.Join(home, ".codex", "models_cache.json"))
b, err := os.ReadFile(catalog.CodexModelsCache())
if err != nil {
return nil
}
+91
View File
@@ -0,0 +1,91 @@
package gateway
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/codexcat"
"github.com/yetone/magpie/internal/provider"
)
// With the backend away, the list a ChatGPT sign-in is handed is made from
// Codex's cache — the one under CODEX_HOME when it is set, as Codex keeps
// it there. A cached entry restored to the account's own window keeps the
// cache's max when magpie's live list has none (one saved before magpie
// kept the max): 872000 stays, it doesn't fall to the window.
func TestCodexCachedListUnderCodexHomeKeepsItsMax(t *testing.T) {
codexSignedIn(t)
codexHome := t.TempDir()
t.Setenv("CODEX_HOME", codexHome)
// magpie handed Codex 600000 earlier; the window is no longer set
os.WriteFile(filepath.Join(codexHome, "models_cache.json"), []byte(`{"etag":"\"v1\"","models":[
{"slug":"gpt-6.1-sol","priority":1,"context_window":600000,"max_context_window":872000,"base_instructions":"sol prompt"},
{"slug":"gpt-6-astra","priority":2,"context_window":272000,"max_context_window":272000}]}`), 0o644)
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(500) }))
defer up.Close()
was := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
defer func() { provider.CodexBase = was }()
// the live list: one without a max, one with a max past the cache's
catalog.SaveLive("codex", provider.CodexBase, []catalog.Model{
{ID: "gpt-6.1-sol", Context: 272000},
{ID: "gpt-6-astra", Context: 272000, MaxContext: 872000},
})
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 []struct {
Slug string `json:"slug"`
Context int `json:"context_window"`
Max int `json:"max_context_window"`
Prompt string `json:"base_instructions"`
} `json:"models"`
}
json.Unmarshal(rec.Body.Bytes(), &got)
want := map[string][2]int{"gpt-6.1-sol": {272000, 872000}, "gpt-6-astra": {272000, 872000}}
seen := 0
for _, m := range got.Models {
w, ok := want[m.Slug]
if !ok {
continue
}
seen++
if m.Context != w[0] || m.Max != w[1] {
t.Errorf("cached %s %d/%d, want %d/%d", m.Slug, m.Context, m.Max, w[0], w[1])
}
if m.Slug == "gpt-6.1-sol" && m.Prompt != "sol prompt" {
t.Errorf("cached gpt-6.1-sol prompt %q, want the cache's", m.Prompt)
}
}
if seen != len(want) {
t.Fatalf("%d of the cache's models listed, want %d (from CODEX_HOME): %s", seen, len(want), rec.Body.String())
}
}
// A ChatGPT account's model in a catalog magpie writes takes its entry from
// the cache under CODEX_HOME when it is set.
func TestCodexCatalogEntryFromCodexHome(t *testing.T) {
codexSignedIn(t)
codexHome := t.TempDir()
t.Setenv("CODEX_HOME", codexHome)
os.WriteFile(filepath.Join(codexHome, "models_cache.json"), []byte(`{"etag":"\"v1\"","models":[
{"slug":"gpt-6.1-sol","context_window":272000,"max_context_window":872000,"base_instructions":"sol prompt"}]}`), 0o644)
es := codexcat.Entries([]catalog.Model{{ID: "codex/gpt-6.1-sol", Name: "Sol", Context: 272000}}, 0)
b, _ := json.Marshal(es[0])
var e struct {
Max int `json:"max_context_window"`
Prompt string `json:"base_instructions"`
}
json.Unmarshal(b, &e)
if e.Prompt != "sol prompt" || e.Max != 872000 {
t.Errorf("entry max %d, prompt %.20q, want the CODEX_HOME cache's 872000 and \"sol prompt\"", e.Max, e.Prompt)
}
}
@@ -112,8 +112,9 @@ func TestCodexNativeModelsTakeSetWindows(t *testing.T) {
if c, x := window(got["gpt-6.1-sol"]); c != 272000 || x != 872000 {
t.Errorf("cached gpt-6.1-sol %d/%d, want 272000/872000", c, x)
}
if c, x := window(got["gpt-5.5"]); c != 272000 || x != 272000 {
t.Errorf("cached gpt-5.5 %d/%d, want 272000/272000", c, x)
// the live list has no max for it: the cache's own stays
if c, x := window(got["gpt-5.5"]); c != 272000 || x != 400000 {
t.Errorf("cached gpt-5.5 %d/%d, want 272000/400000", c, x)
}
}
+5 -11
View File
@@ -85,7 +85,7 @@ func codexVersion() string {
var c struct {
ClientVersion string `json:"client_version"`
}
if b, err := os.ReadFile(filepath.Join(codexCLIHome(), "models_cache.json")); err == nil && json.Unmarshal(b, &c) == nil {
if b, err := os.ReadFile(catalog.CodexModelsCache()); err == nil && json.Unmarshal(b, &c) == nil {
newer(c.ClientVersion)
}
if exe := codexExecutable(); exe != "" {
@@ -100,15 +100,6 @@ func codexVersion() string {
return newerVersion(codexVersionCache.v, codexSeen.get())
}
// codexCLIHome is where Codex CLI keeps its state: CODEX_HOME, else ~/.codex.
func codexCLIHome() string {
if dir := os.Getenv("CODEX_HOME"); dir != "" {
return dir
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".codex")
}
// newerVersion is the later of two versions, a when b isn't one.
func newerVersion(a, b string) string {
if b = claudeSemverRE.FindString(b); b != "" && (a == "" || compareClaudeVersion(b, a) > 0) {
@@ -425,7 +416,10 @@ func CodexNativeWindow(restore bool) func(slug string) (n, most int, ok bool) {
}
for _, m := range live {
if m.ID == slug && m.Context > 0 {
return m.Context, max(m.MaxContext, m.Context), true
// a list without the max (saved before magpie kept
// it) leaves the cache's own; Window still raises it to
// the window when it's below
return m.Context, m.MaxContext, true
}
}
return 0, 0, false
+1 -6
View File
@@ -227,12 +227,7 @@ func codexInstructions(model string) string {
}); ok {
return s
}
dir := os.Getenv("CODEX_HOME")
if dir == "" {
home, _ := os.UserHomeDir()
dir = filepath.Join(home, ".codex")
}
if s, ok := codexCLIPrompts.get(filepath.Join(dir, "models_cache.json"), model, codexPrompts); ok {
if s, ok := codexCLIPrompts.get(catalog.CodexModelsCache(), model, codexPrompts); ok {
return s
}
return codexcat.Prompt