diff --git a/internal/cli/providers_cli.go b/internal/cli/providers_cli.go index 44b098ad3..9cbe17abb 100644 --- a/internal/cli/providers_cli.go +++ b/internal/cli/providers_cli.go @@ -19,6 +19,7 @@ import ( "github.com/yetone/magpie/internal/agent" "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/daemon" "github.com/yetone/magpie/internal/gateway" "github.com/yetone/magpie/internal/plugin" "github.com/yetone/magpie/internal/provider" @@ -1682,6 +1683,8 @@ func serve() error { // before a change and no codex session is on it go agent.KeepCodexDaemonCurrent(context.Background()) go agent.KeepCodexRolloutsPortable(context.Background()) + // a launch agent an older magpie wrote, brought up to date (#2961) + daemon.Mend() public := advertisedURL() fmt.Println(green.Render("●"), "magpie gateway on", bold.Render(gateway.URL())) fmt.Println(muted.Render(" OpenAI "), public+"/v1/chat/completions", muted.Render("·"), public+"/v1/responses") diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index d1bba31b4..f70adb949 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -10,6 +10,7 @@ import ( "errors" "fmt" "html" + "log" "os" "os/exec" "path/filepath" @@ -112,6 +113,20 @@ func Install(o Options) error { return nil } +// Mend brings a service written by an older magpie up to what Install +// writes now, where that can be done in place: on a Mac, a launch agent +// that ran the gateway as Background runs it as Standard (#2961) from the +// next login, or `magpie daemon restart`. The service calls it as it +// starts, so no user has to install it again. +func Mend() { + if goos != "darwin" { + return + } + if (launchd{}).mended() { + log.Print("daemon: the launch agent runs the gateway as Standard, not Background, from the next login or `magpie daemon restart` (#2961)") + } +} + // removedMark is there once the user removed the service (Uninstall): what // magpie puts in place by itself (the terminal's keepGateway, as it quits) is put there // no more. diff --git a/internal/daemon/daemon_test.go b/internal/daemon/daemon_test.go index 717ed6703..8b02f70da 100644 --- a/internal/daemon/daemon_test.go +++ b/internal/daemon/daemon_test.go @@ -284,7 +284,8 @@ func TestLaunchdInstall(t *testing.T) { t.Errorf("install ran\n%s\nwant\n%s", got, want) } - // loaded: install again takes it out first; restart kills and starts + // loaded: install again takes it out first; restart takes it out and + // loads it again (the fake still says loaded, so start kickstarts) os.Remove(filepath.Join(dir, "launchctl.print.fail")) write(t, filepath.Join(dir, "launchctl.print.out"), "gui/501/com.yetone.magpie.serve = {\n\tactive count = 1\n\tstate = running\n\tprogram = /opt/magpie\n\tpid = 777\n\tlast exit code = (never exited)\n\tendpoints = {\n\t\tstate = active\n\t}\n}\n") os.Remove(log) @@ -296,7 +297,7 @@ func TestLaunchdInstall(t *testing.T) { } got = strings.Join(calls(t, log), "\n") svc := "gui/" + uid + "/com.yetone.magpie.serve" - want = "launchctl print " + svc + "\nlaunchctl bootout " + svc + "\nlaunchctl bootstrap gui/" + uid + " " + file + "\nlaunchctl print " + svc + "\nlaunchctl kickstart -k " + svc + want = "launchctl print " + svc + "\nlaunchctl bootout " + svc + "\nlaunchctl bootstrap gui/" + uid + " " + file + "\nlaunchctl print " + svc + "\nlaunchctl bootout " + svc + "\nlaunchctl print " + svc + "\nlaunchctl kickstart " + svc if got != want { t.Errorf("reinstall and restart ran\n%s\nwant\n%s", got, want) } @@ -595,3 +596,71 @@ func TestProgramOf(t *testing.T) { t.Errorf("none: %q", got) } } + +// A launch agent an older magpie wrote, running the gateway as Background, +// is mended to Standard in place: by the service as it starts (Mend), with +// nothing else in it changed and launchd left alone, and by a restart, +// which loads it anew for launchd to read it (#2961). +func TestLaunchdOldAgentMended(t *testing.T) { + log, dir := fakes(t, "darwin") + write(t, filepath.Join(dir, "launchctl.print.fail"), "Could not find service") + if err := Install(Options{}); err != nil { + t.Fatal(err) + } + file := filepath.Join(os.Getenv("HOME"), "Library", "LaunchAgents", "com.yetone.magpie.serve.plist") + b, err := os.ReadFile(file) + if err != nil { + t.Fatal(err) + } + now := string(b) + old := strings.Replace(now, processType, oldProcessType, 1) + if old == now { + t.Fatalf("the plist has no %q:\n%s", processType, now) + } + // loaded, as the running service's is + os.Remove(filepath.Join(dir, "launchctl.print.fail")) + write(t, filepath.Join(dir, "launchctl.print.out"), "state = running\n") + mended := func(what string) { + t.Helper() + b, _ := os.ReadFile(file) + if string(b) != now { + t.Errorf("%s: the plist is\n%s\nwant\n%s", what, b, now) + } + if st, _ := os.Stat(file); st.Mode().Perm() != 0o600 { + t.Errorf("%s: the plist is %v", what, st.Mode().Perm()) + } + } + + write(t, file, old) + os.Remove(log) + Mend() + mended("Mend") + if got := strings.Join(calls(t, log), "\n"); got != "" { + t.Errorf("Mend ran %s", got) + } + Mend() // a second time finds nothing to do + mended("Mend again") + + write(t, file, old) + os.Remove(log) + if err := Restart(); err != nil { + t.Fatal(err) + } + mended("Restart") + uid := strconv.Itoa(os.Getuid()) + svc := "gui/" + uid + "/com.yetone.magpie.serve" + // the fake still says loaded after the bootout, so start kickstarts + // what bootstrap would load: what matters is the bootout first + got := strings.Join(calls(t, log), "\n") + if !strings.HasPrefix(got, "launchctl print "+svc+"\nlaunchctl bootout "+svc+"\n") { + t.Errorf("restart of an old agent ran\n%s\nwant a bootout first", got) + } + + // on Linux there is no launch agent to mend + goos = "linux" + write(t, file, old) + Mend() + if b, _ := os.ReadFile(file); string(b) != old { + t.Errorf("Mend on Linux changed the plist") + } +} diff --git a/internal/daemon/launchd.go b/internal/daemon/launchd.go index 4406e1084..793e6eccb 100644 --- a/internal/daemon/launchd.go +++ b/internal/daemon/launchd.go @@ -2,6 +2,7 @@ package daemon import ( "html" + "log" "os" "path/filepath" "strconv" @@ -49,7 +50,7 @@ func plist(exe string, args, env []string) string { KeepAlive SuccessfulExit ThrottleInterval5 - ProcessTypeStandard + ` + processType + ` StandardOutPath` + s(logFile()) + ` StandardErrorPath` + s(logFile()) + ` `) @@ -107,6 +108,9 @@ func (l launchd) unload() { } func (l launchd) start() error { + if l.mended() { + l.unload() + } if !l.loaded() { _, err := launchctl("bootstrap", domain(), l.file()) return err @@ -125,12 +129,35 @@ func (l launchd) stop() error { return err } +// restart takes the agent out and loads it again: launchd reads the file +// when it loads it, not on a kickstart, so a file mended or changed since +// (Mend, as the service started) is what runs. func (l launchd) restart() error { - if !l.loaded() { - return l.start() + l.unload() + return l.start() +} + +// oldProcessType is what an agent written before #2961 runs the gateway +// as, and processType what plist writes now. +const ( + oldProcessType = "ProcessTypeBackground" + processType = "ProcessTypeStandard" +) + +// mended rewrites an agent written before #2961, which ran the gateway as +// Background, to run it as Standard, and says whether it did. It changes +// that line alone: the rest is the user's install. launchd takes it up +// when it next loads the agent: at the next login, or a start or restart. +func (l launchd) mended() bool { + b, err := os.ReadFile(l.file()) + if err != nil || !strings.Contains(string(b), oldProcessType) { + return false } - _, err := launchctl("kickstart", "-k", service()) - return err + if err := private(l.file(), []byte(strings.Replace(string(b), oldProcessType, processType, 1))); err != nil { + log.Printf("daemon: can't rewrite %s to run the gateway as Standard: %v", l.file(), err) + return false + } + return true } func (l launchd) status() (Status, error) {