fix(ai): renew Cursor logins through the IDE refresh grant (#2919)

SU
16d ago
+170-16

Fixes #2919

What

Every Cursor refresh sent the stored session token as a Bearer to /auth/exchange_user_api_key, an endpoint that trades dashboard user API keys — Cursor answers "Invalid User API Key" every time, so each login was going to lapse about 60 days after /login cursor while its row stayed active and retried with no hint to log in again.

This ports upstream oh-my-pi #14753 to senpi's fork-shaped cursor.ts:

  • A login session now renews the way the Cursor IDE does: POST https://api2.cursor.sh/oauth/token with { grant_type: "refresh_token", client_id: <the IDE's public client id>, refresh_token } and no Authorization header. The renewed session token is stored as both access and refresh (unless the response rotates the refresh token).
  • Cursor answers a session it will not renew with 200 and shouldLogout: true; that now throws OAuthReauthorizationRequiredError("invalid_grant") (credentialRefused: true, so the credential pool disables the row as a verdict on the login) instead of being retried as a transient failure.
  • A dashboard user API key (crsr_...) is a different credential and still trades at auth/exchange_user_api_key with the previous Bearer exchange and camelCase response. Senpi stores no other marker distinguishing a dashboard key from a login session, so the crsr_ prefix is the discriminator.
  • A 5xx refresh failure still throws OAuthTokenEndpointError with the status, which classifyOAuthRefreshFailure keeps transient.

Tests

packages/ai/test/cursor-oauth.test.ts: the refresh block was rewritten for the new endpoint and gained three cases (refresh-grant request shape, shouldLogout mapping, dashboard-key exchange).

  • Before the fix (new tests only): 4 failed / 18 passed — the old code read camelCase accessToken from a snake_case access_token response, and shouldLogout was not mapped to a reauthorization error.
  • After the fix: bun run --cwd packages/ai test test/cursor-oauth.test.ts → 22 passed / 0 failed.
  • Neighbouring regression: oauth-related suites plus models-refresh-joins-token-refresh → 10 files, 106 tests, all passed.
  • bun run check (oxlint, format, lint ratchet, tsc --noEmit over 8,789 + 3,192 files, browser smoke, lock checks) → exit 0.

Notes

  • The change fragment (changes/unreleased/<this PR number>.md, tracker block for packages/ai/src/auth/changes.md plus the packages/ai/CHANGELOG.md entry) follows in a second commit once this PR has its number.
  • Not done: the issue's live renewal check with a sanctioned test account — no such credential is available here, and real credentials must not be used for this. The renewal path is covered by fake-endpoint unit tests only.
  • CLA: to be signed by the user if the repository asks for it; not signed here.

Summary by cubic

Fixes Cursor login renewal so sessions no longer lapse about 60 days after /login cursor.

Uses the same refresh_token grant as the Cursor IDE instead of sending the session token as a Bearer to the dashboard API key exchange, which rejected it every time. A shouldLogout: true response now marks the credential as permanently refused (invalid_grant) instead of retrying it as transient, and dashboard user API keys (crsr_...) still trade through the original key exchange. Adds a user-facing changelog entry for the fix.