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.
125 lines
4.3 KiB
Go
125 lines
4.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"log"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/codexcat"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// The Codex app's picker shows only the models of Codex's list that a
|
|
// flag of OpenAI's names, for Codex on its built-in provider, unless Codex
|
|
// reads its list from a file (model_catalog_json): beside the ChatGPT
|
|
// sign-in it showed GPT alone, magpie's models in the list it was handed
|
|
// all the same (laddieyao on Discord). The CLI shows the whole list. So
|
|
// where the Codex app is installed, magpie writes the list it hands Codex
|
|
// into a file for Codex to read (agent.CodexDesktopList), and Codex asks
|
|
// for no list then: the backend's part of it is asked for here, with
|
|
// Codex's sign-in, and kept.
|
|
|
|
// CodexDesktopChanged is called when the backend's list for the Codex app
|
|
// changed, for the file to be written again (agent.SyncCatalogSoon).
|
|
var CodexDesktopChanged func()
|
|
|
|
// codexDeskFetch asks the backend for its list (a test's stands in).
|
|
var codexDeskFetch = provider.CodexLiveList
|
|
|
|
const (
|
|
// codexDeskFor is how long a list the backend gave stands before it
|
|
// is asked for again; codexDeskRetry how long after one it didn't
|
|
codexDeskFor = time.Hour
|
|
codexDeskRetry = 5 * time.Minute
|
|
codexDeskWait = 30 * time.Second
|
|
)
|
|
|
|
var codexDesk struct {
|
|
sync.Mutex
|
|
body []byte
|
|
got, tried time.Time
|
|
asking bool
|
|
// revoked: the last ask had the backend call the sign-in revoked
|
|
// (provider.CodexListRevoked)
|
|
revoked bool
|
|
done chan struct{} // closed as the ask under way ends
|
|
}
|
|
|
|
// CodexDesktopCatalog is the list a Codex beside its ChatGPT sign-in is
|
|
// handed (/backend-api/codex/models), as a file Codex reads with
|
|
// model_catalog_json: the backend's entries as it sent them, narrowed and
|
|
// set as the codex provider has them, magpie's after them. ok is false
|
|
// while the backend's list is not known: none asked for yet, or none it
|
|
// gave. That is unknown, never an empty list, and the file as it was
|
|
// stands. The backend is asked again in the background when its list is
|
|
// old, CodexDesktopChanged told when it changed.
|
|
//
|
|
// A sign-in the backend calls revoked, with no list of its from before,
|
|
// is handed Codex's last list of its own and magpie's models, as
|
|
// codexModels answers Codex then (#4308): the backend has said all it
|
|
// will for that sign-in, and without it the app's picker had none of
|
|
// magpie's models until the user signed in again.
|
|
func CodexDesktopCatalog() (b []byte, ok bool) {
|
|
codexDesk.Lock()
|
|
body, revoked := codexDesk.body, codexDesk.revoked
|
|
stale := body == nil || time.Since(codexDesk.got) >= codexDeskFor
|
|
if stale && !codexDesk.asking && time.Since(codexDesk.tried) >= codexDeskRetry {
|
|
codexDesk.asking, codexDesk.tried = true, time.Now()
|
|
codexDesk.done = make(chan struct{})
|
|
go codexDeskRefresh(codexDesk.done)
|
|
}
|
|
codexDesk.Unlock()
|
|
if body == nil && revoked {
|
|
var own []any
|
|
for _, e := range codexcat.CacheEntries() {
|
|
own = append(own, e)
|
|
}
|
|
b, err := json.MarshalIndent(map[string]any{"models": codexMerged(own, true)}, "", " ")
|
|
return b, err == nil
|
|
}
|
|
if body == nil {
|
|
return nil, false
|
|
}
|
|
// each caller's own copy: codexMerged writes into the entries
|
|
var list struct {
|
|
Models []any `json:"models"`
|
|
}
|
|
if json.Unmarshal(body, &list) != nil || len(list.Models) == 0 {
|
|
return nil, false
|
|
}
|
|
b, err := json.MarshalIndent(map[string]any{"models": codexMerged(list.Models, false)}, "", " ")
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
return b, true
|
|
}
|
|
|
|
func codexDeskRefresh(done chan struct{}) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), codexDeskWait)
|
|
defer cancel()
|
|
b, err := codexDeskFetch(ctx)
|
|
codexDesk.Lock()
|
|
changed := false
|
|
if err == nil {
|
|
changed = !bytes.Equal(b, codexDesk.body) || codexDesk.revoked
|
|
codexDesk.body, codexDesk.got, codexDesk.revoked = b, time.Now(), 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
|
|
}
|
|
codexDesk.asking = false
|
|
codexDesk.Unlock()
|
|
if err != nil {
|
|
log.Println("codex: the ChatGPT backend's model list for the Codex app:", err)
|
|
}
|
|
if changed && CodexDesktopChanged != nil {
|
|
CodexDesktopChanged()
|
|
}
|
|
close(done)
|
|
}
|