Files
magpie/internal/agent/desktopapp.go
T
yetone 03eb0633af library: an agent not installed is on no row but those of what it still has (@jiahao_jayden on X)
Library → Skills showed a chip for WorkBuddy on every row though it
wasn't installed ("一排有点密恐了"). WorkBuddy has no CLI and no detect
rule, so it counted as installed whenever ~/.workbuddy (or its
models.json) existed; that folder stays after the app is trashed, and
magpie itself writes models.json, mcp.json and skills/ there.

- agent: on a Mac WorkBuddy and WorkBuddy AI are detected by their apps,
  each by its own bundle id (com.tencent.workbuddy.mac,
  com.workbuddy.workbuddy-ai) in /Applications or ~/Applications
  (macBundle). Off a Mac the folder still counts, as before.
- library: an agent that isn't a target but still has skills, servers or
  hooks magpie gave it is held (Target.Gone). A sync takes edits from its
  copies and then only takes things out of it; Read lists it with
  gone: true while it has any, and not once it has none.
- GUI: a gone agent's chip is only on the rows of what it has, lit; a
  click takes it away. All, the How-per-agent sheet and the pickers leave
  it out; By agent lists it as "Not installed here" with Turn all on off.
  No layout change. New strings in zh, zh-TW, ja, de.

Run at this tree (macOS, sandbox HOME, Go caches pinned):
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: clean. go build, GOOS=linux go build -tags
  nogui, GOOS=windows go build: ok.
- go test -tags nogui -timeout 30m ./...: all ok (59 packages);
  internal/gui rerun after the last string rename: ok.
- GUI: the 74 test files that open the Library, chromium and webkit:
  563/563 each. library-gone-agents.test.cjs (new; 1100px and 390px, en,
  zh, zh-TW, ja, de): 11/11 in both; i18n-keys.test.cjs: 3/3.

Broken by hand, each went red, then restored and green:
- old client.js: library-gone-agents fails "a chip for an agent not
  installed on a row it has nothing on".
- workbuddyHere's Mac branch falling back to the folder:
  TestWorkBuddyOnMacIsItsApp "no app, only the folders: WorkBuddy true,
  WorkBuddy AI true".
- syncSkills' Gone guard off: TestGoneAgentListedToTakeOut "a sync gave
  the gone hermes a skill again".
- held() returning nil: "hermes, holding a skill and a server, isn't
  listed as gone".
- the gone sync steps skipped: "hermes, holding nothing, is still listed".

Not verified: the reporter's machine; WorkBuddy detection on Windows and
Linux (unchanged, still by folder); other agents detected by folder alone
(zcode, alma, grok-app, air, hanako, morph, grok) are unchanged. WorkBuddy
AI.app is installed on the owner's Mac without ~/.workbuddy-ai, so it now
shows as detected there.
2026-10-15 14:52:51 +08:00

186 lines
6.3 KiB
Go

package agent
import (
"os"
"path/filepath"
"regexp"
"runtime"
"slices"
"strconv"
"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. store and publisher identify a Microsoft
// Store package by its package name and publisher id.
type desktopApp struct {
id string
names []string
win string
store string
publisher string
}
var desktopApps = map[string]desktopApp{
"codex": {id: "com.openai.codex", names: []string{"ChatGPT.app", "Codex.app"}, store: "OpenAI.Codex", publisher: "2p2nqsd0c76g0"},
// 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. Store packages are read from the current user's AppX registry.
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"))
}
// windowsStorePackages lists the package full names registered for the
// current user on Windows, nil elsewhere; a var so tests never read the
// machine's own registry.
var windowsStorePackages = installedWindowsStorePackages
var (
plistIDRe = regexp.MustCompile(`<key>CFBundleIdentifier</key>\s*<string>([^<]+)</string>`)
plistVersionRe = regexp.MustCompile(`<key>CFBundleShortVersionString</key>\s*<string>([^<]+)</string>`)
)
// macBundle reports whether app's bundle is in one of appFolders, under one
// 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
}
}
}
return false
}
// 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]))
}
}
if app.store != "" {
return storeAppVersion(windowsStorePackages(), app.store, app.publisher)
}
return ""
}
// storeAppVersion finds the newest version of an app among Microsoft Store
// package full names (name_version_architecture_resourceid_publisher). Only
// the main package counts, the one with no resource id: the AppX registry
// keeps a resource package (…_neutral_split.scale-125_…) of a version the
// main package has moved past, so one is no sign the app is installed.
func storeAppVersion(packages []string, name, publisher string) string {
prefix, suffix := name+"_", "_"+publisher
var newest [4]uint64
found := ""
for _, pkg := range packages {
if !strings.HasPrefix(pkg, prefix) || !strings.HasSuffix(pkg, suffix) {
continue
}
fields := strings.Split(strings.TrimSuffix(strings.TrimPrefix(pkg, prefix), suffix), "_")
if len(fields) != 3 || fields[2] != "" {
continue
}
parts := strings.Split(fields[0], ".")
if len(parts) != len(newest) {
continue
}
var version [4]uint64
valid := true
for i, part := range parts {
var err error
version[i], err = strconv.ParseUint(part, 10, 16)
if err != nil {
valid = false
break
}
}
if valid && (found == "" || slices.Compare(version[:], newest[:]) > 0) {
newest, found = version, fields[0]
}
}
return found
}
// 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
}