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.
This commit is contained in:
@@ -190,7 +190,7 @@ For a plugin fix, publish the corrected community package and raise the owning m
|
||||
|
||||
Keep the migration, host, and upstream behavior separate when investigating failures. A plugin bug can require a community change; a host, provider-card, sign-in orchestration, or migration bug can require a magpie change even for moved users.
|
||||
|
||||
The host gets magpie's proxy only as `MAGPIE_*_PROXY` and applies it to each fetch itself (`proxyFor`), and to each HTTP/2 session a plugin opens (`http2.connect`, `tunnel`, #1035). A host on the user's own network goes around that proxy as it does for magpie's own requests (`onLAN` in `host.js`, a port of `internal/netproxy/lan.go`'s: a private, link-local, loopback or CGNAT address, a `.local` name, or a name every address of which is one of those, looked up for 400 ms at most and kept a minute; #1572). A provider's own proxy is still taken as it is. The two rules are kept the same: a change to one is made in the other. A program a plugin starts, such as the Grok plugin's `grok login`, gets it back as `HTTPS_PROXY`/`HTTP_PROXY` from the spawn wrapper in `host.js`, unless the plugin set one. A sign-in that works in the built-in and fails through the plugin may be a host difference like this one, not a plugin bug.
|
||||
The host gets magpie's proxy only as `MAGPIE_*_PROXY` and applies it to each fetch itself (`proxyFor`), and to each HTTP/2 session a plugin opens (`http2.connect`, `tunnel`, #1035). A host on the user's own network goes around that proxy as it does for magpie's own requests (`onLAN` in `host.js`, a port of `internal/netproxy/lan.go`'s: a private, link-local, loopback or CGNAT address, a `.local` name, or a name every address of which is one of those, looked up for 400 ms at most and kept a minute; #1572). A provider's own proxy is still taken as it is. The two rules are kept the same: a change to one is made in the other. A program a plugin starts, such as the Grok plugin's `grok login`, gets it back as `HTTPS_PROXY`/`HTTP_PROXY` from the spawn wrapper in `host.js`, unless the plugin set one. A sign-in that works in the built-in and fails through the plugin may be a host difference like this one, not a plugin bug. The same wrapper starts a program that names no folder of its own in `plugin-work` under magpie's config folder (with `PWD` to match), not in the host's own folder beside `settings.json`: the chat page plugin starts the user's MCP servers this way, and a tool's file saved by a relative name, or one named like magpie's own files, landed among magpie's (`TestPluginProgramsRunInTheirOwnFolder`). A folder the plugin gives is kept.
|
||||
|
||||
magpie downloads the Bun build for this machine (`bunTarget` in [`bun.go`](../../internal/plugin/bun.go)): the baseline build on x64 Linux and Windows, the native `bun-windows-aarch64` on Windows on ARM (it was turned away as having no build until #2183), and on a musl Linux such as Alpine (`libc.Musl`, the check Claude Code's download uses too) the musl one, `bun-linux-x64-musl-baseline` or `bun-linux-aarch64-musl`. A glibc build names glibc's loader, which Alpine lacks, so it couldn't start there and exec said "no such file or directory" (#1877). A musl Linux keeps its Buns and their `state.json` under `bun/musl`, so a machine whose C library changed never runs the other one's build or takes its set-aside versions; everywhere else they stay in `bun`, where a Bun downloaded before is. A bun that is on disk but can't be started says it is a build for another C library and names `MAGPIE_BUN` (`cantStart`), and it isn't fallen back from or set aside: the version before is the same build. Bun's musl build needs libstdc++ and libgcc; on an Alpine without them, `bun add` and the host's start say which library is missing and `apk add libstdc++ libgcc` (`missingLibs`). `TestBunTargetForMusl`, `TestMuslBunKeptApartFromGlibc`, `TestCantStartSaysCLibrary`, `TestHostOnUnstartableBunSaysCLibrary`, `TestMissingLibsNamesThem` and `TestHostOnBunLackingLibsSaysWhich` in [`bunmusl_test.go`](../../internal/plugin/bunmusl_test.go) cover these.
|
||||
|
||||
|
||||
@@ -256,9 +256,39 @@ function proxied(env) {
|
||||
}
|
||||
return env
|
||||
}
|
||||
// And a program started with no folder of its own runs in workDir, not
|
||||
// the host's: the host runs in magpie's config folder, beside
|
||||
// settings.json and providers.json, so whatever a program saved by a
|
||||
// relative name went there, and a tool told to write "providers.json"
|
||||
// would write over magpie's. The chat page plugin (magpie-chat) starts
|
||||
// the user's MCP servers this way and runs their tools for a model,
|
||||
// whose fetched pages and saved files (jiakun_zhao on 𝕏 asked whose
|
||||
// page_<hash>.html and magpie_*.md files they had found) belong in a
|
||||
// folder of their own. A folder the plugin gives is kept.
|
||||
const workDir = path.join(process.cwd(), "plugin-work")
|
||||
let workReady = false
|
||||
function workCwd() {
|
||||
if (!workReady) {
|
||||
try {
|
||||
fs.mkdirSync(workDir, { recursive: true })
|
||||
workReady = true
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
return workDir
|
||||
}
|
||||
const guard = (o) => {
|
||||
if (o != null && typeof o !== "object") return o
|
||||
o = { ...o }
|
||||
if (o.cwd == null || o.cwd === "") {
|
||||
const d = workCwd()
|
||||
if (d) {
|
||||
o.cwd = d
|
||||
// and PWD with it, which a shell script reads for its folder
|
||||
if (process.platform !== "win32") o.env = { ...(o.env ?? process.env), PWD: d }
|
||||
}
|
||||
}
|
||||
o.env = proxied(o.env)
|
||||
if (Array.isArray(o.stdio)) {
|
||||
o.stdio = [...o.stdio]
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
// A plugin that starts programs as the chat page plugin starts the user's
|
||||
// MCP servers: with no folder of its own (node:child_process spawn and
|
||||
// execSync, Bun.spawn), and once with a folder it chose. The link it hands
|
||||
// on says the folder each one ran in, and the PWD it was given.
|
||||
import { execSync, spawn } from "node:child_process"
|
||||
import os from "node:os"
|
||||
|
||||
const script = "process.stdout.write(process.cwd() + '\\n' + (process.env.PWD || ''))"
|
||||
|
||||
function where(opts) {
|
||||
return new Promise((resolve) => {
|
||||
const child = spawn(process.execPath, ["-e", script], { stdio: ["ignore", "pipe", "pipe"], ...opts })
|
||||
let out = ""
|
||||
child.stdout.on("data", (b) => (out += b))
|
||||
child.on("close", () => resolve(out))
|
||||
})
|
||||
}
|
||||
|
||||
export const CLICwdPlugin = async () => ({
|
||||
auth: {
|
||||
provider: "clicwd",
|
||||
methods: [
|
||||
{
|
||||
type: "oauth",
|
||||
label: "CLI",
|
||||
authorize: async () => {
|
||||
const bun = Bun.spawn([process.execPath, "-e", script], { stdout: "pipe", stderr: "pipe" })
|
||||
const q = new URLSearchParams({
|
||||
spawned: await where({}),
|
||||
withEnv: await where({ env: { ...process.env } }),
|
||||
sync: execSync(`"${process.execPath}" -e "${script.replace(/"/g, '\\"')}"`, { stdio: ["ignore", "pipe", "pipe"] }).toString(),
|
||||
bun: await new Response(bun.stdout).text(),
|
||||
own: await where({ cwd: os.tmpdir() }),
|
||||
ownTarget: os.tmpdir(),
|
||||
})
|
||||
return {
|
||||
url: `https://clicwd.invalid/?${q}`,
|
||||
instructions: "",
|
||||
method: "code",
|
||||
callback: async () => ({ type: "failed" }),
|
||||
}
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user