{"record":{"id":"58635506e5d82c20","repo":"mastra-ai/mastra","slug":"failed-to-initiate-openai-codex-device-authorizati","errorCode":null,"errorMessage":"Failed to initiate OpenAI Codex device authorization: ${response.status}","messagePattern":"Failed to initiate OpenAI Codex device authorization: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/auth/providers/openai-codex.ts","lineNumber":425,"sourceCode":"  | { status: 'failed'; error: string };\n\n/**\n * Start a Codex device-code login: request a user code and return the\n * serializable pending state for subsequent polls.\n */\nexport async function startCodexDeviceLogin(options?: { signal?: AbortSignal }): Promise<CodexDeviceLoginPending> {\n  const response = await fetch(DEVICE_USER_CODE_URL, {\n    method: 'POST',\n    headers: {\n      'Content-Type': 'application/json',\n      'User-Agent': 'mastracode',\n    },\n    body: JSON.stringify({ client_id: CLIENT_ID, originator: 'mastracode' }),\n    signal: options?.signal,\n  });\n\n  if (!response.ok) {\n    throw new Error(`Failed to initiate OpenAI Codex device authorization: ${response.status}`);\n  }\n\n  const deviceData = (await response.json()) as {\n    device_auth_id?: string;\n    user_code?: string;\n    usercode?: string;\n    interval?: string | number;\n  };\n\n  const userCode = deviceData.user_code ?? deviceData.usercode;\n\n  if (!deviceData.device_auth_id || !userCode) {\n    throw new Error('OpenAI Codex device authorization response missing required fields');\n  }\n\n  const intervalSeconds =\n    typeof deviceData.interval === 'number' ? deviceData.interval : Number.parseInt(deviceData.interval ?? '', 10) || 5;\n","sourceCodeStart":407,"sourceCodeEnd":443,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/auth/providers/openai-codex.ts#L407-L443","documentation":"The device-authorization POST to OpenAI's Codex device-login endpoint returned a non-OK HTTP status. The library surfaces the raw status code because the response body may not be parseable or may not contain a useful message. This indicates the request was rejected or the endpoint is unavailable at that moment.","triggerScenarios":"fetch to the device authorization endpoint returning 4xx/5xx (e.g. 400 bad client_id, 403 blocked, 429 rate limited, 5xx outage) during startCodexDeviceLogin, called via loginOpenAICodexDevice / loginOpenAICodex.","commonSituations":"OpenAI API outage or degraded service; corporate proxy/firewall stripping the request; stale CLIENT_ID after an SDK/API version change; rate limiting after repeated login attempts.","solutions":["Retry after a short delay — 5xx and 429 are usually transient; add exponential backoff.","Check network/proxy reachability to the OpenAI device-auth endpoint (curl the URL to see the status/body).","Update the mastracode SDK to the latest version in case the CLIENT_ID or endpoint changed.","If 401/403 persists, verify no org/plan restriction blocks Codex device login and contact OpenAI support."],"exampleFix":"// before: single shot, no handling\nconst pending = await startCodexDeviceLogin();\n\n// after: retry transient failures\nlet pending;\nfor (let i = 0; i < 3 && !pending; i++) {\n  try { pending = await startCodexDeviceLogin(); }\n  catch (e) {\n    if (!/status: (5\\d\\d|429)/.test(String(e))) throw e;\n    await new Promise(r => setTimeout(r, 1000 * 2 ** i));\n  }\n}","handlingStrategy":"retry","validationCode":"async function deviceAuthReachable(): Promise<boolean> {\n  try {\n    const res = await fetch(TOKEN_URL, { method: 'HEAD' });\n    return res.status < 500;\n  } catch { return false; }\n}","typeGuard":null,"tryCatchPattern":"for (let attempt = 0; attempt < 4; attempt++) {\n  try {\n    pending = await loginOpenAICodexDevice({});\n    break;\n  } catch (e) {\n    if (!/device authorization: \\d/.test(e.message) || attempt === 3) throw e;\n    await new Promise(r => setTimeout(r, 1000 * 2 ** attempt));\n  }\n}","preventionTips":["Back off exponentially on 429/5xx before giving up.","Check network/proxy access to OpenAI endpoints from your environment.","Keep the SDK updated so CLIENT_ID/endpoint stay current.","Watch OpenAI status pages during incidents."],"tags":["network","oauth","http-status","device-login"],"backgroundTag":"http-5xx-api-error","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}