Skip to content

fix(cursor): refresh against the endpoint the IDE actually uses - #287

Merged
artempartos merged 3 commits into
developfrom
artempartos/cursor-token-refresh
Sep 17, 2026
Merged

artempartos merged 3 commits into
developfrom
artempartos/cursor-token-refresh

Conversation

@artempartos

Copy link
Copy Markdown
Contributor

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_count is 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-cli image, with the container's MITM proxy recording, then a read of the desktop IDE's bundle:

  • the IDE's _performAccessTokenRefresh posts JSON to ${backendUrl}/oauth/token where backendUrl = https://api2.cursor.sh, with its own client id — only the path in our call was right;
  • authenticator.cursor.sh is a real OAuth server (discovery advertises refresh_token and device_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;
  • the stored token is a Cursor session JWT (aud=cursor.com, type=session), which WorkOS rejects with invalid_grant — the IdP was never the place to ask;
  • the CLI itself never refreshes: both the July and September bundles carry only /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 never authenticator.cursor.sh, which the domain filter would have captured.

Confirmed live with a freshly minted credential, using this repo's own request code:

POST https://api2.cursor.sh/oauth/token      content-type: application/json
{"grant_type":"refresh_token","client_id":"…","refresh_token":"…"}
→ 200 {"access_token": <JWT, 60 days>, "id_token": <JWT>, "shouldLogout": false}

What this changes

  • The call: correct host, JSON body, the IDE's client id.
  • Both slots get the new token. The response carries no new refresh token, and the one issued at login has its own 60-day expiry — keeping it would let a credential die on schedule however often we refreshed. The IDE writes the new access token into both, which is why a signed-in IDE never signs in again; this now matches.
  • shouldLogout: true is 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_models no longer retries the model list with a logout verdict.
  • Separately: agents.credential_sync_url is derived from internal_base, like mcp.server_url and cloud.credentials_url beside it. Hardcoded, it would have posted rotated tokens to whatever answered on web in 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, shouldLogout permanent, 401 permanent vs 500 transient, model list not retried on a logout verdict. rubocop clean 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.md records 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

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]>
artempartos and others added 2 commits September 18, 2026 01:34
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]>
@artempartos

Copy link
Copy Markdown
Contributor Author

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 expires_at reads two ways: for an API-key runtime it is the truth, for one declaring expiry: :token it means nobody wrote the column — and those rows sat outside every sweep however dead they were. That is the population #222 was opened for. Refreshing settles it: success fills the column in via sync_expires_at and the row is never undetermined again, rejection condemns it on evidence. Reaches the existing rows without waiting for each user to re-authenticate.

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.

cursor_cli also declares its measured 60-day lifetime and rotation: :static: the exchange returns no new refresh token, so there is no grant family and no holder to rotate out from under another.

@artempartos
artempartos merged commit f06db76 into develop Sep 17, 2026
7 checks passed
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.

1 participant