{"record":{"id":"bf4734cd3198586a","repo":"mastra-ai/mastra","slug":"kimi-for-coding-credentials-have-an-invalid-device","errorCode":null,"errorMessage":"Kimi For Coding credentials have an invalid device ID. Please reconnect the account.","messagePattern":"Kimi For Coding credentials have an invalid device ID\\. Please reconnect the account\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/auth/providers/kimi-coding.ts","lineNumber":44,"sourceCode":"const KIMI_DEVICE_DETAILS = {\n  'X-Msh-Platform': 'mastracode',\n  'X-Msh-Version': asciiHeaderValue(getCurrentVersion()),\n  'X-Msh-Device-Name': asciiHeaderValue(hostname()),\n  'X-Msh-Device-Model': asciiHeaderValue(`${platform()} ${arch()}`),\n  'X-Msh-Os-Version': asciiHeaderValue(release()),\n};\n\nexport function createKimiCodingDeviceId(): string {\n  return randomUUID().replaceAll('-', '');\n}\n\nexport function isKimiCodingDeviceId(value: unknown): value is string {\n  return typeof value === 'string' && DEVICE_ID_PATTERN.test(value);\n}\n\nexport function getKimiCodingDeviceHeaders(deviceId: string): Record<string, string> {\n  if (!isKimiCodingDeviceId(deviceId)) {\n    throw new Error('Kimi For Coding credentials have an invalid device ID. Please reconnect the account.');\n  }\n  return { ...KIMI_DEVICE_DETAILS, 'X-Msh-Device-Id': deviceId };\n}\n\nfunction requestSignal(signal?: AbortSignal): AbortSignal {\n  const timeout = AbortSignal.timeout(REQUEST_TIMEOUT_MS);\n  return signal ? AbortSignal.any([timeout, signal]) : timeout;\n}\n\nfunction trustedHttpUrl(value: unknown): string | null {\n  if (typeof value !== 'string' || !value) return null;\n  try {\n    const url = new URL(value);\n    return url.protocol === 'https:' ? url.href : null;\n  } catch {\n    return null;\n  }\n}","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/auth/providers/kimi-coding.ts#L26-L62","documentation":"getKimiCodingDeviceHeaders builds the device headers (including X-Msh-Device-Id) required by Kimi For Coding, but first validates the stored device ID against DEVICE_ID_PATTERN. If the persisted deviceId fails that pattern (wrong shape/characters/length), the library throws and asks the user to reconnect, because a malformed device ID would be rejected server-side anyway.","triggerScenarios":"Loading Kimi For Coding credentials whose deviceId field is corrupted, empty, manually edited, produced by an older/buggy SDK version, or written by a different tool with a different ID format, then calling getKimiCodingDeviceHeaders (via deviceHeaders/response paths).","commonSituations":"Hand-editing or migrating the credentials file between machines/tools; a partial or failed write during a previous login; format change of the device ID between library versions.","solutions":["Reconnect the account: re-run the Kimi For Coding device login flow to mint fresh credentials with a valid device ID","Inspect the stored credentials and delete/clear the invalid deviceId entry so a fresh one is generated","Check for concurrent writers or truncation of the credentials file; back up before migrating"],"exampleFix":"// before (corrupted stored credential)\n{\"deviceId\": \"\", \"access\": \"eyJ...\"}\n// after: delete and re-login\nawait login('kimi-coding'); // regenerates a pattern-valid device ID","handlingStrategy":"validation","validationCode":"const DEVICE_ID_PATTERN = /^[A-Za-z0-9-]+$/; // match the library's expectation loosely\nconst creds = await loadCredentials();\nif (!creds || !isKimiCodingDeviceId(creds.deviceId)) {\n  await login('kimi-coding'); // re-mint valid credentials before any request\n}","typeGuard":"function isKimiCodingDeviceId(value: unknown): value is string {\n  return typeof value === 'string' && DEVICE_ID_PATTERN.test(value);\n}","tryCatchPattern":"try {\n  headers = getKimiCodingDeviceHeaders(creds.deviceId);\n} catch (err) {\n  if (err instanceof Error && err.message.includes('invalid device ID')) {\n    await login('kimi-coding'); // reconnect to regenerate a valid device ID\n    headers = getKimiCodingDeviceHeaders(creds.deviceId);\n  } else throw err;\n}","preventionTips":["Never hand-edit or truncate the credentials file; migrate it as a whole unit","Validate stored deviceId against isKimiCodingDeviceId at load time and re-login on failure","Avoid concurrent writers to the credentials store","Keep SDK versions consistent across tools that share the credentials file"],"tags":["auth","validation","credentials","device-id","kimi-coding"],"backgroundTag":"invalid-credential-format","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}