1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! `/yolo`: approvals off for this session, visibly.
//!
//! Two ways to run without being asked already existed — `--yes` on the command line and
//! `[cli] approval = "auto"` in the config — and both are decided before the session starts, are
//! invisible afterwards, and cannot be undone without starting another one. This is the third
//! way, and the only one that is a *process setting*: it starts at whatever the other two decided,
//! `/yolo` flips it, and nothing is ever written to `config.toml`. A mode you forgot about must not
//! become what the next session starts with.
//!
//! The flag is shared between the front ends — the terminal, the plain REPL and the dashboard, which
//! reads it once per run when it builds its approver — and read per turn, which is the whole
//! difference from the startup value: `Gate` is chosen at the top of a turn, so a toggle applies to
//! the next turn rather than to the next process.
//!
//! It lives in `eclipse-core` rather than in the CLI for one reason: `/yolo` answers in one
//! sentence on every surface, and a sentence worded in two crates is a sentence that eventually
//! says two different things.
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
/// The words `/yolo` accepts, in the order the refusal lists them.
pub const WORDS: &[&str] = &["on", "off"];
/// Whether tools may run without asking, for as long as this process lives.
///
/// Cloned into both front ends and read per turn; the atomic is the whole point, because the
/// toggle is handled in the same `match` that answers a turn, several frames below the loop that
/// chose the policy.
#[derive(Clone, Debug)]
pub struct Yolo(Arc<AtomicBool>);
impl Yolo {
pub fn new(on: bool) -> Self {
Self(Arc::new(AtomicBool::new(on)))
}
/// Whether the next turn may answer every tool call itself.
pub fn on(&self) -> bool {
self.0.load(Ordering::Relaxed)
}
pub fn set(&self, on: bool) {
self.0.store(on, Ordering::Relaxed);
}
/// `/yolo` with no argument toggles; `on` and `off` set. `Err` is what the caller shows for a
/// word that is not one of them, and nothing was changed.
///
/// The `Ok` text names the state that is now in force rather than the transition, because a
/// toggle's answer has to be readable without knowing what it was before.
pub fn apply(&self, arg: &str) -> Result<String, String> {
match arg.trim() {
"" => {
let on = !self.on();
self.set(on);
Ok(announce(on))
}
"on" => {
self.set(true);
Ok(announce(true))
}
"off" => {
self.set(false);
Ok(announce(false))
}
other => Err(format!(
"`/yolo` is a switch, not a setting: `{other}` is not one of {}. Nothing changed — it is {}.",
quoted(),
state(self.on())
)),
}
}
}
/// The readout's word for the state: what the next turn will do with a call that needs a person.
pub fn state(on: bool) -> &'static str {
if on {
"auto"
} else {
"ask"
}
}
/// The sentence an answer prints, in both fronts, so the two cannot word one mode differently.
pub fn announce(on: bool) -> String {
if on {
"approvals: auto — a tool that needs permission runs instead of parking, until `/yolo off` \
or this session ends. Nothing was written to `config.toml`."
.to_string()
} else {
"approvals: ask — a tool that needs permission parks again, as the session started. \
Nothing was written to `config.toml`."
.to_string()
}
}
/// `on`, `off`, or the words for a refusal, in one place so a message cannot list a verb that does
/// not work.
pub fn quoted() -> String {
WORDS
.iter()
.map(|w| format!("`{w}`"))
.collect::<Vec<_>>()
.join(" or ")
}
/// Why `/yolo` is not a thing on `surface`.
///
/// A job or a platform request that starts with every approval pre-answered is a mode nobody can
/// see and nobody can undo: there is no composer to type `/yolo off` into, and no screen that says
/// it is on. Those two doors decide their policy up front instead.
pub fn refusal(surface: &str) -> String {
format!(
"`/yolo` turns every approval off for the session that typed it, and {surface} has no \
session to turn it back on in — the line was not run. A run's policy is decided before it \
starts: `--yes`, or `[cli] approval = \"auto\"`."
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_argument_toggles_and_on_off_set() {
let yolo = Yolo::new(false);
assert!(!yolo.on());
assert!(yolo.apply("").unwrap().contains("approvals: auto"));
assert!(yolo.on(), "a bare `/yolo` turns it on");
assert!(yolo.apply("").unwrap().contains("approvals: ask"));
assert!(!yolo.on(), "and the next one turns it off again");
yolo.apply("on").unwrap();
assert!(yolo.on());
yolo.apply("on").unwrap();
assert!(yolo.on(), "`on` when it is already on is not an error");
yolo.apply("off").unwrap();
assert!(!yolo.on());
yolo.apply("off").unwrap();
assert!(!yolo.on());
}
#[test]
fn the_answer_names_what_is_in_force_not_what_changed() {
let yolo = Yolo::new(false);
// A toggle's answer has to be readable without knowing what it was: this one says `auto`
// and no earlier state is mentioned.
let said = yolo.apply("").unwrap();
assert!(said.contains("approvals: auto"), "{said}");
assert!(!said.contains("approvals: ask"), "{said}");
}
#[test]
fn a_word_that_is_not_a_switch_is_refused_and_changes_nothing() {
let yolo = Yolo::new(true);
let why = yolo.apply("high").unwrap_err();
assert!(why.contains("`high`"), "{why}");
assert!(why.contains("`on` or `off`"), "{why}");
assert!(yolo.on(), "the mode it was in is the mode it is in");
}
#[test]
fn it_starts_at_whatever_the_startup_flags_decided() {
assert!(Yolo::new(true).on(), "`--yes` is the initial value");
assert!(!Yolo::new(false).on());
// And the clone sees the same switch: one flag for both front ends.
let a = Yolo::new(false);
let b = a.clone();
a.set(true);
assert!(
b.on(),
"the clone is the same flag, not a copy of its value"
);
}
#[test]
fn the_refusal_says_where_a_policy_does_come_from() {
let said = refusal("a cron job");
assert!(said.contains("`/yolo`"), "{said}");
assert!(said.contains("a cron job"), "{said}");
assert!(said.contains("--yes"), "{said}");
assert!(said.contains("[cli] approval"), "{said}");
}
#[test]
fn both_states_have_one_word_and_one_sentence() {
assert_eq!(state(true), "auto");
assert_eq!(state(false), "ask");
assert_eq!(quoted(), "`on` or `off`");
assert_ne!(announce(true), announce(false));
for on in [true, false] {
assert!(
announce(on).contains("config.toml"),
"the promise that nothing is written belongs in the answer"
);
}
}
}