Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(grok): keep a newer grok-oauth.json over a racing token refresh
Jack Rudenko5f0e8aaa7039
diff --git a/packages/cli/src/auth/credential-file.ts b/packages/cli/src/auth/credential-file.tsindex 547eded4..9ea00135 100644--- a/packages/cli/src/auth/credential-file.ts+++ b/packages/cli/src/auth/credential-file.ts@@ -23,7 +23,19 @@** Never logs a credential value: log lines carry the label and the path only.*/-import { readFileSync, statSync } from "node:fs";+import { randomBytes } from "node:crypto";+import {+ closeSync,+ fsyncSync,+ mkdirSync,+ openSync,+ readFileSync,+ renameSync,+ statSync,+ unlinkSync,+ writeSync,+} from "node:fs";+import { dirname } from "node:path";import { log } from "../logger.js";/** What identifies one version of a file without reading it. */@@ -53,6 +65,60 @@ function sameStamp(a: FileStamp, b: FileStamp): boolean {return a.ino === b.ino && a.size === b.size && a.mtimeMs === b.mtimeMs && a.ctimeMs === b.ctimeMs;}+/**+ * Write `data` as JSON: a new temp file (`<path>.tmp-<pid>-<hex>`, opened "wx", mode+ * 0600), fsync, rename onto `path`. A reader sees the old file or the new one, never a+ * partial one. Returns the new stamp. Throws with the old file intact on any failure.+ */+export function writeCredentialFileAtomic(path: string, data: unknown): FileStamp {+ mkdirSync(dirname(path), { recursive: true });+ const tmp = `${path}.tmp-${process.pid}-${randomBytes(6).toString("hex")}`;+ try {+ const fd = openSync(tmp, "wx", 0o600);+ try {+ writeSync(fd, JSON.stringify(data, null, 2));+ fsyncSync(fd);+ } finally {+ closeSync(fd);+ }+ renameSync(tmp, path);+ } catch (err) {+ try {+ unlinkSync(tmp);+ } catch {+ // Never created, or already renamed.+ }+ throw err;+ }+ const stamp = statCredentialFile(path);+ if (!stamp || "error" in stamp) throw new Error(`credential file vanished after write: ${path}`);+ return stamp;+}++/**+ * Which login a credential belongs to. A refresh keeps the lineage when the server+ * does not rotate the refresh token and moves it when it does; a NEW login always+ * moves it. With no refresh token the access token stands in.+ */+function lineageOf(credentials: { access_token: string; refresh_token?: string }): string {+ return credentials.refresh_token+ ? `refresh:${credentials.refresh_token}`+ : `access:${credentials.access_token}`;+}++/** What became of a refreshed credential this process tried to write back. */+export type SaveRefreshedResult<T> =+ /** The file still held `base`'s lineage; `next` was written. */+ | { outcome: "saved" }+ /** Another process wrote a different login during the exchange; nothing written. */+ | { outcome: "newer-on-disk"; onDisk: T }+ /** The file was deleted during the exchange (a logout); nothing written. */+ | { outcome: "removed" }+ /** Still unreadable after the bounded re-read: it may be a newer login caught mid-write. */+ | { outcome: "unreadable" }+ /** The atomic write itself failed (EACCES, ENOSPC); the old file is intact. */+ | { outcome: "write-failed" };+/*** unchanged : same path, same stamp as the last GOOD read* reloaded : the stamp moved; the file parsed and validated; `value` replaced@@ -134,15 +200,57 @@ export class CredentialFileState<T extends { access_token: string; refresh_tokenreturn result;}+ /** A login: the user's intent, written unconditionally (atomic). */+ saveLogin(next: T): void {+ const path = this.opts.path();+ const stamp = writeCredentialFileAtomic(path, next);+ this.current = next;+ this.recorded = { path, stamp };+ }+/**- * Hold `next` as this process's credentials after THIS process wrote it to `path`,- * recording the file's current stamp so our own write is not read back as a change.+ * Hold `next` in memory WITHOUT writing it or recording a stamp: the next sync+ * reads the file again, and a newer login that finished writing wins.*/- adoptWritten(next: T): void {- const path = this.opts.path();- const stamp = statCredentialFile(path);+ keepInMemory(next: T): void {this.current = next;- this.recorded = stamp && !("error" in stamp) ? { path, stamp } : null;+ }++ /**+ * A refresh result. Settles first, then writes `next` ONLY if the file still holds+ * `base`'s lineage (compare-and-set on the login, not on the stamp: our own+ * previous write also moves the stamp). A refresh that raced a login therefore+ * cannot undo the login. Never throws.+ */+ async saveRefreshed(base: T, next: T): Promise<SaveRefreshedResult<T>> {+ const label = this.opts.label;+ const sync = await this.settle();+ if (sync === "unreadable") {+ log(+ `[${label}] credential file unreadable after the token refresh; kept the refreshed token in memory, not written`+ );+ return { outcome: "unreadable" };+ }+ const onDisk = this.current;+ if (sync === "removed" || onDisk === null) return { outcome: "removed" };+ if (lineageOf(onDisk) !== lineageOf(base)) {+ log(`[${label}] credential file changed during the token refresh; kept the newer file`);+ return { outcome: "newer-on-disk", onDisk };+ }+ const path = this.opts.path();+ try {+ const stamp = writeCredentialFileAtomic(path, next);+ this.current = next;+ this.recorded = { path, stamp };+ log(`[${label}] Credentials saved to ${path}`);+ return { outcome: "saved" };+ } catch (err) {+ const code = (err as NodeJS.ErrnoException | null)?.code ?? "error";+ log(+ `[${label}] could not write the refreshed credentials (${path}, ${code}); kept them in memory`+ );+ return { outcome: "write-failed" };+ }}/** Forget the in-memory credentials (a logout in this process). */@@ -157,3 +265,100 @@ export class CredentialFileState<T extends { access_token: string; refresh_tokenreturn "unreadable";}}++export interface RefreshAndCommitOptions<T> {+ /** Whether a credential's access token can still be used without a refresh. */+ isValid: (credentials: T) => boolean;+ /** The terminal "No … credentials found. Please run `claudish login …`" error. */+ noCredentials: () => Error;+}++/**+ * Refresh this process's credentials and commit the result to the file, without ever+ * undoing a login another process wrote meanwhile. ONE implementation, shared by+ * `OAuthManager` and `KimiOAuth`, so the adopt and retry-once rules cannot drift.+ *+ * - `refresh(current)` does the network exchange only and returns the next+ * credentials; it never writes.+ * - A failed refresh is not reported yet: the file is settled first. If it now holds+ * a different login (`claudish login` ran elsewhere), that login is used: its token+ * when still valid, else ONE refresh from its refresh token. Otherwise the original+ * error is rethrown unchanged (same object), so its classification at the auth+ * sites is untouched and no status is invented.+ * - The result is committed with `saveRefreshed`: written only if the file still holds+ * the lineage we refreshed from; a newer login on disk is adopted instead; a deleted+ * file (logout) is not recreated; an unreadable file or a failed write keeps the+ * refreshed token in memory and signs with it.+ *+ * Returns the access token to sign with.+ */+export async function refreshAndCommit<T extends { access_token: string; refresh_token?: string }>(+ state: CredentialFileState<T>,+ refresh: (current: T) => Promise<T>,+ opts: RefreshAndCommitOptions<T>,+ label = "credentials"+): Promise<string> {+ const base = state.value;+ if (!base) throw opts.noCredentials();++ let next: T;+ try {+ next = await refresh(base);+ } catch (err) {+ await state.settle();+ const now = state.value;+ if (now && lineageOf(now) !== lineageOf(base)) {+ log(+ `[${label}] token refresh failed, but the credential file changed on disk; using the newer login`+ );+ return useLogin(state, now, refresh, opts, label, false);+ }+ throw err;+ }+ return commit(state, base, next, refresh, opts, label, true);+}++/** Sign with `login` (a login another process wrote): its token if valid, else refresh once. */+async function useLogin<T extends { access_token: string; refresh_token?: string }>(+ state: CredentialFileState<T>,+ login: T,+ refresh: (current: T) => Promise<T>,+ opts: RefreshAndCommitOptions<T>,+ label: string,+ mayRetry: boolean+): Promise<string> {+ if (opts.isValid(login)) return login.access_token;+ const next = await refresh(login);+ return commit(state, login, next, refresh, opts, label, mayRetry);+}++async function commit<T extends { access_token: string; refresh_token?: string }>(+ state: CredentialFileState<T>,+ base: T,+ next: T,+ refresh: (current: T) => Promise<T>,+ opts: RefreshAndCommitOptions<T>,+ label: string,+ mayRetry: boolean+): Promise<string> {+ const result = await state.saveRefreshed(base, next);+ switch (result.outcome) {+ case "saved":+ return next.access_token;+ case "newer-on-disk":+ // Discard our result. A newer login that is already expired is refreshed once;+ // past that one retry, our fresh token signs this request from memory.+ if (opts.isValid(result.onDisk) || mayRetry) {+ return useLogin(state, result.onDisk, refresh, opts, label, false);+ }+ state.keepInMemory(next);+ return next.access_token;+ case "removed":+ // A logout during the exchange is not undone.+ throw opts.noCredentials();+ case "unreadable":+ case "write-failed":+ state.keepInMemory(next);+ return next.access_token;+ }+}diff --git a/packages/cli/src/auth/grok-oauth.test.ts b/packages/cli/src/auth/grok-oauth.test.tsindex eeee377f..198ea74d 100644--- a/packages/cli/src/auth/grok-oauth.test.ts+++ b/packages/cli/src/auth/grok-oauth.test.ts@@ -1,5 +1,5 @@import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";-import { mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { GrokOAuth, type GrokOAuthCredentials, setGrokOAuthPathForTesting } from "./grok-oauth.js";@@ -50,6 +50,10 @@ function seedCredentialFile(credentials: GrokOAuthCredentials): void {writeFileSync(credentialPath, JSON.stringify(credentials, null, 2), { mode: 0o600 });}+function readCredentialFile(): GrokOAuthCredentials {+ return JSON.parse(readFileSync(credentialPath, "utf-8")) as GrokOAuthCredentials;+}+function internals(oauth: GrokOAuth): GrokOAuthTestInternals {return oauth as unknown as GrokOAuthTestInternals;}@@ -292,10 +296,7 @@ describe("GrokOAuth refresh", () => {test("uses a public-client refresh form and persists a rotated refresh token", async () => {const oauth = new HermeticGrokOAuth();- const state = internals(oauth);seedCredentialFile(expiredCredentials());- const saveCredentials = mock((_credentials: GrokOAuthCredentials) => {});- state.saveCredentials = saveCredentials;const fetchMock = mock(async (_input: RequestInfo | URL, _init?: RequestInit) =>jsonResponse({access_token: "tok-aaa",@@ -320,7 +321,8 @@ describe("GrokOAuth refresh", () => {});// Public-client refresh must never grow an accidental confidential-client secret.expect([...form.keys()].some((key) => /secret/i.test(key))).toBe(false);- expect(saveCredentials.mock.calls[0][0]).toMatchObject({+ // The refresh is committed to the credential file (refreshAndCommit), not to memory only.+ expect(readCredentialFile()).toMatchObject({access_token: "tok-aaa",refresh_token: "ref-aaa",client_id: "client-aaa",@@ -329,10 +331,7 @@ describe("GrokOAuth refresh", () => {test("keeps the existing refresh token when the server does not rotate it", async () => {const oauth = new HermeticGrokOAuth();- const state = internals(oauth);seedCredentialFile(expiredCredentials());- const saveCredentials = mock((_credentials: GrokOAuthCredentials) => {});- state.saveCredentials = saveCredentials;globalThis.fetch = mock(async () =>jsonResponse({ access_token: "tok-aaa", expires_in: 3600 })) as unknown as typeof fetch;@@ -340,7 +339,7 @@ describe("GrokOAuth refresh", () => {await expect(oauth.getToken()).resolves.toBe("tok-aaa");// Dropping the old value here would strand the very next refresh attempt.- expect(saveCredentials.mock.calls[0][0].refresh_token).toBe("ref-old");+ expect(readCredentialFile().refresh_token).toBe("ref-old");});test("names claudish login grok when refresh fails", async () => {diff --git a/packages/cli/src/auth/grok-oauth.ts b/packages/cli/src/auth/grok-oauth.tsindex ce7121e1..ae0b3715 100644--- a/packages/cli/src/auth/grok-oauth.ts+++ b/packages/cli/src/auth/grok-oauth.ts@@ -183,16 +183,10 @@ export class GrokOAuth extends OAuthManager<GrokOAuthCredentials> {/*** Exchange the refresh token. Public client: `client_id` in the body, no* secret. The IdP may ROTATE the refresh token, so whatever comes back is- * persisted — dropping it would strand the next refresh on a dead token.+ * returned for `refreshAndCommit` to persist — dropping it would strand the next+ * refresh on a dead token. Nothing is written here.*/- protected async doRefreshToken(): Promise<string> {- const current = this.credentials;- if (!current) {- throw terminalCredentialError(- `No Grok credentials found. Please run \`${this.loginHint}\` first.`- );- }-+ protected async doRefreshToken(current: GrokOAuthCredentials): Promise<GrokOAuthCredentials> {const body = new URLSearchParams({grant_type: "refresh_token",refresh_token: current.refresh_token,@@ -262,8 +256,7 @@ export class GrokOAuth extends OAuthManager<GrokOAuthCredentials> {client_id: current.client_id || GROK_PUBLIC_CLIENT_ID,scope: parsed.scope ?? current.scope,};- this.saveCredentials(next);- return next.access_token;+ return next;}/**diff --git a/packages/cli/src/auth/oauth-manager.test.ts b/packages/cli/src/auth/oauth-manager.test.tsindex 366ee225..2d270aef 100644--- a/packages/cli/src/auth/oauth-manager.test.ts+++ b/packages/cli/src/auth/oauth-manager.test.ts@@ -37,8 +37,8 @@ class TestOAuthManager extends OAuthManager<BaseCredentials> {protected readonly loginHint = "unused";readonly clipboardCalls: string[] = [];- protected async doRefreshToken(): Promise<string> {- return "tok-unused";+ protected async doRefreshToken(current: BaseCredentials): Promise<BaseCredentials> {+ return current;}protected validateCredentials(_data: unknown): _data is BaseCredentials {diff --git a/packages/cli/src/auth/oauth-manager.ts b/packages/cli/src/auth/oauth-manager.tsindex f1cfaf6a..6aeb7fb6 100644--- a/packages/cli/src/auth/oauth-manager.ts+++ b/packages/cli/src/auth/oauth-manager.ts@@ -14,12 +14,12 @@import { exec } from "node:child_process";import { createHash, randomBytes } from "node:crypto";-import { closeSync, existsSync, mkdirSync, openSync, unlinkSync, writeSync } from "node:fs";+import { existsSync, mkdirSync, unlinkSync } from "node:fs";import { homedir } from "node:os";import { join } from "node:path";import { promisify } from "node:util";import { log } from "../logger.js";-import { CredentialFileState } from "./credential-file.js";+import { CredentialFileState, refreshAndCommit } from "./credential-file.js";import { terminalCredentialError } from "./credentials/credential-error.js";const execAsync = promisify(exec);@@ -52,8 +52,12 @@ export abstract class OAuthManager<T extends BaseCredentials = BaseCredentials>/** CLI login command hint (e.g. "claudish login gemini") */protected abstract readonly loginHint: string;- /** Provider-specific token refresh. Must return the new access_token. */- protected abstract doRefreshToken(): Promise<string>;+ /**+ * Provider-specific token exchange: refresh `current` and RETURN the next+ * credentials. Never writes them: `refreshAndCommit` commits the result through+ * `saveRefreshed`, which will not overwrite a newer login another process wrote.+ */+ protected abstract doRefreshToken(current: T): Promise<T>;/** Validate that parsed JSON has all required fields for this provider's credential type. */protected abstract validateCredentials(data: unknown): data is T;@@ -96,17 +100,10 @@ export abstract class OAuthManager<T extends BaseCredentials = BaseCredentials>return this.fileState.value;}+ /** A LOGIN's credentials: written unconditionally, atomically (temp + rename, 0600). */protected saveCredentials(credentials: T): void {- OAuthManager.ensureClaudishDir();- const credPath = this.getCredentialsPath();- const fd = openSync(credPath, "w", 0o600);- try {- writeSync(fd, JSON.stringify(credentials, null, 2), 0, "utf-8");- } finally {- closeSync(fd);- }- this.fileState.adoptWritten(credentials);- log(`[${this.providerName}] Credentials saved to ${credPath}`);+ this.fileState.saveLogin(credentials);+ log(`[${this.providerName}] Credentials saved to ${this.getCredentialsPath()}`);}protected deleteCredentials(): void {@@ -157,13 +154,26 @@ export abstract class OAuthManager<T extends BaseCredentials = BaseCredentials>}// Before refresh: the refresh starts from the file's credentials as just synced.- this.refreshPromise = this.doRefreshToken().finally(() => {+ this.refreshPromise = this.refreshAndCommit().finally(() => {this.refreshPromise = null;});return this.refreshPromise;}+ /** The shared refresh-and-commit sequence (`credential-file.ts`), for this manager. */+ private refreshAndCommit(): Promise<string> {+ return refreshAndCommit(+ this.fileState,+ (current) => this.doRefreshToken(current),+ {+ isValid: (credentials) => this.isCredentialValid(credentials),+ noCredentials: () => this.noCredentialsError(),+ },+ this.providerName+ );+ }+/*** A forced refresh (the transport's 401 retry). The token that drew the 401 is* not passed in, so the proxy for it is the access token held in memory right@@ -182,12 +192,16 @@ export abstract class OAuthManager<T extends BaseCredentials = BaseCredentials>log(`[${this.providerName}] credential file changed on disk; using the newer login`);return;}- await this.doRefreshToken();+ await this.refreshAndCommit();}protected isTokenValid(): boolean {- if (!this.credentials) return false;- return Date.now() < this.credentials.expires_at - this.tokenRefreshMargin;+ const current = this.credentials;+ return current !== null && this.isCredentialValid(current);+ }++ protected isCredentialValid(credentials: T): boolean {+ return Date.now() < credentials.expires_at - this.tokenRefreshMargin;}// ── PKCE Helpers ───────────────────────────────────────────────────────