Files
magpie/internal/cli/session_cmds_test.go
yetone 7d5db0f29a cli: a command that ends ends its plugin hosts, and one that doesn't serve starts none (#7146)
athemeroy (WangYeruo on X) runs `magpie serve` as a LaunchAgent with the
magpie-google-ai-studio browser plugin and 11 accounts. Each
`magpie --version`, `version`, `licenses` or `group <id>` run beside it
left a second magpie-bun host with PPID 1, holding the Chrome profiles
the gateway's host wanted ("Chrome exited before starting WebView").
Their analysis was right on each count:

- plugin.Cached starts a background list of the plugins' providers
  whenever this process hasn't listed them (provGood is in memory), so
  every short command reaching provider.Catalog (startUp's RenameLegacy,
  groups, models, ...) started a host.
- Main ended through os.Exit and never stopped it. A host whose stdin
  closes leaves only once nothing is pending, and a plugin holding a
  browser keeps Bun up meanwhile, so the host outlived magpie, adopted
  by init. SIGTERM (launchd stopping serve) orphaned serve's own host
  the same way.
- --version and licenses were answered after startUp.

Now:
- Cached asks the plugins in the background only when
  plugin.ListInBackground is on; run sets it from servesPages (app,
  web, serve, TUI). Other commands read the list on disk.
- Main ends through exit(), which runs plugin.EndHosts first, on
  success and error; die does too on SIGINT/SIGTERM/SIGHUP, bounded by
  hostsOnSignal (20s, past the 15s a renewing host gets).
- provider test returns exitError instead of os.Exit(1).
- -v/--version/version and licenses answer before any start-up work.

Audit of the os.Exit calls in internal/cli: data_cli.go:135 and the
CheckEnv exit in Main run before anything can start a host;
claudeLetGo already called EndHosts.

Tests (red on the old code, the non-test sources reverted to
14b6b9555, green with the fix):
- TestOneShotCommandsStartNoPluginHost: groups, group fakeco, models,
  providers, accounts each started a host (5 reds); version, --version,
  -v, licenses and agents start none; Cached in a serving magpie still
  lists.
- TestExitEndsPluginHosts: a real host whose stuck usage hook holds it
  up (new STUCK_USAGE_HOLD, STUCK_PID in testdata/stuck), the magpie
  ended by `version`, by an error and by SIGTERM: all three left the
  host at PPID 1 on the old code, none now. 10 of 10 on macOS, 3 of 3
  on Linux (WSL), version/error on Windows (no SIGTERM there).
- TestSessionCommandsSkipStartUp: version/--version/-v/licenses don't
  run startUp (red on the old code for version). Three tests that used
  `version` to reach startUp/makeDirs use `groups`.

Real use, sandbox HOME on mote, a plugin whose provider list takes 90s
holding a timer, signed in with a key, `magpie serve` running:
- old binary: groups, group holdco and models each left a magpie-bun
  at PPID 1; after SIGTERM to serve, serve's host was at PPID 1 too
  (4 left).
- new binary: only serve's host, and none left after SIGTERM. The
  commands take 0.02-0.15s.

Run at the pushed head:
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., go build: ok (mote).
- go test -count=1 -timeout 45m ./... on mote: 102 packages ok, two
  red, each red the same at the base 14b6b9555 (-count=3/5 there):
  internal/cli TestHealthcheckFindsTheRunningGatewayWithoutMagpieAddr,
  TestUpdateRestartsTheGatewayItFindsWithoutMagpieAddr and
  TestUpdateFindsAMagpieThatWroteNoRecord (#6471, #6223: an ssh
  session's forward holds 127.0.0.1:3425 on mote), and internal/gateway
  TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes (#7117).
- Rebased on 598bc4534 (it changes internal/cli's update code), at
  the pushed head: go test ./internal/cli ./internal/plugin on mote:
  plugin ok; cli ok but for the same three #6471 reds.

Not tried: the reporter's real Google AI Studio plugin with Chrome, and
a real LaunchAgent stop (launchctl); the repro sends serve SIGTERM as
launchd does.
2026-11-19 10:38:25 +08:00

55 lines
1.5 KiB
Go

package cli
import (
"os"
"testing"
)
// What an agent starts for each session or message (the image MCP, Claude
// Code's MCP helper, the status line) never runs the start-up moves, which
// list every provider and account and so start the plugin host and ask
// claude, cursor-agent and devin who is signed in (#3318).
func TestSessionCommandsSkipStartUp(t *testing.T) {
ran := 0
was := startUp
startUp = func() { ran++ }
t.Cleanup(func() { startUp = was })
t.Setenv("MAGPIE_ADDR", "127.0.0.1:1")
t.Setenv("MAGPIE_MCP_CALLBACK", "")
null, err := os.Open(os.DevNull)
if err != nil {
t.Fatal(err)
}
defer null.Close()
stdin := os.Stdin
os.Stdin = null
t.Cleanup(func() { os.Stdin = stdin })
for _, args := range [][]string{
{"mcp", "image"},
{"mcp", "nope"},
{"claude-mcp-helper"},
{"statusline"},
} {
_ = run(args)
if ran != 0 {
t.Fatalf("%v ran the start-up moves", args)
}
}
// nor do the version and the notices, which read no file: the moves
// read the providers, which started a plugin host beside the running
// gateway's and left it behind (#7146)
for _, args := range [][]string{{"version"}, {"--version"}, {"-v"}, {"licenses"}} {
if _, err := printed(t, func() error { return run(args) }); err != nil {
t.Fatalf("%v: %v", args, err)
}
if ran != 0 {
t.Fatalf("%v ran the start-up moves", args)
}
}
// and a command that reads the files still has them brought up first
_ = run([]string{"groups"})
if ran != 1 {
t.Fatalf("groups: start-up ran %d times, want 1", ran)
}
}