Files
magpie/internal/plugin/plugin_cwd_test.go
T
yetone d4f1d6480c plugin: a program a plugin starts with no folder runs in plugin-work, not beside magpie's settings
jiakun_zhao on X asked whose page_<hash>.html, magpie_home.html,
magpie_install.sh and magpie_*.md files they had found after using the
chat page plugin (Magiczerowxy/magpie-chat). That plugin starts the
user's MCP servers from the plugin host with node:child_process spawn
and no cwd, and runs their tools for a model. The host runs in magpie's
config folder (~/.config/magpie), so every file such a tool saved by a
relative name went beside settings.json and providers.json, and a tool
told to write "providers.json" would have written over magpie's.

host.js's spawn wrapper (which already hands on the proxy and keeps
programs off the host's stdio) now starts a program that names no
folder of its own in <config>/plugin-work, created on first use, with
PWD to match outside Windows. A folder the plugin gives is kept.

Not the cause of files in the user's home folder: magpie's host never
runs in ~, and no magpie code names a file page_<hash>.html or calls a
user's MCP tool itself. Files in ~ come from a process whose folder was
~: the chat's server started by hand (node serve.mjs) from ~, or
another agent run there. Filed on magpie-chat for the plugin to give
its MCP servers a workspace of its own and tell the model where it is.

Run (mote, macOS):
- TestPluginProgramsRunInTheirOwnFolder (new; testdata/clicwd checks
  child_process spawn, spawn with env, execSync and Bun.spawn, plus an
  own cwd kept): ok. Red with origin/main's host.js put back by hand
  (plugin-work not made; programs ran in the config folder).
- go test ./internal/plugin ./internal/agentplug ./internal/pluginrt
  -count=1: ok (plugin 261s).
- go test ./internal/gateway -run Plugin -count=1 at 581d64b7d: ok.
- Windows (xiaomi-gpu, win-run): TestPluginProgramsRunInTheirOwnFolder,
  TestPluginCLIHasTheProxy, TestFakePlugin: ok.
- gofmt -l: clean.
Not tried: the full go test ./... suite, the real magpie-chat plugin
with a real MCP server and model, Linux.
2026-10-28 22:20:56 +08:00

63 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package plugin
import (
"context"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/settings"
)
// jiakun_zhao on 𝕏 found page_<hash>.html and magpie_*.md files after
// using the chat page plugin, which starts the user's MCP servers from
// the plugin host and runs their tools for a model. A program a plugin
// starts with no folder of its own ran in the host's, magpie's config
// folder, so a tool's file saved by a relative name went beside
// settings.json, and one named providers.json would write over magpie's.
// It runs in plugin-work under that folder, with PWD to match; a folder
// the plugin gives is kept.
func TestPluginProgramsRunInTheirOwnFolder(t *testing.T) {
sandbox(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/clicwd/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
a, err := Authorize(ctx, "clicwd", 0, nil, NewAccount)
if err != nil {
t.Fatal(err)
}
u, err := url.Parse(a.URL)
if err != nil {
t.Fatal(err)
}
q := u.Query()
want := filepath.Join(settings.Dir(), "plugin-work")
if st, err := os.Stat(want); err != nil || !st.IsDir() {
t.Fatalf("%s is not a folder: %v", want, err)
}
same := func(a, b string) bool {
ea, _ := filepath.EvalSymlinks(a)
eb, _ := filepath.EvalSymlinks(b)
return ea != "" && strings.EqualFold(ea, eb)
}
for _, k := range []string{"spawned", "withEnv", "sync", "bun"} {
cwd, pwd, _ := strings.Cut(q.Get(k), "\n")
if !same(cwd, want) {
t.Errorf("%s: a program the plugin started with no folder ran in %q, want %q", k, cwd, want)
}
if runtime.GOOS != "windows" && !same(pwd, want) {
t.Errorf("%s: its PWD is %q, want %q", k, pwd, want)
}
}
if cwd, _, _ := strings.Cut(q.Get("own"), "\n"); !same(cwd, q.Get("ownTarget")) {
t.Errorf("a program started in %q ran in %q", q.Get("ownTarget"), cwd)
}
}