rayem1w on X (0.1.2330, macOS) was asked for ~/Library/Logs/magpie/serve.log while chasing a Codex "hit a snag" through the gateway, and had no such folder. Only the launch agent (magpie daemon install, written by launchd) and a gateway magpie tui left running wrote a log; the app opened from the Finder, the Dock or the Start menu logged to a stderr nobody reads, on every system. Now runGUI and runPanel (magpie, app, tray, panel, a window view, a magpie: link) call applog.Start before gui.Run. It sends the standard logger (the gateway, the plugin host, providers and Wails' error handler all log through it) to app.log as well as stderr, and points debug.SetCrashOutput at it so a panic's trace is there too: macOS ~/Library/Logs/magpie/app.log Windows %LOCALAPPDATA%\magpie\logs\app.log Linux $XDG_STATE_HOME/magpie/app.log, else ~/.local/state/magpie/app.log portable data/logs/app.log app.log, not serve.log: on a Mac launchd writes serve.log for the service, another process. The file is 0600. At 5 MiB it becomes app.old.log (two files at most); when another magpie (an update's new process) rotated it already, it is only opened again; when it can't be moved it is emptied. stderr goes first, through a writer that never fails, since io.MultiWriter stops at a failed writer and an app opened from Explorer has no stderr. Secrets: the log calls name header names and paths without queries; no key or token was found logged (grep of log.Print* in gateway, provider, plugin, gui, agent). scrub masks bearer/basic credentials, key/token query parameters and sk-, AIza, gh?_ and JWT shapes anyway. How a user finds it: Settings › About › Log, with the path, Copy and an Open that shows its folder (POST /api/settings/log/reveal; the row is hidden in magpie web, which writes no file). magpie log prints where every log is (the app's, app.old.log, the service's, the tui's background serve.log); --path the app's alone, -n N its last lines, -f follows it across a rotation. Docs: reference.md, gui-shell.md, site start (FAQ "Where is magpie's log?" and the CLI table) and privacy (files on your computer), en/zh/ja. Run: At 227ce6074 on origin/mainfbb1b36d1: - mote: go test ./internal/applog ./internal/appdir ./internal/cli ok; -run TestCoreScriptBudget ./internal/gui ok; GOOS=windows go vet ./internal/... and GOOS=linux go vet -tags nogui ./internal/... ok; gofmt -l clean. GUI, chromium: about-log (5 languages x 1100/420, and no row without a log), about-website, i18n-keys: 24 of 25, the one red "a plugin's words are its own" ("Unmarked") is the same at origin/mainfbb1b36d1(#4524). - WSL (magpie-desktop), webkit: about-log and about-website 21 of 21. - Windows (win-run): applog, appdir, tui ok; cli ok but TestLibraryMCPHub's TempDir cleanup (#4140, mcp-npm, not this change). - WSL full go test -tags nogui -timeout 45m ./... at the previous base03f609e3f: every package ok but reds that fail the same at origin/main03f609e3falone: TestSettingsSaveKeepsWhatItDoesNotSend (#4514), TestNoCommandBypassesProc (#4491), internal/agent's install-detection tests (#4033, #3548), and TestCoreScriptBudget, which was this change (app.js 321 over): its comment is cut to one line and both budgets are raised by the remaining 124 bytes, with no room added. - Broken by hand: with startLog left out of runGUI, TestTheAppWritesItsLog is red ("app wrote no log: open .../Library/Logs/magpie/app.log: no such file or directory"); with proc.UserPath taken out of openApp, TestGatewayStartsGetUserPath is red ("runGUI doesn't call proc.UserPath"). The guard now follows a start into a function or func var of this package, since the Wails start sits behind openApp for the test. - Real use on mote: the GUI build, magpie app in a sandbox HOME, wrote ~/Library/Logs/magpie/app.log (0600) with its "started" line; magpie log and magpie log -n 3 printed it. Not tried: the app opened from Windows Explorer or a Linux desktop launcher for real (the paths are unit-tested per OS, the writer on Windows and Linux); the full GUI suite (running on WSL after the push, reds filed if they fail at origin/main too).
132 lines
4.2 KiB
Go
132 lines
4.2 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 || 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
|
|
}
|
|
|
|
// 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)
|
|
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
|
|
}
|