"切换账号 codex 添加的项目丢了 发生过好几次了 另外额度用尽时会强制重启 codex 客户端"
Projects. The Codex app (ChatGPT.app 26.1103 and on) keeps its sidebar's
projects per ChatGPT identity in $CODEX_HOME/.codex-global-state.json,
under electron-account-renderer-state-v1 ({owner, inactive:{<sha256 of
JSON.stringify([chatgpt_user_id, chatgpt_account_id])>: {atoms, globals}}}).
On an account change it parks the old identity's account-scoped globals
and loads the new one's, so magpie's switch (auth.json, then the
app-server's restart) showed an account that never had the app open with
no projects (#5497, #5602, #5702, #5874). Read from the app's own bundle
(early-bootstrap-133 selectRendererOwner, -815's owner hash). The app reads
the file only at launch and writes its memory over it, so magpie writes
it only while the app is closed (codex_projects.go): from the 3s loop once
the file, auth.json or logins.json changed (process listing at most every
30s), and between the quit and the open of every reopen (appQuitDone in
reopenBundle and restartWin). Each identity of auth.json and the saved
Codex logins whose snapshot has no local-projects key gets the project
keys of the identity with the most; an own list (an emptied one too) is
left; g-p- ChatGPT project mirrors and their ties aren't carried, and a
backing naming another account is dropped (the app refuses those). A
file without the key or that doesn't parse is not written; the first
write keeps the file as it was beside it (.magpie-before-projects).
Restart. When quota runs out magpie restarts the app's app-server (the
takeover needs it: the app reads its account only as its app-server
starts, else Send stays grey). A ChatGPT app can then show "hit a snag",
and magpie quit and reopened the whole app (CodexAppRelaunch), and on
Windows reopened an app that started no app-server (codexAppGotNone).
Both now happen only with "Restart the Codex app when magpie switches its
account back" ticked (codexRestartsApp, off by default); otherwise the
Agents row says to quit and reopen, as it did where a relaunch failed.
Run (mote unreachable all session: ssh kex reset on every try; so on the
Linux box and Windows box instead):
- wsl-run: go vet -tags nogui, GOOS=darwin CGO_ENABLED=0 go vet and
GOOS=windows go vet of internal/provider and internal/agent: ok.
- wsl-run go test -tags nogui ./internal/provider ./internal/agent:
provider ok; agent red only on TestGrokInstalled (#3548, WSL binfmt).
- win-run go test -run 'TestCodexProjects|TestCodexRendererOwner|
TestCodexApp|Restart' ./internal/provider ./internal/agent: provider ok;
agent red on TestWorkBuddyRestartOnMac (#4115) and
TestOpenMausBotRestartOnMac (added to #4115), neither touched.
- gofmt -l: clean.
- The owner hash checked against node's crypto (TestCodexRendererOwner).
Broken by hand: carryCodexProjects returning at once -> the four
TestCodexProjects* red; the snag gate removed -> TestCodexAppNotQuitUnasked
red (relaunched 1); the Windows gate removed -> red (reopened 1); the
backing strip removed -> TestCodexProjectsFollowASwitch red.
Not tried: the real Codex app. This Mac's ~/.codex state has no
per-identity key (read-only check), so the switch is reproduced against a
Go port of the app's selectRendererOwner on the issues' layout, not the
app itself. No macOS test run (mote down). The takeover (no account)
still shows no projects in the app, by the app's own rule; they come back
with the account. Projects added while the app runs reach other accounts
once it is closed.
312 lines
10 KiB
Go
312 lines
10 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// crashStub has the Codex app's logs say it showed its error page at at,
|
|
// from the end on, and counts the relaunches.
|
|
type crashStub struct {
|
|
page bool
|
|
at time.Time
|
|
sinces []time.Time
|
|
relaunch int
|
|
err error
|
|
unknown bool
|
|
}
|
|
|
|
func stubCodexCrash(t *testing.T) *crashStub {
|
|
t.Helper()
|
|
c := &crashStub{}
|
|
CodexAppCrashed = func(since time.Time) (time.Time, bool, bool) {
|
|
c.sinces = append(c.sinces, since)
|
|
if c.unknown {
|
|
return time.Time{}, false, false
|
|
}
|
|
if c.page && !c.at.Before(since) {
|
|
return c.at, true, true
|
|
}
|
|
return time.Time{}, false, true
|
|
}
|
|
CodexAppRelaunch = func(context.Context) error {
|
|
c.relaunch++
|
|
return c.err
|
|
}
|
|
return c
|
|
}
|
|
|
|
// ysm119 on Discord: each restart magpie made of the Codex app's
|
|
// app-server left a signed-in ChatGPT app on its "ChatGPT hit a snag"
|
|
// page, which only quitting and opening it again ends (#4550). magpie
|
|
// reads the app's logs after its end and reopens the app once, when no
|
|
// turn has started since; it does nothing when the app logged no page.
|
|
func TestCodexAppReopenedWhenItShowsItsErrorPage(t *testing.T) {
|
|
signIn(t)
|
|
s := stubCodexApp(t)
|
|
c := stubCodexCrash(t)
|
|
now := time.Now()
|
|
idle := func() (int, time.Time) { return 0, now.Add(-time.Minute) }
|
|
step := func() []int { return codexAppStep(context.Background(), now, idle) }
|
|
|
|
s.running = []int{60}
|
|
step()
|
|
if len(c.sinces) > 0 {
|
|
t.Fatalf("the logs read with nothing ended: %v", c.sinces)
|
|
}
|
|
s.on = true
|
|
if got := step(); !slices.Equal(got, []int{60}) {
|
|
t.Fatalf("ended %v, want [60]", got)
|
|
}
|
|
ended := now
|
|
// no page: nothing done, and the watch ends with its window
|
|
now = now.Add(3 * time.Second)
|
|
step()
|
|
if c.relaunch != 0 || len(c.sinces) != 1 || !c.sinces[0].Equal(ended) {
|
|
t.Fatalf("no page: relaunched %d, logs read since %v", c.relaunch, c.sinces)
|
|
}
|
|
now = ended.Add(codexAppCrashWait)
|
|
step()
|
|
n := len(c.sinces)
|
|
now = now.Add(3 * time.Second)
|
|
step()
|
|
if len(c.sinces) != n {
|
|
t.Fatal("the logs still read after the watch's window")
|
|
}
|
|
|
|
// the page, logged two seconds after the end: reopened once
|
|
s.on = false
|
|
ended = now
|
|
if got := step(); !slices.Equal(got, []int{100}) {
|
|
t.Fatalf("ended %v, want [100]", got)
|
|
}
|
|
c.page, c.at = true, ended.Add(2*time.Second)
|
|
now = now.Add(3 * time.Second)
|
|
step()
|
|
if c.relaunch != 1 || CodexAppSnagged() {
|
|
t.Fatalf("the error page: relaunched %d, snag %v", c.relaunch, CodexAppSnagged())
|
|
}
|
|
now = now.Add(3 * time.Second)
|
|
step()
|
|
if c.relaunch != 1 {
|
|
t.Fatalf("relaunched %d times for one page", c.relaunch)
|
|
}
|
|
}
|
|
|
|
// A page logged before magpie's end is none of magpie's: the app showed
|
|
// it for its own reasons, and the user has it in front of them.
|
|
func TestCodexAppErrorPageBeforeTheEndIsLeft(t *testing.T) {
|
|
signIn(t)
|
|
s := stubCodexApp(t)
|
|
c := stubCodexCrash(t)
|
|
now := time.Now()
|
|
idle := func() (int, time.Time) { return 0, now.Add(-time.Minute) }
|
|
s.running, s.on = []int{60}, true
|
|
c.page, c.at = true, now.Add(-time.Minute)
|
|
codexAppStep(context.Background(), now, idle)
|
|
now = now.Add(3 * time.Second)
|
|
codexAppStep(context.Background(), now, idle)
|
|
if c.relaunch != 0 {
|
|
t.Fatalf("relaunched %d for a page before the end", c.relaunch)
|
|
}
|
|
}
|
|
|
|
// The app that came back by itself (its Try again) or is in use is never
|
|
// quit: a Codex turn open since the page, or a request through magpie
|
|
// since or in flight, leaves it running.
|
|
func TestCodexAppInUseAfterItsErrorPageIsLeft(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
turn bool
|
|
busy func(at time.Time) (int, time.Time)
|
|
}{
|
|
{name: "turn open", turn: true},
|
|
{name: "request in flight", busy: func(at time.Time) (int, time.Time) { return 1, at.Add(-time.Minute) }},
|
|
{name: "request since", busy: func(at time.Time) (int, time.Time) { return 0, at.Add(time.Second) }},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
signIn(t)
|
|
s := stubCodexApp(t)
|
|
c := stubCodexCrash(t)
|
|
now := time.Now()
|
|
idle := func() (int, time.Time) { return 0, now.Add(-time.Minute) }
|
|
s.running, s.on = []int{60}, true
|
|
codexAppStep(context.Background(), now, idle)
|
|
c.page, c.at = true, now.Add(time.Second)
|
|
var since []time.Time
|
|
codexTurnRunning = func(_ string, _, from time.Time) bool {
|
|
since = append(since, from)
|
|
return tc.turn
|
|
}
|
|
busy := idle
|
|
if tc.busy != nil {
|
|
busy = func() (int, time.Time) { return tc.busy(c.at) }
|
|
}
|
|
now = now.Add(3 * time.Second)
|
|
codexAppStep(context.Background(), now, busy)
|
|
if c.relaunch != 0 {
|
|
t.Fatalf("relaunched an app in use")
|
|
}
|
|
if tc.turn && (len(since) == 0 || !since[0].Equal(c.at)) {
|
|
t.Fatalf("turns read since %v, want the page's time %v", since, c.at)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Where the app can't be reopened (Linux, a quit it didn't take) the
|
|
// Agents row says to quit it and open it again, until the app-servers
|
|
// that ran at the page are gone or the app is closed.
|
|
func TestCodexAppSnagSaidTillTheAppRestarts(t *testing.T) {
|
|
signIn(t)
|
|
s := stubCodexApp(t)
|
|
c := stubCodexCrash(t)
|
|
c.err = errors.New("still open")
|
|
now := time.Now()
|
|
idle := func() (int, time.Time) { return 0, now.Add(-time.Minute) }
|
|
step := func() { codexAppStep(context.Background(), now, idle) }
|
|
s.running, s.on = []int{60}, true
|
|
step()
|
|
c.page, c.at = true, now.Add(time.Second)
|
|
now = now.Add(3 * time.Second)
|
|
step()
|
|
if c.relaunch != 1 || !CodexAppSnagged() {
|
|
t.Fatalf("relaunched %d, snag %v", c.relaunch, CodexAppSnagged())
|
|
}
|
|
// a listing that fails is unknown: still said
|
|
s.listErr = errors.New("ps")
|
|
now = now.Add(3 * time.Second)
|
|
step()
|
|
if !CodexAppSnagged() {
|
|
t.Fatal("a failed listing cleared the snag")
|
|
}
|
|
s.listErr = nil
|
|
step()
|
|
if !CodexAppSnagged() || c.relaunch != 1 {
|
|
t.Fatalf("the same app-server running: snag %v, relaunched %d", CodexAppSnagged(), c.relaunch)
|
|
}
|
|
s.running = []int{200} // quit and opened by the user
|
|
now = now.Add(3 * time.Second)
|
|
step()
|
|
if CodexAppSnagged() {
|
|
t.Fatal("still said after the app started again")
|
|
}
|
|
|
|
// closed: no longer said
|
|
codexApp.Lock()
|
|
codexApp.snag, codexApp.snagPids = true, []int{200}
|
|
codexApp.Unlock()
|
|
CodexAppOpen = func(context.Context) (bool, bool) { return false, true }
|
|
step()
|
|
if CodexAppSnagged() {
|
|
t.Fatal("still said with the app closed")
|
|
}
|
|
}
|
|
|
|
// The Agents row's Restart and the switch notice's Restart end the
|
|
// app-server themselves: the logs are read after them too.
|
|
func TestCodexAppRestartsWatchForTheErrorPage(t *testing.T) {
|
|
signIn(t)
|
|
s := stubCodexApp(t)
|
|
c := stubCodexCrash(t)
|
|
oldWait, oldGone := codexAppBackWait, codexAppGoneWait
|
|
t.Cleanup(func() { codexAppBackWait, codexAppGoneWait = oldWait, oldGone })
|
|
codexAppBackWait, codexAppGoneWait = time.Second, time.Second
|
|
s.running = []int{60}
|
|
before := time.Now()
|
|
if err := RestartSwitchedCodexApp(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.page, c.at = true, time.Now()
|
|
now := time.Now().Add(3 * time.Second)
|
|
codexAppStep(context.Background(), now, func() (int, time.Time) { return 0, before.Add(-time.Minute) })
|
|
if c.relaunch != 1 {
|
|
t.Fatalf("the switch notice's Restart: relaunched %d", c.relaunch)
|
|
}
|
|
if len(c.sinces) == 0 || c.sinces[0].Before(before) {
|
|
t.Fatalf("logs read since %v, want from the end on", c.sinces)
|
|
}
|
|
}
|
|
|
|
// A watch whose logs can't be read is kept to its window and never
|
|
// reopens the app.
|
|
func TestCodexAppUnreadLogsReopenNothing(t *testing.T) {
|
|
signIn(t)
|
|
s := stubCodexApp(t)
|
|
c := stubCodexCrash(t)
|
|
c.unknown = true
|
|
now := time.Now()
|
|
idle := func() (int, time.Time) { return 0, now.Add(-time.Minute) }
|
|
s.running, s.on = []int{60}, true
|
|
codexAppStep(context.Background(), now, idle)
|
|
for range 3 {
|
|
now = now.Add(codexAppCrashWait)
|
|
codexAppStep(context.Background(), now, idle)
|
|
}
|
|
if c.relaunch != 0 || CodexAppSnagged() {
|
|
t.Fatalf("unread logs: relaunched %d, snag %v", c.relaunch, CodexAppSnagged())
|
|
}
|
|
}
|
|
|
|
// ysm119's case: the Agents row's Restart (RestartCodexApp) ends the app's
|
|
// own app-server, and the page the app shows after it is read and the app
|
|
// reopened.
|
|
func TestCodexAppRowRestartWatchesForTheErrorPage(t *testing.T) {
|
|
signIn(t)
|
|
stubCodexApp(t)
|
|
c := stubCodexCrash(t)
|
|
const bundle = "/Applications/ChatGPT.app"
|
|
ps := []proc.Process{{PID: 2, Args: bundle + "/Contents/Resources/codex app-server --analytics-default-enabled"}}
|
|
oldList, oldEnd, oldPoll := listProcesses, endCodexAppServer, codexAppPoll
|
|
t.Cleanup(func() { listProcesses, endCodexAppServer, codexAppPoll = oldList, oldEnd, oldPoll })
|
|
codexAppPoll = time.Millisecond
|
|
listProcesses = func(context.Context) ([]proc.Process, error) { return slices.Clone(ps), nil }
|
|
endCodexAppServer = func(pid int) error { ps[0].PID = 102; return nil }
|
|
before := time.Now()
|
|
if err := RestartCodexApp(context.Background(), bundle); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.page, c.at = true, time.Now()
|
|
codexAppStep(context.Background(), time.Now().Add(3*time.Second), func() (int, time.Time) { return 0, before.Add(-time.Minute) })
|
|
if c.relaunch != 1 {
|
|
t.Fatalf("the Agents row's Restart: relaunched %d", c.relaunch)
|
|
}
|
|
}
|
|
|
|
// Michael25651150 on X: "额度用尽时会强制重启 codex 客户端". With the
|
|
// user's "Restart the Codex app" off (as it ships), quota running out
|
|
// still restarts the app's app-server, which the takeover needs, but the
|
|
// app showing its error page after it is never quit and opened again
|
|
// unasked: the Agents row says to, and the app's Try again or the row's
|
|
// Restart is the user's. Nor is a Windows app that started no app-server.
|
|
func TestCodexAppNotQuitUnasked(t *testing.T) {
|
|
signIn(t)
|
|
s := stubCodexApp(t)
|
|
c := stubCodexCrash(t)
|
|
codexRestartsApp = func() bool { return false }
|
|
now := time.Now()
|
|
idle := func() (int, time.Time) { return 0, now.Add(-time.Minute) }
|
|
s.running = []int{60}
|
|
codexAppStep(context.Background(), now, idle)
|
|
s.on = true // the account used up: magpie serves it
|
|
if got := codexAppStep(context.Background(), now, idle); !slices.Equal(got, []int{60}) {
|
|
t.Fatalf("ended %v, want [60]: the takeover needs the app-server's restart", got)
|
|
}
|
|
c.page, c.at = true, now.Add(2*time.Second)
|
|
now = now.Add(3 * time.Second)
|
|
codexAppStep(context.Background(), now, idle)
|
|
if c.relaunch != 0 || !CodexAppSnagged() {
|
|
t.Fatalf("relaunched %d, snag %v: want left to the user, said on the row", c.relaunch, CodexAppSnagged())
|
|
}
|
|
reopened := 0
|
|
CodexAppReopen = func(context.Context) error { reopened++; return nil }
|
|
if stuck := codexAppGotNone(context.Background()); !stuck || reopened != 0 {
|
|
t.Fatalf("no app-server: reopened %d, stuck %v", reopened, stuck)
|
|
}
|
|
}
|