Files
magpie/internal/agent/desktopapp.go
T
yetone 2221b63f93 agent, provider: tests no longer read the machine's /Applications or Program Files, so a Mac with DeepSeek Harness.app installed keeps TestAsideOfflineDisconnectRestoresWithoutDaemon green (#1823)
What: every place magpie looks for installed apps outside the home folder
now goes through proc.SystemDirs, which in a test binary testenv isolated
puts the path under the test's home (as it already does for Homebrew's
bin, #1525):
- agent: a new macAppFolders(home) (/Applications via SystemDirs, plus
  ~/Applications) is used by appFolders (desktop apps: Codex, dsh),
  pencilApp, t3App and reasonixDetected;
- cursorLocalRoots: macOS through macAppFolders, Windows' ProgramFiles and
  Linux's /opt, /usr/share, /usr/lib through SystemDirs;
- zedAppPath's /Applications/Zed*.app, npmGlobalPrefix's ProgramFiles npm
  fallback, and provider's /Applications/ZCode.app builtin config.
Production behaviour is unchanged: SystemDirs returns paths as they are
outside a sandboxed test.

Why: HasApp/AppVersion read the real /Applications, so on a Mac with
DeepSeek Harness.app the dsh fixture's CLI was never "missing" and the test
failed every time; CI has no such app, so nobody else saw it. The same
shape was in five other readers.

Test: TestAppFoldersStayInTheTestHome checks every app folder lies under
testenv's home (all OSes), and on macOS that an app installed in the
sandbox's /Applications is the one found.

Run (go1.26.8, sandbox HOME, private GOCACHE):
- repro on old code: appFolders pointed at a temp folder holding a fake
  "DeepSeek Harness.app/Contents/Info.plist" (com.deepseek.dsh) ->
  TestAsideOfflineDisconnectRestoresWithoutDaemon FAIL "native fixture must
  report missing CLI".
- break check: macAppFolders put back to the literal /Applications ->
  TestAppFoldersStayInTheTestHome FAIL (appFolders, macAppFolders,
  cursorLocalRoots look in /Applications; sandbox app not found). Restored:
  pass.
- gofmt -l clean; go vet ./... (darwin), GOOS=linux go vet -tags nogui
  ./..., GOOS=windows go vet ./...: clean.
- go test -tags nogui ./internal/agent ./internal/proc ./internal/provider: ok.
- -race -count=5 on the touched agent tests: ok.
Not run: the full go test ./...; Windows and Linux test runs (vet only);
reasonix's Windows registry lookup and Zed's mdfind fallback still ask
the machine (not folder paths) and are left as they are.

Reported-by: mdwsk88
2026-10-12 23:56:20 +08:00

117 lines
4.1 KiB
Go

package agent
import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/proc"
)
// An agent that is a desktop app as well as a CLI has the app's version shown
// beside the CLI's (#1334, laacmilan: with Codex's CLI and its app both
// installed, the Agents page showed only the CLI's version). Both read the
// same settings, so one row stands for both.
// desktopApp is the macOS bundle an agent's desktop app is known by: its
// bundle id, and the names it has been installed under. OpenAI's Codex app
// is ChatGPT.app (com.openai.codex) since it took ChatGPT's name; the bundle
// id tells it from the ChatGPT chat app (com.openai.chat) of the same name.
// win is its executable under %LOCALAPPDATA%\Programs, where a per-user
// installer puts it on Windows, "" for one magpie doesn't know there.
type desktopApp struct {
id string
names []string
win string
}
var desktopApps = map[string]desktopApp{
"codex": {id: "com.openai.codex", names: []string{"ChatGPT.app", "Codex.app"}},
// 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
// command is optional (star on Discord: 未找到 CLI with the desktop app
// in use). On Windows its installer (electron-builder's per-user NSIS,
// productName DeepSeek Harness) puts it under %LOCALAPPDATA%\Programs.
"dsh": {id: "com.deepseek.dsh", names: []string{"DeepSeek Harness.app"}, win: filepath.Join("DeepSeek Harness", "DeepSeek Harness.exe")},
}
// appFolders are where apps are installed; a var so tests can point it
// elsewhere: /Applications and ~/Applications on macOS, %LOCALAPPDATA%\Programs
// on Windows (the Codex app there is a Store package magpie doesn't read yet).
var appFolders = func() []string {
switch runtime.GOOS {
case "darwin":
home, err := os.UserHomeDir()
if err != nil {
return proc.SystemDirs("/Applications")
}
return macAppFolders(home)
case "windows":
if local := appdir.Getenv("LOCALAPPDATA"); local != "" {
return []string{filepath.Join(local, "Programs")}
}
}
return nil
}
// macAppFolders are the folders a Mac keeps apps in: /Applications and
// home's Applications. /Applications goes through proc.SystemDirs, so in a
// test binary testenv isolated it lies under the test's home: tests read
// the developer's own /Applications, and on a Mac with DeepSeek Harness.app
// installed dsh's CLI was never missing (#1823, mdwsk88). Every reader of a
// Mac's installed apps takes its folders from here.
func macAppFolders(home string) []string {
return append(proc.SystemDirs("/Applications"), filepath.Join(home, "Applications"))
}
var (
plistIDRe = regexp.MustCompile(`<key>CFBundleIdentifier</key>\s*<string>([^<]+)</string>`)
plistVersionRe = regexp.MustCompile(`<key>CFBundleShortVersionString</key>\s*<string>([^<]+)</string>`)
)
// AppVersion is the version of the agent's desktop app installed here, ""
// for an agent without one magpie knows, one not installed, or an Info.plist
// that doesn't say (a binary one isn't read).
func (a *Agent) AppVersion() string {
app, ok := desktopApps[a.ID]
if !ok || a.WSL != "" {
return ""
}
for _, dir := range appFolders() {
for _, name := range app.names {
b, err := os.ReadFile(filepath.Join(dir, name, "Contents", "Info.plist"))
if err != nil {
continue
}
id, v := plistIDRe.FindSubmatch(b), plistVersionRe.FindSubmatch(b)
if id == nil || v == nil || strings.TrimSpace(string(id[1])) != app.id {
continue
}
return strings.TrimSpace(string(v[1]))
}
}
return ""
}
// HasApp reports whether the agent's desktop app is installed here: its
// bundle on macOS (AppVersion), its executable on Windows.
func (a *Agent) HasApp() bool {
if a.AppVersion() != "" {
return true
}
app, ok := desktopApps[a.ID]
if !ok || a.WSL != "" || app.win == "" {
return false
}
for _, dir := range appFolders() {
if isFile(filepath.Join(dir, app.win)) {
return true
}
}
return false
}