Commit Graph
10 Commits
Author SHA1 Message Date
yetone 1315ac7d99 update: the first update finds a magpie without gateway.json, and a restart waits a bounded time (ljlwlcz on X)
Two edges of v0.1.3687's gateway.json:

1. The first update from a magpie older than the record found no gateway.json,
   asked 3425, said "isn't running" and left a service on MAGPIE_ADDR=0.0.0.0:10100
   on the old version. With no record, no MAGPIE_ADDR and no answer at the usual
   address, `magpie update` now tries the installed service's MAGPIE_ADDR (read
   from its unit or launch agent, daemon.Addr), the port in Settings and 3425,
   and takes the first that answers as magpie. --addr and MAGPIE_ADDR stay the
   user's word.
2. A restart asked by `magpie update` waited on "N requests" with no end (Windows
   hands nothing over, long streams never finish). The app and magpie serve now
   wait at most 10 minutes for that ask, then restart; the output says so
   ("or in 10 min at the latest, which cuts what is still running"). `magpie
   update --force` restarts at once (gateway.SetForceRestarter), and says if the
   running magpie is too old to know --force. The app's own look at the disk
   (followReplaced) and autonomous serve follow still never cut a request.

Run (mote refused SSH; win-run on xiaomi-gpu): go vet cli/gui/gateway/daemon
(windows), linux nogui vet; go test -run TestUpdate|TestServeFollow|
TestTerminalUpdate|TestRestart over cli, gui, gateway, daemon: ok except
TestUpdateThatWontStart, red on origin/main on the same box too (#6059).
Broken by hand: drain bound off, record fallback off, serve bound off: the new
tests TestTerminalUpdateWaitIsBoundedAndCanBeForced, TestUpdateFindsAMagpieThatWroteNoRecord,
TestServeFollowDrainIsBounded go red.
Not tried: a real systemd service upgrade; darwin cross vet (cgo) and the full
suite (started after the tag if mote is back).
2026-11-11 13:01:11 +08:00
yetone e4197144a8 perf: every package's package-level regexps compile on first use
v0.1.3516 made gateway's and redact's patterns lazy. The other 299
package-level regexp.MustCompile calls, in 148 files across agent,
provider, gui, library, codexcat, sessions, usage, plugin and the rest,
now go through lazyre.New too, so a launch that never reaches them
(`magpie statusline`, run by Claude Code on every message) doesn't
compile them. Fields, maps and params that held them are *lazyre.RE.

Measured on mote (M5 Pro), sandbox HOME, v0.1.3516-equivalent build
(t42) against this one (t43):
- GODEBUG=inittrace=1 sum: 7.39 MB / 69,837 allocs -> 4.62 MB / 46,765
- `magpie statusline`, 3 runs each: instructions retired ~212M -> ~176M,
  peak memory footprint 19.2-19.4 MB -> 16.8-17.0 MB

Run: go vet ./... (darwin) ok; GOOS=windows go vet on each of the 30
changed packages ok; GOOS=linux go vet ok except ., internal/cli and
internal/gui, which stop in wails v3's cgo files when cross-compiled
(the same at main). New TestAllLazyPatternsCompile at the root compiles
all 574 registered patterns (ok), so a bad pattern fails a test rather
than a first use.
Not run before push: the changed packages' own test suites; they run
on mote after it.
2026-11-10 12:54:39 +08:00
JoeyWu-tech ab25a2317b autostart: on Linux the switch follows a package's own entry (#3372)
mtul0729, packaging magpie for nixpkgs (#3372, COSMIC on Wayland,
v0.1.1334): a package that ships its own autostart entry
($XDG_CONFIG_DIRS/autostart/magpie.desktop, /etc/xdg when unset) has
magpie open at login, but enabled() read only
$XDG_CONFIG_HOME/autostart, so Open at login said off over it. Off
removed a user file that wasn't there, and the package's entry went on
starting magpie. On wrote this copy's own path over the package's entry:
under a package manager a versioned path, gone once the package is
upgraded and collected. The package doc says the switch "can't say one
thing while the system does another"; on such a system it did.

Now, as the XDG Autostart spec has it:
- The entry that counts is the user's, else the first of the
  $XDG_CONFIG_DIRS folders (/etc/xdg when unset or empty) that has one.
  A relative folder has none, as the spec says, and neither has a file
  listed there, a folder this user can't search or a name too long,
  which systemd's generator and GLib can't open either. The user's own
  folder listed there too, by any name (os.SameFile, so /home linked to
  /var/home too), is still the user's.
- An entry is read as GLib's key file and systemd's parser read it: the
  keys of its [Desktop Entry] group alone (a [Desktop Action]'s Exec is
  not the entry's), spaces around = and CRLF lines as any others, the
  last of a key given twice, true or 1 for Hidden. It opens magpie when
  it is an application (Type=Application) with an Exec other than true
  (below), not Hidden.
- X-GNOME-Autostart-enabled=false (or 0) reads off too, as the issue
  asks. GNOME's and Cinnamon's sessions read it; systemd's generator
  (KDE's and COSMIC's sessions under systemd) doesn't, so there such an
  entry reads off while it starts.
- Off, over a package's entry that some desktop starts, writes an entry
  of the user's that hides it; that includes one GNOME has off, which
  systemd's generator still starts. Otherwise off removes the user's
  entry, as before. On, over a package's entry that starts magpie,
  removes the user's entry and writes no path; otherwise (no package
  entry, a hidden one, or one GNOME has off) it writes this copy's
  entry, as before.

The hiding entry is "[Desktop Entry] Type=Application Name=magpie
Exec=true Hidden=true". A session holds back the entries of a name only
once it has read one, and each reads differently (from their source):
- gnome-session (main and 42.0) and cinnamon-session load an entry with
  g_desktop_app_info_new_from_filename. GLib (main,
  g_desktop_app_info_load_from_keyfile) refuses one with no Type or
  Name, or whose Exec names a program it can't find, and the session
  skips an entry it can't load, so the package's starts.
- mate-session's egg_desktop_file_new refuses an application entry with
  no Exec, and the entry then takes no app id.
- cosmic-session without systemd never reads Hidden: it starts what Exec
  names, and takes the name once that ran.
- systemd's generator (main, xdg-autostart-service.c) reads Hidden
  first, and hides an entry it can't parse, which still holds back the
  rest.
true is on every system by that name, so each session reads the entry,
and COSMIC's runs true where it would have run magpie. A copy of the
package's own Exec would stop holding the package's back on GNOME once
its store path is collected. With this box's GLib 2.72.4 (Ubuntu
22.04's) and 2.84.2 (anaconda's), through ctypes: Exec=true and no Exec
load, hidden; an Exec naming a store path that is gone, and a bare
"[Desktop Entry] Hidden=true", don't load. An entry that runs true opens
nothing, so it reads off: XFCE's Session and Startup lists the hiding
entry as magpie, unticked, and ticking it there writes Hidden=false into
it (xfae-model.c), which runs true, not magpie. On takes it away for the
package's.

What is no entry, and what can't be read: a path where no regular file
is (a folder, a link that leads nowhere or round in a loop) is no entry,
as systemd's generator and GLib pass over it, and on leaves a folder
there alone. A file that can't be read (EACCES, EPERM, EIO) counts and
starts nothing, as systemd's generator has it; GLib passes over it, so
where the first entry is unreadable GNOME starts the next folder's while
Open at login reads off. Any other failed read, magpie out of files or
memory, says nothing of what the desktop reads at login: Set returns it
and writes nothing, and Open at login reads on, as the package's doc has
it for a system that can't be asked (Windows' Run key that can't be read
is not a no either).

The user's entry is written where the desktop reads it. A link there (to
magpie's launcher, which install.sh writes, the usual way to have an app
open at login; or into a read-only store), or a hard link, is replaced
by the entry, not written through: edit.WriteAtomic writes a link's
target, and a hard-linked file in place, so off over a package's entry
would have left the launcher running true, and failed over a read-only
target with the switch still on. Main's enable() wrote through such a
link too, when on was asked over one (a linked launcher already reads
on); it goes through the same write now.

Old entries: a user entry an older magpie wrote stays and still counts
over a package's, as before. The switch reads on over it, so in the app
taking it away is off, then on; `magpie autostart on` does it in one
step. One whose store path was collected reads on as well, though
systemd's generator starts nothing for it and still holds the package's
back, so on COSMIC or KDE magpie doesn't open while the switch says on;
GLib passes over it, and GNOME starts the package's.

The daemon and the terminal: `magpie daemon install` refuses while
magpie opens at login, so the app and the service don't both serve the
gateway, and `magpie tui` quitting hands the gateway on (keepGateway,
75e78a5a) only where nothing opens magpie at login. Both read
autostart.Enabled, code unchanged, so a package's entry counts for both
now. Where a desktop session runs XDG autostart that is the point:
install says to run `magpie autostart off` first, which now hides the
entry, where before the service went in beside an app the package's
entry went on opening. On a box no desktop logs into (NixOS-WSL, a NixOS
server over SSH) nothing runs the entry, yet install refuses and
keepGateway only says the gateway stopped; `magpie autostart off` gets
both going. That is #3756, filed with this, for yetone to say what they
should count on Linux; a session check wouldn't settle it, since install
over SSH into a desktop box has no DISPLAY either.

An entry the desktop's own settings switched off reads off for the guard
as well: XFCE writes Hidden=true into the user's entry, which main still
counted (it read Type and Exec alone), so install refused; now it goes
in. I kept that reading, and daemon.go's comment now says so, rather
than have the guard count such an entry. The switch and `magpie
autostart` read it off, and a guard counting it would refuse with
"magpie already opens at login" where `magpie autostart` says it
doesn't. The Mac has a state for that case (NeedsAllowing, which the CLI
and the app explain in System Settings' words), and Linux has none to
show it; Windows reads a Run value Task Manager switched off as off, and
on switches it back, as Set(true) does here. What it costs: ticking
magpie again in the desktop's settings after the install opens the app
beside the service, in #3756 too. For an AppImage or a tarball, with no
package entry, on and off write and remove as before, and what changes
is how an entry reads (Hidden, the GNOME key, a failed read, as above)
and that a link at the entry is replaced, not written through.

docs/subsystems/gui-shell.md says how Open at login reads on Linux, and
that the terminal's hand-off counts a package's entry (#3756).

Rebased onto origin/main 58fb3a77: 824100f3 added a line to
gui-shell.md next to the one this patch changes, so 7e9003aa no longer
applied. The code and the docs line are as they were (the same patch
for internal/).

What was run (tree 59fb33a8, on origin/main 58fb3a77, go1.26.8,
linux/amd64, Linux 5.15), outside graftcp, umask 077:
- go test -tags nogui ./internal/autostart ./internal/testenv -v:
  autostart ok, its new tests among them; testenv ok but TestProgram,
  red under this box's umask 077 (#3640), as on main.
- gofmt -l clean. go vet -tags nogui for linux, windows and darwin, go
  vet for windows without the tag, go build -tags nogui for linux and
  darwin, and go build for windows: exit 0 each; untagged linux vet and
  build stop at pkg-config (no GTK headers here). GOOS=freebsd vet: not
  run at this tree.
- go test -tags nogui -count=1 -timeout 45m ./... (log kept), Bun
  1.4.2, 8m35s, load 19 at its start: 70 packages ok, autostart,
  daemon, cli and tui among them; reds in 6, each known: agent
  TestInstalledVersion, TestCursorFolderFromTheEditorIsNotTheCLI,
  TestQoderSettingsFromAnIDEPluginIsNotTheCLI, TestOmpDetectedWithPiDir
  and TestZedAppPath (#3141), TestCodexDriftReached (#3398) and the two
  Reasonix tests (#3641); davsync TestSyncTeraCloudStillSeesOtherWrites
  (#3420);
  edit TestWriteAtomicReadOnlyHardLink and testenv TestProgram
  (umask 077, #3640); gateway TestPluginToldWhereTheAgentWorks (#3675),
  the package ran to its end; gui TestGatewayHeldByAContainer and
  TestGatewayFixQuitsOnlyAMagpie (#3649).

The rest is what was run for 7e9003aa, at tree 719db0e7 on origin/main
6e6aba51, the same code (not this tree):
- Real use (real.log): the built magpie (-tags nogui) and origin/main's,
  each in a sandbox HOME under env -i, with a package entry (Exec=magpie
  tray, magpie on PATH) in XDG_CONFIG_DIRS, and systemd 249's
  systemd-xdg-autostart-generator after each step:
  - the package's entry alone: main says magpie doesn't open at login
    while the generator starts magpie tray; this says on.
  - off, twice: main writes nothing and it still starts; this writes the
    hiding entry, and the generator starts nothing.
  - on: main writes its own path, which it starts; this takes the hiding
    entry away, and the generator starts the package's magpie tray.
  - no package entry, on then off: both write their own path, which
    starts, then remove it, and nothing starts. The package's entry put
    back, off again: as the first off.
  - the package's entry with X-GNOME-Autostart-enabled=false: both say
    off while the generator starts it. Off: main writes nothing and it
    still starts; this writes the hiding entry, and nothing starts. On:
    both write their own path.
  - the hiding entry ticked again as XFCE does (Exec=true, Hidden=false)
    over the package's: the generator runs /usr/bin/true; main says on,
    this says off. On: this takes it away and the package's magpie tray
    starts; main writes its own path.
  - the user's entry a link to magpie's launcher, off over the
    package's: main takes the link away and the package's entry still
    starts; this writes the hiding entry in the link's place, and
    nothing starts. The launcher is unchanged in both.
  - an old entry of magpie's naming a store path that is gone, over the
    package's: both say on, and the generator starts nothing. On: this
    takes it away and the package's starts; main writes its own path.
- The new tests over origin/main's autostart_other.go, with a nil
  systemRecords and readEntry added so they build (red-on-main.log): all
  9 new ones red, and TestSet and TestSetThenTruncatedRecordReadsAsOff,
  which main has, pass. The first red of each: :101 "the package's entry
  opens magpie at login, and Open at login reads off"; :226 "the second
  system folder's alone: off, want on" (and 5 more rows: three more
  where a system folder alone has the entry, the user's own entry
  Hidden, and the user's own off by the GNOME key); :241
  "XDG_CONFIG_DIRS unset: [], want
  ["/etc/xdg/autostart/magpie.desktop"]"; :311 and :373, no hiding entry
  written; :440, off over a link to the launcher left no entry of the
  user's, and :437, on over it with no package's entry wrote through to
  the launcher (main's own enable(), the sibling above); :491 "a folder
  at the user's entry, the package's entry there: true: off, want on";
  :534, on over an unreadable user entry leaves it; :568, no hiding
  entry to read.
- Broken by hand (breaks.log), each one part of the fix undone, all 36
  red, the fix put back ok after. The first red of each:
  - where entries come from: no system folders :101; the system's
    folders over the user's :108; unset XDG_CONFIG_DIRS with no /etc/xdg
    :241; a relative folder kept :266; the user's folder, by any name,
    taken for a system's :290.
  - how an entry reads: Hidden ignored, Hidden=false read as hidden,
    Hidden=1 not read, all :226; the GNOME key ignored, and its 0 not
    read, :226; the system's entry starting whatever its Type :226;
    spaces around = kept, a line's CR kept, every group's keys read as
    the entry's, all :226; an entry that runs true read as opening
    magpie :120.
  - what off and on write: off always removing :108; off writing the
    hiding entry with no package entry :173; off deciding by GNOME's
    reading :373; on always writing this copy's path :131; on over a
    package's entry keeping the hiding entry :127; on taking away a
    folder of the entry's name :495.
  - the hiding entry: with no Exec :323, no Type :318, a store path for
    Exec :326, true named by a path :326.
  - what can't be read: a file in XDG_CONFIG_DIRS, a folder this user
    can't search and a name too long, each read as a failed read, :269;
    a folder at the entry's path, and a link loop, read as one :491; an
    unreadable entry read as no entry :528; every failed read an entry
    that starts nothing :576; magpie's own failed read read as no entry
    :576, and taken for a no :580.
  - links: a symlinked entry written through :437, and a hard-linked one
    :437.
- internal/autostart, daemon, cli and tui (pkgs.log): ok.
  internal/autostart with -race -count=3 (race.log): ok; nothing in it
  runs in a goroutine of its own.
- Probes at this tree, thrown away after (probes-3756.log): keepGateway
  with the real reading and all else stubbed, and Install behind
  daemon_test's fakes (fake systemctl, launchctl and loginctl alone on
  PATH). With the package's entry, keepGateway only says the gateway
  stopped and Install refuses before any systemctl call. Under the
  hiding entry off writes, and with no package entry but the user's own
  entry Hidden=true as XFCE writes it, keepGateway installs the service
  and Install goes through (the fake's daemon-reload, enable and
  restart).
- Full suite, -tags nogui (CI's Linux job tags gtk3), outside graftcp
  (systemd-run, umask 077, Bun 1.4.2 first on PATH as on CI, -timeout
  45m, log kept): 64 packages ok, autostart, daemon, cli and tui among
  them, and 8 packages red, each filed:
  - agent TestCodexDriftReached: #3398; TestInstalledVersion,
    TestCopilotFolderFromAnIDEPluginIsNotTheCLI,
    TestQoderSettingsFromAnIDEPluginIsNotTheCLI,
    TestOmpDetectedWithPiDir and TestZedAppPath: #3141;
    TestReasonixCLIRequiresSupportedNativeVersion and
    TestReasonixCLIRecoversAfterProbeTimeout: this box's /tmp/.git,
    #3641.
  - davsync TestSyncTeraCloudStillSeesOtherWrites: #3420.
  - edit TestWriteAtomicReadOnlyHardLink: this box's umask 077, #3640.
  - gateway TestPluginToldWhereTheAgentWorks: this box's /tmp/.git,
    #3675.
  - gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie:
    this box's millions of open fds, #3649.
  - #3741.
  - provider: "fatal error: close of synctest channel from outside
    bubble" in TestUnreadLoginsWriteErrorsKeepCredentials, main's CI
    red, #3632; the package's tests after it didn't run.
  - testenv TestProgram: this box's umask 077, #3640, red so at
    origin/main 6e6aba51 alone too (testprogram-main.log).
- gofmt -l: clean. go vet -tags nogui for linux, windows and darwin,
  GOOS=windows go vet ./..., GOOS=freebsd go vet ./internal/autostart/
  ./internal/daemon/, go build -tags nogui for linux and darwin and
  GOOS=windows go build ./...: all clean. Untagged linux vet and build
  stop at pkg-config here (no GTK headers on this box).

Not changed: gnome-session also reads $XDG_DATA_DIRS/gnome/autostart,
between the user's folder and $XDG_CONFIG_DIRS; it isn't in the spec,
and this doesn't read it. systemd reads an XDG_CONFIG_DIRS that is set
but empty as no folders, and opens a relative folder from its working
directory; this follows the spec: /etc/xdg for an empty one, and no
relative folder. Whether the program an entry's Exec or TryExec names is
there isn't checked, and an entry with any Exec (Exec=magpie serve,
another program's magpie.desktop) reads as magpie opening at login. With
no package entry that starts magpie (none, a hidden one, or one GNOME
has off), on still writes the path self() gives, a versioned one under a
package manager (#3621, where skygunner has a branch, 0aac5452, which
git apply --check takes over this one); a Linux refresh() that took away
an old entry of magpie's whose Exec is gone, where a package's entry
starts magpie, belongs there too.

Not tried: a real login into GNOME, KDE, MATE, Cinnamon, XFCE or COSMIC
(what each session does is read from its source, GLib's loading tried
with the two GLibs above); NixOS itself, and home-manager's links; a
real `magpie daemon install` or keepGateway over a package's entry,
which would reach this box's own systemd and gateway
(TestRefusedBesideAutostart and TestKeepGatewayAsTheTerminalQuits stub
what they read, and the probes above use the real reading).

Batch run, at tree 104d75f0a (the twelve coffemon ports together, on
origin/main 7d022bdb6), mote, macOS, go1.26.8:
- go test -tags nogui -count=1 -timeout 45m on the packages the batch
  changes (agent, autostart, daemon, gateway, library, proc, provider,
  sessions, testenv, tui, gui): ok each.
- GUI, node --test with Playwright 1.62.0, Chromium and WebKit:
  library-*.test.cjs and i18n-keys.test.cjs, 1044 of 1044 pass.
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... and go build ./...: ok at cdd7c8e46, the same
  batch on origin/main 00098f12b one rebase before.
- The whole go test ./... at cdd7c8e46: 86 packages ok, one red,
  internal/cli TestEveryRouteHasACommandOrIsNamed (GET
  /api/context/reply has no command), red on main too (#5026).
- Linux (wsl-run, Ubuntu 24.04, at 339055896, the batch on f4c664a09):
  proc, autostart, testenv, tui ok; agent: only TestGrokInstalled red
  (#3548); #4033's proc.Look reds (Zed, Copilot, Qoder, Cursor, Omp)
  pass.
- Windows (win-run, Windows 11, at 339055896): proc, autostart, testenv,
  tui ok; library and agent red in the same tests as on origin/main
  f4c664a09 (#4115, #1827, #4786; #5068, #5069, #5070 filed now).
- Rebased onto origin/main fb1f4856a (three commits, no file in
  common with the batch) and run again at the pushed tree: gofmt -l
  nothing, go build ./... and go vet ./... ok; go test -tags nogui
  sessions, agent, autostart, tui, proc, testenv ok; i18n-keys and
  library-* 1044 of 1044 pass.
- Not run before the push: the whole GUI suite (a run at cdd7c8e46 was
  cut off at 1170 of ~1174 tests when mote rebooted; it runs again after
  the tag and any red is filed).
2026-11-03 20:38:11 +08:00
yetone 75e78a5a04 tui: quitting hands the gateway to magpie's service on Linux
zaidu259434 on X: "启动codex这些cli连接会失败,AI分析没自动启动magpie的网关".
With magpie installed inside WSL (or on a server over SSH), nothing
opens magpie at login, and XDG autostart doesn't run there. So once
`magpie tui` quit, the agents it had connected asked a gateway that was
gone.

When the terminal quits having served the gateway with an agent
connected, on Linux and where magpie doesn't open at login, keepGateway:
- starts magpie's service (magpie daemon) when it is installed;
- installs it when the user never removed it (daemon.Removed: Uninstall
  now leaves a daemon-removed mark, and Install clears it), so it starts
  with the system or the login from then on, and says how to remove it;
- otherwise (no systemd, which is WSL's default, or the user removed it)
  starts `magpie serve` in its own session, logging to magpie's cache,
  and says so. In WSL it also says how to turn systemd on.
macOS, Windows, nothing connected, or magpie opening at login: the same
"gateway stopped" note as before. Docs: gui-shell.

Run:
- TestKeepGatewayAsTheTerminalQuits covers each of those cases, and
  TestUninstallRecordsTheUsersNo checks the mark.
- go test -timeout 45m on the four packages, the same run as the
  previous commit's: all four ok (gateway 428s, agent 255s, tui 23s, daemon 16s), at 6874095b1 plus this change, before the rebase
- GOOS=linux and GOOS=windows go vet, and gofmt -l, are clean.
- Real run in the magpie-desktop distro (WSL2, systemd on): a Linux build
  from this tree started under setsid as serveInBackground does,
  answered 200, and still answered 200 with its pid alive 30s after its
  wsl.exe session ended. It was stopped afterwards.

Not tried:
- the interactive TUI quit itself in WSL (over ssh there is no terminal
  to drive it), so the message and the path it picks are covered by the
  unit test only;
- a real daemon.Install inside WSL (it writes a systemd user unit for the
  distro's user).
2026-10-25 12:42:20 +08:00
yetone 5e30d5b5ae autostart, gui: Open at login says when the Mac's own switch for it is off (#2120)
Anoninz on #2120: Settings said Open at login was On while System
Settings › General › Login Items › Allow in the Background had magpie
switched off, so magpie never opened at login. autostart.Enabled now
returns a State: off, on, or needs allowing.

- macOS 13+: with the plist present, Enabled asks Background Task
  Management through SMAppService statusForLegacyURL: (cgo, ServiceManagement
  weak-linked, minos stays 12.0). RequiresApproval reads as needs allowing.
  Any other answer, a failed ask (-1), macOS 12 or a build without cgo
  (the darwin CLI release) leaves the plist alone deciding, as before: a
  failed read is never "off". launchctl print-disabled did not follow
  the switch on macOS 26.2, so it isn't used.
- BTM matches the item by the plist's Label, not its path: the tests ask
  about a label of their own, and TestMain stubs the ask so no test reads
  the user's real item.
- GUI: Settings › General keeps On and adds one line, "Switched off in
  System Settings’ Login Items…", with an "Open Login Items" button that
  opens x-apple.systempreferences:com.apple.LoginItems-Settings.extension
  through /api/open (that one URL is allowed; other x-apple URLs aren't).
  The window's focus reload clears it once switched back on. The row is
  hidden in magpie web, as before. Strings in zh, zh-TW, ja, de.
- CLI `magpie autostart` says where to switch it on. The daemon counts a
  held-back record as on (it is the user's choice, held by the system).

Part B (SMAppService.mainApp) is not in this commit: it can't be verified
here without registering in the user's real login items and logging out
to see a login launch stay tray-only.

Run:
- go test ./internal/autostart (cgo and CGO_ENABLED=0), cli, daemon, gui
  after rebasing on origin/main e4ac5e871: ok except gui's
  TestSetAnswersWhileDevinAndProvidersHang, red the same alone on
  origin/main e4ac5e871 at load ~400 (1 of 2), #1375.
- set -o pipefail; go test -timeout 45m ./... (base dd70c5e6a): every
  package ok but internal/agent, past 45m at load ~300; alone: ok (1761s).
- gofmt -l clean; go vet darwin; GOOS=linux -tags nogui and GOOS=windows
  go vet ./... clean.
- Broken by hand, each red: the binding always -1
  (TestLegacyStatusAsksTheSystem), heldBack always false and heldBack
  "!= Enabled" (TestEnabledFollowsTheSystemsSwitch); renderLoginAllow not
  called, the line not removed on focus, a wrong URL opened
  (login-allow.test.cjs).
- Real use, macOS 26.2, a build with the label patched to
  com.yetone.magpie-autostart-probe and the user's own HOME: autostart on
  -> "opens at login"; its Allow in the Background switch turned off in
  System Settings -> the needs-allowing message; back on -> "opens at
  login"; autostart off -> plist gone. com.yetone.magpie.plist untouched.
- GUI (playwright 1.62.0, chromium + webkit): login-allow.test.cjs new,
  40 of 40 (5 languages, 1000/440px, app and web). The 106 GUI files that
  load Settings or open URLs, 6 wide: reds were codex-predictions (#2624),
  segs-glide (#2683), lightweight (#2638), usage-checkin (#2549) - each
  the same alone on origin/main e4ac5e871 -, mobile-web (compares to
  origin/main, which had moved; 30 of 30 after the rebase), and
  account-alias and fonts under load only (#2984, pass alone). After the
  rebase: mobile-web, login-allow, settings-general, i18n-keys all green.
  The other ~700 GUI files were not run: at load 200-400 the whole suite
  was going at ~1000 tests an hour.
2026-10-20 02:29:39 +08:00
yetone d9ca84a2f4 gateway: built-in TLS, magpie serve/web/daemon install --tls-cert --tls-key, reloaded on renewal (RoderickZH)
magpie serve, magpie web and magpie daemon install take --tls-cert <file>
--tls-key <file> (or MAGPIE_TLS_CERT / MAGPIE_TLS_KEY). The gateway and the
web UI on its listener then serve HTTPS to other computers. Without them
nothing changes.

- Half a pair, a file that doesn't parse, or a flag with no value is
  refused at start with a message naming the missing flag.
- The pair is re-read when either file's mtime or size changes (certbot
  renewal), with no restart. A renewal that doesn't parse keeps the
  certificate already served and is logged once.
- Other computers get HTTPS only. Plain HTTP from them gets net/http's
  400 "Client sent an HTTP request to an HTTPS server".
- This computer (loopback, or a peer address equal to the local one) may
  still use plain http on the same port. Its connections are sniffed on
  their own goroutine (a first byte of 0x16 is TLS) for up to 10s, so a
  silent client holds nobody else up. gateway.URL() stays
  http://127.0.0.1:<port>, which agents, MCP servers, ServedBy, the
  healthcheck and daemon status all use, and no certificate covers
  loopback.
- WSL NAT and containers count as other computers.
- ALPN is http/1.1 only (hijacked connections and websockets need it), with
  TLS 1.2 as the minimum.
- Printed links, LAN addresses, publicURL, the serve/web banners and
  daemon install show https:// for other computers' addresses.
- The DNS-rebinding Host checks (foreignPage, rebound, the MCP
  loopbackHost) don't depend on the scheme and are unchanged.
- daemon install carries MAGPIE_TLS_* into the unit or plist. A reinstall
  or `daemon web on` that names no certificate keeps the installed pair,
  and --no-tls removes it.
- Docs: reference.md gets an HTTPS section that names Caddy as the
  alternative, gateway-routing.md gets a TLS row, and docker.html (en,
  zh, ja) gets the env rows.

What was run:
- New tests: internal/gateway/tls_test.go (others HTTPS-only, this
  computer both, a silent local client holds no other, renewed
  certificate served, CheckTLS, addresses, Relisten keeps TLS),
  internal/cli/tls_cli_test.go and daemon TestInstallCarriesTLS (linux
  and darwin, paths with space, %, & and ").
- Each was broken by hand and went red: no wrap in listen, no reload, a
  synchronous sniff, a bad renewal dropping the certificate, http in
  publicURL or the pinned LANURLs, the carried entries removed, and
  keepTLS as a no-op.
- Real use under a sandbox HOME, with a self-signed pair, on
  0.0.0.0:3461:
  - Half a pair, a garbage certificate, a flag with no value, and the
    env set to half a pair each exit 1 with the message.
  - curl -k https://127.0.0.1 and https://<LAN IP>/v1/models with a key:
    200. Plain http on loopback: 200.
  - The cert files were swapped for a second pair while it ran.
    openssl s_client then showed the new CN and serial, and curl
    --cacert with the new pair got 200. A junk key file then kept the
    second pair, logged once.
  - From the Windows box over the LAN: plain http got 400; https with
    the key got 200, and without it 401.
  - magpie web --addr 0.0.0.0:3462: https network links. From Windows,
    http got 400 and https with the key got 303.
- gofmt -l clean. go vet passed on darwin, GOOS=linux -tags nogui and
  GOOS=windows.
- go test -tags nogui for ./internal/cli, ./internal/daemon, and the TLS
  tests of ./internal/gateway and ./internal/gui: all pass at the rebased
  head.
- Two reds in the full suite (`go test -tags nogui ./...`) are the same
  on origin/main f074996d9; both are filed:
  - TestGatewayHeldByAContainer failed 2 of 5 there (#2107).
  - The gateway package runs past go test's default 10m under load:
    1208s on main and 1221s here, both passing with -timeout 40m
    (#2108).
2026-10-14 13:36:03 +08:00
yetone 82f3befd29 daemon: magpie daemon install --web serves the browser page from the service, with a link that lasts (HuanCheng Bai)
HuanCheng Bai on Discord runs `magpie daemon` on a Linux server and asked
for the daemon to serve the web page too, or for an option to turn it on.
`magpie web` run beside it is a second process, and he read it as not
seeing the daemon's state.

- `magpie serve --web[=host:port]` serves the gateway and `magpie web`'s
  page from one process (stats, StartWeb, Wait). The default page address
  is 127.0.0.1:3430. Any other argument is refused, where before it was
  ignored.
- `magpie daemon install --web | --lan | --addr host:port | --no-web` and
  `magpie daemon web [on|off] [--lan] [--addr host:port]`. The unit's
  ExecStart and the plist's ProgramArguments become `serve --web=<addr>`,
  and the key goes into the 0600 file's environment as MAGPIE_WEB_KEY. The
  key is made once. A reinstall with no flags keeps the page and its key
  (read back from the file by daemon.Web), so the link survives restarts
  and reinstalls. `web off` drops it. MAGPIE_WEB_KEY in the shell, when
  set, is used and checked like magpie web's. The page defaults to
  loopback; --lan or a non-loopback --addr prints the network links and
  magpie web --lan's warning.
- `magpie daemon status`, install and `daemon web` print the page's link.
- `magpie web` while the service serves a page prints that page's link and
  exits. Beside a gateway another magpie serves, it says so and points to
  `magpie daemon web on`.
- docs/reference.md (the service section and the Use list), and the
  site's start page (en, zh, ja). The READMEs don't cover the daemon.

Run:
- Tests: TestInstallWithWeb (systemd and launchd, plutil lint, read back,
  reinstall without the page, the shell's key not carried, no key
  refused), TestWebNotInstalled, TestParseDaemon, TestWebOptions,
  TestDaemonStatusPrintsPage and TestServeArgs. Each of these went red
  with its line reverted by hand: withKey, prevKey in webOptions, and the
  status page line.
- Sandbox HOME with a fake launchctl:
  - `daemon install --web --lan` wrote a 0600 plist with
    `serve --web=0.0.0.0:3430` and the key. plutil said OK, and it printed
    the link, the network links and the warning.
  - Reinstalling with no flags kept the key. `daemon status` showed the
    link.
  - `daemon web off` / `daemon web` / `daemon web on` behaved as above,
    with a new key after off.
  - `daemon web bogus` and `serve --lan` gave the usage errors.
- `serve --web=127.0.0.1:3462` served the gateway (3461) and the page in
  one process. The page answered 200 with the key and 401 without, and the
  page and /v1/models both showed 3 models. `magpie web --no-open` beside
  it printed the service's link and exited 0.
- gofmt -l; go vet (gui), and go vet -tags nogui for darwin, linux and
  windows; go test -tags nogui ./... in a sandbox HOME.

Not tried: a real systemd on Linux. The unit text is checked by test only.
2026-10-14 08:32:41 +08:00
yetone c6de0d2a54 daemon, agentenv: the service tools run through proc, and PI_DESKTOP_DATA_DIR is cleared in every sandbox
main has been red on ubuntu and macOS since 692a779d3's run (38376844048),
with two guards that each caught a commit run on chosen packages only:

- TestNoCommandBypassesProc: 5617281f5's daemon.run called exec.Command,
  so on Windows the service tools would open a console window. It now uses
  proc.Command.
- TestFolderVarsAreListed: cdb7b5ff7 made Sessions read PI-Desktop's data
  folder from $PI_DESKTOP_DATA_DIR without listing it in agentenv.Vars, so
  a test could read a developer's real PI-Desktop chats.

Run: go test -tags nogui -count=1 ./internal/agent ./internal/proc
./internal/daemon ./internal/agentenv ./internal/sessions (sandbox HOME),
all ok; both tests failed on the run above before; gofmt -l; go vet
darwin, GOOS=linux -tags nogui, GOOS=windows.
2026-10-13 17:49:07 +08:00
yetone 05edac2c36 daemon: the service's PATH has the Node a terminal gets, nvm's default among them (bookyue on Discord)
What changed: `magpie daemon install` now widens PATH before writing it
into the systemd unit or launch agent, the way the app does when opened
from the Dock (proc.UserPath): the version managers' and npm's folders
that exist, nvm's default Node (not its newest), then the login shell's
own PATH (proc.LoginPath), each after the folders the install's shell
already had. When no node is found even then, the install says so.

Why: the service has only the PATH written into it. With nvm loaded
lazily, or an install run over ssh without a login shell, that PATH had
no node, and plugins (and the MCP servers and agent CLIs they start)
couldn't find it under systemd (bookyue, #feedback 838099).

Run: gofmt -l; go vet darwin, linux -tags nogui, windows (daemon, cli);
go test ./internal/daemon ./internal/cli -count=1 on macOS; the daemon
test binary for linux/arm64 in debian:bookworm-slim (all pass).
TestInstallFindsNvmNode uses the real widening with a sandbox HOME
holding nvm v20 (default) and v22: the unit's PATH has v20's bin, not
v22's, after the shell's own. With the widenPath() call removed by hand
it fails (PATH is the shell's alone, Node() empty).

Not run: a real systemd install with nvm since this change (the earlier
container run, before it, covered install/restart/linger); not tried
with fnm or volta, which UserPath handles the same way for the app.
2026-10-13 17:15:51 +08:00
yetone 5617281f55 cli: magpie daemon install|uninstall|start|stop|restart|status keeps the gateway running as a service (HuanCheng on Discord)
What changed: a new internal/daemon package and `magpie daemon`, which run
`magpie serve` as the user's own service, with no window and no desktop
session.
- Linux: a systemd user unit at ~/.config/systemd/user/magpie.service
  (Restart=on-failure), driven by systemctl --user. status reads
  `systemctl --user show`, and says `sudo loginctl enable-linger $USER`
  when the user doesn't linger. su/sudo without the user's bus, no
  systemd, and WSL without systemd each get an error that says what to
  do.
- macOS: a launch agent, com.yetone.magpie.serve (RunAtLoad, KeepAlive on
  failure), loaded with launchctl bootstrap gui/$UID. Its log is
  ~/Library/Logs/magpie/serve.log.
- Windows and Android: refused, pointing to `magpie autostart on` (or to
  WSL).
- The service carries PATH, the proxy variables, MAGPIE_*, the XDG folders
  and the agents' folder variables from the install shell. The file is
  0600 because a proxy can hold a password. It runs magpie by its PATH
  name when that is the same file, so Homebrew's link survives updates.
  It refuses a go-build binary and an AppTranslocation copy.
- Install is refused while `magpie autostart` is on: the tray app serves
  the gateway too, and the two would crash-loop on the port.
- Docs: a reference.md section, and one sentence on the site's start page
  (en, zh, ja).

Why: users on headless Linux boxes asked for a supported way to keep
`magpie serve` up across reboots instead of hand-written units. The
existing `magpie autostart` needs a desktop session (an XDG autostart
entry).

What was run:
- gofmt -l; go vet on darwin, GOOS=linux -tags nogui, and GOOS=windows.
- go test ./internal/daemon ./internal/cli on macOS, at origin/main
  5b9530b56.
- The daemon tests cross-built for linux/arm64 and run in a Debian
  container.
- The tests use fake systemctl, launchctl and loginctl on PATH in a test
  home.
- Each guarantee was broken by hand and its test went red: the % escape,
  the autostart refusal, the 0600 chmod, the bootout before re-bootstrap,
  the go-build refusal, the bus advice, the last-exit filter and the
  PATH link.
- Real use on Linux: a Debian bookworm container running systemd as PID 1,
  with the linux/arm64 build, as an unprivileged user.
  - install: the gateway answered on :3425, and the unit was 0600.
  - kill -9: systemd restarted it 5s later.
  - Container restart with linger on: it came back by itself.
  - restart, stop, start and uninstall worked, and the unit and its wants
    link were gone afterwards.
  - Under sudo without XDG_RUNTIME_DIR it printed the bus advice.
- macOS: the darwin build ran `magpie daemon install` under a sandbox HOME
  with a fake launchctl. plutil -lint passed on the plist, and the calls
  were print and then bootstrap.

What wasn't: no real launch agent was loaded on a Mac (deliberately: never
on this machine), so launchd's own acceptance of the plist is checked
only by plutil. The linger advice was checked by the unit test, not seen
from a real SSH login. Windows was not run beyond vet.
2026-10-13 17:05:59 +08:00