Skip to content

Commit 88d1da6

Browse files
juncursoragent
authored andcommitted
fix(claude): derive response.failed status from the classified error payload
Internal response.failed envelopes carry the classified {type, code, message} but no numeric status, and the Claude outbound defaulted every such failure to 500 — inside the transient set — so a Cursor plan/quota 429 reached Claude Code as a retryable overloaded_error and surfaced as "Repeated 529 Overloaded errors" while /api/logs correctly recorded 429. Derive the missing status with httpStatusFromTerminalError, the same mapping the request log uses, so rate-limit, auth, and invalid-request failures keep their real Anthropic error type. Unclassified synthetic tails still land on transient 5xx and map to overloaded_error as designed. Co-authored-by: Cursor <[email protected]>
1 parent 0f10e57 commit 88d1da6

2 files changed

Lines changed: 87 additions & 3 deletions

File tree

‎src/claude/outbound.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* - errors: {type:"error", error:{type,message}}; may arrive mid-stream after HTTP 200.
1212
*/
1313
import { createHash } from "node:crypto";
14+
import { httpStatusFromTerminalError } from "../lib/errors";
1415
import { isTransientUpstreamStatus } from "../lib/upstream-retry";
1516
import {
1617
isTranslatorBudgetExceededError,
@@ -555,9 +556,19 @@ export function responsesSseToAnthropicSse(
555556
}
556557
const status = code === "translation_buffer_limit"
557558
? 413
558-
: typeof error.status === "number" ? error.status : 500;
559-
// status-absent response.failed (relaySseWithFailedTail synthetic tail) defaults
560-
// to 500, which is in the transient set — the mid-stream reset shape maps to
559+
: typeof error.status === "number"
560+
? error.status
561+
// Internal response.failed envelopes carry the classified {type, code, message}
562+
// but no numeric status. Derive it with the same mapping /api/logs uses so a
563+
// classified 429/401/400 reaches Claude Code as its real Anthropic error type
564+
// instead of being masked as retryable overload.
565+
: httpStatusFromTerminalError({
566+
type: typeof error.type === "string" ? error.type : undefined,
567+
code: typeof error.code === "string" ? error.code : null,
568+
message,
569+
});
570+
// Unclassified status-absent response.failed (relaySseWithFailedTail synthetic
571+
// tail) still lands on a transient 5xx here — the mid-stream reset shape maps to
561572
// overloaded_error by design.
562573
fail(
563574
status,

‎tests/claude-outbound.test.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,79 @@ describe("claude outbound SSE", () => {
534534
});
535535
});
536536

537+
// Internal response.failed envelopes carry the classified {type, code, message} but no
538+
// numeric status (adapterFailureFromEvent drops httpStatus at the wire boundary). The
539+
// classified status must be derived the same way /api/logs derives it, or every classified
540+
// failure is masked as retryable overloaded_error — a Cursor plan/quota 429 surfaced in
541+
// Claude Code as "Repeated 529 Overloaded errors".
542+
test("failed with classified rate_limit_error but NO numeric status -> rate_limit_error, not overloaded", async () => {
543+
const upstream = [
544+
sse("response.created", { response: {} }),
545+
sse("response.failed", {
546+
response: {
547+
status: "failed",
548+
error: { type: "rate_limit_error", code: "rate_limit_exceeded", message: "Cursor rate limit exceeded: quota exhausted" },
549+
},
550+
}),
551+
].join("");
552+
const events = await collectEvents(responsesSseToAnthropicSse(streamFrom(upstream), "m"));
553+
expect(events.at(-1)!.data).toEqual({
554+
type: "error",
555+
error: { type: "rate_limit_error", message: "Cursor rate limit exceeded: quota exhausted" },
556+
});
557+
});
558+
559+
test("failed with classified authentication_error but NO numeric status -> authentication_error, not overloaded", async () => {
560+
const upstream = [
561+
sse("response.created", { response: {} }),
562+
sse("response.failed", {
563+
response: {
564+
status: "failed",
565+
error: { type: "authentication_error", code: "invalid_api_key", message: "Cursor authentication failed: expired token" },
566+
},
567+
}),
568+
].join("");
569+
const events = await collectEvents(responsesSseToAnthropicSse(streamFrom(upstream), "m"));
570+
expect(events.at(-1)!.data).toEqual({
571+
type: "error",
572+
error: { type: "authentication_error", message: "Cursor authentication failed: expired token" },
573+
});
574+
});
575+
576+
test("failed with classified invalid_request_error but NO numeric status -> invalid_request_error, not overloaded", async () => {
577+
const upstream = [
578+
sse("response.created", { response: {} }),
579+
sse("response.failed", {
580+
response: {
581+
status: "failed",
582+
error: { type: "invalid_request_error", code: "context_length_exceeded", message: "Cursor context limit exceeded" },
583+
},
584+
}),
585+
].join("");
586+
const events = await collectEvents(responsesSseToAnthropicSse(streamFrom(upstream), "m"));
587+
expect(events.at(-1)!.data).toEqual({
588+
type: "error",
589+
error: { type: "invalid_request_error", message: "Cursor context limit exceeded" },
590+
});
591+
});
592+
593+
test("failed with classified server overload but NO numeric status -> still overloaded_error", async () => {
594+
const upstream = [
595+
sse("response.created", { response: {} }),
596+
sse("response.failed", {
597+
response: {
598+
status: "failed",
599+
error: { type: "server_error", code: "server_is_overloaded", message: "Cursor server overloaded: unavailable" },
600+
},
601+
}),
602+
].join("");
603+
const events = await collectEvents(responsesSseToAnthropicSse(streamFrom(upstream), "m"));
604+
expect(events.at(-1)!.data).toEqual({
605+
type: "error",
606+
error: { type: "overloaded_error", message: "Cursor server overloaded: unavailable" },
607+
});
608+
});
609+
537610
test("internal reader exception stays api_error (not promoted to overloaded)", async () => {
538611
const boom = new ReadableStream<Uint8Array>({
539612
start(controller) {

0 commit comments

Comments
 (0)