cursor: a content refusal is a request error, not a sign-in (#1179)

Cursor labels a refusal of the request's content as unauthenticated ("The provider refused to serve this request based on the content"). The plugin turned that into a 401 and asked the user to sign in again, though the account serves other requests. statusOf now matches that exact wording before the sign-in rule and answers 400 with Cursor's own message; the sign-in is kept. Every other unauthenticated error still asks for a sign-in. cursor 0.2.48.

Run at the reviewed head e43311357 merged onto origin/main 2ede7d53, on a Mac:
- HOME=$(mktemp -d) bun test packages/cursor: 431 pass, 0 fail.
- bun scripts/check.mjs cursor: ok.

Broken by hand: with the new line taken out, 5 tests go red (the content-refusal cases at 200/unauthenticated, 401 and 403/permission_denied, and RunSSE with stream off and on).

Not tried: a real Cursor refusal; the cases are fixtures of the reported wording.

Co-authored-by: Xulin Wu <wuxulin@bytedance.com>
This commit is contained in:
wuuli
2026-11-15 18:12:40 +08:00
committed by GitHub
co-authored by Xulin Wu
parent 2ede7d53c1
commit 6b5c19d50e
5 changed files with 59 additions and 3 deletions
+4
View File
@@ -96,6 +96,10 @@ plugin's `fetch` answers them on Cursor's agent API:
before.
Failures keep their statuses: 401 to sign in again, 429 at the
usage limit, 400 for a prompt too long, 403 for a region refusal.
Cursor's explicit "The provider refused to serve this request based on
the content" is a 400 request refusal even when Cursor labels it
unauthenticated. Its message is kept without a sign-in hint; the
account is kept, and this does not make the refused request succeed.
Tool results can continue a retained Run in the same plugin process. The owner
uses the same session/cache key and first user message as `conversation_id`, so
+21
View File
@@ -26,6 +26,27 @@ test("the codes and words map as the built-in's cursorStatus", () => {
expect(failure(200, end("unauthenticated", "Error"))).toEqual({ status: 401, message: "unauthenticated — sign in to Cursor again in magpie" })
})
// Cursor can refuse a prompt under an authentication code. The observed
// wording identifies the request, not an expired sign-in; the surrounding
// Connect envelopes below are synthetic variants of that failure.
for (const [status, code] of [[200, "unauthenticated"], [401, ""], [403, "permission_denied"]]) {
test(`a content refusal is a request error (${status}, ${code})`, () => {
const message = "Unauthorized request.: The provider refused to serve this request based on the content"
const error = { code, message: "Error", details: [{ debug: { details: {
title: "Unauthorized request.", detail: "The provider refused to serve this request based on the content",
} } }] }
for (const body of [error, { error }]) {
expect(failure(status, JSON.stringify(body))).toEqual({ status: 400, message })
}
expect(failure(status, JSON.stringify({ code, message }))).toEqual({ status: 400, message })
})
}
test("an unrelated unauthorized request still asks for sign-in", () => {
expect(failure(200, JSON.stringify({ code: "unauthenticated", message: "Unauthorized request." })))
.toEqual({ status: 401, message: "Unauthorized request. — sign in to Cursor again in magpie" })
})
// The built-in answered any "expired" with a 401 and marked nothing, a
// session's as a trial's; nor did a success clear a mark.
test("any expired is the built-in's 401", () => {
+30 -2
View File
@@ -103,7 +103,7 @@ const httpError = (e) =>
typeof e.body === "string"
? new Response(e.body, { status: e.status, headers: { "content-type": "text/plain" } })
: new Response(JSON.stringify(e.body), { status: e.status, headers: { "content-type": "application/json" } })
function fakeAPI(base, { refuse, sseHttp, appendHttp, hangAppend } = {}) {
function fakeAPI(base, { refuse, refuseAfterText, sseHttp, appendHttp, hangAppend } = {}) {
const seen = { sse: [], appends: [] }
const streams = new Map() // request id → RunSSE's controller
globalThis.fetch = async (url, init = {}) => {
@@ -115,7 +115,10 @@ function fakeAPI(base, { refuse, sseHttp, appendHttp, hangAppend } = {}) {
const id = fields(Buffer.from(init.body).subarray(5))[0].data.toString()
seen.sse.push({ id, type: h.get("content-type"), auth: h.get("authorization"), encoding: h.get("accept-encoding") })
if (sseHttp) return httpError(sseHttp)
if (refuse) return new Response(end(JSON.stringify({ error: refuse })), { headers: { "content-type": "application/connect+proto" } })
if (refuse) return new Response(Buffer.concat([
...(refuseAfterText ? [update(1, pb().str(1, "partial answer").done())] : []),
end(JSON.stringify({ error: refuse })),
]), { headers: { "content-type": "application/connect+proto" } })
return new Response(new ReadableStream({ start: (c) => streams.set(id, c) }), { headers: { "content-type": "application/connect+proto" } })
}
if (u === "https://api2.cursor.sh/aiserver.v1.BidiService/BidiAppend") {
@@ -260,6 +263,31 @@ test("an error Cursor ends RunSSE with keeps its status", async () => {
expect(r.body.error.message).toContain("Authentication error")
})
for (const stream of [false, true]) {
test(`a content refusal through RunSSE keeps the sign-in and fails the request (stream=${stream})`, async () => {
const base = `http://127.0.0.1:${await freePort()}`
const message = "Unauthorized request.: The provider refused to serve this request based on the content"
fakeAPI(base, { refuse: { code: "unauthenticated", message }, refuseAfterText: stream })
const hooks = await CursorAuthPlugin()
const auth = fresh()
const loader = await hooks.auth.loader(async () => auth)
const response = await loader.fetch(base + "/v1/chat/completions", {
method: "POST",
body: JSON.stringify({ model: "grok-4.7-fast", stream, messages: [{ role: "user", content: "hello" }] }),
})
expect(response.headers.get("X-Magpie-Sign-In")).toBe("kept")
if (stream) {
const text = await response.text()
const errors = text.split("\n").filter((line) => line.startsWith("data: {")).map((line) => JSON.parse(line.slice(6))).filter((event) => event.error)
expect(errors).toHaveLength(1)
expect(errors[0].error).toMatchObject({ code: 400, message })
} else {
expect(response.status).toBe(400)
expect((await response.json()).error.message).toBe(message)
}
})
}
const SIGNED_OUT = { code: "unauthenticated", message: "Error", details: [{ debug: { error: "ERROR_NOT_LOGGED_IN", details: { title: "Authentication error", detail: "If you are logged in, try logging out and back in." } } }] }
// REFUSED_STREAM comes as an error on the stream; CANCEL (and NO_ERROR)
+3
View File
@@ -1686,6 +1686,9 @@ function statusOf(status, code, msg) {
const low = msg.toLowerCase()
// magpie names the provider before the message itself
const out = (status, message) => ({ status, message })
// Cursor labels this prompt refusal unauthenticated too. Asking the user
// to sign in again would misdiagnose a request error as an account fault.
if (low.includes("the provider refused to serve this request based on the content")) return out(400, msg)
if (regional(msg)) return out(403, msg + " — Cursor serves your team only in some regions and turned this request away; signing in again won't change that")
if (code === "permission_denied") return out(403, msg)
if (code === "unauthenticated" || status === 401 || low.includes("expired"))
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-cursor-auth",
"version": "0.2.47",
"version": "0.2.48",
"description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to",
"type": "module",
"main": "./opencode.mjs",