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(macrod): report a prompt stopped before delivery as not delivered (#9649)

Wolf Mermelstein55aacae8268e
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 4581616a98..23fc385751 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
@@ -799,7 +799,12 @@ export type FailureNoticeKind =
* The provider ended the run in failure; its own page for the run is
* where any detail it kept lives.
*/
- | 'provider_run_failed';
+ | 'provider_run_failed'
+ /**
+ * The runtime accepted the prompt but never handed it to the agent;
+ * the fix is to send it again.
+ */
+ | 'prompt_not_delivered';
/**
* Every filter, each over one field of one kind of item. Id lists match any
diff --git a/crates/agent_runtime_protocol/src/domain/turn.rs b/crates/agent_runtime_protocol/src/domain/turn.rs
index 5ef874a842..695a5b7045 100644
--- a/crates/agent_runtime_protocol/src/domain/turn.rs
+++ b/crates/agent_runtime_protocol/src/domain/turn.rs
@@ -49,6 +49,9 @@ pub enum FailureNoticeKind {
/// The provider ended the run in failure; its own page for the run is
/// where any detail it kept lives.
ProviderRunFailed,
+ /// The runtime accepted the prompt but never handed it to the agent;
+ /// the fix is to send it again.
+ PromptNotDelivered,
}
/// An external page where the person can act on a [`FailureNotice`].
diff --git a/crates/coding_agent_worker/README.md b/crates/coding_agent_worker/README.md
index be21e977c5..6bf3755b2f 100644
--- a/crates/coding_agent_worker/README.md
+++ b/crates/coding_agent_worker/README.md
@@ -75,7 +75,8 @@ A prompt from Macro while the native agent is working or showing a dialog
waits, like input typed into the TUI mid-turn. The running turn keeps streaming
and its permission dialogs still reach Macro; the prompt is typed once the turn
ends. Stopping from Macro while it waits interrupts the native turn and drops
-the prompt. Claude Code's exit confirmation, left by a second Ctrl-C while
+the prompt, which fails with a `prompt_not_delivered` notice so Macro shows the
+message was never typed in rather than a stopped turn. Claude Code's exit confirmation, left by a second Ctrl-C while
background work runs, is dismissed with Escape ("Stay") before the prompt is
typed.
diff --git a/crates/coding_agent_worker/src/herdr/acp_agent.rs b/crates/coding_agent_worker/src/herdr/acp_agent.rs
index 299af6b1d3..d2ca744e59 100644
--- a/crates/coding_agent_worker/src/herdr/acp_agent.rs
+++ b/crates/coding_agent_worker/src/herdr/acp_agent.rs
@@ -38,6 +38,7 @@ use agent_client_protocol::schema::v1::{
ListSessionsRequest, ListSessionsResponse, SessionId, SessionInfo,
};
use agent_fold::domain::transcript::{Fold, LogEvent};
+use agent_runtime_protocol::domain::turn::{FailureNotice, FailureNoticeKind};
use claude_fold::ClaudeLog;
use codex_fold::CodexLog;
@@ -179,6 +180,7 @@ pub(crate) struct AdapterOptions {
struct RpcError {
code: i64,
message: String,
+ data: Option<Value>,
}
impl RpcError {
@@ -186,6 +188,7 @@ impl RpcError {
Self {
code: -32603,
message: message.into(),
+ data: None,
}
}
@@ -193,6 +196,23 @@ impl RpcError {
Self {
code: -32602,
message: message.into(),
+ data: None,
+ }
+ }
+
+ /// A prompt that was stopped before it was typed into the TUI. Answering
+ /// `cancelled` would read in Macro as a message the agent saw.
+ fn not_delivered() -> Self {
+ let notice = FailureNotice {
+ kind: FailureNoticeKind::PromptNotDelivered,
+ title: "Message not delivered".to_owned(),
+ body: "The turn running in Herdr was stopped before this message was typed in, so the agent never received it. Send it again.".to_owned(),
+ link: None,
+ };
+ Self {
+ code: -32603,
+ message: "the prompt was stopped before it reached the native agent".to_owned(),
+ data: serde_json::to_value(notice).ok(),
}
}
}
@@ -375,11 +395,13 @@ impl Adapter {
fn respond(&self, id: Value, result: Result<Value, RpcError>) {
self.send(match result {
Ok(result) => json!({"jsonrpc": "2.0", "id": id, "result": result}),
- Err(error) => json!({
- "jsonrpc": "2.0",
- "id": id,
- "error": {"code": error.code, "message": error.message},
- }),
+ Err(error) => {
+ let mut body = json!({"code": error.code, "message": error.message});
+ if let Some(data) = error.data {
+ body["data"] = data;
+ }
+ json!({"jsonrpc": "2.0", "id": id, "error": body})
+ }
});
}
@@ -500,6 +522,7 @@ impl Adapter {
_ => Err(RpcError {
code: -32601,
message: format!("{method} is not supported"),
+ data: None,
}),
}
}
@@ -841,7 +864,8 @@ impl Adapter {
native_turns += 1;
tracing::Span::current().record("native_turns", native_turns);
// An interrupted native turn ends "cancelled" too; only a
- // Stop from Macro drops the waiting prompt.
+ // Stop from Macro drops the waiting prompt, which then
+ // answers as not delivered.
self.follow(herdr, session, live, &cancel, true).await?;
if cancel.is_cancelled() {
return Ok(None);
@@ -855,8 +879,8 @@ impl Adapter {
tracing::warn!(parent: &wait, error = %error.message, "queued prompt failed while waiting");
})?;
let Some(info) = waited else {
- wait.record("outcome", "cancelled");
- return Ok("cancelled");
+ wait.record("outcome", "not_delivered");
+ return Err(RpcError::not_delivered());
};
wait.record("outcome", "ready");
match info {
@@ -904,7 +928,7 @@ impl Adapter {
}
}
if cancel.is_cancelled() {
- return Ok("cancelled");
+ return Err(RpcError::not_delivered());
}
// The observer cannot acquire this session until the turn ends. Publish
// native settings now, while the freshly started composer is visible.
diff --git a/crates/coding_agent_worker/src/herdr/acp_agent/test.rs b/crates/coding_agent_worker/src/herdr/acp_agent/test.rs
index 7f5f263561..5aa7405f35 100644
--- a/crates/coding_agent_worker/src/herdr/acp_agent/test.rs
+++ b/crates/coding_agent_worker/src/herdr/acp_agent/test.rs
@@ -789,6 +789,70 @@ async fn a_prompt_to_a_busy_native_session_waits_for_its_turn_then_submits() {
adapter.shutdown.cancel();
}
+#[tokio::test]
+async fn a_stop_while_a_prompt_waits_reports_it_was_never_delivered() {
+ use std::io::Write as _;
+ let root = tempfile::tempdir().unwrap();
+ let id = uuid::Uuid::new_v4().to_string();
+ let transcript = save_transcript(root.path(), &id);
+ let (mut adapter, mut output) = test_adapter(root.path());
+ Arc::get_mut(&mut adapter).unwrap().herdr =
+ Some(queueing_herdr(root.path(), &transcript, "working"));
+ adapter
+ .load_session(&json!({"sessionId":id,"cwd":root.path()}))
+ .await
+ .unwrap();
+ let session = adapter.session(&json!({"sessionId":id})).unwrap();
+ session.prompt_pending.store(true, Ordering::SeqCst);
+ let mut file = std::fs::OpenOptions::new()
+ .append(true)
+ .open(&transcript)
+ .unwrap();
+ writeln!(
+ file,
+ "{}",
+ json!({"type":"user", "uuid":"local-user", "message":{"content":"typed in herdr"}})
+ )
+ .unwrap();
+
+ let prompt = tokio::spawn({
+ let adapter = adapter.clone();
+ async move {
+ adapter
+ .respond_to_request(
+ json!("prompt-1"),
+ "session/prompt",
+ json!({"sessionId":id, "prompt":[{"type":"text", "text":"from macro"}]}),
+ )
+ .await;
+ }
+ });
+ let mut next = async || {
+ tokio::time::timeout(Duration::from_secs(5), output.recv())
+ .await
+ .unwrap()
+ .unwrap()
+ };
+ // The prompt is waiting behind the native turn when Stop arrives.
+ assert_eq!(
+ next().await["params"]["update"]["content"]["text"],
+ "typed in herdr"
+ );
+ adapter.cancel(&json!({"sessionId":session.id}));
+ prompt.await.unwrap();
+
+ let response = next().await;
+ assert_eq!(response["id"], "prompt-1");
+ assert_eq!(response["error"]["data"]["kind"], "prompt_not_delivered");
+ let calls = std::fs::read_to_string(root.path().join("calls")).unwrap();
+ assert!(calls.contains(&format!(
+ "agent send-keys {} esc\n",
+ agent_name(&session.id)
+ )));
+ assert!(!calls.contains("agent prompt"));
+ adapter.shutdown.cancel();
+}
+
#[tokio::test]
async fn a_prompt_dismisses_the_exit_confirmation_a_second_ctrl_c_left() {
let root = tempfile::tempdir().unwrap();
diff --git a/crates/folds/agent_fold/src/domain/test/fold.rs b/crates/folds/agent_fold/src/domain/test/fold.rs
index 8e5ded4d0f..56b68936ad 100644
--- a/crates/folds/agent_fold/src/domain/test/fold.rs
+++ b/crates/folds/agent_fold/src/domain/test/fold.rs
@@ -930,6 +930,45 @@ fn a_prompt_error_carrying_a_notice_fails_its_turn_with_the_notice() {
);
}
+/// A native Herdr turn stopped while a Macro prompt waited behind it: the
+/// native turn's own `cancelled` end arrives first and must not close the
+/// prompt's turn, so the prompt's not-delivered error is what the reader sees.
+#[test]
+fn a_prompt_stopped_before_delivery_fails_with_its_notice_after_the_native_turn_ends() {
+ let log = concat!(
+ r#"{"direction":"to_runtime","content":{"type":"acp","jsonrpc":"2.0","id":"p","method":"session/prompt","params":{"sessionId":"s1","prompt":[{"type":"text","text":"merge conflict fix"}]}}}"#,
+ "\n",
+ r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"CI is still running."}}}}}"#,
+ "\n",
+ r#"{"direction":"to_runtime","content":{"type":"acp","jsonrpc":"2.0","method":"session/cancel","params":{"sessionId":"s1"}}}"#,
+ "\n",
+ r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","method":"_session/turn_complete","params":{"sessionId":"s1","outcome":{"kind":"cancelled"}}}}"#,
+ "\n",
+ r#"{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","id":"p","error":{"code":-32603,"#,
+ r#""message":"the prompt was stopped before it reached the native agent","#,
+ r#""data":{"kind":"prompt_not_delivered","title":"Message not delivered","#,
+ r#""body":"Send it again."}}}}"#,
+ );
+
+ let messages = fold(parse_log(log));
+
+ // The prompt, the native turn's output it failed on, and the Stop.
+ assert_eq!(messages.len(), 3);
+ let agent = &messages[1];
+ assert_eq!(
+ agent.stop,
+ Some(StopReason::Failed {
+ message: "the prompt was stopped before it reached the native agent".to_owned(),
+ notice: Some(FailureNotice {
+ kind: FailureNoticeKind::PromptNotDelivered,
+ title: "Message not delivered".to_owned(),
+ body: "Send it again.".to_owned(),
+ link: None,
+ }),
+ })
+ );
+}
+
/// Error `data` that is not a notice - another runtime's debugging payload -
/// leaves the failure opaque rather than half-parsed.
#[test]
diff --git a/packages/sdk/generated/agent-fold/index.ts b/packages/sdk/generated/agent-fold/index.ts
index 4c344c2f7a..39322fb89e 100644
--- a/packages/sdk/generated/agent-fold/index.ts
+++ b/packages/sdk/generated/agent-fold/index.ts
@@ -678,7 +678,12 @@ export type FailureNoticeKind =
* The provider ended the run in failure; its own page for the run is
* where any detail it kept lives.
*/
-"provider_run_failed";
+"provider_run_failed" |
+/**
+ * The runtime accepted the prompt but never handed it to the agent;
+ * the fix is to send it again.
+ */
+"prompt_not_delivered";
/**
* Every filter, each over one field of one kind of item. Id lists match any