Skip to main content

vtcode_commons/error_category/
safeguard.rs

1//! Typed refusal carrier: which safeguard stopped a tool call.
2
3use super::ErrorCategory;
4
5/// Which safeguard refused a tool call.
6///
7/// Producers that refuse a call return [`SafeguardDenial`] so the refusal is
8/// recognized by type after it crosses an `anyhow` boundary. Matching words
9/// like "policy" or "sandbox" in the message is only the fallback for refusals
10/// from sources that do not carry a kind, and it misreads any error that merely
11/// mentions a file called `policy_facade.rs`.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13pub enum SafeguardKind {
14    /// A mutating or unavailable tool in the read-only Planning workflow.
15    PlanningWorkflow,
16    /// The command-security validator rejected a command.
17    CommandSecurity,
18    /// A tool deny policy or workspace boundary refused the call.
19    ToolPolicy,
20    /// Permission review refused the call.
21    PermissionReview,
22    /// The safety gateway's validation failed.
23    SafetyValidation,
24}
25
26impl SafeguardKind {
27    /// Error category recovery and presentation decisions key on.
28    #[must_use]
29    pub const fn category(self) -> ErrorCategory {
30        match self {
31            Self::PlanningWorkflow => ErrorCategory::PlanningPolicyViolation,
32            Self::CommandSecurity | Self::ToolPolicy | Self::PermissionReview | Self::SafetyValidation => {
33                ErrorCategory::PolicyViolation
34            }
35        }
36    }
37}
38
39/// A tool call refused by a safeguard. Displays as its message unchanged.
40#[derive(Debug, Clone, thiserror::Error)]
41#[error("{message}")]
42pub struct SafeguardDenial {
43    kind: SafeguardKind,
44    message: String,
45}
46
47impl SafeguardDenial {
48    #[must_use]
49    pub fn new(kind: SafeguardKind, message: impl Into<String>) -> Self {
50        Self { kind, message: message.into() }
51    }
52
53    #[must_use]
54    pub const fn kind(&self) -> SafeguardKind {
55        self.kind
56    }
57}
58
59impl SafeguardDenial {
60    /// A mutating or unavailable tool refused in the read-only Planning workflow.
61    #[must_use]
62    pub fn planning_workflow(message: impl Into<String>) -> anyhow::Error {
63        anyhow::Error::new(Self::new(SafeguardKind::PlanningWorkflow, message))
64    }
65
66    /// The safety gateway's validation failed.
67    #[must_use]
68    pub fn safety_validation(message: impl Into<String>) -> anyhow::Error {
69        anyhow::Error::new(Self::new(SafeguardKind::SafetyValidation, message))
70    }
71}