gateway: the Codex app's list stands on Codex's cache while the backend can't be reached (not_4O4 on X)

Beside the ChatGPT sign-in the Codex app's picker reads
magpie-desktop-models.json, written only once the ChatGPT backend's model
list is known. not_4O4 (Windows, Codex App 26.1106.6628.0) had no such file:
the backend's ask never succeeded from there, so the file was never written
and the picker showed GPT alone while the account had room, magpie's models
only (and no GPT) once it was used up and magpie became the provider.

After an ask has failed with no list from before for that sign-in,
CodexDesktopCatalog now hands Codex's own models_cache.json entries and
magpie's models (the revoked sign-in's path), told once through
CodexDesktopChanged; the backend's list replaces it when it answers. With no
cache the file stays as it was, and a 502 after a revoked sign-in is still
no answer.

Run (mote): go vet gateway and agent (darwin), GOOS=windows and linux -tags
nogui vet of gateway, go test ./internal/gateway ./internal/agent;
TestCodexDesktopCatalogBackendUnreachable is new and TestCodexDesktop*,
TestCodexDesktopCatalogRevokedSignIn still pass. On the Windows box
(win-run) the agent's Codex desktop tests passed and a used-up probe wrote
magpie-models.json with relative key as in the report.
Broken by hand: the stand-in only for revoked, the new test went red.
Not tried: the real Codex app on the reporter's network; the reporter's
used-up half (GPT missing from the provider catalog) did not reproduce, the
catalog carries codex/<slug> entries in the probe on macOS and Windows.
This commit is contained in:
yetone
2026-11-12 16:54:10 +08:00
parent c4f7ba9d3a
commit 327da691ae
3 changed files with 80 additions and 6 deletions
File diff suppressed because one or more lines are too long
+24 -5
View File
@@ -49,6 +49,9 @@ var codexDesk struct {
// revoked: the last ask had the backend call the sign-in revoked
// (provider.CodexListRevoked)
revoked bool
// failed: the last ask got no list (the backend not reachable from
// here, or not answering 200), and none is known for who
failed bool
// who is the sign-in body and revoked are of (codexDeskWho); asked
// the one the last ask was for; want the one a caller last asked about
who, asked, want string
@@ -80,10 +83,10 @@ func CodexDesktopCatalog() (b []byte, ok bool) {
who := codexDeskWho()
codexDesk.Lock()
codexDesk.want = who
body, revoked := codexDesk.body, codexDesk.revoked
body, revoked, failed := codexDesk.body, codexDesk.revoked, codexDesk.failed
mine := codexDesk.who == who
if !mine {
body, revoked = nil, false
body, revoked, failed = nil, false, false
}
stale := body == nil || time.Since(codexDesk.got) >= codexDeskFor
if stale && !codexDesk.asking && (codexDesk.asked != who || time.Since(codexDesk.tried) >= codexDeskRetry) {
@@ -92,11 +95,18 @@ func CodexDesktopCatalog() (b []byte, ok bool) {
go codexDeskRefresh(codexDesk.done, who)
}
codexDesk.Unlock()
if body == nil && revoked {
if body == nil && (revoked || failed) {
var own []any
for _, e := range codexcat.CacheEntries() {
own = append(own, e)
}
// the backend can't be asked (not reachable from here: a Windows
// box behind a firewall): Codex's own last list, from its
// models_cache.json, stands in. With none of it, the file stays
// as it was rather than hold magpie's models alone.
if !revoked && len(own) == 0 {
return nil, false
}
b, err := json.MarshalIndent(map[string]any{"models": codexMerged(own, true)}, "", " ")
return b, err == nil
}
@@ -124,19 +134,28 @@ func codexDeskRefresh(done chan struct{}, who string) {
b, err := codexDeskFetch(ctx)
codexDesk.Lock()
changed := false
wasRevoked := codexDesk.revoked && codexDesk.who == who
if codexDesk.who != who {
// another sign-in's list, and what was said of it, is not who's
codexDesk.body, codexDesk.revoked, codexDesk.who = nil, false, who
codexDesk.body, codexDesk.revoked, codexDesk.failed, codexDesk.who = nil, false, false, who
}
if err == nil {
changed = !bytes.Equal(b, codexDesk.body) || codexDesk.revoked
codexDesk.body, codexDesk.got, codexDesk.revoked = b, time.Now(), false
codexDesk.body, codexDesk.got, codexDesk.revoked, codexDesk.failed = b, time.Now(), false, false
} else if revoked := provider.CodexListRevoked(err); revoked != codexDesk.revoked {
// told only where it changes what is handed out: with no list
// from before (CodexDesktopCatalog)
changed = codexDesk.body == nil
codexDesk.revoked = revoked
}
// no list for who from before, and none now: the cached one stands in
// (CodexDesktopCatalog), told once
// (a 502 after the backend called the sign-in revoked is still no
// answer, as before)
if err != nil && codexDesk.body == nil && !codexDesk.failed && !wasRevoked && !codexDesk.revoked {
codexDesk.failed = true
changed = true
}
// Codex went on to another sign-in while this one was asked about:
// told, for the next look to ask about that one
if codexDesk.want != who {
+55 -1
View File
@@ -15,6 +15,7 @@ import (
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
@@ -30,7 +31,7 @@ func codexDeskReset(t *testing.T) {
<-done
}
codexDesk.Lock()
codexDesk.body, codexDesk.got, codexDesk.tried, codexDesk.asking, codexDesk.done, codexDesk.revoked = nil, time.Time{}, time.Time{}, false, nil, false
codexDesk.body, codexDesk.got, codexDesk.tried, codexDesk.asking, codexDesk.done, codexDesk.revoked, codexDesk.failed = nil, time.Time{}, time.Time{}, false, nil, false, false
codexDesk.who, codexDesk.asked, codexDesk.want = "", "", ""
codexDesk.Unlock()
}
@@ -264,3 +265,56 @@ func TestCodexDesktopCatalogKeepsNewlyListedBesidePicks(t *testing.T) {
t.Errorf("the account's own models beside the picks: %v\n%s", own, b)
}
}
// A backend that can't be reached from here (a firewall, a proxy that
// leaves chatgpt.com out; not_4O4 on X, Windows) never gave a list, so the
// file for the Codex app was never written and its picker had GPT alone
// beside the sign-in. Once the ask has failed with no list from before,
// Codex's own last list (models_cache.json) stands in, with magpie's
// models after it; with no such cache the file stays as it was.
func TestCodexDesktopCatalogBackendUnreachable(t *testing.T) {
codexSignedIn(t)
codexDeskReset(t)
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Anthropic: "http://127.0.0.1:1",
Models: []string{"claude-opus-5.5"}}); err != nil {
t.Fatal(err)
}
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "down", http.StatusBadGateway)
}))
defer up.Close()
was := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
defer func() { provider.CodexBase = was }()
var changed atomic.Int32
CodexDesktopChanged = func() { changed.Add(1) }
// no cache either: unknown, the file as it was
if _, ok := CodexDesktopCatalog(); ok {
t.Fatal("known before the backend answered")
}
codexDeskAsked(t)
if b, ok := CodexDesktopCatalog(); ok {
t.Fatalf("no list and no cache, yet known: %s", b)
}
if changed.Load() != 1 {
t.Fatalf("the failed ask told %d times, want once", changed.Load())
}
if err := os.WriteFile(catalog.CodexModelsCache(),
[]byte(`{"etag":"x","models":[{"slug":"gpt-5.5","priority":1,"base_instructions":"You are Codex, based on GPT-5.","visibility":"list"}]}`), 0o644); err != nil {
t.Fatal(err)
}
codexDesk.Lock()
codexDesk.tried = time.Time{}
codexDesk.Unlock()
CodexDesktopCatalog()
codexDeskAsked(t)
b, ok := CodexDesktopCatalog()
if !ok {
t.Fatal("the cached list does not stand in for a backend that can't be reached")
}
if !strings.Contains(string(b), `"gpt-5.5"`) || !strings.Contains(string(b), "relay/claude-opus-5.5") {
t.Fatalf("want Codex's cached GPT and magpie's models:\n%s", b)
}
}