Files
magpie/internal/cli/plugin_agent_gone_cli_test.go
yetone 3ec3cb704b plugin: turning an agent plugin off or removing it puts the agent's config back first (#3810)
An agent a plugin adds is known only while the plugin is on. Turning
the plugin off or removing it (CLI `magpie plugin off/rm`, or the
Plugins page) left the agent's config on magpie, and `magpie <agent>
default` then said `unknown agent`, with nothing to put it back.

- provider.SetPluginOff (off) and provider.RemovePlugin first call
  provider.PluginAgentGoing, set by internal/agent to UnwirePlugin: the
  agent is disconnected as `magpie <agent> default` does (Disconnect),
  while the plugin still loads. A plugin off already is read from its
  module as it is; one whose module is gone puts back each file whose
  stash names its spec. If that fails, the plugin is left as it was
  and the error says why.
- The stash now records the plugin's spec and the file's format, so
  `magpie <id> default` for the agent of a plugin deleted on disk puts
  the file back from the stash alone (agent.RestoreGone; an older
  stash's format comes from its file's name, agentplug.FormatOf). A
  file that no longer holds magpie's gateway key is the user's and is
  left alone; one that can't be read is unknown: nothing is written,
  the stash is kept. With the plugin only switched off, `default` says
  the agent is off magpie already instead of "unknown agent".
- Turning the plugin back on (or installing it again) does not rewire
  the agent by itself: its config stays as the user had it. Connecting
  it (its switch on the Agents page) puts back the model it was on,
  through Disconnect's `.reconnect`. The CLI says so when it turns off
  or removes a plugin whose agent was on magpie.

Run (this change on origin/main f3c4d7b7a, then rebased onto
0fb0bfda3, which changes only an internal/gateway test; provider and
cli run again at the pushed head):
- gofmt -l: none; go vet -tags nogui on darwin, linux, windows for
  agent, agentplug, cli, provider, gui, plugin: ok.
- go test -tags nogui -timeout 45m -count=1 ./internal/agent
  ./internal/agentplug ./internal/cli ./internal/provider
  ./internal/plugin ./internal/gui: agent, agentplug, cli, plugin, gui
  ok; provider crashes in TestUnreadLoginsWriteErrorsKeepCredentials
  ("close of synctest channel from outside bubble", filememo.Forget),
  the same on origin/main 0fb0bfda3 run alone: #3632, open, not this
  change. At the pushed head: cli ok; provider with -skip
  TestUnreadLogins ok.
- Real use, sandbox HOME, magpie built from this tree, gateway on
  127.0.0.1:3491, web on 3492: `magpie plugin add agent-kilo`
  (@magpie-community/agent-kilo 0.1.0, not on npm, from the plugins
  repo's dist branch), a kilo.jsonc with comments and a provider of its
  own, `magpie kilo magpie/deepseek/deepseek-chat` (wired, provider
  block added). Then, each time from wired: `magpie plugin off` -> cmp
  byte-identical; `plugin on` -> still the user's; `plugin rm` ->
  byte-identical; POST /api/plugins/off and /api/plugins/remove (the
  Plugins page's calls) -> byte-identical; plugin dir deleted and
  plugins.json emptied by hand, then `magpie kilo default` -> put back,
  byte-identical. `magpie kilo default` with the plugin off says it is
  off magpie already.

Broken by hand, each red, then restored:
- PluginAgentGoing left nil: the four off/remove tests fail.
- the SetPluginOff call removed: Off and ModuleGone tests fail; the
  RemovePlugin call removed: Remove and RemovedWhileOff fail.
- Spec not stashed: ModuleGone fails. pluggedOff disabled:
  RemovedWhileOff fails. FormatOf fallback removed: TestRestoreGoneAgent
  fails. The gateway-key check removed: LeavesAFileOffMagpie fails.
- Unwire instead of Disconnect: the Off test fails (How "first", not
  "again"). The off-plugin check in RestoreGone skipped: the Off test
  fails. RestoreGone not called from the CLI: TestGoneAgentDefault
  fails with `unknown agent "kilo"`.

Not tried: the Windows box; a real Kilo CLI reading the restored file;
the GUI suite (no Plugins page code changed: the page's calls reach the
same provider functions, tried above through the web API). Not changed:
an inline JSON object magpie wrote into comes back reformatted (same
values) on any plugin agent's Disconnect, `default` included: #3814.
2026-10-27 10:01:46 +08:00

92 lines
3.1 KiB
Go

package cli
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/agentplug"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/settings"
)
// goneAgentSrc is an agent plugin as agent-kilo is: a JSONC file, the
// model under "model", a provider block of its own.
const goneAgentSrc = `
export const agent = { id: "kilo", name: "Kilo", config: "~/.config/kilo/kilo.jsonc", model: "model", prefix: "magpie/" };
export function connect({ gateway }) {
return { "provider.magpie": { npm: "@ai-sdk/openai-compatible", options: { baseURL: gateway.v1, apiKey: gateway.key } } };
}
`
const goneAgentMine = "{\n // mine\n \"model\": \"anthropic/claude-sonnet-4-5\", // mine\n \"provider\": {\n \"anthropic\": {}\n }\n}\n"
// goneAgentHome has the kilo plugin, its kilo.jsonc as the user wrote it,
// and kilo on magpie's a/m through `magpie kilo a/m`; it returns the file
// and the plugin's folder.
func goneAgentHome(t *testing.T) (string, string) {
t.Helper()
groupsHome(t)
home := os.Getenv("HOME")
dir := filepath.Join(home, "plugins", "kilo")
os.MkdirAll(dir, 0o755)
os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"name":"agent-kilo","magpie":{"agent":"./agent.js"}}`), 0o644)
os.WriteFile(filepath.Join(dir, "agent.js"), []byte(goneAgentSrc), 0o644)
b, _ := json.Marshal(plugin.List{Plugins: []plugin.Entry{{Spec: dir}}})
os.MkdirAll(settings.Dir(), 0o755)
if err := os.WriteFile(filepath.Join(settings.Dir(), "plugins.json"), b, 0o644); err != nil {
t.Fatal(err)
}
agentplug.Reload()
t.Cleanup(agentplug.Reload)
path := filepath.Join(home, ".config", "kilo", "kilo.jsonc")
os.MkdirAll(filepath.Dir(path), 0o755)
os.WriteFile(path, []byte(goneAgentMine), 0o644)
if err := run([]string{"kilo", "a/m"}); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(path); string(b) == goneAgentMine {
t.Fatal("not wired")
}
return path, dir
}
func fileIs(t *testing.T, path, want string) {
t.Helper()
if b, _ := os.ReadFile(path); string(b) != want {
t.Fatalf("%s isn't as the user had it:\n%s", path, b)
}
}
// `magpie plugin off` and `magpie plugin rm` put the agent's config back
// byte for byte before the plugin goes (#3810).
func TestPluginOffAndRemovePutTheAgentBack(t *testing.T) {
for _, op := range []string{"off", "rm"} {
t.Run(op, func(t *testing.T) {
path, dir := goneAgentHome(t)
if err := run([]string{"plugin", op, dir}); err != nil {
t.Fatal(err)
}
fileIs(t, path, goneAgentMine)
})
}
}
// With the plugin deleted on disk, `magpie kilo default` puts kilo.jsonc
// back from what magpie stashed, where it said "unknown agent".
func TestGoneAgentDefault(t *testing.T) {
path, dir := goneAgentHome(t)
os.RemoveAll(dir)
os.WriteFile(filepath.Join(settings.Dir(), "plugins.json"), []byte(`{"plugins":[]}`), 0o644)
agentplug.Reload()
if err := run([]string{"kilo", "default"}); err != nil {
t.Fatal(err)
}
fileIs(t, path, goneAgentMine)
// nothing left to put back: it is an unknown agent again
if err := run([]string{"kilo", "default"}); err == nil {
t.Fatal("kilo is known with nothing of it left")
}
}