Files
magpie/internal/library/mcp_reap_test.go
T
yetone c89284ae66 library: a server turned off everywhere has its running processes ended (#3885)
Turning MCP servers off in the Library rewrote the agents' configs, but
Codex, the Codex app and Claude Code read them only at start, so the
servers they had started kept running. With every server turned off, the
reporter's machine still held 17.75 GB of node processes for them.

A change, and the sync at start, now record each stdio server it took out
of an agent's config as that config had it. Once no agent's config runs
that command and those arguments (another agent's entry, or the user's own
under another name, keeps it), proc.EndTrees ends every process whose
command line runs it, with all it started: the cmd /d /s /c "npx -y x"
wrapper, the node npx-cli.js under it and the server under that. magpie
and what it runs under are never picked, and a process under one picked
goes with that one. An interpreter with nothing after it (node, npx) is
never matched. It runs after the change lets go of the Library lock and
before it returns, so magpie library mcp ends them too before it exits.

On Windows the processes come from a Toolhelp snapshot with their command
lines (no PowerShell); elsewhere from ps -o pid=,ppid=,args=.

Run at base e54a7d50b, before the rebase onto 34d38ed66:
- gofmt -l internal/: clean; go vet -tags nogui for linux, windows, darwin
  on internal/proc, internal/library, internal/cli: ok
- go test -count=1 -timeout 45m ./internal/proc ./internal/library
  ./internal/cli: ok (28s, 197s, 66s)
- the proc test binary on the Windows box: TestParseFamily, TestPicks,
  TestEndTreesEndsWhatItStarted, TestEnd*, TestList*: PASS
- broken by hand: endFamily ending the picked process alone on macOS made
  TestEndTreesEndsWhatItStarted red (its child ran 20s on); dropping the
  dropped-server record made TestDroppedServerEndedOnceNoAgentRunsIt red
- real, on the Windows box in a sandbox USERPROFILE, with Node 22.11.0
  (SHA-256 checked against nodejs.org's SHASUMS256): magpie library mcp add
  fs npx -y @modelcontextprotocol/server-filesystem <dir>
  agents=codex,claude; a stand-in agent started it as Codex does, through
  cmd with stdin held open: cmd -> node npx-cli.js -> cmd -> node
  index.js, 4 processes. Off for Codex only (Claude Code still has it): 4
  left. Off for every agent: "ended 1 process trees of fs", 0 left, the
  stand-in agent still running.

Not tried: a real Codex or Claude Code session holding the server (no
signed-in agent may be copied to the sandbox). Not done here, from the
same issue: writing the installed bin instead of npx, the opencode.json
servers stub, and an applied.json record of MCP changes.

At the rebased head (base 34d38ed66): go build ./..., GOOS=linux/windows/darwin
go vet -tags nogui ./internal/proc ./internal/library ok; TestParseFamily,
TestPicks, TestEndTrees*, TestDroppedServer*, TestLaunchedBy ok. The
rebase brought no proc or library changes.
2026-10-28 00:19:24 +08:00

94 lines
3.9 KiB
Go

package library
import (
"path/filepath"
"slices"
"testing"
)
// A server turned off goes from the agents that run it only once no agent's
// config runs that command: another agent's entry, or the user's own under
// another name, keeps it (#3885).
func TestDroppedServerEndedOnceNoAgentRunsIt(t *testing.T) {
h := sandbox(t)
write(t, filepath.Join(h, ".codex", "config.toml"), "")
write(t, filepath.Join(h, ".config", "opencode", "opencode.json"), "{}")
var dropped []*Server
was := ending
ending = func(d []*Server) { dropped = append(dropped, d...) }
t.Cleanup(func() { ending = was })
names := func(ss []*Server) []string {
var out []string
for _, s := range ss {
out = append(out, s.Name+": "+s.Command+" "+filepath.Join(s.Args...))
}
return out
}
ok(t)(SaveServer("", Server{Name: "tavily", Transport: "stdio", Command: "npx", Args: []string{"-y", "tavily-mcp"}, Agents: []string{"codex", "opencode"}}))
if len(dropped) != 0 {
t.Fatalf("dropped on adding: %v", names(dropped))
}
ok(t)(ServerAgents("tavily", []string{"opencode"}))
if got := names(dropped); !slices.Equal(got, []string{"tavily: npx " + filepath.Join("-y", "tavily-mcp")}) {
t.Fatalf("dropped from codex: %v", got)
}
if got := unused(dropped, Targets()); len(got) != 0 {
t.Errorf("OpenCode runs it still, but it is ended: %v", names(got))
}
// the user's own entry in Claude Code, by another name, runs the same
write(t, filepath.Join(h, ".claude.json"), `{"mcpServers": {"web": {"command": "npx", "args": ["-y", "tavily-mcp"]}}}`)
dropped = nil
ok(t)(ServerAgents("tavily", nil))
if len(dropped) != 1 {
t.Fatalf("dropped from opencode: %v", names(dropped))
}
if got := unused(dropped, Targets()); len(got) != 0 {
t.Errorf("Claude Code's own entry runs it, but it is ended: %v", names(got))
}
write(t, filepath.Join(h, ".claude.json"), `{}`)
if got := unused(dropped, Targets()); len(got) != 1 {
t.Errorf("no agent runs it, but it isn't ended: %v", names(got))
}
// taken out of the library while on an agent
ok(t)(ServerAgents("tavily", []string{"codex"}))
dropped = nil
ok(t)(RemoveServer("tavily"))
if got := unused(dropped, Targets()); len(got) != 1 {
t.Errorf("removed: %v, ended %v", names(dropped), names(got))
}
}
// launchedBy reads the processes a server's command line starts, from the
// lines of #3885's Windows machine and their macOS and Linux forms.
func TestLaunchedBy(t *testing.T) {
tavily := launchedBy(&Server{Command: "npx", Args: []string{"-y", "tavily-mcp"}})
for line, want := range map[string]bool{
`C:\WINDOWS\system32\cmd.exe /d /s /c "npx -y tavily-mcp"`: true,
`"C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npx-cli.js" -y tavily-mcp`: true,
`node /usr/local/lib/node_modules/npm/bin/npx-cli.js -y tavily-mcp`: true,
`npm exec tavily-mcp`: false, // what npx runs is under the npx it is ended with
`node C:\Users\u\AppData\Local\npm-cache\_npx\1\node_modules\tavily-mcp\build\index.js`: false,
`cmd.exe /d /s /c "npx -y tavily-mcp-two"`: false,
`bunx -y tavily-mcp`: false,
`npx -y other tavily-mcp`: false,
`tavily-mcp`: false,
} {
if got := tavily(line); got != want {
t.Errorf("%s: %v, want %v", line, got, want)
}
}
bin := launchedBy(&Server{Command: `C:\tools\firecrawl-mcp.cmd`})
if !bin(`C:\WINDOWS\system32\cmd.exe /d /s /c "C:\tools\firecrawl-mcp.cmd"`) || bin(`firecrawl-mcp-x`) {
t.Error("a server with no arguments is told by its program")
}
// an interpreter with nothing after it is anything it runs
for _, s := range []*Server{{Command: "node"}, {Command: "npx"}, {URL: "https://x", Command: "npx", Args: []string{"x"}}, {}} {
if launchKey(s) != "" || launchedBy(s)(`node index.js`) || launchedBy(s)(`node`) {
t.Errorf("%+v picks", s)
}
}
}