iuengei on Discord: after turning Codex's routing off and restarting,
Codex's official models answered "unexpected status 404 Not Found: magpie
knows no model \"gpt-6.1-sol\"", in ChatGPT and Always ChatGPT modes too.
A Codex thread keeps the provider it was started on (the
[model_providers.magpie] table stays for that reason). A thread started
while magpie was Codex's provider (magpie API, or the takeover of a
used-up account) goes on sending to /v1 after Disconnect or a switch back
to ChatGPT. An official model picked in it reaches serve as a bare
"gpt-6.1-sol"; with no ChatGPT account in magpie serving it (the Codex
sign-in removed from Providers, or subscription accounts off) it was
answered 404. /backend-api/codex relays such a model on Codex's own
sign-in; /v1 never did.
Now handle (Responses) puts such a request on the ChatGPT backend's route
(codexBackend) signed with Codex's own ChatGPT sign-in as Codex keeps it
(provider.CodexSignInAsKept, never renewed by magpie), magpie's key and
x-openai-actor-authorization left behind (codexOwnOffMagpie). Only for: a
bare model no provider, group or stand-in serves, not disabled or
switched off; one of Codex's own (its models cache, magpie's entries left
out) or OpenAI's in the catalog; from Codex on this computer (local); no
gateway key held to models or accounts. When the sign-in can't be had
(subscription accounts off, signed out, expired) the 404 says the thread
was started on magpie, why the sign-in can't be used, and to start a new
thread. Codex's identity and prompts are untouched.
Tests (internal/gateway/codex_own_v1_test.go): the reporter's case, a
sandbox HOME with Codex signed in, gpt-6.1-sol in its models cache, the
sign-in removed from magpie's Providers, Codex's request to /v1/responses
with magpie's key: was 404 "magpie knows no model \"gpt-6.1-sol\"", now
200 from a stand-in ChatGPT backend that got Codex's token and account
id, not magpie's key or header. Not sent: from another computer, a key
held to fake/*, a model that isn't OpenAI's (still 404). With accounts
off, a 404 that says why.
Run (on mote, at this commit's tree rebased on 173fb6ab0):
- gofmt -l internal/gateway internal/provider: clean
- go vet darwin, GOOS=linux -tags nogui, GOOS=windows for
./internal/gateway ./internal/provider: ok
- go test -timeout 45m ./internal/gateway ./internal/provider: ok (gateway 482s, provider 91s)
Broken by hand: the hook in handle off, GoesOnSignIn and SaysWhy are red
(the old 404); local() off, StaysHere/another_computer is red. The held
key is also kept off the relay by codexAccounts in codexBackend, so the
guard here off alone leaves that subtest green.
Not tried: a real Codex app thread against a real ChatGPT account (no
live ~/.codex touched).
143 lines
5.8 KiB
Go
143 lines
5.8 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// codexOwnOffMagpieSetup: Codex signed in to ChatGPT, its models cache
|
|
// listing gpt-6.1-sol, and no ChatGPT account in magpie serving it: the
|
|
// sign-in removed from magpie's Providers (hidden), or with discoveryOff
|
|
// subscription accounts turned off. The ChatGPT backend stands in and
|
|
// notes what it was sent.
|
|
func codexOwnOffMagpieSetup(t *testing.T, discoveryOff bool) (got *http.Header, models *[]string) {
|
|
t.Helper()
|
|
codexSignedIn(t)
|
|
if discoveryOff {
|
|
t.Setenv("MAGPIE_NO_ACCOUNTS", "1")
|
|
} else if err := provider.Delete("codex"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
provider.ForgetAccounts()
|
|
home := os.Getenv("HOME")
|
|
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"),
|
|
[]byte(`{"etag":"e1","models":[{"slug":"gpt-6.1-sol","display_name":"gpt-6.1-sol","priority":1}]}`), 0o600)
|
|
got, models = &http.Header{}, &[]string{}
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
b, _ := io.ReadAll(r.Body)
|
|
*got = r.Header.Clone()
|
|
*models = append(*models, modelOf(b))
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, sse(
|
|
`data: {"type":"response.created","response":{"id":"r1","model":"gpt-6.1-sol"}}`,
|
|
`data: {"type":"response.output_text.delta","delta":"pong"}`,
|
|
`data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":7,"output_tokens":1}}}`))
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
was := provider.CodexBase
|
|
provider.CodexBase = up.URL + "/backend-api/codex"
|
|
t.Cleanup(func() { provider.CodexBase = was })
|
|
return got, models
|
|
}
|
|
|
|
func codexOnMagpieRequest(from string) *http.Request {
|
|
req := httptest.NewRequest("POST", "/v1/responses",
|
|
strings.NewReader(`{"model":"gpt-6.1-sol","stream":true,"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"ping"}]}]}`))
|
|
req.RemoteAddr = from
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("User-Agent", "codex_cli_rs/0.164.0 (Mac OS 26.2.0; arm64) Terminal")
|
|
req.Header.Set("Originator", "codex_cli_rs")
|
|
req.Header.Set("Authorization", "Bearer "+Token)
|
|
req.Header.Set("X-Openai-Actor-Authorization", "magpie")
|
|
return req
|
|
}
|
|
|
|
// A thread Codex started while magpie was its provider keeps sending to
|
|
// magpie's /v1 after Codex is disconnected or set back to ChatGPT; one of
|
|
// Codex's own models picked in it, which no account in magpie serves,
|
|
// goes to the ChatGPT backend on Codex's own sign-in, magpie's key and
|
|
// header left behind, where it was answered 404 "magpie knows no model
|
|
// \"gpt-6.1-sol\"" (iuengei on Discord).
|
|
func TestCodexOwnModelOnMagpieThreadGoesOnSignIn(t *testing.T) {
|
|
got, models := codexOwnOffMagpieSetup(t, false)
|
|
rec := httptest.NewRecorder()
|
|
New().Handler().ServeHTTP(rec, codexOnMagpieRequest("127.0.0.1:51000"))
|
|
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "pong") {
|
|
t.Fatalf("got %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
if len(*models) != 1 || (*models)[0] != "gpt-6.1-sol" {
|
|
t.Fatalf("backend asked for %v", *models)
|
|
}
|
|
auth := got.Get("Authorization")
|
|
if auth == "" || strings.Contains(auth, Token) {
|
|
t.Fatalf("Authorization %q: want Codex's sign-in, not magpie's key", auth)
|
|
}
|
|
if got.Get("Chatgpt-Account-Id") != "acct-1" {
|
|
t.Fatalf("chatgpt-account-id %q, want acct-1", got.Get("Chatgpt-Account-Id"))
|
|
}
|
|
if v := got.Get("X-Openai-Actor-Authorization"); v != "" {
|
|
t.Fatalf("magpie's provider header went to OpenAI: %q", v)
|
|
}
|
|
}
|
|
|
|
// Not for a request from another computer, nor a gateway key held to some
|
|
// models, nor a model that isn't OpenAI's: those are answered as before,
|
|
// nothing sent on Codex's sign-in.
|
|
func TestCodexOwnModelOnMagpieThreadStaysHere(t *testing.T) {
|
|
t.Run("another computer", func(t *testing.T) {
|
|
_, models := codexOwnOffMagpieSetup(t, false)
|
|
rec := httptest.NewRecorder()
|
|
New().Handler().ServeHTTP(rec, codexOnMagpieRequest("192.0.2.7:51000"))
|
|
if len(*models) != 0 {
|
|
t.Fatalf("a request from another computer went on Codex's sign-in: %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
})
|
|
t.Run("held key", func(t *testing.T) {
|
|
_, models := codexOwnOffMagpieSetup(t, false)
|
|
req := codexOnMagpieRequest("127.0.0.1:51000")
|
|
req = req.WithContext(access.WithIdentity(req.Context(), access.Identity{KeyID: "k1", Models: []string{"fake/*"}}))
|
|
rec := httptest.NewRecorder()
|
|
New().handle(provider.Responses)(rec, req)
|
|
if len(*models) != 0 {
|
|
t.Fatalf("a held key went on Codex's sign-in: %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
})
|
|
t.Run("not OpenAI's", func(t *testing.T) {
|
|
_, models := codexOwnOffMagpieSetup(t, false)
|
|
req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(`{"model":"no-such-model-xyz","input":"hi"}`))
|
|
req.RemoteAddr = "127.0.0.1:51000"
|
|
req.Header = codexOnMagpieRequest("127.0.0.1:51000").Header
|
|
rec := httptest.NewRecorder()
|
|
New().Handler().ServeHTTP(rec, req)
|
|
if len(*models) != 0 || rec.Code != 404 {
|
|
t.Fatalf("an unknown model: %d %s, sent %v", rec.Code, rec.Body.String(), *models)
|
|
}
|
|
})
|
|
}
|
|
|
|
// With subscription accounts off, magpie reads no sign-in, Codex's
|
|
// neither: the request is answered why it can't be served and what to do,
|
|
// not with the bare "magpie knows no model".
|
|
func TestCodexOwnModelOnMagpieThreadSaysWhy(t *testing.T) {
|
|
_, models := codexOwnOffMagpieSetup(t, true)
|
|
rec := httptest.NewRecorder()
|
|
New().Handler().ServeHTTP(rec, codexOnMagpieRequest("127.0.0.1:51000"))
|
|
body := rec.Body.String()
|
|
if rec.Code != 404 || len(*models) != 0 {
|
|
t.Fatalf("got %d %s, sent %v", rec.Code, body, *models)
|
|
}
|
|
for _, want := range []string{"one of Codex's own models", "started while magpie was Codex's model provider", "subscription accounts are off", "Start a new thread"} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("answer lacks %q: %s", want, body)
|
|
}
|
|
}
|
|
}
|