Norethcy on X (v0.1.4045): the Codex app (ChatGPT.app) went through a routing group. The ChatGPT account ran out mid-conversation, and the group's fallback answered. While that reply went on, magpie restarted the app's app-server and cut the turn off. magpie ends the app's app-server (codexAppStep, every 3s) once Codex's route has changed. That happens here: the account is spent, so magpie becomes Codex's provider. The end waits for the gateway to be idle for 10s, and for no turn open in a session file. The causes: - sessionTurnOpen read only the last 512 KB of a rollout. A turn that wrote more than that counted as no turn at all: a tool's long output, images, an hour of work (one of the owner's own turns is 22 MB). A tool running 10s between two requests was then taken for the turn's end. The file is now read back in 512 KB chunks to the last task_started, task_complete or turn_aborted. Each chunk overlaps the next by a mark's length, so a mark across two reads is found whole. - With the account spent, a turn open past 2m with quiet sessions counted as stuck on the spent account and was ended. A turn that went on through magpie after the route changed is served, not stuck. It is no longer ended for that (goneOn). - busy was read before CodexBeforeAppEnd moved the open chats. A request that began meanwhile was ended under. It is read again just before the end. - When the listing showed no new app-server after the end, the app was quit and reopened (Windows) even while it sent through magpie. Now it isn't reopened while a request is in flight or one began since the end. - A magpie that took the gateway over (an update) saw none of the old magpie's streams, so its first look could end the app-server under one. Its start now counts as a Codex request just ended. The crash relaunch (codexAppCrashStep) and RestartCodexAppWhenIdle use the same turn check, so they wait for a long turn too. The buttons the user presses restart at once, as before. Tests (internal/provider/codex_app_inflight_test.go). All 6 fail with the old codex_app_server.go swapped in, and pass now: - TestCodexTurnOpenPastTheTail: turns of 512 KB+1, 1.5 MB and 22 MB, open and ended, with the app-server's start before and after the turn. - TestCodexTurnOpenMarkAcrossReads: each mark straddling the read boundary at every byte. - TestCodexAppServerWaitsForATurnTheFallbackServes: the reporter's case with the real session-file check. The route goes out while the fallback streams; a 600 KB turn has a 15s tool gap, then a tool running past 2m. The app-server is ended only once the turn ends. - TestCodexAppServerEndLooksAtRequestsAgain, TestCodexAppNotReopenedWhileItSends, TestCodexAppStartCountsAsARequest. Broken by hand: with goneOn forced false, the fallback test fails at "a tool running over 2m0s in a turn the fallback served". Cost: an open 22 MB turn takes 97 ms and 559 KB per look (benchmark on mica). A look happens at most every 3s, and only while an app-server is waiting to be ended, the gateway is idle and such a turn is open. An ended or short turn is found in the last chunk, as before. Run on mote: - Full suite, go test -timeout 45m ./..., at this change onb3395ffda: every package ok (internal/gateway 969s, internal/provider 223s) except two. TestNoEagerPackageRegexps failed on internal/davsync/setuptext.go, which48e394146fixed on main. TestMainWindowCorners/macOS_15_SDK was killed at its 40s deadline at load ~29 (#1936, #2645). Log kept in the scratch folder. - Rebased onto origin/main36c5ee1e6(three commits: davsync, claude, gui WebView2), then ran . ./internal/provider ./internal/davsync: ok. TestMainWindowCorners alone: ok (17s). - gofmt -l: clean. GOOS=windows go vet ./...: ok. GOOS=linux go vet -tags nogui ./...: ok. Not tried: the real ChatGPT app in a sandbox HOME, or a real account running out. Real rollout files were checked for the marks (task_started, task_complete, turn_aborted) and for turn sizes of 14 to 22 MB.
1242 lines
45 KiB
Go
1242 lines
45 KiB
Go
package provider
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"io/fs"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/dirent"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// The Codex app greys its Send for every model, magpie's too, while the
|
|
// ChatGPT account it is signed in to is used up (its composer's gate,
|
|
// XP in ChatGPT.app 26.1013's app-primary: authMethod "chatgpt" and
|
|
// rate_limit.allowed false). It reads authMethod from its app-server's
|
|
// account/read, which answers no account once magpie is Codex's provider
|
|
// (requires_openai_auth false), but it asks only as the app-server
|
|
// starts. So when magpie becomes Codex's provider for the used-up
|
|
// account (the agent package's AppOut), and again when it puts Codex
|
|
// back on the account after its reset, magpie ends the app's own
|
|
// app-server, the one it started with: the app starts another at once,
|
|
// reads account/read again, and sends (tried in ChatGPT.app 26.1013 on
|
|
// macOS; its window, threads and drafts stay). It does that on every
|
|
// system the app runs on, with nothing for the user to do, once no Codex
|
|
// turn is running: none through magpie, none open in Codex's sessions.
|
|
//
|
|
// The same holds for every change magpie makes to where Codex sends its
|
|
// requests: a Disconnect that takes openai_base_url out, a Connect that
|
|
// puts it in, failover, the gateway moved to another port. The app-server
|
|
// read config.toml as it started, and a Codex app left running kept
|
|
// sending to 127.0.0.1:3425 after Codex was disconnected (JiangLiuer222 on
|
|
// X), so each such change restarts it the same way.
|
|
//
|
|
// A change to the model list alone never does: the app takes an
|
|
// app-server's end as a crash and relaunches, and the list changes far
|
|
// more often than the route (an effort picked in the app, a provider
|
|
// renamed), which ended the app every few minutes (#4550). The app reads
|
|
// a new list at its next launch, or on the Agents row's Restart.
|
|
//
|
|
// The app-server is ended as a clean exit (proc.End): the app reads any
|
|
// other exit as a crash and shows it. A signed-in ChatGPT app can show its
|
|
// error page all the same; codex_app_crash.go reopens it then.
|
|
|
|
// CodexAppRoute is set by the agent package: what in Codex's config the
|
|
// app-server reads as it starts and magpie writes (its provider and base
|
|
// URL: route, compared, never parsed), and whether magpie is Codex's
|
|
// provider for its used-up ChatGPT account (out, AppOut); known false
|
|
// when Codex's config can't be read. Nothing the Codex app writes itself
|
|
// (its model or effort pick) and not the model list is in route, or a
|
|
// pick would restart it.
|
|
var CodexAppRoute func() (route string, out, known bool)
|
|
|
|
// CodexAppDesk is set by the agent package: when magpie named its model
|
|
// list for the Codex app in Codex's config (model_catalog_json beside the
|
|
// ChatGPT sign-in), zero while it names none or the time isn't known. The
|
|
// app-server reads model_catalog_json only as it starts and never again
|
|
// (codex-rs app-server model_catalog.rs reloads on an account change
|
|
// alone), and the app keeps one app-server for every window, so an app
|
|
// that started before the list was named shows Codex's own models alone
|
|
// until it is quit (not_4O4 on X, the Windows app: a new window and a new
|
|
// project on D: listed GPT alone). Unlike a new list, which changes with
|
|
// every pick and is left for the app's next launch (#4550), the list is
|
|
// named once, as Codex is connected or the list first becomes known: its
|
|
// app-servers from before are restarted for it once, as for a Connect.
|
|
var CodexAppDesk func() time.Time
|
|
|
|
// CodexBeforeAppEnd is set by the agent package: it is called with Codex's
|
|
// home just before the app's app-server is ended for a change of where
|
|
// Codex goes, to put the chats the app has open onto the provider Codex is
|
|
// on now. The app-server that starts in its place resumes them on their
|
|
// row's provider, and Codex refuses a chat of another one.
|
|
var CodexBeforeAppEnd func(home string)
|
|
|
|
// 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
|
|
|
|
// CodexTakeoverOff is set by the agent package: whether the user has
|
|
// turned off magpie's takeover of Codex's used-up account (AppOutOff):
|
|
// its sign-in set to Always ChatGPT, as the takeover note's Keep on
|
|
// ChatGPT does, or Codex not connected. The takeover ending while it says
|
|
// so is the user's change, and the Codex app's app-server is restarted
|
|
// for it as for a Connect, Restart the Codex app to follow off too: the
|
|
// one the takeover started answers account/read with no account, so the
|
|
// app shows none of its ChatGPT projects or chats till it restarts, which
|
|
// the user who turned the takeover off to have them back can't tell
|
|
// (Herzz____ on X, the Windows app's project list empty). Asked only as
|
|
// the takeover ends; false while it isn't set.
|
|
var CodexTakeoverOff 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
|
|
// 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 !codexRestartsApp() {
|
|
// the user's app is never quit unasked (Michael25651150 on X):
|
|
// the Agents row says to, and its Restart does it
|
|
log.Printf("codex: the Codex app started no app-server in place of the one magpie ended; left for the user to reopen (Restart the Codex app is off)")
|
|
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
|
|
// one app-server is ended before magpie says it can't.
|
|
var (
|
|
codexAppEvery = 3 * time.Second
|
|
codexAppIdle = 10 * time.Second
|
|
codexAppTries = 3
|
|
)
|
|
|
|
// codexTurnWindow is how long a session file written to last counts as a
|
|
// turn maybe still running when it has one started and not ended: a turn
|
|
// 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.
|
|
const codexTurnSlack = 2 * time.Second
|
|
|
|
// codexAppListWait bounds one listing of the app-servers and their homes:
|
|
// at a load of 200 a 10s bound ran out on the owner's Mac (the change was
|
|
// taken up at the next look, 9s later).
|
|
const codexAppListWait = 30 * time.Second
|
|
|
|
// codexAppServersOn are the pids of the Codex app's own app-servers that
|
|
// run on the Codex home home; vars for tests.
|
|
var (
|
|
codexAppServersOn = func(ctx context.Context, home string) ([]int, error) {
|
|
ps, err := listProcesses(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var out []int
|
|
for _, p := range ps {
|
|
if isCodexAppOwnServer(p.Args) && codexAppHomeIs(ctx, p.PID, home) {
|
|
out = append(out, p.PID)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
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
|
|
// turn started before it can't be running in any of them. It is zero,
|
|
// "unknown", when one's start can't be read, and every open turn counts.
|
|
func codexServersSince(pids []int) time.Time {
|
|
var since time.Time
|
|
for _, p := range pids {
|
|
t, err := codexServerStart(p)
|
|
if err != nil || t.IsZero() {
|
|
return time.Time{}
|
|
}
|
|
if since.IsZero() || t.Before(since) {
|
|
since = t
|
|
}
|
|
}
|
|
return since
|
|
}
|
|
|
|
// isCodexAppOwnServer is whether a command line is the Codex app's own
|
|
// app-server, which it talks to over stdio: one another program starts
|
|
// from the app's bundled codex names where it listens (`app-server
|
|
// --listen stdio://`, Miyi Helper on macOS), and is that program's to end.
|
|
func isCodexAppOwnServer(args string) bool {
|
|
if !isCodexAppServer(args) {
|
|
return false
|
|
}
|
|
_, rest := splitExe(args)
|
|
for _, f := range strings.Fields(rest) {
|
|
if f == "--listen" || strings.HasPrefix(f, "--listen=") {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// codexAppHomeIs is whether app-server pid runs on Codex home home: its
|
|
// CODEX_HOME, else its HOME's .codex. A system that doesn't tell
|
|
// (Windows) counts as yes: one Codex home is the rule there.
|
|
func codexAppHomeIs(ctx context.Context, pid int, home string) bool {
|
|
env, err := proc.EnvOf(ctx, pid, "CODEX_HOME", "HOME")
|
|
if errors.Is(err, proc.ErrNoEnv) {
|
|
return true
|
|
}
|
|
if err != nil {
|
|
return false
|
|
}
|
|
h := env["CODEX_HOME"]
|
|
if h == "" && env["HOME"] != "" {
|
|
h = filepath.Join(env["HOME"], ".codex")
|
|
}
|
|
return h != "" && sameDir(h, home)
|
|
}
|
|
|
|
// sameDir is whether a and b name one folder, through links too.
|
|
func sameDir(a, b string) bool {
|
|
a, b = filepath.Clean(a), filepath.Clean(b)
|
|
if a == b {
|
|
return true
|
|
}
|
|
ra, err1 := filepath.EvalSymlinks(a)
|
|
rb, err2 := filepath.EvalSymlinks(b)
|
|
return err1 == nil && err2 == nil && ra == rb
|
|
}
|
|
|
|
// codexApp is what KeepCodexAppOnItsProvider knows.
|
|
var codexApp struct {
|
|
sync.Mutex
|
|
seen bool
|
|
route string
|
|
// out: whether the route last seen was magpie as Codex's provider for
|
|
// its used-up account (CodexAppRoute's out)
|
|
out bool
|
|
// stale: the app-servers started before Codex's provider last changed,
|
|
// each with how often it was ended
|
|
stale map[int]int
|
|
// stuck: one of them outlived codexAppTries ends, or may not be ended
|
|
stuck bool
|
|
// switched: Codex's ChatGPT account changed since the app-servers were
|
|
// last listed (codexAppAccountSwitched)
|
|
switched bool
|
|
// auto: every switch since then magpie made by itself
|
|
// (SwitchWhenSpent), none the user made
|
|
auto bool
|
|
// spent: the app-servers are on an account used up, so a turn they
|
|
// left open can't be served by them any more: Codex was switched off
|
|
// it (SwitchWhenSpent, or by hand off one known held, noteCodexSwitch),
|
|
// or magpie became Codex's provider for it (out)
|
|
spent bool
|
|
// desk: magpie's list named for the app as last handled (CodexAppDesk)
|
|
desk time.Time
|
|
// 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
|
|
// endedAt: when the last of them was ended
|
|
endedAt time.Time
|
|
// changedAt: when the app-servers in stale were last listed for a
|
|
// change
|
|
changedAt time.Time
|
|
// why: what made the app-servers in stale stale, said in the log line
|
|
// when they are ended, so a crash a user reports can be put down to
|
|
// an account switch, a route change or a used-up account (#4550)
|
|
why string
|
|
// back: the app-servers left running on magpie as Codex's provider
|
|
// when magpie gave Codex back its ChatGPT account at the reset, with
|
|
// Restart the Codex app to follow off (CodexAppBack), and when they
|
|
// were last listed
|
|
back map[int]bool
|
|
backAt time.Time
|
|
// crashSince, crashBy: magpie last ended the app's app-server at
|
|
// crashSince, and until crashBy its logs are read for the page the
|
|
// app shows when it can't take another (codexAppCrashStep); snag:
|
|
// the app showed that page and magpie couldn't reopen it, while the
|
|
// app-servers in snagPids (those running then) run
|
|
crashSince, crashBy time.Time
|
|
// crashSpent: an end in that window was made for a used-up account
|
|
// (spent), where the app was greyed already and what it shows after
|
|
// is no working window to leave alone (codexAppCrashStep)
|
|
crashSpent bool
|
|
snag bool
|
|
snagPids []int
|
|
}
|
|
|
|
// codexAppBackEvery is how often the app-servers left on the takeover
|
|
// are listed again to see whether they went: a listing is a PowerShell
|
|
// run on Windows.
|
|
var codexAppBackEvery = 30 * time.Second
|
|
|
|
// CodexAppBack is whether the Codex app still runs on the app-server it
|
|
// started while magpie was Codex's provider for its used-up account, now
|
|
// that magpie gave Codex back its ChatGPT account and left the app
|
|
// running (Restart the Codex app to follow off, #4550). That app-server
|
|
// read account/read as it started, when Codex's provider took no ChatGPT
|
|
// account, so the app shows no account, no usage meter and none of its
|
|
// ChatGPT side until it restarts: the Agents row says so and offers the
|
|
// restart (練習^1.0beta by mail, 11-04: the panels loaded only after
|
|
// `magpie codex default` and a restart).
|
|
func CodexAppBack() bool {
|
|
c := &codexApp
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
return len(c.back) > 0
|
|
}
|
|
|
|
// checkCodexAppBack forgets the app-servers left on the takeover that are
|
|
// gone: the app started another, which reads the account afresh, or was
|
|
// quit. A listing that fails keeps what was known.
|
|
func checkCodexAppBack(ctx context.Context, now time.Time, home string) {
|
|
c := &codexApp
|
|
c.Lock()
|
|
due := len(c.back) > 0 && now.Sub(c.backAt) >= codexAppBackEvery
|
|
c.Unlock()
|
|
if !due {
|
|
return
|
|
}
|
|
lctx, cancel := context.WithTimeout(ctx, codexAppListWait)
|
|
pids, err := codexAppServersOn(lctx, home)
|
|
cancel()
|
|
if err != nil {
|
|
return
|
|
}
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
c.backAt = now
|
|
for p := range c.back {
|
|
if !slices.Contains(pids, p) {
|
|
delete(c.back, p)
|
|
}
|
|
}
|
|
}
|
|
|
|
// CodexAppStuck is whether a Codex app is left on the provider it started
|
|
// with, which magpie couldn't restart: the user quits and opens it.
|
|
func CodexAppStuck() bool {
|
|
c := &codexApp
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
return c.stuck
|
|
}
|
|
|
|
// codexAppAccountSwitched has the Codex app's own app-servers, left on the
|
|
// ChatGPT account Codex was switched away from (noteCodexSwitch), ended
|
|
// as a route change's are, once no Codex turn runs: the app starts
|
|
// another, which reads auth.json and the new account afresh, so the app
|
|
// shows that account's limits and sends on it with no quit and reopen
|
|
// (zhibukaixinyao on Discord: Windows has no Restart button). One
|
|
// switched away and back before then is restarted all the same, which
|
|
// costs the app nothing it shows.
|
|
//
|
|
// They are listed at codexAppStep's next look, as a route change's are:
|
|
// every one on Codex's home, by the test a route change uses, and listed
|
|
// again at the look after when the listing fails. The pid the switch
|
|
// itself saw was the first app-server-looking process in a five-second
|
|
// listing, which was another program's (Miyi Helper's `--listen stdio://`
|
|
// one, listed ahead of the app's on the owner's Mac) or none when ps was
|
|
// slow, and then nothing was ended and the app stayed grey on the used-up
|
|
// account till the user restarted it (danzi002 on Discord).
|
|
func codexAppAccountSwitched() { codexAppSwitched(false, false) }
|
|
|
|
// 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() { codexAppSwitched(true, false) }
|
|
|
|
// codexAppSwitched notes a switch of Codex's account: spent, off one used
|
|
// up; byMagpie, made by magpie itself (SwitchWhenSpent). magpie's switch
|
|
// back restarts the app only with RestartApp on; its switch off a spent
|
|
// account, and one the user made among them, restart it all the same.
|
|
func codexAppSwitched(spent, byMagpie bool) {
|
|
c := &codexApp
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
c.auto = byMagpie && (!c.switched || c.auto)
|
|
c.switched, c.spent = true, c.spent || spent
|
|
}
|
|
|
|
// 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.
|
|
//
|
|
// A Codex request this magpie didn't serve is one the magpie before it
|
|
// may still be serving: a magpie that hands the gateway over (an update)
|
|
// lets the requests in flight finish in the old one, and the new one saw
|
|
// none of them. Its start counts as a Codex request ended then, so a
|
|
// reply streaming from the old one isn't cut short by the new one's
|
|
// first look.
|
|
func KeepCodexAppOnItsProvider(ctx context.Context, busy func() (int, time.Time)) {
|
|
started := time.Now()
|
|
held := func() (int, time.Time) {
|
|
n, last := busy()
|
|
if last.Before(started) {
|
|
last = started
|
|
}
|
|
return n, last
|
|
}
|
|
t := time.NewTicker(codexAppEvery)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case now := <-t.C:
|
|
codexAppStep(ctx, now, held)
|
|
}
|
|
}
|
|
}
|
|
|
|
// codexAppStep notes the app-servers running as Codex's route changes
|
|
// (magpie becomes Codex's provider for the used-up account, or stops being
|
|
// it; Codex is connected or disconnected), and ends each of them once no
|
|
// Codex turn is running. Started with magpie on, the app may
|
|
// have opened before: its app-servers are ended too. It says which pids it
|
|
// ended.
|
|
func codexAppStep(ctx context.Context, now time.Time, busy func() (int, time.Time)) []int {
|
|
codexAppCrashStep(ctx, now, busy)
|
|
codexProjectsStep(ctx)
|
|
if CodexAppRoute == nil {
|
|
return nil
|
|
}
|
|
route, out, known := CodexAppRoute()
|
|
if !known {
|
|
return nil
|
|
}
|
|
home := codexHome()
|
|
c := &codexApp
|
|
c.Lock()
|
|
switched := c.switched
|
|
wasOut := c.seen && c.out
|
|
c.Unlock()
|
|
// the takeover ending now: by the user's hand (CodexTakeoverOff), or
|
|
// by the reset, which is magpie's own change
|
|
offByHand := wasOut && !out && CodexTakeoverOff != nil && CodexTakeoverOff()
|
|
// 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
|
|
// magpie becoming Codex's provider for the used-up account: the
|
|
// app-servers are on that account, and a turn left open on it when
|
|
// quota ran out mid-turn would hold their restart, and the app's grey
|
|
// Send, for up to codexTurnWindow (xiaoq6285 on Discord)
|
|
into := changed && out && (!c.seen || !c.out)
|
|
// what the app works without: a switch among accounts with room,
|
|
// magpie's own switch back or one the user made, and the takeover out
|
|
// at the reset. The ChatGPT app shows the end of its app-server as an
|
|
// error and relaunches (#4550), and on its new account shows that
|
|
// account's projects, none for one that never had the app open (the
|
|
// app keeps them per ChatGPT identity, codex_projects.go): "switching
|
|
// the preferred account force-restarts the client, and the projects
|
|
// are lost" (Michael25651150 on X, after 15acba534). So that waits for
|
|
// the user to restart it unless they asked magpie to (RestartApp;
|
|
// atom0964 on Discord: both of Codex's sign-in ticks off, and magpie
|
|
// still restarted the app to switch accounts); the account rows say
|
|
// the app is still on the account before (CodexAppStale), with a
|
|
// Restart. A Connect or a Disconnect is the user's, and restarts it,
|
|
// and so does the takeover turned off by hand (CodexTakeoverOff). So
|
|
// does whatever leaves the app-server on a used-up account: the
|
|
// takeover in, the first look on one and a switch off one. The app
|
|
// greys Send for every model while that account's rate limit says not
|
|
// allowed and reads the account only as its app-server starts
|
|
// (8b0132b3d), so nothing magpie serves would reach it until a
|
|
// restart; an app-server left on an account that runs out later is
|
|
// restarted then (restartCodexAppLeftOnSpent).
|
|
byHand := c.seen && c.route != route && c.out == out || offByHand
|
|
needed := into || c.switched && c.spent
|
|
// left on the takeover: the app-servers it started while magpie was
|
|
// Codex's provider, which show no account till they restart
|
|
leftBack := false
|
|
if changed && !byHand && !needed && !codexRestartsApp() {
|
|
why := "magpie switched Codex's ChatGPT account back"
|
|
if c.switched && !c.auto {
|
|
why = "Codex's ChatGPT account was switched"
|
|
}
|
|
if !c.switched {
|
|
why = "magpie gave Codex back its ChatGPT account after the reset"
|
|
leftBack = c.out && !out
|
|
}
|
|
log.Printf("codex: %s; the Codex app's app-server is left running (Restart the Codex app to follow is off), and the app follows once it is restarted", why)
|
|
c.switched, c.auto = false, false
|
|
changed = false
|
|
}
|
|
if !changed {
|
|
c.seen, c.route, c.out = true, route, out
|
|
}
|
|
if changed {
|
|
// whatever restarts them now ends the takeover's too
|
|
c.back = nil
|
|
}
|
|
c.Unlock()
|
|
if leftBack {
|
|
lctx, cancel := context.WithTimeout(ctx, codexAppListWait)
|
|
pids, err := codexAppServersOn(lctx, home)
|
|
cancel()
|
|
if err == nil && len(pids) > 0 {
|
|
c.Lock()
|
|
c.back, c.backAt = map[int]bool{}, now
|
|
for _, p := range pids {
|
|
c.back[p] = true
|
|
}
|
|
c.Unlock()
|
|
}
|
|
} else {
|
|
checkCodexAppBack(ctx, now, home)
|
|
}
|
|
if !changed && !codexAppDeskStep(ctx, out || wasOut, home) {
|
|
return nil
|
|
}
|
|
if changed {
|
|
lctx, cancel := context.WithTimeout(ctx, codexAppListWait)
|
|
pids, err := codexAppServersOn(lctx, home)
|
|
cancel()
|
|
if err != nil {
|
|
// unknown: the change is taken up again at the next look
|
|
log.Printf("codex: listing the Codex app's app-servers: %v", err)
|
|
return nil
|
|
}
|
|
c.Lock()
|
|
switch {
|
|
case c.switched:
|
|
c.why = "Codex's ChatGPT account was switched"
|
|
case into:
|
|
c.why = "Codex's account is used up, so magpie serves it"
|
|
default:
|
|
c.why = "where Codex's config sends it changed"
|
|
}
|
|
c.spent = c.spent || into
|
|
c.seen, c.route, c.out, c.switched, c.auto = true, route, out, false, false
|
|
c.changedAt = now
|
|
c.stale, c.stuck = map[int]int{}, false
|
|
for _, p := range pids {
|
|
c.stale[p] = 0
|
|
}
|
|
c.Unlock()
|
|
}
|
|
c.Lock()
|
|
n := len(c.stale) + len(c.ended)
|
|
c.Unlock()
|
|
if n == 0 {
|
|
return nil
|
|
}
|
|
lctx, cancel := context.WithTimeout(ctx, codexAppListWait)
|
|
pids, err := codexAppServersOn(lctx, home)
|
|
cancel()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
running := map[int]bool{}
|
|
for _, p := range pids {
|
|
running[p] = true
|
|
}
|
|
c.Lock()
|
|
for p := range c.stale {
|
|
if !running[p] {
|
|
// 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)
|
|
endedAt := c.endedAt
|
|
c.Unlock()
|
|
if none {
|
|
// a Codex request through magpie since the end came from an
|
|
// app-server magpie didn't list: the app has one, and is never
|
|
// quit and opened under it
|
|
if inFlight, last := busy(); inFlight > 0 || last.After(endedAt) {
|
|
return nil
|
|
}
|
|
if codexAppGotNone(ctx) {
|
|
c.Lock()
|
|
c.stuck = true
|
|
c.Unlock()
|
|
}
|
|
return nil
|
|
}
|
|
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()
|
|
stale := make([]int, 0, len(c.stale))
|
|
for p := range c.stale {
|
|
stale = append(stale, p)
|
|
}
|
|
spent, changedAt := c.spent, c.changedAt
|
|
c.Unlock()
|
|
if codexTurnRunning(home, now, codexServersSince(stale)) {
|
|
// a turn Codex went on with through magpie after the change is
|
|
// served, by a routing group's fallback or magpie's takeover, and
|
|
// is not stuck however long a tool in it runs: only a turn that
|
|
// sent nothing since is (Norethcy on X: the reply a group's
|
|
// fallback served after the ChatGPT account ran out was cut short)
|
|
goneOn := last.After(changedAt.Add(codexAppIdle))
|
|
if !spent || goneOn || 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)
|
|
}
|
|
if CodexBeforeAppEnd != nil {
|
|
CodexBeforeAppEnd(home)
|
|
}
|
|
// a request Codex began while the turn was looked for, or the open
|
|
// chats moved, is in flight now: the end waits for the next look
|
|
if inFlight, again := busy(); inFlight > 0 || again.After(last) {
|
|
return nil
|
|
}
|
|
var ended []int
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
for p, tries := range c.stale {
|
|
if tries >= codexAppTries {
|
|
c.stuck = true
|
|
continue
|
|
}
|
|
c.stale[p] = tries + 1
|
|
if err := endCodexAppServer(p); err != nil {
|
|
log.Printf("codex: restarting the Codex app's app-server %d on its provider: %v", p, err)
|
|
if errors.Is(err, proc.ErrDenied) {
|
|
c.stale[p], c.stuck = codexAppTries, true
|
|
}
|
|
continue
|
|
}
|
|
ended = append(ended, p)
|
|
if c.ended == nil {
|
|
c.ended = map[int]bool{}
|
|
}
|
|
c.ended[p], c.backBy, c.endedAt = true, now.Add(codexAppBackWait), now
|
|
}
|
|
if len(ended) > 0 {
|
|
log.Printf("codex: ended the Codex app's app-server %v for the app to start another: %s", ended, c.why)
|
|
watchCodexAppCrashLocked(now)
|
|
c.crashSpent = c.crashSpent || spent
|
|
}
|
|
return ended
|
|
}
|
|
|
|
// codexAppDeskStep notes the Codex app's app-servers that started before
|
|
// magpie last named its list for the app (CodexAppDesk) as stale, once for
|
|
// each naming, and says whether a step should go on to end them. Not
|
|
// around the takeover (takeover: magpie Codex's provider for the used-up
|
|
// account now or until this look), where the list is taken out and named
|
|
// again as it ends and the app is left on the takeover's app-server
|
|
// (c.back) as the reset leaves it (#4550): that naming is noted and
|
|
// nothing ended for it. A start that can't be read is not counted.
|
|
func codexAppDeskStep(ctx context.Context, takeover bool, home string) bool {
|
|
if CodexAppDesk == nil {
|
|
return true
|
|
}
|
|
at := CodexAppDesk()
|
|
c := &codexApp
|
|
c.Lock()
|
|
fresh := !at.IsZero() && !at.Equal(c.desk)
|
|
if fresh && (takeover || c.back != nil) {
|
|
c.desk, fresh = at, false
|
|
}
|
|
c.Unlock()
|
|
if !fresh {
|
|
return true
|
|
}
|
|
lctx, cancel := context.WithTimeout(ctx, codexAppListWait)
|
|
pids, err := codexAppServersOn(lctx, home)
|
|
cancel()
|
|
if err != nil {
|
|
// unknown: taken up again at the next look
|
|
log.Printf("codex: listing the Codex app's app-servers: %v", err)
|
|
return false
|
|
}
|
|
var before []int
|
|
for _, p := range pids {
|
|
if t, err := codexServerStart(p); err == nil && !t.IsZero() && t.Before(at) {
|
|
before = append(before, p)
|
|
}
|
|
}
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
c.desk = at
|
|
if len(before) == 0 {
|
|
return true
|
|
}
|
|
c.why = "magpie's model list was named for the Codex app after its app-server started"
|
|
if c.stale == nil {
|
|
c.stale = map[int]int{}
|
|
}
|
|
for _, p := range before {
|
|
if _, ok := c.stale[p]; !ok {
|
|
c.stale[p] = 0
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// codexTurnOpen is whether a Codex session under home has a turn started
|
|
// and not ended (task_started after its last task_complete or
|
|
// turn_aborted) in a file written to within codexTurnWindow of now: the
|
|
// app-server running it would cut it short. A turn started before since,
|
|
// when the app-servers about to be ended started, ran in an app-server
|
|
// gone since (force quit, crashed): a SIGKILLed one writes no
|
|
// turn_aborted, and opening that thread again in the app writes
|
|
// thread_settings_applied, so the file stays in the window as long as
|
|
// the user looks at it. Counting it kept a switched account from ever
|
|
// reaching the app (蛋仔 on Discord, 10-29 and 10-30: "点了会话再切换账号
|
|
// 就重启不了"). A zero since counts every open turn.
|
|
func codexTurnOpen(home string, now, since time.Time) bool {
|
|
open := false
|
|
_ = filepath.WalkDir(filepath.Join(home, "sessions"), func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil || open {
|
|
if open {
|
|
return fs.SkipAll
|
|
}
|
|
return nil
|
|
}
|
|
if d.IsDir() || !strings.HasSuffix(d.Name(), ".jsonl") {
|
|
return nil
|
|
}
|
|
info, err := sessionFileInfo(path)
|
|
if err != nil || now.Sub(info.ModTime()) > codexTurnWindow {
|
|
return nil
|
|
}
|
|
open = sessionTurnOpen(path, since)
|
|
return nil
|
|
})
|
|
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 {
|
|
if quiet, ok := codexSessionsQuietListed(filepath.Join(home, "sessions"), now, d); ok {
|
|
return quiet
|
|
}
|
|
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 := sessionFileInfo(path)
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if now.Sub(info.ModTime()) < d {
|
|
quiet = false
|
|
return fs.SkipAll
|
|
}
|
|
return nil
|
|
})
|
|
return quiet && err == nil
|
|
}
|
|
|
|
// codexSessionsQuietListed is codexSessionsQuietFor read with one listing
|
|
// a folder (dirent.Each) where the system has one, not a listing and an
|
|
// lstat() a file: Codex idle runs it on every look while Codex is idle,
|
|
// over every session file of the history, and each walk allocated a
|
|
// listing, a path and an lstat's answer a file. Nothing is made for a
|
|
// file now, only a path for each folder. ok is false where a folder can't
|
|
// be read so (no bulk listing, Windows, an error), and the caller walks
|
|
// the folder itself.
|
|
func codexSessionsQuietListed(dir string, now time.Time, d time.Duration) (quiet, ok bool) {
|
|
if _, err := os.Lstat(dir); errors.Is(err, fs.ErrNotExist) {
|
|
return true, true
|
|
}
|
|
quiet = true
|
|
since := now.Add(-d).UnixNano()
|
|
var in func(dir string) bool
|
|
in = func(dir string) bool {
|
|
var sub []string // folders, read after this one's listing is done
|
|
if !dirent.Each(dir, func(name string, s dirent.Stat) bool {
|
|
switch {
|
|
case s.Mode == fs.ModeDir:
|
|
sub = append(sub, filepath.Join(dir, name))
|
|
case s.Mod > since && strings.HasSuffix(name, ".jsonl"):
|
|
quiet = false
|
|
}
|
|
return quiet
|
|
}) {
|
|
return false
|
|
}
|
|
for _, f := range sub {
|
|
if !quiet {
|
|
break
|
|
}
|
|
if !in(f) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
if !in(dir) {
|
|
return false, false
|
|
}
|
|
return quiet, true
|
|
}
|
|
|
|
// sessionFileInfo is the session file at path's own time and size, never
|
|
// the folder listing's (a fs.DirEntry's Info): Windows stamps a file in
|
|
// its folder only as it is closed, and Codex keeps a session's file open
|
|
// while the app-server runs it, so the listing has the time it was
|
|
// opened (os.ReadDir on the Windows box: a file held open and written
|
|
// to an hour after its stamp still listed the stamp, at its old size).
|
|
// A turn running in a thread opened more than codexTurnWindow before
|
|
// then counted as no turn, and its app-server was ended under it.
|
|
func sessionFileInfo(path string) (fs.FileInfo, error) { return os.Lstat(path) }
|
|
|
|
// sessionTurnTail is how much of a session file sessionTurnOpen reads at
|
|
// a time, from its end back.
|
|
const sessionTurnTail = 512 << 10
|
|
|
|
// sessionTurnMarks are the session lines that start and end a turn.
|
|
var (
|
|
sessionTurnStarted = []byte(`"type":"task_started"`)
|
|
sessionTurnEnds = [][]byte{[]byte(`"type":"task_complete"`), []byte(`"type":"turn_aborted"`)}
|
|
)
|
|
|
|
// sessionTurnOpen is whether the session file at path ends inside a turn
|
|
// started at since or later. A file that can't be read, or a turn whose
|
|
// start can't be read, counts as open: a turn cut short costs more than
|
|
// an app greyed a few seconds more.
|
|
//
|
|
// The file is read back from its end until the last line that starts or
|
|
// ends a turn. Only its last 512 KB were read, and a turn that had
|
|
// written more since it started (a tool's long output, an image, a turn
|
|
// going on for an hour: the owner's run 22 MB) read as no turn at all,
|
|
// so the app's app-server was ended under it as soon as Codex had sent
|
|
// nothing through magpie for codexAppIdle, while a tool ran between two
|
|
// of its requests (Norethcy on X: the turn a routing group's fallback
|
|
// served after the ChatGPT account ran out was cut short).
|
|
func sessionTurnOpen(path string, since time.Time) bool {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return !errors.Is(err, fs.ErrNotExist)
|
|
}
|
|
defer f.Close()
|
|
info, err := f.Stat()
|
|
if err != nil {
|
|
return true
|
|
}
|
|
// each read reaches into the next one's bytes by a mark's length less
|
|
// one, so a mark across the two is found whole
|
|
over := int64(len(sessionTurnEnds[0]))
|
|
buf := make([]byte, min(info.Size(), sessionTurnTail+over))
|
|
for end := info.Size(); end > 0; {
|
|
from := max(end-sessionTurnTail, 0)
|
|
b := buf[:min(end+over, info.Size())-from]
|
|
if _, err := f.ReadAt(b, from); err != nil && !errors.Is(err, io.EOF) {
|
|
return true
|
|
}
|
|
started := bytes.LastIndex(b, sessionTurnStarted)
|
|
ended := -1
|
|
for _, m := range sessionTurnEnds {
|
|
ended = max(ended, bytes.LastIndex(b, m))
|
|
}
|
|
switch {
|
|
case started < 0 && ended < 0:
|
|
end = from
|
|
continue
|
|
case started <= ended:
|
|
return false
|
|
case since.IsZero():
|
|
return true
|
|
}
|
|
at, ok := sessionTurnStartTime(f, from+int64(started))
|
|
return !ok || !at.Before(since.Add(-codexTurnSlack))
|
|
}
|
|
return false
|
|
}
|
|
|
|
// sessionTurnStartTime is the time of the line holding a turn's start at
|
|
// offset off of f, read with the bytes around it: the line may begin
|
|
// before the bytes sessionTurnOpen found it in, or end after them.
|
|
func sessionTurnStartTime(f *os.File, off int64) (time.Time, bool) {
|
|
const around = 16 << 10
|
|
from := max(off-around, 0)
|
|
b := make([]byte, off-from+around)
|
|
n, err := f.ReadAt(b, from)
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
|
return time.Time{}, false
|
|
}
|
|
return sessionLineTime(b[:n], int(off-from))
|
|
}
|
|
|
|
// sessionLineTime is the "timestamp" of the session file line holding
|
|
// offset i of b, which Codex writes on every rollout line.
|
|
func sessionLineTime(b []byte, i int) (time.Time, bool) {
|
|
line := b[bytes.LastIndexByte(b[:i], '\n')+1:]
|
|
if j := bytes.IndexByte(line, '\n'); j >= 0 {
|
|
line = line[:j]
|
|
}
|
|
var l struct {
|
|
Timestamp time.Time `json:"timestamp"`
|
|
}
|
|
if json.Unmarshal(line, &l) != nil || l.Timestamp.IsZero() {
|
|
return time.Time{}, false
|
|
}
|
|
return l.Timestamp, true
|
|
}
|
|
|
|
// codexAppGoneWait is how long RestartCodexApp waits for the app-servers
|
|
// it ended to be gone; a var for tests.
|
|
var codexAppGoneWait = 10 * time.Second
|
|
|
|
// RestartCodexApp restarts the app-server the Codex desktop app at bundle
|
|
// (/Applications/ChatGPT.app) runs for itself, as codexAppStep does: it is
|
|
// ended as a clean exit, and the app starts another, which reads Codex's
|
|
// config and model list afresh, while the app's window, threads and drafts
|
|
// 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 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 {
|
|
return err
|
|
}
|
|
in := strings.TrimSuffix(bundle, "/") + "/Contents/"
|
|
var ended []int
|
|
for _, p := range ps {
|
|
if !strings.HasPrefix(strings.TrimSpace(p.Args), in) || !isCodexAppOwnServer(p.Args) {
|
|
continue
|
|
}
|
|
if err := endCodexAppServer(p.PID); err != nil {
|
|
return err
|
|
}
|
|
ended = append(ended, p.PID)
|
|
watchCodexAppCrash(time.Now())
|
|
}
|
|
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 {
|
|
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 gone, false, nil
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return gone, false, ctx.Err()
|
|
case <-time.After(codexAppPoll):
|
|
}
|
|
}
|
|
}
|
|
|
|
// RestartCodexAppWhenIdle ends the Codex app's own app-servers once, for a magpie
|
|
// that won't be running to watch them (magpie uninstall): the app starts
|
|
// 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, 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)
|
|
pids, err := codexAppServersOn(lctx, home)
|
|
cancel()
|
|
if err != nil || len(pids) == 0 {
|
|
return 0, false, err
|
|
}
|
|
until := time.Now().Add(wait)
|
|
since := codexServersSince(pids)
|
|
for codexTurnRunning(home, time.Now(), since) {
|
|
if time.Now().After(until) {
|
|
return 0, true, nil
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return 0, true, ctx.Err()
|
|
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
|
|
}
|
|
gone = append(gone, p)
|
|
}
|
|
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
|
|
}
|
|
|
|
// What RestartSwitchedCodexApp found when it couldn't restart the app's
|
|
// app-server; the agent package says each in the reader's language.
|
|
var (
|
|
ErrCodexAppGone = errors.New("the Codex app isn't running")
|
|
ErrCodexAppNoEnd = errors.New("the Codex app's app-server couldn't be ended")
|
|
ErrCodexAppNotBack = errors.New("the Codex app didn't start another app-server")
|
|
)
|
|
|
|
// RestartSwitchedCodexApp is the Restart on Codex's accounts' "the Codex
|
|
// app is still signed in as" notice (nb5p_hQ on X): it restarts the app's
|
|
// own app-servers now, as codexAppStep does once no Codex turn runs, so
|
|
// the app reads the account Codex was switched to; the user said yes to
|
|
// cutting a running turn short. Each is ended as a clean exit, which the
|
|
// app answers by starting another (its window, threads and drafts stay;
|
|
// quitting the app and opening it again opened a new window, yoshi037070);
|
|
// it is never killed, as the app shows a kill as a crash. It waits until
|
|
// the ended ones are gone and a new one runs, and errs when either doesn't
|
|
// happen in time: a listing that fails is unknown, never success.
|
|
func RestartSwitchedCodexApp(ctx context.Context) error {
|
|
home := codexHome()
|
|
list := func() ([]int, error) {
|
|
lctx, cancel := context.WithTimeout(ctx, codexAppListWait)
|
|
defer cancel()
|
|
return codexAppServersOn(lctx, home)
|
|
}
|
|
pids, err := list()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(pids) == 0 {
|
|
DismissCodexApp()
|
|
return ErrCodexAppGone
|
|
}
|
|
for _, p := range pids {
|
|
if err := endCodexAppServer(p); err != nil {
|
|
log.Printf("codex: restarting the Codex app's app-server %d for the account switched to: %v", p, err)
|
|
return ErrCodexAppNoEnd
|
|
}
|
|
watchCodexAppCrash(time.Now())
|
|
}
|
|
// waitFor lists the app-servers until ok holds of them, for at most limit
|
|
waitFor := func(limit time.Duration, ok func([]int) bool) (bool, error) {
|
|
end := time.Now().Add(limit)
|
|
for {
|
|
now, err := list()
|
|
if err == nil && ok(now) {
|
|
return true, nil
|
|
}
|
|
if time.Now().After(end) {
|
|
return false, err
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return false, ctx.Err()
|
|
case <-time.After(200 * time.Millisecond):
|
|
}
|
|
}
|
|
}
|
|
gone, err := waitFor(codexAppGoneWait, func(now []int) bool {
|
|
return !slices.ContainsFunc(now, func(p int) bool { return slices.Contains(pids, p) })
|
|
})
|
|
if !gone {
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ErrCodexAppNoEnd
|
|
}
|
|
back, err := waitFor(codexAppBackWait, func(now []int) bool { return len(now) > 0 })
|
|
if !back {
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// 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()
|
|
codexApp.Lock()
|
|
codexApp.back = nil // the new one reads the account (CodexAppBack)
|
|
codexApp.Unlock()
|
|
return nil
|
|
}
|