permissions: --yolo, command-aware allow rules, load allow.json
ci / check (push) Canceled after 0s
ci / check (pull_request) Canceled after 0s

This commit is contained in:
hermes committed 2026-10-02 21:37:49 +00:00
1 parent b36559bd95
commit 23dc8cd36e
6 files changed
+148 -36

No files matched your search

+32 -10
View File
@@ -18,20 +18,40 @@ impl Prompt {
}
/// Some = decided.
/// Chained commands have no rule: they are asked every time.
fn can_always(&self) -> bool {
!self.req.rule.is_empty()
}
fn opts(&self) -> Vec<(Decision, String)> {
let mut v = vec![(Decision::Once, "[y] allow once".to_string())];
if self.can_always() {
let r = self
.req
.rule
.strip_prefix("bash:")
.unwrap_or(&self.req.rule);
v.push((Decision::Always, format!("[a] allow always: {r}")));
}
v.push((Decision::Deny, "[n] deny".into()));
v
}
pub fn key(&mut self, k: KeyEvent) -> Option<Decision> {
match k.code {
KeyCode::Char('y' | 'Y') => Some(Decision::Once),
KeyCode::Char('a' | 'A') => Some(Decision::Always),
KeyCode::Char('a' | 'A') if self.can_always() => Some(Decision::Always),
KeyCode::Char('n' | 'N') | KeyCode::Esc => Some(Decision::Deny),
KeyCode::Up | KeyCode::Left => {
self.sel = (self.sel + 2) % 3;
let n = self.opts().len();
self.sel = (self.sel + n - 1) % n;
None
}
KeyCode::Down | KeyCode::Right | KeyCode::Tab => {
self.sel = (self.sel + 1) % 3;
self.sel = (self.sel + 1) % self.opts().len();
None
}
KeyCode::Enter => Some([Decision::Once, Decision::Always, Decision::Deny][self.sel]),
KeyCode::Enter => Some(self.opts()[self.sel].0),
_ => None,
}
}
@@ -49,11 +69,7 @@ impl Prompt {
let th = theme::t();
let a = f.area();
let w = (a.width * 3 / 5).clamp(44, 100).min(a.width);
let opts = [
"[y] allow once".to_string(),
format!("[a] allow always: {}", self.req.rule),
"[n] deny".to_string(),
];
let opts: Vec<String> = self.opts().into_iter().map(|o| o.1).collect();
let mut lines = vec![
Line::styled("hx wants to run:", th.dim()),
Line::default(),
@@ -102,7 +118,7 @@ mod tests {
Prompt::new(Request {
tool: "bash".into(),
detail: "rm -rf x".into(),
rule: "bash:rm".into(),
rule: "bash:rm -rf x".into(),
})
}
fn k(c: KeyCode) -> KeyEvent {
@@ -117,5 +133,11 @@ mod tests {
let mut q = p();
assert_eq!(q.key(k(KeyCode::Down)), None);
assert_eq!(q.key(k(KeyCode::Enter)), Some(Decision::Always));
// chained command: no "always"
let mut c = p();
c.req.rule.clear();
assert_eq!(c.key(k(KeyCode::Char('a'))), None);
c.key(k(KeyCode::Down));
assert_eq!(c.key(k(KeyCode::Enter)), Some(Decision::Deny));
}
}