From bc00ee1df2d12176cfa8926b73f3fe1fba6f2d38 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 5 Oct 2026 22:25:30 +0800 Subject: [PATCH] =?UTF-8?q?plugin,=20provider:=20a=20program=20a=20plugin?= =?UTF-8?q?=20starts=20has=20magpie's=20proxy,=20and=20a=20CLI=20login=20w?= =?UTF-8?q?ith=20no=20link=20says=20why=20(=F0=9D=95=8F=20on=20Discord)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Grok plugin's sign-in said only "grok login gave no link to open". The plugin host has magpie's proxy only as MAGPIE_*_PROXY, so the `grok login` the plugin runs went out with none, where the built-in's had it (netproxy.Env); where x.ai is reached only through a proxy, it failed. host.js's spawn wrapper now gives a started program HTTPS_PROXY and HTTP_PROXY (the account's own proxy, none for direct, else magpie's) when the plugin set none. grok 1.0.46, unable to reach x.ai, prints "Error: error sending request for url (https://auth.x.ai/oauth2/device/code): …", whose URL the built-in's runCLISignIn took for the page to open; a line saying error gives no link, and a login with none ends with the CLI's last line. The plugin does the same from 0.1.8, which the grok mover now asks for. plugin_cliproxy_test.go and TestCLISignInSaysWhyNoLink fail before this. Checked with grok 1.0.46 itself through the plugin host and the published plugin's code, given a proxy that refuses: the CLI goes through it and the error names what it said. --- docs/subsystems/provider-plugins.md | 2 ++ internal/plugin/host.js | 24 ++++++++++++- internal/plugin/plugin_cliproxy_test.go | 40 ++++++++++++++++++++++ internal/plugin/testdata/cliproxy/index.js | 38 ++++++++++++++++++++ internal/provider/cursor_signin_test.go | 21 ++++++++++++ internal/provider/grok.go | 26 ++++++++++++-- internal/provider/migrate_side.go | 2 +- 7 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 internal/plugin/plugin_cliproxy_test.go create mode 100644 internal/plugin/testdata/cliproxy/index.js diff --git a/docs/subsystems/provider-plugins.md b/docs/subsystems/provider-plugins.md index 0fd68748f..379e14501 100644 --- a/docs/subsystems/provider-plugins.md +++ b/docs/subsystems/provider-plugins.md @@ -59,6 +59,8 @@ 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. 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 GUI's plugin integration includes `subOf`, `pluginSubs`, and `startPluginSignIn`. A change to sign-in or provider presentation must follow these paths as well as the built-in paths it affects. ## Verification diff --git a/internal/plugin/host.js b/internal/plugin/host.js index a413a9549..efdd6d26d 100644 --- a/internal/plugin/host.js +++ b/internal/plugin/host.js @@ -218,10 +218,32 @@ process.stdout.write = (chunk, enc, cb) => process.stderr.write(chunk, enc, cb) // "Login canceled"), and its stdout is the host's answers. It reads // nothing instead and writes to stderr. node:child_process starts every // program through Bun.spawn and Bun.spawnSync, so these see all of them. +// +// And it has the proxy the plugin's own fetches take, as a built-in's CLI +// had magpie's (netproxy.Env): the host has its *_PROXY only as +// MAGPIE_*_PROXY (see below), so `grok login`, which the Grok plugin runs, +// went out with none and, where x.ai is reached only through one, printed +// no link to open (𝕏 on Discord). One the plugin set itself is kept. const own = (v, fd) => v === "inherit" || v === fd || v === (fd ? process.stdout : process.stdin) +const PROXY_VARS = ["HTTPS_PROXY", "HTTP_PROXY", "ALL_PROXY"] +function proxied(env) { + env = { ...(env ?? process.env) } + if (Object.keys(env).some((k) => PROXY_VARS.includes(k.toUpperCase()) && env[k])) return env + const mine = via.getStore() + if (mine === "direct") return env + const set = mine ? { HTTPS_PROXY: mine, HTTP_PROXY: mine } : { HTTPS_PROXY: globalProxy.https, HTTP_PROXY: globalProxy.http, NO_PROXY: proxyVar("NO_PROXY") } + for (const [k, v] of Object.entries(set)) { + if (!v || Object.keys(env).some((e) => e.toUpperCase() === k)) continue + env[k] = v + // Windows' names are one whatever their case + if (process.platform !== "win32") env[k.toLowerCase()] = v + } + return env +} const guard = (o) => { - if (!o || typeof o !== "object") return o + if (o != null && typeof o !== "object") return o o = { ...o } + o.env = proxied(o.env) if (Array.isArray(o.stdio)) { o.stdio = [...o.stdio] if (own(o.stdio[0], 0)) o.stdio[0] = "ignore" diff --git a/internal/plugin/plugin_cliproxy_test.go b/internal/plugin/plugin_cliproxy_test.go new file mode 100644 index 000000000..6d55cbec5 --- /dev/null +++ b/internal/plugin/plugin_cliproxy_test.go @@ -0,0 +1,40 @@ +package plugin + +import ( + "context" + "net/url" + "path/filepath" + "testing" + "time" +) + +// 𝕏 on Discord: the Grok plugin's sign-in said "grok login gave no link +// to open". The plugin runs `grok login`, and the host has magpie's proxy +// only as MAGPIE_*_PROXY, so the CLI went out with none where x.ai is +// reached only through one. A program a plugin starts has it, as the +// built-in's `grok login` had it. +func TestPluginCLIHasTheProxy(t *testing.T) { + sandbox(t) + const proxy = "http://127.0.0.1:7890" + t.Setenv("HTTPS_PROXY", proxy) + t.Setenv("HTTP_PROXY", proxy) + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + abs, _ := filepath.Abs("testdata/cliproxy/index.js") + if _, err := Add(ctx, abs); err != nil { + t.Fatal(err) + } + a, err := Authorize(ctx, "cliproxy", 0, nil, NewAccount) + if err != nil { + t.Fatal(err) + } + u, err := url.Parse(a.URL) + if err != nil { + t.Fatal(err) + } + for _, k := range []string{"inherited", "copied"} { + if got := u.Query().Get(k); got != proxy { + t.Errorf("a CLI started with the %s environment has HTTPS_PROXY %q, want %q", k, got, proxy) + } + } +} diff --git a/internal/plugin/testdata/cliproxy/index.js b/internal/plugin/testdata/cliproxy/index.js new file mode 100644 index 000000000..029e988d5 --- /dev/null +++ b/internal/plugin/testdata/cliproxy/index.js @@ -0,0 +1,38 @@ +// A plugin that signs in with a CLI of the vendor's, as the Grok plugin +// runs `grok login`: the link it hands on says the proxy the CLI was +// given, started with the host's environment and with a copy of it. +import { spawn } from "node:child_process" + +function proxyOf(env) { + return new Promise((resolve) => { + const child = spawn(process.execPath, ["-e", "process.stdout.write(process.env.HTTPS_PROXY || 'none')"], { + env, + stdio: ["ignore", "pipe", "pipe"], + }) + let out = "" + child.stdout.on("data", (b) => (out += b)) + child.on("close", () => resolve(out)) + }) +} + +export const CLIProxyPlugin = async () => ({ + auth: { + provider: "cliproxy", + methods: [ + { + type: "oauth", + label: "CLI", + authorize: async () => { + const inherited = await proxyOf(undefined) + const copied = await proxyOf({ ...process.env }) + return { + url: `https://cliproxy.invalid/?inherited=${encodeURIComponent(inherited)}&copied=${encodeURIComponent(copied)}`, + instructions: "", + method: "code", + callback: async () => ({ type: "failed" }), + } + }, + }, + ], + }, +}) diff --git a/internal/provider/cursor_signin_test.go b/internal/provider/cursor_signin_test.go index b55ec8f3a..aacdb96e2 100644 --- a/internal/provider/cursor_signin_test.go +++ b/internal/provider/cursor_signin_test.go @@ -82,3 +82,24 @@ func TestCLISignInFirstLinkAsPrinted(t *testing.T) { t.Errorf("link %q", got) } } + +// 𝕏 on Discord: "grok login gave no link to open", without what the CLI +// said. A login that fails says its last line, and the endpoint its error +// names isn't taken for the page to open. +func TestCLISignInSaysWhyNoLink(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("fake CLI is a shell script") + } + said := "Error: error sending request for url (https://auth.x.ai/oauth2/device/code): client error (Connect): tunnel error: failed to create underlying connection: tcp connect error: Connection refused (os error 61)" + exe := filepath.Join(t.TempDir(), "login") + if err := os.WriteFile(exe, []byte("#!/bin/sh\necho '"+said+"' >&2\nexit 1\n"), 0o755); err != nil { + t.Fatal(err) + } + s := &signInFlow{done: make(chan struct{})} + s.st = SignInState{State: "waiting"} + err := runCLISignIn(s, "grok login", nil, true, nil, func() (string, string, bool) { return "", "", false }, nil, exe) + if err == nil || err.Error() != "grok login gave no link to open: "+said { + s.mu.Lock() + t.Fatalf("err %v, link %q", err, s.st.URL) + } +} diff --git a/internal/provider/grok.go b/internal/provider/grok.go index 527c25a4b..26543dc64 100644 --- a/internal/provider/grok.go +++ b/internal/provider/grok.go @@ -704,6 +704,15 @@ func runCLISignIn(s *signInFlow, what string, env []string, using bool, failed f var partial struct { sync.Mutex link string + last string // the last line it printed, which says why there's no link + } + noLink := func() error { + partial.Lock() + defer partial.Unlock() + if partial.last != "" { + return fmt.Errorf("%s gave no link to open: %s", what, partial.last) + } + return fmt.Errorf("%s gave no link to open", what) } go func() { rd := bufio.NewReader(out) @@ -722,7 +731,9 @@ func runCLISignIn(s *signInFlow, what string, env []string, using bool, failed f switch { case sent: case link == "": - link = cursorLoginURL.FindString(line) + if !failedLine.MatchString(line) { + link = cursorLoginURL.FindString(line) + } case linkRest.MatchString(strings.TrimSpace(line)) && (!whole(link) || queryRest.MatchString(strings.TrimSpace(line))): // a whole link takes only more of its query, not "Waiting..." printed after it link += strings.TrimSpace(line) @@ -741,6 +752,9 @@ func runCLISignIn(s *signInFlow, what string, env []string, using bool, failed f } if strings.TrimSpace(line) != "" { tail = append(tail, strings.TrimSpace(line)) + partial.Lock() + partial.last = strings.TrimSpace(line) + partial.Unlock() } if rerr != nil { break @@ -772,7 +786,7 @@ func runCLISignIn(s *signInFlow, what string, env []string, using bool, failed f select { case l, ok := <-got: if !ok { - return fmt.Errorf("%s gave no link to open", what) + return noLink() } u = l case <-time.After(linkWait): @@ -782,7 +796,7 @@ func runCLISignIn(s *signInFlow, what string, env []string, using bool, failed f partial.Unlock() if u == "" { cancel() - return fmt.Errorf("%s gave no link to open", what) + return noLink() } } s.mu.Lock() @@ -791,6 +805,12 @@ func runCLISignIn(s *signInFlow, what string, env []string, using bool, failed f return nil } +// failedLine is a line saying the login failed, whose URL is the endpoint +// it couldn't reach, not a page to open: grok 1.0.46 behind a proxy it +// can't get through prints "Error: error sending request for url +// (https://auth.x.ai/oauth2/device/code): …" (𝕏 on Discord). +var failedLine = regexp.MustCompile(`(?i)\berror\b`) + // linkWait is how long a login command has to print its link. var linkWait = 30 * time.Second diff --git a/internal/provider/migrate_side.go b/internal/provider/migrate_side.go index ae68cdc7b..7331930b0 100644 --- a/internal/provider/migrate_side.go +++ b/internal/provider/migrate_side.go @@ -108,7 +108,7 @@ func init() { // names, as the built-in does: the homes stay where they are. movers["grok"] = &mover{ pkg: "@magpie-community/opencode-grok-auth", - min: "0.1.7", // a failure's status and its sign-in mark as the built-in's; grok-4.7's reasoning levels; a token Grok refuses early reads as expired; the sign-in renewed ahead of time through auth.refresh; a 429 shows a spent Rate limit window until it lifts + min: "0.1.8", // a failure's status and its sign-in mark as the built-in's; grok-4.7's reasoning levels; a token Grok refuses early reads as expired; the sign-in renewed ahead of time through auth.refresh; a 429 shows a spent Rate limit window until it lifts; a login with no link says why agents: []string{"grok"}, out: func() ([]Moving, error) { var out []Moving