jjt on Discord: Codex's ChatGPT sign-in was revoked, and magpie's
models were gone from Codex until they signed in again.
Cause: Codex keeps a revoked sign-in's auth.json as it was and remembers
the permanent refresh failure in memory alone (codex-rs
login/src/auth/manager.rs). magpie read auth.json alone
(codexChatGPTKnown), counted the sign-in and wired Codex beside it
(openai_base_url, requires_openai_auth). There, Codex's /models went to
the gateway, which passed OpenAI's 401 token_revoked through, so the
picker had no magpie model; and every turn, magpie's models too, failed
at Codex 0.162.1's workspace routing discovery
(/backend-api/wham/accounts/check, 401).
Change:
- provider: codexRefresh tells a refusal Codex takes as permanent
(request_chatgpt_token_refresh: refresh_token_expired, _reused or
_invalidated, a 400 invalid_grant, or a 401 - here only a 401 that
carries an error code, read as oauth/error.rs reads it). When magpie's
own refresh of Codex's sign-in (codexTokenRenewing, from the usage read
that KeepOnAnAccountWithRoom runs) is refused that way, and the store
still holds that refresh token, codex-signin-gone.json keeps the Codex
home -> sha256 of the refused refresh token (no token stored). A
successful refresh forgets it. Writes Touch the catalog so the agents
resync; the loop's noteCodexGoneNow notices a sign-in made again
elsewhere within minutes.
- agent: codexChatGPTKnown reads such a sign-in as none
(provider.CodexSignInGone), as v0.1.2087 did for forced_login_method =
"api": magpie is Codex's provider without requires_openai_auth, the
user's auth.json stays. A record that can't be read is unknown (as
magpie last wired it). Signing in again gives another refresh token,
which the record doesn't name: Sync writes requires_openai_auth back.
- gateway: codexModels doesn't pass a revoked /models answer
(provider.SignInRevoked) through: it lists Codex's cached models and
magpie's. The desk file does the same while the live list says revoked
(CodexListRevoked). Any other refusal (token_expired, 5xx) is as before.
- docs: agent-wiring and gateway-routing.
Run:
- real codex 0.162.1 (app-server over stdio, and exec) on mote, sandbox
HOME/CODEX_HOME, fake OpenAI (chatgpt_base_url,
CODEX_REFRESH_TOKEN_URL_OVERRIDE, provider.CodexBase) answering 401
token_revoked and refresh_token_invalidated; no real network.
- Old wiring beside the dead sign-in, old gateway: model/list had no
magpie model (jjt's case). With the gateway change: fake/m1 listed,
but exec -m fake/m1 fails at workspace routing discovery (401), and
account/read errors too - why the wiring change is needed.
- Uncommitted e2e in the agent package with magpie's own written
config: Set("fake/m1") wrote it beside the sign-in; magpie's
LoginUsage("codex") then heard the refresh refused, Sync rewrote it
to model_provider = "magpie", model_catalog_json, no
requires_openai_auth. account/read: {"account":null,
"requiresOpenaiAuth":false}; model/list: fake/m1 plus Codex's
models; exec -m fake/m1 answered "hi from magpie"; auth.json byte
for byte the same.
- Red on the old code (broken by hand): without the codexChatGPTKnown
check TestCodexGoneSignInIsNoChatGPT is red; without the
noteCodexSignInGone call TestCodexSignInGoneWhenRefusedForGood is red;
with codexRefreshGone true for every failure TestCodexRefreshGoneRule
is red; TestCodexModelListRevokedSignIn and
TestCodexDesktopCatalogRevokedSignIn are red on the old gateway.
Taking out the store re-check in noteCodexSignInGone leaves the tests
green: a token Codex rotated meanwhile is already held by the hash
comparison, and the re-check only saves a needless record, log line
and resync. Not guarded on its own.
- via mote-run, at this commit rebased on origin/main 1f03d7546:
gofmt -l clean; go vet ./internal/provider ./internal/agent
./internal/gateway for darwin, linux (-tags nogui) and windows ok;
go test -timeout 45m -p 1 of the three: provider ok (92.3s), agent ok
(192.1s), gateway ok (530.9s). The first full run had the new provider
test red: run after the other Codex tests it read their cached logins
and listed none; it now has the resetRevoked/rememberLogins setup its
siblings use.
Rebased after the run onto 05a6087e1 (access: key model scope) and
8cbd0a9c9 (GUI loading bird); neither touches the Codex paths, and
this push wasn't run again on them.
Not tried: the real Codex desktop app (ChatGPT.app); signing in again
with the real binary (the unit test writes a new auth.json); the
keychain credential store; magpie's left-alone mode, where magpie
doesn't refresh Codex's sign-in, so nothing is recorded there;
Codex homes on Windows and in WSL.
152 lines
5.1 KiB
Go
152 lines
5.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// revokedBody is what the ChatGPT backend answered a request on a revoked
|
|
// sign-in with (ysicing, #1793).
|
|
const revokedBody = `{"error":{"message":"Encountered invalidated oauth token for user, failing request","type":null,"code":"token_revoked","param":null},"status":401}`
|
|
|
|
// A Codex whose ChatGPT sign-in was revoked asks for its model list beside
|
|
// it (jjt on Discord, #4308). The backend's 401 went to Codex, whose one
|
|
// refresh fails for good for such a sign-in, and Codex kept its bundled
|
|
// list: none of magpie's models until the user signed in again. Now the
|
|
// list is Codex's last of its own and magpie's. A 401 that says the token
|
|
// expired is still Codex's to refresh, and a sign-in made again has the
|
|
// backend's list on the next ask: nothing of the revoked one is kept.
|
|
func TestCodexModelListRevokedSignIn(t *testing.T) {
|
|
setup(t, provider.Chat, &fake{t: t})
|
|
forgetCodexLists(t)
|
|
var say atomic.Value
|
|
say.Store("revoked")
|
|
chatgpt(t, func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
switch say.Load() {
|
|
case "revoked":
|
|
w.WriteHeader(401)
|
|
io.WriteString(w, revokedBody)
|
|
case "expired":
|
|
w.WriteHeader(401)
|
|
io.WriteString(w, `{"error":{"message":"Provided authentication token is expired. Please try refreshing it.","code":"token_expired"},"status":401}`)
|
|
default:
|
|
io.WriteString(w, `{"models":[{"slug":"gpt-new","priority":1}]}`)
|
|
}
|
|
})
|
|
home, _ := os.UserHomeDir()
|
|
os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
|
|
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"),
|
|
[]byte(`{"etag":"\"v1\"","models":[{"slug":"gpt-5.5","priority":1,"base_instructions":"x"}]}`), 0o644)
|
|
was := codexListFor
|
|
codexListFor = time.Nanosecond
|
|
t.Cleanup(func() { codexListFor = was })
|
|
h := New().Handler()
|
|
|
|
if code, has := codexListAsk(h); code != 200 || !has["fake/m1"] || !has["gpt-5.5"] {
|
|
t.Fatalf("revoked sign-in: %d %v, want 200 with magpie's models and Codex's last list", code, has)
|
|
}
|
|
codexLists.asking.Wait()
|
|
|
|
say.Store("expired")
|
|
if code, _ := codexListAsk(h); code != 401 {
|
|
t.Fatalf("expired token: %d, want the backend's 401 for Codex to refresh", code)
|
|
}
|
|
codexLists.asking.Wait()
|
|
|
|
say.Store("ok")
|
|
if code, has := codexListAsk(h); code != 200 || !has["gpt-new"] || !has["fake/m1"] {
|
|
t.Fatalf("signed in again: %d %v, want the backend's list with magpie's", code, has)
|
|
}
|
|
}
|
|
|
|
// The Codex app's file list (CodexDesktopCatalog) for a sign-in the
|
|
// backend calls revoked, with no list of the backend's from before: Codex's
|
|
// last list of its own and magpie's models, told as a change for the file
|
|
// to be written. A network error after it says nothing either way, and a
|
|
// sign-in made again brings the backend's list back.
|
|
func TestCodexDesktopCatalogRevokedSignIn(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)
|
|
}
|
|
home, _ := os.UserHomeDir()
|
|
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"),
|
|
[]byte(`{"etag":"\"v1\"","models":[{"slug":"gpt-5.5","priority":1,"base_instructions":"x"}]}`), 0o644)
|
|
var say atomic.Value
|
|
say.Store("revoked")
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch say.Load() {
|
|
case "revoked":
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(401)
|
|
io.WriteString(w, revokedBody)
|
|
case "down":
|
|
http.Error(w, "down", http.StatusBadGateway)
|
|
default:
|
|
io.WriteString(w, `{"models":[{"slug":"gpt-new","priority":1,"visibility":"list"}]}`)
|
|
}
|
|
}))
|
|
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) }
|
|
slugs := func(b []byte) map[string]bool {
|
|
var got struct {
|
|
Models []map[string]any `json:"models"`
|
|
}
|
|
json.Unmarshal(b, &got)
|
|
out := map[string]bool{}
|
|
for _, m := range got.Models {
|
|
s, _ := m["slug"].(string)
|
|
out[s] = true
|
|
}
|
|
return out
|
|
}
|
|
again := func() {
|
|
codexDesk.Lock()
|
|
codexDesk.got, codexDesk.tried = time.Time{}, time.Time{}
|
|
codexDesk.Unlock()
|
|
CodexDesktopCatalog()
|
|
codexDeskAsked(t)
|
|
}
|
|
|
|
if _, ok := CodexDesktopCatalog(); ok {
|
|
t.Fatal("known before the backend answered")
|
|
}
|
|
codexDeskAsked(t)
|
|
if changed.Load() != 1 {
|
|
t.Fatalf("revoked told %d times, want once", changed.Load())
|
|
}
|
|
b, ok := CodexDesktopCatalog()
|
|
if s := slugs(b); !ok || !s["relay/claude-opus-5.5"] || !s["gpt-5.5"] {
|
|
t.Fatalf("revoked sign-in: %v %s", ok, b)
|
|
}
|
|
|
|
say.Store("down")
|
|
again()
|
|
if _, ok := CodexDesktopCatalog(); ok {
|
|
t.Fatal("a 502 after the revoked sign-in taken as an answer")
|
|
}
|
|
|
|
say.Store("ok")
|
|
again()
|
|
b, ok = CodexDesktopCatalog()
|
|
if s := slugs(b); !ok || !s["gpt-new"] || !s["relay/claude-opus-5.5"] || s["gpt-5.5"] {
|
|
t.Fatalf("signed in again: %v %s", ok, b)
|
|
}
|
|
}
|