| 1 | use serde::{Deserialize, Serialize}; |
| 2 | |
| 3 | /// Terminal reasons shared by TUI, JSONL, Fleet, and benchmark receipts. |
| 4 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 5 | #[serde(rename_all = "snake_case")] |
| 6 | pub enum RunTerminationReason { |
| 7 | Resolved, |
| 8 | Unresolved, |
| 9 | Canceled, |
| 10 | Timeout, |
| 11 | BudgetExhausted, |
| 12 | ApprovalRequired, |
| 13 | ModelError, |
| 14 | ToolError, |
| 15 | InfrastructureError, |
| 16 | } |
| 17 | |
| 18 | impl RunTerminationReason { |
| 19 | #[must_use] |
| 20 | pub const fn as_str(self) -> &'static str { |
| 21 | match self { |
| 22 | Self::Resolved => "resolved", |
| 23 | Self::Unresolved => "unresolved", |
| 24 | Self::Canceled => "canceled", |
| 25 | Self::Timeout => "timeout", |
| 26 | Self::BudgetExhausted => "budget_exhausted", |
| 27 | Self::ApprovalRequired => "approval_required", |
| 28 | Self::ModelError => "model_error", |
| 29 | Self::ToolError => "tool_error", |
| 30 | Self::InfrastructureError => "infrastructure_error", |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #[must_use] |
| 35 | pub const fn is_success(self) -> bool { |
| 36 | matches!(self, Self::Resolved) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// Reduce the existing Engine outcome plus typed subsystem evidence into the |
| 41 | /// terminal status shared by machine-facing projections. Successful and |
| 42 | /// canceled turns are unambiguous. For failed turns, the terminal error |
| 43 | /// category wins over historical tool failures from earlier, recovered steps; |
| 44 | /// approval/tool evidence is the fallback when the terminal event has no |
| 45 | /// typed cause. |
| 46 | #[must_use] |
| 47 | pub fn classify_turn_termination( |
| 48 | status: crate::core::events::TurnOutcomeStatus, |
| 49 | error_category: Option<codewhale_error_taxonomy::ErrorCategory>, |
| 50 | tool_error_seen: bool, |
| 51 | approval_required: bool, |
| 52 | ) -> RunTerminationReason { |
| 53 | use crate::core::events::TurnOutcomeStatus; |
| 54 | use codewhale_error_taxonomy::ErrorCategory; |
| 55 | |
| 56 | match status { |
| 57 | TurnOutcomeStatus::Completed => RunTerminationReason::Resolved, |
| 58 | TurnOutcomeStatus::Interrupted => RunTerminationReason::Canceled, |
| 59 | TurnOutcomeStatus::Failed => match error_category { |
| 60 | Some(ErrorCategory::Budget) => RunTerminationReason::BudgetExhausted, |
| 61 | Some(ErrorCategory::Timeout) => RunTerminationReason::Timeout, |
| 62 | Some( |
| 63 | ErrorCategory::Network |
| 64 | | ErrorCategory::Authentication |
| 65 | | ErrorCategory::Authorization |
| 66 | | ErrorCategory::RateLimit |
| 67 | | ErrorCategory::InvalidInput |
| 68 | | ErrorCategory::Parse, |
| 69 | ) => RunTerminationReason::ModelError, |
| 70 | Some(ErrorCategory::Tool) => RunTerminationReason::ToolError, |
| 71 | Some(ErrorCategory::State | ErrorCategory::Internal) => { |
| 72 | RunTerminationReason::InfrastructureError |
| 73 | } |
| 74 | None if approval_required => RunTerminationReason::ApprovalRequired, |
| 75 | None if tool_error_seen => RunTerminationReason::ToolError, |
| 76 | None => RunTerminationReason::Unresolved, |
| 77 | }, |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | #[cfg(test)] |
| 82 | mod tests { |
| 83 | use super::*; |
| 84 | |
| 85 | #[test] |
| 86 | fn failed_turns_keep_model_tool_and_infrastructure_distinct() { |
| 87 | use crate::core::events::TurnOutcomeStatus; |
| 88 | use codewhale_error_taxonomy::ErrorCategory; |
| 89 | |
| 90 | assert_eq!( |
| 91 | classify_turn_termination( |
| 92 | TurnOutcomeStatus::Failed, |
| 93 | Some(ErrorCategory::Network), |
| 94 | false, |
| 95 | false, |
| 96 | ), |
| 97 | RunTerminationReason::ModelError |
| 98 | ); |
| 99 | assert_eq!( |
| 100 | classify_turn_termination( |
| 101 | TurnOutcomeStatus::Failed, |
| 102 | Some(ErrorCategory::InvalidInput), |
| 103 | false, |
| 104 | false, |
| 105 | ), |
| 106 | RunTerminationReason::ModelError |
| 107 | ); |
| 108 | assert_eq!( |
| 109 | classify_turn_termination( |
| 110 | TurnOutcomeStatus::Failed, |
| 111 | Some(ErrorCategory::Internal), |
| 112 | false, |
| 113 | false, |
| 114 | ), |
| 115 | RunTerminationReason::InfrastructureError |
| 116 | ); |
| 117 | assert_eq!( |
| 118 | classify_turn_termination(TurnOutcomeStatus::Failed, None, true, false), |
| 119 | RunTerminationReason::ToolError |
| 120 | ); |
| 121 | assert_eq!( |
| 122 | classify_turn_termination( |
| 123 | TurnOutcomeStatus::Failed, |
| 124 | Some(ErrorCategory::InvalidInput), |
| 125 | true, |
| 126 | false, |
| 127 | ), |
| 128 | RunTerminationReason::ModelError, |
| 129 | "a recovered tool error must not hide the terminal model failure" |
| 130 | ); |
| 131 | assert_eq!( |
| 132 | classify_turn_termination( |
| 133 | TurnOutcomeStatus::Failed, |
| 134 | Some(ErrorCategory::InvalidInput), |
| 135 | true, |
| 136 | true, |
| 137 | ), |
| 138 | RunTerminationReason::ModelError, |
| 139 | "historical approval evidence must not hide terminal truncation" |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn typed_error_categories_precede_approval_required_fallback() { |
| 145 | use crate::core::events::TurnOutcomeStatus; |
| 146 | use codewhale_error_taxonomy::ErrorCategory; |
| 147 | |
| 148 | for (category, expected) in [ |
| 149 | (ErrorCategory::Budget, RunTerminationReason::BudgetExhausted), |
| 150 | (ErrorCategory::Timeout, RunTerminationReason::Timeout), |
| 151 | (ErrorCategory::Network, RunTerminationReason::ModelError), |
| 152 | (ErrorCategory::Tool, RunTerminationReason::ToolError), |
| 153 | ( |
| 154 | ErrorCategory::State, |
| 155 | RunTerminationReason::InfrastructureError, |
| 156 | ), |
| 157 | ( |
| 158 | ErrorCategory::Internal, |
| 159 | RunTerminationReason::InfrastructureError, |
| 160 | ), |
| 161 | ] { |
| 162 | assert_eq!( |
| 163 | classify_turn_termination(TurnOutcomeStatus::Failed, Some(category), false, true), |
| 164 | expected, |
| 165 | "typed category {category:?} must win over historical approval_required" |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | assert_eq!( |
| 170 | classify_turn_termination(TurnOutcomeStatus::Failed, None, false, true), |
| 171 | RunTerminationReason::ApprovalRequired |
| 172 | ); |
| 173 | assert_eq!( |
| 174 | classify_turn_termination(TurnOutcomeStatus::Failed, None, true, true), |
| 175 | RunTerminationReason::ApprovalRequired |
| 176 | ); |
| 177 | } |
| 178 | } |
| 179 |