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    /// Allows edits and automatically applies actionable focused-review
13    /// suggestions after completed turns.
14    AutoEditAddressComments,
15    /// Restricts the agent to repository inspection without filesystem writes
16    /// or mutating command approvals.
17    ReadOnly,
18}
19
20impl PermissionMode {
21    /// Ordered permission-mode options shown by interactive selectors.
22    pub const ALL: [PermissionMode; 3] = [
23        PermissionMode::AutoEdit,
24        PermissionMode::AutoEditAddressComments,
25        PermissionMode::ReadOnly,
26    ];
27
28    /// Returns explanatory text shown by interactive selectors.
29    pub fn description(self) -> &'static str {
30        match self {
31            Self::AutoEdit => "Allow the agent to edit files automatically.",
32            Self::AutoEditAddressComments => {
33                "Auto Edit, then address focused-review suggestions up to 3 times."
34            }
35            Self::ReadOnly => "Inspect the repository without changing files.",
36        }
37    }
38
39    /// Returns the wire label used for persistence and provider invocation.
40    pub fn label(self) -> &'static str {
41        match self {
42            Self::AutoEdit => "auto_edit",
43            Self::AutoEditAddressComments => "auto_edit_address_comments",
44            Self::ReadOnly => "read_only",
45        }
46    }
47
48    /// Returns the user-facing label shown in the UI.
49    pub fn display_label(self) -> &'static str {
50        match self {
51            Self::AutoEdit => "Auto Edit",
52            Self::AutoEditAddressComments => "Auto Edit + Auto Address Comments",
53            Self::ReadOnly => "Read Only",
54        }
55    }
56
57    /// Returns whether the provider must deny repository mutations.
58    pub fn is_read_only(self) -> bool {
59        self == Self::ReadOnly
60    }
61}
62
63impl fmt::Display for PermissionMode {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        write!(f, "{}", self.label())
66    }
67}
68
69impl FromStr for PermissionMode {
70    type Err = String;
71
72    fn from_str(s: &str) -> Result<Self, Self::Err> {
73        match s {
74            "auto_edit" => Ok(PermissionMode::AutoEdit),
75            "auto_edit_address_comments" => Ok(PermissionMode::AutoEditAddressComments),
76            "read_only" => Ok(PermissionMode::ReadOnly),
77            _ => Err(format!("Unknown permission mode: {s}")),
78        }
79    }
80}
81
82#[cfg(test)]
83#[path = "permission_test.rs"]
84mod tests;