Commit Graph
8 Commits
Author SHA1 Message Date
yetone 382c8c0bdf proc: magpie serve reaps the processes it adopts (Kling__Lab on X)
254 <defunct> children of `magpie serve` after 7 days, growing ~9 an hour
(magpie, uv, node, npm, magpie-bun, code-review-graph), only a restart
cleared them. Every command magpie starts is waited for (the AST guard
TestEveryStartedCommandIsWaited holds, and I read each Start site and
Output/StartTree/StartReaped by hand: none is missed). The zombies are the
grandchildren: an agent CLI's MCP servers whose parent ended are adopted by
magpie where it is PID 1 (a container) or a child subreaper, and nothing
waits for a process it never started.

proc.KeepReaping (Linux, from serve) does nothing unless magpie adopts
(PID 1 or PR_GET_CHILD_SUBREAPER), and then reaps a child of its own that
was a zombie at two looks 5s apart. Not tried: the reporter's container;
the cause (PID 1 / subreaper) is inferred from the report, not seen.

Run: TestAdoptedOrphansAreReaped (subreaper test process, 6 orphans) and
the proc package through wsl-run on magpie-desktop: ok. Broken by hand
(KeepReaping returning at once): 6 adopted zombies left, red.
2026-11-13 10:29:07 +08:00
RainierandClaude Opus 5.5 4d9b798d50 web: a LazyCat box's administrators open the page with the box's sign-in (MAGPIE_WEB_SIGN_IN=lazycat) (#5987)
magpie in a LazyCat app (懒猫微服) is opened from the box's app list by
someone signed in to the box already, and asks for a key they never
saw: it is in the container's log. A package can only hand the key to
every browser that reaches the app, or put a proxy in front that adds
the key's cookie to what the box signed in.

The box's ingress, lzc-ingress, sends a browser that isn't signed in to
the box's login page, and puts on every request it passes the app the
box account (X-HC-User-ID), its role (X-HC-User-Role, ADMIN or NORMAL)
and what sent it (X-HC-Source, client for a browser), in place of what
the caller sent. With MAGPIE_WEB_SIGN_IN=lazycat webGuard takes that
sign-in besides the key: lazyCatSignIn lets in an ADMIN account sent by
a browser, each of the three named once, and tells anyone else the box's
reason.

The box's sign-in is a cookie of the box's, and the box's apps are one
site, so a browser sends it with another app's page's requests too: an
earlier build of this change let another app's page on the box include
/boot.js and read window.bootPrefs. So it is taken only for what the
page itself or the user asked for (boxFetched, the Fetch Metadata:
Sec-Fetch-Site same-origin or none, or a GET or HEAD navigation to a
document from another page), never for another page's script, frame,
fetch or form POST, nor from a browser that sends no Fetch Metadata.

It sets no cookie of the page's own, so signing out of the box signs out
of the page. The key's link and cookie work as before, /api still
answers the page alone (webOwnPage), and the gateway's client routes on
the page's port go to the gateway first (onePort), which asks for a
gateway key. StartWeb refuses another value, and lazycat where
LAZYCAT_APP_DOMAIN, which LazyCat sets in an app's containers, isn't
set: all magpie can check, so the setting is the package's word that the
box's ingress is the only way to the page's port. magpie web prints the
box's address beside the link.

Docs: reference.md's new LazyCat section, with a manifest and what must
never reach the page's port, and gui-shell.md's Browser mode row.

On a box (lzcos 1.6.2+fix.4), a probe app that echoes its request
headers: a signed-in browser's request carries X-HC-User-ID,
X-HC-User-Role ADMIN and X-HC-Source client, forged ones replaced; not
signed in, / goes to the login page and a public path gets none of the
three (forged ones taken off, repeated or in lower case too), while
X-HC-User-Groups passes on as the caller wrote it, so it is never read;
a TCP ingress passes every header as sent. Another app's container
reaches the app through the ingress alone (straight to the app's
container it times out); the box itself, and a container in its network
(network_mode: host, which a manifest may set), reach the port.

A test app made from the docs' manifest, running a build of this change
on 68cb92e8e in the image's place as its user (uid 65532): the box's
administrator opens the console with no key, and no cookie is set;
/v1/models still asks for a gateway key; from another app's page a
navigation opens the console, while a script include of /boot.js and a
frame are turned away; in the container, past the ingress, a request
with no Fetch Metadata, no X-HC-Source, NORMAL or a header twice gets a
401. Bound to /lzcapp/var/config, which the box makes as root's, magpie
couldn't write /config; bound to /lzcapp/var it can, and a gateway key
outlived the container.

Tests, through webServed with the headers the probe saw:
TestLazyCatSignInOpensThePage, TurnsAway, AsksWhereFrom, IsAskedEachTime
and IsNoGatewayKey (the client routes, with each sign-in that opens the
page), and TestWebBoxSignInChecksItsValue.
TestRemoteOpensTheOtherMagpie, new on main, gives webHandler no box
sign-in.

Run, at this tree, on 6c3f113ce: Docker golang:1.26.8 (linux/arm64):
gofmt clean; go vet -tags gtk3 ./... (with the GTK and WebKitGTK
headers, as CI), -tags nogui and GOOS=windows clean; both builds ok.
macOS 27.2 (go1.26.8, not root, a temporary HOME): go vet ./... and the
two cross vets clean, the three builds ok; go test -tags nogui ./...,
TestScriptFindsClaudeMoved skipped (it runs the real
/Applications/Claude.app, #5989): 92 packages ok; red outside
this change, each red the same at 6c3f113ce:
TestBunProxyLoginWithReservedCharacters (#3397),
TestGitUpdateInstallsTheBranchsCommitNow and
TestGitUpdateKeepsACommitPin (#5988), on this Mac's bun 1.3.11,
older than MinBun; TestEveryRouteHasACommandOrIsNamed: POST
/api/settings/join, new with e053c337a, has no command. /join, new
with it too, is a client route, so the docs' manifest lists it.

Broken by hand, each red, all green again put back; a break that lets in
what must be turned away was seen to reach the page: TurnsAway with the
once-each check out, no sender taken, the role in any case, the role or
the sender check out (IsAskedEachTime too), app:… and system alone
turned away, or the key's "open the link" kept in the box's 401;
AsksWhereFrom with boxFetched out, no Fetch Metadata taken, another
page's navigation taken of any method or into a frame, an object or an
embed (or anything but an iframe), its request for a document that isn't
a navigation, or another app's page's script (same-site, no-cors);
OpensThePage with another page's navigation taken by GET alone, or none
taken (IsNoGatewayKey too), a cookie set, or webOwnPage out;
IsAskedEachTime with the first sign-in kept; ChecksItsValue with lazycat
taken outside LazyCat or StartWeb not asking webBoxSignIn's error;
IsNoGatewayKey with the client routes behind the box's sign-in, or one
the box signs in on, or a HEAD to one, given to the page;
TestLazyCatManifestListsTheClientRoutes with /me out of the docs.

Not tried: a NORMAL account in a real browser (the box has one
account); signing out of the box in a browser; the box's own client
apps opening the page; a browser without Fetch Metadata; a header named
twice on a signed-in request through the ingress (a browser can't send
one); a multi-instance app; another lzcos version; a community plugin's
page on the box; a real vendor call through the box.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 10:15:40 +08:00
yetone 6c201095d8 gateway: in Docker, other containers on the host are shown the host's Docker bridge address
jiezhengj (Discord #feedback) runs magpie in Docker on a NAS at
192.168.31.218. The web UI's host address, http://192.168.31.218:3425,
works for other devices. Other containers on the NAS couldn't use it, and
not the container's own 172.17.0.x either. What works for them is the
host's Docker bridge address, http://172.17.0.1:3425. On Linux,
host.docker.internal is there only in a container given extra_hosts.
magpie showed no such address.

- gateway.ContainerHostURL: in a container on a bridge network, the
  gateway of /proc/net/route's default route (172.17.0.1 on the default
  bridge, a compose network's own otherwise), on the gateway's port.
  It is "" outside a container and on the host's network
  (`network_mode: host`, told by the host's
  docker0/br-*/veth*/podman* interfaces). In that case ContainerAddrs is
  false too, since the addresses are the host's own.
- The GUI gets it as /api/providers' gateway.containerHost,
  /api/settings' lanContainerHost and boot's sideGateway.containerHost.
- settings.js lanShown is fed by the new gwLAN(g) for Gateway, Connect,
  the sidebar and the key page. Each case lists:
  - Opened at the host: the host's address first, then the bridge
    address, labelled "Other containers on this host".
  - At localhost: the bridge address alone, with that label, and a line
    that the host's own is MAGPIE_PUBLIC_URL's.
  - No default route: as before.
  The container's own 172.17.0.x is no longer offered beside them. The
  sidebar line and the key page prefer an address other machines reach.
  The scheme comes from the URL, not a hard-coded http. Three new strings
  are in zh, zh-TW, ja and de.
- CLI: `magpie web` and the share banner print "other containers on this
  host <url>".
- docs/reference.md and site docs/docker (en, zh, ja): the log example and a new "Other
  containers on the same host" paragraph.

Run on mote, at this change on d8501848a:
- gofmt -l: clean. node --check: passes.
- Vet: darwin go vet, GOOS=linux go vet -tags nogui and GOOS=windows go
  vet of internal/gateway, internal/gui and internal/cli: ok.
- go test -timeout 45m: internal/gateway ok. internal/gui is red only on
  TestMainWindowCorners/macOS_26_SDK, which is red on macOS 27 at main
  (#3949). internal/cli is red only on TestEveryRouteHasACommandOrIsNamed
  for GET /api/context/reply, red at main (#5026).
- GUI, chromium and webkit: lan-docker (en, zh, zh-TW, ja, de) and the
  40 files on the caller-keys fixture or the LAN paths. 586 tests, 580
  pass. Each red is also red alone at the base d8501848a or af9ac3d25:
  - phone-connect-menu 420, 4 reds (#4706, #4707).
  - gateway-key-models webkit en (#4616, #4081; 4 of 4 at the base).
  - gateway-caller-keys chromium en (#3797; 2 of 3 at the base, 1 of 3
    here).

After the rebase onto 3f1f1882a (187f3e272, the usage page alone, came after): the commits
between touch none of these files. Linux and Windows vet ok. go test -run
'Container|HostNetwork|DefaultGateway|PublicURL|LAN|Boot|Provider' ok in
gateway, gui and cli. GUI lan-docker, i18n-keys, side-gateway-lan and
key-page: 56 of 56, in both engines.

Broken by hand, in a copy:
- defaultGateway reading big-endian: TestDefaultGateway goes red
  ("1.0.17.172").
- The old settings.js, client.js and keys.js: every lan-docker test goes
  red, in both engines and every language.
TestDefaultGateway stubs the in-container check, so it tests the
container path on mote and CI too.

Not tried:
- a real Docker container, on a NAS or anywhere;
- reaching 172.17.0.1 from a container on another compose network on the
  reporter's NAS;
- a published port that differs from the gateway's (MAGPIE_PUBLIC_URL
  sets the host's; the bridge address uses the gateway's own port);
- Podman's rootless networking (slirp4netns, pasta), where the default
  route may not be the host.
2026-11-03 19:41:02 +08:00
yetone e056779a67 gui: the app serves its page to a browser, behind its key (Settings › Browser access)
jiakun_zhao on X ran `magpie web` in a terminal beside the desktop app,
to manage it through a Cloudflare tunnel. Settings › Network and sharing ›
Browser access now has the app serve the same page itself: Off, This
computer (127.0.0.1, for a tunnel or SSH forward) or Local network.

Access is magpie web's, unchanged: the key in the link is traded for the
cookie (webGuard), the allow list holds, and the gateway's client routes
on the page's port need a gateway key (onePort). A rebound Host and
Origin: null get no cookie. There is no way to serve it without the key.
The key and choice live in web-page.json (0600) beside settings.json, so
a sync never carries them. GET/POST /api/web-page are the app windows'
alone: a browser on the page can't read the key or turn the page off. A
busy port (magpie web, the magpie before an update) is tried every 4s;
`magpie web` on the same port opens the app's link instead.

Run at d43b7c40d (this change on ef155a5ca), then rebased onto the four
newer main commits, which touch gateway, usage, proc and library only:
- gofmt -l internal/gui internal/cli: nothing. go vet, GOOS=linux go vet
  -tags nogui and GOOS=windows go vet of internal/gui and internal/cli:
  clean. go build: ok.
- go test -timeout 45m ./internal/gui/ ./internal/cli/: ok (180s, 70s).
- GUI, alone: browser-access.test.cjs and model-test-one.test.cjs pass in
  both engines; i18n-keys 23 of 24, its red is #3897 ("Runs as" from
  7f827b3b0), not this change. agent-stale-copies.test.cjs 80 of 80 at
  8d343a811 (this change before the rebase).
- Full GUI suite at 8d343a811: cut short. The disk filled (708MB free):
  browsers failed to launch with ENOSPC in agent-stale-copies, which then
  passed 80 of 80 alone, and node ended with exit 7 before the rest of
  the files ran. There is no full GUI record for this change.

Not tried: a real Cloudflare tunnel to the page, and Local network from
another machine.
2026-10-27 18:58:16 +08:00
yetone a3d4f8c5c4 cli: magpie serve and magpie web give what they start the user's node too (#3803)
5b4da5bee (#2685) made proc.UserPath add fnm's node on Windows, and on
macOS and Linux it reads the login shell's PATH. The app, the panel and
the daemon called it; magpie serve and magpie web didn't, so one started
by autostart or a service without a terminal's PATH ran an MCP server or
agent by npx.cmd's full path with no node ("'node' is not recognized").
Both now call it once their flags are read, before the gateway starts.
magpie tui runs in a terminal and keeps that PATH.

TestGatewayStartsGetUserPath parses internal/cli and fails when runGUI,
runPanel, webCmd or serveCmd stops calling proc.UserPath.

Run:
- go test ./internal/cli ./internal/proc: ok. The new test is red on
  origin/main's web_cli.go (webCmd and serveCmd named).
- gofmt -l clean; go vet -tags nogui GOOS=windows and linux: ok.
Not tried: magpie serve started from a Windows service with fnm.
2026-10-27 06:52:44 +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 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