Some grok tests looked Grok Build up as the plugin does and ran what they
found (`grok --version`, `grok models`). On a machine with Grok Build
installed that ran the real CLI, which can reach xAI.
packages/grok/nocli.mjs is imported first by every grok test file, and
each calls noRealGrok() before reading its environment. It sets every
place executable() looks:
- PATH: /usr/bin and /bin only (the fakes' scripts run cat, rm, sleep),
each left out if it holds a grok; empty on Windows.
- GROK_BIN_DIR: a folder that doesn't exist; GROK_HOME: an empty folder.
- HOME and USERPROFILE: an empty folder, for ~/.grok/bin and
~/.local/bin. Bun's os.homedir() keeps the HOME the process started
with (checked: setting process.env.HOME doesn't change it), so the
helper mock.module()s node:os with a homedir that reads HOME
(USERPROFILE) at call time, as Node's does, before index.mjs loads.
Other packages in the same run see Node's behaviour: the started HOME
unless a test changed it.
A test that wants a CLI still puts its fake in GROK_BIN_DIR.
Marker proof: a fake grok (under a .grok folder, so it counts as Grok
Build) that appends its argv to a marker file, placed on the outer PATH,
in ~/.local/bin and ~/.grok/bin under the test HOME. Run as
TMPDIR=$H HOME=$H/home PATH=$H/outer:$PATH bun test:
- old tests (92d5557), packages/grok: marker written, 5 runs (1 `grok
--version`, 4 `grok models`, from ~/.grok/bin under the test HOME),
78 pass 4 fail
- new tests, packages/grok: no marker, 82 pass 0 fail
- new tests with the mock.module line commented out: marker written
again (same 5 runs), 78 pass 4 fail
- new tests, whole repo: no marker, 2166 pass 0 fail across 189 files
- bun scripts/check.mjs: ok (no version bump: test-only change)
Not tried: Windows; a run with the real HOME (the marker run covers the
same folders under a test HOME); the real grok was never run.
Closes#325
Grok can reject a token hours before its expires_at. A 401 used to be
passed on (requests) or read as "Grok's sign-in has expired; run `grok
login`" (models), with no renewal tried: token(home, expired) was never
called with expired set. And an auth.json caught while the CLI rewrote
it read as "not signed in", which the refresh hook marks as an expired
sign-in.
- token(home, rejected): rejected is the token Grok turned away. A token
on disk other than that one was renewed already and is used; else the
CLI renews it (`grok models` in the home). A run that leaves the same
token throws "Grok Build couldn't renew the sign-in (grok models
<exited N | ran and left the same token>: <its last line>)", never
"not signed in" or "expired".
- What it says helps: for Grok Build's own home (current()), open Grok
Build and send it a message; for a home of the plugin's (a second
device-code account, a CPA import) add the account in magpie again.
No "run `grok login`" where that signs in a different home. A home of
the plugin's with no sign-in left says to add the account again too.
- Requests: a 401 renews once and resends the same body with the new
token. Unrenewable, the 401 carries that message plus Grok's own words
(JSON error), still X-Magpie-Sign-In: kept. Models: a 401 renews once
and lists again; a renewed token also refused says so. Usage: same
retry once. auth.refresh's unrenewed case uses the same message.
- look(): an empty or non-JSON auth.json is torn. read() tries it again
for ~0.5s (6 x 100ms), then keeps the last sign-in read whole there,
as the CLI does ("auth reload: auth.json unreadable, keeping previous
credentials", in its binary's strings). Torn with nothing read before
is an error saying it couldn't be read, with no signIn mark. No
auth.json.lock is taken: it is the CLI's flock around its renewal,
Node can't take a flock, and holding it could only delay a renewal.
Changed tests: signin.test's 401 now has no CLI and expects the no-CLI
message beside Grok's body; usage.test and models.test the same; a
private home's "not signed in" says to add the account again.
Run (worktree at 59990e1, PATH without ~/.grok/bin and ~/.local/bin so
no real grok is found; TMPDIR and HOME in a fresh temp dir):
- packages/grok: bun test, 82 pass 0 fail
- repo: bun test, 2166 pass 0 fail across 189 files; bun scripts/check.mjs ok
Broken by hand (renew.test.mjs, 9 tests):
- old index.mjs: all 9 red
- TORN_TRIES = 1: the mid-write test red
- no last-good fallback: the stays-torn test red
- unchanged token not caught after renewal: the three couldn't-renew
tests (request private home, request shared home, models) red
- shared() always false: the shared-home message test red
Not tried: the real Grok Build CLI or a real Grok account (the fake CLI is
/bin/sh, fetch is a stand-in); a real 401 from Grok before expires_at; a
CLI writing auth.json in place while it is read (the torn file is
written by the test); a body that is a stream (not resent, as before:
only a string or no body is retried); Windows. magpie's mover min for
grok is not raised here.
Grok's billing (/v1/billing?format=credits) says nothing of a plan's rate
limit: a free account read 0% used, 100 left, while its requests came back
429. The plugin now keeps an account's last 429 (by its CLI home) and
usage shows a "Rate limit" window at 100%, aside, before the period's,
until the reset the 429 named (Retry-After in seconds or as a date, or a
*ratelimit*reset* header), or, with none named, until a request goes
through or 5 minutes have passed. magpie's gateway already rests the
account; this is what the card says.