swarm-review-classifier: a claude that exits non-zero reports the error it printed
This commit is contained in:
parent
1302965ca7
commit
6c1d670a57
1 changed files with 79 additions and 2 deletions
|
|
@ -320,7 +320,7 @@ impl Claude {
|
||||||
tokio::pin!(wait);
|
tokio::pin!(wait);
|
||||||
match tokio::time::timeout(deadline, &mut wait).await {
|
match tokio::time::timeout(deadline, &mut wait).await {
|
||||||
Ok(Ok(())) => sink.take(),
|
Ok(Ok(())) => sink.take(),
|
||||||
Ok(Err(e)) => Err(format!("claude failed: {e}")),
|
Ok(Err(e)) => Err(claude_failure(&e.to_string(), sink.reported_error())),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
cancel.cancel(true);
|
cancel.cancel(true);
|
||||||
let _ = wait.await;
|
let _ = wait.await;
|
||||||
|
|
@ -414,6 +414,33 @@ impl ResultSink {
|
||||||
.take()
|
.take()
|
||||||
.unwrap_or_else(|| Err("claude ended without a result".to_owned()))
|
.unwrap_or_else(|| Err("claude ended without a result".to_owned()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// What claude said when its `result` event was an error, without
|
||||||
|
/// consuming the outcome: the reason a run that then exited non-zero
|
||||||
|
/// failed, which the exit status alone does not carry.
|
||||||
|
fn reported_error(&self) -> Option<String> {
|
||||||
|
match &*self
|
||||||
|
.result
|
||||||
|
.lock()
|
||||||
|
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||||
|
{
|
||||||
|
Some(Err(reported)) => Some(reported.clone()),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The failure text for a claude that exited non-zero: its exit (with the
|
||||||
|
/// stderr tail it carries) and, when claude reported one, the error from its
|
||||||
|
/// `result` event. A claude that dies on an API or account error writes that
|
||||||
|
/// error to stdout and nothing to stderr, so the exit alone reads as a bare
|
||||||
|
/// status.
|
||||||
|
fn claude_failure(exit: &str, reported: Option<String>) -> String {
|
||||||
|
let exit = exit.trim_end();
|
||||||
|
match reported {
|
||||||
|
Some(reported) => format!("claude failed: {exit}; {reported}"),
|
||||||
|
None => format!("claude failed: {exit}"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sink for ResultSink {
|
impl Sink for ResultSink {
|
||||||
|
|
@ -421,7 +448,11 @@ impl Sink for ResultSink {
|
||||||
if event["type"] != "result" {
|
if event["type"] != "result" {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let text = event["result"].as_str().unwrap_or_default().to_owned();
|
let text = match event["result"].as_str().filter(|t| !t.is_empty()) {
|
||||||
|
Some(text) => text,
|
||||||
|
None => event["subtype"].as_str().unwrap_or_default(),
|
||||||
|
}
|
||||||
|
.to_owned();
|
||||||
let outcome = if event["is_error"].as_bool().unwrap_or(false) {
|
let outcome = if event["is_error"].as_bool().unwrap_or(false) {
|
||||||
Err(format!("claude reported an error: {text}"))
|
Err(format!("claude reported an error: {text}"))
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -522,6 +553,52 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn sink_after(event: &serde_json::Value) -> ResultSink {
|
||||||
|
let sink = ResultSink::default();
|
||||||
|
sink.on_event(event);
|
||||||
|
sink
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn an_error_result_is_what_a_nonzero_exit_reports() {
|
||||||
|
let sink = sink_after(&serde_json::json!({
|
||||||
|
"type": "result", "is_error": true,
|
||||||
|
"result": "Credit balance is too low",
|
||||||
|
}));
|
||||||
|
let reported = sink.reported_error();
|
||||||
|
assert_eq!(
|
||||||
|
claude_failure("claude exited exit status: 1\n", reported),
|
||||||
|
"claude failed: claude exited exit status: 1; claude reported an error: \
|
||||||
|
Credit balance is too low"
|
||||||
|
);
|
||||||
|
// Reading it does not consume the outcome.
|
||||||
|
assert!(sink.take().is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn an_error_result_without_text_names_its_subtype() {
|
||||||
|
let sink = sink_after(&serde_json::json!({
|
||||||
|
"type": "result", "is_error": true, "subtype": "error_during_execution",
|
||||||
|
}));
|
||||||
|
assert_eq!(
|
||||||
|
sink.reported_error().as_deref(),
|
||||||
|
Some("claude reported an error: error_during_execution")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn no_reported_error_leaves_the_exit_alone() {
|
||||||
|
let ok = sink_after(&serde_json::json!({
|
||||||
|
"type": "result", "is_error": false, "result": "{}",
|
||||||
|
}));
|
||||||
|
assert_eq!(ok.reported_error(), None);
|
||||||
|
assert_eq!(ResultSink::default().reported_error(), None);
|
||||||
|
assert_eq!(
|
||||||
|
claude_failure("claude exited exit status: 1\nsome stderr", None),
|
||||||
|
"claude failed: claude exited exit status: 1\nsome stderr"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn chain_renders_every_level() {
|
fn chain_renders_every_level() {
|
||||||
let e = Level(
|
let e = Level(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue