Files
magpie/internal/agent/preview_stash_keys_test.go
T
yetone 0d70f335ec agent: the disconnect preview of a plugin's agent shows what it puts back, not nothing (plugins#799)
Purfeather found that disconnecting MiniMax Design (an agent a plugin adds)
showed the dialog's generic sentence: /api/agents/.../disconnect-preview
returned changes: []. The preview runs Disconnect in a process whose HOME
(and APPDATA, LOCALAPPDATA, XDG_*) is a temporary copy, but a plugin agent's
stash key is "plugin:<id>:" + the file's whole path, so the copied
stash.json named the file under the real home. The dry run found no stash,
read the agent as not on magpie, and changed nothing.

The same holds for every built-in whose stash key carries its file's path
(Droid, AtomCode, Cline, Qwen, Kimi, Zed, Hermes, Muse, Snow, ...): their
previews found the file wired but not what to put back, and showed magpie's
lines taken out instead of the user's model going back.

The fix is in one place, DisconnectPreview: after copying magpie's files it
rewrites the copy's stash so each key naming a path under home names it
under the copy's home (moveStashKeys/movedKey; a separator must follow
home, and Windows compares without case, since %APPDATA% can spell the
home folder otherwise). The real stash, its key format and the real
connect/disconnect are unchanged, so stashes users already have keep
working and nothing is migrated. A stash that is missing or doesn't parse
is left as it is.

Run at this head (30a0bfe88 + this commit; origin/main had not moved):
- mote: go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows
  go vet ./...: clean. gofmt -l: nothing.
- mote: go test -tags nogui -timeout 45m ./...: 87 packages ok, exit 0
  (internal/agent 381s).
- xiaomi-gpu (Windows): go test -tags nogui -run
  'TestDisconnectPreview|TestMovedKey' ./internal/agent: ok, the four new
  tests PASS (the case-insensitive branch runs there only).
- mote GUI files that show the preview (agent-changes-before-now,
  agent-disconnect-preview, agent-leave-magpie, agent-own-direct,
  aside-native, mask-actions): Chromium 60/60, WebKit 34/34.

Broken by hand: with the key rewrite skipped, TestDisconnectPreviewOfAPluginAgent
fails with "the preview doesn't take magpie's entry out: []" (the
reporter's exact empty list), TestDisconnectPreviewOfAPluginAgentsModel
and TestDisconnectPreviewOfDroidPutsItsModelBack fail with no "~" line
putting the user's model back. Restored: all pass.

Not tried: a real MiniMax Design on Windows, or the GUI dialog against a
real magpie; the tests run the real preview path (this test binary as the
dry run's process) with the plugin's agent module as plugins#799 has it.
Paths stored in stash values (not keys) are not rewritten; none of the
values read in a disconnect hold a whole path.

Reported-by: Purfeather
2026-11-07 04:55:56 +08:00

152 lines
5.5 KiB
Go

package agent
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
// previewOf is the disconnect preview of a, run as the GUI runs it: this
// test binary as the dry run's process (TestMain).
func previewOf(t *testing.T, a *Agent) []FileChange {
t.Helper()
exe, err := os.Executable()
if err != nil {
t.Fatal(err)
}
tmp, err := filepath.EvalSymlinks(os.TempDir())
if err != nil {
t.Fatal(err)
}
t.Setenv("TMPDIR", tmp)
changes, err := DisconnectPreview(a, exe)
if err != nil {
t.Fatal(err)
}
return changes
}
// previewSays is whether a line of changes holds s, as put in or taken out.
func previewSays(changes []FileChange, op, s string) bool {
for _, c := range changes {
for _, l := range c.Lines {
if l.Op == op && (strings.Contains(l.Text, s) || strings.Contains(l.Was, s)) {
return true
}
}
}
return false
}
// mmdSrc is MiniMax Design's agent as plugins#799 has it: a connect-only
// agent whose file is under %APPDATA%, one key holding magpie's entry.
const mmdSrc = `
export const agent = { id: "minimax-design", name: "MiniMax Design", config: "$APPDATA/@hilo/desktop/hub-config.json", format: "json" };
export function connect({ gateway, models }) {
return { "config.customModels.user-custom-magpie": { protocol: "openai-compatible", providerName: "Magpie", baseUrl: gateway.v1, apiKey: gateway.key, models: (models || []).map(m => ({ id: m.id })) } };
}
`
// A plugin's agent keys its stash by its file's whole path, under home;
// the preview's dry run moves home, so the stash it copied must name the
// file there too, or the dry run finds nothing magpie wrote and the dialog
// shows no change (Purfeather, plugins#799).
func TestDisconnectPreviewOfAPluginAgent(t *testing.T) {
home := pluggedHome(t, mmdSrc)
t.Setenv("APPDATA", filepath.Join(home, "AppData", "Roaming"))
path := filepath.Join(home, "AppData", "Roaming", "@hilo", "desktop", "hub-config.json")
os.MkdirAll(filepath.Dir(path), 0o755)
mine := "{\n \"config\": {\n \"customModels\": {\n \"mine\": { \"protocol\": \"anthropic\" }\n },\n \"homeAgentModelId\": \"mine/x\"\n }\n}\n"
os.WriteFile(path, []byte(mine), 0o644)
a, err := Find("minimax-design")
if err != nil {
t.Fatal(err)
}
setUpInMagpie(a.ID)
if err := a.Field("provider").Set(magpieID); err != nil {
t.Fatal(err)
}
wired, _ := os.ReadFile(path)
stashBefore, _ := os.ReadFile(stashPath())
changes := previewOf(t, a)
if !previewSays(changes, "-", "user-custom-magpie") || !previewSays(changes, "-", "openai-compatible") {
t.Fatalf("the preview doesn't take magpie's entry out: %+v", changes)
}
if previewSays(changes, "-", `"mine"`) || previewSays(changes, "-", "homeAgentModelId") {
t.Fatalf("the preview takes the user's own out: %+v", changes)
}
// a preview writes nothing
if b, _ := os.ReadFile(path); string(b) != string(wired) {
t.Fatalf("the preview wrote the file:\n%s", b)
}
if b, _ := os.ReadFile(stashPath()); string(b) != string(stashBefore) {
t.Fatalf("the preview wrote the stash:\n%s", b)
}
// and the real disconnect does what it showed
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(path); strings.Contains(string(b), "user-custom-magpie") || !strings.Contains(string(b), "homeAgentModelId") {
t.Fatalf("disconnected:\n%s", b)
}
}
// One with a model field: the preview puts the user's own model back, as
// the stash has it, not only takes magpie's out.
func TestDisconnectPreviewOfAPluginAgentsModel(t *testing.T) {
path, _ := kiloWired(t)
a, err := Find("kilo")
if err != nil {
t.Fatal(err)
}
changes := previewOf(t, a)
if !previewSays(changes, "~", "anthropic/claude-sonnet-4-5") || !previewSays(changes, "-", "@ai-sdk/openai-compatible") {
t.Fatalf("the preview doesn't put the user's model back: %+v", changes)
}
if b, _ := os.ReadFile(path); string(b) == kiloMine {
t.Fatal("the preview disconnected the real file")
}
}
// A built-in keyed by its file's path is the same: Droid's stash holds the
// model the user started on, which its preview puts back.
func TestDisconnectPreviewOfDroidPutsItsModelBack(t *testing.T) {
_, path := droidHome(t)
os.WriteFile(path, []byte(droidUser), 0o644)
a := droid(os.Getenv("HOME"))
setUpInMagpie(a.ID)
if err := a.Apply("model", "magpie/anth/claude-sonnet-5"); err != nil {
t.Fatal(err)
}
changes := previewOf(t, a)
if !previewSays(changes, "~", `"model": "claude-opus-4-7"`) {
t.Fatalf("the preview doesn't put Droid's model back: %+v", changes)
}
}
// A key names a path under home the way the system does: Windows' without
// case, and only where a separator follows home.
func TestMovedKey(t *testing.T) {
home := filepath.Join(string(filepath.Separator)+"h", "me")
to := filepath.Join(string(filepath.Separator)+"t", "copy")
in := "plugin:x:" + filepath.Join(home, "a", "b.json")
if got := movedKey(in, home, to); got != "plugin:x:"+filepath.Join(to, "a", "b.json") {
t.Fatalf("moved %q", got)
}
if got := movedKey("droid:"+filepath.Join(home, ".factory", "settings.json")+":model", home, to); got != "droid:"+filepath.Join(to, ".factory", "settings.json")+":model" {
t.Fatalf("moved %q", got)
}
for _, k := range []string{"codex.model", "plugin:x:" + home + "x" + string(filepath.Separator) + "f", "plugin:x:" + filepath.Join(string(filepath.Separator)+"else", "f")} {
if got := movedKey(k, home, to); got != k {
t.Fatalf("%q moved to %q", k, got)
}
}
if runtime.GOOS == "windows" {
if got := movedKey(`plugin:x:C:\users\ME\AppData\f`, `C:\Users\me`, `C:\t`); got != `plugin:x:C:\t\AppData\f` {
t.Fatalf("moved %q", got)
}
}
}