Files
magpie/internal/agent/codex_app.go
T
yetone 46aa7e6b89 providers: the Codex app's switch notice has a Restart (nb5p_hQ on X)
After an account switch, Codex's accounts say the Codex app is still on
the account before. A user asked for a button that does the restart for
them. The notice now has Restart: magpie's own dialog asks first, then
magpie ends the app's own app-server (POST /api/codex/daemon/restart-app,
provider.RestartSwitchedCodexApp), waits for it to be gone and for the
app to start another, which reads auth.json and the new account. The
window stays, as the owner chose on 10-27 (quit and reopen opened a new
window). It ends the app-server now even mid-turn, after the confirm says
a running turn stops.

The app-server is ended as a clean exit (proc.End), never killed: the app
shows a killed app-server as a crash, so there is no force fallback. Each
failure says so in the notice, in the reader's language: the app isn't
running, the app-server couldn't be ended, the app didn't start another
within 20s, or the listing failed ("couldn't tell", not success).

Only the Codex desktop app gets the button (macOS ChatGPT.app/Codex.app,
the Windows Codex app). CLI agents in a terminal get none: restarting
them would lose the user's session. The text stays as it was.

app.js is 457 bytes smaller than main (comments cut), no budget raise.
The button and the confirm and four notices are in zh, zh-TW, ja and de.

Run:
- mote (darwin): gofmt -l clean; go vet agent, provider, gui on darwin
  and GOOS=windows; GOOS=linux go vet agent and provider (gui needs
  WebKitGTK headers, which neither mote nor magpie-desktop has).
- mote: go test provider -run 'TestRestartSwitchedCodexApp|TestCodexApp|
  TestRestartCodexApp' ok; agent TestRestartSwitchedCodexAppSays,
  TestEveryNotice ok; gui TestCoreScriptBudget etc. ok.
- win-run: the same provider tests ok, with real helper processes ended
  by proc.End (TerminateProcess) and a new one started.
- mote GUI: codex-app-stale + i18n-keys, chromium and webkit, en zh
  zh-TW ja de, 900 and 480px: 54/54.
- Broken by hand: TestRestartSwitchedCodexApp and ...Real go red when
  the wait for a new app-server always holds ("none back: <nil>").
- Real app: on mote, the real ChatGPT.app 26.1021 opened under the
  test's sandbox HOME (not signed in), its app-server 5232 ended, the
  app started 5901 within 3s, no diagnostic report; the app was quit.
  (A throwaway test, not committed.)

Not tried: a signed-in Codex app switching between two real accounts
(whether its window shows the new account's limits after this); the real
Windows Codex app; the Restart clicked in the real magpie GUI.
2026-10-30 12:35:18 +08:00

102 lines
3.9 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)
}
// What the Restart on Codex's "the Codex app is still signed in as" notice
// says when it didn't go through (tNotice).
var (
noticeSwitchAppGone = newNotice("The {agent} app isn't running any more. It opens on the new account.")
noticeSwitchAppNoEnd = newNotice("magpie couldn't restart the {agent} app's app-server. Quit the app and open it again.")
noticeSwitchAppNotBack = newNotice("The {agent} app's app-server ended, but the app didn't start another. Quit the app and open it again.")
noticeSwitchAppUnknown = newNotice("magpie couldn't tell whether the {agent} app restarted. Quit the app and open it again.")
)
// restartSwitchedApp restarts the Codex app's app-server; a var so tests
// don't end a real one.
var restartSwitchedApp = provider.RestartSwitchedCodexApp
// RestartSwitchedCodexApp restarts the Codex desktop app's app-server
// after Codex's account was switched, so the app is on the new account
// (provider.RestartSwitchedCodexApp), and says in the reader's language
// what went wrong when it didn't.
func RestartSwitchedCodexApp(ctx context.Context) error {
err := restartSwitchedApp(ctx)
switch {
case err == nil:
return nil
case errors.Is(err, provider.ErrCodexAppGone):
return errors.New(noticeSwitchAppGone.say("agent", "Codex"))
case errors.Is(err, provider.ErrCodexAppNoEnd):
return errors.New(noticeSwitchAppNoEnd.say("agent", "Codex"))
case errors.Is(err, provider.ErrCodexAppNotBack):
return errors.New(noticeSwitchAppNotBack.say("agent", "Codex"))
}
return errors.New(noticeSwitchAppUnknown.say("agent", "Codex"))
}