ysm119 on Discord: each Restart of Codex in magpie left the ChatGPT desktop app on "ChatGPT 遇到了问题 / 请重试" till it was quit and opened again. magpie ends the app's own app-server cleanly (code 0); a signed-in ChatGPT 26.1021 then fails "Hosted AppServerManager identity is not ready" on the one it starts, logs fatal_error_broadcasted and shows the page. That is the app's bug; magpie now follows it up. - provider/codex_app_crash.go: for 60s after every end magpie makes (the watcher's step, RestartCodexApp, RestartSwitchedCodexApp) the app's logs are read for a whole fatal_error_broadcasted line stamped after the end. On one, with no request through magpie since or in flight and no Codex turn opened since, the app is quit and opened once. Otherwise it is left as it is and logged. Where it can't be reopened (Linux, an app that didn't quit) the Agents row says "The Codex app hit an error · quit it and open it again" (appSnag) until the app-servers seen then are gone or the app is closed. Unreadable logs reopen nothing. - agent/codex_app_crash.go: the logs as the app's file logger writes them (macOS ~/Library/Logs/com.openai.codex, Windows %LOCALAPPDATA%\Codex\Logs and the Store package's LocalCache, Linux $XDG_STATE_HOME/codex/logs; YYYY/MM/DD folders, tail-read, half lines dropped). The relaunch is the Agents row's: on macOS ChatGPT.app found by its bundle id, quit as ⌘Q does and opened once it has ended, never forced; on Windows codexAppReopen. - GUI line in en, zh, zh-TW, ja, de; docs/subsystems/agent-wiring.md. Run on mote (macOS) at this head on origin/main08982805e: - go vet ./internal/agent ./internal/provider ./internal/gui (darwin and GOOS=windows), GOOS=linux go vet ./internal/agent ./internal/provider; go build ./...; gofmt -l clean. - go test ./internal/provider ok; go test -run 'TestCodexApp|Restart|Reopen' ./internal/agent ok. - go test ./internal/gui: red only in TestMainWindowCorners (#3949) and TestPanelHiddenByWailsMarksPageOutOfSight (#5322), AppKit window tests red on mote's macOS 27 and filed before this change. - win-run and wsl-run: go test -run TestCodexApp ./internal/agent ./internal/provider ok (the macOS relaunch test skips on Windows, as TestWorkBuddySidecarOutlivesQuitOnMac does, #4115). - node --test codex-app-snag, codex-app-out, codex-app-back, i18n-keys, i18n-en-no-cjk: chromium and webkit, every language, 980 and 440px. i18n-keys' "a plugin's words are its own" stays red for "View routing" (#5327/#5332, red on main since73c98ebc2); this change added a word there and took it out again. - Core script budget: under it, not raised. Broken by hand, each red then restored: - codexAppCrashStep's call removed: 4 provider tests red. - the watch not armed in RestartSwitchedCodexApp: TestCodexAppRestartsWatchForTheErrorPage red; in RestartCodexApp: TestCodexAppRowRestartWatchesForTheErrorPage red. - the turn and request checks off: TestCodexAppInUseAfterItsErrorPageIsLeft red. - the snag not set: TestCodexAppSnagSaidTillTheAppRestarts red. - the stamp's >= since check dropped, or a half-written last line kept: TestCodexAppCrashedReadsItsOwnLogs red. - a wrong Store package path: TestCodexAppCrashedReadsEachSystemsFolder red. Not tried: a real signed-in ChatGPT app reaching the identity-not-ready page, and a real quit and reopen of the user's ChatGPT.app (the relaunch is tested with faked process listing, quit and open in a sandbox HOME). The log line and folders are read from ChatGPT 26.1021's file-based-logger and the reporters' logs; a later app that renames them reopens nothing, as before this change.
280 lines
8.9 KiB
Go
280 lines
8.9 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)
|
|
}
|
|
}
|