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