Skip to main content

ag_contracts/
permission.rs

1//! `PermissionMode` values that govern agent execution permissions.
2
3use std::fmt;
4use std::str::FromStr;
5
6/// Supported permission mode values for agent execution workflows.
7#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
8pub enum PermissionMode {
9    /// Allows the agent to edit files automatically within its sandbox.
10    #[default]
11    AutoEdit,
12    /// Restricts the agent to repository inspection without filesystem writes
13    /// or mutating command approvals.
14    ReadOnly,
15}
16
17impl PermissionMode {
18    /// Ordered permission-mode options shown by interactive selectors.
19    pub const ALL: [PermissionMode; 2] = [PermissionMode::AutoEdit, PermissionMode::ReadOnly];
20
21    /// Returns the wire label used for persistence and provider invocation.
22    pub fn label(self) -> &'static str {
23        match self {
24            Self::AutoEdit => "auto_edit",
25            Self::ReadOnly => "read_only",
26        }
27    }
28
29    /// Returns the user-facing label shown in the UI.
30    pub fn display_label(self) -> &'static str {
31        match self {
32            Self::AutoEdit => "Auto Edit",
33            Self::ReadOnly => "Read Only",
34        }
35    }
36
37    /// Returns whether the provider must deny repository mutations.
38    pub fn is_read_only(self) -> bool {
39        self == Self::ReadOnly
40    }
41}
42
43impl fmt::Display for PermissionMode {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        write!(f, "{}", self.label())
46    }
47}
48
49impl FromStr for PermissionMode {
50    type Err = String;
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        match s {
54            "auto_edit" => Ok(PermissionMode::AutoEdit),
55            "read_only" => Ok(PermissionMode::ReadOnly),
56            _ => Err(format!("Unknown permission mode: {s}")),
57        }
58    }
59}
60
61#[cfg(test)]
62#[path = "permission_test.rs"]
63mod tests;