Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(cursor): show a failed Cursor run as a notice and trace why it failed (#8867)

Wolf Mermelstein6288b0d6cc6b
diff --git a/apps/web/src/lib/service-clients/service-agent-fold/generated/types.ts b/apps/web/src/lib/service-clients/service-agent-fold/generated/types.ts
index eaf1a69972..93746c493d 100644
--- a/apps/web/src/lib/service-clients/service-agent-fold/generated/types.ts
+++ b/apps/web/src/lib/service-clients/service-agent-fold/generated/types.ts
@@ -502,7 +502,12 @@ export type FailureNoticeKind =
*/
| 'provider_usage_limit'
/** The selected model cannot read image attachments in this conversation. */
- | 'unsupported_image_input';
+ | 'unsupported_image_input'
+ /**
+ * The provider ended the run in failure; its own page for the run is
+ * where any detail it kept lives.
+ */
+ | 'provider_run_failed';
/** A file modification a tool reported. */
export type FileDiff = {
diff --git a/crates/agent_runtime_protocol/src/domain/turn.rs b/crates/agent_runtime_protocol/src/domain/turn.rs
index 9985a6a24c..24d0f1e136 100644
--- a/crates/agent_runtime_protocol/src/domain/turn.rs
+++ b/crates/agent_runtime_protocol/src/domain/turn.rs
@@ -46,6 +46,9 @@ pub enum FailureNoticeKind {
ProviderUsageLimit,
/// The selected model cannot read image attachments in this conversation.
UnsupportedImageInput,
+ /// The provider ended the run in failure; its own page for the run is
+ /// where any detail it kept lives.
+ ProviderRunFailed,
}
/// An external page where the person can act on a [`FailureNotice`].
diff --git a/crates/cursor_cloud_agents/src/domain/error.rs b/crates/cursor_cloud_agents/src/domain/error.rs
index 6e026a0298..5ce391518b 100644
--- a/crates/cursor_cloud_agents/src/domain/error.rs
+++ b/crates/cursor_cloud_agents/src/domain/error.rs
@@ -1,6 +1,6 @@
//! Errors the session service can produce.
-use crate::domain::model::RepoUrl;
+use crate::domain::model::{CursorAgentId, RepoUrl, RunStatus};
use agent_client_protocol::schema::v1::SessionId;
pub use agent_runtime_protocol::domain::turn::{FailureLink, FailureNotice, FailureNoticeKind};
use thiserror::Error;
@@ -132,6 +132,53 @@ impl UsageLimitExceeded {
}
}
+/// A run Cursor ended in neither `FINISHED` nor `CANCELLED`.
+///
+/// Cursor's API carries no reason for a failed run: the run record and the
+/// stream's `result` have only the status, and the stream's `error` events
+/// seen in production are all about the stream itself. The run's page on
+/// cursor.com is the one place that says why, so the notice sends the person
+/// there. Before this the turn failed with a raw report naming a source line.
+#[derive(Debug, Error)]
+#[error("cursor run ended in {status:?}")]
+pub struct RunFailed {
+ /// The agent whose run failed, for the link to Cursor's page for it.
+ pub agent: CursorAgentId,
+ /// The terminal status Cursor reported.
+ pub status: RunStatus,
+}
+
+impl RunFailed {
+ /// Where Cursor shows an agent's runs; the agent id follows.
+ pub const AGENT_PAGE_URL: &'static str = "https://cursor.com/agents/";
+
+ /// What to show the person who sent the prompt.
+ #[must_use]
+ pub fn notice(&self) -> FailureNotice {
+ FailureNotice {
+ kind: FailureNoticeKind::ProviderRunFailed,
+ title: "Cursor run failed".to_owned(),
+ body: "Cursor stopped this run with an error. Open the run in Cursor to see why, \
+ then send another message to continue."
+ .to_owned(),
+ link: Some(FailureLink {
+ label: "Open in Cursor".to_owned(),
+ url: format!("{}{}", Self::AGENT_PAGE_URL, self.agent),
+ }),
+ }
+ }
+
+ /// The failure as the `session/prompt` error carries it.
+ #[must_use]
+ pub fn refusal(&self) -> PromptRefusal {
+ let notice = self.notice();
+ PromptRefusal {
+ message: format!("{} {}", notice.title, notice.body),
+ notice: Some(Box::new(notice)),
+ }
+ }
+}
+
/// A provider response proving a prompt was rejected before execution.
#[derive(Debug, Error)]
#[error("{0}")]
diff --git a/crates/cursor_cloud_agents/src/domain/service.rs b/crates/cursor_cloud_agents/src/domain/service.rs
index 76fae84bff..238ca5473c 100644
--- a/crates/cursor_cloud_agents/src/domain/service.rs
+++ b/crates/cursor_cloud_agents/src/domain/service.rs
@@ -39,7 +39,7 @@
mod test;
use crate::domain::artifact::{ArtifactListing, CollectedArtifact, inline_text, mime_type};
-use crate::domain::error::{PromptRefusal, SessionError};
+use crate::domain::error::{PromptRefusal, RunFailed, SessionError};
use crate::domain::event::CursorEvent;
use crate::domain::journal::{CursorJournal, JournalEntry, JournalInput, ReplayMachine};
use crate::domain::model::{
@@ -319,6 +319,57 @@ fn captured_content(
.collect()
}
+/// The turn's failure for a run Cursor ended in neither finished nor
+/// cancelled.
+///
+/// The span and the log keep everything Cursor said about the run: its
+/// terminal status and the last `error` event journaled for it. The person
+/// gets a notice pointing at the run's page, the only place the reason is.
+/// Read from the journal rather than the stream loop so a run whose terminal
+/// status was recovered after a restart is reported the same way.
+fn run_failure(
+ session: &Session,
+ agent: &CursorAgentId,
+ run: &CursorRunId,
+ status: RunStatus,
+) -> SessionError {
+ let reported = session
+ .state
+ .lock()
+ .expect("session state poisoned")
+ .journal_entries
+ .iter()
+ .rev()
+ .filter(|entry| entry.run.as_ref() == Some(run))
+ .find_map(|entry| match &entry.input {
+ JournalInput::Sse(record) => match record.decode() {
+ CursorEvent::Error { code, message } => Some((code, message)),
+ _ => None,
+ },
+ _ => None,
+ });
+ let (code, message) = reported.unzip();
+ let code = code.flatten();
+ let span = tracing::Span::current();
+ span.record("cursor.run.error.code", code.as_deref());
+ span.record("cursor.run.error.message", message.as_deref());
+ tracing::warn!(
+ %agent,
+ cursor.run.id = %run,
+ cursor.run.status = ?status,
+ cursor.run.error.code = code.as_deref(),
+ cursor.run.error.message = message.as_deref(),
+ "Cursor ended the run in failure"
+ );
+ SessionError::Rejected(
+ RunFailed {
+ agent: agent.clone(),
+ status,
+ }
+ .refusal(),
+ )
+}
+
/// Restate a rejection the person who prompted can act on - a repository
/// Cursor cannot reach, a Cursor account out of budget - in their terms,
/// leaving every other failure exactly as it arrived.
@@ -1554,6 +1605,9 @@ where
cursor.ingest.emit = ?mode.emit,
cursor.ingest.strict = mode.strict,
cursor.ingest.attempt = mode.attempt,
+ cursor.run.status = tracing::field::Empty,
+ cursor.run.error.code = tracing::field::Empty,
+ cursor.run.error.message = tracing::field::Empty,
cursor.stream.reconnects = tracing::field::Empty,
cursor.stream.fell_back_to_poll = tracing::field::Empty,
),
@@ -1583,6 +1637,7 @@ where
.machine
.terminal_status(run);
if let Some(status) = terminal {
+ tracing::Span::current().record("cursor.run.status", tracing::field::debug(&status));
if strict
&& !session
.state
@@ -1607,7 +1662,7 @@ where
RunStatus::Cancelled => Ok(StopReason::Cancelled),
RunStatus::Finished => Ok(StopReason::EndTurn),
_ if strict => Ok(StopReason::EndTurn),
- status => Err(rootcause::report!("cursor run {run} ended in {status:?}").into()),
+ status => Err(run_failure(session, agent, run, status)),
};
}
let mut captured = captured_content(session, run);
@@ -1890,7 +1945,15 @@ where
}
// Cursor said its piece about this run; a fresh
// connection would only be told the same thing.
- CursorEvent::Error { .. } => break None,
+ CursorEvent::Error { code, message } => {
+ tracing::warn!(
+ cursor.run.id = %run,
+ cursor.run.error.code = code,
+ cursor.run.error.message = %message,
+ "Cursor stream reported an error"
+ );
+ break None;
+ }
CursorEvent::Done => break None,
_ => {}
}
@@ -2065,6 +2128,9 @@ where
sleep_unless_cancelled(cancel, poll_delay(attempt)).await;
}
}
+ if let Some(status) = &terminal {
+ span.record("cursor.run.status", tracing::field::debug(status));
+ }
let Some(status) = terminal else {
// Not a failure to describe in provider terms: the run is still
// going as far as Cursor is concerned, and this turn has simply
@@ -2111,7 +2177,7 @@ where
match status {
RunStatus::Finished => Ok(StopReason::EndTurn),
RunStatus::Cancelled => Ok(StopReason::Cancelled),
- status => Err(rootcause::report!("cursor run {run} ended in {status:?}").into()),
+ status => Err(run_failure(session, agent, run, status)),
}
}
diff --git a/crates/cursor_cloud_agents/src/domain/service/test.rs b/crates/cursor_cloud_agents/src/domain/service/test.rs
index 94f7a7c13d..3c12f5ac7a 100644
--- a/crates/cursor_cloud_agents/src/domain/service/test.rs
+++ b/crates/cursor_cloud_agents/src/domain/service/test.rs
@@ -1,5 +1,7 @@
use super::*;
-use crate::domain::error::{PromptRefusal, SessionError};
+use crate::domain::error::{
+ FailureLink, FailureNotice, FailureNoticeKind, PromptRefusal, SessionError,
+};
use crate::domain::event::{CursorEvent, InteractionUpdate, ToolCallEvent, Truncation};
use crate::domain::journal::NativeRecord;
use crate::domain::model::{
@@ -647,12 +649,22 @@ async fn an_error_run_status_fails_the_turn() {
.expect("stream open");
events.send(CursorEvent::Done).expect("stream open");
- assert!(
- matches!(
- service.prompt(&session, "hi").await,
- Err(SessionError::Cursor(_))
- ),
- "a run that ended in Error must not report a stop reason"
+ let Err(SessionError::Rejected(refusal)) = service.prompt(&session, "hi").await else {
+ panic!("a run that ended in Error must not report a stop reason");
+ };
+ assert_eq!(
+ refusal.notice.map(|notice| *notice),
+ Some(FailureNotice {
+ kind: FailureNoticeKind::ProviderRunFailed,
+ title: "Cursor run failed".to_owned(),
+ body: "Cursor stopped this run with an error. Open the run in Cursor to see why, \
+ then send another message to continue."
+ .to_owned(),
+ link: Some(FailureLink {
+ label: "Open in Cursor".to_owned(),
+ url: "https://cursor.com/agents/bc-fake".to_owned(),
+ }),
+ })
);
}
diff --git a/packages/sdk/generated/agent-fold/index.ts b/packages/sdk/generated/agent-fold/index.ts
index 5c8d37ca7c..4fcbe4bdf2 100644
--- a/packages/sdk/generated/agent-fold/index.ts
+++ b/packages/sdk/generated/agent-fold/index.ts
@@ -456,7 +456,12 @@ export type FailureNoticeKind =
*/
"provider_usage_limit" |
/** The selected model cannot read image attachments in this conversation. */
-"unsupported_image_input";
+"unsupported_image_input" |
+/**
+ * The provider ended the run in failure; its own page for the run is
+ * where any detail it kept lives.
+ */
+"provider_run_failed";
/** A file modification a tool reported. */
export type FileDiff = {