bitssly on X (tweet 2118722838249234609, v0.1.3177, Store Codex 26.1029.7803.0, app-server 0.166.0-alpha.6): when Codex's usage ran out the Codex app flickered, showed "Hosted AppServerManager identity is not ready" (fatal_error_broadcasted) and had to be quit and opened again, four times in a day. The page is the app's own bug after its app-server is ended (#4550), and magpie ends it on purpose when it becomes Codex's provider for the used-up account.4f624241freads the app's log for the page and reopens the app, but on Windows it never saw it: codexLogFiles kept only files whose fs.DirEntry Info() time was after the end, and on Windows that is the folder listing's time, which NTFS brings up to date only as a handle on the file is closed. The app keeps its day's log open, so the file the page went to listed hours old and was skipped. On the Windows box the Store app's own log listed 10:27Z (os.ReadDir and Get-ChildItem) while the app wrote to it at 15:22Z (os.Lstat, File.GetLastWriteTimeUtc). The same listing time judged Codex's session files, which the app-server holds open: codexTurnOpen skipped a turn running in a thread opened over 30 minutes before, and codexSessionsQuietFor counted such a session quiet, so on Windows the app-server could be ended under a running turn. - agent/codex_app_crash.go: codexLogFiles reads os.Lstat per file. - provider/codex_app_server.go: sessionFileInfo (os.Lstat) for codexTurnOpen and codexSessionsQuietFor; a file gone between the listing and the stat is skipped. - The other readers that judge a file another program may hold open by its listing time (sessions, claude_out_soon, subscription_usage, claude_subscription's idle sweep) are #5549. magpie's first look after its own restart ending an app-server that already started under the takeover (one of bitssly's four pages came a minute after an auto-update) is #5550. Run: - Windows box (win-run), Store Codex 26.1029.7803.0 installed there: the two new tests red on the old code (TestCodexAppCrashedReadsAFileTheAppHoldsOpen: crashed false; TestCodexSessionHeldOpenReadAtItsOwnTime: "not open"), green with the change; go test -run 'TestCodexApp|TestCodexSession|TestCodexTurn' ./internal/agent ./internal/provider ok. A Go probe on the box read the live Store log at listing 15:22:00Z, own 15:32:02Z. - Broken by hand on Windows: codexSessionsQuietFor back on e.Info() makes TestCodexSessionHeldOpenReadAtItsOwnTime red ("quiet"); the test checks quiet before the turn, since reading the file closes a handle and Windows then brings the listing up to date. - mote at this head on origin/main80ae904c2: go test ./... -count=1 -timeout 45m: 86 packages ok, red only in internal/gui TestMainWindowCorners (#3949, open, AppKit on mote's macOS 27). - Rebased onto origin/mainf9a88c4fb(1fd0bb967update,f9a88c4fbwsl touched agent and provider): at this head go test ./internal/agent ./internal/provider ./internal/netproxy ./internal/update ./internal/plugin ./internal/pii ok on mote, GOOS=windows go vet of agent and provider ok. - go vet ./... (darwin), GOOS=windows go vet ./... ok; GOOS=linux go vet ./internal/agent ./internal/provider ok; gofmt -l clean. - On macOS and Linux the listing already carries the file's own time, so the tests pass there on old and new code alike. Not tried: a real used-up ChatGPT account reaching the page and magpie reopening the Store app (the reopen itself is #4550's, unchanged).
206 lines
6.4 KiB
Go
206 lines
6.4 KiB
Go
package agent
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// The Codex app's own logs, read for the page it shows when it can't go on
|
|
// after magpie restarted its app-server (provider.CodexAppCrashed, see
|
|
// provider/codex_app_crash.go). The app writes them as its file logger
|
|
// does in 26.1021 (file-based-logger: macOS ~/Library/Logs/<bundle id>,
|
|
// Windows %LOCALAPPDATA%\Codex\Logs, Linux $XDG_STATE_HOME/codex/logs,
|
|
// each with a YYYY/MM/DD folder for the UTC day a file was opened), one
|
|
// line per entry: `<RFC 3339 UTC> <level> [<scope>] <event> <key=value…>`.
|
|
// The Store app on Windows writes the same folder under its package's
|
|
// LocalCache (seen on the Windows box).
|
|
|
|
// codexLogEnv reads the environment for the log folders; a var for tests.
|
|
var codexLogEnv = os.Getenv
|
|
|
|
// codexAppLogRoots are the folders the Codex app writes its logs in here.
|
|
func codexAppLogRoots() []string {
|
|
home := codexLogEnv("HOME")
|
|
if home == "" {
|
|
home, _ = os.UserHomeDir()
|
|
}
|
|
switch {
|
|
case restartOnMac:
|
|
return []string{filepath.Join(home, "Library", "Logs", desktopApps["codex"].id)}
|
|
case restartOnWindows:
|
|
local := codexLogEnv("LOCALAPPDATA")
|
|
if local == "" {
|
|
local = filepath.Join(home, "AppData", "Local")
|
|
}
|
|
return []string{
|
|
filepath.Join(local, "Codex", "Logs"),
|
|
filepath.Join(local, "Packages", "OpenAI.Codex_2p2nqsd0c76g0", "LocalCache", "Local", "Codex", "Logs"),
|
|
}
|
|
case restartOnLinux:
|
|
state := codexLogEnv("XDG_STATE_HOME")
|
|
if state == "" {
|
|
state = filepath.Join(home, ".local", "state")
|
|
}
|
|
return []string{filepath.Join(state, "codex", "logs")}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// codexLogDays is how many of a root's newest day folders are read: a file
|
|
// stays in the day it was opened in while the app writes on to it.
|
|
const codexLogDays = 7
|
|
|
|
// codexLogTail is how much of the end of a log file is read.
|
|
const codexLogTail = 512 << 10
|
|
|
|
// codexFatalLine is what the app's AppServerConnection logs as it shows the
|
|
// page (broadcastFatalError).
|
|
const codexFatalLine = " [AppServerConnection] fatal_error_broadcasted"
|
|
|
|
// codexAppCrashed is provider.CodexAppCrashed: the first page the app
|
|
// logged at or after since (less a second for the clock's grain), read in
|
|
// the files written to since then. known is false when no root can be
|
|
// read; a root that isn't there is the app never having run on it.
|
|
func codexAppCrashed(since time.Time) (at time.Time, crashed, known bool) {
|
|
from := since.Add(-time.Second)
|
|
for _, root := range codexAppLogRoots() {
|
|
files, ok := codexLogFiles(root, from)
|
|
known = known || ok
|
|
for _, f := range files {
|
|
if t, ok := codexFatalIn(f, from); ok && (!crashed || t.Before(at)) {
|
|
at, crashed = t, true
|
|
}
|
|
}
|
|
}
|
|
return at, crashed, known
|
|
}
|
|
|
|
// codexLogFiles are the .log files under root's newest day folders written
|
|
// to at or after from; ok is false when root can't be read.
|
|
func codexLogFiles(root string, from time.Time) (files []string, ok bool) {
|
|
if _, err := os.ReadDir(root); errors.Is(err, os.ErrNotExist) {
|
|
return nil, true
|
|
} else if err != nil {
|
|
return nil, false
|
|
}
|
|
var days []string
|
|
for _, y := range codexLogDirs(root) {
|
|
for _, m := range codexLogDirs(y) {
|
|
days = append(days, codexLogDirs(m)...)
|
|
}
|
|
}
|
|
slices.Sort(days)
|
|
if len(days) > codexLogDays {
|
|
days = days[len(days)-codexLogDays:]
|
|
}
|
|
for _, d := range days {
|
|
es, _ := os.ReadDir(d)
|
|
for _, e := range es {
|
|
if e.IsDir() || !strings.HasSuffix(e.Name(), ".log") {
|
|
continue
|
|
}
|
|
// the file's own time, never the folder listing's: Windows
|
|
// stamps a file in its folder only as it is closed, and the
|
|
// app keeps a day's file open (on the Windows box the
|
|
// listing said 10:27Z while the app wrote on at 15:22Z), so
|
|
// the file being written to read as older than from and its
|
|
// page was never seen (bitssly on X)
|
|
p := filepath.Join(d, e.Name())
|
|
if info, err := os.Lstat(p); err == nil && !info.ModTime().Before(from) {
|
|
files = append(files, p)
|
|
}
|
|
}
|
|
}
|
|
return files, true
|
|
}
|
|
|
|
// codexLogDirs are dir's folders named by digits alone (a year, month or
|
|
// day), in full.
|
|
func codexLogDirs(dir string) []string {
|
|
es, _ := os.ReadDir(dir)
|
|
var out []string
|
|
for _, e := range es {
|
|
if e.IsDir() && e.Name() != "" && strings.Trim(e.Name(), "0123456789") == "" {
|
|
out = append(out, filepath.Join(dir, e.Name()))
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// codexFatalIn is the time of the first page logged in the file at path
|
|
// at or after from, read in its last codexLogTail bytes. A line the app
|
|
// is still writing (no newline yet) and the part line the tail cuts into
|
|
// are left out.
|
|
func codexFatalIn(path string, from time.Time) (time.Time, bool) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return time.Time{}, false
|
|
}
|
|
defer f.Close()
|
|
info, err := f.Stat()
|
|
if err != nil {
|
|
return time.Time{}, false
|
|
}
|
|
off := max(info.Size()-codexLogTail, 0)
|
|
b, err := io.ReadAll(io.NewSectionReader(f, off, info.Size()-off))
|
|
if err != nil {
|
|
return time.Time{}, false
|
|
}
|
|
if off > 0 {
|
|
i := bytes.IndexByte(b, '\n')
|
|
if i < 0 {
|
|
return time.Time{}, false
|
|
}
|
|
b = b[i+1:]
|
|
}
|
|
if i := bytes.LastIndexByte(b, '\n'); i >= 0 {
|
|
b = b[:i+1]
|
|
} else {
|
|
return time.Time{}, false
|
|
}
|
|
for line := range bytes.Lines(b) {
|
|
if !bytes.Contains(line, []byte(codexFatalLine)) {
|
|
continue
|
|
}
|
|
stamp, _, _ := bytes.Cut(line, []byte(" "))
|
|
t, err := time.Parse(time.RFC3339Nano, string(stamp))
|
|
if err == nil && !t.Before(from) {
|
|
return t, true
|
|
}
|
|
}
|
|
return time.Time{}, false
|
|
}
|
|
|
|
// codexAppRelaunch is provider.CodexAppRelaunch: it quits the Codex app
|
|
// and opens it again, as the Agents row's Restart does for an app: on
|
|
// macOS ChatGPT.app (com.openai.codex) asked to quit as ⌘Q does and opened
|
|
// once every process of its bundle has ended, never forced (an app still
|
|
// open then is left, and the row says to quit it); on Windows the Store
|
|
// app's windows closed, its tree ended when it stays in the tray, and
|
|
// opened again (codexAppReopen). Nothing is done for an app not running:
|
|
// it was quit meanwhile. Linux isn't reopened.
|
|
func codexAppRelaunch(ctx context.Context) error {
|
|
switch {
|
|
case restartOnWindows:
|
|
return codexAppReopen(ctx)
|
|
case restartOnMac:
|
|
c, ok := findRestartable(restartable{mac: desktopApps["codex"]})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
a, err := Find("codex")
|
|
if err != nil {
|
|
a = &Agent{ID: "codex", Name: "Codex"}
|
|
}
|
|
return reopenBundle(ctx, a, c.bundle, c.exe)
|
|
}
|
|
return errors.New("magpie reopens the Codex app on macOS and Windows only")
|
|
}
|