codex: a Windows Codex app that starts no app-server after a switch is reopened (蛋仔 on Discord)
After magpie switched Codex's account, the Windows Codex app answered every send with "Codex app-server process is not available" until it was quit and opened again. The report said the app doesn't reconnect after a clean exit. That is half right. The app (MSIX OpenAI.Codex 26.1021, an Electron ChatGPT.exe that spawns codex.exe app-server over stdio) does start another, but only once the old one's stdout reaches EOF and its exit has fired. On Windows a child of the app-server (an MCP server, a tool command) inherits that pipe handle. A child left running keeps the pipe open, so the app never sees the close, starts none, and its send() throws. Reproduced on the Windows box with the real codex.exe 0.164 app-server plus an MCP server: stdout ended only when the MCP server did, and at once when the tree was ended. magpie took "the old pid is gone" as done in each place it restarts the app-server. None of them checked that a new one came: - codexAppStep (the switch watcher) now records the pids it ended and waits codexAppBackWait (30s) for another app-server. If none comes and the app still runs (CodexAppOpen), it quits and opens the app as the Agents row's Restart does (CodexAppReopen: restartWin by the OpenAI.Codex_2p2nqsd0c76g0!App AUMID, Windows only). Where it can't, the row says to (CodexAppStuck). - RestartCodexApp (macOS Restart), RestartCodexAppWhenIdle (uninstall) and the switch notice's Restart (RestartSwitchedCodexApp,46aa7e6b8) wait for a new app-server too, and reopen or say so the same way. RestartSwitchedCodexApp's own 20s codexAppBackWait went into the shared 30s one. - recheckLeft no longer clears the "still on the old account" record while the app is open with no app-server. - A turn stuck open when quota ran out mid-task (蛋仔's follow-up) held the restart for up to codexTurnWindow (30m), since an open turn in Codex's sessions meant "a task runs". A switch off a used-up account (SwitchWhenSpent) now marks it spent (codexAppSpentSwitch). After such a switch, a turn that has written no session file and sent nothing through magpie for codexTurnStuckAfter (2m) counts as stuck, and the app-server is restarted past it. Its next request would be refused on that account anyway. A tool still running in that turn is ended with it. A switch for any other reason waits for the turn as before. macOS is never reopened unasked (a reopen opens a new window, yoshi037070, 10-27). WSL needs nothing: WSL Codex has no desktop app, and the Windows app's app-server runs on the Windows side. Run (at this head unless said): - mote: go test -timeout 45m ./... at 4cf444bc4, this change on origin/main807048470. It was rebased ondca3e2d71after (5 commits, none in codex_app*/codex_switch/codex_daemon), and the -run below was run again at the pushed head. Result: every package ok except gateway and gui, whose reds aren't this change's (it touches neither package). gateway: TestAntigravityTurn* x4 and TestCodexSpentGrokFailingIsTold, red the same on origin/main46aa7e6b8(#4362), and TestUnroutedUnknownHoldAnsweredSooner, which passes alone 3 of 3 at this head (filed #4377). gui: TestMainWindowCorners (#3949). - mote: provider and agent -run 'CodexApp|CodexDaemon|RestartCodex| RestartSwitched|CodexSwitched|CodexSessionsQuiet|Switch' ok; gofmt -l clean; GOOS=windows, linux, darwin go vet ./internal/agent ./internal/provider ok. - win-run, the same -run: everything of mine passes. Two tests are red, TestCodexDaemonEnvIsTheTerminals and TestWorkBuddyRestartOnMac, the same on origin/main (#4115). - Broken by hand, each red, then restored: the step's no-app-server branch, RestartCodexApp's not-back check, uninstall's reopen, recheckLeft's open check (4 provider tests); RestartSwitchedCodexApp without the reopen (TestRestartSwitchedCodexApp "reopened 0"); the codexWinApp matcher widened to \WindowsApps\OpenAI. (TestCodexAppReopenedOnWindows). The stuck-turn bypass taken out ("a turn stuck on the used-up account: ended []"), the spent mark not set (TestCodexSwitchedWhenUsedUp), and the bypass without its spent and quiet checks ("a turn open after a switch not for spend: ended [60]"). - Real Codex app on Windows (xiaomi-gpu, OpenAI.Codex 26.1021.3705.0, from a throwaway test, not committed, run as a scheduled task in the console session): - RestartCodexAppWhenIdle ended the real app-server, and the app started another within ~0.6s. codexAppBack saw it (left false). - codexAppReopen quit ChatGPT.exe 43832 and opened the app by its AUMID in 3.7s (main now 70060). A new app-server came 9.6s after. Not tried: - The stuck state itself in the real app: an MCP child really holding the app-server's stdout inside the Codex app. It was reproduced with codex.exe outside the app, and the reopen was run on a working app. - A signed-in real account switch on Windows. - The quota-mid-task hang with a real used-up account. Why the app sat on the turn wasn't reproduced. A plain usage_limit_reached refusal closes the turn (task_complete, asdd9828f2bfound), so this covers a turn that stays open, whatever holds it. - Whether a reopen loses an unsent draft. - The macOS path with a real stuck app (it says to quit and reopen; nothing is reopened there).
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -52,6 +52,10 @@ 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 when none starts in place of the one ended, the app is
|
||||
// reopened on Windows, where its stdout was held open
|
||||
provider.CodexAppOpen = codexAppOpen
|
||||
provider.CodexAppReopen = codexAppReopen
|
||||
// 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 {
|
||||
|
||||
@@ -99,3 +99,54 @@ func RestartSwitchedCodexApp(ctx context.Context) error {
|
||||
}
|
||||
return errors.New(noticeSwitchAppUnknown.say("agent", "Codex"))
|
||||
}
|
||||
|
||||
// codexWinApp is the Windows Codex app: the OpenAI.Codex Store package's
|
||||
// ChatGPT.exe (26.1021 on the Windows box, under C:\Program Files\
|
||||
// WindowsApps\OpenAI.Codex_…\app\), never the ChatGPT chat app's.
|
||||
var codexWinApp = restartable{winExe: "ChatGPT.exe", win: func(image string) bool {
|
||||
return winBase(image, "ChatGPT.exe") && winHas(image, `\WindowsApps\OpenAI.Codex_`)
|
||||
}}
|
||||
|
||||
// codexWinAUMID opens the Windows Codex app when its main process's
|
||||
// AppUserModelID can't be read: its package family and its one app.
|
||||
const codexWinAUMID = "OpenAI.Codex_2p2nqsd0c76g0!App"
|
||||
|
||||
// codexAppOpen is provider.CodexAppOpen: whether the Codex desktop app
|
||||
// runs here, ChatGPT.app (com.openai.codex) on macOS, the Store app on
|
||||
// Windows; not known elsewhere.
|
||||
func codexAppOpen(context.Context) (open, known bool) {
|
||||
switch {
|
||||
case restartOnWindows:
|
||||
_, ok := findRestartable(codexWinApp)
|
||||
return ok, true
|
||||
case restartOnMac:
|
||||
_, ok := findRestartable(restartable{mac: desktopApps["codex"]})
|
||||
return ok, true
|
||||
}
|
||||
return false, false
|
||||
}
|
||||
|
||||
// codexAppReopen is provider.CodexAppReopen: on Windows it quits the
|
||||
// Codex app and opens it again, as the Agents row's Restart does for
|
||||
// Claude Desktop (restartWin), when the app started no app-server in
|
||||
// place of the one magpie ended and so can't send. Its threads are kept
|
||||
// by Codex; a draft not sent is lost, but nothing could be sent from it.
|
||||
// macOS isn't reopened unasked: there the Agents row's Restart and the
|
||||
// line magpie shows are the user's.
|
||||
func codexAppReopen(ctx context.Context) error {
|
||||
if !restartOnWindows {
|
||||
return errors.New("magpie reopens only the Windows Codex app")
|
||||
}
|
||||
c, ok := findRestartable(codexWinApp)
|
||||
if !ok {
|
||||
return nil // quit meanwhile: it starts an app-server as it opens
|
||||
}
|
||||
if c.main.aumid == "" {
|
||||
c.main.aumid = codexWinAUMID
|
||||
}
|
||||
a, err := Find("codex")
|
||||
if err != nil {
|
||||
a = &Agent{ID: "codex", Name: "Codex"}
|
||||
}
|
||||
return a.restartWin(ctx, codexWinApp, c)
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@ package agent
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -96,3 +98,60 @@ func TestRestartSwitchedCodexAppSays(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// When the Windows Codex app starts no app-server in place of the one
|
||||
// magpie ended (its stdout held open by a process that one started), it
|
||||
// can't send till it is quit and opened again (蛋仔 on Discord, 10-30):
|
||||
// magpie does that, to the OpenAI.Codex package's ChatGPT.exe only, by
|
||||
// its AppUserModelID. macOS isn't reopened unasked.
|
||||
func TestCodexAppReopenedOnWindows(t *testing.T) {
|
||||
restartSandbox(t)
|
||||
restartOnWindows = true
|
||||
appCloseWait, appBackWait, appTrayGrace = time.Second, time.Second, 10*time.Millisecond
|
||||
const image = `C:\Program Files\WindowsApps\OpenAI.Codex_26.1021.3705.0_x64__2p2nqsd0c76g0\app\ChatGPT.exe`
|
||||
start := time.Now().Add(-time.Hour)
|
||||
alive := true
|
||||
winProcs = func(name string) []winProc {
|
||||
if !strings.EqualFold(name, "ChatGPT.exe") {
|
||||
return nil
|
||||
}
|
||||
// the ChatGPT chat app beside it, always running
|
||||
ps := []winProc{{pid: 50, parent: 4, image: `C:\Program Files\WindowsApps\OpenAI.ChatGPT-Desktop_1.2025.1.0_x64__2p2nqsd0c76g0\app\ChatGPT.exe`, start: start}}
|
||||
if alive {
|
||||
ps = append(ps,
|
||||
winProc{pid: 10, parent: 4, image: image, start: start, aumid: "OpenAI.Codex_2p2nqsd0c76g0!App"},
|
||||
winProc{pid: 11, parent: 10, image: image, start: start.Add(time.Second)})
|
||||
}
|
||||
return ps
|
||||
}
|
||||
winShown = func([]int) bool { return false } // in the tray
|
||||
var did []string
|
||||
winClose = func(pids []int) error { did = append(did, fmt.Sprint("close ", pids)); return nil }
|
||||
winEnd = func(pid int) error { did = append(did, fmt.Sprint("end ", pid)); alive = false; return nil }
|
||||
winLaunch = func(p winProc) error { did = append(did, "open "+p.aumid); alive = true; return nil }
|
||||
|
||||
if open, known := codexAppOpen(context.Background()); !open || !known {
|
||||
t.Fatalf("open %v, known %v", open, known)
|
||||
}
|
||||
if err := codexAppReopen(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := []string{"close [10 11]", "end 10", "open OpenAI.Codex_2p2nqsd0c76g0!App"}; !slices.Equal(did, want) {
|
||||
t.Fatalf("did %q, want %q", did, want)
|
||||
}
|
||||
|
||||
// quit meanwhile: nothing to do, it starts one as it opens
|
||||
alive, did = false, nil
|
||||
if open, known := codexAppOpen(context.Background()); open || !known {
|
||||
t.Fatalf("quit: open %v, known %v", open, known)
|
||||
}
|
||||
if err := codexAppReopen(context.Background()); err != nil || did != nil {
|
||||
t.Fatalf("quit: %v, did %q", err, did)
|
||||
}
|
||||
|
||||
restartOnWindows = false
|
||||
alive = true
|
||||
if err := codexAppReopen(context.Background()); err == nil || did != nil {
|
||||
t.Fatalf("not Windows: %v, did %q", err, did)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,62 @@ import (
|
||||
// itself (its model pick) is in route, or a pick would restart it.
|
||||
var CodexAppRoute func() (route string, out, known bool)
|
||||
|
||||
// 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
|
||||
// app.asar). On Windows every process the app-server started inherits
|
||||
// that stdout (Rust's Command creates children with bInheritHandles), so
|
||||
// an MCP server or tool command left running after it holds the pipe
|
||||
// open: the app never sees it close, starts no other, and every send says
|
||||
// "Codex app-server process is not available" until Codex is quit and
|
||||
// opened again (蛋仔 on Discord, 10-30; reproduced on the Windows box
|
||||
// with codex 0.164's app-server and an MCP server that outlived it: its
|
||||
// stdout ended only when the MCP server did). So once the ones ended are
|
||||
// gone, magpie waits codexAppBackWait for a new one; when none comes
|
||||
// while the app still runs, it has the agent package reopen the app
|
||||
// (CodexAppReopen), and says it is stuck when that can't be done.
|
||||
//
|
||||
// CodexAppOpen, set by the agent package, says whether the Codex desktop
|
||||
// app runs here; known false when that can't be told. CodexAppReopen
|
||||
// quits it and opens it again, erring where magpie doesn't (macOS, where
|
||||
// a reopen opens a new window: the user does it).
|
||||
var (
|
||||
CodexAppOpen func(ctx context.Context) (open, known bool)
|
||||
CodexAppReopen func(ctx context.Context) error
|
||||
// codexAppBackWait is how long after the ended app-servers are gone a
|
||||
// new one has to start: the app starts one within ~1.5s of the old
|
||||
// one's end, hidden in the tray too (its own logs on the Windows box,
|
||||
// 10-27 to 10-29)
|
||||
codexAppBackWait = 30 * time.Second
|
||||
)
|
||||
|
||||
// codexAppGotNone is what codexAppStep does when no app-server started in
|
||||
// place of the ones it ended: nothing when the app was quit meanwhile (it
|
||||
// starts one as it opens), else reopen it. stuck says it runs with none
|
||||
// and wasn't reopened: the user quits it and opens it again.
|
||||
func codexAppGotNone(ctx context.Context) (stuck bool) {
|
||||
if open, known := codexAppOpenNow(ctx); known && !open {
|
||||
return false
|
||||
}
|
||||
if CodexAppReopen == nil {
|
||||
return true
|
||||
}
|
||||
if err := CodexAppReopen(ctx); err != nil {
|
||||
log.Printf("codex: the Codex app started no app-server in place of the one magpie ended; reopening it: %v", err)
|
||||
return true
|
||||
}
|
||||
log.Printf("codex: the Codex app started no app-server in place of the one magpie ended; reopened it")
|
||||
return false
|
||||
}
|
||||
|
||||
// codexAppOpenNow is CodexAppOpen, unknown while it isn't set.
|
||||
func codexAppOpenNow(ctx context.Context) (open, known bool) {
|
||||
if CodexAppOpen == nil {
|
||||
return false, false
|
||||
}
|
||||
return CodexAppOpen(ctx)
|
||||
}
|
||||
|
||||
// codexAppEvery is how often KeepCodexAppOnItsProvider looks; codexAppIdle
|
||||
// how long after Codex's last request through magpie a turn counts ended
|
||||
// (a tool runs between two of a turn's requests); codexAppTries how often
|
||||
@@ -66,6 +122,16 @@ var (
|
||||
// whose app-server died leaves one open for good.
|
||||
const codexTurnWindow = 30 * time.Minute
|
||||
|
||||
// codexTurnStuckAfter is how long a turn left open on an app-server whose
|
||||
// account was used up has to have written nothing to Codex's sessions,
|
||||
// with no Codex request through magpie either, before it counts stuck and
|
||||
// the app-server is restarted past it. Such a turn can't go on: its next
|
||||
// request is refused on that account, and when quota ran out mid-turn
|
||||
// the app sat on it and greyed every thread until restarted (蛋仔 on
|
||||
// Discord). A tool still running in it loses only a reply that account
|
||||
// would refuse.
|
||||
var codexTurnStuckAfter = 2 * time.Minute
|
||||
|
||||
// codexTurnSlack is how much earlier than the app-servers' start a turn's
|
||||
// timestamp may be and still count as theirs: the two clocks are read by
|
||||
// different programs.
|
||||
@@ -92,9 +158,10 @@ var (
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
endCodexAppServer = proc.End
|
||||
codexTurnRunning = func(home string, now, since time.Time) bool { return codexTurnOpen(home, now, since) }
|
||||
codexServerStart = proc.Started
|
||||
endCodexAppServer = proc.End
|
||||
codexTurnRunning = func(home string, now, since time.Time) bool { return codexTurnOpen(home, now, since) }
|
||||
codexSessionsQuiet = func(home string, now time.Time, d time.Duration) bool { return codexSessionsQuietFor(home, now, d) }
|
||||
codexServerStart = proc.Started
|
||||
)
|
||||
|
||||
// codexServersSince is when the oldest of the app-servers pids started: a
|
||||
@@ -173,6 +240,13 @@ var codexApp struct {
|
||||
// switched: Codex's ChatGPT account changed since the app-servers were
|
||||
// last listed (codexAppAccountSwitched)
|
||||
switched bool
|
||||
// spent: the switch was off an account used up (SwitchWhenSpent), so a
|
||||
// turn the app-servers left open can't be served by them any more
|
||||
spent bool
|
||||
// ended: the app-servers ended and no new one seen since, and by when
|
||||
// one has to start (codexAppBackWait after the last end)
|
||||
ended map[int]bool
|
||||
backBy time.Time
|
||||
}
|
||||
|
||||
// CodexAppStuck is whether a Codex app is left on the provider it started
|
||||
@@ -208,6 +282,16 @@ func codexAppAccountSwitched() {
|
||||
c.switched = true
|
||||
}
|
||||
|
||||
// codexAppSpentSwitch is codexAppAccountSwitched for a switch off an
|
||||
// account that was used up: a turn the app-server left open is restarted
|
||||
// past too once it has gone quiet (codexTurnStuckAfter).
|
||||
func codexAppSpentSwitch() {
|
||||
c := &codexApp
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
c.switched, c.spent = true, true
|
||||
}
|
||||
|
||||
// KeepCodexAppOnItsProvider runs codexAppStep every codexAppEvery till
|
||||
// ctx ends. busy says how many of Codex's requests are in flight through
|
||||
// magpie and when the last ended.
|
||||
@@ -266,7 +350,7 @@ func codexAppStep(ctx context.Context, now time.Time, busy func() (int, time.Tim
|
||||
c.Unlock()
|
||||
}
|
||||
c.Lock()
|
||||
n := len(c.stale)
|
||||
n := len(c.stale) + len(c.ended)
|
||||
c.Unlock()
|
||||
if n == 0 {
|
||||
return nil
|
||||
@@ -284,15 +368,49 @@ func codexAppStep(ctx context.Context, now time.Time, busy func() (int, time.Tim
|
||||
c.Lock()
|
||||
for p := range c.stale {
|
||||
if !running[p] {
|
||||
delete(c.stale, p) // it read the provider afresh as it started
|
||||
// gone: the one the app starts in its place reads the
|
||||
// provider afresh, once it starts (c.ended)
|
||||
delete(c.stale, p)
|
||||
}
|
||||
}
|
||||
none := false
|
||||
if len(c.ended) > 0 {
|
||||
// any other app-server running serves the app; one ended and
|
||||
// still running is waited for, and the wait counts from its end
|
||||
back, left := false, false
|
||||
for _, p := range pids {
|
||||
back = back || !c.ended[p]
|
||||
left = left || c.ended[p]
|
||||
}
|
||||
switch {
|
||||
case back:
|
||||
c.ended = nil
|
||||
case left:
|
||||
c.backBy = now.Add(codexAppBackWait)
|
||||
case !now.Before(c.backBy):
|
||||
c.ended, none = nil, true
|
||||
}
|
||||
}
|
||||
n = len(c.stale)
|
||||
c.Unlock()
|
||||
if n == 0 {
|
||||
if none {
|
||||
if codexAppGotNone(ctx) {
|
||||
c.Lock()
|
||||
c.stuck = true
|
||||
c.Unlock()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if inFlight, last := busy(); inFlight > 0 || now.Sub(last) < codexAppIdle {
|
||||
if n == 0 {
|
||||
c.Lock()
|
||||
if len(c.ended) == 0 {
|
||||
c.spent = false
|
||||
}
|
||||
c.Unlock()
|
||||
return nil
|
||||
}
|
||||
inFlight, last := busy()
|
||||
if inFlight > 0 || now.Sub(last) < codexAppIdle {
|
||||
return nil
|
||||
}
|
||||
c.Lock()
|
||||
@@ -300,9 +418,13 @@ func codexAppStep(ctx context.Context, now time.Time, busy func() (int, time.Tim
|
||||
for p := range c.stale {
|
||||
stale = append(stale, p)
|
||||
}
|
||||
spent := c.spent
|
||||
c.Unlock()
|
||||
if codexTurnRunning(home, now, codexServersSince(stale)) {
|
||||
return nil
|
||||
if !spent || now.Sub(last) < codexTurnStuckAfter || !codexSessionsQuiet(home, now, codexTurnStuckAfter) {
|
||||
return nil
|
||||
}
|
||||
log.Printf("codex: a Codex turn left open on the used-up account has done nothing for %s; restarting the Codex app's app-server past it", codexTurnStuckAfter)
|
||||
}
|
||||
var ended []int
|
||||
c.Lock()
|
||||
@@ -321,6 +443,10 @@ func codexAppStep(ctx context.Context, now time.Time, busy func() (int, time.Tim
|
||||
continue
|
||||
}
|
||||
ended = append(ended, p)
|
||||
if c.ended == nil {
|
||||
c.ended = map[int]bool{}
|
||||
}
|
||||
c.ended[p], c.backBy = true, now.Add(codexAppBackWait)
|
||||
}
|
||||
return ended
|
||||
}
|
||||
@@ -358,6 +484,34 @@ func codexTurnOpen(home string, now, since time.Time) bool {
|
||||
return open
|
||||
}
|
||||
|
||||
// codexSessionsQuietFor is whether no Codex session file under home was
|
||||
// written in the last d. A sessions folder that can't be read counts as
|
||||
// written: a turn cut short costs more than a restart a little later.
|
||||
func codexSessionsQuietFor(home string, now time.Time, d time.Duration) bool {
|
||||
quiet := true
|
||||
err := filepath.WalkDir(filepath.Join(home, "sessions"), func(path string, e fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if e.IsDir() || !strings.HasSuffix(e.Name(), ".jsonl") {
|
||||
return nil
|
||||
}
|
||||
info, err := e.Info()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if now.Sub(info.ModTime()) < d {
|
||||
quiet = false
|
||||
return fs.SkipAll
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return quiet && err == nil
|
||||
}
|
||||
|
||||
// sessionTurnTail is how much of a session file's end sessionTurnOpen reads.
|
||||
const sessionTurnTail = 512 << 10
|
||||
|
||||
@@ -419,7 +573,8 @@ var codexAppGoneWait = 10 * time.Second
|
||||
// stay as they are. The Agents row's Restart quit the app and opened it
|
||||
// again, which opened a new window (yoshi037070 on Discord, 10-27). An
|
||||
// app-server another program runs from the bundle (--listen) is left
|
||||
// alone. It waits until the ones ended are gone, and errs when none ran.
|
||||
// alone. It waits until the ones ended are gone and the app has started
|
||||
// another (codexAppBackWait), and errs when none ran or none started.
|
||||
func RestartCodexApp(ctx context.Context, bundle string) error {
|
||||
ps, err := listProcesses(ctx)
|
||||
if err != nil {
|
||||
@@ -439,19 +594,54 @@ func RestartCodexApp(ctx context.Context, bundle string) error {
|
||||
if len(ended) == 0 {
|
||||
return errors.New("the Codex app's app-server isn't running")
|
||||
}
|
||||
gone, back, err := codexAppBack(ctx, func(ctx context.Context) ([]int, error) {
|
||||
ps, err := listProcesses(ctx)
|
||||
var pids []int
|
||||
for _, p := range ps {
|
||||
if strings.HasPrefix(strings.TrimSpace(p.Args), in) && isCodexAppOwnServer(p.Args) {
|
||||
pids = append(pids, p.PID)
|
||||
}
|
||||
}
|
||||
return pids, err
|
||||
}, ended)
|
||||
switch {
|
||||
case err != nil:
|
||||
return err
|
||||
case !gone:
|
||||
return errors.New("the Codex app's app-server didn't end")
|
||||
case !back:
|
||||
return errors.New("the Codex app started no new app-server: quit it and open it again")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// codexAppPoll is how often codexAppBack lists the app-servers.
|
||||
var codexAppPoll = 200 * time.Millisecond
|
||||
|
||||
// codexAppBack waits for the app-servers ended to be gone, for
|
||||
// codexAppGoneWait, and then for another of list's to run, for
|
||||
// codexAppBackWait: gone and back say which came. A listing that fails
|
||||
// is tried again.
|
||||
func codexAppBack(ctx context.Context, list func(context.Context) ([]int, error), ended []int) (gone, back bool, err error) {
|
||||
end := time.Now().Add(codexAppGoneWait)
|
||||
for {
|
||||
ps, err := listProcesses(ctx)
|
||||
if err == nil && !slices.ContainsFunc(ps, func(p proc.Process) bool { return slices.Contains(ended, p.PID) }) {
|
||||
return nil
|
||||
pids, err := list(ctx)
|
||||
if err == nil {
|
||||
left := slices.ContainsFunc(pids, func(p int) bool { return slices.Contains(ended, p) })
|
||||
if !gone && !left {
|
||||
gone, end = true, time.Now().Add(codexAppBackWait)
|
||||
}
|
||||
if slices.ContainsFunc(pids, func(p int) bool { return !slices.Contains(ended, p) }) && !left {
|
||||
return true, true, nil
|
||||
}
|
||||
}
|
||||
if time.Now().After(end) {
|
||||
return errors.New("the Codex app's app-server didn't end")
|
||||
return gone, false, nil
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-time.After(200 * time.Millisecond):
|
||||
return gone, false, ctx.Err()
|
||||
case <-time.After(codexAppPoll):
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -461,7 +651,8 @@ func RestartCodexApp(ctx context.Context, bundle string) error {
|
||||
// a new one, which reads Codex's config as it is now. It waits up to wait
|
||||
// for a running Codex turn to end, and cuts none short: left says an
|
||||
// app-server was still on the old config when it gave up, or couldn't be
|
||||
// ended, so the user quits and opens the app.
|
||||
// ended, or that the app started none in its place and couldn't be
|
||||
// reopened (codexAppGotNone), so the user quits and opens the app.
|
||||
func RestartCodexAppWhenIdle(ctx context.Context, wait time.Duration) (ended int, left bool, err error) {
|
||||
home := codexHome()
|
||||
lctx, cancel := context.WithTimeout(ctx, codexAppListWait)
|
||||
@@ -482,21 +673,33 @@ func RestartCodexAppWhenIdle(ctx context.Context, wait time.Duration) (ended int
|
||||
case <-time.After(codexAppEvery):
|
||||
}
|
||||
}
|
||||
var gone []int
|
||||
for _, p := range pids {
|
||||
if err := endCodexAppServer(p); err != nil {
|
||||
log.Printf("codex: restarting the Codex app's app-server %d: %v", p, err)
|
||||
left = true
|
||||
continue
|
||||
}
|
||||
ended++
|
||||
gone = append(gone, p)
|
||||
}
|
||||
return ended, left, nil
|
||||
if len(gone) > 0 {
|
||||
went, back, err := codexAppBack(ctx, func(ctx context.Context) ([]int, error) {
|
||||
lctx, cancel := context.WithTimeout(ctx, codexAppListWait)
|
||||
defer cancel()
|
||||
return codexAppServersOn(lctx, home)
|
||||
}, gone)
|
||||
switch {
|
||||
case err != nil:
|
||||
return len(gone), true, err
|
||||
case !went:
|
||||
left = true
|
||||
case !back:
|
||||
left = left || codexAppGotNone(ctx)
|
||||
}
|
||||
}
|
||||
return len(gone), left, nil
|
||||
}
|
||||
|
||||
// codexAppBackWait is how long RestartSwitchedCodexApp waits for the Codex
|
||||
// app to start a new app-server after the old one ended; a var for tests.
|
||||
var codexAppBackWait = 20 * time.Second
|
||||
|
||||
// What RestartSwitchedCodexApp found when it couldn't restart the app's
|
||||
// app-server; the agent package says each in the reader's language.
|
||||
var (
|
||||
@@ -574,7 +777,14 @@ func RestartSwitchedCodexApp(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ErrCodexAppNotBack
|
||||
// the Windows app whose old app-server's stdout is held open
|
||||
// starts none till it is quit and opened again (codexAppGotNone)
|
||||
if open, known := codexAppOpenNow(ctx); known && !open {
|
||||
return ErrCodexAppGone
|
||||
}
|
||||
if codexAppGotNone(ctx) {
|
||||
return ErrCodexAppNotBack
|
||||
}
|
||||
}
|
||||
DismissCodexApp()
|
||||
return nil
|
||||
|
||||
@@ -25,24 +25,39 @@ type codexAppStub struct {
|
||||
listErr error
|
||||
endErr error
|
||||
turnOpen bool
|
||||
ended []int
|
||||
// next: the app-server the app starts after one is ended
|
||||
next int
|
||||
// written: a Codex session file was written in the last
|
||||
// codexTurnStuckAfter
|
||||
written bool
|
||||
ended []int
|
||||
// next: the app-server the app starts after one is ended; none when
|
||||
// noNext (its stdout held open, the Windows app starts no other)
|
||||
next int
|
||||
noNext bool
|
||||
}
|
||||
|
||||
func stubCodexApp(t *testing.T) *codexAppStub {
|
||||
t.Helper()
|
||||
s := &codexAppStub{known: true, next: 100}
|
||||
oldOn, oldList, oldEnd, oldTurn := CodexAppRoute, codexAppServersOn, endCodexAppServer, codexTurnRunning
|
||||
t.Cleanup(func() {
|
||||
CodexAppRoute, codexAppServersOn, endCodexAppServer, codexTurnRunning = oldOn, oldList, oldEnd, oldTurn
|
||||
oldOpen, oldReopen, oldQuiet := CodexAppOpen, CodexAppReopen, codexSessionsQuiet
|
||||
reset := func() {
|
||||
codexApp.Lock()
|
||||
codexApp.seen, codexApp.route, codexApp.stale, codexApp.stuck, codexApp.switched = false, "", nil, false, false
|
||||
codexApp.ended, codexApp.backBy, codexApp.spent = nil, time.Time{}, false
|
||||
codexApp.Unlock()
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
CodexAppRoute, codexAppServersOn, endCodexAppServer, codexTurnRunning = oldOn, oldList, oldEnd, oldTurn
|
||||
CodexAppOpen, CodexAppReopen, codexSessionsQuiet = oldOpen, oldReopen, oldQuiet
|
||||
reset()
|
||||
})
|
||||
codexApp.Lock()
|
||||
codexApp.seen, codexApp.route, codexApp.stale, codexApp.stuck, codexApp.switched = false, "", nil, false, false
|
||||
codexApp.Unlock()
|
||||
reset()
|
||||
// the app as tests find it: open, and never reopened
|
||||
CodexAppOpen = func(context.Context) (bool, bool) { return true, true }
|
||||
CodexAppReopen = func(context.Context) error {
|
||||
t.Error("the Codex app reopened")
|
||||
return errors.New("not in this test")
|
||||
}
|
||||
CodexAppRoute = func() (string, bool, bool) {
|
||||
r := s.route
|
||||
if s.on {
|
||||
@@ -63,12 +78,17 @@ func stubCodexApp(t *testing.T) *codexAppStub {
|
||||
s.ended = append(s.ended, pid)
|
||||
// the app starts another app-server, which reads the provider anew
|
||||
if i := slices.Index(s.running, pid); i >= 0 {
|
||||
s.running[i] = s.next
|
||||
s.next++
|
||||
if s.noNext {
|
||||
s.running = slices.Delete(s.running, i, i+1)
|
||||
} else {
|
||||
s.running[i] = s.next
|
||||
s.next++
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
codexTurnRunning = func(string, time.Time, time.Time) bool { return s.turnOpen }
|
||||
codexSessionsQuiet = func(string, time.Time, time.Duration) bool { return !s.written }
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -576,13 +596,22 @@ func TestRestartCodexAppEndsOnlyItsServer(t *testing.T) {
|
||||
{PID: 5, Args: "/opt/homebrew/bin/codex app-server"},
|
||||
{PID: 6, Args: bundle + "/Contents/Resources/codex exec-server --remote x"},
|
||||
}
|
||||
oldList, oldEnd, oldWait := listProcesses, endCodexAppServer, codexAppGoneWait
|
||||
t.Cleanup(func() { listProcesses, endCodexAppServer, codexAppGoneWait = oldList, oldEnd, oldWait })
|
||||
oldList, oldEnd, oldWait, oldBack, oldPoll := listProcesses, endCodexAppServer, codexAppGoneWait, codexAppBackWait, codexAppPoll
|
||||
t.Cleanup(func() {
|
||||
listProcesses, endCodexAppServer, codexAppGoneWait, codexAppBackWait, codexAppPoll = oldList, oldEnd, oldWait, oldBack, oldPoll
|
||||
})
|
||||
codexAppPoll = time.Millisecond
|
||||
listProcesses = func(context.Context) ([]proc.Process, error) { return slices.Clone(ps), nil }
|
||||
var ended []int
|
||||
respawn := true
|
||||
endCodexAppServer = func(pid int) error {
|
||||
ended = append(ended, pid)
|
||||
ps = slices.DeleteFunc(ps, func(p proc.Process) bool { return p.PID == pid })
|
||||
i := slices.IndexFunc(ps, func(p proc.Process) bool { return p.PID == pid })
|
||||
if respawn {
|
||||
ps[i].PID += 100 // the app starts another
|
||||
} else {
|
||||
ps = slices.Delete(ps, i, i+1)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := RestartCodexApp(context.Background(), bundle); err != nil {
|
||||
@@ -592,6 +621,14 @@ func TestRestartCodexAppEndsOnlyItsServer(t *testing.T) {
|
||||
t.Fatalf("ended %v, want only the app's own app-server 2", ended)
|
||||
}
|
||||
|
||||
// gone, and the app started no other (its stdout held open): said,
|
||||
// for the user to quit and open it, never taken as done
|
||||
respawn, ended = false, nil
|
||||
codexAppBackWait = 50 * time.Millisecond
|
||||
if err := RestartCodexApp(context.Background(), bundle); err == nil || !slices.Equal(ended, []int{102}) {
|
||||
t.Fatalf("no app-server came back: %v, ended %v", err, ended)
|
||||
}
|
||||
|
||||
// none running: said
|
||||
ended = nil
|
||||
if err := RestartCodexApp(context.Background(), "/Applications/Other.app"); err == nil || len(ended) > 0 {
|
||||
@@ -627,6 +664,25 @@ func TestRestartCodexAppWhenIdle(t *testing.T) {
|
||||
if n, left, err := RestartCodexAppWhenIdle(context.Background(), time.Second); n != 2 || left || err != nil || !slices.Equal(s.ended, []int{7, 8}) {
|
||||
t.Fatalf("ended %v (%d), left %v, %v", s.ended, n, left, err)
|
||||
}
|
||||
|
||||
// none started in their place: the app is reopened, and said left
|
||||
// when it can't be
|
||||
oldBack, oldPoll := codexAppBackWait, codexAppPoll
|
||||
t.Cleanup(func() { codexAppBackWait, codexAppPoll = oldBack, oldPoll })
|
||||
codexAppBackWait, codexAppPoll = 20*time.Millisecond, time.Millisecond
|
||||
s.noNext = true
|
||||
reopened := 0
|
||||
CodexAppReopen = func(context.Context) error { reopened++; return nil }
|
||||
if n, left, err := RestartCodexAppWhenIdle(context.Background(), time.Second); n != 2 || left || err != nil || reopened != 1 {
|
||||
t.Fatalf("none came back, reopened: %d %v %v, reopened %d times", n, left, err, reopened)
|
||||
}
|
||||
s.running = []int{9}
|
||||
CodexAppReopen = func(context.Context) error { return errors.New("not here") }
|
||||
if _, left, _ := RestartCodexAppWhenIdle(context.Background(), time.Second); !left {
|
||||
t.Fatal("none came back and the app not reopened: not said")
|
||||
}
|
||||
|
||||
s.running = []int{10}
|
||||
s.endErr = proc.ErrDenied
|
||||
if _, left, _ := RestartCodexAppWhenIdle(context.Background(), time.Second); !left {
|
||||
t.Fatal("an app-server not ended isn't said")
|
||||
@@ -705,3 +761,167 @@ func TestCodexAppSwitchRestartsTheAppsOwnServers(t *testing.T) {
|
||||
t.Fatalf("the new app-servers read the new account as they started: ended %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// The Windows Codex app said "Codex app-server process is not available"
|
||||
// on every send after magpie switched its account, until Codex was quit
|
||||
// and opened again (蛋仔 on Discord, 10-30): magpie ended its app-server
|
||||
// and took the old one gone as the switch done, but the app starts
|
||||
// another only once the old one's stdout ends, and a process that
|
||||
// app-server started holds that open on Windows. Once the ones ended are
|
||||
// gone, a new one has to start within codexAppBackWait; when none does
|
||||
// and the app still runs, the app is reopened, and said stuck when it
|
||||
// can't be.
|
||||
func TestCodexAppReopenedWhenNoServerComesBack(t *testing.T) {
|
||||
signIn(t)
|
||||
s := stubCodexApp(t)
|
||||
s.noNext = true
|
||||
open, reopened := true, 0
|
||||
var reopenErr error
|
||||
CodexAppOpen = func(context.Context) (bool, bool) { return open, true }
|
||||
CodexAppReopen = func(context.Context) error {
|
||||
reopened++
|
||||
if reopenErr != nil {
|
||||
return reopenErr
|
||||
}
|
||||
s.running = []int{500} // the app opened again, with its own
|
||||
return nil
|
||||
}
|
||||
start := time.Now()
|
||||
idle := func() (int, time.Time) { return 0, start.Add(-time.Minute) }
|
||||
step := func(at time.Duration) []int { return codexAppStep(context.Background(), start.Add(at), idle) }
|
||||
end := func(at time.Duration) {
|
||||
t.Helper()
|
||||
s.on = !s.on // a route change, as a switch is
|
||||
if got := step(at); len(got) != 1 {
|
||||
t.Fatalf("ended %v, want the one app-server", got)
|
||||
}
|
||||
}
|
||||
|
||||
s.running = []int{60}
|
||||
step(0)
|
||||
end(0)
|
||||
if step(codexAppBackWait / 2); reopened != 0 {
|
||||
t.Fatal("reopened before a new app-server had its time")
|
||||
}
|
||||
if step(codexAppBackWait); reopened != 1 || CodexAppStuck() {
|
||||
t.Fatalf("no app-server came back: reopened %d times, stuck %v", reopened, CodexAppStuck())
|
||||
}
|
||||
if step(2 * codexAppBackWait); reopened != 1 {
|
||||
t.Fatalf("the reopened app's app-server: reopened %d times", reopened)
|
||||
}
|
||||
|
||||
// the app started another in time: nothing more
|
||||
s.noNext = false
|
||||
end(3 * codexAppBackWait)
|
||||
if step(5 * codexAppBackWait); reopened != 1 || CodexAppStuck() {
|
||||
t.Fatalf("a new app-server started: reopened %d times, stuck %v", reopened, CodexAppStuck())
|
||||
}
|
||||
|
||||
// one ended that takes its time to go: the wait counts from its end
|
||||
s.noNext = true
|
||||
slow := endCodexAppServer
|
||||
endCodexAppServer = func(pid int) error { s.ended = append(s.ended, pid); return nil }
|
||||
end(6 * codexAppBackWait)
|
||||
endCodexAppServer = slow
|
||||
if step(7*codexAppBackWait + time.Second); reopened != 1 {
|
||||
t.Fatal("reopened while the one ended still ran")
|
||||
}
|
||||
s.running = nil
|
||||
if step(8 * codexAppBackWait); reopened != 1 {
|
||||
t.Fatal("reopened as the one ended went")
|
||||
}
|
||||
if step(8*codexAppBackWait + codexAppBackWait + 2*time.Second); reopened != 2 {
|
||||
t.Fatalf("none came back after it went: reopened %d times", reopened)
|
||||
}
|
||||
|
||||
// quit meanwhile: it starts one as it opens
|
||||
s.running = []int{600}
|
||||
end(10 * codexAppBackWait)
|
||||
open = false
|
||||
if step(12 * codexAppBackWait); reopened != 2 || CodexAppStuck() {
|
||||
t.Fatalf("the app quit: reopened %d times, stuck %v", reopened, CodexAppStuck())
|
||||
}
|
||||
|
||||
// not reopened (macOS, or the reopen failed): said stuck
|
||||
open, reopenErr = true, errors.New("magpie doesn't reopen it here")
|
||||
s.running = []int{700}
|
||||
end(13 * codexAppBackWait)
|
||||
if step(15 * codexAppBackWait); reopened != 3 || !CodexAppStuck() {
|
||||
t.Fatalf("not reopened: reopened %d times, stuck %v", reopened, CodexAppStuck())
|
||||
}
|
||||
}
|
||||
|
||||
// When quota ran out in the middle of a task the Codex app sat on it and
|
||||
// had to be restarted (蛋仔 on Discord): the turn stays open in Codex's
|
||||
// sessions, and an open turn held the restart for up to codexTurnWindow.
|
||||
// After a switch off a used-up account, a turn that has written nothing
|
||||
// and sent nothing through magpie for codexTurnStuckAfter is stuck, and
|
||||
// the app-server is restarted past it. A switch for any other reason, or
|
||||
// a turn still writing or sending, holds it as before.
|
||||
func TestCodexAppServerRestartedPastATurnStuckOnTheSpentAccount(t *testing.T) {
|
||||
signIn(t)
|
||||
s := stubCodexApp(t)
|
||||
now := time.Now()
|
||||
quiet := func() (int, time.Time) { return 0, now.Add(-codexTurnStuckAfter - time.Second) }
|
||||
step := func(busy func() (int, time.Time)) []int { return codexAppStep(context.Background(), now, busy) }
|
||||
|
||||
s.running, s.turnOpen = []int{60}, true
|
||||
step(quiet)
|
||||
codexAppAccountSwitched() // switched back to an account with room
|
||||
if got := step(quiet); got != nil {
|
||||
t.Fatalf("a turn open after a switch not for spend: ended %v", got)
|
||||
}
|
||||
|
||||
codexAppSpentSwitch()
|
||||
if got := step(func() (int, time.Time) { return 0, now.Add(-time.Minute) }); got != nil {
|
||||
t.Fatalf("Codex's last request through magpie a minute ago: ended %v", got)
|
||||
}
|
||||
s.written = true
|
||||
if got := step(quiet); got != nil {
|
||||
t.Fatalf("the turn still writing to its session: ended %v", got)
|
||||
}
|
||||
s.written = false
|
||||
if got := step(quiet); !slices.Equal(got, []int{60}) {
|
||||
t.Fatalf("a turn stuck on the used-up account: ended %v, want [60]", got)
|
||||
}
|
||||
// the app's next app-server is on the new account: nothing more ended,
|
||||
// and the spend is forgotten once it is back
|
||||
if got := step(quiet); got != nil {
|
||||
t.Fatalf("after the new app-server: ended %v", got)
|
||||
}
|
||||
codexApp.Lock()
|
||||
spent := codexApp.spent
|
||||
codexApp.Unlock()
|
||||
if spent {
|
||||
t.Fatal("the spend is still held after the app-server came back")
|
||||
}
|
||||
}
|
||||
|
||||
// codexSessionsQuietFor reads the sessions' write times: a file written
|
||||
// within d is not quiet, an older one or none is, and a sessions folder
|
||||
// that can't be read is not.
|
||||
func TestCodexSessionsQuietFor(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
now := time.Now()
|
||||
if !codexSessionsQuietFor(home, now, time.Minute) {
|
||||
t.Fatal("no sessions folder: not quiet")
|
||||
}
|
||||
dir := filepath.Join(home, "sessions", "2026", "10", "30")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f := filepath.Join(dir, "rollout-x.jsonl")
|
||||
if err := os.WriteFile(f, []byte("{}\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if codexSessionsQuietFor(home, now, time.Minute) {
|
||||
t.Fatal("a session written just now counts quiet")
|
||||
}
|
||||
old := now.Add(-5 * time.Minute)
|
||||
if err := os.Chtimes(f, old, old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !codexSessionsQuietFor(home, now, time.Minute) {
|
||||
t.Fatal("a session last written 5 minutes ago: not quiet")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,14 +60,33 @@ func TestRestartSwitchedCodexApp(t *testing.T) {
|
||||
t.Fatalf("one that stays: %v", err)
|
||||
}
|
||||
|
||||
// ended, and the app starts none: said, not success
|
||||
// ended, and the app starts none: said, not success, where the app
|
||||
// can't be reopened (macOS: the user does it)
|
||||
endCodexAppServer = func(pid int) error {
|
||||
s.running = slices.DeleteFunc(s.running, func(p int) bool { return p == pid })
|
||||
return nil
|
||||
}
|
||||
if err := RestartSwitchedCodexApp(ctx); !errors.Is(err, ErrCodexAppNotBack) {
|
||||
t.Fatalf("none back: %v", err)
|
||||
reopened := 0
|
||||
CodexAppReopen = func(context.Context) error { reopened++; return errors.New("not here") }
|
||||
s.running = []int{7}
|
||||
if err := RestartSwitchedCodexApp(ctx); !errors.Is(err, ErrCodexAppNotBack) || reopened != 1 {
|
||||
t.Fatalf("none back: %v, reopened %d", err, reopened)
|
||||
}
|
||||
// the Windows app, whose old app-server's stdout a leftover child
|
||||
// holds open, starts none till it is quit and opened again (蛋仔 on
|
||||
// Discord): reopened, it is on the new account
|
||||
CodexAppReopen = func(context.Context) error { reopened++; return nil }
|
||||
s.running = []int{7}
|
||||
if err := RestartSwitchedCodexApp(ctx); err != nil || reopened != 2 {
|
||||
t.Fatalf("none back on Windows: %v, reopened %d", err, reopened)
|
||||
}
|
||||
// the user quit the app meanwhile: it opens on the new account
|
||||
CodexAppOpen = func(context.Context) (bool, bool) { return false, true }
|
||||
s.running = []int{7}
|
||||
if err := RestartSwitchedCodexApp(ctx); !errors.Is(err, ErrCodexAppGone) || reopened != 2 {
|
||||
t.Fatalf("app quit: %v, reopened %d", err, reopened)
|
||||
}
|
||||
CodexAppOpen = func(context.Context) (bool, bool) { return true, true }
|
||||
|
||||
// the listing fails: unknown, not success
|
||||
s.running, s.listErr = []int{9}, errors.New("ps failed")
|
||||
@@ -170,10 +189,11 @@ func TestRestartSwitchedCodexAppReal(t *testing.T) {
|
||||
t.Fatalf("app-server %d after the restart, first %d: want a new one", now, first)
|
||||
}
|
||||
|
||||
// the app doesn't start another: said
|
||||
// the app doesn't start another, and can't be reopened: said
|
||||
f.mu.Lock()
|
||||
f.back = false
|
||||
f.mu.Unlock()
|
||||
CodexAppReopen = func(context.Context) error { return errors.New("not here") }
|
||||
if err := RestartSwitchedCodexApp(context.Background()); !errors.Is(err, ErrCodexAppNotBack) {
|
||||
t.Fatalf("none back: %v", err)
|
||||
}
|
||||
|
||||
@@ -238,11 +238,26 @@ func recheckLeft() {
|
||||
if findCodexDaemon(ps, codexHome()) != codexDaemonLeft.pid {
|
||||
codexDaemonLeft = leftDaemon{}
|
||||
}
|
||||
if findCodexApp(ps) != codexAppLeft.pid {
|
||||
// one that went with none in its place left an app still on nothing
|
||||
// new while it runs (codexAppGotNone): the app read no sign-in
|
||||
// afresh, so it is still said
|
||||
if p := findCodexApp(ps); p != codexAppLeft.pid && (p != 0 || !codexAppStillOpen()) {
|
||||
codexAppLeft = leftDaemon{}
|
||||
}
|
||||
}
|
||||
|
||||
// codexAppStillOpen is whether the Codex desktop app is known to run
|
||||
// (CodexAppOpen), for an app-server gone with none in its place.
|
||||
func codexAppStillOpen() bool {
|
||||
if CodexAppOpen == nil {
|
||||
return false
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
open, known := CodexAppOpen(ctx)
|
||||
return open && known
|
||||
}
|
||||
|
||||
// DismissCodexDaemon stops saying the app-server is on another account.
|
||||
func DismissCodexDaemon() {
|
||||
codexDaemonMu.Lock()
|
||||
|
||||
@@ -577,6 +577,55 @@ func TestCodexAppStale(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// An app-server that went with none started in its place left the app
|
||||
// with nothing that read the new sign-in (蛋仔 on Discord, Windows: "Codex
|
||||
// app-server process is not available" till Codex was reopened): while
|
||||
// the app runs, that is still said; once it quits, or it starts another,
|
||||
// it isn't.
|
||||
func TestCodexAppStaleWhileNoServerCameBack(t *testing.T) {
|
||||
t.Setenv("HOME", t.TempDir())
|
||||
t.Setenv("USERPROFILE", t.TempDir())
|
||||
server, open := 60, true
|
||||
oldList, oldOpen := listProcesses, CodexAppOpen
|
||||
t.Cleanup(func() {
|
||||
listProcesses, CodexAppOpen = oldList, oldOpen
|
||||
DismissCodexDaemon()
|
||||
DismissCodexApp()
|
||||
codexApp.Lock()
|
||||
codexApp.switched = false
|
||||
codexApp.Unlock()
|
||||
})
|
||||
listProcesses = func(context.Context) ([]proc.Process, error) {
|
||||
if server == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return []proc.Process{{PID: server, Args: codexWindowsAppServer}}, nil
|
||||
}
|
||||
CodexAppOpen = func(context.Context) (bool, bool) { return open, true }
|
||||
recheck := func() { codexDaemonMu.Lock(); codexDaemonChecked = time.Time{}; codexDaemonMu.Unlock() }
|
||||
DismissCodexApp()
|
||||
|
||||
noteCodexSwitch("a@x", "b@x")
|
||||
server = 0 // ended, and the app started none
|
||||
recheck()
|
||||
if got := CodexAppStale(); got != "a@x" {
|
||||
t.Fatalf("app open with no app-server: got %q, want a@x", got)
|
||||
}
|
||||
open = false
|
||||
recheck()
|
||||
if got := CodexAppStale(); got != "" {
|
||||
t.Fatalf("app quit: %q", got)
|
||||
}
|
||||
|
||||
open, server = true, 60
|
||||
noteCodexSwitch("a@x", "b@x")
|
||||
server = 61
|
||||
recheck()
|
||||
if got := CodexAppStale(); got != "" {
|
||||
t.Fatalf("a new app-server: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// The daemon magpie restarts runs with the login shell's proxy and PATH,
|
||||
// as one a terminal started would (guotianci6 on X): magpie opened from
|
||||
// the desktop has neither.
|
||||
|
||||
@@ -340,6 +340,9 @@ func SwitchWhenSpent(ctx context.Context, agent string) (string, error) {
|
||||
setLoginReturn(agent, r)
|
||||
share, _ := loginSwitching(agent)
|
||||
log.Printf("%s: %s has used %g%% or more of its allowance; signed it in to %s", agent, from, share, to)
|
||||
if agent == "codex" {
|
||||
codexAppSpentSwitch()
|
||||
}
|
||||
}
|
||||
if agent == "codex" {
|
||||
if was := CodexAppStale(); was != "" {
|
||||
|
||||
@@ -60,9 +60,22 @@ func TestCodexSwitchedWhenUsedUp(t *testing.T) {
|
||||
if err := SetLoginOn("codex", "spare@example.com", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
codexApp.Lock()
|
||||
codexApp.switched, codexApp.spent = false, false
|
||||
codexApp.Unlock()
|
||||
})
|
||||
if to := switched(); to != "spare@example.com" {
|
||||
t.Fatalf("switched to %q", to)
|
||||
}
|
||||
// the Codex app's app-server is restarted past a turn left stuck on
|
||||
// the account used up (TestCodexAppServerRestartedPastATurnStuckOnTheSpentAccount)
|
||||
codexApp.Lock()
|
||||
spent := codexApp.spent
|
||||
codexApp.Unlock()
|
||||
if !spent {
|
||||
t.Fatal("the switch off a used-up account isn't marked spent for the Codex app")
|
||||
}
|
||||
var live codexAuth
|
||||
readJSON(filepath.Join(home, ".codex", "auth.json"), &live)
|
||||
if live.Tokens.RefreshToken != "r-spare" {
|
||||
|
||||
Reference in New Issue
Block a user