Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
plugin: a program a plugin starts with no folder runs in plugin-work, not beside magpie's settings
yetoned4f1d6480c17
diff --git a/docs/subsystems/provider-plugins.md b/docs/subsystems/provider-plugins.mdindex 11484bea7..93b755372 100644--- a/docs/subsystems/provider-plugins.md+++ b/docs/subsystems/provider-plugins.md@@ -190,7 +190,7 @@ For a plugin fix, publish the corrected community package and raise the owning mKeep 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.diff --git a/internal/plugin/host.js b/internal/plugin/host.jsindex f16968426..034490874 100644--- a/internal/plugin/host.js+++ b/internal/plugin/host.js@@ -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 oo = { ...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]diff --git a/internal/plugin/plugin_cwd_test.go b/internal/plugin/plugin_cwd_test.gonew file mode 100644index 000000000..5cd352e73--- /dev/null+++ b/internal/plugin/plugin_cwd_test.go@@ -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)+ }+}diff --git a/internal/plugin/testdata/clicwd/index.js b/internal/plugin/testdata/clicwd/index.jsnew file mode 100644index 000000000..6fa20fd2b--- /dev/null+++ b/internal/plugin/testdata/clicwd/index.js@@ -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" }),+ }+ },+ },+ ],+ },+})