Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(agents): retry a turn Cursor could not start (#6770)
Wolf Mermelsteinb8b6e879bab8
diff --git a/crates/cursor_cloud_agents/src/api.rs b/crates/cursor_cloud_agents/src/api.rsindex bbd74fd791..f50db8ff70 100644--- a/crates/cursor_cloud_agents/src/api.rs+++ b/crates/cursor_cloud_agents/src/api.rs@@ -200,6 +200,36 @@ const BRANCH_UNVERIFIABLE_RETRY_BACKOFF: [std::time::Duration; 2] = [std::time::Duration::from_secs(5),];+/// How long to wait before each retry of a POST that starts a turn; its+/// length is the retry budget.+///+/// Sized against the one limit we have watched a turn die on — GitHub+/// throttling Cursor's token mint, asking for sixty seconds (prod,+/// 2026-09-22). Forty-two seconds of asking does not outlast that, and is+/// not meant to: a turn that sits silent for a full minute is its own kind+/// of broken. It covers a limit that clears early, a gateway restarting,+/// and every other transient that is measured in seconds.+const TURN_START_RETRY_BACKOFF: [std::time::Duration; 3] = [+ std::time::Duration::from_secs(2),+ std::time::Duration::from_secs(10),+ std::time::Duration::from_secs(30),+];++/// Whether a failed POST is worth simply asking again.+///+/// Status alone, deliberately. Every provider states its transients in its+/// own dialect — Cursor's 429 arrives as a `connect` envelope wrapping a+/// base64 blob — and a classifier that reads bodies is a classifier that+/// goes stale the first time one changes. The status codes do not: 429 and+/// 5xx mean "not now", 408 means the request never landed, and every other+/// 4xx is a fact about the request itself that an identical second POST+/// cannot change.+fn is_transient(status: reqwest::StatusCode) -> bool {+ status == reqwest::StatusCode::TOO_MANY_REQUESTS+ || status == reqwest::StatusCode::REQUEST_TIMEOUT+ || status.is_server_error()+}+/// Which repository refusal, if any, a create-agent failure is.////// Matches only the documented envelope, `{"error": {"code", "message"}}`,@@ -239,6 +269,7 @@ pub struct CursorClient {/// [`BRANCH_UNVERIFIABLE_RETRY_BACKOFF`]. Overridable so a test can/// exercise the retry without sleeping through the production schedule.branch_retry_backoff: Vec<std::time::Duration>,+ turn_start_retry_backoff: Vec<std::time::Duration>,}impl CursorClient {@@ -266,6 +297,7 @@ impl CursorClient {http,config,branch_retry_backoff: BRANCH_UNVERIFIABLE_RETRY_BACKOFF.to_vec(),+ turn_start_retry_backoff: TURN_START_RETRY_BACKOFF.to_vec(),})}@@ -277,6 +309,14 @@ impl CursorClient {self}+ /// Replace the retry schedule for turn-starting POSTs. An empty schedule+ /// disables the retry.+ #[must_use]+ pub fn with_turn_start_retry_backoff(mut self, backoff: Vec<std::time::Duration>) -> Self {+ self.turn_start_retry_backoff = backoff;+ self+ }+fn url(&self, path: &str) -> String {format!("{}{path}", self.config.base_url)}@@ -326,6 +366,46 @@ impl CursorClient {Ok((status, text))}+ /// POST a request that starts a turn, riding out transient failures.+ ///+ /// Only the two calls that begin a turn take this path: minting an agent+ /// and opening a follow-up run. Both are safe to repeat, because a+ /// failed status is Cursor saying it started nothing, so a second POST+ /// cannot duplicate work on cursor.com. Cancelling and archiving keep+ /// the plain path — making a stop wait out someone else's limit is worse+ /// than the stop failing.+ ///+ /// A budget spent without success returns the last answer rather than a+ /// failure of its own, so an exhausted retry is reported exactly as the+ /// same failure is today.+ async fn post_starting_turn<Body>(+ &self,+ path: &str,+ body: &Body,+ ) -> Result<(reqwest::StatusCode, String), rootcause::Report>+ where+ Body: serde::Serialize + Sync,+ {+ let mut retries = self.turn_start_retry_backoff.iter();+ loop {+ let (status, text) = self.post_for_text(path, body).await?;+ if !is_transient(status) {+ return Ok((status, text));+ }+ let Some(delay) = retries.next() else {+ return Ok((status, text));+ };+ tracing::warn!(+ path,+ status = status.as_u16(),+ retry_in_ms = delay.as_millis() as u64,+ detail = %text,+ "cursor could not start the turn; retrying"+ );+ tokio::time::sleep(*delay).await;+ }+ }+/// Turn one POST's status and body into the reply or a report.fn decode_post<Reply>(path: &str,@@ -578,7 +658,7 @@ impl CursorAgents for CursorClient {};let mut retries = self.branch_retry_backoff.iter();let (status, text) = loop {- let (status, text) = self.post_for_text("/v1/agents", &request).await?;+ let (status, text) = self.post_starting_turn("/v1/agents", &request).await?;let Some(repo) = repo else {break (status, text);};@@ -632,9 +712,9 @@ impl CursorAgents for CursorClient {},model: model.map(ModelSelection::from),};- let reply: CreateRunResponse = self- .post_json(&format!("/v1/agents/{agent}/runs"), &request)- .await?;+ let path = format!("/v1/agents/{agent}/runs");+ let (status, text) = self.post_starting_turn(&path, &request).await?;+ let reply: CreateRunResponse = Self::decode_post(&path, status, &text)?;Ok(CursorRunId::new(reply.into_run_id()))}diff --git a/crates/cursor_cloud_agents/src/api/test.rs b/crates/cursor_cloud_agents/src/api/test.rsindex ec2e6a5cbb..1c63f13a17 100644--- a/crates/cursor_cloud_agents/src/api/test.rs+++ b/crates/cursor_cloud_agents/src/api/test.rs@@ -257,6 +257,136 @@ async fn a_branch_still_unverifiable_after_retrying_is_typed_with_the_branch() {assert_eq!(requests.try_iter().count(), 2, "one retry, then give up");}+/// The 429 Cursor answered a real create with while GitHub was throttling its+/// token mint (prod, 2026-09-22), trimmed of its base64 twin. Read by these+/// tests as a status and nothing else — which is the point.+const RATE_LIMITED_BODY: &str = r#"{"code":"resource_exhausted","message":"[resource_exhausted] Error","details":[{"debug":{"error":"ERROR_RATE_LIMITED","details":{"title":"GitHub rate limited","isRetryable":true,"additionalInfo":{"operation":"getScopedInstallationAccessKey","retryAfter":"60"}}}}]}"#;++/// A client whose turn-start schedule does not sleep, so the retry path runs+/// in test time rather than the production forty-two seconds.+fn client_with_instant_turn_retries(base_url: String, retries: usize) -> CursorClient {+ client_against(base_url).with_turn_start_retry_backoff(vec![std::time::Duration::ZERO; retries])+}++/// The statuses that mean "not now" are the whole classifier: no body is+/// read, so a provider that rephrases its errors cannot break this.+#[test]+fn only_statuses_that_mean_not_now_are_retried() {+ for status in [+ reqwest::StatusCode::TOO_MANY_REQUESTS,+ reqwest::StatusCode::REQUEST_TIMEOUT,+ reqwest::StatusCode::INTERNAL_SERVER_ERROR,+ reqwest::StatusCode::BAD_GATEWAY,+ reqwest::StatusCode::SERVICE_UNAVAILABLE,+ ] {+ assert!(is_transient(status), "{status} is worth asking again");+ }+ for status in [+ reqwest::StatusCode::BAD_REQUEST,+ reqwest::StatusCode::UNAUTHORIZED,+ reqwest::StatusCode::FORBIDDEN,+ reqwest::StatusCode::NOT_FOUND,+ reqwest::StatusCode::UNPROCESSABLE_ENTITY,+ ] {+ assert!(+ !is_transient(status),+ "{status} is a fact about the request, not the moment"+ );+ }+}++/// The limit that killed a turn in production: upstream of Cursor, clearing+/// on its own, and now waited out without the person who prompted ever+/// learning it happened.+#[tokio::test]+async fn a_rate_limited_create_is_asked_again() {+ let (base_url, requests) = stand_in_server_sequence(vec![+ ("429 Too Many Requests".to_owned(), RATE_LIMITED_BODY),+ ("200 OK".to_owned(), CREATED_BODY),+ ]);+ let (agent, _) = client_with_instant_turn_retries(base_url, 3)+ .create_agent("prompt", Some(&repo()), true, &[], None)+ .await+ .expect("the second answer is a created agent");+ assert_eq!(agent.as_str(), "bc-00000000-0000-0000-0000-000000000001");+ assert_eq!(requests.try_iter().count(), 2, "one retry, then created");+}++/// Nothing about the retry is rate-limit shaped: a gateway that fell over+/// between two prompts is the same kind of "not now".+#[tokio::test]+async fn a_failing_gateway_is_asked_again() {+ let (base_url, requests) = stand_in_server_sequence(vec![+ ("502 Bad Gateway".to_owned(), "<html>bad gateway</html>"),+ ("200 OK".to_owned(), CREATED_BODY),+ ]);+ let (agent, _) = client_with_instant_turn_retries(base_url, 3)+ .create_agent("prompt", Some(&repo()), true, &[], None)+ .await+ .expect("the second answer is a created agent");+ assert_eq!(agent.as_str(), "bc-00000000-0000-0000-0000-000000000001");+ assert_eq!(requests.try_iter().count(), 2, "one retry, then created");+}++/// A follow-up prompt mints a token the same way a create does, so it rides+/// out the same transients.+#[tokio::test]+async fn a_rate_limited_follow_up_run_is_asked_again() {+ let (base_url, requests) = stand_in_server_sequence(vec![+ ("429 Too Many Requests".to_owned(), RATE_LIMITED_BODY),+ (+ "200 OK".to_owned(),+ r#"{"id":"run-00000000-0000-0000-0000-000000000002"}"#,+ ),+ ]);+ let run = client_with_instant_turn_retries(base_url, 3)+ .create_run(+ &CursorAgentId::new("bc-00000000-0000-0000-0000-000000000001".to_owned()),+ "prompt",+ None,+ )+ .await+ .expect("the second answer is a run");+ assert_eq!(run.as_str(), "run-00000000-0000-0000-0000-000000000002");+ assert_eq!(requests.try_iter().count(), 2, "one retry, then a run");+}++/// A transient that outlasts the budget is reported as the same failure it+/// was before any of this: the retry adds attempts, never a new error.+#[tokio::test]+async fn a_transient_that_outlasts_the_budget_fails_as_it_always_did() {+ let (base_url, requests) = stand_in_server_sequence(vec![+ ("429 Too Many Requests".to_owned(), RATE_LIMITED_BODY),+ ("429 Too Many Requests".to_owned(), RATE_LIMITED_BODY),+ ]);+ let error = client_with_instant_turn_retries(base_url, 1)+ .create_agent("prompt", Some(&repo()), true, &[], None)+ .await+ .expect_err("the stand-in never relents");+ assert!(+ error+ .downcast_current_context::<crate::domain::error::PromptRejected>()+ .is_some(),+ "still the rejection a 4xx has always been"+ );+ assert_eq!(requests.try_iter().count(), 2, "one retry, then give up");+}++/// A rejection the request itself earned must still cost exactly one POST —+/// retrying a malformed model id only makes the user wait for the same no.+#[tokio::test]+async fn a_rejection_is_never_asked_again() {+ let (base_url, requests) = stand_in_server_sequence(vec![(+ "400 Bad Request".to_owned(),+ r#"{"error":{"code":"validation_error","message":"Model 'nope' does not match a known variant"}}"#,+ )]);+ client_with_instant_turn_retries(base_url, 3)+ .create_agent("prompt", Some(&repo()), true, &[], None)+ .await+ .expect_err("the stand-in rejects the create");+ assert_eq!(requests.try_iter().count(), 1, "asked exactly once");+}+/// The retry exists for one wording only. A `validation_error` about anything/// else is not asked again — a malformed model id will not fix itself.#[tokio::test]