jackwener/OpenCLI · error · CommandExecutionError

Grok whoami failed: ${result.detail}

Error message

Grok whoami failed: ${result.detail}

What it means

verifyGrokIdentity probes grok.com's /api/auth/session from inside the page. When the in-page fetch itself throws (network failure, JSON parse error, page context destroyed mid-navigation), the probe returns {kind:'exception'} and the library rethrows it as a CommandExecutionError with the message 'Grok whoami failed: <detail>'. This means the whoami/verify step crashed before it could classify the result as auth or HTTP-status failure.

Source

Thrown at clis/grok/auth.js:34

    try {
      const res = await fetch('/api/auth/session', { credentials: 'include', headers: { 'Accept': 'application/json' } });
      if (res.status === 401 || res.status === 403) {
        return { kind: 'auth', detail: 'Grok /api/auth/session HTTP ' + res.status };
      }
      if (!res.ok) return { kind: 'http', httpStatus: res.status };
      const d = await res.json();
      const user = d && d.user;
      if (!user || !user.id) {
        return { kind: 'auth', detail: 'Grok /api/auth/session has no user — anonymous' };
      }
      return { ok: true, user_id: String(user.id), name: String(user.name || '') };
    } catch (e) {
      return { kind: 'exception', detail: String(e && e.message || e) };
    }
  })()`);
  if (result?.kind === 'auth') throw new AuthRequiredError('grok.com', result.detail);
  if (result?.kind === 'http') throw new CommandExecutionError(`HTTP ${result.httpStatus} from /api/auth/session`);
  if (result?.kind === 'exception') throw new CommandExecutionError(`Grok whoami failed: ${result.detail}`);
  if (!result?.ok) throw new CommandExecutionError(`Unexpected Grok probe: ${JSON.stringify(result)}`);
  return { user_id: result.user_id, name: result.name };
}

registerSiteAuthCommands({
  site: 'grok',
  domain: 'grok.com',
  loginUrl: 'https://grok.com/auth/sign-in',
  columns: ['user_id', 'name'],
  quickCheck: hasGrokSessionCookie,
  verify: verifyGrokIdentity,
  poll: async (page) => {
    if (!await hasGrokSessionCookie(page)) {
      throw new AuthRequiredError('grok.com', 'Waiting for Grok session cookie');
    }
    return verifyGrokIdentity(page);
  },
});

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Re-run the command after confirming the grok.com browser session is open and the network can reach grok.com
  2. Log in again (grok login) to refresh the session, then retry verify
  3. Inspect the <detail> in the message: 'Unexpected token ... in JSON' means a non-JSON (HTML/blocked) response — check for bot-protection/CDN blocks
  4. If it recurs, clear site cookies for grok.com and re-authenticate to reset a corrupted session

Example fix

// before
const result = await page.evaluate(`(async () => { ... fetch('/api/auth/session') ... })()`);
// after (guard before probing)
if (!await hasGrokSessionCookie(page)) throw new AuthRequiredError('grok.com', 'session cookie missing');
await page.goto('https://grok.com/');
await page.wait(2);
const result = await page.evaluate(`(async () => { ... })()`);
Defensive patterns

Strategy: try-catch

Validate before calling

const cookies = await page.getCookies({ url: 'https://grok.com' });
const hasSession = cookies.some(c => c.name === '__Secure-next-auth.session-token' && c.value);
if (!hasSession) throw new Error('Run `grok login` first — no session cookie.');

Type guard

function isProbeResult(r) {
  return !!r && typeof r === 'object' && ['auth','http','exception'].includes(r.kind) || r?.ok === true;
}

Try / catch

try {
  const who = await verifyGrokIdentity(page);
} catch (e) {
  if (String(e.message).startsWith('Grok whoami failed:')) {
    // probe crashed (network/page context); refresh session and retry once
    await page.goto('https://grok.com/');
    return verifyGrokIdentity(page);
  }
  throw e;
}

Prevention

When it happens

Trigger: page.evaluate's fetch to /api/auth/session throws: navigation interrupted the page context, the browser session was closed, a JSON parse failure on a non-JSON response body, or any unexpected exception inside the probe's try/catch (auth.js:28-30).

Common situations: Running `grok whoami` while the persistent browser session was killed mid-run; grok.com returning an HTML error page (5xx/CDN block) that fails res.json(); flaky network or proxy interference; a Grok front-end deploy changing the session endpoint behavior.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/0cc558c41518624f. Report an issue: GitHub.