Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/provider-cursor/src/server/CursorAgentSdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,32 @@ describe("CursorAgentSdkRunner", () => {
}).pipe(Effect.provide(layerTest)),
);
});

describe("isCursorUsageLimitError", () => {
it("recognizes codeless Cursor usage-limit messages, usage-pricing codes, and wrapped causes", () => {
for (const limitError of [
new Error("Weekly usage limit reached. It resets in 3 days. Buy credits to keep going now."),
{ message: "You've hit your monthly usage limit" },
{ message: "Your spending limit has been reached." },
{ message: "Rate limit exceeded. Please try again later." },
{ message: "Too many requests" },
{ message: "Enable usage-based pricing to continue.", code: "USAGE_PRICING_REQUIRED" },
{
message: "Enable usage-based pricing to continue.",
code: "USAGE_PRICING_REQUIRED_CHANGEABLE",
},
new Error("Run failed", {
cause: Object.assign(new Error("Quota"), { code: "PRO_USER_USAGE_LIMIT" }),
}),
]) {
assert.isTrue(CursorAgentSdk.isCursorUsageLimitError(limitError));
}

for (const nonLimitError of [
new Error("ENOENT: open /tmp/usage limit reached.txt"),
Object.assign(new Error("Usage limit reached."), { code: "internal" }),
]) {
assert.isFalse(CursorAgentSdk.isCursorUsageLimitError(nonLimitError));
}
});
});
49 changes: 41 additions & 8 deletions packages/provider-cursor/src/server/CursorAgentSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,52 @@ export function cursorSdkFailureMessage(error: CursorAgentSdkRunnerError): strin

// The SDK maps backend details to codes like PRO_USER_USAGE_LIMIT or RATE_LIMITED, without a status.
const CURSOR_USAGE_LIMIT_CODE =
/^(?:resource_exhausted|rate_limited(?:_changeable)?|\w*_usage_limit|\w*_rate_limit(?:_exceeded)?)$/i;
/^(?:resource_exhausted|rate_limited(?:_changeable)?|usage_pricing_required(?:_changeable)?|\w*_usage_limit(?:_exceeded)?|\w*_rate_limit(?:_exceeded)?)$/i;

export function isCursorUsageLimitError(cause: unknown): boolean {
const error = isCursorAgentSdkRunnerError(cause) ? cause.cause : cause;
if (typeof error !== "object" || error === null) return false;
const code = Reflect.get(error, "code");
// Local SDK runs wrap ConnectError in an internal error whose code toRunError drops,
// leaving only the backend message (for example "Weekly usage limit reached...").
const CURSOR_USAGE_LIMIT_MESSAGE =
/^\s*(?:(?:your\s+)?(?:(?:daily|weekly|monthly|free|pro|plan|team|account|api|cursor|included|on-demand)\s+){0,2}(?:usage|spend(?:ing)?|rate|requests?)\s+limits?\s+(?:(?:has|have)\s+been\s+|was\s+|is\s+)?(?:reached|exceeded)|(?:you(?:'ve|\s+have)?\s+)?(?:hit|reached|exceeded)\s+(?:your|the)\s+(?:[\w-]+\s+){0,3}(?:usage|spend(?:ing)?|rate|requests?)\s+limits?|rate[\s-]limited|too many requests)\b/i;

const GENERIC_CURSOR_USAGE_ERROR_CODES = new Set([
"",
"error",
"unknown",
"unspecified",
"custom",
"custom_message",
"provider_error",
]);

function hasGenericOrNoErrorCode(code: unknown): boolean {
return (
Reflect.get(error, "name") === "RateLimitError" ||
Reflect.get(error, "status") === 429 ||
(typeof code === "string" && CURSOR_USAGE_LIMIT_CODE.test(code))
code == null ||
(typeof code === "string" && GENERIC_CURSOR_USAGE_ERROR_CODES.has(code.toLowerCase()))
);
}

export function isCursorUsageLimitError(cause: unknown): boolean {
const seen = new Set<unknown>();
let current = isCursorAgentSdkRunnerError(cause) ? cause.cause : cause;
while (typeof current === "object" && current !== null && !seen.has(current)) {
seen.add(current);
const code = Reflect.get(current, "code");
const message = Reflect.get(current, "message");
if (
Reflect.get(current, "name") === "RateLimitError" ||
Reflect.get(current, "status") === 429 ||
(typeof code === "string" && CURSOR_USAGE_LIMIT_CODE.test(code)) ||
(hasGenericOrNoErrorCode(code) &&
typeof message === "string" &&
CURSOR_USAGE_LIMIT_MESSAGE.test(message))
) {
return true;
}
current = Reflect.get(current, "cause");
}
return false;
}

/**
* Names of the inline MCP servers a Cursor team policy rejected. The SDK refuses the whole run
* with an InlineMcpServersBlockedError, which it wraps in an UnknownAgentError.
Expand Down
Loading