{"record":{"id":"2dd81ad71e59fe76","repo":"mastra-ai/mastra","slug":"refresh-failed","errorCode":null,"errorMessage":"Refresh failed","messagePattern":"Refresh failed","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/auth/client.ts","lineNumber":140,"sourceCode":"  }\n\n  const response = await fetch(input, init);\n\n  if (response.status !== 401 || !_currentToken) {\n    return response;\n  }\n\n  // Avoid multiple concurrent refreshes\n  if (!_refreshInFlight) {\n    _refreshInFlight = (async () => {\n      try {\n        // Dynamic import to avoid circular dependency\n        const { tryRefreshToken, loadCredentials } = await import('./credentials.js');\n        const creds = await loadCredentials();\n        if (!creds) throw new Error('No credentials');\n\n        const newToken = await tryRefreshToken(creds);\n        if (!newToken) throw new Error('Refresh failed');\n\n        _currentToken = newToken;\n        return newToken;\n      } finally {\n        _refreshInFlight = null;\n      }\n    })();\n  }\n\n  let newToken: string;\n  try {\n    newToken = await _refreshInFlight;\n  } catch {\n    // Refresh failed — return the original 401 response\n    return response;\n  }\n\n  // Retry with the refreshed token.","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/auth/client.ts#L122-L158","documentation":"In the same authenticatedFetch refresh path, tryRefreshToken(creds) returned a falsy value, so the CLI throws 'Refresh failed'. The refresh token itself was rejected or unusable — the stored session cannot be extended and the caller must re-authenticate interactively.","triggerScenarios":"authenticatedFetch's single-flight refresh invokes tryRefreshToken and it resolves null/undefined (refresh endpoint rejected the refresh token, returned an error, or network call failed silently), triggering `throw new Error('Refresh failed')`.","commonSituations":"Refresh token expired or revoked server-side; user logged out elsewhere invalidating the session; offline/flaky network during refresh; platform auth service rejecting an old refresh-token format after an API change.","solutions":["Run `mastra auth login` to start a fresh session (the refresh token is no longer usable).","Confirm network connectivity to the Mastra auth endpoint and retry.","If it recurs immediately, delete stored credentials and log in again.","Check for version updates to the CLI in case the auth flow changed."],"exampleFix":"// before\nmastra auth tokens list  # -> Error: Refresh failed\n// after\nmastra auth login\nmastra auth tokens list","handlingStrategy":"try-catch","validationCode":"// verify session is refreshable before dependent work\nconst { loadCredentials } = await import('./credentials.js');\nconst { tryRefreshToken } = await import('./credentials.js');\nconst creds = await loadCredentials();\nif (creds && !(await tryRefreshToken(creds))) {\n  throw new Error('Refresh token rejected — run `mastra auth login`');\n}","typeGuard":"function isRefreshFailure(err: unknown): boolean {\n  return err instanceof Error && err.message === 'Refresh failed';\n}","tryCatchPattern":"try {\n  return await platformFetch(url, init);\n} catch (err) {\n  if (isRefreshFailure(err)) {\n    console.error('Cannot refresh session. Run `mastra auth login` and retry.');\n    process.exitCode = 1;\n    return;\n  }\n  throw err;\n}","preventionTips":["Re-login periodically; refresh tokens are not permanent.","Avoid logging out / revoking sessions on one device when other devices share the session.","Ensure reliable network access to the auth endpoint during long jobs — refresh mid-run needs connectivity.","Pin/update CLI versions when the auth API changes."],"tags":["cli","auth","refresh-token","session"],"backgroundTag":"jwt-token-expired","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}