{"record":{"id":"0cc558c41518624f","repo":"jackwener/OpenCLI","slug":"grok-whoami-failed-result-detail","errorCode":null,"errorMessage":"Grok whoami failed: ${result.detail}","messagePattern":"Grok whoami failed: (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/grok/auth.js","lineNumber":34,"sourceCode":"    try {\n      const res = await fetch('/api/auth/session', { credentials: 'include', headers: { 'Accept': 'application/json' } });\n      if (res.status === 401 || res.status === 403) {\n        return { kind: 'auth', detail: 'Grok /api/auth/session HTTP ' + res.status };\n      }\n      if (!res.ok) return { kind: 'http', httpStatus: res.status };\n      const d = await res.json();\n      const user = d && d.user;\n      if (!user || !user.id) {\n        return { kind: 'auth', detail: 'Grok /api/auth/session has no user — anonymous' };\n      }\n      return { ok: true, user_id: String(user.id), name: String(user.name || '') };\n    } catch (e) {\n      return { kind: 'exception', detail: String(e && e.message || e) };\n    }\n  })()`);\n  if (result?.kind === 'auth') throw new AuthRequiredError('grok.com', result.detail);\n  if (result?.kind === 'http') throw new CommandExecutionError(`HTTP ${result.httpStatus} from /api/auth/session`);\n  if (result?.kind === 'exception') throw new CommandExecutionError(`Grok whoami failed: ${result.detail}`);\n  if (!result?.ok) throw new CommandExecutionError(`Unexpected Grok probe: ${JSON.stringify(result)}`);\n  return { user_id: result.user_id, name: result.name };\n}\n\nregisterSiteAuthCommands({\n  site: 'grok',\n  domain: 'grok.com',\n  loginUrl: 'https://grok.com/auth/sign-in',\n  columns: ['user_id', 'name'],\n  quickCheck: hasGrokSessionCookie,\n  verify: verifyGrokIdentity,\n  poll: async (page) => {\n    if (!await hasGrokSessionCookie(page)) {\n      throw new AuthRequiredError('grok.com', 'Waiting for Grok session cookie');\n    }\n    return verifyGrokIdentity(page);\n  },\n});","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/grok/auth.js#L16-L52","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Re-run the command after confirming the grok.com browser session is open and the network can reach grok.com","Log in again (grok login) to refresh the session, then retry verify","Inspect the <detail> in the message: 'Unexpected token ... in JSON' means a non-JSON (HTML/blocked) response — check for bot-protection/CDN blocks","If it recurs, clear site cookies for grok.com and re-authenticate to reset a corrupted session"],"exampleFix":"// before\nconst result = await page.evaluate(`(async () => { ... fetch('/api/auth/session') ... })()`);\n// after (guard before probing)\nif (!await hasGrokSessionCookie(page)) throw new AuthRequiredError('grok.com', 'session cookie missing');\nawait page.goto('https://grok.com/');\nawait page.wait(2);\nconst result = await page.evaluate(`(async () => { ... })()`);","handlingStrategy":"try-catch","validationCode":"const cookies = await page.getCookies({ url: 'https://grok.com' });\nconst hasSession = cookies.some(c => c.name === '__Secure-next-auth.session-token' && c.value);\nif (!hasSession) throw new Error('Run `grok login` first — no session cookie.');","typeGuard":"function isProbeResult(r) {\n  return !!r && typeof r === 'object' && ['auth','http','exception'].includes(r.kind) || r?.ok === true;\n}","tryCatchPattern":"try {\n  const who = await verifyGrokIdentity(page);\n} catch (e) {\n  if (String(e.message).startsWith('Grok whoami failed:')) {\n    // probe crashed (network/page context); refresh session and retry once\n    await page.goto('https://grok.com/');\n    return verifyGrokIdentity(page);\n  }\n  throw e;\n}","preventionTips":["Always run a quick cookie check (hasGrokSessionCookie equivalent) before verify","Keep the browser session open for the duration of the command; avoid navigating away during evaluate","Treat non-JSON/5xx responses from grok.com as a signal to re-login rather than retry blindly","Pin opencli + browser driver versions so page.evaluate returns serialized objects reliably"],"tags":["browser-automation","network","whoami","grok"],"backgroundTag":"in-page-fetch-failed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}