Skip to main content

ag_runtime/
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)]
81#[path = "permission_test.rs"]
82mod tests;