grok 0.1.17: the sign-in page opens once, not twice (andyfmyth on X)

"magpie里的grok 登录验证 在mac下 似乎每次都会弹两次验证, 在浏览器窗口里":
`grok login --device-auth` (Grok Build 1.0.50, the webbrowser crate)
opens the device page itself, and magpie opened the same URL again.

Reproduced in a sandbox HOME/GROK_HOME on macOS: grok's own device-code
tab appeared in Chrome with magpie not involved, with and without
SSH_CONNECTION set. What decides grok's own open, from the binary's
strings and webbrowser 1.2.4's source:
- macOS: NSWorkspace, $BROWSER never read (the macOS binary has no
  "BROWSER" string);
- Windows: ShellExecuteW, no $BROWSER either;
- Linux and the rest: $BROWSER, else xdg-open, skipped with no display.

So the sign-in now says `opened: true` on darwin and win32 (magpie then
opens nothing; magpie >= the commit that raises the grok mover min reads
it), and elsewhere gives grok BROWSER=true, a no-op, so the opener stays
the only one to open the page. OpenCode ignores the extra field.

Tests: login.test.mjs runs the sign-in as darwin/win32 (opened, $BROWSER
untouched) and linux/freebsd (not opened, BROWSER=true reaches grok).
Both fail on 0.1.16's index.mjs (10 pass, 2 fail).

Run: HOME=<scratch> bun test packages/grok: 49 pass, 0 fail;
bun scripts/check.mjs: exit 0.
This commit is contained in:
yetone
2026-10-19 11:29:05 +08:00
parent ab38da681a
commit 47fbf634e7
3 changed files with 54 additions and 2 deletions
+15 -1
View File
@@ -462,12 +462,24 @@ function modelOf(id) {
// ---- sign-in ------------------------------------------------------------------
// opensPage is whether `grok login --device-auth` opens its page in the
// browser itself, which it does wherever it can: on macOS (NSWorkspace) and
// Windows (ShellExecute) always, with nothing to turn it off; on Linux and
// the BSDs through $BROWSER, else xdg-open where a display is. There it is
// given a $BROWSER that opens nothing, and the one who asked (magpie, which
// knows where the user's browser is) opens it; here the sign-in says the
// page is open already, so it isn't opened a second time (andyfmyth on X:
// "每次都会弹两次验证").
const opensPage = () => process.platform === "darwin" || process.platform === "win32"
// login runs `grok login --device-auth` in home and hands on the first link
// it prints; done is settled when the command is.
function login(exe, home) {
const env = envFor(home)
if (!opensPage()) env.BROWSER = "true"
const child = spawn(exe, ["login", "--device-auth"], {
cwd: homedir(),
env: envFor(home),
env,
stdio: ["ignore", "pipe", "pipe"],
})
const tail = []
@@ -533,6 +545,8 @@ async function signIn() {
}
return {
url,
// not OpenCode's: the page is open already, by Grok Build itself
opened: opensPage(),
instructions: "Approve the sign-in on the page; Grok Build finishes it.",
method: "auto",
callback: async () => {
+38
View File
@@ -28,6 +28,7 @@ afterAll(() => {
async function signIn(body) {
const bin = mkdtempSync(join(dir, "bin-"))
symlinkSync("/bin/sh", join(bin, "grok"))
symlinkSync("/bin/sh", join(bin, "grok.exe")) // as a test that says it is Windows looks for it
writeFileSync(script, `${body}\n`)
const env = { GROK_BIN_DIR: process.env.GROK_BIN_DIR, GROK_HOME: process.env.GROK_HOME }
process.env.GROK_BIN_DIR = bin
@@ -62,6 +63,43 @@ test.skipIf(process.platform === "win32")("a login's link is still handed on", a
expect(a.url).toBe("https://accounts.x.ai/oauth2/device?user_code=AB12-CD34")
})
// `grok login --device-auth` opens its page itself where it can, and the
// sign-in's opener opened it too: two pages every time (andyfmyth on X).
// On macOS and Windows, where nothing turns Grok Build's off, the sign-in
// says the page is open already; elsewhere Grok Build is given a $BROWSER
// that opens nothing, and the page is the opener's to open.
async function signInOn(platform, body) {
const was = Object.getOwnPropertyDescriptor(process, "platform")
const browser = process.env.BROWSER
delete process.env.BROWSER
Object.defineProperty(process, "platform", { ...was, value: platform })
try {
return await signIn(body)
} finally {
Object.defineProperty(process, "platform", was)
if (browser !== undefined) process.env.BROWSER = browser
}
}
// the link carries the $BROWSER Grok Build was given
const saysBrowser = `echo "https://accounts.x.ai/oauth2/device?user_code=AB12-CD34&browser=$BROWSER"; exit 1`
test.skipIf(process.platform === "win32")("on macOS and Windows the page Grok Build opened isn't opened again", async () => {
for (const os of ["darwin", "win32"]) {
const a = await signInOn(os, saysBrowser)
expect(a.opened).toBe(true)
expect(a.url).toBe("https://accounts.x.ai/oauth2/device?user_code=AB12-CD34&browser=")
}
})
test.skipIf(process.platform === "win32")("elsewhere Grok Build opens nothing and the page is the opener's", async () => {
for (const os of ["linux", "freebsd"]) {
const a = await signInOn(os, saysBrowser)
expect(a.opened).toBe(false)
expect(a.url).toBe("https://accounts.x.ai/oauth2/device?user_code=AB12-CD34&browser=true")
}
})
// A login that ends signed in is kept, or says where it looked and what it
// found (llllleo9 on 𝕏: "grok login finished without an account"). Run in a
// sandbox against a stand-in auth.x.ai, grok 1.0.62 writes "email": null
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-grok-auth",
"version": "0.1.16",
"version": "0.1.17",
"description": "OpenCode provider plugin: Grok (SuperGrok / X Premium+) through the Grok Build CLI's sign-in",
"type": "module",
"main": "./index.mjs",