{"record":{"id":"5fdb759721deab61","repo":"mastra-ai/mastra","slug":"invalid-kimi-for-coding-device-authorization-respo","errorCode":null,"errorMessage":"Invalid Kimi For Coding device authorization response","messagePattern":"Invalid Kimi For Coding device authorization response","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/auth/providers/kimi-coding.ts","lineNumber":140,"sourceCode":"  if (!response.ok) {\n    const text = await response.text().catch(() => '');\n    throw new Error(`Kimi For Coding device authorization failed: ${response.status}${text ? ` ${text}` : ''}`);\n  }\n\n  const data = await readJson(response);\n  const deviceCode = data?.device_code;\n  const userCode = data?.user_code;\n  const verificationUri = trustedHttpUrl(data?.verification_uri);\n  const verificationUriComplete = trustedHttpUrl(data?.verification_uri_complete);\n  if (\n    typeof deviceCode !== 'string' ||\n    !deviceCode ||\n    typeof userCode !== 'string' ||\n    !userCode ||\n    !verificationUri ||\n    !verificationUriComplete\n  ) {\n    throw new Error('Invalid Kimi For Coding device authorization response');\n  }\n\n  const interval = data?.interval;\n  const expiresIn = data?.expires_in;\n  return {\n    clientId,\n    deviceId,\n    deviceCode,\n    userCode,\n    url: verificationUriComplete,\n    instructions: `Enter code: ${userCode}`,\n    state: createDeviceCodePollState({\n      intervalSeconds:\n        typeof interval === 'number' && Number.isFinite(interval) && interval > 0\n          ? interval\n          : DEFAULT_POLL_INTERVAL_SECONDS,\n      expiresInSeconds:\n        typeof expiresIn === 'number' && Number.isFinite(expiresIn) && expiresIn > 0","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/auth/providers/kimi-coding.ts#L122-L158","documentation":"After a 2xx device-authorization response, startKimiCodingDeviceLogin validates that device_code, user_code, verification_uri, and verification_uri_complete are all present (with URIs vetted by trustedHttpUrl). If the server returned an OK status but the payload lacks any of these required device-flow fields, the library throws this contract error instead of returning a partially usable pending login.","triggerScenarios":"The authorization endpoint responds 200 with an incomplete payload: an error object with 200 status, truncated body, an API version that renamed fields, or a proxy stripping/rewriting the JSON.","commonSituations":"Kimi API change altering device-flow field names; captive portal/proxy returning 200 with HTML; server-side partial outage producing malformed success responses.","solutions":["Capture and inspect the raw response body to see which field is missing or renamed","Retry the device login — a transient server issue may resolve it","Verify you are hitting the official endpoint (no proxy rewriting)","Update the SDK if Kimi changed the device-flow response contract"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"// Pre-flight shape check on a raw response, mirroring the library's contract\nfunction isDeviceAuthPayload(d) {\n  return !!d && typeof d === 'object' && !!d.device_code && typeof d.user_code === 'string' &&\n    !!d.verification_uri && !!d.verification_uri_complete;\n}","typeGuard":"function isDeviceAuthResponse(d: unknown): d is { device_code: string; user_code: string; verification_uri: string; verification_uri_complete: string } {\n  const o = d as Record<string, unknown>;\n  return !!o && typeof o === 'object' && typeof o.device_code === 'string' && !!o.device_code &&\n    typeof o.user_code === 'string' && !!o.user_code &&\n    typeof o.verification_uri === 'string' && !!o.verification_uri &&\n    typeof o.verification_uri_complete === 'string' && !!o.verification_uri_complete;\n}","tryCatchPattern":"try {\n  pending = await startKimiCodingDeviceLogin();\n} catch (err) {\n  if (err instanceof Error && err.message.includes('Invalid Kimi For Coding device authorization response')) {\n    // 2xx but incomplete payload: retry once; if persistent, surface for SDK/API-version update\n    pending = await startKimiCodingDeviceLogin();\n  } else throw err;\n}","preventionTips":["Retry once on contract errors — transient server issues often produce incomplete payloads","Capture the raw body on failure to detect field renames or proxy rewriting","Ensure no middlebox rewrites JSON responses from the auth endpoint","Update the SDK when Kimi changes the device-flow response contract"],"tags":["oauth","device-flow","schema-validation","response-parsing","kimi-coding"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}