Files
magpie/internal/tui/keep.go
T
yetone af55a7917f tui: quitting where magpie opens at login says how to have the gateway handed on (#3756)
With #3372, Open at login on Linux reads a package's own autostart entry
in $XDG_CONFIG_DIRS (nixpkgs ships one). JoeyWu-tech's #3756 asked what
`magpie daemon install`'s guard and `magpie tui`'s hand-off as it quits
should count on a box no desktop logs into (NixOS-WSL, a server over SSH),
where nothing ever starts that entry.

Decided: both keep counting what Open at login shows (autostart.Enabled).
A session check can't tell (an SSH login into a desktop box has no
DISPLAY either), and counting less than the switch shows would install
the service beside an app the desktop does start. The install already
refuses with "run `magpie autostart off` first", which is the way out.
The terminal's quit only said "the gateway stopped", with nothing about
why or what to do; it now says magpie opens at login and serves it then,
and that `magpie autostart off` has magpie tui hand it on where no
desktop logs in (atLoginNote). Off Linux and with nothing connected the
note is as before.

What was run (wsl-run, magpie-desktop Ubuntu 24.04, go1.26.8):
  go test -tags nogui -count=1 ./internal/autostart ./internal/daemon
  ./internal/testenv ./internal/tui: ok
  With keep.go as before (the test kept): TestKeepGatewayAsTheTerminalQuits
  red, "app at login: ... the gateway stopped with magpie tui; ..."
  gofmt -l: nothing

Not tried: a real NixOS-WSL login; mtul0729 ran the #3372 patch on real
NixOS + COSMIC, at the systemd generator's level.

Batch run, at tree 104d75f0a (the twelve coffemon ports together, on
origin/main 7d022bdb6), mote, macOS, go1.26.8:
- go test -tags nogui -count=1 -timeout 45m on the packages the batch
  changes (agent, autostart, daemon, gateway, library, proc, provider,
  sessions, testenv, tui, gui): ok each.
- GUI, node --test with Playwright 1.62.0, Chromium and WebKit:
  library-*.test.cjs and i18n-keys.test.cjs, 1044 of 1044 pass.
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... and go build ./...: ok at cdd7c8e46, the same
  batch on origin/main 00098f12b one rebase before.
- The whole go test ./... at cdd7c8e46: 86 packages ok, one red,
  internal/cli TestEveryRouteHasACommandOrIsNamed (GET
  /api/context/reply has no command), red on main too (#5026).
- Linux (wsl-run, Ubuntu 24.04, at 339055896, the batch on f4c664a09):
  proc, autostart, testenv, tui ok; agent: only TestGrokInstalled red
  (#3548); #4033's proc.Look reds (Zed, Copilot, Qoder, Cursor, Omp)
  pass.
- Windows (win-run, Windows 11, at 339055896): proc, autostart, testenv,
  tui ok; library and agent red in the same tests as on origin/main
  f4c664a09 (#4115, #1827, #4786; #5068, #5069, #5070 filed now).
- Rebased onto origin/main fb1f4856a (three commits, no file in
  common with the batch) and run again at the pushed tree: gofmt -l
  nothing, go build ./... and go vet ./... ok; go test -tags nogui
  sessions, agent, autostart, tui, proc, testenv ok; i18n-keys and
  library-* 1044 of 1044 pass.
- Not run before the push: the whole GUI suite (a run at cdd7c8e46 was
  cut off at 1170 of ~1174 tests when mote rebooted; it runs again after
  the tag and any red is filed).
2026-11-03 20:38:11 +08:00

144 lines
4.8 KiB
Go

package tui
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/autostart"
"github.com/yetone/magpie/internal/daemon"
"github.com/yetone/magpie/internal/proc"
)
// On Linux with no desktop session (a WSL distro, a server over SSH)
// nothing opens magpie at login, so once magpie tui quit, the agents it had
// connected asked a gateway that was gone: Codex in WSL "连接会失败,没自动
// 启动magpie的网关" (zaidu259434 on X, with magpie installed in WSL and
// pointed at Windows' magpie as a remote one). As it quits having served
// the gateway with an agent connected, the terminal hands the gateway on:
// to magpie's service (magpie daemon), installed now where systemd runs it
// and the user never removed it, which starts it with the system or the
// user's login from then on; else to a `magpie serve` of its own, in the
// background until the system stops.
// keeper is what keepGateway asks and does; a var for the tests.
type keeper struct {
goos string
autostartOn func() bool
status func() (daemon.Status, error)
removed func() bool
start func() error
install func() error
linger func() string
background func() (pid int, log string, err error)
}
var keep = keeper{
goos: runtime.GOOS,
autostartOn: func() bool { return autostart.Enabled() != autostart.Off },
status: daemon.Get,
removed: daemon.Removed,
start: daemon.Start,
install: func() error { return daemon.Install(daemon.Options{}) },
linger: daemon.Linger,
background: serveInBackground,
}
// keepGateway hands the gateway on as the terminal quits having served it
// (see above), and is what to say of it.
func keepGateway(connected bool) string {
k := keep
if k.goos != "linux" || !connected {
return gatewayGoneNote()
}
if k.autostartOn() {
return atLoginNote()
}
st, err := k.status()
switch {
case err == nil && st.Installed:
if err := k.start(); err == nil {
return "magpie: the gateway goes on in magpie's service (magpie daemon status)"
}
case err == nil && !k.removed():
if err := k.install(); err == nil {
msg := "magpie: the gateway goes on in magpie's service, which now starts with this system or your login, so agents connected to magpie reach it with magpie tui closed; magpie daemon uninstall removes it"
if l := k.linger(); l != "" {
msg += " (" + l + ")"
}
return msg
}
}
pid, log, err := k.background()
if err != nil {
return gatewayGoneNote() + " (couldn't start magpie serve in the background: " + err.Error() + ")"
}
msg := fmt.Sprintf("magpie: the gateway goes on in the background (magpie serve, pid %d, log %s) until this system stops", pid, log)
if wslHere() {
msg += "; for it to start with WSL, turn systemd on with [boot] systemd=true in /etc/wsl.conf and run magpie daemon install"
} else {
msg += "; magpie daemon install starts it with the system"
}
return msg
}
// atLoginNote is said where magpie opens at login, so the terminal left
// the gateway to the app. On Linux that can be a package's own autostart
// entry on a box no desktop logs into (NixOS-WSL, a server over SSH),
// which nothing ever starts (#3756): the note says the switch that has the
// terminal hand the gateway on instead.
func atLoginNote() string {
return gatewayGoneNote() + "; magpie opens at login and serves it then. Where no desktop logs in here, `magpie autostart off` has magpie tui hand it on as it quits"
}
// BackgroundLog is the log of a magpie serve that magpie tui left
// running as it quit (serveInBackground).
func BackgroundLog() string { return filepath.Join(appdir.Cache(), "serve.log") }
// serveInBackground starts `magpie serve` on its own, outliving this
// process and its terminal, its output in a log in magpie's cache.
func serveInBackground() (int, string, error) {
exe, err := os.Executable()
if err != nil {
return 0, "", err
}
log := BackgroundLog()
if err := os.MkdirAll(filepath.Dir(log), 0o700); err != nil {
return 0, "", err
}
f, err := os.OpenFile(log, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
if err != nil {
return 0, "", err
}
defer f.Close()
cmd := proc.Command(exe, "serve")
cmd.Stdout, cmd.Stderr = f, f
detach(cmd)
// reaped if it ends while this process runs: a released one stayed
// <defunct> until it quit
if err := proc.StartReaped(cmd); err != nil {
return 0, "", err
}
return cmd.Process.Pid, log, nil
}
// wslHere: this Linux is a WSL distro.
func wslHere() bool {
b, _ := os.ReadFile("/proc/version")
return strings.Contains(strings.ToLower(string(b)), "microsoft")
}
// connected says one of agents is connected to magpie.
func connected(agents []*agent.Agent) bool {
for _, a := range agents {
if a.Routed != nil && a.Routed() {
return true
}
}
return false
}