Skip to main content

adk_eval/
error.rs

1//! Error types for the evaluation framework
2
3use thiserror::Error;
4
5/// Result type alias for evaluation operations
6pub type Result<T> = std::result::Result<T, EvalError>;
7
8/// Errors that can occur during evaluation
9#[derive(Error, Debug)]
10#[non_exhaustive]
11pub enum EvalError {
12    /// Failed to load test file
13    #[error("Failed to load test file: {0}")]
14    LoadError(String),
15
16    /// Failed to parse test file
17    #[error("Failed to parse test file: {0}")]
18    ParseError(String),
19
20    /// Test case execution failed
21    #[error("Test case execution failed: {0}")]
22    ExecutionError(String),
23
24    /// Agent error during evaluation
25    #[error("Agent error: {0}")]
26    AgentError(String),
27
28    /// Invalid configuration
29    #[error("Invalid configuration: {0}")]
30    ConfigError(String),
31
32    /// IO error
33    #[error("IO error: {0}")]
34    IoError(#[from] std::io::Error),
35
36    /// JSON serialization error
37    #[error("JSON error: {0}")]
38    JsonError(#[from] serde_json::Error),
39
40    /// Scoring error
41    #[error("Scoring error: {0}")]
42    ScoringError(String),
43
44    /// LLM judge error
45    #[error("LLM judge error: {0}")]
46    JudgeError(String),
47
48    /// Structured judge parse error
49    #[error("Structured judge parse error: {0}")]
50    JudgeParseError(String),
51
52    /// Embedding scorer error
53    #[error("Embedding scorer error: {0}")]
54    EmbeddingError(String),
55
56    /// Baseline I/O error
57    #[error("Baseline I/O error: {0}")]
58    BaselineError(String),
59
60    /// Annotation format error
61    #[error("Annotation format error: {0}")]
62    AnnotationError(String),
63
64    /// Test generation error
65    #[error("Test generation error: {0}")]
66    GenerationError(String),
67
68    /// Statistics computation error
69    #[error("Statistics computation error: {0}")]
70    StatisticsError(String),
71
72    /// A budget, a spend cap or a kill switch refused a model call or run, so nothing was
73    /// judged. The evaluation stops rather than counting the refusal as a failed case.
74    #[error("evaluation stopped: {0}; raise the evaluation's budget or unfreeze the kill switch")]
75    Refused(Box<adk_core::AdkError>),
76}
77
78impl EvalError {
79    /// Wraps an error from an agent run or a judge call, keeping a budget, spend-cap or
80    /// kill-switch refusal ([`is_refusal`]) as [`EvalError::Refused`].
81    pub(crate) fn from_model_error(
82        error: adk_core::AdkError,
83        wrap: impl FnOnce(adk_core::AdkError) -> EvalError,
84    ) -> Self {
85        if is_refusal(&error) { EvalError::Refused(Box::new(error)) } else { wrap(error) }
86    }
87
88    /// Returns `true` for [`EvalError::Refused`].
89    pub fn is_refusal(&self) -> bool {
90        matches!(self, EvalError::Refused(_))
91    }
92}
93
94/// Returns `true` when `error` is a refusal by a run budget, a spend ledger or a kill switch
95/// rather than a failure of the agent or the judge.
96///
97/// # Example
98///
99/// ```
100/// use adk_core::{AdkError, ErrorCategory, ErrorComponent, GOVERNANCE_FROZEN_CODE};
101///
102/// let frozen = AdkError::new(
103///     ErrorComponent::Server,
104///     ErrorCategory::Forbidden,
105///     GOVERNANCE_FROZEN_CODE,
106///     "frozen",
107/// );
108/// assert!(adk_eval::error::is_refusal(&frozen));
109/// assert!(!adk_eval::error::is_refusal(&AdkError::model("upstream timeout")));
110/// ```
111pub fn is_refusal(error: &adk_core::AdkError) -> bool {
112    error.code == adk_core::GOVERNANCE_FROZEN_CODE
113        || error.code == adk_core::GOVERNANCE_STALE_CODE
114        || error.code == adk_core::SPEND_LIMIT_EXCEEDED_CODE
115        || error.code.starts_with("budget.")
116}
117
118impl From<adk_core::AdkError> for EvalError {
119    fn from(err: adk_core::AdkError) -> Self {
120        EvalError::from_model_error(err, |err| EvalError::AgentError(err.to_string()))
121    }
122}
123
124impl From<EvalError> for adk_core::AdkError {
125    fn from(err: EvalError) -> Self {
126        use adk_core::{ErrorCategory, ErrorComponent};
127        // A refusal keeps the budget, spend or kill-switch error it carries.
128        if let EvalError::Refused(error) = err {
129            return *error;
130        }
131        let (category, code) = match &err {
132            EvalError::LoadError(_) => (ErrorCategory::NotFound, "eval.load"),
133            EvalError::ParseError(_) => (ErrorCategory::InvalidInput, "eval.parse"),
134            EvalError::ExecutionError(_) => (ErrorCategory::Internal, "eval.execution"),
135            EvalError::AgentError(_) => (ErrorCategory::Internal, "eval.agent"),
136            EvalError::ConfigError(_) => (ErrorCategory::InvalidInput, "eval.config"),
137            EvalError::IoError(_) => (ErrorCategory::Internal, "eval.io"),
138            EvalError::JsonError(_) => (ErrorCategory::Internal, "eval.json"),
139            EvalError::ScoringError(_) => (ErrorCategory::Internal, "eval.scoring"),
140            EvalError::JudgeError(_) => (ErrorCategory::Internal, "eval.judge"),
141            EvalError::JudgeParseError(_) => (ErrorCategory::InvalidInput, "eval.judge_parse"),
142            EvalError::EmbeddingError(_) => (ErrorCategory::Internal, "eval.embedding"),
143            EvalError::BaselineError(_) => (ErrorCategory::Internal, "eval.baseline"),
144            EvalError::AnnotationError(_) => (ErrorCategory::InvalidInput, "eval.annotation"),
145            EvalError::GenerationError(_) => (ErrorCategory::Internal, "eval.generation"),
146            EvalError::StatisticsError(_) => (ErrorCategory::Internal, "eval.statistics"),
147            // Returned as is above.
148            EvalError::Refused(_) => (ErrorCategory::ResourceExhausted, "eval.refused"),
149        };
150        adk_core::AdkError::new(ErrorComponent::Eval, category, code, err.to_string())
151            .with_source(err)
152    }
153}