fix(cli): return failure for interrupted headless runs (#11938)

This commit is contained in:
Lifei Zhou
2026-09-10 02:52:16 +00:00
committed by GitHub
parent bded379069
commit bea9954b93
+91 -5
View File
@@ -1545,6 +1545,7 @@ impl CliSession {
let run_started = Instant::now();
let mut first_token_at: Option<Instant> = None;
let mut last_usage: Option<ProviderUsage> = None;
let mut stream_error = None;
use futures::StreamExt;
loop {
@@ -1707,7 +1708,9 @@ impl CliSession {
self.messages = updated_conversation;
}
Some(Err(e)) => {
handle_agent_error(&e, is_stream_json_mode);
if interactive || !is_stream_json_mode {
handle_agent_error(&e, is_stream_json_mode);
}
cancel_token_clone.cancel();
drop(stream);
if let Err(e) = self.handle_interrupted_messages(false).await {
@@ -1720,6 +1723,7 @@ impl CliSession {
- depending on the error you may be able to continue",
);
}
stream_error = Some(e);
break;
}
None => break,
@@ -1735,11 +1739,30 @@ impl CliSession {
}
}
let terminal_error = headless_run_error(
interactive,
cancel_token_clone.is_cancelled(),
stream_error,
&self.messages,
);
if is_stream_json_mode {
if let Some(error) = &terminal_error {
emit_stream_event(&StreamEvent::Error {
error: error.to_string(),
});
}
}
if !is_json_mode && !is_stream_json_mode {
output::flush_markdown_buffer_current_theme(&mut markdown_buffer);
}
if is_json_mode {
let status = if terminal_error.is_some() {
"error"
} else {
"completed"
};
let metadata = match self
.agent
.config
@@ -1754,7 +1777,7 @@ impl CliSession {
cache_read_input_tokens: totals.accumulated_usage.cache_read_input_tokens,
cache_write_input_tokens: totals.accumulated_usage.cache_write_input_tokens,
cost_usd: totals.accumulated_cost,
status: "completed".to_string(),
status: status.to_string(),
},
Err(_) => JsonMetadata {
total_tokens: None,
@@ -1763,7 +1786,7 @@ impl CliSession {
cache_read_input_tokens: None,
cache_write_input_tokens: None,
cost_usd: None,
status: "completed".to_string(),
status: status.to_string(),
},
};
let json_output = JsonOutput {
@@ -1771,7 +1794,7 @@ impl CliSession {
metadata,
};
println!("{}", serde_json::to_string_pretty(&json_output)?);
} else if is_stream_json_mode {
} else if is_stream_json_mode && terminal_error.is_none() {
let totals = self
.agent
.config
@@ -1805,7 +1828,7 @@ impl CliSession {
cache_write_input_tokens,
cost_usd,
});
} else {
} else if !is_stream_json_mode {
println!();
if self.stats {
print_run_stats(run_started, first_token_at, last_usage.as_ref());
@@ -1816,6 +1839,10 @@ impl CliSession {
output::emit_attention_bell();
}
if let Some(error) = terminal_error {
return Err(error);
}
Ok(())
}
@@ -2784,6 +2811,26 @@ fn handle_agent_error(e: &anyhow::Error, is_stream_json_mode: bool) {
}
}
fn headless_run_error(
interactive: bool,
cancelled: bool,
stream_error: Option<anyhow::Error>,
messages: &Conversation,
) -> Option<anyhow::Error> {
if interactive {
return None;
}
stream_error
.or_else(|| cancelled.then(|| anyhow::anyhow!("Headless run interrupted")))
.or_else(|| {
messages
.last()
.and_then(|message| message.content.iter().find_map(MessageContent::as_error))
.map(|error| anyhow::anyhow!(error.message.clone()))
})
}
async fn get_reasoner(
) -> Result<(Arc<dyn Provider>, goose_providers::model::ModelConfig), anyhow::Error> {
use goose::providers::create;
@@ -2851,11 +2898,50 @@ mod tests {
use super::*;
use goose::agents::extension::Envs;
use goose::config::ExtensionConfig;
use goose::conversation::message::MessageErrorKind;
use serde_json::json;
use std::collections::HashMap;
use std::time::Duration;
use test_case::test_case;
#[test]
fn only_headless_terminal_failures_are_propagated() {
let messages = Conversation::new_unvalidated([
Message::assistant().with_error(MessageErrorKind::Other, "provider failed")
]);
assert_eq!(
headless_run_error(
false,
false,
Some(anyhow::anyhow!("stream failed")),
&Conversation::default(),
)
.unwrap()
.to_string(),
"stream failed"
);
assert_eq!(
headless_run_error(false, false, None, &messages)
.unwrap()
.to_string(),
"provider failed"
);
assert_eq!(
headless_run_error(false, true, None, &Conversation::default())
.unwrap()
.to_string(),
"Headless run interrupted"
);
assert!(headless_run_error(
true,
true,
Some(anyhow::anyhow!("stream failed")),
&messages,
)
.is_none());
}
#[test]
fn provider_only_confirmation_preserves_authoritative_request() {
let arguments = json!({"command": "cat ~/.ssh/id_rsa"})