Files
magpie/internal/provider/codex_app_crash.go
T
yetone 961427f122 codex: the ChatGPT app left on "hit a snag" after a switch off a used-up account is reopened without the Restart tick (RealZeroP27 on X)
RealZeroP27 (switch only, requests not through magpie, ChatGPT app on
macOS 27.2.1): when the account ran out and magpie signed Codex in to the
next one, the app showed "ChatGPT 遇到了问题 请重试" and only a manual
restart cleared it.

Cause: magpie did end the app-server (a switch off a used-up account
always does), and the signed-in ChatGPT app fails on the one it starts
next, a fault in the app (codex_app_crash.go, #4550). magpie reopens the
app on that page only with "Restart the Codex app" ticked, which ships
off (15acba534, Michael25651150), so the page stayed.

Now an end made for a used-up account (a switch off one, or the takeover
in; crashSpent) has the app quit and opened once on that page whatever the
tick says: the app was greyed on that account already and the page is all
its window shows. Every other end (route change, switch among accounts
with room) still waits for the tick. The page check (no composer, no turn
or request since) is unchanged. The Windows reopen when no app-server came
back (codexAppGotNone) is unchanged and stays tick-gated.

This partly reverses 15acba534's gate for the used-up case; a product
call. A quit of a working app is still never done unasked.

Run on mote at this head:
- go test -count=1 ./internal/provider ./internal/agent: ok.
- go vet ./internal/provider on darwin, GOOS=windows, GOOS=linux nogui;
  gofmt -l clean.
- TestCodexAppReopenedAfterASpentEndUnasked (switch, takeover) is new;
  TestCodexAppNotQuitUnasked now ends for a route change.
Broken by hand: spentEnd forced false -> the new test red in both subtests.

Not tried: the real ChatGPT app, the Windows box. No GUI file touched.
2026-11-11 17:12:44 +08:00

172 lines
6.3 KiB
Go

package provider
import (
"context"
"errors"
"log"
"slices"
"time"
)
// A signed-in ChatGPT app (26.1021 and on) can't always take the
// app-server magpie ended: the one it starts in its place answers its
// handshake, and then the app's own Hosted AppServerManager finds the
// account it had cached cleared with the old connection ("Hosted
// AppServerManager identity is not ready"), logs fatal_error_broadcasted
// and shows "ChatGPT hit a snag / Please try again" over the whole window
// till it is quit and opened again (ysm119 on Discord, #4550: sljdxde,
// wang9262, Indekkusu545). That is in the app, out of magpie's reach; the
// app-server's own exit is clean (code 0, which it counts expected).
//
// So for codexAppCrashWait after magpie ends an app-server, its logs are
// read for that line (CodexAppCrashed, the agent package's: the app's own
// log folder, a line stamped after the end). When the app shows the page
// and no Codex turn has started since nor a request gone through magpie
// (the page has no composer: one that ran means it came back), magpie
// quits the app and opens it again, once (CodexAppRelaunch: ⌘Q's quit on
// macOS, never forced; the Agents row's Restart on Windows). After an end
// for a used-up account (crashSpent) that follows magpie's account switch
// itself, with nothing to turn on: the page is all the window shows, the
// switch is what the user asked magpie for, and only a quit and open
// clears it (RealZeroP27 on X: "只能通过手动重启 codex 解决"). After any
// other end it needs the user's "Restart the Codex app" (RestartApp):
// unasked, a quit of a working app read as magpie force-restarting it
// (Michael25651150 on X). Where it is off or can't be done, or the app
// didn't quit, the Agents row says to (snag, CodexAppSnagged) until the
// app-servers seen then are gone.
// CodexAppCrashed and CodexAppRelaunch are set by the agent package.
// CodexAppCrashed says whether the Codex app logged the page it shows
// when it can't go on (fatal_error_broadcasted) at or after since, and
// when; known false when its logs can't be read. CodexAppRelaunch quits
// the app and opens it again, erring where it can't.
var (
CodexAppCrashed func(since time.Time) (at time.Time, crashed, known bool)
CodexAppRelaunch func(ctx context.Context) error
// codexAppCrashWait is how long after an end the logs are read: the
// app logs the page within seconds of the new app-server's start
codexAppCrashWait = 60 * time.Second
)
// watchCodexAppCrash starts reading the app's logs for the page, from
// now, or carries a watch already running on to now's window.
func watchCodexAppCrash(now time.Time) {
c := &codexApp
c.Lock()
defer c.Unlock()
watchCodexAppCrashLocked(now)
}
func watchCodexAppCrashLocked(now time.Time) {
c := &codexApp
if c.crashBy.IsZero() || c.crashSince.After(now) {
c.crashSince = now
}
c.crashBy = now.Add(codexAppCrashWait)
}
// CodexAppSnagged is whether the Codex app showed the page it shows when
// it can't go on after magpie restarted its app-server, and magpie
// couldn't reopen it: the user quits it and opens it again.
func CodexAppSnagged() bool {
c := &codexApp
c.Lock()
defer c.Unlock()
return c.snag
}
// codexAppCrashStep is codexAppStep's look at the app after an end: it
// reads the logs while a watch runs, reopens the app that shows the page,
// and forgets a snag once the app-servers running at it are gone.
func codexAppCrashStep(ctx context.Context, now time.Time, busy func() (int, time.Time)) {
c := &codexApp
c.Lock()
since, by, snag, snagPids := c.crashSince, c.crashBy, c.snag, slices.Clone(c.snagPids)
spentEnd := c.crashSpent
c.Unlock()
home := codexHome()
if snag {
if open, known := codexAppOpenNow(ctx); known && !open {
codexAppUnsnag()
return
}
lctx, cancel := context.WithTimeout(ctx, codexAppListWait)
pids, err := codexAppServersOn(lctx, home)
cancel()
// a listing that fails is unknown: the snag stays. With none
// running at it, one that starts is the app's new start
if err == nil && (len(snagPids) > 0 || len(pids) > 0) &&
!slices.ContainsFunc(pids, func(p int) bool { return slices.Contains(snagPids, p) }) {
codexAppUnsnag()
}
if by.IsZero() {
return
}
}
if by.IsZero() || CodexAppCrashed == nil {
return
}
at, crashed, _ := CodexAppCrashed(since)
if !crashed {
// a page not seen in the window, or logs that can't be read, end
// the watch: an unread log never reopens the app
if !now.Before(by) {
c.Lock()
if c.crashBy.Equal(by) {
c.crashSince, c.crashBy, c.crashSpent = time.Time{}, time.Time{}, false
}
c.Unlock()
}
return
}
c.Lock()
c.crashSince, c.crashBy, c.crashSpent = time.Time{}, time.Time{}, false
c.Unlock()
// the page has no composer: a request or a turn since means the app
// came back (its Try again) and is in use, never cut short
if busy != nil {
if n, last := busy(); n > 0 || last.After(at) {
log.Printf("codex: the Codex app showed its error page after magpie restarted its app-server, and has sent since; left as it is")
return
}
}
if codexTurnRunning(home, now, at) {
log.Printf("codex: the Codex app showed its error page after magpie restarted its app-server, and a Codex turn runs since; left as it is")
return
}
lctx, cancel := context.WithTimeout(ctx, codexAppListWait)
pids, _ := codexAppServersOn(lctx, home)
cancel()
err := errNoRelaunch
switch {
case !codexRestartsApp() && !spentEnd:
// quitting the user's app and opening it again, unasked, takes
// their window with it (Michael25651150 on X: "额度用尽时会强制重启
// codex 客户端"); the Agents row says to, and its Try again or
// Restart is theirs
err = errRelaunchOff
case CodexAppRelaunch != nil:
err = CodexAppRelaunch(ctx)
}
if err == nil {
log.Printf("codex: the Codex app showed its error page after magpie restarted its app-server; quit it and opened it again")
return
}
log.Printf("codex: the Codex app showed its error page after magpie restarted its app-server; reopening it: %v", err)
c.Lock()
c.snag, c.snagPids = true, pids
c.Unlock()
}
func codexAppUnsnag() {
c := &codexApp
c.Lock()
c.snag, c.snagPids = false, nil
c.Unlock()
}
var (
errNoRelaunch = errors.New("magpie doesn't reopen the Codex app here")
errRelaunchOff = errors.New("left to the user: Restart the Codex app when magpie switches its account is off")
)