codex: an account switch restarts nothing while Codex's provider takes no ChatGPT account
danzi002 on Discord: with requires_openai_auth = false on Codex's provider,
switching the Codex account in magpie still restarted the Codex app's
app-server and said the app and daemon were left on the old account.
Codex's provider with no requires_openai_auth = true goes by no ChatGPT
account: codex-rs ModelProviderInfo::account_state answers no account, and
resolve_provider_auth sends the table's key, never auth.json. So which
account auth.json holds is nothing a running Codex uses.
- provider.CodexNoAccount (set to agent.codexNoAccount): the config's top
model_provider is not openai, has a [model_providers] table, and the table
doesn't say requires_openai_auth = true. A profile, the built-in provider,
no table or an unreadable config count as false (may use the account).
- codexAppStep forgets a switch (and a spent switch) while it holds, so the
app-server is left running; a route change still restarts it.
- CodexAppStale / CodexDaemonStale say nothing, and `magpie accounts`'
switch line says nothing needs restarting.
- Kept: route-change restarts (model_provider, openai_base_url,
model_catalog_json, base_url, catalog hash: read only as the app-server
starts) and the daemon's model-list restarts; and switch restarts when
magpie writes requires_openai_auth = true (ChatGPT sign-in with room).
Run (mote-run; full suite at this change on e0ad48641):
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
./..., go build for darwin/linux(nogui)/windows: ok.
- go test -tags nogui -timeout 45m ./...: 81 packages ok, no FAIL.
- New tests: TestCodexNoAccountSwitchRestartsNothing (provider),
TestCodexNoAccountFollowsRequiresOpenAIAuth and
TestCodexNoAccountOfEachConfig (agent).
- Broken by hand: codexNoAccount returning false in provider made
TestCodexNoAccountSwitchRestartsNothing red (ended the app-servers);
agent's codexNoAccount returning false made both agent tests red.
- Real codex-cli 0.165.0 in a sandbox CODEX_HOME with a provider table at
requires_openai_auth = false: app-server account/read and getAuthStatus
answered no account before and after auth.json was switched from a@x to
b@x, and `codex exec` against a loopback capture server sent
`Authorization: Bearer <the table's key>` with no chatgpt-account-id for
both accounts.
After rebasing onto origin/main (2 commits: gateway desktop probe,
usage purposes): go vet ./... and go test of internal/agent, provider,
cli, usage and gateway at the pushed head: ok.
Not tried: the real Codex desktop app (macOS or the Windows MSIX) through
a switch; a daemon with live sessions through a switch; the GUI suite
(no GUI asset changed; the switch notice reads CodexAppStale).
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -52,6 +52,9 @@ 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
|
||||
// but not when Codex's account is switched while its provider takes
|
||||
// no ChatGPT account: the app-server goes by none
|
||||
provider.CodexNoAccount = codexNoAccount
|
||||
// and when none starts in place of the one ended, the app is
|
||||
// reopened on Windows, where its stdout was held open
|
||||
provider.CodexAppOpen = codexAppOpen
|
||||
@@ -106,6 +109,37 @@ func codexAppRoute() (route string, out, known bool) {
|
||||
return b.String(), out, true
|
||||
}
|
||||
|
||||
// codexNoAccount is whether the provider Codex's config.toml names takes
|
||||
// no ChatGPT account: a model_provider whose [model_providers] table
|
||||
// doesn't say requires_openai_auth = true (magpie's with the user's own
|
||||
// false, or magpie as the provider with no sign-in to keep). Codex then
|
||||
// sends the table's own key and its app-server reads no account
|
||||
// (codex-rs ModelProviderInfo::account_state, resolve_provider_auth), so
|
||||
// switching the account in auth.json changes nothing a running Codex
|
||||
// goes by. The built-in openai provider, a provider with no table here, a
|
||||
// profile (which may name another provider) or a config that can't be
|
||||
// read is false: those may go by the account.
|
||||
func codexNoAccount() bool {
|
||||
defer filememo.Hold()()
|
||||
home, _ := homes()
|
||||
a := codex(home)
|
||||
if _, err := os.ReadFile(a.Path); err != nil {
|
||||
return false
|
||||
}
|
||||
if p, _ := edit.GetTOMLTop(a.Path, "profile"); p != "" {
|
||||
return false
|
||||
}
|
||||
id, _ := edit.GetTOMLTop(a.Path, "model_provider")
|
||||
if id == "" || id == "openai" {
|
||||
return false
|
||||
}
|
||||
t, err := edit.GetTOMLTable(a.Path, "model_providers."+id)
|
||||
if err != nil || t == nil {
|
||||
return false
|
||||
}
|
||||
return t["requires_openai_auth"] != "true"
|
||||
}
|
||||
|
||||
// others are clients that reach the gateway without being agents magpie
|
||||
// sets up: known only by their requests, to be drawn with a logo.
|
||||
var others = []*Agent{
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
)
|
||||
|
||||
// codexNoAccount says Codex's provider takes no ChatGPT account, so an
|
||||
// account switch restarts nothing (danzi002 on Discord), only for a
|
||||
// provider table without requires_openai_auth = true: magpie API beside a
|
||||
// ChatGPT sign-in with room has magpie write true, and the Codex app keeps
|
||||
// that account, so a switch there still restarts it. The user's own false
|
||||
// stays through Sync and counts.
|
||||
func TestCodexNoAccountFollowsRequiresOpenAIAuth(t *testing.T) {
|
||||
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "")
|
||||
path := filepath.Join(home, ".codex", "config.toml")
|
||||
if codexNoAccount() {
|
||||
t.Fatalf("Codex on its own ChatGPT sign-in takes no account:\n%s", read())
|
||||
}
|
||||
cx := codex(home)
|
||||
if err := cx.Field("login").Set("api"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := cx.Fields[0].Set("fake/m1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if codexNoAccount() {
|
||||
t.Fatalf("magpie API with requires_openai_auth = true takes no account:\n%s", read())
|
||||
}
|
||||
if err := edit.SetTOMLKey(path, "model_providers."+magpieID, "requires_openai_auth", false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := cx.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !codexNoAccount() {
|
||||
t.Fatalf("the user's requires_openai_auth = false takes an account:\n%s", read())
|
||||
}
|
||||
// a profile may name another provider: not known, so not none
|
||||
if err := edit.SetTOMLTop(path, edit.KV{Path: "profile", Value: "work"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if codexNoAccount() {
|
||||
t.Fatalf("with a profile:\n%s", read())
|
||||
}
|
||||
}
|
||||
|
||||
// The provider's table decides it, whoever's it is; the built-in openai
|
||||
// provider, one with no table and a config that can't be read don't
|
||||
// count.
|
||||
func TestCodexNoAccountOfEachConfig(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
name, config string
|
||||
want bool
|
||||
}{
|
||||
{"none", "", false},
|
||||
{"openai", "model_provider = \"openai\"\n", false},
|
||||
{"no table", "model_provider = \"ollama\"\n", false},
|
||||
{"magpie false", "model_provider = \"magpie\"\n[model_providers.magpie]\nbase_url = \"http://127.0.0.1:3425/v1\"\nrequires_openai_auth = false\n", true},
|
||||
{"magpie unset", "model_provider = \"magpie\"\n[model_providers.magpie]\nbase_url = \"http://127.0.0.1:3425/v1\"\n", true},
|
||||
{"magpie true", "model_provider = \"magpie\"\n[model_providers.magpie]\nbase_url = \"http://127.0.0.1:3425/v1\"\nrequires_openai_auth = true\n", false},
|
||||
{"theirs false", "model_provider = \"relay\"\n[model_providers.relay]\nbase_url = \"https://relay.example/v1\"\nrequires_openai_auth = false\n", true},
|
||||
{"table only", "[model_providers.magpie]\nrequires_openai_auth = false\n", false},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, c.config)
|
||||
if got := codexNoAccount(); got != c.want {
|
||||
t.Fatalf("got %v, want %v", got, c.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
t.Run("unreadable", func(t *testing.T) {
|
||||
home, _ := codexHome(t, "", "model_provider = \"magpie\"\n[model_providers.magpie]\nrequires_openai_auth = false\n")
|
||||
path := filepath.Join(home, ".codex", "config.toml")
|
||||
if err := os.Remove(path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Mkdir(path, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if codexNoAccount() {
|
||||
t.Fatal("a config that can't be read takes no account")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -208,6 +208,12 @@ func accountsCmd(args []string) error {
|
||||
fmt.Println(green.Render("✓"), "magpie now uses", args[3], "for", id)
|
||||
return nil
|
||||
}
|
||||
if id == "codex" && provider.CodexNoAccount != nil && provider.CodexNoAccount() {
|
||||
// Codex's provider takes no ChatGPT account: nothing running
|
||||
// goes by it, and nothing needs a restart
|
||||
fmt.Println(green.Render("✓"), id, "is now signed in as", args[3], muted.Render("· Codex's provider takes no ChatGPT account, so nothing needs restarting"))
|
||||
return nil
|
||||
}
|
||||
fmt.Println(green.Render("✓"), id, "is now signed in as", args[3], muted.Render("· sessions already running keep their account until restarted"))
|
||||
if id == "codex" {
|
||||
if was := provider.CodexDaemonStale(); was != "" {
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/proc"
|
||||
)
|
||||
|
||||
// With Codex on a provider that takes no ChatGPT account (magpie's table
|
||||
// with requires_openai_auth = false, danzi002 on Discord: "requires_openai_auth
|
||||
// = false 的时候就不应该重启 codex 了"), the app-server sends the
|
||||
// provider's own key and reads no account at all, so switching the account
|
||||
// in auth.json changes nothing it goes by: neither a switch nor a switch
|
||||
// off a spent account restarts the Codex app's app-server, and neither the
|
||||
// app nor the background daemon is said to be left on the old account. A
|
||||
// route change still restarts it, and with the provider taking the account
|
||||
// again a switch does too.
|
||||
func TestCodexNoAccountSwitchRestartsNothing(t *testing.T) {
|
||||
signIn(t)
|
||||
t.Setenv("HOME", t.TempDir())
|
||||
t.Setenv("USERPROFILE", t.TempDir())
|
||||
s := stubCodexApp(t)
|
||||
old, oldHome, oldNo := listProcesses, processCodexHome, CodexNoAccount
|
||||
t.Cleanup(func() {
|
||||
listProcesses, processCodexHome, CodexNoAccount = old, oldHome, oldNo
|
||||
DismissCodexDaemon()
|
||||
DismissCodexApp()
|
||||
})
|
||||
processCodexHome = func(int) (string, bool) { return "", false }
|
||||
listProcesses = func(context.Context) ([]proc.Process, error) {
|
||||
ps := []proc.Process{{PID: 9, Args: "codex app-server --managed-daemon"}}
|
||||
for _, p := range s.running {
|
||||
ps = append(ps, proc.Process{PID: p, Args: codexWindowsAppServer})
|
||||
}
|
||||
return ps, nil
|
||||
}
|
||||
noAccount := true
|
||||
CodexNoAccount = func() bool { return noAccount }
|
||||
now := time.Now()
|
||||
idle := func() (int, time.Time) { return 0, now.Add(-time.Minute) }
|
||||
step := func() []int { return codexAppStep(context.Background(), now, idle) }
|
||||
recheck := func() {
|
||||
codexDaemonMu.Lock()
|
||||
codexDaemonChecked = time.Now()
|
||||
codexDaemonMu.Unlock()
|
||||
}
|
||||
|
||||
s.route, s.running = "magpie,noauth", []int{60}
|
||||
if got := step(); got != nil {
|
||||
t.Fatalf("nothing changed: ended %v", got)
|
||||
}
|
||||
noteCodexSwitch("a@x", "b@x")
|
||||
recheck()
|
||||
for range 3 {
|
||||
if got := step(); got != nil {
|
||||
t.Fatalf("switched on a provider that takes no account: ended %v", got)
|
||||
}
|
||||
}
|
||||
if got := CodexAppStale(); got != "" {
|
||||
t.Fatalf("the Codex app said to be left on %q", got)
|
||||
}
|
||||
if got := CodexDaemonStale(); got != "" {
|
||||
t.Fatalf("the daemon said to be left on %q", got)
|
||||
}
|
||||
codexAppSpentSwitch()
|
||||
if got := step(); got != nil {
|
||||
t.Fatalf("switched off a spent account on a provider that takes no account: ended %v", got)
|
||||
}
|
||||
// where Codex sends its requests changed: restarted as ever
|
||||
s.route = "magpie,auth"
|
||||
if got := step(); !slices.Equal(got, []int{60}) {
|
||||
t.Fatalf("route changed: ended %v, want [60]", got)
|
||||
}
|
||||
// on a provider that takes the account a switch restarts it
|
||||
noAccount = false
|
||||
noteCodexSwitch("b@x", "c@x")
|
||||
recheck()
|
||||
if got := CodexAppStale(); got != "b@x" {
|
||||
t.Fatalf("on a provider that takes the account: app left on %q, want b@x", got)
|
||||
}
|
||||
if got := step(); !slices.Equal(got, []int{100}) {
|
||||
t.Fatalf("switched on a provider that takes the account: ended %v, want [100]", got)
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,22 @@ import (
|
||||
// itself (its model pick) is in route, or a pick would restart it.
|
||||
var CodexAppRoute func() (route string, out, known bool)
|
||||
|
||||
// CodexNoAccount is set by the agent package: whether the provider Codex's
|
||||
// config names takes no ChatGPT account at all (a [model_providers] table
|
||||
// without requires_openai_auth = true, such as magpie's with the user's
|
||||
// own requires_openai_auth = false; danzi002 on Discord). Such a provider
|
||||
// sends its own key, and the app-server answers account/read with no
|
||||
// account for it (codex-rs ModelProviderInfo::account_state and
|
||||
// resolve_provider_auth), so which ChatGPT account auth.json holds is
|
||||
// nothing the app-server, the Codex app or the daemon goes by: an account
|
||||
// switch restarts none of them. false when it can't be told.
|
||||
var CodexNoAccount func() bool
|
||||
|
||||
// codexNoAccount is CodexNoAccount, false while it isn't set.
|
||||
func codexNoAccount() bool {
|
||||
return CodexNoAccount != nil && CodexNoAccount()
|
||||
}
|
||||
|
||||
// An app-server ended is not yet one started in its place. The app starts
|
||||
// another only once it has read the end of the old one's stdout and seen
|
||||
// it exit (its StdioConnection closes on both, ChatGPT.exe 26.1021's
|
||||
@@ -325,6 +341,18 @@ func codexAppStep(ctx context.Context, now time.Time, busy func() (int, time.Tim
|
||||
home := codexHome()
|
||||
c := &codexApp
|
||||
c.Lock()
|
||||
switched := c.switched
|
||||
c.Unlock()
|
||||
// an account switch is nothing to an app-server on a provider that
|
||||
// takes no ChatGPT account (CodexNoAccount): it is forgotten, and a
|
||||
// route change later restarts it all the same
|
||||
if switched && codexNoAccount() {
|
||||
log.Printf("codex: Codex's account was switched while its provider takes no ChatGPT account; the Codex app's app-server is left running")
|
||||
c.Lock()
|
||||
c.switched, c.spent = false, false
|
||||
c.Unlock()
|
||||
}
|
||||
c.Lock()
|
||||
// at the first look only a used-up account's app-servers are ended:
|
||||
// what the others read before magpie started isn't known
|
||||
changed := !c.seen && out || c.seen && c.route != route || c.switched
|
||||
|
||||
@@ -203,9 +203,20 @@ func noteLeft(left *leftDaemon, pid int, from, to string) {
|
||||
// it isn't running or is on the account Codex is.
|
||||
func CodexDaemonStale() string {
|
||||
codexDaemonMu.Lock()
|
||||
defer codexDaemonMu.Unlock()
|
||||
recheckLeft()
|
||||
return codexDaemonLeft.user
|
||||
was := codexDaemonLeft.user
|
||||
codexDaemonMu.Unlock()
|
||||
return leftOnAccount(was)
|
||||
}
|
||||
|
||||
// leftOnAccount is was, the account an app-server is left on, or "" when
|
||||
// Codex's provider takes no ChatGPT account (CodexNoAccount): the
|
||||
// app-server goes by none, so nothing is to be restarted for it.
|
||||
func leftOnAccount(was string) string {
|
||||
if was == "" || codexNoAccount() {
|
||||
return ""
|
||||
}
|
||||
return was
|
||||
}
|
||||
|
||||
// CodexAppStale answers the account the Codex desktop app is still signed
|
||||
@@ -215,9 +226,10 @@ func CodexDaemonStale() string {
|
||||
// until it is quit and opened again.
|
||||
func CodexAppStale() string {
|
||||
codexDaemonMu.Lock()
|
||||
defer codexDaemonMu.Unlock()
|
||||
recheckLeft()
|
||||
return codexAppLeft.user
|
||||
was := codexAppLeft.user
|
||||
codexDaemonMu.Unlock()
|
||||
return leftOnAccount(was)
|
||||
}
|
||||
|
||||
// recheckLeft forgets an app-server left on an account before that has
|
||||
|
||||
Reference in New Issue
Block a user