Skip to main content

ag_agent/model/
permission.rs

1use std::fmt;
2use std::str::FromStr;
3
4/// Supported permission mode values for agent execution workflows.
5#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
6pub enum PermissionMode {
7    /// Allows the agent to edit files automatically within its sandbox.
8    #[default]
9    AutoEdit,
10    /// Allows edits and automatically applies actionable focused-review
11    /// suggestions after completed turns.
12    AutoEditAddressComments,
13    /// Restricts the agent to repository inspection without filesystem writes
14    /// or mutating command approvals.
15    ReadOnly,
16}
17
18impl PermissionMode {
19    /// Ordered permission-mode options shown by interactive selectors.
20    pub const ALL: [PermissionMode; 3] = [
21        PermissionMode::AutoEdit,
22        PermissionMode::AutoEditAddressComments,
23        PermissionMode::ReadOnly,
24    ];
25
26    /// Returns explanatory text shown by interactive selectors.
27    pub fn description(self) -> &'static str {
28        match self {
29            Self::AutoEdit => "Allow the agent to edit files automatically.",
30            Self::AutoEditAddressComments => {
31                "Auto Edit, then address focused-review suggestions up to 3 times."
32            }
33            Self::ReadOnly => "Inspect the repository without changing files.",
34        }
35    }
36
37    /// Returns the wire label used for persistence and provider invocation.
38    pub fn label(self) -> &'static str {
39        match self {
40            Self::AutoEdit => "auto_edit",
41            Self::AutoEditAddressComments => "auto_edit_address_comments",
42            Self::ReadOnly => "read_only",
43        }
44    }
45
46    /// Returns the user-facing label shown in the UI.
47    pub fn display_label(self) -> &'static str {
48        match self {
49            Self::AutoEdit => "Auto Edit",
50            Self::AutoEditAddressComments => "Auto Edit + Auto Address Comments",
51            Self::ReadOnly => "Read Only",
52        }
53    }
54
55    /// Returns whether the provider must deny repository mutations.
56    pub fn is_read_only(self) -> bool {
57        self == Self::ReadOnly
58    }
59}
60
61impl fmt::Display for PermissionMode {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        write!(f, "{}", self.label())
64    }
65}
66
67impl FromStr for PermissionMode {
68    type Err = String;
69
70    fn from_str(s: &str) -> Result<Self, Self::Err> {
71        match s {
72            "auto_edit" => Ok(PermissionMode::AutoEdit),
73            "auto_edit_address_comments" => Ok(PermissionMode::AutoEditAddressComments),
74            "read_only" => Ok(PermissionMode::ReadOnly),
75            _ => Err(format!("Unknown permission mode: {s}")),
76        }
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn test_from_str_accepts_supported_modes() {
86        // Arrange
87        let permission_modes = ["auto_edit", "auto_edit_address_comments", "read_only"];
88
89        // Act
90        let parsed_permission_modes = permission_modes.map(PermissionMode::from_str);
91
92        // Assert
93        assert_eq!(
94            parsed_permission_modes,
95            [
96                Ok(PermissionMode::AutoEdit),
97                Ok(PermissionMode::AutoEditAddressComments),
98                Ok(PermissionMode::ReadOnly),
99            ]
100        );
101    }
102
103    #[test]
104    fn test_from_str_rejects_removed_permission_modes() {
105        // Arrange
106        let removed_mode = "autonomous";
107
108        // Act
109        let parsed_permission_mode = PermissionMode::from_str(removed_mode);
110
111        // Assert
112        assert_eq!(
113            parsed_permission_mode,
114            Err("Unknown permission mode: autonomous".to_string())
115        );
116    }
117
118    #[test]
119    fn test_default_uses_auto_edit_mode() {
120        // Arrange, Act
121        let permission_mode = PermissionMode::default();
122
123        // Assert
124        assert_eq!(permission_mode, PermissionMode::AutoEdit);
125    }
126
127    #[test]
128    fn test_label_and_display_label_return_persisted_and_user_facing_text() {
129        // Arrange
130        let permission_modes = PermissionMode::ALL;
131
132        // Act
133        let labels = permission_modes.map(PermissionMode::label);
134        let display_labels = permission_modes.map(PermissionMode::display_label);
135        let descriptions = permission_modes.map(PermissionMode::description);
136        let read_only = permission_modes.map(PermissionMode::is_read_only);
137
138        // Assert
139        assert_eq!(
140            labels,
141            ["auto_edit", "auto_edit_address_comments", "read_only"]
142        );
143        assert_eq!(
144            display_labels,
145            [
146                "Auto Edit",
147                "Auto Edit + Auto Address Comments",
148                "Read Only"
149            ]
150        );
151        assert_eq!(
152            descriptions,
153            [
154                "Allow the agent to edit files automatically.",
155                "Auto Edit, then address focused-review suggestions up to 3 times.",
156                "Inspect the repository without changing files.",
157            ]
158        );
159        assert_eq!(read_only, [false, false, true]);
160    }
161
162    #[test]
163    fn test_display_uses_persisted_label() {
164        // Arrange
165        let permission_mode = PermissionMode::AutoEdit;
166
167        // Act
168        let formatted = permission_mode.to_string();
169
170        // Assert
171        assert_eq!(formatted, "auto_edit");
172    }
173}