yoshi037070 (Discord, 10-27): when magpie restarted Codex, a new Codex window opened. The switch paths (Connect, Disconnect, failover) already end only the app's own app-server (codexAppStep, proc.End: SIGTERM on macOS, exit 0 on Windows), and the app starts another and keeps its window. The Agents row's Restart beside a Codex app left on the old list did not: on macOS ReopenCodexApp ran osascript "quit" and then `open` on the bundle, and that open is what brought up a new window. Now ReopenCodexApp keeps its check (only a bundle the row lists as left on the old list) and calls provider.RestartCodexApp, which ends each process whose command line starts with <bundle>/Contents/ and is the app's own app-server (isCodexAppOwnServer: an app-server, not --listen), waits up to 10s for them to be gone, and errs when none ran. The app, its window and another program's --listen server are left alone. The confirm says only the app-server restarts and the window stays; the status says "Codex app-server restarted" (zh, zh-TW, ja, de). app.js stays under its budget, with no raise (1,310,458 of 1,310,460 after rebasing on97332140a): the "asked first" comment went and appReopen's shrank. The agent-wiring page says the same. The model picker: the app's restart recovery doesn't refetch its models query; it refetches on the next window focus once the list is over 5 minutes old (staleTime), or when the picker is mounted again. Run (rebased on origin/main, at this commit's tree): - go test ./internal/agent: ok (280s). - go test ./internal/provider -run 'Codex|Restart': ok. Before the rebase, the whole package with -skip TestUnreadLoginsWriteErrorsKeepCredentials (#3632 crashes the binary) was ok except TestFetchAsksASharedBaseOnce and its two siblings (#3854). - go test ./internal/gui -run TestCoreScriptBudget: ok. - GUI: agent-stale-copies in chromium and webkit, 5 languages, 980/440px: 80 pass. i18n-keys: its only red is #3897 ("Runs as"). Before the rebase codex-app-stale passed too. - gofmt -l clean; go vet darwin and windows for agent, provider, gui; linux for agent and provider (gui's linux vet needs wails' cgo here). - Real app: a clone of ChatGPT.app 26.1021.31345 (APFS clone in a sandbox HOME, so the bundle is unique to it) run under that HOME. The real RestartCodexApp ended its app-server 32922 in 3.1s; the same main process (31894) started app-server 34143, and its one window (2273016) stayed the only window. The user's own ChatGPT was untouched. Broken by hand: - reopenApp back to appQuit + appOpen: TestCodexAppReopen red ("did quit …, open …"). - isCodexAppServer in place of isCodexAppOwnServer: TestRestartCodexApp… red (ended [2 3], the --listen one too). - the bundle prefix dropped: red (ended [2 4], another bundle's). Not tried: the picker refreshing in a signed-in app (the sandbox app isn't signed in); Windows has no such Restart (the row's Restart for the Codex app is macOS only; Windows switches already end codex.exe with exit 0 and the app keeps its window,3aead9c62).
70 lines
2.4 KiB
Go
70 lines
2.4 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"runtime"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// The Codex app on macOS keeps running, its app-server with it, when its
|
|
// window is closed, so a change to Codex's model list reaches it only once
|
|
// its app-server starts again. The Agents row's Restart does that for the
|
|
// user (the owner, 10-23): it ends the app's own app-server, and the app
|
|
// starts another and keeps its window. Quitting the app and opening it
|
|
// again, as it did before, opened a new window (yoshi037070, 10-27).
|
|
|
|
// appQuit and appOpen ask macOS to quit a bundle and to open it; vars so
|
|
// tests don't touch a real app.
|
|
var (
|
|
appQuit = func(ctx context.Context, bundle string) error {
|
|
out, err := proc.CommandContext(ctx, "osascript", "-e", `tell application "`+strings.NewReplacer(`\`, `\\`, `"`, `\"`).Replace(bundle)+`" to quit`).CombinedOutput()
|
|
return cmdErr(out, err)
|
|
}
|
|
appOpen = func(ctx context.Context, bundle string) error {
|
|
out, err := proc.CommandContext(ctx, "open", bundle).CombinedOutput()
|
|
return cmdErr(out, err)
|
|
}
|
|
)
|
|
|
|
// appQuitWait is how long a quit app's processes have to end.
|
|
var appQuitWait = 30 * time.Second
|
|
|
|
func cmdErr(out []byte, err error) error {
|
|
if err != nil && len(strings.TrimSpace(string(out))) > 0 {
|
|
return errors.New(strings.TrimSpace(string(out)))
|
|
}
|
|
return err
|
|
}
|
|
|
|
// restartAppServer ends the app-server the app at a bundle runs for
|
|
// itself; a var so tests don't end a real one.
|
|
var restartAppServer = provider.RestartCodexApp
|
|
|
|
// ReopenCodexApp restarts the app-server of the Codex desktop app at
|
|
// bundle; the app starts a new one and its window stays. Only an app the
|
|
// Agents row lists as left on the old list is touched, never a path a
|
|
// request names otherwise.
|
|
func ReopenCodexApp(ctx context.Context, bundle string) error {
|
|
if runtime.GOOS != "darwin" {
|
|
return errors.New("only the macOS Codex app is restarted by magpie")
|
|
}
|
|
a, err := Find("codex")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return reopenApp(ctx, a, bundle)
|
|
}
|
|
|
|
func reopenApp(ctx context.Context, a *Agent, bundle string) error {
|
|
if bundle == "" || !slices.ContainsFunc(a.StaleCopies(), func(c StaleCopy) bool { return c.Kind == "app" && c.Bundle == bundle }) {
|
|
return errors.New("that Codex app has the current list already")
|
|
}
|
|
return restartAppServer(ctx, bundle)
|
|
}
|