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
This commit is contained in:
yetone
2026-10-12 23:56:20 +08:00
parent 8792ee063d
commit 2221b63f93
9 changed files with 83 additions and 18 deletions
+8 -5
View File
@@ -35,6 +35,7 @@ import (
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/proc"
"github.com/yetone/magpie/internal/provider"
)
@@ -71,18 +72,20 @@ func cursorLocalRoots() []string {
home, _ := os.UserHomeDir()
switch runtime.GOOS {
case "darwin":
return []string{"/Applications", filepath.Join(home, "Applications")}
return macAppFolders(home)
case "windows":
// LOCALAPPDATA is the user's (a test's lies under its home);
// ProgramFiles is the machine's, so it goes through SystemDirs
var out []string
for _, v := range []string{"LOCALAPPDATA", "ProgramFiles"} {
if d := os.Getenv(v); d != "" {
for _, d := range append([]string{os.Getenv("LOCALAPPDATA")}, proc.SystemDirs(os.Getenv("ProgramFiles"))...) {
if d != "" {
out = append(out, filepath.Join(d, "Programs"), d)
}
}
return out
}
return []string{"/opt", "/usr/share", "/usr/lib", filepath.Join(home, ".local", "share"), filepath.Join(home, "Applications"),
filepath.Join(home, ".local", "bin"), filepath.Join(home, "bin"), filepath.Join(home, "Downloads"), home}
return append(proc.SystemDirs("/opt", "/usr/share", "/usr/lib"), filepath.Join(home, ".local", "share"), filepath.Join(home, "Applications"),
filepath.Join(home, ".local", "bin"), filepath.Join(home, "bin"), filepath.Join(home, "Downloads"), home)
}
// findCursorLocal is the program of the Cursor Private Inference among the
+15 -4
View File
@@ -8,6 +8,7 @@ import (
"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
@@ -44,11 +45,11 @@ var desktopApps = map[string]desktopApp{
var appFolders = func() []string {
switch runtime.GOOS {
case "darwin":
dirs := []string{"/Applications"}
if home, err := os.UserHomeDir(); err == nil {
dirs = append(dirs, filepath.Join(home, "Applications"))
home, err := os.UserHomeDir()
if err != nil {
return proc.SystemDirs("/Applications")
}
return dirs
return macAppFolders(home)
case "windows":
if local := appdir.Getenv("LOCALAPPDATA"); local != "" {
return []string{filepath.Join(local, "Programs")}
@@ -57,6 +58,16 @@ var appFolders = func() []string {
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>`)
+47
View File
@@ -4,9 +4,56 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/proc"
"github.com/yetone/magpie/internal/testenv"
)
// #1823 (mdwsk88): HasApp read the developer's own /Applications, so on a
// Mac with DeepSeek Harness.app installed
// TestAsideOfflineDisconnectRestoresWithoutDaemon found dsh's app, and its
// CLI was never missing. Every folder magpie looks for installed apps in —
// the desktop apps', Pencil's, T3 Code's, Reasonix Studio's, Cursor Private
// Inference's — lies under the test's home, on every OS, and an app a test
// installs in the sandbox's /Applications is the one found.
func TestAppFoldersStayInTheTestHome(t *testing.T) {
sandbox := os.Getenv(testenv.Marker)
if sandbox == "" {
t.Fatal("the package's tests aren't isolated (testenv)")
}
inside := func(what, dir string) {
t.Helper()
rel, err := filepath.Rel(sandbox, dir)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) || filepath.IsAbs(rel) {
t.Errorf("%s looks in %s, outside the test's home %s", what, dir, sandbox)
}
}
for _, d := range appFolders() {
inside("appFolders", d)
}
for _, d := range macAppFolders(sandbox) {
inside("macAppFolders", d)
}
for _, d := range cursorLocalRoots() {
inside("cursorLocalRoots", d)
}
if runtime.GOOS != "darwin" {
return
}
dsh := &Agent{ID: "dsh", Name: "DeepSeek Harness", Bin: "dsh"}
if dsh.HasApp() {
t.Fatal("an empty sandbox has DeepSeek Harness.app")
}
apps := proc.SystemDirs("/Applications")[0]
t.Cleanup(func() { os.RemoveAll(filepath.Join(apps, "DeepSeek Harness.app")) })
installApp(t, apps, "DeepSeek Harness.app", codexAppPlist("com.deepseek.dsh", "0.2.0"))
if v := dsh.AppVersion(); v != "0.2.0" {
t.Fatalf("the sandbox's /Applications/DeepSeek Harness.app: %q", v)
}
}
// codexAppPlist is the Codex app's Info.plist as macOS has it (ChatGPT.app
// 26.930.31730, its keys in their order), cut to the keys around the ones
// read, with the bundle id and version given: com.openai.chat stands for
+6 -2
View File
@@ -207,8 +207,12 @@ var npmPrefixes memo
func npmGlobalPrefix() string {
npm, err := proc.Look("npm")
if err != nil {
npm = filepath.Join(os.Getenv("ProgramFiles"), "nodejs", "npm.cmd")
if os.Getenv("ProgramFiles") == "" || !isFile(npm) {
pf := proc.SystemDirs(os.Getenv("ProgramFiles"))
if len(pf) == 0 {
return ""
}
npm = filepath.Join(pf[0], "nodejs", "npm.cmd")
if !isFile(npm) {
return ""
}
}
+1 -1
View File
@@ -81,7 +81,7 @@ func pencilApp(home string) bool {
if runtime.GOOS != "darwin" {
return false
}
for _, d := range []string{"/Applications", filepath.Join(home, "Applications")} {
for _, d := range macAppFolders(home) {
for _, n := range []string{"Pencil.app", "pen.dev.app"} {
if _, err := os.Stat(filepath.Join(d, n)); err == nil {
return true
+1 -1
View File
@@ -23,7 +23,7 @@ func reasonixDetected(home string) bool {
}
}
case "darwin":
for _, dir := range []string{"/Applications", filepath.Join(home, "Applications")} {
for _, dir := range macAppFolders(home) {
if reasonixStudioPackage(filepath.Join(dir, "Reasonix Studio.app", "Contents", "Resources", "app.asar")) {
return true
}
+1 -1
View File
@@ -97,7 +97,7 @@ func t3App(home string) bool {
if runtime.GOOS != "darwin" {
return false
}
for _, d := range []string{"/Applications", filepath.Join(home, "Applications")} {
for _, d := range macAppFolders(home) {
for _, n := range []string{"T3 Code (Alpha).app", "T3 Code.app"} {
if _, err := os.Stat(filepath.Join(d, n)); err == nil {
return true
+2 -3
View File
@@ -46,10 +46,9 @@ func saveZedCredential(url string) error {
// Trust the installed Zed bundle, including a CLI symlink to a custom location.
func zedAppPath(ctx context.Context) (string, error) {
home, _ := os.UserHomeDir()
paths := []string{
paths := append([]string{
filepath.Join(home, "Applications", "Zed.app"), filepath.Join(home, "Applications", "Zed Preview.app"),
"/Applications/Zed.app", "/Applications/Zed Preview.app",
}
}, proc.SystemDirs("/Applications/Zed.app", "/Applications/Zed Preview.app")...)
binName := os.Getenv("MAGPIE_ZED_BIN")
if binName == "" {
binName = "zed"
+2 -1
View File
@@ -33,6 +33,7 @@ import (
"strings"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/proc"
)
type zcodeBuiltin struct {
@@ -230,7 +231,7 @@ var zcodeBuiltinFiles = func() []string {
home, _ := os.UserHomeDir()
fs, _ := filepath.Glob(filepath.Join(home, ".zcode", "v2", "runtime", "provider", "*", "*", "*", "zcode-builtin.json"))
if runtime.GOOS == "darwin" {
fs = append(fs, "/Applications/ZCode.app/Contents/Resources/config/provider/zcode-builtin.json")
fs = append(fs, proc.SystemDirs("/Applications/ZCode.app/Contents/Resources/config/provider/zcode-builtin.json")...)
}
return fs
}