Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

agent: Lody on Linux is run with its import link, which xdg-open turned away (Atlas on Discord)

yetone4dfd88206e82
diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md
index 47a9fdbbc..e134afaed 100644
--- a/docs/subsystems/agent-wiring.md
+++ b/docs/subsystems/agent-wiring.md
@@ -166,11 +166,31 @@ and seven runtime targets. Only loopback HTTP gateways are offered; the key
is always the public attribution token `magpie-lody`, never a LAN secret.
With `MAGPIE_ADDR` pinned beyond loopback there is no link: the row has no
button, and `magpie lody add` says why and opens nothing
-(`TestLodyLinkCarriesNoSecret`). The OS chooses the installed Lody handler. A compatible Lody build must
-implement this import route.
+(`TestLodyLinkCarriesNoSecret`). On macOS and Windows the OS chooses the
+installed Lody handler: Lody's bundle and installer claim `lody://`. On
+Linux `agent.OpenLody` runs Lody itself with the link, as its desktop
+entry's Exec says (`ai.lody.desktop.desktop`, then Nightly's
+`ai.lody.desktop.nightly.desktop` and OSS's `dev.loro.lody.oss.desktop`,
+in `$XDG_DATA_HOME/applications` before `$XDG_DATA_DIRS`): Lody's AppImage
+writes that entry at each start but makes itself the default only for its
+channel's scheme (`ai.lody.stable`), so `lody://` reaches it only through
+a desktop database that nothing builds without `update-desktop-database`,
+and xdg-open's gio answers "Operation not supported" while Lody runs
+(Atlas, 10-25). A second Lody hands the link to the running one. With no
+entry whose program is there, the link goes to xdg-open as before
+(`TestOpenLodyRunsItsDesktopEntry`, `TestLodyImportRunsLodyOnLinux`). The
+web page opens the link from the browser and is not covered. A compatible
+Lody build must implement this import route.
Lody asks which Providers to add, names them with `-magpie`, and writes only
-to the current desktop's local machine after confirmation. Magpie does not
+to the current desktop's local machine after confirmation (Lody 0.108.1:
+the import's machine is always the desktop's own, and its link names no
+other). So a machine Lody Desktop reaches as a remote, one that runs only
+Lody's command-line agent, takes no import from magpie running there:
+nothing on it opens `lody://`, and a link opened on the desktop's computer
+would point that computer's agents at its own loopback. Lody Desktop
+opened on that machine itself shares the agent's `~/.lody/machine-id`, so
+the import lands on the same machine (Atlas, 10-25). Magpie does not
read or write Lody's private document store, select models there, or claim
that opening the link completed the import. Lody exposes no public status
reader: `Agent.Added` remains nil and the JSON row omits `added`. The GUI
diff --git a/internal/agent/agentenv_test.go b/internal/agent/agentenv_test.go
index 6c16903c5..e7ad344ab 100644
--- a/internal/agent/agentenv_test.go
+++ b/internal/agent/agentenv_test.go
@@ -26,6 +26,7 @@ var notAnAgent = map[string]string{
"USER": "the process' own",
"XDG_CONFIG_HOME": "magpie's own folder's, which appdir decides",
"XDG_DATA_HOME": "the same",
+ "XDG_DATA_DIRS": "the system's own data folders, where a .deb puts Lody's desktop entry; a test that reaches agent.OpenLody sets it to its own",
"XDG_CACHE_HOME": "Go's build cache's too, which a sandbox keeps shared; Air's ACP OpenCode under it is found by path in a test's own home (#2423)",
"MAGPIE_ADDR": "magpie's own",
"MAGPIE_DEBUG": "magpie's own debug flag",
diff --git a/internal/agent/lody.go b/internal/agent/lody.go
index 6470ac91a..737cc53fd 100644
--- a/internal/agent/lody.go
+++ b/internal/agent/lody.go
@@ -5,12 +5,15 @@ import (
"encoding/json"
"net/url"
"os"
+ "os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
+ "github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/gateway"
+ "github.com/yetone/magpie/internal/proc"
)
// LodyScheme opens Lody's local-machine provider confirmation dialog.
@@ -128,3 +131,157 @@ func lody(home string) *Agent {
// click, generated runtime files, or Lody's private document store.
}
}
+
+// lodyEntries are the desktop entries Lody's Linux builds go by, stable
+// first: ai.lody.desktop (Lody, from its .deb in /usr/share/applications,
+// or written by its AppImage to $XDG_DATA_HOME/applications at each
+// start), ai.lody.desktop.nightly (Lody Nightly) and dev.loro.lody.oss
+// (Lody OSS).
+var lodyEntries = []string{"ai.lody.desktop.desktop", "ai.lody.desktop.nightly.desktop", "dev.loro.lody.oss.desktop"}
+
+// lodyGOOS is the system OpenLody hands the link over on; tests stand in
+// another.
+var lodyGOOS = runtime.GOOS
+
+// startLody starts Lody with the link and reaps it once it ends; tests
+// stand in for it.
+var startLody = func(cmd *exec.Cmd) error {
+ if err := cmd.Start(); err != nil {
+ return err
+ }
+ go cmd.Wait() //nolint:errcheck
+ return nil
+}
+
+// OpenLody hands Lody's import link to the Lody installed here. On Linux
+// it runs that Lody with the link itself, as its desktop entry says, which
+// is what xdg-open runs when lody:// is registered: Lody's AppImage makes
+// itself the default only for its own channel's scheme (ai.lody.stable),
+// and leaves lody:// to the desktop database, which nothing builds without
+// update-desktop-database, so xdg-open, gio and a browser turn the link
+// away though Lody is running (Atlas, 10-25: "failed to open uri"). A
+// second Lody passes the link to the running one and quits. With no entry
+// of Lody's, and on macOS and Windows, where Lody's bundle and installer
+// claim lody:// themselves, open opens it.
+func OpenLody(link string, open func(string) error) error {
+ if argv := lodyCommand(link); argv != nil {
+ if err := startLody(proc.Command(argv[0], argv[1:]...)); err == nil {
+ return nil
+ }
+ }
+ return open(link)
+}
+
+// lodyCommand is the command Lody's desktop entry runs for link, nil with
+// no entry whose program is there.
+func lodyCommand(link string) []string {
+ if lodyGOOS == "darwin" || lodyGOOS == "windows" {
+ return nil
+ }
+ data := appdir.Getenv("XDG_DATA_HOME")
+ if data == "" {
+ if home, err := os.UserHomeDir(); err == nil {
+ data = filepath.Join(home, ".local", "share")
+ }
+ }
+ dirs := []string{data}
+ sys := os.Getenv("XDG_DATA_DIRS")
+ if sys == "" {
+ sys = "/usr/local/share:/usr/share"
+ }
+ for _, d := range filepath.SplitList(sys) {
+ if filepath.IsAbs(d) {
+ dirs = append(dirs, d)
+ }
+ }
+ for _, name := range lodyEntries {
+ // the first folder with an entry of that name has the one that
+ // counts, as for xdg-open
+ for _, dir := range dirs {
+ if dir == "" {
+ continue
+ }
+ b, err := os.ReadFile(filepath.Join(dir, "applications", name))
+ if err != nil {
+ continue
+ }
+ if argv := desktopExec(string(b), link); argv != nil {
+ return argv
+ }
+ break
+ }
+ }
+ return nil
+}
+
+// desktopExec is the command a desktop entry's Exec runs for one URL, as
+// the Desktop Entry Specification reads it: arguments in double quotes with
+// backslash escapes, %u and %U the URL, %% a %, the other field codes
+// dropped, and the URL put last when Exec takes none. It is nil with no
+// Exec, or with no program there to run.
+func desktopExec(entry, link string) []string {
+ line := ""
+ 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 && strings.TrimSpace(k) == "Exec" {
+ line = strings.TrimSpace(v)
+ break
+ }
+ }
+ var argv []string
+ var arg strings.Builder
+ has, quoted, gaveURL := false, false, false
+ for i := 0; i < len(line); i++ {
+ switch c := line[i]; {
+ case quoted && c == '\\' && i+1 < len(line):
+ i++
+ arg.WriteByte(line[i])
+ case c == '"':
+ quoted, has = !quoted, true
+ case !quoted && (c == ' ' || c == '\t'):
+ if has {
+ argv = append(argv, arg.String())
+ }
+ arg.Reset()
+ has = false
+ case c == '%' && i+1 < len(line):
+ i++
+ switch line[i] {
+ case '%':
+ arg.WriteByte('%')
+ has = true
+ case 'u', 'U':
+ arg.WriteString(link)
+ has, gaveURL = true, true
+ }
+ default:
+ arg.WriteByte(c)
+ has = true
+ }
+ }
+ if has {
+ argv = append(argv, arg.String())
+ }
+ if len(argv) == 0 {
+ return nil
+ }
+ if !gaveURL {
+ argv = append(argv, link)
+ }
+ if !filepath.IsAbs(argv[0]) {
+ p, err := proc.LookPath(argv[0])
+ if err != nil {
+ return nil
+ }
+ argv[0] = p
+ } else if st, err := os.Stat(argv[0]); err != nil || st.IsDir() || st.Mode()&0111 == 0 {
+ // an AppImage moved or deleted since Lody wrote its entry
+ return nil
+ }
+ return argv
+}
diff --git a/internal/agent/lody_test.go b/internal/agent/lody_test.go
index ec0eb3808..bd3896838 100644
--- a/internal/agent/lody_test.go
+++ b/internal/agent/lody_test.go
@@ -6,6 +6,7 @@ import (
"net"
"net/url"
"os"
+ "os/exec"
"path/filepath"
"reflect"
"strings"
@@ -238,3 +239,133 @@ func loopbackHost(h string) bool {
ip := net.ParseIP(h)
return ip != nil && ip.IsLoopback()
}
+
+// Lody on Linux is run with its import link as its desktop entry says.
+// Its AppImage makes itself the default only for ai.lody.stable://, so
+// lody:// reaches it only through a desktop database that nothing builds
+// without update-desktop-database, and xdg-open turned the link away
+// while Lody ran (Atlas, 10-25: "failed to open uri"). The entries are
+// the ones Lody 0.108.1 writes: its AppImage's, with the path quoted and
+// --no-sandbox kept, and its .deb's.
+func TestOpenLodyRunsItsDesktopEntry(t *testing.T) {
+ home := t.TempDir()
+ t.Setenv("HOME", home)
+ data := filepath.Join(home, "data")
+ sys := filepath.Join(home, "sys")
+ t.Setenv("XDG_DATA_HOME", data)
+ t.Setenv("XDG_DATA_DIRS", sys)
+ oldOS, oldStart := lodyGOOS, startLody
+ t.Cleanup(func() { lodyGOOS, startLody = oldOS, oldStart })
+ lodyGOOS = "linux"
+ var ran []string
+ startErr := error(nil)
+ startLody = func(cmd *exec.Cmd) error {
+ if startErr == nil {
+ ran = cmd.Args
+ }
+ return startErr
+ }
+ var opened string
+ open := func(u string) error { opened = u; return nil }
+ link := LodyLink("http://127.0.0.1:3425")
+
+ program := func(path string) string {
+ if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(path, []byte("#!/bin/sh\n"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ return path
+ }
+ entry := func(dir, name, execLine string) string {
+ p := filepath.Join(dir, "applications", name)
+ if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil {
+ t.Fatal(err)
+ }
+ body := "[Desktop Entry]\nName=Lody\nExec=" + execLine + "\nTerminal=false\nType=Application\nMimeType=x-scheme-handler/ai.lody.stable;x-scheme-handler/lody;\n"
+ if err := os.WriteFile(p, []byte(body), 0644); err != nil {
+ t.Fatal(err)
+ }
+ return p
+ }
+ try := func(want []string) {
+ t.Helper()
+ ran, opened = nil, ""
+ if err := OpenLody(link, open); err != nil {
+ t.Fatal(err)
+ }
+ if want == nil {
+ if ran != nil || opened != link {
+ t.Fatalf("ran %q and opened %q, want the link opened", ran, opened)
+ }
+ return
+ }
+ if !reflect.DeepEqual(ran, want) || opened != "" {
+ t.Fatalf("ran %q and opened %q, want %q run", ran, opened, want)
+ }
+ }
+
+ try(nil) // no Lody entry: opened as before
+
+ appImage := program(filepath.Join(home, "Apps $1", `Lody "x".AppImage`))
+ stable := entry(data, "ai.lody.desktop.desktop", `"`+strings.NewReplacer(`\`, `\\`, `"`, `\"`, `$`, `\$`, "`", "\\`", "%", "%%").Replace(appImage)+`" "--no-sandbox" %U`)
+ try([]string{appImage, "--no-sandbox", link})
+
+ // the .deb's entry, under XDG_DATA_DIRS, counts only after the user's own
+ deb := program(filepath.Join(home, "opt", "Lody", "lodyDesktop"))
+ entry(sys, "ai.lody.desktop.desktop", deb+" %U")
+ try([]string{appImage, "--no-sandbox", link})
+ // an AppImage deleted since Lody wrote its entry runs nothing, and the
+ // system's entry of that name doesn't stand in for it
+ if err := os.Remove(appImage); err != nil {
+ t.Fatal(err)
+ }
+ try(nil)
+ if err := os.Remove(stable); err != nil {
+ t.Fatal(err)
+ }
+ try([]string{deb, link})
+
+ // Nightly's, with an Exec that takes no URL: it goes last
+ if err := os.RemoveAll(filepath.Join(sys, "applications")); err != nil {
+ t.Fatal(err)
+ }
+ nightly := program(filepath.Join(home, "nightly", "lody"))
+ entry(data, "ai.lody.desktop.nightly.desktop", nightly+" --no-sandbox")
+ try([]string{nightly, "--no-sandbox", link})
+
+ // a Lody that won't start: the link is opened as before
+ startErr = os.ErrPermission
+ try(nil)
+ startErr = nil
+
+ // macOS and Windows claim lody:// from Lody's bundle and installer
+ for _, goos := range []string{"darwin", "windows"} {
+ lodyGOOS = goos
+ try(nil)
+ }
+}
+
+func TestDesktopExec(t *testing.T) {
+ sh := "/bin/sh"
+ for _, c := range []struct {
+ entry string
+ want []string
+ }{
+ {"[Desktop Entry]\nExec=/bin/sh -c 'x' %U\n", []string{sh, "-c", "'x'", "L"}},
+ {"[Desktop Entry]\nExec=\"/bin/sh\" \"100%%\" %u %f %i\n", []string{sh, "100%", "L"}},
+ {"[Desktop Action new]\nExec=/bin/false\n[Desktop Entry]\nExec=sh %U\n", []string{sh, "L"}},
+ {"[Desktop Entry]\nName=Lody\n", nil},
+ {"[Desktop Entry]\nExec=/no/such/lody %U\n", nil},
+ {"[Desktop Entry]\nExec=no-such-lody-program %U\n", nil},
+ } {
+ got := desktopExec(c.entry, "L")
+ if len(got) > 0 && got[0] != sh && filepath.Base(got[0]) == "sh" {
+ got[0] = sh // sh found on PATH
+ }
+ if !reflect.DeepEqual(got, c.want) {
+ t.Errorf("%q: %q, want %q", c.entry, got, c.want)
+ }
+ }
+}
diff --git a/internal/cli/main.go b/internal/cli/main.go
index bfa3977ad..dd5c993ff 100644
--- a/internal/cli/main.go
+++ b/internal/cli/main.go
@@ -478,7 +478,12 @@ func run(args []string) error {
// Lody takes only a gateway on this computer's loopback
return fmt.Errorf("%s takes magpie only at a loopback address, and the gateway is at %s: nothing was opened", a.Name, gateway.URL())
}
- openInBrowser(link)
+ if a.ID == "lody" {
+ // Lody on Linux is run with the link (agent.OpenLody)
+ _ = agent.OpenLody(link, func(u string) error { openInBrowser(u); return nil })
+ } else {
+ openInBrowser(link)
+ }
fmt.Println(green.Render("✓"), bold.Render(a.Name), muted.Render("opened to add magpie — confirm it there"))
fmt.Println(muted.Render(" " + link))
return nil
diff --git a/internal/gui/lody_test.go b/internal/gui/lody_test.go
index c1e6082c7..4225e405e 100644
--- a/internal/gui/lody_test.go
+++ b/internal/gui/lody_test.go
@@ -6,8 +6,10 @@ import (
"net/http/httptest"
"os"
"path/filepath"
+ "runtime"
"strings"
"testing"
+ "time"
"github.com/yetone/magpie/internal/agent"
)
@@ -21,6 +23,9 @@ func (w *importWindows) OpenURL(u string) error { w.opened = u; return nil }
func TestLodyImportHandoff(t *testing.T) {
home := sandboxHome(t)
+ // no Lody desktop entry of the machine's is run (agent.OpenLody)
+ t.Setenv("XDG_DATA_HOME", filepath.Join(home, ".local", "share"))
+ t.Setenv("XDG_DATA_DIRS", filepath.Join(home, "share"))
if err := os.MkdirAll(filepath.Join(home, ".lody"), 0700); err != nil {
t.Fatal(err)
}
@@ -86,3 +91,56 @@ func TestLodyImportHandoff(t *testing.T) {
t.Fatalf("handoff modified Lody's catalog: %q %v", b, err)
}
}
+
+// On Linux the window's Add magpie runs Lody with its link, as Lody's
+// desktop entry says, rather than handing lody:// to xdg-open, which turned
+// it away while Lody ran (Atlas, 10-25: "failed to open uri"): Lody's
+// AppImage makes itself the default only for ai.lody.stable://. The entry
+// is the one Lody 0.108.1's AppImage writes, with a fake AppImage.
+func TestLodyImportRunsLodyOnLinux(t *testing.T) {
+ if runtime.GOOS != "linux" {
+ t.Skip("desktop entries are Linux's; macOS and Windows open lody:// (agent.TestOpenLodyRunsItsDesktopEntry)")
+ }
+ home := sandboxHome(t)
+ data := filepath.Join(home, ".local", "share")
+ t.Setenv("XDG_DATA_HOME", data)
+ t.Setenv("XDG_DATA_DIRS", filepath.Join(home, "share"))
+ got := filepath.Join(home, "got")
+ appImage := filepath.Join(home, "Applications", "Lody.AppImage")
+ if err := os.MkdirAll(filepath.Dir(appImage), 0700); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(appImage, []byte("#!/bin/sh\nprintf '%s\\n' \"$@\" > "+got+"\n"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ apps := filepath.Join(data, "applications")
+ if err := os.MkdirAll(apps, 0700); err != nil {
+ t.Fatal(err)
+ }
+ entry := "[Desktop Entry]\nName=Lody\nExec=\"" + appImage + "\" \"--no-sandbox\" %U\nTerminal=false\nType=Application\nMimeType=x-scheme-handler/ai.lody.stable;x-scheme-handler/lody;\nCategories=Utility;\n"
+ if err := os.WriteFile(filepath.Join(apps, "ai.lody.desktop.desktop"), []byte(entry), 0644); err != nil {
+ t.Fatal(err)
+ }
+ link := agent.LodyLink("http://127.0.0.1:3425")
+ w := &importWindows{}
+ mux := http.NewServeMux()
+ providerRoutes(mux, w)
+ body, _ := json.Marshal(map[string]string{"url": link})
+ rec := httptest.NewRecorder()
+ mux.ServeHTTP(rec, httptest.NewRequest("POST", "/api/open", strings.NewReader(string(body))))
+ if rec.Code != http.StatusNoContent {
+ t.Fatalf("open answered %d %s", rec.Code, rec.Body)
+ }
+ if w.opened != "" {
+ t.Fatalf("the link went to xdg-open: %s", w.opened)
+ }
+ want := "--no-sandbox\n" + link + "\n"
+ var b []byte
+ for deadline := time.Now().Add(30 * time.Second); string(b) != want && time.Now().Before(deadline); {
+ time.Sleep(20 * time.Millisecond)
+ b, _ = os.ReadFile(got)
+ }
+ if string(b) != want {
+ t.Fatalf("Lody ran with %q, want %q", b, want)
+ }
+}
diff --git a/internal/gui/providers.go b/internal/gui/providers.go
index 1fe10177d..cd675d6c0 100644
--- a/internal/gui/providers.go
+++ b/internal/gui/providers.go
@@ -2332,8 +2332,14 @@ func providerRoutes(mux *http.ServeMux, w Windows) {
// and System Settings' Login Items, where Open at login is allowed
// again: that one pane, not System Settings' scheme at large
if strings.HasPrefix(in.URL, "https://") || strings.HasPrefix(in.URL, "http://") || strings.HasPrefix(in.URL, agent.CindyScheme) || strings.HasPrefix(in.URL, agent.LodyScheme) || in.URL == autostart.LoginItemsURL {
+ open := w.OpenURL
+ if strings.HasPrefix(in.URL, agent.LodyScheme) {
+ // Lody on Linux is run with the link, which xdg-open
+ // turned away (agent.OpenLody)
+ open = func(u string) error { return agent.OpenLody(u, w.OpenURL) }
+ }
// Open again on a sign-in says why it didn't open (#2043)
- if err := w.OpenURL(in.URL); err != nil {
+ if err := open(in.URL); err != nil {
fail(rw, err)
return
}