cli: the image MCP, Claude's MCP helper and statusline skip the start-up moves (#3318)

Every magpie subcommand ran the start-up moves before its own work:
RenameLegacy and the effort moves list every provider and account,
which starts the bun plugin host and runs claude auth status, a full
claude -p /usage, cursor-agent about and devin auth status. Claude Code
starts `magpie mcp image` for each session and `magpie statusline` on
every message, so a machine with 16 Claude sessions held 16 plugin
hosts beside the GUI's (~1.3GB footprint), and each status line took
5s. These three talk to the running gateway and nothing else; they are
now dispatched before the moves, which become startUp so a test can
tell whether they ran.

Run at 7b1faf99f + this change (macOS, real HOME, read-only commands):
- old (origin/main build) `mcp image`: 4 child processes after 2s,
  footprint 57MB; new: 0 children, 17MB.
- `statusline` against the live gateway: 5.24s old, 0.13s new, same
  line printed.
- initialize + tools/list over stdio: same replies from both builds.
- TestSessionCommandsSkipStartUp: ok; red with startUp() moved back
  above the dispatch by hand ("[mcp image] ran the start-up moves").
- go test -tags nogui ./internal/cli ./internal/imagemcp
  ./internal/claudebridge: ok. gofmt clean; go vet GOOS=linux and
  GOOS=windows -tags nogui, and darwin with the GUI: clean.
Not run: the full suite. Not tried on Linux or Windows.
This commit is contained in:
yetone
2026-10-25 02:13:44 +08:00
parent 7b1faf99f2
commit 40a0b442c0
2 changed files with 84 additions and 25 deletions
+41 -25
View File
@@ -221,6 +221,32 @@ var tuiRun = tui.Run
// Windows, half a second for each one another program holds open).
func syncAgentsOnChange() { catalog.Changed = agent.SyncCatalogSoon }
// startUp brings the files magpie and its agents keep up to what this
// magpie reads, before a command that may read them; a var so a test can
// tell it ran.
var startUp = func() {
// started by an update, the magpie it replaces goes first: the moves
// below write the files it may still be writing
update.AwaitPredecessor()
makeDirs()
settings.Migrate()
// every built-in plugin starts on (plugins.yml), on a new install too
if _, err := pluginrt.Settle(); err != nil {
fmt.Fprintf(os.Stderr, "magpie: plugins.yml: %v\n", err)
}
if _, err := pluginrt.Refresh(); err != nil {
fmt.Fprintf(os.Stderr, "magpie: plugins.yml: %v\n", err)
}
// the providers and settings read once for every agent's fields, which
// the moves below look at (a write among them reads them again)
release := provider.Hold()
agent.RenameLegacy()
agent.MoveCursorEfforts()
agent.MoveAntigravityEfforts()
agent.MoveOffAccountIDs()
release()
}
func run(args []string) error {
// a command that saved a provider or a group ends with the agents'
// files written, which its save no longer waits for; and a test's run
@@ -260,26 +286,22 @@ func run(args []string) error {
if len(args) > 1 && (args[0] == "accounts" || args[0] == "account") && args[1] == "discovery" {
return accountsDiscoveryCmd(args[2:])
}
// started by an update, the magpie it replaces goes first: the moves
// below write the files it may still be writing
update.AwaitPredecessor()
makeDirs()
settings.Migrate()
// every built-in plugin starts on (plugins.yml), on a new install too
if _, err := pluginrt.Settle(); err != nil {
fmt.Fprintf(os.Stderr, "magpie: plugins.yml: %v\n", err)
// what an agent starts for each session or message talks to the
// running gateway and nothing else: the moves below list every
// provider and account, which starts the plugin host and asks claude,
// cursor-agent and devin who is signed in — five processes and a
// Claude /usage for each Claude Code session's image MCP (#3318)
if len(args) > 0 {
switch args[0] {
case "mcp":
return imagemcp.Run(args[1:])
case "claude-mcp-helper": // internal: stdio MCP subprocess spawned by Claude Code
return claudebridge.RunMCP(args[1:])
case "statusline":
return statuslineCmd(args)
}
}
if _, err := pluginrt.Refresh(); err != nil {
fmt.Fprintf(os.Stderr, "magpie: plugins.yml: %v\n", err)
}
// the providers and settings read once for every agent's fields, which
// the moves below look at (a write among them reads them again)
release := provider.Hold()
agent.RenameLegacy()
agent.MoveCursorEfforts()
agent.MoveAntigravityEfforts()
agent.MoveOffAccountIDs()
release()
startUp()
// a provider added, edited or removed, or a list fetched anew, reaches
// the model lists agents keep in files of their own, behind the save
// that changed it (agent.SyncCatalogSoon)
@@ -419,8 +441,6 @@ func run(args []string) error {
return sessionsCmd(args)
case "quota", "quotas":
return quotaCmd(args)
case "statusline":
return statuslineCmd(args)
case "update":
return updateCmd(args)
case "library", "lib":
@@ -437,12 +457,8 @@ func run(args []string) error {
return s3Cmd(args[1:])
case "github":
return githubCmd(args[1:])
case "mcp":
return imagemcp.Run(args[1:])
case "claude-code":
return claudeCodeCmd(args[1:])
case "claude-mcp-helper": // internal: stdio MCP subprocess spawned by Claude Code
return claudebridge.RunMCP(args[1:])
}
a, err := agent.Find(args[0])
+43
View File
@@ -0,0 +1,43 @@
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)
}
}
// and a command that reads the files still has them brought up first
_ = run([]string{"version"})
if ran != 1 {
t.Fatalf("version: start-up ran %d times, want 1", ran)
}
}