Repository navigation
fix(cursor): refresh against the endpoint the IDE actually uses - #287
Conversation
Our token refresh had been answering 404 since 2026-09-05, and the fault was
ours. Reading the desktop IDE's own bundle shows `_performAccessTokenRefresh`
posting JSON to `${backendUrl}/oauth/token` with `backendUrl =
https://api2.cursor.sh` and its own client id. Of the four things our call
specified, only the path was right: the host was `authenticator.cursor.sh` (a
real OAuth server, but a different one, which does not know this client), the
body was form-encoded rather than JSON, and the client id was an invented slug.
Corrected and confirmed against the live service with a freshly minted
credential: the call answers `{access_token, id_token, shouldLogout}` with a
60-day token.
Two things about the response shape matter. There is **no new refresh token**, and
the one issued at login carries its own 60-day expiry — so keeping it would let a
credential die on schedule however often we refreshed. The IDE writes the new
access token into both slots, which is what makes a signed-in IDE never need
signing in again, and this now does the same. And a 200 carrying
`shouldLogout: true` is the server ending the session, so it is reported as a
permanent failure rather than retried three times before the row is condemned.
Also here: the write-back URL is derived from `internal_base` like the MCP and
cloud-credential URLs beside it, instead of hardcoding `http://web:4002` — any
deployment whose internal host differs would have posted rotated tokens to
whatever answered there.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
A NULL `expires_at` means two different things and the sweep treated both as "leave alone". For a runtime whose credential genuinely has no expiry — an API key — that is correct. For one that declares `expiry: :token` it cannot be: the token carries an expiry by construction, so a NULL says nobody wrote the column, and the row sat outside every sweep however dead it was. That is the population behind the Cursor credentials that stayed `active` with a token months past its end while their sessions finished green, having authenticated with nothing. `refresh_due` now also selects those rows, and the refresh settles which reading is true: it succeeds and `sync_expires_at` fills the column in on the way through — so the row is never undetermined again — or it is rejected and the credential is condemned on evidence rather than on suspicion. This reaches the existing population without waiting for each user to re-authenticate, which is what the launch-gate alternative in #222 would have required. Also: `cursor_cli` declares the lifetime that is now measured (60 days) and `rotation: :static`, because the exchange returns no new refresh token — there is no grant family, so no holder can be rotated out from under another. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Artem Petrov <[email protected]>
The badge turned amber 30 minutes before expiry whatever the runtime. For Claude's 8-hour token that is a useful warning; for Cursor's 60-day one it is a notice nobody will be looking at, half an hour before a two-month credential stops working. The window is now a tenth of the lifetime the adapter declares — 48 minutes at 8 hours, 6 days at 60 days — floored at the old 30 minutes so a short-lived or undeclared token behaves exactly as before. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Artem Petrov <[email protected]>
|
Two follow-ups pushed onto this branch, both of which only make sense once the refresh works: 1. The sweep now selects credentials whose expiry was never derived. A NULL 2. "Expiring soon" is scaled to the runtime's lifetime. It was a flat 30 minutes — right for Claude's 8 hours, useless for Cursor's 60 days. Now a tenth of the declared life, floored at 30 minutes, so nothing shortens.
|
Why
CursorCliAdapter#refresh!has been answering 404 with an HTML page since at least 2026-09-05 — roughly ten times an evening, from the web process, recorded nowhere (refresh_failure_countis 0 on every cursor credential, because that path never marks one).The fault was ours. The endpoint exists; we had it wrong in three of four particulars.
How it was found
A device login inside the freshly built
cursor-cliimage, with the container's MITM proxy recording, then a read of the desktop IDE's bundle:_performAccessTokenRefreshposts JSON to${backendUrl}/oauth/tokenwherebackendUrl = https://api2.cursor.sh, with its own client id — only the path in our call was right;authenticator.cursor.shis a real OAuth server (discovery advertisesrefresh_tokenanddevice_code, token endpoint/oauth2/token), but it does not know our client id, nor the WorkOS client id cursor.com's own web login uses;aud=cursor.com,type=session), which WorkOS rejects withinvalid_grant— the IdP was never the place to ask;/auth/poll,/auth/exchange_user_api_key,/auth/cursor_dev_session_token, and the refresh policy they share consumes an API key. Six months of production HTTP logs (119 files, 2026-03-15 → 2026-09-15) show one auth endpoint —GET /auth/poll— and neverauthenticator.cursor.sh, which the domain filter would have captured.Confirmed live with a freshly minted credential, using this repo's own request code:
What this changes
shouldLogout: trueis permanent. It arrives on a 200: the server ending the session, not a transient failure. Reported as permanent so the row is condemned once instead of retried three times first.fetch_available_modelsno longer retries the model list with a logout verdict.agents.credential_sync_urlis derived frominternal_base, likemcp.server_urlandcloud.credentials_urlbeside it. Hardcoded, it would have posted rotated tokens to whatever answered onwebin a deployment whose internal host differs.Verification
Live: the request shape run through Ruby against the real endpoint — 200, new token, 60 days.
Tests (
test/services/agents/cursor_cli_adapter_test.rb): request shape asserted against the IDE's, both slots updated,shouldLogoutpermanent, 401 permanent vs 500 transient, model list not retried on a logout verdict.rubocopclean on the tree; the agent and credential suites pass locally, the rest is CI's.The lesson worth keeping
The old call was written from an OAuth template, its test stubbed our own URL, and it was green for five months while failing every night in production.
docs/design/agent-credential-lifecycle.mdrecords this under §Cursor, next to the tracked-domain work that now captures auth traffic: an adapter that talks to a vendor is not working until it has been seen working on live traffic.🤖 Generated with Claude Code