feat(ai): add Cursor OAuth authentication and builtin provider
What
Adds Cursor (Pro/Ultra/Teams) OAuth authentication as a first-class senpi auth flow with a registered builtin cursor provider. /login cursor works end-to-end: browser deep-link approval, token polling, storage in auth.json, and automatic refresh under the credential-store lock.
The provider is authentication-only for now: Cursor chat runs on a protobuf Connect-RPC agent protocol (agent.v1.AgentService) that is not ported, so the provider registers with an empty model catalog and an empty API map — nothing becomes selectable in pickers, and Models.getAuth("cursor") resolves the stored access token for integrations that speak the protocol.
Flow design (referenced implementations: can1357/oh-my-pi, router-for-me/CLIProxyAPI)
Cursor uses a browser deep-link + poll handshake instead of a device-code or loopback-callback grant:
logingenerates a PKCE S256 pair and a request uuid, then notifiesauth_urlforhttps://cursor.com/loginDeepControl?challenge&uuid&mode=login&redirectTarget=cli.- The CLI polls
https://api2.cursor.sh/auth/poll?uuid&verifierwith capped geometric backoff (1s ×1.2 up to 10s, 150 attempts). 404 = "not approved yet"; 400/401/403/410 fail fast as definitive rejections; 429 keeps polling without burning the transient budget; network errors and 5xx tolerate 3 consecutive failures (a pending poll resets the counter). refreshPOSTs the stored refresh token as a bearer toauth/exchange_user_api_keyand keeps the previous refresh token when the server does not rotate it (CLIProxyAPI's rotation-fallback pattern). Senpi'sModelsalready runs refresh under the store lock, covering CLIProxyAPI's singleflight dedupe.- Token expiry derives from the access-token JWT
expclaim minus a 5-minute skew, with a 1-hour fallback for unreadable tokens.
Improvements over the upstream oh-my-pi flow:
- Fixes a self-swallowed error bug: upstream throws its polling error inside its own
try, so a definitive 401 was caught and retried as if it were a network hiccup. Here terminal statuses fail immediately with the server detail. - Abort-aware polling: the poll sleep listens on the interaction signal (upstream's loop cannot be cancelled).
- Strict response-shape validation; error messages carry HTTP status + short server
errorstrings, never raw bodies or token material.
Changes
packages/ai/src/auth/oauth/cursor.ts(new) — the flow;load.ts+bun-oauth.tsloader registrationpackages/ai/src/providers/cursor.ts(new),providers/all.ts,types.ts(KnownProvider)packages/coding-agent: display name,defaultModelPerProvidertotality entry (inert until models exist),docs/providers.mdchanges.mdentries (packages/ai/src,packages/coding-agent/src/core) and[Unreleased]CHANGELOG entries in both packages- Tests:
packages/ai/test/cursor-oauth.test.ts(19 tests: backoff timing, terminal-vs-transient classification, abort, timeout budget, refresh rotation/failure, JWT expiry, provider registration) + existing suites extended
Validation
npm run checkgreen (biome, pinned deps, ts-imports, shrinkwrap/install locks,tsc --noEmit, browser smoke)- Full
packages/aisuite: 2054 passed / 0 failed; coding-agentmodel-resolver+ oauth-related suites green - senpi-qa evidence under
local-ignore/qa-evidence/20260816-cursor-oauth/(not committed, per policy):- Channel 3 mock loop
--self-test: 48/48 - Channel 4 CLI smoke
--self-test: 8/8 - Targeted real-CLI RPC run (sandboxed, no credentials):
get_auth_providerslists cursor as OAuth;login_start cursoremits the realcursor.com/loginDeepControldeep link with valid PKCE challenge + uuid;login_cancelaborts with"Login cancelled"before any poll request leaves the machine; no credential persisted; realauth.jsonchecksum unchanged — 7/7
- Channel 3 mock loop


Summary by cubic
Adds Cursor OAuth authentication and a builtin cursor provider. Previously Cursor sign-in was unsupported; now /login cursor opens a browser deep link, polls until approved, stores tokens in auth.json, and auto-refreshes. The provider is authentication-only and exposes no models, so pickers and routing are unchanged.
- Implements a deep-link + poll OAuth flow in
packages/ai/src/auth/oauth/cursor.ts: 404 waits; 400/401/403/410 fail fast; 429 backs off; abort-aware waits; refresh viaapi2.cursor.sh/auth/exchange_user_api_keykeeps the prior refresh token when not rotated; expiry derives from the access-token JWTexpminus 5 minutes (1-hour fallback). - Registers the flow and provider in
packages/ai:auth/oauth/load.ts,bun-oauth.ts,providers/cursor.ts,providers/all.ts, andtypes.ts(addscursor).packages/coding-agentadds the display name and a defaultcursor: "auto"entry (inert until models exist). Docs updated inREADME.md, provider docs, and QA env-var references (no env var forcursor). - Tests cover polling/backoff, abort/timeouts, refresh rotation, JWT expiry, and provider registration (
packages/ai/test/cursor-oauth.test.tsplus updates to related suites). No migration required. To validate, run/login cursor; no models appear yet.