vtcode-commons 0.186.0

Shared traits for paths, telemetry, and error reporting reused across VT Code component extractions
Documentation
//! Typed refusal carrier: which safeguard stopped a tool call.

use super::ErrorCategory;

/// Which safeguard refused a tool call.
///
/// Producers that refuse a call return [`SafeguardDenial`] so the refusal is
/// recognized by type after it crosses an `anyhow` boundary. Matching words
/// like "policy" or "sandbox" in the message is only the fallback for refusals
/// from sources that do not carry a kind, and it misreads any error that merely
/// mentions a file called `policy_facade.rs`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SafeguardKind {
    /// A mutating or unavailable tool in the read-only Planning workflow.
    PlanningWorkflow,
    /// The command-security validator rejected a command.
    CommandSecurity,
    /// A tool deny policy or workspace boundary refused the call.
    ToolPolicy,
    /// Permission review refused the call.
    PermissionReview,
    /// The safety gateway's validation failed.
    SafetyValidation,
}

impl SafeguardKind {
    /// Error category recovery and presentation decisions key on.
    #[must_use]
    pub const fn category(self) -> ErrorCategory {
        match self {
            Self::PlanningWorkflow => ErrorCategory::PlanningPolicyViolation,
            Self::CommandSecurity | Self::ToolPolicy | Self::PermissionReview | Self::SafetyValidation => {
                ErrorCategory::PolicyViolation
            }
        }
    }
}

/// A tool call refused by a safeguard. Displays as its message unchanged.
#[derive(Debug, Clone, thiserror::Error)]
#[error("{message}")]
pub struct SafeguardDenial {
    kind: SafeguardKind,
    message: String,
}

impl SafeguardDenial {
    #[must_use]
    pub fn new(kind: SafeguardKind, message: impl Into<String>) -> Self {
        Self { kind, message: message.into() }
    }

    #[must_use]
    pub const fn kind(&self) -> SafeguardKind {
        self.kind
    }
}

impl SafeguardDenial {
    /// A mutating or unavailable tool refused in the read-only Planning workflow.
    #[must_use]
    pub fn planning_workflow(message: impl Into<String>) -> anyhow::Error {
        anyhow::Error::new(Self::new(SafeguardKind::PlanningWorkflow, message))
    }

    /// The safety gateway's validation failed.
    #[must_use]
    pub fn safety_validation(message: impl Into<String>) -> anyhow::Error {
        anyhow::Error::new(Self::new(SafeguardKind::SafetyValidation, message))
    }
}