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 6874095b1 plus this change, before the rebase
- GOOS=linux and GOOS=windows go vet, and gofmt -l, are clean.
- Real run in the magpie-desktop distro (WSL2, systemd on): a Linux build
from this tree started under setsid as serveInBackground does,
answered 200, and still answered 200 with its pid alive 30s after its
wsl.exe session ended. It was stopped afterwards.
Not tried:
- the interactive TUI quit itself in WSL (over ssh there is no terminal
to drive it), so the message and the path it picks are covered by the
unit test only;
- a real daemon.Install inside WSL (it writes a systemd user unit for the
distro's user).
128 lines
4.0 KiB
Go
128 lines
4.0 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"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"
|
|
)
|
|
|
|
// 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 || k.autostartOn() {
|
|
return gatewayGoneNote()
|
|
}
|
|
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
|
|
}
|
|
|
|
// 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 := filepath.Join(appdir.Cache(), "serve.log")
|
|
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 := exec.Command(exe, "serve")
|
|
cmd.Stdout, cmd.Stderr = f, f
|
|
detach(cmd)
|
|
if err := cmd.Start(); err != nil {
|
|
return 0, "", err
|
|
}
|
|
pid := cmd.Process.Pid
|
|
cmd.Process.Release()
|
|
return 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
|
|
}
|