daemon: a launch agent written as Background is mended to Standard (#2961)
The commit before writes ProcessType Standard for new installs. Every
agent already installed kept Background until the user ran `magpie
daemon install` again, which no one is told to do. Now `magpie serve`
rewrites that one line of the installed launch agent as it starts
(daemon.Mend), leaving the rest of the user's install as it is, and
says so in the log. launchd reads the file when it loads the agent, not on
a kickstart: so it is taken up at the next login, or at `magpie daemon
restart`, which now takes the agent out and loads it again instead of
`kickstart -k` (and `start` does the same when it mends the file).
This is the same class as #6261: the gateway's process held to
background priority by macOS while agents wait on it.
Run:
- TestLaunchdOldAgentMended: an installed agent rewritten to the old
Background line is mended byte for byte to what Install writes, 0600,
with no launchctl call from Mend; Restart boots it out before loading
it; on Linux nothing is touched. TestLaunchdInstall's restart now
expects bootout, then load. Broken by hand (mended never mends):
red at "Mend: the plist is" and "want a bootout first".
- TestLaunchdInstall (plutil -lint on darwin), the whole daemon package,
vet on darwin, linux, windows: ok; and the package run named in 6261's commit: all ok (gateway 908s, plugin 159s, cli 52s) at base 7eecbcc56; at the pushed head (rebased on 459d668b3, which touches none of these files) build, gofmt, linux and windows vet, and awake, daemon, cli tests ok again
- Real launchd on mote (macOS 27.2), a throwaway agent of its own label
running /bin/sleep: as Background, launchctl print says "spawn type =
background (5)" and ps pri 4, the band #6261's app was napped into.
The file rewritten to Standard and `kickstart -k`: still background
(5), pri 4. bootout + bootstrap: "spawn type = daemon (3)", pri 20.
So a kickstart doesn't take the mended file up, and restart's reload
does.
Not tried: a real `magpie daemon install` on mote (it would take the
gateway's port from the other sessions there).
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
<key>KeepAlive</key>
|
||||
<dict><key>SuccessfulExit</key><false/></dict>
|
||||
<key>ThrottleInterval</key><integer>5</integer>
|
||||
<key>ProcessType</key><string>Standard</string>
|
||||
` + processType + `
|
||||
<key>StandardOutPath</key>` + s(logFile()) + `
|
||||
<key>StandardErrorPath</key>` + 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 = "<key>ProcessType</key><string>Background</string>"
|
||||
processType = "<key>ProcessType</key><string>Standard</string>"
|
||||
)
|
||||
|
||||
// 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) {
|
||||
|
||||
Reference in New Issue
Block a user