ac4af582301a4e8a29245dbba767eba31334ddf9
8
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
ab25a2317b |
autostart: on Linux the switch follows a package's own entry (#3372)
mtul0729, packaging magpie for nixpkgs (#3372, COSMIC on Wayland, v0.1.1334): a package that ships its own autostart entry ($XDG_CONFIG_DIRS/autostart/magpie.desktop, /etc/xdg when unset) has magpie open at login, but enabled() read only $XDG_CONFIG_HOME/autostart, so Open at login said off over it. Off removed a user file that wasn't there, and the package's entry went on starting magpie. On wrote this copy's own path over the package's entry: under a package manager a versioned path, gone once the package is upgraded and collected. The package doc says the switch "can't say one thing while the system does another"; on such a system it did. Now, as the XDG Autostart spec has it: - The entry that counts is the user's, else the first of the $XDG_CONFIG_DIRS folders (/etc/xdg when unset or empty) that has one. A relative folder has none, as the spec says, and neither has a file listed there, a folder this user can't search or a name too long, which systemd's generator and GLib can't open either. The user's own folder listed there too, by any name (os.SameFile, so /home linked to /var/home too), is still the user's. - An entry is read as GLib's key file and systemd's parser read it: the keys of its [Desktop Entry] group alone (a [Desktop Action]'s Exec is not the entry's), spaces around = and CRLF lines as any others, the last of a key given twice, true or 1 for Hidden. It opens magpie when it is an application (Type=Application) with an Exec other than true (below), not Hidden. - X-GNOME-Autostart-enabled=false (or 0) reads off too, as the issue asks. GNOME's and Cinnamon's sessions read it; systemd's generator (KDE's and COSMIC's sessions under systemd) doesn't, so there such an entry reads off while it starts. - Off, over a package's entry that some desktop starts, writes an entry of the user's that hides it; that includes one GNOME has off, which systemd's generator still starts. Otherwise off removes the user's entry, as before. On, over a package's entry that starts magpie, removes the user's entry and writes no path; otherwise (no package entry, a hidden one, or one GNOME has off) it writes this copy's entry, as before. The hiding entry is "[Desktop Entry] Type=Application Name=magpie Exec=true Hidden=true". A session holds back the entries of a name only once it has read one, and each reads differently (from their source): - gnome-session (main and 42.0) and cinnamon-session load an entry with g_desktop_app_info_new_from_filename. GLib (main, g_desktop_app_info_load_from_keyfile) refuses one with no Type or Name, or whose Exec names a program it can't find, and the session skips an entry it can't load, so the package's starts. - mate-session's egg_desktop_file_new refuses an application entry with no Exec, and the entry then takes no app id. - cosmic-session without systemd never reads Hidden: it starts what Exec names, and takes the name once that ran. - systemd's generator (main, xdg-autostart-service.c) reads Hidden first, and hides an entry it can't parse, which still holds back the rest. true is on every system by that name, so each session reads the entry, and COSMIC's runs true where it would have run magpie. A copy of the package's own Exec would stop holding the package's back on GNOME once its store path is collected. With this box's GLib 2.72.4 (Ubuntu 22.04's) and 2.84.2 (anaconda's), through ctypes: Exec=true and no Exec load, hidden; an Exec naming a store path that is gone, and a bare "[Desktop Entry] Hidden=true", don't load. An entry that runs true opens nothing, so it reads off: XFCE's Session and Startup lists the hiding entry as magpie, unticked, and ticking it there writes Hidden=false into it (xfae-model.c), which runs true, not magpie. On takes it away for the package's. What is no entry, and what can't be read: a path where no regular file is (a folder, a link that leads nowhere or round in a loop) is no entry, as systemd's generator and GLib pass over it, and on leaves a folder there alone. A file that can't be read (EACCES, EPERM, EIO) counts and starts nothing, as systemd's generator has it; GLib passes over it, so where the first entry is unreadable GNOME starts the next folder's while Open at login reads off. Any other failed read, magpie out of files or memory, says nothing of what the desktop reads at login: Set returns it and writes nothing, and Open at login reads on, as the package's doc has it for a system that can't be asked (Windows' Run key that can't be read is not a no either). The user's entry is written where the desktop reads it. A link there (to magpie's launcher, which install.sh writes, the usual way to have an app open at login; or into a read-only store), or a hard link, is replaced by the entry, not written through: edit.WriteAtomic writes a link's target, and a hard-linked file in place, so off over a package's entry would have left the launcher running true, and failed over a read-only target with the switch still on. Main's enable() wrote through such a link too, when on was asked over one (a linked launcher already reads on); it goes through the same write now. Old entries: a user entry an older magpie wrote stays and still counts over a package's, as before. The switch reads on over it, so in the app taking it away is off, then on; `magpie autostart on` does it in one step. One whose store path was collected reads on as well, though systemd's generator starts nothing for it and still holds the package's back, so on COSMIC or KDE magpie doesn't open while the switch says on; GLib passes over it, and GNOME starts the package's. The daemon and the terminal: `magpie daemon install` refuses while magpie opens at login, so the app and the service don't both serve the gateway, and `magpie tui` quitting hands the gateway on (keepGateway, |
||
|
|
75e78a5a04 |
tui: quitting hands the gateway to magpie's service on Linux
zaidu259434 on X: "启动codex这些cli连接会失败,AI分析没自动启动magpie的网关".
With magpie installed inside WSL (or on a server over SSH), nothing
opens magpie at login, and XDG autostart doesn't run there. So once
`magpie tui` quit, the agents it had connected asked a gateway that was
gone.
When the terminal quits having served the gateway with an agent
connected, on Linux and where magpie doesn't open at login, keepGateway:
- starts magpie's service (magpie daemon) when it is installed;
- installs it when the user never removed it (daemon.Removed: Uninstall
now leaves a daemon-removed mark, and Install clears it), so it starts
with the system or the login from then on, and says how to remove it;
- otherwise (no systemd, which is WSL's default, or the user removed it)
starts `magpie serve` in its own session, logging to magpie's cache,
and says so. In WSL it also says how to turn systemd on.
macOS, Windows, nothing connected, or magpie opening at login: the same
"gateway stopped" note as before. Docs: gui-shell.
Run:
- TestKeepGatewayAsTheTerminalQuits covers each of those cases, and
TestUninstallRecordsTheUsersNo checks the mark.
- go test -timeout 45m on the four packages, the same run as the
previous commit's: all four ok (gateway 428s, agent 255s, tui 23s, daemon 16s), at
|
||
|
|
5e30d5b5ae |
autostart, gui: Open at login says when the Mac's own switch for it is off (#2120)
Anoninz on #2120: Settings said Open at login was On while System Settings › General › Login Items › Allow in the Background had magpie switched off, so magpie never opened at login. autostart.Enabled now returns a State: off, on, or needs allowing. - macOS 13+: with the plist present, Enabled asks Background Task Management through SMAppService statusForLegacyURL: (cgo, ServiceManagement weak-linked, minos stays 12.0). RequiresApproval reads as needs allowing. Any other answer, a failed ask (-1), macOS 12 or a build without cgo (the darwin CLI release) leaves the plist alone deciding, as before: a failed read is never "off". launchctl print-disabled did not follow the switch on macOS 26.2, so it isn't used. - BTM matches the item by the plist's Label, not its path: the tests ask about a label of their own, and TestMain stubs the ask so no test reads the user's real item. - GUI: Settings › General keeps On and adds one line, "Switched off in System Settings’ Login Items…", with an "Open Login Items" button that opens x-apple.systempreferences:com.apple.LoginItems-Settings.extension through /api/open (that one URL is allowed; other x-apple URLs aren't). The window's focus reload clears it once switched back on. The row is hidden in magpie web, as before. Strings in zh, zh-TW, ja, de. - CLI `magpie autostart` says where to switch it on. The daemon counts a held-back record as on (it is the user's choice, held by the system). Part B (SMAppService.mainApp) is not in this commit: it can't be verified here without registering in the user's real login items and logging out to see a login launch stay tray-only. Run: - go test ./internal/autostart (cgo and CGO_ENABLED=0), cli, daemon, gui after rebasing on origin/main |
||
|
|
d9ca84a2f4 |
gateway: built-in TLS, magpie serve/web/daemon install --tls-cert --tls-key, reloaded on renewal (RoderickZH)
magpie serve, magpie web and magpie daemon install take --tls-cert <file> --tls-key <file> (or MAGPIE_TLS_CERT / MAGPIE_TLS_KEY). The gateway and the web UI on its listener then serve HTTPS to other computers. Without them nothing changes. - Half a pair, a file that doesn't parse, or a flag with no value is refused at start with a message naming the missing flag. - The pair is re-read when either file's mtime or size changes (certbot renewal), with no restart. A renewal that doesn't parse keeps the certificate already served and is logged once. - Other computers get HTTPS only. Plain HTTP from them gets net/http's 400 "Client sent an HTTP request to an HTTPS server". - This computer (loopback, or a peer address equal to the local one) may still use plain http on the same port. Its connections are sniffed on their own goroutine (a first byte of 0x16 is TLS) for up to 10s, so a silent client holds nobody else up. gateway.URL() stays http://127.0.0.1:<port>, which agents, MCP servers, ServedBy, the healthcheck and daemon status all use, and no certificate covers loopback. - WSL NAT and containers count as other computers. - ALPN is http/1.1 only (hijacked connections and websockets need it), with TLS 1.2 as the minimum. - Printed links, LAN addresses, publicURL, the serve/web banners and daemon install show https:// for other computers' addresses. - The DNS-rebinding Host checks (foreignPage, rebound, the MCP loopbackHost) don't depend on the scheme and are unchanged. - daemon install carries MAGPIE_TLS_* into the unit or plist. A reinstall or `daemon web on` that names no certificate keeps the installed pair, and --no-tls removes it. - Docs: reference.md gets an HTTPS section that names Caddy as the alternative, gateway-routing.md gets a TLS row, and docker.html (en, zh, ja) gets the env rows. What was run: - New tests: internal/gateway/tls_test.go (others HTTPS-only, this computer both, a silent local client holds no other, renewed certificate served, CheckTLS, addresses, Relisten keeps TLS), internal/cli/tls_cli_test.go and daemon TestInstallCarriesTLS (linux and darwin, paths with space, %, & and "). - Each was broken by hand and went red: no wrap in listen, no reload, a synchronous sniff, a bad renewal dropping the certificate, http in publicURL or the pinned LANURLs, the carried entries removed, and keepTLS as a no-op. - Real use under a sandbox HOME, with a self-signed pair, on 0.0.0.0:3461: - Half a pair, a garbage certificate, a flag with no value, and the env set to half a pair each exit 1 with the message. - curl -k https://127.0.0.1 and https://<LAN IP>/v1/models with a key: 200. Plain http on loopback: 200. - The cert files were swapped for a second pair while it ran. openssl s_client then showed the new CN and serial, and curl --cacert with the new pair got 200. A junk key file then kept the second pair, logged once. - From the Windows box over the LAN: plain http got 400; https with the key got 200, and without it 401. - magpie web --addr 0.0.0.0:3462: https network links. From Windows, http got 400 and https with the key got 303. - gofmt -l clean. go vet passed on darwin, GOOS=linux -tags nogui and GOOS=windows. - go test -tags nogui for ./internal/cli, ./internal/daemon, and the TLS tests of ./internal/gateway and ./internal/gui: all pass at the rebased head. - Two reds in the full suite (`go test -tags nogui ./...`) are the same on origin/main f074996d9; both are filed: - TestGatewayHeldByAContainer failed 2 of 5 there (#2107). - The gateway package runs past go test's default 10m under load: 1208s on main and 1221s here, both passing with -timeout 40m (#2108). |
||
|
|
82f3befd29 |
daemon: magpie daemon install --web serves the browser page from the service, with a link that lasts (HuanCheng Bai)
HuanCheng Bai on Discord runs `magpie daemon` on a Linux server and asked
for the daemon to serve the web page too, or for an option to turn it on.
`magpie web` run beside it is a second process, and he read it as not
seeing the daemon's state.
- `magpie serve --web[=host:port]` serves the gateway and `magpie web`'s
page from one process (stats, StartWeb, Wait). The default page address
is 127.0.0.1:3430. Any other argument is refused, where before it was
ignored.
- `magpie daemon install --web | --lan | --addr host:port | --no-web` and
`magpie daemon web [on|off] [--lan] [--addr host:port]`. The unit's
ExecStart and the plist's ProgramArguments become `serve --web=<addr>`,
and the key goes into the 0600 file's environment as MAGPIE_WEB_KEY. The
key is made once. A reinstall with no flags keeps the page and its key
(read back from the file by daemon.Web), so the link survives restarts
and reinstalls. `web off` drops it. MAGPIE_WEB_KEY in the shell, when
set, is used and checked like magpie web's. The page defaults to
loopback; --lan or a non-loopback --addr prints the network links and
magpie web --lan's warning.
- `magpie daemon status`, install and `daemon web` print the page's link.
- `magpie web` while the service serves a page prints that page's link and
exits. Beside a gateway another magpie serves, it says so and points to
`magpie daemon web on`.
- docs/reference.md (the service section and the Use list), and the
site's start page (en, zh, ja). The READMEs don't cover the daemon.
Run:
- Tests: TestInstallWithWeb (systemd and launchd, plutil lint, read back,
reinstall without the page, the shell's key not carried, no key
refused), TestWebNotInstalled, TestParseDaemon, TestWebOptions,
TestDaemonStatusPrintsPage and TestServeArgs. Each of these went red
with its line reverted by hand: withKey, prevKey in webOptions, and the
status page line.
- Sandbox HOME with a fake launchctl:
- `daemon install --web --lan` wrote a 0600 plist with
`serve --web=0.0.0.0:3430` and the key. plutil said OK, and it printed
the link, the network links and the warning.
- Reinstalling with no flags kept the key. `daemon status` showed the
link.
- `daemon web off` / `daemon web` / `daemon web on` behaved as above,
with a new key after off.
- `daemon web bogus` and `serve --lan` gave the usage errors.
- `serve --web=127.0.0.1:3462` served the gateway (3461) and the page in
one process. The page answered 200 with the key and 401 without, and the
page and /v1/models both showed 3 models. `magpie web --no-open` beside
it printed the service's link and exited 0.
- gofmt -l; go vet (gui), and go vet -tags nogui for darwin, linux and
windows; go test -tags nogui ./... in a sandbox HOME.
Not tried: a real systemd on Linux. The unit text is checked by test only.
|
||
|
|
c6de0d2a54 |
daemon, agentenv: the service tools run through proc, and PI_DESKTOP_DATA_DIR is cleared in every sandbox
main has been red on ubuntu and macOS since 692a779d3's run (38376844048),
with two guards that each caught a commit run on chosen packages only:
- TestNoCommandBypassesProc: 5617281f5's daemon.run called exec.Command,
so on Windows the service tools would open a console window. It now uses
proc.Command.
- TestFolderVarsAreListed:
|
||
|
|
05edac2c36 |
daemon: the service's PATH has the Node a terminal gets, nvm's default among them (bookyue on Discord)
What changed: `magpie daemon install` now widens PATH before writing it into the systemd unit or launch agent, the way the app does when opened from the Dock (proc.UserPath): the version managers' and npm's folders that exist, nvm's default Node (not its newest), then the login shell's own PATH (proc.LoginPath), each after the folders the install's shell already had. When no node is found even then, the install says so. Why: the service has only the PATH written into it. With nvm loaded lazily, or an install run over ssh without a login shell, that PATH had no node, and plugins (and the MCP servers and agent CLIs they start) couldn't find it under systemd (bookyue, #feedback 838099). Run: gofmt -l; go vet darwin, linux -tags nogui, windows (daemon, cli); go test ./internal/daemon ./internal/cli -count=1 on macOS; the daemon test binary for linux/arm64 in debian:bookworm-slim (all pass). TestInstallFindsNvmNode uses the real widening with a sandbox HOME holding nvm v20 (default) and v22: the unit's PATH has v20's bin, not v22's, after the shell's own. With the widenPath() call removed by hand it fails (PATH is the shell's alone, Node() empty). Not run: a real systemd install with nvm since this change (the earlier container run, before it, covered install/restart/linger); not tried with fnm or volta, which UserPath handles the same way for the app. |
||
|
|
5617281f55 |
cli: magpie daemon install|uninstall|start|stop|restart|status keeps the gateway running as a service (HuanCheng on Discord)
What changed: a new internal/daemon package and `magpie daemon`, which run
`magpie serve` as the user's own service, with no window and no desktop
session.
- Linux: a systemd user unit at ~/.config/systemd/user/magpie.service
(Restart=on-failure), driven by systemctl --user. status reads
`systemctl --user show`, and says `sudo loginctl enable-linger $USER`
when the user doesn't linger. su/sudo without the user's bus, no
systemd, and WSL without systemd each get an error that says what to
do.
- macOS: a launch agent, com.yetone.magpie.serve (RunAtLoad, KeepAlive on
failure), loaded with launchctl bootstrap gui/$UID. Its log is
~/Library/Logs/magpie/serve.log.
- Windows and Android: refused, pointing to `magpie autostart on` (or to
WSL).
- The service carries PATH, the proxy variables, MAGPIE_*, the XDG folders
and the agents' folder variables from the install shell. The file is
0600 because a proxy can hold a password. It runs magpie by its PATH
name when that is the same file, so Homebrew's link survives updates.
It refuses a go-build binary and an AppTranslocation copy.
- Install is refused while `magpie autostart` is on: the tray app serves
the gateway too, and the two would crash-loop on the port.
- Docs: a reference.md section, and one sentence on the site's start page
(en, zh, ja).
Why: users on headless Linux boxes asked for a supported way to keep
`magpie serve` up across reboots instead of hand-written units. The
existing `magpie autostart` needs a desktop session (an XDG autostart
entry).
What was run:
- gofmt -l; go vet on darwin, GOOS=linux -tags nogui, and GOOS=windows.
- go test ./internal/daemon ./internal/cli on macOS, at origin/main
|