greentic-aw-runtime 1.2.2

Enterprise Agentic Worker runtime — Plan-Act-Observe loop, Redis state, tool dispatch via greentic-ext-runtime
Documentation
//! All error and termination types used by the AW runtime.
//!
//! IMPORTANT: external surfaces (runner, designer) MUST render
//! end-user-facing replies via [`AgentError::user_facing_message`].
//! Raw `Display` of `AgentError` is for internal logs only.

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::config::AgentConfig;

/// Substring of [`AgentError::StateUnavailable`]'s `Display`; the event
/// bridges match it to classify the failure as retryable.
pub const STATE_UNAVAILABLE_MARKER: &str = "agent state temporarily unavailable";

impl AgentError {
    /// Classify a state-backend failure met before the turn changed anything:
    /// a backend that did not answer (`Redis`) is transient and retryable;
    /// undecodable or too-new state is deterministic and must not be retried
    /// (nor overwritten).
    #[must_use]
    pub fn from_state_error(error: StateError) -> Self {
        match error {
            StateError::Redis(_) => Self::StateUnavailable(error),
            other => Self::StateLoad(other),
        }
    }
}

#[derive(Debug, Error)]
pub enum AgentError {
    #[error("agent state load failed: {0}")]
    StateLoad(#[from] StateError),

    /// The state backend could not answer (a Redis blip). Transient: the turn
    /// ran nothing and saved nothing, so it is safe to retry. The `Display`
    /// text carries [`STATE_UNAVAILABLE_MARKER`] so the dispatch bridges
    /// redeliver the request instead of answering with a failure.
    #[error("agent state temporarily unavailable: {0}")]
    StateUnavailable(StateError),

    #[error("llm provider unavailable")]
    LlmProviderUnavailable,

    #[error("llm error: {0}")]
    Llm(#[from] LlmError),

    #[error("config error: {0}")]
    Config(#[from] ConfigError),

    #[error("tool dispatch error: {0}")]
    ToolDispatch(String),

    #[error("daily token budget exceeded")]
    TokenBudgetExceeded,

    #[error("credit budget exceeded")]
    CreditBudgetExceeded,

    #[error("session lock could not be acquired within wait window")]
    LockTimeout,

    #[error("session lock was lost mid-step; aborting without saving")]
    LockLost,

    #[error("loop exceeded max iterations")]
    MaxIterations,

    #[error("step timed out")]
    Timeout,

    #[error("internal: {0}")]
    Internal(String),

    #[error("guardrail denied ({direction}): {message}")]
    GuardrailDenied {
        direction: crate::guardrail::GuardrailDirection,
        code: String,
        message: String,
        details: Option<String>,
    },
}

impl LlmError {
    /// Whether a fresh attempt can succeed: provider 5xx, 429 and transport
    /// failures are transient; 4xx and undecodable replies are deterministic.
    #[must_use]
    pub fn is_retryable(&self) -> bool {
        matches!(
            self,
            Self::ServiceUnavailable | Self::RateLimited(_) | Self::Transport(_)
        )
    }
}

impl AgentError {
    /// Returns a sanitised, end-user-appropriate string. No Rust error
    /// chain, no internal detail, no PII leakage. Tenants can override
    /// the LLM-unavailability + budget messages via `AgentLimits`.
    pub fn user_facing_message(&self, config: &AgentConfig) -> String {
        match self {
            Self::LlmProviderUnavailable | Self::Llm(_) => config
                .limits
                .provider_failure_message
                .clone()
                .unwrap_or_else(|| {
                    "I'm having trouble reaching my reasoning system. \
                     Please try again in a moment."
                        .into()
                }),
            Self::TokenBudgetExceeded => "Daily usage limit reached. \
                 Please try again tomorrow or contact your administrator."
                .to_string(),
            Self::CreditBudgetExceeded => "Your account has no remaining credits. \
                 Please top up your balance or contact your administrator."
                .to_string(),
            Self::Timeout => {
                "I'm taking longer than expected — please try a simpler request.".to_string()
            }
            Self::MaxIterations => "I wasn't able to finish reasoning about that. \
                 Could you rephrase or break it into smaller steps?"
                .to_string(),
            _ => "Something went wrong. Please try again.".to_string(),
        }
    }
}

#[derive(Debug, Error)]
pub enum StateError {
    #[error("redis error: {0}")]
    Redis(String),
    #[error("schema version {found} not supported (max supported: {supported})")]
    SchemaIncompatible { found: u32, supported: u32 },
    #[error("decode error: {0}")]
    Decode(String),
    #[error("lock acquisition timed out after {0:?}")]
    LockTimeout(std::time::Duration),
    /// A held lock is no longer owned by this holder (expired and possibly
    /// taken by another worker). Distinct from a transient backend error on
    /// refresh: the step must stop rather than save over the new owner's state.
    #[error("session lock no longer owned by this holder")]
    LockLost,
}

#[derive(Debug, Error)]
pub enum LlmError {
    #[error("provider returned 5xx after retries")]
    ServiceUnavailable,
    #[error("provider returned 4xx: {0}")]
    BadRequest(String),
    /// Provider returned 429. Retryable, unlike the other 4xx-class errors.
    #[error("provider rate limited: {0}")]
    RateLimited(String),
    #[error("transport: {0}")]
    Transport(String),
    #[error("decode: {0}")]
    Decode(String),
}

#[derive(Debug, Error)]
pub enum MemoryError {
    #[error("memory provider unavailable: {0}")]
    Backend(String),
    #[error("memory provider not configured")]
    NotConfigured,
    /// A size cap of the in-process provider refused the write. Safe to show
    /// the model: it names the limit, never any stored content.
    #[error("memory limit reached: {0}")]
    LimitExceeded(String),
}

#[derive(Debug, Error)]
pub enum ConfigError {
    #[error("agent_id {0} not found for tenant")]
    AgentNotFound(String),
    #[error("provider misconfigured: {0}")]
    Misconfigured(String),
    #[error("internal: {0}")]
    Internal(String),
}

/// Reason the Plan-Act-Observe loop exited. Surfaced via
/// [`crate::AgentOutput::terminated_by`] and as an OTel attribute on
/// the `aw.step` span.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TerminationReason {
    FinalReply,
    MaxIterations,
    Timeout,
    Error,
    TokenBudgetExceeded,
    ConversationEnded,
    /// A `flow:` tool parked on the user (a card awaiting its submit). The
    /// turn ended with [`crate::AgentOutput::pending_presentation`] to show;
    /// the next turn resumes the tool with the user's answer.
    AwaitingToolInput,
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::config::{AgentConfig, AgentLimits, LlmProviderRef};

    fn config_with(message: Option<&str>) -> AgentConfig {
        AgentConfig {
            agent_id: "a".into(),
            system_prompt: "".into(),
            tools: vec![],
            guardrails: vec![],
            llm: LlmProviderRef {
                provider: "openai".into(),
                model: "gpt-4".into(),
                credential_ref: None,
            },
            limits: AgentLimits {
                provider_failure_message: message.map(str::to_string),
                ..AgentLimits::default()
            },
            memory: None,
            knowledge: None,
            conversational: false,
            opening_message: None,
            on_text_while_parked: Default::default(),
        }
    }

    #[test]
    fn user_facing_message_defaults_for_provider_unavailable() {
        let cfg = config_with(None);
        let msg = AgentError::LlmProviderUnavailable.user_facing_message(&cfg);
        assert!(msg.contains("reasoning system"));
        assert!(msg.contains("try again"));
    }

    #[test]
    fn user_facing_message_uses_tenant_override_when_set() {
        let cfg = config_with(Some("Please retry in 5 minutes."));
        let msg = AgentError::LlmProviderUnavailable.user_facing_message(&cfg);
        assert_eq!(msg, "Please retry in 5 minutes.");
    }

    #[test]
    fn user_facing_message_never_leaks_internal_detail() {
        let cfg = config_with(None);
        let leaky = AgentError::Internal("DATABASE_HOST=192.168.1.5".into());
        let msg = leaky.user_facing_message(&cfg);
        assert!(!msg.contains("DATABASE_HOST"));
        assert!(!msg.contains("192.168"));
    }

    #[test]
    fn user_facing_message_budget_distinct_from_default() {
        let cfg = config_with(None);
        let budget = AgentError::TokenBudgetExceeded.user_facing_message(&cfg);
        assert!(budget.contains("limit"));
        assert_ne!(
            budget,
            AgentError::Internal("x".into()).user_facing_message(&cfg)
        );
    }

    #[test]
    fn rate_limits_and_transport_errors_are_retryable_but_4xx_is_not() {
        assert!(LlmError::ServiceUnavailable.is_retryable());
        assert!(LlmError::RateLimited("429".into()).is_retryable());
        assert!(LlmError::Transport("reset".into()).is_retryable());
        assert!(!LlmError::BadRequest("400".into()).is_retryable());
        assert!(!LlmError::Decode("bad".into()).is_retryable());
    }

    #[test]
    fn conversation_ended_serde_snake_case() {
        let json = serde_json::to_string(&TerminationReason::ConversationEnded).unwrap();
        assert_eq!(json, "\"conversation_ended\"");
        let back: TerminationReason = serde_json::from_str("\"conversation_ended\"").unwrap();
        assert_eq!(back, TerminationReason::ConversationEnded);
    }
}