{"record":{"id":"da58c9253523599d","repo":"mastra-ai/mastra","slug":"kimi-for-coding-credentials-are-invalid-please-re","errorCode":null,"errorMessage":"Kimi For Coding credentials are invalid. Please reconnect the account.","messagePattern":"Kimi For Coding credentials are invalid\\. Please reconnect the account\\.","errorType":"exception","errorClass":"ProviderAuthRequiredError","httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/providers/kimi-coding.ts","lineNumber":35,"sourceCode":"export function setAuthStorage(storage: AuthStorage | undefined): void {\n  authStorageInstance = storage ?? null;\n}\n\nfunction getAuthStorage(): AuthStorage {\n  if (!authStorageInstance) authStorageInstance = new AuthStorage();\n  return authStorageInstance;\n}\n\nexport function buildKimiCodingOAuthFetch(options: { credentialStore?: CredentialStore } = {}): typeof fetch {\n  return (async (input: string | URL | Request, init?: Parameters<typeof fetch>[1]) => {\n    const store = options.credentialStore ?? getAuthStorage();\n    store.reload();\n    const credential = store.get(PROVIDER_ID);\n    if (!credential || credential.type !== 'oauth') {\n      throw new ProviderAuthRequiredError('Not logged in to Kimi For Coding.');\n    }\n    if (!isKimiCodingDeviceId(credential.deviceId)) {\n      throw new ProviderAuthRequiredError('Kimi For Coding credentials are invalid. Please reconnect the account.');\n    }\n    const deviceHeaders = getKimiCodingDeviceHeaders(credential.deviceId);\n    const token = await store.getApiKey(PROVIDER_ID);\n    if (!token) throw new ProviderAuthRequiredError('Failed to refresh the Kimi For Coding token.');\n\n    const headers = new Headers(input instanceof Request ? input.headers : undefined);\n    if (init?.headers) new Headers(init.headers).forEach((value, key) => headers.set(key, value));\n    for (const [key, value] of Object.entries(deviceHeaders)) headers.set(key, value);\n    headers.delete('authorization');\n    headers.delete('x-api-key');\n    headers.set('Authorization', `Bearer ${token}`);\n    return fetch(input, { ...init, headers });\n  }) as typeof fetch;\n}\n\nexport function buildKimiCodingApiKeyFetch(apiKey: string): typeof fetch {\n  return (async (input: string | URL | Request, init?: Parameters<typeof fetch>[1]) => {\n    const headers = new Headers(input instanceof Request ? input.headers : undefined);","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/providers/kimi-coding.ts#L17-L53","documentation":"After finding an oauth credential for 'kimi-for-coding', buildKimiCodingOAuthFetch validates the stored deviceId with isKimiCodingDeviceId. The deviceId is required to build the device headers the Kimi API expects. If the deviceId is missing, malformed, or fails validation, it throws ProviderAuthRequiredError telling the user to reconnect the account.","triggerScenarios":"A request through the Kimi For Coding OAuth fetch where store.get('kimi-for-coding') returns an oauth credential whose deviceId is undefined/null or does not match the expected device-id format (isKimiCodingDeviceId returns false).","commonSituations":"Partially written or hand-edited credentials file; a schema change in an SDK version that altered the stored credential shape; credentials synced from another tool that omit the deviceId; corrupted storage.","solutions":["Re-run the Kimi For Coding OAuth login to rewrite the credential with a valid deviceId.","Delete the corrupt 'kimi-for-coding' credential entry and log in again from scratch.","Inspect the stored credential JSON to confirm deviceId is present and correctly formatted.","If migrating SDK versions, clear old credentials and reconnect rather than carrying them over."],"exampleFix":"// before (corrupt stored credential)\n{ \"id\": \"kimi-for-coding\", \"type\": \"oauth\" } // missing deviceId\n// after: reconnect\nawait kimiForCodingLogin();\n// stores { \"id\": \"kimi-for-coding\", \"type\": \"oauth\", \"deviceId\": \"<valid-id>\", ... }","handlingStrategy":"validation","validationCode":"import { isKimiCodingDeviceId } from '@mastra/code-sdk';\nconst cred = store.get('kimi-for-coding');\nif (cred?.type === 'oauth' && !isKimiCodingDeviceId(cred.deviceId)) {\n  await kimiForCodingLogin(); // credential is corrupt; reconnect\n}","typeGuard":"function hasValidKimiCred(c: unknown): c is { type: 'oauth'; deviceId: string } {\n  return !!c && typeof c === 'object' && (c as any).type === 'oauth' &&\n    typeof (c as any).deviceId === 'string' && (c as any).deviceId.length > 0;\n}","tryCatchPattern":"try {\n  return await fetchWithAuth(input, init);\n} catch (e) {\n  if (e instanceof ProviderAuthRequiredError && /reconnect/.test(e.message)) {\n    await kimiForCodingLogin(); // full reconnect rewrites the credential\n    return await fetchWithAuth(input, init);\n  }\n  throw e;\n}","preventionTips":["Never hand-edit the credentials file; use the SDK login flow.","Validate stored credentials after version upgrades or migrations.","Delete and re-create the credential if its shape looks unexpected."],"tags":["auth","oauth","kimi","corrupt-credentials"],"backgroundTag":"invalid-oauth-credentials","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}