diff --git a/crates/hx-core/src/command.rs b/crates/hx-core/src/command.rs index 0be66cf..61338f4 100644 --- a/crates/hx-core/src/command.rs +++ b/crates/hx-core/src/command.rs @@ -65,6 +65,7 @@ pub fn builtins(models: &[&str]) -> Vec { b("new", "start a new session (old one stays resumable)", None), b("clear", "alias of /new", None), b("sessions", "browse, resume, delete sessions", None), + b("yolo", "toggle: run every tool without asking", None), b("lsp", "language server status", None), b("copy", "copy the last answer to the clipboard", None), b("usage", "subscription limits, resets, credits", None), diff --git a/crates/hx-core/src/permit.rs b/crates/hx-core/src/permit.rs index fcc3315..e15fcb8 100644 --- a/crates/hx-core/src/permit.rs +++ b/crates/hx-core/src/permit.rs @@ -104,7 +104,7 @@ fn compound(cmd: &str) -> bool { /// Rule for "allow always". Bash: `bash: *` (first word, plus the subcommand when /// there is one, e.g. `bash:cargo test *`); risky or compound commands store the exact command, or /// nothing at all for compound ones (empty rule = the prompt offers no "always"). -/// File tools are covered as a whole by the tool name. +/// File tools have no rule (the prompt offers `allow all edits this session` instead). // ponytail: patterns are `*` globs on the full command line; add argument-aware rules if needed. pub fn request(tool: &str, args: &serde_json::Value) -> Request { let s = |k: &str| args[k].as_str().unwrap_or_default().to_string(); @@ -112,7 +112,7 @@ pub fn request(tool: &str, args: &serde_json::Value) -> Request { return Request { tool: tool.into(), detail: s("path"), - rule: tool.into(), + rule: String::new(), }; } let cmd = s("command"); @@ -164,6 +164,7 @@ fn glob(pat: &str, s: &str) -> bool { pub struct Allow { rules: HashSet, all_edits: bool, + yolo: bool, file: Option, } @@ -177,13 +178,14 @@ impl Allow { Self { rules, all_edits: false, + yolo: false, file: Some(file), } } /// Is this request covered by a stored rule? `bash:cargo test *` also covers a bare `cargo test`. pub fn allows(&self, r: &Request) -> bool { - if self.all_edits && matches!(r.tool.as_str(), "write" | "edit") { + if self.yolo || self.all_edits && matches!(r.tool.as_str(), "write" | "edit") { return true; } if r.tool != "bash" { @@ -198,6 +200,12 @@ impl Allow { .any(|p| glob(p, &r.detail) || p.strip_suffix(" *").is_some_and(|b| b == r.detail)) } + /// Toggle: allow everything (not persisted); returns the new state. + pub fn toggle_yolo(&mut self) -> bool { + self.yolo = !self.yolo; + self.yolo + } + /// Allow every write/edit until the session ends. pub fn allow_edits(&mut self) { self.all_edits = true; @@ -243,7 +251,7 @@ mod tests { assert_eq!(bash("ls -l").rule, "bash:ls *"); assert_eq!(bash("rm -rf target").rule, "bash:rm -rf target"); assert_eq!(bash("cargo test && rm -rf ~").rule, ""); - assert_eq!(request("edit", &json!({"path": "a.rs"})).rule, "edit"); + assert_eq!(request("edit", &json!({"path": "a.rs"})).rule, ""); assert!(gated("bash") && !gated("read")); } @@ -281,5 +289,6 @@ mod tests { assert!(!a.allows(&e)); a.allow_edits(); assert!(a.allows(&e) && !a.allows(&bash("make"))); + assert!(a.toggle_yolo() && a.allows(&bash("rm -rf /")) && !a.toggle_yolo()); } } diff --git a/crates/hx-tui/src/lib.rs b/crates/hx-tui/src/lib.rs index 374d5b8..b66c386 100644 --- a/crates/hx-tui/src/lib.rs +++ b/crates/hx-tui/src/lib.rs @@ -1169,6 +1169,20 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) { let l = skills_list(&app.specs, &dirs); show(app, "skills", l); } + "yolo" => { + let on = app + .state + .allow + .lock() + .unwrap_or_else(|e| e.into_inner()) + .toggle_yolo(); + let m = if on { + "yolo on: nothing asks" + } else { + "yolo off" + }; + app.t.push(Item::Info(m.into())); + } "lsp" => { let st = lsp_status().await; let l = if st.is_empty() { diff --git a/tests/tui_smoke.py b/tests/tui_smoke.py index 4bf1a3d..14b3947 100644 --- a/tests/tui_smoke.py +++ b/tests/tui_smoke.py @@ -67,7 +67,7 @@ check("tab completion", "es.md") send(b"\x15") # ctrl-u send(b"make hello.txt"); send(b"\r", 2) check("permission prompt", "permission") -check("allow always rule", "allow always: write") +check("allow all edits option", "allow all edits this session") send(b"y", 2) check("second prompt for bash", "allow always: sort *") send(b"y", 3)