fix(ai): renew Cursor logins through the IDE refresh grant (#2919)
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/tokenwith{ grant_type: "refresh_token", client_id: <the IDE's public client id>, refresh_token }and noAuthorizationheader. 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
200andshouldLogout: true; that now throwsOAuthReauthorizationRequiredError("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 atauth/exchange_user_api_keywith the previous Bearer exchange and camelCase response. Senpi stores no other marker distinguishing a dashboard key from a login session, so thecrsr_prefix is the discriminator. - A 5xx refresh failure still throws
OAuthTokenEndpointErrorwith the status, whichclassifyOAuthRefreshFailurekeeps 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
accessTokenfrom a snake_caseaccess_tokenresponse, andshouldLogoutwas 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 --noEmitover 8,789 + 3,192 files, browser smoke, lock checks) → exit 0.
Notes
- The change fragment (
changes/unreleased/<this PR number>.md, tracker block forpackages/ai/src/auth/changes.mdplus thepackages/ai/CHANGELOG.mdentry) 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.