diff --git a/internal/agent/apprestart.go b/internal/agent/apprestart.go index cf569bfc7..6ea3fb0fb 100644 --- a/internal/agent/apprestart.go +++ b/internal/agent/apprestart.go @@ -69,11 +69,12 @@ func winHas(image, part string) bool { return strings.Contains(strings.ToLower(image), strings.ToLower(part)) } -// restartOnWindows and restartOnMac say which system this is; vars so a -// test runs either. +// restartOnWindows, restartOnMac and restartOnLinux say which system this +// is; vars so a test runs any. var ( restartOnWindows = runtime.GOOS == "windows" restartOnMac = runtime.GOOS == "darwin" + restartOnLinux = runtime.GOOS == "linux" ) // winProc is a process running on Windows: its id, its parent's, its diff --git a/internal/agent/apprestart_test.go b/internal/agent/apprestart_test.go index 0aae28083..6dab350cf 100644 --- a/internal/agent/apprestart_test.go +++ b/internal/agent/apprestart_test.go @@ -33,17 +33,17 @@ func restartSandbox(t *testing.T) string { os.MkdirAll(filepath.Join(home, ".workbuddy"), 0o755) os.WriteFile(filepath.Join(home, ".workbuddy", "models.json"), []byte(`[]`), 0o600) notOnMac(t) - wasWin, wasMac, wasFolders, wasRunning := restartOnWindows, restartOnMac, appFolders, running + wasWin, wasMac, wasLinux, wasFolders, wasRunning := restartOnWindows, restartOnMac, restartOnLinux, appFolders, running wasQuit, wasOpen, wasQuitWait := appQuit, appOpen, appQuitWait wasProcs, wasClose, wasEnd, wasLaunch, wasCloseWait, wasBackWait, wasPoll := winProcs, winClose, winEnd, winLaunch, appCloseWait, appBackWait, appPoll wasShown, wasGrace := winShown, appTrayGrace t.Cleanup(func() { - restartOnWindows, restartOnMac, appFolders, running = wasWin, wasMac, wasFolders, wasRunning + restartOnWindows, restartOnMac, restartOnLinux, appFolders, running = wasWin, wasMac, wasLinux, wasFolders, wasRunning appQuit, appOpen, appQuitWait = wasQuit, wasOpen, wasQuitWait winProcs, winClose, winEnd, winLaunch, appCloseWait, appBackWait, appPoll = wasProcs, wasClose, wasEnd, wasLaunch, wasCloseWait, wasBackWait, wasPoll winShown, appTrayGrace = wasShown, wasGrace }) - restartOnWindows, restartOnMac = false, false + restartOnWindows, restartOnMac, restartOnLinux = false, false, false appPoll = 5 * time.Millisecond winShown = func([]int) bool { return true } return home diff --git a/internal/agent/codex_app.go b/internal/agent/codex_app.go index 3ca4c10d6..baaaead2e 100644 --- a/internal/agent/codex_app.go +++ b/internal/agent/codex_app.go @@ -3,6 +3,7 @@ package agent import ( "context" "errors" + "regexp" "runtime" "slices" "strings" @@ -113,9 +114,14 @@ const codexWinAUMID = "OpenAI.Codex_2p2nqsd0c76g0!App" // codexAppOpen is provider.CodexAppOpen: whether the Codex desktop app // runs here, ChatGPT.app (com.openai.codex) on macOS, the Store app on -// Windows; not known elsewhere. +// Windows, OpenAI's package's /usr/lib/chatgpt/ChatGPT on Linux (any of +// its processes: Electron's helpers run the same executable); not known +// elsewhere. func codexAppOpen(context.Context) (open, known bool) { switch { + case restartOnLinux: + app := desktopApps["codex"] + return len(running("^"+regexp.QuoteMeta(app.linux+"/ChatGPT")+"( |$)")) > 0, true case restartOnWindows: _, ok := findRestartable(codexWinApp) return ok, true diff --git a/internal/agent/codex_async.go b/internal/agent/codex_async.go index dc1d095ba..54c03b402 100644 --- a/internal/agent/codex_async.go +++ b/internal/agent/codex_async.go @@ -59,7 +59,9 @@ type CodexReader struct { // codexReaders are the Codexes that read the config at at: the CLI on // PATH, the desktop app's own (its bundled codex-cli on a Mac; the Store -// app's on Windows, whose version magpie can't read), the editor +// app's on Windows, whose version magpie can't read; on Linux the +// package's resources/codex, which says its version as the CLI does: +// codex-cli 0.166.0-alpha.5.1 in chatgpt 26.1027), the editor // extension's, or a WSL distro's CLI. A var so tests can name them. var codexReaders = func(at place) []CodexReader { if at.spell != nil { @@ -82,6 +84,10 @@ var codexReaders = func(at place) []CodexReader { if storeAppVersion(windowsStorePackages(), app.store, app.publisher) != "" { out = append(out, CodexReader{Kind: "app"}) } + case "linux": + if dir := app.linuxDir(); dir != "" { + out = append(out, CodexReader{Kind: "app", Version: installedVersion(filepath.Join(dir, "resources", "codex"))}) + } } if extensionHere(at.home, "openai.chatgpt") { out = append(out, CodexReader{Kind: "ide"}) diff --git a/internal/agent/codex_linux_app_test.go b/internal/agent/codex_linux_app_test.go new file mode 100644 index 000000000..311747a89 --- /dev/null +++ b/internal/agent/codex_linux_app_test.go @@ -0,0 +1,160 @@ +package agent + +import ( + "context" + "os" + "path/filepath" + "regexp" + "runtime" + "slices" + "testing" + + "github.com/yetone/magpie/internal/proc" +) + +// linuxCodexMetadata is resources/linux-package-metadata.json as OpenAI's +// chatgpt 26.1027.30824 deb installs it (seen in the WSL distro +// magpie-desktop, Ubuntu 24.04). +const linuxCodexMetadata = `{ + "codexAppBrand": "chatgpt", + "codexBuildFlavor": "prod", + "version": "26.1027.30824" +} +` + +// installLinuxCodexApp puts OpenAI's Linux package's files magpie reads +// under the sandbox's /usr/lib/chatgpt, and says where. +func installLinuxCodexApp(t *testing.T) string { + t.Helper() + dir := desktopApps["codex"].linuxDir() + if dir != "" { + t.Fatalf("an empty sandbox has the Linux Codex app at %s", dir) + } + app := desktopApps["codex"] + if app.linux == "" { + t.Fatal("no Linux folder for the Codex app") + } + dir = proc.SystemDirs(app.linux)[0] + res := filepath.Join(dir, "resources") + if err := os.MkdirAll(res, 0o755); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { os.RemoveAll(dir) }) + if err := os.WriteFile(filepath.Join(res, "linux-package-metadata.json"), []byte(linuxCodexMetadata), 0o644); err != nil { + t.Fatal(err) + } + return dir +} + +// pota on Discord: magpie had no account sign-in or switch for the +// ChatGPT desktop app on Linux, which macOS and Windows have. OpenAI's +// Linux package (chatgpt, in preview since 08-11) installs the app to +// /usr/lib/chatgpt; the Agents row shows its version, from the package's +// metadata, with or without a CLI, and Codex's folder isn't a leftover +// while it is installed. +func TestCodexLinuxAppVersion(t *testing.T) { + was, wasStore := appFolders, windowsStorePackages + appFolders = func() []string { return nil } + windowsStorePackages = func() []string { return nil } + t.Cleanup(func() { appFolders, windowsStorePackages = was, wasStore }) + t.Setenv("PATH", t.TempDir()) // no codex CLI + codex := &Agent{ID: "codex", Name: "Codex", Bin: "codex"} + if v := codex.AppVersion(); v != "" || codex.HasApp() { + t.Fatalf("no app installed: %q, %v", v, codex.HasApp()) + } + + dir := installLinuxCodexApp(t) + if v := codex.AppVersion(); v != "26.1027.30824" { + t.Fatalf("the Linux package's version: %q", v) + } + if !codex.HasApp() { + t.Fatal("the Linux app not installed") + } + if c, ok := codex.CLI(); !ok || c.App != "26.1027.30824" { + t.Fatalf("the app alone, no CLI: %+v %v", c, ok) + } + + // metadata that doesn't say: installed, no version + if err := os.WriteFile(filepath.Join(dir, "resources", "linux-package-metadata.json"), []byte(`{"codexAppBrand":"chatgpt"}`), 0o644); err != nil { + t.Fatal(err) + } + if v := codex.AppVersion(); v != "" || !codex.HasApp() { + t.Fatalf("metadata without a version: %q, %v", v, codex.HasApp()) + } + // Codex's twin in WSL has none of this machine's + if (&Agent{ID: "codex@wsl:Ubuntu", WSL: "Ubuntu", Bin: "codex"}).HasApp() { + t.Fatal("WSL twin") + } +} + +// After a switch magpie restarts the app's app-server, and when the app +// starts none it says to quit and open it again, unless the app was quit +// meanwhile: whether it is open is known on Linux too, from any process of +// /usr/lib/chatgpt/ChatGPT (the main one and Electron's helpers, as ps +// listed them for 26.1027), never from its codex alone or another +// program's ChatGPT. +func TestCodexLinuxAppOpen(t *testing.T) { + restartSandbox(t) + var cmds []string + running = func(pat string) []runningProc { + re := regexp.MustCompile(pat) + var out []runningProc + for _, c := range cmds { + if re.MatchString(c) { + out = append(out, runningProc{cmd: c}) + } + } + return out + } + if _, known := codexAppOpen(context.Background()); known { + t.Fatal("known on no system") + } + restartOnLinux = true + for _, tc := range []struct { + cmds []string + open bool + }{ + {[]string{"/usr/lib/chatgpt/ChatGPT", "/usr/lib/chatgpt/ChatGPT --type=zygote --no-zygote-sandbox", codexLinuxAppServerCmd}, true}, + {[]string{"/usr/lib/chatgpt/ChatGPT --type=renderer"}, true}, + {nil, false}, + {[]string{codexLinuxAppServerCmd, "/usr/lib/chatgpt/chrome_crashpad_handler --monitor-self", "/opt/ChatGPT/ChatGPT", "/usr/lib/chatgpt/ChatGPTx"}, false}, + } { + cmds = tc.cmds + if open, known := codexAppOpen(context.Background()); open != tc.open || !known { + t.Errorf("%q: open %v, known %v; want %v", tc.cmds, open, known, tc.open) + } + } +} + +const codexLinuxAppServerCmd = "/usr/lib/chatgpt/resources/codex -c features.code_mode_host=true app-server --analytics-default-enabled" + +// The Linux app's codex reads Codex's config as the CLI does: it is one +// of the readers whose version decides what magpie writes there, read +// from its --version (codex-cli 0.166.0-alpha.5.1 in chatgpt 26.1027). +func TestCodexLinuxAppReadsTheConfig(t *testing.T) { + if runtime.GOOS != "linux" { + t.Skip("the Linux package is read on Linux only") + } + t.Setenv("PATH", t.TempDir()) // no codex CLI + home := t.TempDir() + if got := codexReaders(place{home: home}); slices.ContainsFunc(got, func(r CodexReader) bool { return r.Kind == "app" }) { + t.Fatalf("no app installed: %+v", got) + } + dir := installLinuxCodexApp(t) + bin := filepath.Join(dir, "resources", "codex") + if err := os.WriteFile(bin, []byte("#!/bin/sh\necho codex-cli 0.166.0-alpha.5.1\n"), 0o755); err != nil { + t.Fatal(err) + } + wasRun := runVersion + runVersion = func(b string) string { + if b != bin { + t.Errorf("version read from %s, want %s", b, bin) + } + return "codex-cli 0.166.0-alpha.5.1" + } + t.Cleanup(func() { runVersion = wasRun }) + got := codexReaders(place{home: home}) + if !slices.Contains(got, CodexReader{Kind: "app", Version: "0.166.0-alpha.5.1"}) { + t.Fatalf("the Linux app's codex not read: %+v", got) + } +} diff --git a/internal/agent/desktopapp.go b/internal/agent/desktopapp.go index b0e5f1eab..3dcdd0a8c 100644 --- a/internal/agent/desktopapp.go +++ b/internal/agent/desktopapp.go @@ -32,7 +32,8 @@ import ( // (HKCU\Software\'s InstallLocation, and its Uninstall entry's // DisplayVersion): its installer lets the user pick another folder. more // are the app's other builds, each under a bundle id of its own (CodeBuddy's -// China build beside its international one). +// China build beside its international one). linux is the folder the +// vendor's own Linux packages install the app to. type desktopApp struct { id string names []string @@ -40,11 +41,18 @@ type desktopApp struct { store string publisher string guid string + linux string more []desktopApp } var desktopApps = map[string]desktopApp{ - "codex": {id: "com.openai.codex", names: []string{"ChatGPT.app", "Codex.app"}, store: "OpenAI.Codex", publisher: "2p2nqsd0c76g0"}, + // On Linux OpenAI's chatgpt package (a deb, an rpm and an Arch + // package, in preview since 08-11) installs the app to /usr/lib/chatgpt: + // ChatGPT, with its codex in resources and its version in + // resources/linux-package-metadata.json (26.1027.30824's deb, seen in + // the WSL distro magpie-desktop). Its app-server reads ~/.codex, as the + // CLI does (pota on Discord: no account switch for it on Linux). + "codex": {id: "com.openai.codex", names: []string{"ChatGPT.app", "Codex.app"}, store: "OpenAI.Codex", publisher: "2p2nqsd0c76g0", linux: "/usr/lib/chatgpt"}, // DeepSeek Harness Desktop (0.2.0-rc.2's DeepSeek Harness.app, // com.deepseek.dsh) reads ~/.dsh as the CLI does — its own profile, // profiles/desktop, beside the CLI's — and needs no CLI: its terminal @@ -336,11 +344,48 @@ func (app desktopApp) version() string { return strings.TrimSpace(string(v[1])) } if app.store != "" { - return storeAppVersion(windowsStorePackages(), app.store, app.publisher) + if v := storeAppVersion(windowsStorePackages(), app.store, app.publisher); v != "" { + return v + } } if dir, v := app.registered(); dir != "" { return v } + return app.linuxVersion() +} + +// linuxDir is the folder the app's Linux package installed it to, "" +// when it isn't there. Through proc.SystemDirs, so a test binary testenv +// isolated never reads the machine's own /usr/lib. +func (app desktopApp) linuxDir() string { + if app.linux == "" { + return "" + } + dir := proc.SystemDirs(app.linux)[0] + if !isFile(filepath.Join(dir, "resources", "linux-package-metadata.json")) { + return "" + } + return dir +} + +var linuxPackageVersionRe = regexp.MustCompile(`"version"\s*:\s*"([^"]+)"`) + +// linuxVersion is the version of the app's Linux package, from its +// resources/linux-package-metadata.json ({"codexAppBrand":"chatgpt", +// "codexBuildFlavor":"prod","version":"26.1027.30824"}); "" when it isn't +// installed or doesn't say. +func (app desktopApp) linuxVersion() string { + dir := app.linuxDir() + if dir == "" { + return "" + } + b, err := os.ReadFile(filepath.Join(dir, "resources", "linux-package-metadata.json")) + if err != nil { + return "" + } + if m := linuxPackageVersionRe.FindSubmatch(b); m != nil { + return strings.TrimSpace(string(m[1])) + } return "" } @@ -397,7 +442,8 @@ func storeAppVersion(packages []string, name, publisher string) string { // HasApp reports whether the agent's desktop app is installed here: its // bundle on macOS (AppVersion), its executable on Windows: under -// %LOCALAPPDATA%\Programs, or in the folder its installer registered. +// %LOCALAPPDATA%\Programs, or in the folder its installer registered; on +// Linux the folder its package installed (desktopApp.linux). func (a *Agent) HasApp() bool { app, ok := desktopApps[a.ID] return ok && a.WSL == "" && slices.ContainsFunc(app.builds(), desktopApp.installed) @@ -405,7 +451,7 @@ func (a *Agent) HasApp() bool { // installed reports whether app is installed on this machine (see HasApp). func (app desktopApp) installed() bool { - if app.version() != "" { + if app.version() != "" || app.linuxDir() != "" { return true } if app.win == "" { diff --git a/internal/provider/codex_app_server_test.go b/internal/provider/codex_app_server_test.go index 4c42813e7..adfcb8506 100644 --- a/internal/provider/codex_app_server_test.go +++ b/internal/provider/codex_app_server_test.go @@ -500,6 +500,11 @@ func TestCodexOutUntil(t *testing.T) { // codexWindowsAppServer is the Windows Codex app's own app-server. const codexWindowsAppServer = `C:\Users\yeton\AppData\Local\OpenAI\Codex\bin\2993767bd1dfb009\codex.exe -c features.code_mode_host=true app-server --analytics-default-enabled -c plugins.codex-app-tools@openai-bundled.mcp_servers.codex_app.enabled=true` +// codexLinuxAppServer is the Linux Codex app's own app-server, as the +// official chatgpt 26.1027 deb ran it in the WSL distro magpie-desktop +// (Ubuntu 24.04). +const codexLinuxAppServer = "/usr/lib/chatgpt/resources/codex -c features.code_mode_host=true app-server --analytics-default-enabled -c plugins.codex-app-tools@openai-bundled.mcp_servers.codex_app.enabled=true" + // Only the app's own app-server is restarted: another program's, started // from the app's bundled codex with --listen (Miyi Helper's, seen on the // owner's Mac), is that program's. @@ -510,6 +515,7 @@ func TestCodexAppOwnServer(t *testing.T) { // as Windows listed it on the Windows box: run from LOCALAPPDATA, // not from its package (zhibukaixinyao on Discord) codexWindowsAppServer, + codexLinuxAppServer, } { if !isCodexAppOwnServer(own) { t.Errorf("the app's own app-server not taken: %s", own) @@ -523,6 +529,8 @@ func TestCodexAppOwnServer(t *testing.T) { "/Applications/ChatGPT.app/Contents/Resources/codex-cli/CodexCLI.app/Contents/MacOS/codex app-server --listen=ws://127.0.0.1:4500", "/Applications/ChatGPT.app/Contents/Resources/codex-cli/CodexCLI.app/Contents/MacOS/codex -c forced_login_method=\"chatgpt\" exec-server --remote https://codex-cloud-environments.chatgpt.com/api", "/usr/local/bin/codex app-server", + "/usr/lib/chatgpt/resources/codex app-server --listen stdio://", + "/usr/lib/chatgpt/resources/codex exec-server --remote x", } { if isCodexAppOwnServer(other) { t.Errorf("taken for the app's own app-server: %s", other) diff --git a/internal/provider/codex_daemon.go b/internal/provider/codex_daemon.go index bb58a86f0..6133b8a5d 100644 --- a/internal/provider/codex_daemon.go +++ b/internal/provider/codex_daemon.go @@ -291,7 +291,10 @@ func DismissCodexApp() { // on Windows the Store app's (OpenAI.Codex, ChatGPT.exe), which copies its // codex out of WindowsApps and runs it from %LOCALAPPDATA%: // `C:\Users\…\AppData\Local\OpenAI\Codex\bin\\codex.exe -c … app-server -// --analytics-default-enabled …` (26.1021 on the Windows box). Not the +// --analytics-default-enabled …` (26.1021 on the Windows box); on Linux +// the official app's (the chatgpt deb, rpm or Arch package, 26.1027): +// `/usr/lib/chatgpt/resources/codex -c … app-server +// --analytics-default-enabled …`. Not the // managed daemon, nor an app-server's helpers or proxy, nor an editor's // codex. func findCodexApp(ps []proc.Process) int { @@ -312,7 +315,7 @@ func isCodexAppServer(args string) bool { // the program's own path: ps leaves a path's spaces unquoted, so // "…/Codex Computer Use.app/Contents/MacOS/…" reads as one named Codex exe, rest := splitExe(args) - if !strings.Contains(exe, ".app/Contents/") && !windowsCodexApp(exe) { + if !strings.Contains(exe, ".app/Contents/") && !windowsCodexApp(exe) && !linuxCodexApp(exe) { return false } if base := strings.ToLower(exe[strings.LastIndexAny(exe, `/\`)+1:]); base != "codex" && base != "codex.exe" { @@ -331,6 +334,15 @@ func windowsCodexApp(exe string) bool { return strings.Contains(e, `\appdata\local\openai\codex\bin\`) || strings.Contains(e, `\windowsapps\openai.`) } +// linuxCodexApp is whether exe is the Linux Codex app's codex: the one in +// its resources, /usr/lib/chatgpt/resources/codex in OpenAI's packages +// (the deb's and the Arch package's, chatgpt 26.1027). Not a codex the +// user installed (npm's, a release's): those run from a folder of their +// own. +func linuxCodexApp(exe string) bool { + return strings.HasPrefix(exe, "/") && strings.HasSuffix(exe, "/chatgpt/resources/codex") +} + // RestartCodexDaemon has Codex restart its app-server, which reads the // sign-in again: `codex app-server daemon restart`, run with the codex CLI // magpie finds and the Codex home it signs Codex in through. Codex 0.162's diff --git a/internal/provider/codex_daemon_test.go b/internal/provider/codex_daemon_test.go index a6bbd61db..470222496 100644 --- a/internal/provider/codex_daemon_test.go +++ b/internal/provider/codex_daemon_test.go @@ -497,12 +497,18 @@ func TestFindCodexApp(t *testing.T) { 9: {{PID: 3, Args: "codex"}, {PID: 4, Args: "/Users/me/.codex/packages/app-server-daemon/releases/0.162.0-aarch64-apple-darwin/bin/codex app-server --listen unix:///x --managed-daemon"}, {PID: 9, Args: app}}, 8: {{PID: 8, Args: `"C:\Program Files\WindowsApps\OpenAI.Codex_1.0\app\resources\codex.exe" app-server`}}, 61552: {{PID: 3, Args: `C:\Users\yeton\AppData\Local\Microsoft\WinGet\Links\codex.exe`}, {PID: 61552, Args: codexWindowsAppServer}}, + // the Linux app's, beside npm's codex running an app-server of + // its own (pota on Discord: no account switch for it on Linux) + 1957170: {{PID: 3, Args: "/usr/lib/node_modules/@openai/codex/vendor/x86_64-unknown-linux-musl/codex/codex app-server"}, {PID: 1957170, Args: codexLinuxAppServer}}, 0: { {PID: 3, Args: "codex app-server proxy"}, {PID: 5, Args: "/Applications/ChatGPT.app/Contents/Resources/codex-cli/CodexCLI.app/Contents/MacOS/codex exec-server --remote x"}, {PID: 6, Args: "/Applications/ChatGPT.app/Contents/Resources/codex-cli/CodexCLI.app/Contents/MacOS/codex app-server daemon restart"}, {PID: 7, Args: "/Users/me/.vscode/extensions/openai.chatgpt-1.0/bin/darwin-aarch64/codex app-server"}, {PID: 10, Args: "/Users/me/.codex/computer-use/Codex Computer Use.app/Contents/MacOS/SkyComputerUseService app-server"}, + {PID: 11, Args: "/usr/lib/chatgpt/ChatGPT --type=renderer"}, + {PID: 12, Args: "/home/me/.local/bin/codex app-server"}, + {PID: 13, Args: "chatgpt/resources/codex app-server"}, }, } { if got := findCodexApp(ps); got != want {