From e26d26ab5beb18446a835e956e134f4d3a06c55b Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 15 Oct 2026 18:27:41 +0800 Subject: [PATCH] =?UTF-8?q?agent:=20a=20Mac=20app=20in=20a=20folder=20of?= =?UTF-8?q?=20/Applications'=20own,=20or=20wherever=20Spotlight=20has=20it?= =?UTF-8?q?s=20bundle=20id,=20is=20found:=20DeepSeek=20Harness=20Desktop?= =?UTF-8?q?=20in=20/Applications/AI=20no=20longer=20reads=20as=20=E6=9C=AA?= =?UTF-8?q?=E6=89=BE=E5=88=B0=20CLI=20(#2325,=20sungoku)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sungoku (macOS 15.8.1, magpie 0.1.1253) keeps DeepSeek Harness.app in /Applications/AI and has no dsh CLI. magpie looked for the bundle in /Applications and ~/Applications only, so the app wasn't found and dsh's row said 未找到 CLI (the same gone rule as cjyrainbow's Windows case, #2177). macBundles is now the one place a Mac app's bundle is looked for, by AppVersion/HasApp (dsh, Codex) and macBundle (WorkBuddy): its names in each app folder, then in each folder one level down that is no bundle itself (an .app inside another app's bundle doesn't count), then wherever Spotlight has its bundle id (`mdfind kMDItemCFBundleIdentifier == ''`, as zedAppPath already finds Zed.app). The bundle's Info.plist must still carry the app's id. Spotlight's answer is kept 5 minutes per id, because the Agents page asks on every read and a missing app would otherwise run mdfind each time. It is never asked in a test binary testenv isolated (MAGPIE_TEST_SANDBOX), so no package's tests see the developer's own apps (#1823). The agent TestMain stubs it too. appHere (Claude.app, OpenCode, Goose: whether their folders are gone) also looks one folder down, through the same inAppFolders. Not changed: pencil, t3code, reasonix, claudescience and cursorlocal read macAppFolders with their own names and still look only at the top level. Test: TestDshDesktopAppInASubfolder covers no app; an app inside Other.app (not taken); another bundle id in /Applications/AI (not taken); sungoku's /Applications/AI/DeepSeek Harness.app (found, 0.2.0-rc.2); and DSH.app on another folder, found only once Spotlight names it. Broken by hand three ways, red each time: no subfolder scan, no Spotlight, bundles inside bundles allowed. Real mdfind query tried read-only on this Mac: com.apple.Safari -> /Applications/Safari.app, com.openai.codex -> /Applications/ChatGPT.app. DeepSeek Harness isn't installed here, so not tried with the real app. Run in this worktree (origin/main dd25997a4 + 041e99063 and 05c94fdf1 cherry-picked): - go test -tags nogui -p 1 -count=1 -run 'TestDsh|Desktop|App|Gone|WorkBuddy|Detect' ./internal/agent/: ok - gofmt -l internal/agent: clean - go vet -tags nogui ./internal/agent/ for GOOS=darwin, linux, windows: clean Full suite: SUITE_LINE --- internal/agent/desktopapp.go | 133 ++++++++++++++++++++++++----- internal/agent/dsh_desktop_test.go | 67 +++++++++++++++ internal/agent/gone.go | 10 +-- internal/agent/main_test.go | 3 + 4 files changed, 187 insertions(+), 26 deletions(-) diff --git a/internal/agent/desktopapp.go b/internal/agent/desktopapp.go index b095049f5..36e307a89 100644 --- a/internal/agent/desktopapp.go +++ b/internal/agent/desktopapp.go @@ -1,6 +1,7 @@ package agent import ( + "context" "os" "path/filepath" "regexp" @@ -8,6 +9,8 @@ import ( "slices" "strconv" "strings" + "sync" + "time" "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/proc" @@ -166,20 +169,112 @@ var ( // of its names: with its bundle id, or an Info.plist that isn't XML (a // binary one says nothing either way, and the name is the app's own). func macBundle(app desktopApp) bool { - for _, dir := range appFolders() { - for _, name := range app.names { - b, err := os.ReadFile(filepath.Join(dir, name, "Contents", "Info.plist")) - if err != nil { - continue - } - if id := plistIDRe.FindSubmatch(b); id == nil || strings.TrimSpace(string(id[1])) == app.id { - return true - } + for _, bundle := range macBundles(app) { + b, err := os.ReadFile(filepath.Join(bundle, "Contents", "Info.plist")) + if err != nil { + continue + } + if id := plistIDRe.FindSubmatch(b); id == nil || strings.TrimSpace(string(id[1])) == app.id { + return true } } return false } +// macBundles are where app's bundle may be on a Mac, the likeliest first: +// under one of its names in appFolders, or in a folder of theirs one level +// down (sungoku keeps DeepSeek Harness.app in /Applications/AI, and magpie +// 0.1.1253 said 未找到 CLI, #2325), then wherever Spotlight knows its bundle +// id to be (spotlightApps), as Zed's is found (zedAppPath). A caller still +// reads each one's Info.plist. +func macBundles(app desktopApp) []string { + out := inAppFolders(appFolders(), app.names...) + if app.id != "" { + for _, p := range spotlightApps(app.id) { + if strings.HasSuffix(p, ".app") && !slices.Contains(out, p) { + out = append(out, p) + } + } + } + return out +} + +// inAppFolders are the paths names may have in dirs, a Mac's app folders: +// in each, then in each folder of its one level down that is no bundle +// itself (/Applications/AI/DeepSeek Harness.app, #2325). +func inAppFolders(dirs []string, names ...string) []string { + var out []string + for _, dir := range dirs { + for _, name := range names { + out = append(out, filepath.Join(dir, name)) + } + entries, err := os.ReadDir(dir) + if err != nil { + continue + } + for _, e := range entries { + if !e.IsDir() || strings.HasSuffix(e.Name(), ".app") || strings.HasPrefix(e.Name(), ".") { + continue + } + for _, name := range names { + out = append(out, filepath.Join(dir, e.Name(), name)) + } + } + } + return out +} + +// spotlightApps are the bundles Spotlight has with bundle id id, on a Mac; +// none elsewhere. A var so tests never ask this machine's Spotlight. +var spotlightApps = spotlightBundles + +// spotlight keeps each bundle id's answer for spotlightFresh: the Agents +// page asks whether an app is installed on every read, and an app that +// isn't anywhere would ask mdfind each time. +var spotlight struct { + sync.Mutex + m map[string]spotlightAnswer +} + +type spotlightAnswer struct { + paths []string + at time.Time +} + +const spotlightFresh = 5 * time.Minute + +// spotlightSandbox is testenv.Marker, which agent can't import: set in a +// test binary testenv isolated, where this Mac's Spotlight would find the +// developer's own apps (#1823) from every package that asks for one. +const spotlightSandbox = "MAGPIE_TEST_SANDBOX" + +func spotlightBundles(id string) []string { + if runtime.GOOS != "darwin" || os.Getenv(spotlightSandbox) != "" { + return nil + } + spotlight.Lock() + defer spotlight.Unlock() + if a, ok := spotlight.m[id]; ok && time.Since(a.at) < spotlightFresh { + return a.paths + } + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + var paths []string + out, err := proc.CommandContext(ctx, "mdfind", "kMDItemCFBundleIdentifier == '"+id+"'").Output() + if err == nil { + for _, p := range strings.Split(strings.TrimSpace(string(out)), "\n") { + if p = strings.TrimSpace(p); p != "" { + paths = append(paths, p) + } + } + } + if spotlight.m == nil { + spotlight.m = map[string]spotlightAnswer{} + } + spotlight.m[id] = spotlightAnswer{paths: paths, at: time.Now()} + return paths +} + // 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). @@ -188,18 +283,16 @@ func (a *Agent) AppVersion() string { 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])) + for _, bundle := range macBundles(app) { + b, err := os.ReadFile(filepath.Join(bundle, "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])) } if app.store != "" { return storeAppVersion(windowsStorePackages(), app.store, app.publisher) diff --git a/internal/agent/dsh_desktop_test.go b/internal/agent/dsh_desktop_test.go index 63e11474f..e5adea10a 100644 --- a/internal/agent/dsh_desktop_test.go +++ b/internal/agent/dsh_desktop_test.go @@ -378,3 +378,70 @@ func TestDshDesktopRegisteredAnyHive(t *testing.T) { } } } + +// sungoku (#2325, macOS 15.8.1, magpie 0.1.1253): DeepSeek Harness.app in +// /Applications/AI, a folder of the user's own, and no dsh CLI; the Agents +// page said 未找到 CLI. The app is found one folder down in an app folder, +// and wherever Spotlight has its bundle id; a bundle of another id there +// is still not dsh's, nor is an app inside another app's bundle. +func TestDshDesktopAppInASubfolder(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("DSH_HOME", "") + t.Setenv("PATH", t.TempDir()) // no dsh CLI + apps := t.TempDir() + var spotlit []string + wasFolders, wasSpot := appFolders, spotlightApps + appFolders = func() []string { return []string{apps} } + spotlightApps = func(id string) []string { + if id != "com.deepseek.dsh" { + return nil + } + return spotlit + } + t.Cleanup(func() { appFolders, spotlightApps = wasFolders, wasSpot }) + desktop := filepath.Join(home, ".dsh", "profiles", "desktop", "cordis.patch.yml") + if err := os.MkdirAll(filepath.Dir(desktop), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(desktop, []byte(dshDesktopPatch), 0o644); err != nil { + t.Fatal(err) + } + a := dsh(home) + a.Bin = "magpie-test-gone-dsh" + if !a.CLIMissing() { + t.Fatal("no app, no CLI: not missing") + } + + // another app's bundle holding one of that name is no folder of apps + installApp(t, filepath.Join(apps, "Other.app"), "DeepSeek Harness.app", dshAppPlist) + if !a.CLIMissing() { + t.Fatal("an app inside another app's bundle taken for the desktop app") + } + + // another bundle id in /Applications/AI is not dsh's + installApp(t, filepath.Join(apps, "AI"), "DeepSeek Harness.app", strings.Replace(dshAppPlist, "com.deepseek.dsh", "com.example.other", 1)) + if !a.CLIMissing() { + t.Fatal("another bundle id in a subfolder taken for the desktop app") + } + + // sungoku's: /Applications/AI/DeepSeek Harness.app + os.RemoveAll(filepath.Join(apps, "AI")) + installApp(t, filepath.Join(apps, "AI"), "DeepSeek Harness.app", dshAppPlist) + if a.CLIMissing() || a.AppVersion() != "0.2.0-rc.2" { + t.Fatalf("app in /Applications/AI: missing %v, version %q", a.CLIMissing(), a.AppVersion()) + } + + // deeper, or on another disk: where Spotlight has it, under any name + os.RemoveAll(filepath.Join(apps, "AI")) + elsewhere := t.TempDir() + installApp(t, filepath.Join(elsewhere, "Tools", "AI"), "DSH.app", dshAppPlist) + if !a.CLIMissing() { + t.Fatal("found with Spotlight saying nothing") + } + spotlit = []string{filepath.Join(elsewhere, "Tools", "AI", "DSH.app")} + if a.CLIMissing() || a.AppVersion() != "0.2.0-rc.2" { + t.Fatalf("app Spotlight knows: missing %v, version %q", a.CLIMissing(), a.AppVersion()) + } +} diff --git a/internal/agent/gone.go b/internal/agent/gone.go index 703d548f7..2f3dab212 100644 --- a/internal/agent/gone.go +++ b/internal/agent/gone.go @@ -152,16 +152,14 @@ func extensionHere(home, id string) bool { } // appHere reports whether one of the bundles names is in a Mac's app -// folders. +// folders, or a folder of theirs one level down (inAppFolders). func appHere(names ...string) bool { if runtime.GOOS != "darwin" { return false } - for _, dir := range appFolders() { - for _, n := range names { - if isDir(filepath.Join(dir, n)) { - return true - } + for _, p := range inAppFolders(appFolders(), names...) { + if isDir(p) { + return true } } return false diff --git a/internal/agent/main_test.go b/internal/agent/main_test.go index 6c49a3b59..ba95273a7 100644 --- a/internal/agent/main_test.go +++ b/internal/agent/main_test.go @@ -38,6 +38,9 @@ func TestMain(m *testing.M) { // nor what an installer registered there (DeepSeek Harness Desktop's // folder, #2279) windowsRegisteredApp = func(string) (string, string) { return "", "" } + // nor where this Mac's Spotlight has an app (DeepSeek Harness.app + // outside /Applications, #2325) + spotlightApps = func(string) []string { return nil } // Aside's default is changed through the aside binary, which answers for // the account the developer is actually running: a test that picks a // model would change the real Aside's default, and the running one would