Files
magpie/internal/agent/desktopapp_xdg.go
T
yetone 61394481c7 agent: DeepSeek Harness Desktop is found on Linux (AUR, .deb, AppImage entry)
xianhongtao (magpie 0.1.3232, Arch Linux) has DeepSeek Harness Desktop
from the AUR (deepseek-harness-desktop 0.2.2rc.1-1) and no dsh CLI.
magpie knew the app on macOS and Windows only, so it said "DeepSeek
Harness CLI not found ~/.dsh/profiles/web/cordis.patch.yml".

HasApp for dsh now also finds the Linux app (linuxApp, desktopapp_xdg.go):
- a folder holding the Electron executable deepseek-harness with
  resources/app.asar beside it: /usr/lib/deepseek-harness-desktop (the
  AUR's) or /opt/DeepSeek Harness (electron-builder's .deb/.rpm);
- or the app's desktop entry in the XDG data dirs (XDG_DATA_HOME first,
  then XDG_DATA_DIRS, the first file of a name wins, Hidden=true hides
  it): Name=DeepSeek Harness, Type Application, MimeType holding
  x-scheme-handler/dsh, and its Exec program there (on PATH, or an
  absolute executable file: an AppImage anywhere). Scans of a folder are
  kept by its mtime, trusted only when read over a second after it
  changed and for a minute at most.
Linux only; macOS and Windows keep their own checks.

Siblings: everything reading HasApp for dsh follows: the profile a fresh
~/.dsh gets (profiles/desktop), the desktop lead and notices, cliHeld and
CLIMissing (so the GUI's and the CLI's "CLI not found" go away). The
"running" check for noticeDsh2 also knows deepseek-harness, the app's
Linux executable. With both profiles present (the reporter's case) Path
stays profiles/web as on macOS; the desktop lead picks the model shown.

Run (mote, through mote-run):
- go test -count=1 -race -run 'TestDshLinux|TestDshDesktop|TestCodexLinux|Lody' ./internal/agent/: ok
- go test -count=1 ./internal/agent/ ./internal/gui/ ./internal/cli/: ok
- gofmt -l clean; go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...; GOOS=linux/windows go build: ok
Run (wsl-run, magpie-desktop, Ubuntu 24.04): the TestDshLinux tests
-count=3: ok. The first WSL run caught a stale scan of a folder made
again within the same mtime tick; the 1s rule fixes it.

Broken by hand (mote), each red, then restored:
- installed() without onLinux: all three new tests red ("the AUR's app:
  app false, CLI missing true", "the AUR's entry, its program on PATH,
  not found", the profile = profiles/web, want profiles/desktop);
- no scheme check: "another app's entry taken for DeepSeek Harness's";
- no Hidden check: "an entry the user hid still counts".

Not tried: a real AUR, .deb or AppImage install of DeepSeek Harness
Desktop (the fixtures are the AUR PKGBUILD's entry and layout as the
reporter gave them, and AppImageLauncher's entry shape); the GUI suite
(no GUI file changed). The full suite runs after the push.

Reported-by: xianhongtao (Discord)
2026-11-07 19:48:49 +08:00

188 lines
5.7 KiB
Go

package agent
import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/proc"
)
// linuxApp is how a desktop app whose vendor ships no Linux package is
// found on Linux, where others package it (DeepSeek Harness Desktop:
// the AUR, a .deb, an AppImage). It is installed when one of dirs, the
// folders packagers install it to, holds exe and an Electron app's
// resources/app.asar, or when a desktop entry in the XDG data folders'
// applications/ is the app's (its Name is name and it handles links of
// scheme, the app's own) and its program is there: what a package puts
// in /usr/share/applications, and what AppImageLauncher, Gear Lever or
// the user writes in ~/.local/share/applications for an AppImage
// anywhere.
type linuxApp struct {
dirs []string
exe string
name string
scheme string
}
// linuxAppOS is the system linuxApp looks on; tests stand in Linux.
var linuxAppOS = runtime.GOOS
// installed reports whether l is installed here (see linuxApp); false
// for no linuxApp, and on macOS and Windows.
func (l *linuxApp) installed() bool {
if l == nil || linuxAppOS == "darwin" || linuxAppOS == "windows" {
return false
}
// through proc.SystemDirs, so a test binary never reads the machine's
for _, dir := range proc.SystemDirs(l.dirs...) {
if isFile(filepath.Join(dir, l.exe)) && isFile(filepath.Join(dir, "resources", "app.asar")) {
return true
}
}
return slices.ContainsFunc(appEntryPrograms(l.name, l.scheme), programThere)
}
// programThere reports whether a desktop entry's program is there: an
// executable file at its path, or one on PATH by its name.
func programThere(prog string) bool {
if !filepath.IsAbs(prog) {
_, err := proc.LookPath(prog)
return err == nil
}
st, err := os.Stat(prog)
return err == nil && !st.IsDir() && st.Mode()&0o111 != 0
}
// appEntryDirs are the folders desktop entries are read from, the
// user's first (xdgDataDirs' applications/). The system's go through
// proc.SystemDirs: testenv sets the user's data folder to the test's own
// but leaves $XDG_DATA_DIRS, which names the machine's.
func appEntryDirs() []string {
data := xdgDataDirs()
if len(data) == 0 {
return nil
}
dirs := []string{filepath.Join(data[0], "applications")}
for _, d := range proc.SystemDirs(data[1:]...) {
dirs = append(dirs, filepath.Join(d, "applications"))
}
return dirs
}
// appEntryPrograms are the programs of the desktop entries named name
// that handle scheme's links, as the Desktop Entry Specification finds
// entries: the first folder with an entry of a file name has the one that
// counts, and one that is Hidden hides it. Each folder is read again
// when its time changed (an entry added, removed or renamed over), else
// once a minute: this runs whenever dsh's row is read.
func appEntryPrograms(name, scheme string) []string {
var progs []string
seen := map[string]bool{}
for _, dir := range appEntryDirs() {
for file, prog := range entriesIn(dir, name, scheme) {
if seen[file] {
continue
}
seen[file] = true
if prog != "" {
progs = append(progs, prog)
}
}
}
return progs
}
// entryScan is what one applications/ folder held at its time, read at
// read: each entry's file name, with the program of one that is the
// app's ("" for another app's, or a hidden one).
type entryScan struct {
at, read time.Time
entries map[string]string
}
var entryScans = struct {
sync.Mutex
m map[string]entryScan
}{m: map[string]entryScan{}}
// entriesIn is the desktop entries in dir (see entryScan); none when it
// can't be read.
func entriesIn(dir, name, scheme string) map[string]string {
st, err := os.Stat(dir)
if err != nil || !st.IsDir() {
return nil
}
key := dir + "\x00" + name + "\x00" + scheme
entryScans.Lock()
scan, ok := entryScans.m[key]
entryScans.Unlock()
// an entry written over in place leaves the folder's time: read
// again after a minute. A folder changed within a second of the read
// is read again too: Linux stamps times by the kernel's tick, so a
// change soon after the read can leave the time as it was.
if ok && scan.at.Equal(st.ModTime()) && scan.read.Sub(scan.at) > time.Second && time.Since(scan.read) < time.Minute {
return scan.entries
}
files, err := os.ReadDir(dir)
if err != nil {
return nil
}
entries := map[string]string{}
for _, f := range files {
if f.IsDir() || !strings.HasSuffix(f.Name(), ".desktop") {
continue
}
b, err := os.ReadFile(filepath.Join(dir, f.Name()))
if err != nil {
continue
}
entries[f.Name()] = appEntryProgram(string(b), name, scheme)
}
entryScans.Lock()
entryScans.m[key] = entryScan{at: st.ModTime(), read: time.Now(), entries: entries}
entryScans.Unlock()
return entries
}
// appEntryProgram is the program a desktop entry runs when it is the
// app's: its Name is name, it handles scheme's links and isn't Hidden;
// "" otherwise. A program run through env (Exec=env VAR=x prog) is prog.
func appEntryProgram(entry, name, scheme string) string {
keys := map[string]string{}
in := false
for _, l := range strings.Split(entry, "\n") {
l = strings.TrimSpace(l)
if strings.HasPrefix(l, "[") {
in = l == "[Desktop Entry]"
continue
}
if k, v, ok := strings.Cut(l, "="); in && ok {
if k = strings.TrimSpace(k); keys[k] == "" {
keys[k] = strings.TrimSpace(v)
}
}
}
if keys["Name"] != name || keys["Hidden"] == "true" || keys["Type"] != "" && keys["Type"] != "Application" {
return ""
}
if !slices.Contains(strings.Split(keys["MimeType"], ";"), "x-scheme-handler/"+scheme) {
return ""
}
argv := desktopArgs(entry, "")
if len(argv) > 0 && argv[0] == "env" {
argv = argv[1:]
for len(argv) > 0 && strings.Contains(argv[0], "=") {
argv = argv[1:]
}
}
if len(argv) == 0 {
return ""
}
return argv[0]
}