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.
This commit is contained in:
yetone
2026-10-27 10:01:46 +08:00
parent 205e3b8ae7
commit 3ec3cb704b
9 changed files with 652 additions and 37 deletions
+28
View File
@@ -11,6 +11,7 @@ import (
"strings"
"time"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/middleware"
"github.com/yetone/magpie/internal/plugin"
@@ -72,10 +73,12 @@ func pluginCmd(args []string) error {
}
rest[0] = installedName(rest[0])
moved := provider.MovedOnto(rest[0])
wired := wiredPluginAgent(rest[0])
if err := provider.RemovePlugin(ctx, rest[0]); err != nil {
return err
}
fmt.Println(green.Render("✓"), "removed", rest[0])
sayAgentBack(wired)
for _, id := range moved {
fmt.Println(faint.Render(" " + id + " is off until the plugin is added again; its accounts are kept"))
}
@@ -92,10 +95,15 @@ func pluginCmd(args []string) error {
return errors.New(pluginUsage)
}
rest[0] = installedName(rest[0])
var wired *agent.Agent
if sub == "off" {
wired = wiredPluginAgent(rest[0])
}
if err := provider.SetPluginOff(ctx, rest[0], sub == "off"); err != nil {
return err
}
fmt.Println(green.Render("✓"), rest[0], "is", sub)
sayAgentBack(wired)
if sub == "off" {
for _, id := range provider.MovedOnto(rest[0]) {
fmt.Println(faint.Render(" " + id + " is off with it; its accounts are kept"))
@@ -673,3 +681,23 @@ func readsText(reads []string) string {
}
return "reads " + strings.Join(reads, ", ")
}
// wiredPluginAgent is the agent the plugin named adds, while magpie is in
// its config: turning the plugin off or removing it takes magpie out
// first (provider.PluginAgentGoing, #3810).
func wiredPluginAgent(name string) *agent.Agent {
for _, a := range agent.All() {
if a.Plugin != "" && (a.Plugin == name || plugin.Name(a.Plugin) == name) && a.Wired() {
return a
}
}
return nil
}
// sayAgentBack says that the agent a plugin added was taken off magpie.
func sayAgentBack(a *agent.Agent) {
if a == nil {
return
}
fmt.Println(faint.Render(" " + a.Name + " is disconnected from magpie, back to what it had before; with the plugin back, connecting it (its switch on the Agents page) puts back the model it was on"))
}