返回 CodeWhale
app_mode.rs
根目录 / crates / config / src / app_mode.rs
1 //! The TUI's user-facing operating mode. Lives in codewhale-config so
2 //! settings, receipts, and other crates can name it without depending on
3 //! the TUI; the TUI adds the localized picker strings through an extension
4 //! trait.
5
6 /// Supported application modes for the TUI.
7 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
8 pub enum AppMode {
9 Agent,
10 Plan,
11 }
12
13 impl AppMode {
14 pub const ALL: [Self; 2] = [Self::Plan, Self::Agent];
15
16 #[must_use]
17 pub fn parse(value: &str) -> Option<Self> {
18 match value.trim().to_ascii_lowercase().as_str() {
19 "agent" | "act" | "work" | "auto" | "1" => Some(Self::Agent),
20 "plan" | "2" => Some(Self::Plan),
21 other if Self::is_retired_operate_alias(other) => Some(Self::Agent),
22 // Invisible one-way permission shorthand only — never a visible
23 // mode. These spellings resolve to Work; the bypass posture they
24 // imply is carried by the permission surface (settings load,
25 // CLI/runtime wire), not by a mode.
26 other if Self::is_legacy_bypass_alias(other) => Some(Self::Agent),
27 _ => None,
28 }
29 }
30
31 /// Legacy mode spellings (`yolo`, `4`, `bypass`, `bypass-permissions`,
32 /// `bypasspermissions`) that carried the Full Access posture. [`Self::parse`]
33 /// folds them to Work; callers that must preserve the permission meaning
34 /// (the `/mode` command, the runtime policy wire, persisted thread records)
35 /// ask this one predicate instead of keeping their own copy of the list.
36 #[must_use]
37 pub fn is_legacy_bypass_alias(value: &str) -> bool {
38 matches!(
39 value.trim().to_ascii_lowercase().as_str(),
40 "yolo" | "4" | "bypass" | "bypass-permissions" | "bypasspermissions"
41 )
42 }
43
44 /// Operate was retired as a mode. Its spellings still parse, as Work, so a
45 /// saved setting or an older client never errors; callers that want to say
46 /// so once ask this predicate.
47 #[must_use]
48 pub fn is_retired_operate_alias(value: &str) -> bool {
49 matches!(
50 value.trim().to_ascii_lowercase().as_str(),
51 "operate" | "operation" | "ops" | "3"
52 )
53 }
54
55 #[must_use]
56 pub fn from_setting(value: &str) -> Self {
57 Self::parse(value).unwrap_or(Self::Agent)
58 }
59
60 #[must_use]
61 pub fn as_setting(self) -> &'static str {
62 match self {
63 Self::Agent => "agent",
64 Self::Plan => "plan",
65 }
66 }
67
68 /// Short label used in the UI footer.
69 pub fn label(self) -> &'static str {
70 match self {
71 AppMode::Agent => "WORK",
72 AppMode::Plan => "PLAN",
73 }
74 }
75
76 #[must_use]
77 pub fn hook_label(self) -> &'static str {
78 match self {
79 AppMode::Agent => "ACT",
80 AppMode::Plan => "PLAN",
81 }
82 }
83
84 #[must_use]
85 pub fn display_name(self) -> &'static str {
86 match self {
87 AppMode::Agent => "Work",
88 AppMode::Plan => "Plan",
89 }
90 }
91
92 #[must_use]
93 pub fn number(self) -> char {
94 match self {
95 AppMode::Agent => '1',
96 AppMode::Plan => '2',
97 }
98 }
99
100 #[must_use]
101 pub fn uses_agent_baseline(self) -> bool {
102 matches!(self, Self::Agent)
103 }
104
105 /// Description shown in help or onboarding text.
106 pub fn description(self) -> &'static str {
107 match self {
108 AppMode::Agent => "Work mode - direct work in the current session with tools",
109 AppMode::Plan => "Plan mode - research and design before implementing",
110 }
111 }
112 }
113
114 #[cfg(test)]
115 mod tests {
116 use super::*;
117
118 #[test]
119 fn app_mode_helpers_centralize_parse_and_labels() {
120 assert_eq!(AppMode::parse("agent"), Some(AppMode::Agent));
121 assert_eq!(AppMode::parse("act"), Some(AppMode::Agent));
122 assert_eq!(AppMode::parse("work"), Some(AppMode::Agent));
123 assert_eq!(AppMode::parse("2"), Some(AppMode::Plan));
124 assert_eq!(AppMode::parse("auto"), Some(AppMode::Agent));
125 assert_eq!(AppMode::parse("3"), Some(AppMode::Agent));
126 assert_eq!(AppMode::parse("operate"), Some(AppMode::Agent));
127 assert!(AppMode::is_retired_operate_alias(" Operate "));
128 // Legacy YOLO spellings resolve to Work; the bypass posture they imply
129 // travels on the permission surface, not on a mode.
130 assert_eq!(AppMode::parse("YOLO"), Some(AppMode::Agent));
131 assert_eq!(AppMode::parse("4"), Some(AppMode::Agent));
132 assert_eq!(AppMode::parse("bypass"), Some(AppMode::Agent));
133 assert_eq!(AppMode::parse("bypass-permissions"), Some(AppMode::Agent));
134 for alias in [
135 "yolo",
136 " YOLO ",
137 "4",
138 "bypass",
139 "bypass-permissions",
140 "BypassPermissions",
141 ] {
142 assert!(AppMode::is_legacy_bypass_alias(alias), "{alias}");
143 }
144 for mode in ["agent", "act", "plan", "operate", "1", "full-access", ""] {
145 assert!(!AppMode::is_legacy_bypass_alias(mode), "{mode}");
146 }
147 assert_eq!(AppMode::parse("multitask"), None);
148 assert_eq!(AppMode::parse("5"), None);
149 assert_eq!(AppMode::parse("fast"), None);
150 assert_eq!(AppMode::from_setting("multitask"), AppMode::Agent);
151 assert_eq!(AppMode::from_setting("5"), AppMode::Agent);
152
153 assert_eq!(AppMode::Agent.as_setting(), "agent");
154 assert_eq!(AppMode::Plan.display_name(), "Plan");
155 assert_eq!(AppMode::Agent.number(), '1');
156 assert_eq!(AppMode::ALL, [AppMode::Plan, AppMode::Agent]);
157 }
158
159 /// The durable form of a mode is the `as_setting()` string persisted into
160 /// settings and session records — `AppMode` derives no `Serialize`, so the
161 /// round-trip that has to hold is string -> mode -> string.
162 #[test]
163 fn setting_strings_round_trip_for_every_mode() {
164 for mode in AppMode::ALL {
165 let setting = mode.as_setting();
166 assert_eq!(
167 AppMode::from_setting(setting),
168 mode,
169 "from_setting({setting})"
170 );
171 assert_eq!(AppMode::parse(setting), Some(mode), "parse({setting})");
172 }
173
174 assert_eq!(AppMode::Agent.as_setting(), "agent");
175 assert_eq!(AppMode::Plan.as_setting(), "plan");
176 }
177
178 /// `from_setting` is the de-facto default: an absent, empty, or unreadable
179 /// stored value must land on Work rather than panicking.
180 #[test]
181 fn from_setting_falls_back_to_act_for_unknown_values() {
182 assert_eq!(AppMode::from_setting(""), AppMode::Agent);
183 assert_eq!(AppMode::from_setting(" "), AppMode::Agent);
184 assert_eq!(AppMode::from_setting("nonsense"), AppMode::Agent);
185 assert_eq!(AppMode::from_setting("OPERATE"), AppMode::Agent);
186 }
187
188 #[test]
189 fn labels_and_descriptions_cover_every_mode() {
190 assert_eq!(AppMode::Agent.label(), "WORK");
191 assert_eq!(AppMode::Plan.label(), "PLAN");
192
193 assert_eq!(AppMode::Agent.display_name(), "Work");
194 assert_eq!(AppMode::Plan.display_name(), "Plan");
195
196 for mode in AppMode::ALL {
197 assert!(!mode.description().is_empty());
198 }
199 }
200
201 #[test]
202 fn only_work_uses_the_agent_baseline() {
203 assert!(AppMode::Agent.uses_agent_baseline());
204 assert!(!AppMode::Plan.uses_agent_baseline());
205 }
206 }
207
207 lines RUST