Skip to content

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

Open
surlymochan wants to merge 2 commits into
code-yeongyu:mainfrom
surlymochan:fix/2919-cursor-refresh-renewal
Open

surlymochan wants to merge 2 commits into
code-yeongyu:mainfrom
surlymochan:fix/2919-cursor-refresh-renewal

Conversation

@surlymochan

@surlymochan surlymochan commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

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.

Written for commit cc3e48a. Summary will update on new commits.

View guided diff

@code-yeongyu

Copy link
Copy Markdown
Owner

Thanks @surlymochan. One thing to settle before merge, plus two smaller notes:

  1. The kept refresh token may bring the lapse back one cycle later (cursor.ts:276 vs the docstring at :232). The docstring says the renewed session token is both the access and the refresh credential unless the response rotates the refresh token. But :276 keeps the old refresh token when refresh_token is missing, and cursor-oauth.test.ts:373-383 locks that in. If the docstring is right, the original session token stays saved as the refresh credential and expires on its original schedule, so the next renewal hits shouldLogout/4xx and the ~60-day lapse returns. Please settle which behaviour Cursor actually has (a source or a captured response shape), and save the renewed token as the refresh credential when that is the contract.
  2. Client identity (cursor.ts:23): the refresh sends the Cursor IDE's OAuth client_id, while login uses redirectTarget=cli (:129) and the transport identifies as x-cursor-client-type: cli (cursor-agent.ts:877). Can you link the source for that client_id (the upstream PR you based it on)? We don't ship requests that present another client's identity, so the maintainers will rule on this one.
  3. packages/coding-agent/docs/providers.md:249 still says Cursor tokens refresh via exchange_user_api_key; please update it, since it ships in the tarball.

Minor: the crsr_ routing (:223) depends on an unverified prefix. A different prefix fails cleanly without looping, so it's fine as is.

@code-yeongyu

Copy link
Copy Markdown
Owner

@surlymochan, a maintainer ruling on the client identity question: an OAuth refresh must present the same client_id the token was issued under. That is the protocol, not impersonation. So:

  • If our Cursor login (redirectTarget=cli) obtains its token under the same client_id as cursor.ts:23, the refresh is fine.
  • If the login uses a different client_id and the refresh switches to the IDE's, that's out.

Please show, with the login code path, which client_id issues the token (a link to the line that sets it, or a captured request). Together with the MEDIUM fix about saving the renewed token as the refresh credential, that is what's left. No faked User-Agent or version anywhere, as now.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cursor logins never renew: refresh sends the session token to the API-key exchange (port of oh-my-pi v18.8.4 fix)

2 participants