proc: a probe never runs a macOS app's own program, so asking opencode's version doesn't open OpenCode (saint carol)
saint carol on Discord: opening magpie, often right after an update, brings up OpenCode, which they never asked for. magpie asks `opencode --version` at every start and after a CLI update (proc.Version for the agents' CLI info, library/rtk.go openCodeVersion, agent/cliupdate.go runCheck), all through proc.ProbeContext. When the `opencode` on PATH is OpenCode.app's own program (a link to OpenCode.app/Contents/MacOS/OpenCode, or the bundle's MacOS folder on PATH, where a case-insensitive disk finds OpenCode as opencode), running it with any arguments opens the app's window. A GUI app never answers --version, and a timeout isn't cached, so it happened again at each start. Nothing else in magpie opens OpenCode: no `open -a`, no opencode:// URL, and the Sessions menu's Open/Terminal actions are a user's click. ProbeContext now refuses (cmd.Err, nothing started) a program that is the main executable of an outermost .app bundle, after links: the CFBundleExecutable of an XML Info.plist, or the program named as the bundle when the plist can't be read (binary, as Kiro CLI.app's is). Every probe goes through it: version, auth status, Grok's agentProbe. A bundle's other programs still run (OpenCode's opencode-cli, Kiro CLI's kiro-cli beside kiro_cli_desktop), and so does a bundle inside another app's: ChatGPT.app's codex is CodexCLI.app's main executable (plist checked on this Mac, ChatGPT 26.1027). Tests (internal/proc/appmain_test.go, not on Windows): a fake OpenCode.app whose programs record that they ran. - TestProbeNeverOpensAnApp: a link to OpenCode is not run, Version is "", `opencode` found in the bundle's folder by PATH is refused, the bundled opencode-cli still answers. - TestProbeUnreadPlistByName: no plist / a binary one: OpenCode is not run, opencode-cli is. - TestProbeRunsANestedAppsCLI: CodexCLI.app's codex inside ChatGPT.app, with that app's plist keys, answers. - TestAppMainOnlyBundles: paths that only look like a bundle. Broken by hand: with the guard off, TestProbeNeverOpensAnApp and TestProbeUnreadPlistByName go red (OpenCode ran); with the nested check off, TestProbeRunsANestedAppsCLI and TestAppMainOnlyBundles go red. Run on mote ata8d6a861aplus this change (go test -count=1 -timeout 45m): internal/proc, library, wslrun, agent, provider ok. internal/cli red on TestEveryRouteHasACommandOrIsNamed (POST /api/usage/checkin-account has no command), which is #4826 frome87140242and not touched here. GOOS=windows go vet ./... ok; GOOS=linux go vet -tags nogui ./... ok (wails' Linux GUI doesn't cross-vet from a Mac); gofmt -l empty. Not tried: the real OpenCode desktop app (the cause is read from its bundle layout and magpie's code, not reproduced), the reporter's PATH or link, and a shell wrapper that execs the GUI program (still run).
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package proc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// errAppMain is what a probe of an app's own program ends with, unrun.
|
||||
var errAppMain = errors.New("not run: it is a macOS app's own program, which opens the app's window")
|
||||
|
||||
// appMain reports whether the program at path is the main executable of
|
||||
// an app a user opens (…/X.app/Contents/MacOS/<CFBundleExecutable>, X.app
|
||||
// in no other bundle), after links. Run, that opens the app: its Dock icon
|
||||
// and window, whatever the arguments, and a GUI app given --version never
|
||||
// answers it. An `opencode` on PATH that resolves to OpenCode.app's own
|
||||
// program (a folder of the bundle on PATH, where a Mac's case-insensitive
|
||||
// disk finds OpenCode as opencode, or a link to it) put OpenCode's window
|
||||
// up each time magpie asked its version, at every start and after each
|
||||
// update (saint carol on Discord). The bundle's other programs (OpenCode's
|
||||
// opencode-cli, Kiro CLI.app's kiro-cli beside its kiro_cli_desktop) are
|
||||
// CLIs and are run, and so is a bundle inside an app's own: ChatGPT.app's
|
||||
// codex is CodexCLI.app's main executable. When Info.plist can't be read
|
||||
// (a binary one, as Kiro CLI.app's is), the program named as its bundle
|
||||
// is taken for the main one.
|
||||
func appMain(path string) bool {
|
||||
if path == "" {
|
||||
return false
|
||||
}
|
||||
if real, err := filepath.EvalSymlinks(path); err == nil {
|
||||
path = real
|
||||
}
|
||||
if abs, err := filepath.Abs(path); err == nil {
|
||||
path = abs
|
||||
}
|
||||
macos := filepath.Dir(path)
|
||||
contents := filepath.Dir(macos)
|
||||
bundle := filepath.Dir(contents)
|
||||
if !strings.EqualFold(filepath.Base(macos), "MacOS") || !strings.EqualFold(filepath.Base(contents), "Contents") ||
|
||||
!strings.HasSuffix(strings.ToLower(filepath.Base(bundle)), ".app") {
|
||||
return false
|
||||
}
|
||||
for up := filepath.Dir(bundle); up != filepath.Dir(up); up = filepath.Dir(up) {
|
||||
if strings.HasSuffix(strings.ToLower(filepath.Base(up)), ".app") {
|
||||
return false // a helper or a CLI another app ships
|
||||
}
|
||||
}
|
||||
exe := bundleExecutable(filepath.Join(contents, "Info.plist"))
|
||||
if exe == "" {
|
||||
exe = filepath.Base(bundle)
|
||||
exe = exe[:len(exe)-len(".app")]
|
||||
}
|
||||
return strings.EqualFold(exe, filepath.Base(path))
|
||||
}
|
||||
|
||||
var bundleExeRe = regexp.MustCompile(`<key>CFBundleExecutable</key>\s*<string>([^<]+)</string>`)
|
||||
|
||||
// bundleExecutable is the CFBundleExecutable an XML Info.plist names, ""
|
||||
// when it can't be read (a binary plist too) or names none.
|
||||
func bundleExecutable(plist string) string {
|
||||
b, err := os.ReadFile(plist)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if m := bundleExeRe.FindSubmatch(b); m != nil {
|
||||
return strings.TrimSpace(string(m[1]))
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
//go:build !windows
|
||||
|
||||
package proc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/testenv"
|
||||
)
|
||||
|
||||
// openCodeBundle lays out OpenCode.app as its desktop app ships on a Mac:
|
||||
// its own program OpenCode (the window), named by Info.plist's
|
||||
// CFBundleExecutable, and a CLI beside it. Each program writes its name to
|
||||
// ran when it is started.
|
||||
func openCodeBundle(t *testing.T, plist string) (macos, ran string) {
|
||||
dir := t.TempDir()
|
||||
ran = filepath.Join(dir, "ran")
|
||||
contents := filepath.Join(dir, "Applications", "OpenCode.app", "Contents")
|
||||
macos = filepath.Join(contents, "MacOS")
|
||||
if err := os.MkdirAll(macos, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if plist != "" {
|
||||
if err := os.WriteFile(filepath.Join(contents, "Info.plist"), []byte(plist), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
testenv.Program(t, filepath.Join(macos, "OpenCode"), "#!/bin/sh\necho window >> '"+ran+"'\n")
|
||||
testenv.Program(t, filepath.Join(macos, "opencode-cli"), "#!/bin/sh\necho cli >> '"+ran+"'\necho 2.2.5\n")
|
||||
return macos, ran
|
||||
}
|
||||
|
||||
const openCodePlist = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<plist version="1.0"><dict>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>OpenCode</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>ai.opencode.desktop</string>
|
||||
</dict></plist>`
|
||||
|
||||
func probeVersion(t *testing.T, bin string) (string, error) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
out, err := ProbeContext(ctx, bin, "--version").Output()
|
||||
return strings.TrimSpace(string(out)), err
|
||||
}
|
||||
|
||||
func ranOf(t *testing.T, ran string) string {
|
||||
b, err := os.ReadFile(ran)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return strings.TrimSpace(string(b))
|
||||
}
|
||||
|
||||
// An opencode that is OpenCode.app's own program, reached through a link
|
||||
// or a folder of the bundle on PATH, is never run by a version probe: run,
|
||||
// it opens OpenCode's window, which magpie's version check did at each
|
||||
// start and after each update (saint carol on Discord). The CLI beside it
|
||||
// in the bundle is still asked.
|
||||
func TestProbeNeverOpensAnApp(t *testing.T) {
|
||||
macos, ran := openCodeBundle(t, openCodePlist)
|
||||
bin := filepath.Join(t.TempDir(), "opencode")
|
||||
if err := os.Symlink(filepath.Join(macos, "OpenCode"), bin); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := probeVersion(t, bin); err == nil {
|
||||
t.Fatal("a link to the app's own program was run")
|
||||
}
|
||||
if v := Version(bin); v != "" {
|
||||
t.Fatalf("the app's own program said version %q", v)
|
||||
}
|
||||
// PATH holding the bundle's folder: on a Mac's case-insensitive disk
|
||||
// "opencode" finds OpenCode itself
|
||||
if _, err := os.Stat(filepath.Join(macos, "opencode")); err == nil {
|
||||
t.Setenv("PATH", macos)
|
||||
if _, err := probeVersion(t, "opencode"); err == nil {
|
||||
t.Fatal("opencode found in the bundle's folder was run")
|
||||
}
|
||||
}
|
||||
if got := ranOf(t, ran); got != "" {
|
||||
t.Fatalf("a probe started %q", got)
|
||||
}
|
||||
if v, err := probeVersion(t, filepath.Join(macos, "opencode-cli")); err != nil || v != "2.2.5" {
|
||||
t.Fatalf("the bundle's CLI answered %q, %v", v, err)
|
||||
}
|
||||
if got := ranOf(t, ran); got != "cli" {
|
||||
t.Fatalf("ran %q, want only the CLI", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A bundle whose Info.plist can't be read (none, or a binary one, as Kiro
|
||||
// CLI.app's is): the program named as the bundle is its main one and is
|
||||
// not run, the CLI beside it is.
|
||||
func TestProbeUnreadPlistByName(t *testing.T) {
|
||||
for name, plist := range map[string]string{"none": "", "binary": "bplist00\x01\x02"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
macos, ran := openCodeBundle(t, plist)
|
||||
if _, err := probeVersion(t, filepath.Join(macos, "OpenCode")); err == nil {
|
||||
t.Error("OpenCode was run")
|
||||
}
|
||||
if v, err := probeVersion(t, filepath.Join(macos, "opencode-cli")); err != nil || v != "2.2.5" {
|
||||
t.Errorf("the bundle's CLI answered %q, %v", v, err)
|
||||
}
|
||||
if got := ranOf(t, ran); got != "cli" {
|
||||
t.Fatalf("ran %q, want only the CLI", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ChatGPT.app ships its codex as CodexCLI.app's main executable, inside
|
||||
// its own bundle: a CLI, and asked its version (Info.plist as 26.1027's).
|
||||
func TestProbeRunsANestedAppsCLI(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
contents := filepath.Join(dir, "ChatGPT.app", "Contents", "Resources", "codex-cli", "CodexCLI.app", "Contents")
|
||||
if err := os.MkdirAll(filepath.Join(contents, "MacOS"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
plist := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<plist version="1.0"><dict>
|
||||
<key>CFBundleDisplayName</key><string>ChatGPT</string>
|
||||
<key>CFBundleExecutable</key><string>codex</string>
|
||||
<key>CFBundleIdentifier</key><string>com.openai.codex.cli</string>
|
||||
<key>CFBundleName</key><string>ChatGPT</string>
|
||||
<key>CFBundlePackageType</key><string>APPL</string>
|
||||
<key>CFBundleVersion</key><string>1</string>
|
||||
</dict></plist>`
|
||||
if err := os.WriteFile(filepath.Join(contents, "Info.plist"), []byte(plist), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
codex := filepath.Join(contents, "MacOS", "codex")
|
||||
testenv.Program(t, codex, "#!/bin/sh\necho codex-cli 0.166.0\n")
|
||||
if v, err := probeVersion(t, codex); err != nil || v != "codex-cli 0.166.0" {
|
||||
t.Fatalf("ChatGPT's codex answered %q, %v", v, err)
|
||||
}
|
||||
}
|
||||
|
||||
// A program outside any bundle, or in a folder only named like one, runs.
|
||||
func TestAppMainOnlyBundles(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
for _, p := range []string{
|
||||
filepath.Join(dir, "bin", "opencode"),
|
||||
filepath.Join(dir, "MacOS", "opencode"),
|
||||
filepath.Join(dir, "Contents", "MacOS", "opencode"),
|
||||
filepath.Join(dir, "x.apps", "Contents", "MacOS", "opencode"),
|
||||
filepath.Join(dir, "X.app", "Contents", "MacOS", "x-cli"),
|
||||
filepath.Join(dir, "A.app", "Contents", "Frameworks", "X.app", "Contents", "MacOS", "X"),
|
||||
} {
|
||||
if appMain(p) {
|
||||
t.Errorf("%s taken for an app's own program", p)
|
||||
}
|
||||
}
|
||||
if !appMain(filepath.Join(dir, "X.app", "Contents", "MacOS", "X")) {
|
||||
t.Error("a bundle with no Info.plist: the program named as it was taken for a CLI")
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,11 @@ func ProbeContext(ctx context.Context, name string, args ...string) *exec.Cmd {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
p := &probe{cancel: cancel, killing: make(chan struct{}), ended: make(chan struct{})}
|
||||
cmd := CommandContext(ctx, name, args...)
|
||||
// a probe never opens an app: asked its version, an app's own program
|
||||
// puts its window up instead (appMain)
|
||||
if cmd.Err == nil && appMain(cmd.Path) {
|
||||
cmd.Err = errAppMain
|
||||
}
|
||||
probeGroup(cmd, p)
|
||||
probes.Lock()
|
||||
probes.m[p] = struct{}{}
|
||||
|
||||
Reference in New Issue
Block a user