xyxyxy_15591 (Discord #feedback) asked for a silent auto-update: once an update is in, the window shouldn't pop up. With "Restart automatically" (Settings, beside Version) on, the window state was already kept (a closed window came back as the tray icon only), and What's New stays quiet after an in-app restart. What did show was magpie taking focus: - macOS relaunched with `open -n` and no `-g`, so LaunchServices brought magpie to the front even when only the menu-bar icon came back; - with the window open, showHere's Focus() activated the app (every OS); - joining the Dock activated magpie, and with Dock on Wails' Regular policy activated it at launch. Now a restart magpie makes by itself (AutoRestart, `magpie update` from a terminal) writes a one-shot restart-quiet mark (read once, good for 10 minutes). The next start reads it: `open -n -g`, no Focus(), the Dock joined without activating, Accessory policy at launch and the Dock set afterwards. A restart the user clicks removes any mark and comes to the front as before. `open` gets -g also whenever no window is wanted. No new setting: zero-config, the existing switch is the one asked for. Run (base841326709, rebased onced880ef0, no upstream change to these files): - mote: gofmt -l clean; go vet ./internal/gui ./internal/update ./internal/cli; GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet on gui/update/cli: clean. internal/update ok; TestRestartBySelfComesBackQuietly, TestUpdateThatWontStart, TestWhatsNew*, *Handover* ok, except TestHandoverLeavesTheMenuBarIconForTheNextMagpie, killed at 30s the same at origin/main7b09d2c95(AppKit on mote, #4125). - Windows (win-run, CGO_ENABLED=0): internal/update ok; internal/gui reds the same set as origin/main7b09d2c95(#4115, #1827, #2958, #4514). - WSL (wsl-run): full `go test -tags nogui -timeout 45m ./...`, 80 ok; reds TestInstalledVersion, TestGrokInstalled, TestOmpDetectedWithPiDir (#4033, #3548), TestNoCommandBypassesProc (#4491), TestSettingsSaveKeepsWhatItDoesNotSend (#4514) all red the same at origin/main 7b09d2c95; TestColdTurnsTaken (#4510) passed there alone. internal/gui + internal/update rerun at this head: internal/update ok; internal/gui red only TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestColdTurnsTaken (#4510). Broken by hand: without -g, TestRelaunchOpen and TestRelaunchOpenTriedAgain fail; without markQuietRestart, TestRestartBySelfComesBackQuietly fails "window true: the start after a restart by itself isn't quiet"; without the unmark on a clicked restart, it fails "a restart the user asked for is marked quiet". Not tried: a real update restart on any OS (`open` of a GUI app over ssh on mote fails, RBSRequestErrorDomain 5), so -g and no-focus weren't seen in a real session. Windows' ShowWindow inside Wails may still bring the window forward when it was open; Linux window managers were not tried. No GUI assets changed, so no Playwright run.
327 lines
10 KiB
Go
327 lines
10 KiB
Go
//go:build darwin || linux
|
|
|
|
package gui
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/update"
|
|
)
|
|
|
|
// successorEnv has this test binary, started again, play the new magpie
|
|
// (TestHandoverSuccessor).
|
|
const successorEnv = "MAGPIE_TEST_SUCCESSOR"
|
|
|
|
// TestHandoverSuccessor is the new version a restart starts, in a process
|
|
// of its own: it takes the gateway over from the magpie that started it
|
|
// and serves it until it is ended.
|
|
func TestHandoverSuccessor(t *testing.T) {
|
|
if os.Getenv(successorEnv) == "" {
|
|
t.Skip("the new magpie of TestRestartHandsTheGatewayOver")
|
|
}
|
|
exe, err := update.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if takeOver(exe) == 0 {
|
|
t.Fatal("no gateway was taken over")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
if err := gateway.New().ListenAndServe(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// handoverSandbox is a magpie serving the gateway on a port of its own,
|
|
// as the app does, to restart; what it set is put back after.
|
|
func handoverSandbox(t *testing.T, up string) *gateway.Server {
|
|
t.Helper()
|
|
if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Key: "upstream", Chat: up + "/v1", Models: []string{"m1"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("MAGPIE_ADDR", "127.0.0.1:0")
|
|
t.Setenv("MAGPIE_NO_HANDOVER", "")
|
|
oldExe, oldHello, oldAck := startExe, helloWait, ackWait
|
|
oldStart, oldRelaunch := startBeside, relaunchBinary
|
|
exe, err := update.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
startExe = exe
|
|
handoverHost.Store(true)
|
|
t.Cleanup(func() {
|
|
endBackend()
|
|
serving.Wait()
|
|
backendCtx, endBackend = context.WithCancel(context.Background())
|
|
served.Store(nil)
|
|
pending.Store(nil)
|
|
handedOver.Store(false)
|
|
handoverHost.Store(false)
|
|
startExe, helloWait, ackWait = oldExe, oldHello, oldAck
|
|
startBeside, relaunchBinary = oldStart, oldRelaunch
|
|
gateway.ForwardCallbacks("")
|
|
})
|
|
gw := serveGateway()
|
|
if gw == nil {
|
|
t.Fatal("the gateway wasn't served")
|
|
}
|
|
eventually(t, "the gateway never listened", func() bool { return gw.Addr() != "" })
|
|
t.Setenv("MAGPIE_ADDR", gw.Addr()) // as the new one finds it in settings
|
|
return gw
|
|
}
|
|
|
|
// slowUpstream answers a streamed request with a first chunk, then the
|
|
// rest once release is closed; any other at once.
|
|
func slowUpstream(t *testing.T, release <-chan struct{}) *httptest.Server {
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
body, _ := io.ReadAll(r.Body)
|
|
if !strings.Contains(string(body), `"stream":true`) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"quick"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, "data: {\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"first \"}}]}\n\n")
|
|
w.(http.Flusher).Flush()
|
|
select {
|
|
case <-release:
|
|
case <-r.Context().Done():
|
|
return
|
|
}
|
|
io.WriteString(w, "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"last\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n")
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
return up
|
|
}
|
|
|
|
// servedByPid is the pid of the magpie answering at addr, 0 when none.
|
|
func servedByPid(addr string) int {
|
|
c := &http.Client{Timeout: 2 * time.Second, Transport: &http.Transport{DisableKeepAlives: true}}
|
|
r, err := c.Get("http://" + addr + "/")
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
defer r.Body.Close()
|
|
var info struct {
|
|
Pid int `json:"pid"`
|
|
}
|
|
json.NewDecoder(r.Body).Decode(&info)
|
|
return info.Pid
|
|
}
|
|
|
|
// xuanwo on X: a restart to update on a Mac or Linux hands the gateway to
|
|
// the new version, a process of its own, rather than waiting for it to be
|
|
// idle. A reply streaming through the old one when it restarts finishes
|
|
// whole; requests sent meanwhile are answered, by the new one once it
|
|
// serves; and the old one quits only once its stream has ended.
|
|
func TestRestartHandsTheGatewayOver(t *testing.T) {
|
|
release := make(chan struct{})
|
|
up := slowUpstream(t, release)
|
|
defer close(release)
|
|
gw := handoverSandbox(t, up.URL)
|
|
addr := gw.Addr()
|
|
|
|
var child *exec.Cmd
|
|
startBeside = func(exe string, window bool, view string) error {
|
|
child = exec.Command(exe, "-test.run=^TestHandoverSuccessor$", "-test.v")
|
|
child.Env = append(os.Environ(), successorEnv+"=1")
|
|
child.Stdout, child.Stderr = os.Stderr, os.Stderr
|
|
return child.Start()
|
|
}
|
|
t.Cleanup(func() {
|
|
if child != nil && child.Process != nil {
|
|
child.Process.Kill()
|
|
child.Wait()
|
|
}
|
|
})
|
|
relaunchBinary = func(string, string, bool, string) error {
|
|
t.Error("restarted as before, rather than handing over")
|
|
return nil
|
|
}
|
|
|
|
// a reply streaming through the gateway
|
|
resp, err := http.Post("http://"+addr+"/v1/chat/completions", "application/json",
|
|
strings.NewReader(`{"model":"plan/m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
stream := bufio.NewReader(resp.Body)
|
|
if line, err := stream.ReadString('\n'); err != nil || !strings.Contains(line, "first") {
|
|
t.Fatalf("the stream's first chunk: %q %v", line, err)
|
|
}
|
|
// it doesn't hold the restart back
|
|
if b := restartBusy(); b.Any() {
|
|
t.Fatalf("a restart that hands over waits for %+v", b)
|
|
}
|
|
|
|
// requests all along the restart are answered
|
|
var asked, answered atomic.Int32
|
|
stopAsking := make(chan struct{})
|
|
asking := make(chan struct{})
|
|
var stopOnce sync.Once
|
|
stop := func() { stopOnce.Do(func() { close(stopAsking) }); <-asking }
|
|
t.Cleanup(stop)
|
|
go func() {
|
|
defer close(asking)
|
|
for {
|
|
select {
|
|
case <-stopAsking:
|
|
return
|
|
default:
|
|
}
|
|
asked.Add(1)
|
|
r, err := http.Post("http://"+addr+"/v1/chat/completions", "application/json", strings.NewReader(`{"model":"plan/m1","messages":[{"role":"user","content":"hi"}]}`))
|
|
if err != nil {
|
|
t.Errorf("a request during the restart: %v", err)
|
|
return
|
|
}
|
|
b, _ := io.ReadAll(r.Body)
|
|
r.Body.Close()
|
|
if r.StatusCode != 200 || !strings.Contains(string(b), "quick") {
|
|
t.Errorf("a request during the restart: %d %s", r.StatusCode, b)
|
|
return
|
|
}
|
|
answered.Add(1)
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
}()
|
|
|
|
if !beginHandover("", startExe, false, "", false) {
|
|
t.Fatal("the restart didn't hand over")
|
|
}
|
|
quit := make(chan struct{})
|
|
leave(func() { close(quit) })
|
|
|
|
eventually2(t, 30*time.Second, "the new magpie never served the gateway", func() bool {
|
|
return child != nil && child.Process != nil && servedByPid(addr) == child.Process.Pid
|
|
})
|
|
if !handedOver.Load() {
|
|
t.Fatal("the new one serves, and this one doesn't know it handed over")
|
|
}
|
|
// the old one keeps its stream past the 2s an ordinary end gives
|
|
// requests (gateway.shortDrain), and so doesn't quit yet
|
|
select {
|
|
case <-quit:
|
|
t.Fatal("quit with a reply streaming")
|
|
case <-time.After(3 * time.Second):
|
|
}
|
|
for range 5 {
|
|
if pid := servedByPid(addr); pid != child.Process.Pid {
|
|
t.Fatalf("a request after the handover reached pid %d", pid)
|
|
}
|
|
}
|
|
stop()
|
|
if answered.Load() == 0 || answered.Load() != asked.Load() {
|
|
t.Fatalf("%d of %d requests during the restart answered", answered.Load(), asked.Load())
|
|
}
|
|
|
|
release <- struct{}{}
|
|
rest, err := io.ReadAll(stream)
|
|
if err != nil || !strings.Contains(string(rest), "last") || !strings.Contains(string(rest), "[DONE]") {
|
|
t.Fatalf("the stream was cut: %q %v", rest, err)
|
|
}
|
|
select {
|
|
case <-quit:
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("the old one didn't quit once its stream ended")
|
|
}
|
|
}
|
|
|
|
// A handover that can't be made leaves the gateway with this one, which
|
|
// then restarts as before (the new one opened once it has exited), but
|
|
// only once the requests it has in flight have finished: the restart was
|
|
// let go without waiting for them, since a handover carries them (the
|
|
// sandbox run's bind failed and its stream was cut).
|
|
func TestHandoverNotMadeRestartsAsBeforeOnceIdle(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
start func(string, bool, string) error
|
|
}{
|
|
{"never says hello", func(string, bool, string) error { return nil }},
|
|
{"doesn't start", func(string, bool, string) error { return errors.New("no such file") }},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
release := make(chan struct{})
|
|
up := slowUpstream(t, release)
|
|
defer close(release)
|
|
gw := handoverSandbox(t, up.URL)
|
|
helloWait = 200 * time.Millisecond
|
|
startBeside = c.start
|
|
var relaunched atomic.Bool
|
|
relaunchBinary = func(exe, aside string, window bool, view string) error {
|
|
relaunched.Store(true)
|
|
return nil
|
|
}
|
|
resp, err := http.Post("http://"+gw.Addr()+"/v1/chat/completions", "application/json",
|
|
strings.NewReader(`{"model":"plan/m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
stream := bufio.NewReader(resp.Body)
|
|
if line, err := stream.ReadString('\n'); err != nil || !strings.Contains(line, "first") {
|
|
t.Fatalf("the stream's first chunk: %q %v", line, err)
|
|
}
|
|
if !beginHandover("", startExe, false, "", false) {
|
|
t.Fatal("the restart didn't try to hand over")
|
|
}
|
|
quit := make(chan struct{})
|
|
leave(func() { close(quit) })
|
|
select {
|
|
case <-quit:
|
|
t.Fatal("quit with a reply streaming")
|
|
case <-time.After(time.Second):
|
|
}
|
|
if relaunched.Load() {
|
|
t.Fatal("restarted with a reply streaming")
|
|
}
|
|
if servedByPid(gw.Addr()) != os.Getpid() {
|
|
t.Fatal("the gateway isn't this one's while the restart waits")
|
|
}
|
|
release <- struct{}{}
|
|
rest, err := io.ReadAll(stream)
|
|
if err != nil || !strings.Contains(string(rest), "[DONE]") {
|
|
t.Fatalf("the stream was cut: %q %v", rest, err)
|
|
}
|
|
select {
|
|
case <-quit:
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("never quit")
|
|
}
|
|
if !relaunched.Load() {
|
|
t.Fatal("not restarted as before")
|
|
}
|
|
if handedOver.Load() {
|
|
t.Fatal("says it handed over")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func eventually2(t *testing.T, most time.Duration, what string, ok func() bool) {
|
|
t.Helper()
|
|
for deadline := time.Now().Add(most); time.Now().Before(deadline); time.Sleep(20 * time.Millisecond) {
|
|
if ok() {
|
|
return
|
|
}
|
|
}
|
|
t.Fatal(what)
|
|
}
|