Commit Graph
3 Commits
Author SHA1 Message Date
yetone 9d0c9b41ea serve: follow an update put in where it runs from; a replaced magpie.real is magpie
acezan on Discord (Linux 5.15, 0.1.2959 to 0.1.2960): `magpie web` as a
system unit updated /root/.local/bin/magpie.real, and the user unit's
`magpie serve` (`magpie daemon install`) ran on for good from "magpie.real
(deleted)". The panel said an older magpie served the gateway, and its
"close it here" then said the port holder "isn't magpie" and left it.

Why serve stayed old: the app and `magpie web` follow the program they
started from (gui's followReplaced) and restart into an update; plain
`magpie serve` had no such look, and answered `magpie update`'s restart
ask (gateway.RestartPath) "unsupported". Nothing else can restart it
reliably: a system unit doesn't reach the user's `systemctl --user`, and
on a Mac the launch agent's serve (#4335) is the same serve. So serve now
looks itself (internal/cli/serve_follow.go): every 5s it checks its
program with update.Replaced; once replaced, it waits until the gateway
is idle (nothing in flight, 10s since the last end, as gui's idle rule;
after an hour, nothing in flight is enough), ends the plugin hosts and
runs the new program in its own place (update.Reexec: exec in place on
Unix, same PID, so systemd and launchd keep tracking it; a new process
on Windows). The restart ask now answers restarting/waiting/current. A
build that isn't a release never does it.

Why "not magpie": proc.IsMagpie judged the file name, and "magpie.real"
(the program the installer's wrapper script runs) didn't match. It now
trims ".real". And proc.RunsMagpie, used by the takeover and the port
check, also reads Go's build info of the running program (on Linux
through /proc/<pid>/exe, which still reads after the file is replaced)
and calls it magpie when its main package is github.com/yetone/magpie.
A process that isn't magpie is still never ended (the existing
"other-server" case passes unchanged).

Tests, each red on the old code:
- cli TestServeRestartsIntoAnUpdate: a copy of the test binary named
  magpie.real runs `serve` on a free port; a script renamed over it is
  run in its place with the same pid and args, and the old port stops
  answering. Old code: "serve ran on as the old version" after 40s.
- cli TestServeFollowWaitsForIdle: current / waiting / tool-chain gap /
  waitMost with a request in flight / idle / non-release.
- gui TestGatewayFixQuitsAReplacedMagpieReal: a fake older gateway named
  magpie.real, its file removed after start, is quit and the gateway
  served here. Old code: {OK:false Reason:not-magpie}, acezan's case.
- proc TestIsMagpie magpie.real case; TestRunsMagpieByItsBuild: a copy
  named server.real, its file removed, is told by its build (Linux
  /proc/<pid>/exe), and is not magpie with the real main path.

Run: the targeted tests above on Linux (wsl-run, magpie-desktop, -tags
nogui) and macOS (mote-run), all pass. Broken by hand on Linux: dropped
the ".real" trim, made RunsMagpie only IsMagpie, and set serve's follow
to nil; all four new tests went red (output above), restored after.
gofmt -l clean; go vet (darwin, GOOS=linux -tags nogui, GOOS=windows)
and builds on mote.

Not tried: a real systemd user unit or launch agent with a real update;
the Windows restart path (Reexec starts a new process) beyond vet; the
macOS TCC part of #4335 (this restarts the launch agent's serve into the
new bundle path only if that path is where it started from). #4335 stays
open, and ggbdpq's daemon.RestartReplaced patch there is not applied.
2026-11-04 23:55:14 +08:00
yetone bde11c7780 plugin, cli tests: stay off the network, so a host start never waits 30s on the real models.dev (#1922)
What: internal/plugin's and internal/cli's TestMain now call
testenv.Offline, as gateway's and provider's already do, and each package
has TestStaysOffTheNetwork. TestPluginListSaysMiddleware runs on
testenv.Bun via MAGPIE_BUN like its siblings, instead of downloading
Bun 1.4.2 from GitHub on every run (the guard turned that up).

Why: every plugin test's sandbox is a new home with no models.dev
catalog, so startOn fetched the real https://models.dev/api.json, with a
30s cap, on each host start until one succeeded. When models.dev didn't
answer the macOS runner, a test that starts two hosts (Add, then Restart
or a list changed elsewhere) spent 30s + 30s; the second start reached
its init call with the test's one-minute ctx already gone: "starting
plugins: context deadline exceeded" at 60s, TestPluginsChangedElsewhere
at 90s. Ubuntu's runner got models.dev's answer in time.

What was run:
- Injected the delay on the old code: a DialContext that holds every
  non-loopback dial. TestFallenBackListKeepsTheLastOne/throws=,=1,=own
  60.1-60.4s, TestUnloadedPluginKeepsProviders 60.32s,
  TestPluginsChangedElsewhere 90.36s, each "starting plugins: context
  deadline exceeded" - CI's signature. With Offline on top of the same
  slow dial: all pass in 0.4-1.0s.
- TestStaysOffTheNetwork fails on the old TestMain ("models.dev answered
  200 OK"), passes with the change.
- 8 parallel -race processes x -count=6 of the three tests on the old
  code: all passed (models.dev answering here), so the delay, not load,
  is the cause.
- go test -tags nogui ./internal/plugin/... ./internal/cli/... pass;
  go vet (darwin, linux, windows), gofmt -l clean.

Not tried: on a macOS CI runner itself (the push's run is the check).
The product path is unchanged: a magpie whose models.dev fetch hangs
still waits up to 30s on each host start until one succeeds.
2026-10-13 13:02:58 +08:00
yetone 23e764d0e0 cli: magpie's commands move from the root into internal/cli; the root keeps only main.go
The root held 89 .go files (30 commands, 59 tests). They move as they are
to internal/cli (package cli), with git mv so history follows them. The
only code change is main() becoming cli.Main(version): the root's new
main.go keeps `var version` in package main, so -ldflags "-X
main.version=…" in the Makefile, the Dockerfile and magpie-releases, and
every workflow, work unchanged. go-winres's rsrc_windows_*.syso still land
next to package main in the root.

Tests that read testdata by a root-relative path now go up one level
(../plugin/testdata/fake, ../sessions/testdata). Five subsystem doc links
point at internal/cli/.

Run:
- go build ./..., gofmt -l, go vet on darwin, linux and windows (nogui)
- builds with tags production, dev, "production nogui"; windows
  -H windowsgui; linux CGO_ENABLED=0
- make release-cli TARGETS="darwin/arm64 linux/amd64 windows/amd64
  android/arm64" VERSION=9.9.9-tidy: all four built, binary prints
  "magpie 9.9.9-tidy"
- docker build --build-arg VERSION=7.7.7-tidy: container prints
  "magpie 7.7.7-tidy"
- go test ./internal/cli (default and nogui): 152 tests, the same count
  as the root on main
- go test ./...: all packages pass (agent, gateway, library, provider
  timed out at 10m under other sessions' load and passed rerun alone
  with -timeout 40m; none of them changed)
- the built binary in a sandbox HOME: version, help, ls, providers,
  claude, and an unknown command's error
2026-10-12 20:40:22 +08:00