codex: a Codex switched off in magpie still has its held ChatGPT account read, so the Codex app sends magpie's models (sherlock_23333 on X)
sherlock_23333 on X: once the ChatGPT account Codex is signed in to ran out, the Codex app's Send stayed greyed even on a magpie model, through restarts and reconnects. The gate is the Codex app's own (ChatGPT.app 26.1021, composer gM): Send is blocked while its authMethod is chatgpt and its own /wham/usage says rate_limit.allowed false with no credits. magpie's one lever is the takeover (codex.out): magpie becomes Codex's provider without requires_openai_auth, so the app-server reports no ChatGPT account and the gate lifts. That runs only when provider.CodexUsedUp says the account is held, and CodexUsedUp read LoginUsage, which returns nothing for a provider switched off or removed in magpie (leftAlone). So a user who turned Codex off in magpie once its allowance ran out never got the takeover, and one already taken over was put back on the held account. Now, with Codex left alone, CodexUsedUp reads the active account alone (codexHoldReading) while Codex takes magpie's models beside its sign-in or magpie is its provider for the held account (Agent.HoldMatters, via the new provider.CodexHoldMatters hook). At most once a minute (the loginReading cache), with the token as Codex keeps it, never renewed or written (codexTokenAsKept); the Usage page still shows nothing of it. Codex's prompts and base_instructions are untouched. Run atf41b63232+ this change, on mote via mote-run: gofmt -l clean; go vet ./...; GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet ./...; go build ./... all ok. go test -count=1 -timeout 45m ./internal/provider ./internal/agent: ok (219s, 340s). Rebased on9f25de56c(a Cursor mover min and a GUI test), then at9f25de56c+ this change: go vet and go test -count=1 -timeout 45m of ./internal/provider ./internal/agent again: ok (178s, 273s).8c63c5b78(plugin speech models; no file in common) landed on main after that run and this is rebased onto it; the two packages were not run again with it. Broken by hand: without the leftAlone branch in CodexUsedUp, TestCodexHoldReadLeftAlone fails ("switched off: CodexUsedUp = false on a held account"); with the left-alone token read sent back through codexToken, it fails with the expired sign-in refreshed once. Not tried: the reporter's own config (whether their Codex was off in magpie is inferred, not confirmed); the real Codex app against a held account; internal/gateway's suite (no gateway code changed); Linux and Windows test runs (vet only).
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -187,6 +187,12 @@ type Agent struct {
|
||||
// its sign-in only as it opens, sends nothing on it till it is opened
|
||||
// again (MasterYi on Discord).
|
||||
AppOut func() bool
|
||||
// HoldMatters reports Codex takes magpie's models beside a ChatGPT
|
||||
// sign-in, so that magpie is its provider while the Codex app holds
|
||||
// the account (out), or is that already: what the app holds is read
|
||||
// for it even with Codex switched off in magpie
|
||||
// (provider.CodexHoldMatters).
|
||||
HoldMatters func() bool
|
||||
// SideModel is the model Codex is set to when it is one of magpie's,
|
||||
// picked beside its ChatGPT sign-in (by the base URL, magpie not its
|
||||
// provider), "" otherwise: the gateway answers the Codex app's hidden
|
||||
|
||||
@@ -52,6 +52,13 @@ func init() {
|
||||
// and when Codex is connected or disconnected, or its route otherwise
|
||||
// changes, so a running app sends where its config now says
|
||||
provider.CodexAppRoute = codexAppRoute
|
||||
// and what the Codex app holds is read while Codex takes magpie's
|
||||
// models beside its sign-in, Codex switched off in magpie or not
|
||||
provider.CodexHoldMatters = func() bool {
|
||||
home, _ := homes()
|
||||
a := codex(home)
|
||||
return a.HoldMatters != nil && a.HoldMatters()
|
||||
}
|
||||
}
|
||||
|
||||
// codexAppRoute is where Codex's config sends it, as the app-server reads
|
||||
|
||||
@@ -1525,8 +1525,12 @@ func codexIn(at place) *Agent {
|
||||
// mark stays for Sync to put it back beside the sign-in: the
|
||||
// Agents row said "its own models stay in /model" while the Codex
|
||||
// app was meant to be in its API state (#2267)
|
||||
Joined: func() bool { return joined() && !asProvider() },
|
||||
AppOut: func() bool { return asProvider() && !api() && chatGPT() && stashLoad()[at.key("codex.out")] == "1" },
|
||||
Joined: func() bool { return joined() && !asProvider() },
|
||||
AppOut: func() bool { return asProvider() && !api() && chatGPT() && stashLoad()[at.key("codex.out")] == "1" },
|
||||
HoldMatters: func() bool {
|
||||
return stashLoad()[at.key("codex.login")] == "" && chatGPT() &&
|
||||
(viaBase() || asProvider() && stashLoad()[at.key("codex.out")] == "1")
|
||||
},
|
||||
Beside: beside,
|
||||
Routed: routed,
|
||||
FailingOver: failingOver,
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// What the Codex app holds is read, Codex switched off in magpie or not,
|
||||
// while it decides where Codex sends (sherlock_23333 on X): Codex on
|
||||
// magpie's models beside its ChatGPT sign-in, or magpie its provider for
|
||||
// the held account. Not for a Codex kept on ChatGPT, on magpie's API, or
|
||||
// not on magpie at all.
|
||||
func TestCodexHoldMatters(t *testing.T) {
|
||||
home, _ := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "")
|
||||
t.Cleanup(func() { codexUsedUp = func() bool { return false } })
|
||||
cx := codex(home)
|
||||
if cx.HoldMatters() {
|
||||
t.Fatal("not on magpie: HoldMatters")
|
||||
}
|
||||
if err := cx.Fields[0].Set("fake/m1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !cx.HoldMatters() {
|
||||
t.Fatal("beside the sign-in: not HoldMatters")
|
||||
}
|
||||
codexUsedUp = func() bool { return true }
|
||||
if err := cx.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !cx.AppOut() || !cx.HoldMatters() {
|
||||
t.Fatalf("held, magpie its provider: AppOut %v, HoldMatters %v", cx.AppOut(), cx.HoldMatters())
|
||||
}
|
||||
// the hook the provider package asks
|
||||
if provider.CodexHoldMatters == nil {
|
||||
t.Fatal("provider.CodexHoldMatters not set")
|
||||
}
|
||||
for _, v := range []string{"chatgpt", "api"} {
|
||||
if err := cx.Field("login").Set(v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cx.HoldMatters() {
|
||||
t.Fatalf("login %s: HoldMatters", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1054,6 +1054,24 @@ func codexToken(ctx context.Context, dir string) (tok, accountID string, err err
|
||||
return codexTokenRenewing(ctx, dir, "")
|
||||
}
|
||||
|
||||
// codexTokenAsKept is the access token Codex keeps in dir, as it is: an
|
||||
// expired one is an error, not refreshed, for a sign-in magpie leaves to
|
||||
// Codex (leftAlone), which renews it itself.
|
||||
func codexTokenAsKept(dir string) (tok, accountID string, err error) {
|
||||
b, _, err := readCodexLogin(dir, false)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
var a codexAuth
|
||||
if json.Unmarshal(b, &a) != nil || a.Tokens.AccessToken == "" {
|
||||
return "", "", errors.New("Codex is signed out; run codex login")
|
||||
}
|
||||
if exp, _ := jwtClaims(a.Tokens.AccessToken)["exp"].(float64); exp != 0 && !time.Now().Before(time.Unix(int64(exp), 0)) {
|
||||
return "", "", errors.New("Codex's sign-in has expired; Codex renews it as it runs")
|
||||
}
|
||||
return a.Tokens.AccessToken, codexAccountID(a), nil
|
||||
}
|
||||
|
||||
// codexTokenRenewing is codexToken, and with stale, the access token the
|
||||
// backend just answered 401 "token expired" for while its exp was still
|
||||
// ahead (willz on Discord, #2973: a Team account's), refreshes the
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// sherlock_23333 on X: the ChatGPT account Codex is signed in to ran out,
|
||||
// and the Codex app greyed Send for magpie's models too, through restarts.
|
||||
// With Codex switched off or removed in magpie nothing read the account,
|
||||
// so magpie never became Codex's provider while the app held it. Its hold
|
||||
// is read still, while Codex takes magpie's models, and only then: the
|
||||
// account Codex is on alone, its sign-in never renewed by magpie.
|
||||
func TestCodexHoldReadLeftAlone(t *testing.T) {
|
||||
home := signIn(t)
|
||||
rememberLogins(true)
|
||||
body := heldUsage(t)
|
||||
var usage, refresh atomic.Int64
|
||||
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/backend-api/wham/usage":
|
||||
usage.Add(1)
|
||||
json.NewEncoder(w).Encode(body)
|
||||
case "/oauth/token":
|
||||
refresh.Add(1)
|
||||
w.WriteHeader(500)
|
||||
default:
|
||||
w.WriteHeader(404)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(fake.Close)
|
||||
oldBase, oldTok := CodexBase, codexTokenURL
|
||||
CodexBase, codexTokenURL = fake.URL+"/backend-api/codex", fake.URL+"/oauth/token"
|
||||
matters := false
|
||||
oldHook := CodexHoldMatters
|
||||
CodexHoldMatters = func() bool { return matters }
|
||||
t.Cleanup(func() { CodexBase, codexTokenURL, CodexHoldMatters = oldBase, oldTok, oldHook })
|
||||
fresh := func() {
|
||||
loginUsageCache.Lock()
|
||||
loginUsageCache.m, loginUsageCache.pending = nil, nil
|
||||
loginUsageCache.Unlock()
|
||||
}
|
||||
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
p map[string]any
|
||||
}{
|
||||
{"switched off", map[string]any{"id": "codex", "off": true}},
|
||||
{"removed from magpie", map[string]any{"id": "codex", "hidden": true}},
|
||||
} {
|
||||
writeFile(t, Path(), map[string]any{"providers": []map[string]any{c.p}})
|
||||
// not taking magpie's models: left alone, nothing read
|
||||
matters = false
|
||||
fresh()
|
||||
usage.Store(0)
|
||||
if CodexUsedUp(t.Context()) || usage.Load() != 0 {
|
||||
t.Fatalf("%s, Codex not on magpie: read %d times", c.name, usage.Load())
|
||||
}
|
||||
// taking them: the app's hold is read, and known after
|
||||
matters = true
|
||||
fresh()
|
||||
if !CodexUsedUp(t.Context()) {
|
||||
t.Fatalf("%s: CodexUsedUp = false on a held account", c.name)
|
||||
}
|
||||
if usage.Load() != 1 {
|
||||
t.Fatalf("%s: read %d times, want 1", c.name, usage.Load())
|
||||
}
|
||||
if !CodexUsedUpKnown() {
|
||||
t.Fatalf("%s: CodexUsedUpKnown = false after the read", c.name)
|
||||
}
|
||||
// the Usage page still reads nothing of it
|
||||
if u := LoginUsage(t.Context(), "codex"); len(u) != 0 {
|
||||
t.Fatalf("%s: LoginUsage = %v, want nothing", c.name, u)
|
||||
}
|
||||
}
|
||||
|
||||
// a sign-in about to expire is left to Codex: not refreshed, not
|
||||
// written, the hold unknown
|
||||
auth := filepath.Join(home, ".codex", "auth.json")
|
||||
var a map[string]any
|
||||
b, _ := os.ReadFile(auth)
|
||||
json.Unmarshal(b, &a)
|
||||
a["tokens"].(map[string]any)["access_token"] = fakeJWT(map[string]any{"exp": float64(time.Now().Add(-time.Minute).Unix())})
|
||||
writeFile(t, auth, a)
|
||||
before, _ := os.ReadFile(auth)
|
||||
fresh()
|
||||
usage.Store(0)
|
||||
if CodexUsedUp(t.Context()) {
|
||||
t.Fatal("expired sign-in: CodexUsedUp = true")
|
||||
}
|
||||
after, _ := os.ReadFile(auth)
|
||||
if refresh.Load() != 0 || usage.Load() != 0 || !bytes.Equal(before, after) {
|
||||
t.Fatalf("expired sign-in left alone: refreshed %d, read %d, auth.json rewritten %v", refresh.Load(), usage.Load(), !bytes.Equal(before, after))
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,10 @@ package provider
|
||||
// those ran every minute or so for a Claude switched off, and a request
|
||||
// made just as a proxy came back up after a restart went out on the
|
||||
// machine's own address (Riko on Discord).
|
||||
//
|
||||
// One read is kept for Codex: the account it is signed in to, while it
|
||||
// takes magpie's models, for whether the Codex app holds it
|
||||
// (codexHoldReading), which decides where Codex sends at all.
|
||||
|
||||
// leftAlone says whether magpie leaves provider id alone in the
|
||||
// background: switched off, or its account removed from magpie.
|
||||
|
||||
@@ -378,6 +378,10 @@ func loginQuota(ctx context.Context, l Login) SubscriptionQuota {
|
||||
var renew codexRenew
|
||||
switch {
|
||||
case l.Agent == "claude": // Claude Code reads its own (claudeWindows)
|
||||
case l.Active && leftAlone(l.Agent):
|
||||
// read for the Codex app's hold alone (codexHoldReading): the
|
||||
// token as Codex keeps it, never renewed nor written by magpie
|
||||
tok, accountID, err = codexTokenAsKept(codexHome())
|
||||
case l.Active:
|
||||
tok, accountID, err = codexToken(ctx, codexHome())
|
||||
renew = func(ctx context.Context, stale string) (string, string, error) {
|
||||
@@ -445,11 +449,41 @@ func codexSignInRead(l Login, tok string, err error) error {
|
||||
// credits left isn't (the Codex app keeps sending, and the backend
|
||||
// answers). False when that isn't known.
|
||||
func CodexUsedUp(ctx context.Context) bool {
|
||||
if leftAlone("codex") {
|
||||
return codexReadingHeld(codexHoldReading(ctx))
|
||||
}
|
||||
// read as LoginUsage has it, fetched at most once a minute: the agent
|
||||
// package asks on every catalog sync
|
||||
return codexReadingHeld(LoginUsage(ctx, "codex"))
|
||||
}
|
||||
|
||||
// CodexHoldMatters is set by the agent package: whether Codex is wired to
|
||||
// magpie beside its ChatGPT sign-in, so that magpie becomes its provider
|
||||
// while the Codex app holds the account (codex.out), or is its provider
|
||||
// for that already. Nil or false: what the app holds changes nothing.
|
||||
var CodexHoldMatters func() bool
|
||||
|
||||
// codexHoldReading is the reading CodexUsedUp goes by for a Codex switched
|
||||
// off or removed in magpie (leftAlone), which LoginUsage reads nothing
|
||||
// of: the account Codex is signed in to alone, only while Codex takes
|
||||
// magpie's models (CodexHoldMatters), at most once a minute (loginReading),
|
||||
// its sign-in never renewed (loginQuota). The Codex app greys Send for
|
||||
// every model, magpie's too, while it holds the account, and turning
|
||||
// Codex off in magpie once its allowance ran out left the app so for good:
|
||||
// nothing read, magpie never became its provider, and one that was
|
||||
// stopped being it (sherlock_23333 on X).
|
||||
func codexHoldReading(ctx context.Context) map[string]SubscriptionQuota {
|
||||
if settings.AccountDiscoveryOff() || CodexHoldMatters == nil || !CodexHoldMatters() {
|
||||
return nil
|
||||
}
|
||||
for _, l := range Logins("codex") {
|
||||
if l.Active {
|
||||
return map[string]SubscriptionQuota{l.User: loginReading(ctx, l).q}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CodexUsedUpKnown is CodexUsedUp over what was read already, asking no
|
||||
// one and waiting for no one. False when nothing was read yet, which is
|
||||
// CodexUsedUp's own answer for an account it can't tell about.
|
||||
|
||||
Reference in New Issue
Block a user