use std::fmt;
use std::path::PathBuf;
use anyhow::anyhow;
use crate::interrupt;
pub struct FailedRun {
pub recovery: Option<Box<crate::recovery::Recovery>>,
pub error: anyhow::Error,
pub pr_url: Option<String>,
pub log: Option<PathBuf>,
pub interrupted: bool,
pub ticket_lines: Vec<String>,
}
impl FailedRun {
pub fn held(&self) -> bool {
is_hold(&self.error)
}
pub fn waiting(&self) -> bool {
self.waiting_on().is_some()
}
fn waiting_on(&self) -> Option<&crate::spec_run::TicketsNotDone> {
self.error
.downcast_ref::<crate::spec_run::TicketsNotDone>()
.filter(|not_done| !self.interrupted && not_done.only_waiting)
}
pub fn comment(&self, branch: Option<&str>) -> String {
if let Some(not_done) = self.waiting_on() {
return not_done.comment();
}
crate::recovery::comment(
self.recovery.as_deref(),
branch,
&crate::recovery::cause(&self.error),
self.pr_url.as_deref(),
crate::host::name().as_deref(),
)
}
}
pub fn category(error: &anyhow::Error) -> crate::logs::FailureCategory {
use crate::logs::FailureCategory;
if is_hold(error) {
FailureCategory::Held
} else if error
.downcast_ref::<crate::spec_run::TicketsNotDone>()
.is_some_and(|not_done| not_done.only_waiting)
{
FailureCategory::Waiting
} else if error
.chain()
.any(|layer| layer.is::<crate::harness::interpretation::SessionFailure>())
{
FailureCategory::Crashed
} else {
FailureCategory::StageFailed
}
}
pub const OUT_OF_DISK: &str =
"the host ran out of disk space; free disk space on it, then retry the Command";
pub fn out_of_disk(error: &anyhow::Error) -> bool {
let unowned = error.chain().any(|layer| {
layer.is::<crate::harness::interpretation::SessionFailure>()
|| layer.is::<crate::spec_run::TicketsNotDone>()
|| layer.is::<crate::base_fix::ChildEnded>()
});
!unowned
&& error.chain().any(|layer| {
layer
.downcast_ref::<std::io::Error>()
.is_some_and(|io| io.kind() == std::io::ErrorKind::StorageFull)
|| says_out_of_disk(&layer.to_string())
})
}
fn says_out_of_disk(text: &str) -> bool {
text.contains("No space left on device")
}
pub fn is_hold(error: &anyhow::Error) -> bool {
error.is::<PolicyRefusal>()
|| error.is::<crate::security::review::Hold>()
|| error.is::<MergeHold>()
}
#[derive(Debug)]
pub enum PolicyRefusal {
NothingToFix,
ReviewRequired { pr_url: String },
}
impl std::error::Error for PolicyRefusal {}
impl fmt::Display for PolicyRefusal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NothingToFix => f.write_str(
"the merge was refused with nothing left to fix, so the PR stays ready for review",
),
Self::ReviewRequired { pr_url } => write!(
f,
"PR {pr_url} needs an approving review before it can merge; it is ready for review"
),
}
}
}
#[derive(Debug)]
pub struct MergeHold {
pub reason: String,
}
impl std::error::Error for MergeHold {}
impl fmt::Display for MergeHold {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.reason)
}
}
impl From<anyhow::Error> for FailedRun {
fn from(error: anyhow::Error) -> Self {
let interrupted = interrupt::requested();
FailedRun {
recovery: None,
error: interrupted_or(error, interrupted),
pr_url: None,
log: None,
interrupted,
ticket_lines: Vec::new(),
}
}
}
pub fn interrupted_or(error: anyhow::Error, interrupted: bool) -> anyhow::Error {
if interrupted {
anyhow!("interrupted")
} else {
error
}
}
#[cfg(test)]
mod tests {
use anyhow::Context;
use super::*;
use crate::logs::FailureCategory;
#[test]
fn the_failure_category_comes_from_the_typed_error_not_its_words() {
let crashed = Err::<(), _>(anyhow::Error::new(
crate::harness::interpretation::SessionFailure::exited("claude", 1),
))
.context("Code review failed")
.unwrap_err();
for (error, category) in [
(
anyhow::Error::new(MergeHold {
reason: "protected path".into(),
}),
FailureCategory::Held,
),
(
anyhow::Error::new(PolicyRefusal::NothingToFix),
FailureCategory::Held,
),
(
anyhow::Error::new(crate::spec_run::TicketsNotDone::waiting()),
FailureCategory::Waiting,
),
(crashed, FailureCategory::Crashed),
(
anyhow!("held: claude exited 1"),
FailureCategory::StageFailed,
),
] {
assert_eq!(super::category(&error), category, "{error:#}");
}
}
#[test]
fn running_out_of_disk_is_recognised_anywhere_in_the_cause_chain() {
let full = anyhow::Error::new(std::io::Error::from_raw_os_error(libc::ENOSPC))
.context("could not write Session trace");
let git = anyhow!(
"git write-tree failed: error: unable to write loose object file: No space left on \
device"
)
.context("could not push the failed ticket run's work");
assert!(out_of_disk(&full), "{full:#}");
assert!(out_of_disk(&git), "{git:#}");
let other = anyhow::Error::new(std::io::Error::from_raw_os_error(libc::EACCES))
.context("could not write Session trace");
assert!(!out_of_disk(&other), "{other:#}");
assert!(!out_of_disk(&anyhow!("claude exited 1")));
}
#[test]
fn an_agents_diagnostic_or_a_spec_runs_tickets_naming_a_full_disk_are_not_the_hosts() {
let mut session = crate::harness::interpretation::SessionFailure::exited("claude", 1);
session.diagnostic = Some("npm test failed: ENOSPC: No space left on device".into());
let session = anyhow::Error::new(session).context("Ticket session failed");
assert!(!out_of_disk(&session), "{session:#}");
let mut tickets = crate::spec_run::TicketsNotDone::waiting();
tickets.only_waiting = false;
let tickets = anyhow::Error::new(tickets)
.context("#3 (failed: git worktree add failed: No space left on device)");
assert!(!out_of_disk(&tickets), "{tickets:#}");
}
}