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 (base 841326709, rebased on ced880ef0, 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/main 7b09d2c95 (AppKit on mote, #4125).
- Windows (win-run, CGO_ENABLED=0): internal/update ok; internal/gui
reds the same set as origin/main 7b09d2c95 (#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.
molvqingtai (Discord, 10-24): on macOS, magpie's restart after a
self-update still leaves it closed on versions after fb23bca5c, and only
while the screen is locked. Their 10-22 log (v0.1.1314) had the old
process exit 0 and `open` of the bundle fail ~160ms later with -600
(procNotFound), LaunchServices still sending to the gone process.
fb23bca5c tried that open again for ~13.5s (8 tries), and the report
says that isn't enough: LaunchServices can go on refusing for as long as
the session is locked, and the relaunch had nothing but LaunchServices
to start magpie with. With magpie closed, Prevent sleep is off too: the
Mac sleeps and the agents lose their connection.
Cause, as far as it could be checked: every route back went through
LaunchServices. A locked session was not reproduced here (the screen was
never locked), so "refuses for as long as it is locked" is the report's
shape, not something observed.
Fix (internal/update/update.go): relaunchCommand is now relaunchScript
over openWaits. The same open with the same pauses runs first; once the
last try has failed, the bundle's own program (Contents/MacOS/<the
CFBundleExecutable its Info.plist names, else this one's name, else
magpie>) is exec'd by the helper shell with RelaunchArgs: `tray` when no
window was up, `gui` when one was. That is what the Open at login launch
agent runs (ProgramArguments: Contents/MacOS/magpie tray), so it needs
nothing from LaunchServices. The shell is runAfterExit's, in a session of
its own (#2362); `exec` makes it the new magpie, so there is no shell
left to exit and take it with it. The instance lock is a flock on a file
keyed by the program's path, the same for open and for this start, so a
late open from LaunchServices hands over to the one already running.
Each try is written to relaunch.log in magpie's own folder
(appdir.Config(), ~/.config/magpie on the Mac): "magpie has exited;
opening <bundle>", then per try "open try N: exit S <open's stderr on
one line>", then "open gave up after N tries; starting <program> <args>"
or "no executable at <path>". The file is moved to relaunch.log.old past
64KB. A missing log folder writes nothing and changes nothing else.
Tests (internal/update/relaunch_fallback_test.go), with fakeOpen from
relaunch_retry_test.go first on PATH answering with the -600 text and a
fake bundle whose program writes the arguments it got:
- TestRelaunchStartsTheBundleWhenOpenNeverWorks: through runAfterExit
after a gone pid, open refused every time; the program runs with tray
(no window) and gui (window), after len(waits)+1 opens.
- TestRelaunchLogsEachTry: the log has the opening line, each try's exit
status with open's -600 text, and the fallback's program and args.
- TestRelaunchOpenedStartsNothingElse: open working on try 2 is the
whole relaunch; the program isn't also run; the log says exit 0.
- TestRelaunchNoProgramIsLogged: a bundle without its program is
logged and the shell exits non-zero.
- TestRelaunchRunsTheProgramInfoPlistNames: "magpie dev" from
Info.plist, in a bundle path with a quote in it.
- TestRelaunchLogInMagpiesFolder: the log is under XDG_CONFIG_HOME's
magpie, and is moved to .old past 64KB.
Red by hand: with the fallback replaced by the old `|| exit 1`,
StartsTheBundle timed out at 30s ("the new version starting never
happened"), and LogsEachTry, NoProgramIsLogged and
RunsTheProgramInfoPlistNames failed; with note() a no-op, LogsEachTry, OpenedStartsNothingElse
and NoProgramIsLogged failed. All green with the fix.
Real run: a relauncher built from this tree calling update.Relaunch on
a scratch Scratch.app (CFBundleExecutable scratch, a script writing a
marker), under env -i with a sandbox HOME and an `open` first on PATH
that always fails with the -600 text. The relauncher exited at once;
relaunch.log under the sandbox's ~/.config/magpie had 8 tries 02:02:45
to 02:02:59, each "exit 1 _LSOpenURLsWithCompletionHandler() ... -600.",
then "open gave up after 8 tries; starting '.../scratch' 'tray'". The
marker: args=tray ppid=1 pgid=pid (own group), no tty, and still alive
3s later, after the relauncher and its shell were gone.
Run at 843417957 plus this change: gofmt -l internal/ (clean);
go vet ./... (darwin); GOOS=linux go vet -tags nogui ./...;
GOOS=windows go vet ./...; go test -count=1 ./internal/update (ok).
The full suite (-timeout 45m) was started on this tree before the push
and had not finished; per the owner's ship-at-once rule this didn't wait
for it.
Not tried: a real update with the screen locked (the screen here was
never locked), so whether the program started by path comes up under a
locked session is not seen, only that the login agent starts it the
same way. Not tried with the real magpie binary in the scratch bundle
(it would read the Keychain): the tray and the instance lock under this
start are the login agent's path, not observed here. An open that exits
0 while magpie doesn't come up isn't covered; relaunch.log will show it.