Files
magpie/internal/plugin/testdata/clicwd/index.js
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

47 lines
1.6 KiB
JavaScript

// 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" }),
}
},
},
],
},
})