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

+10 -1
View File
@@ -53,6 +53,8 @@ pub struct Setup {
pub open_sessions: bool,
/// Capture the mouse (wheel scrolling); terminal text selection then needs shift-drag.
pub mouse: bool,
/// No permission prompts (`--yolo`).
pub yolo: bool,
/// MCP servers for the side panel: (name, connected).
pub mcp: Vec<(String, bool)>,
}
@@ -234,6 +236,7 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
specs,
open_sessions,
mouse,
yolo,
mcp,
} = setup;
let (tx, mut rx) = mpsc::unbounded_channel();
@@ -250,7 +253,7 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
Box::pin(async move { orx.await.unwrap_or(false) })
}));
}
{
if !yolo {
let tx = tx.clone();
let _ = agent.cx.state.permit.set(std::sync::Arc::new(move |r| {
let (otx, orx) = tokio::sync::oneshot::channel();
@@ -270,6 +273,12 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
let _ = agent.cx.state.subs.events.set(stx);
let abort: AbortHandle = agent.abort_handle();
let cwd = agent.cx.cwd.clone();
*agent
.cx
.state
.allow
.lock()
.unwrap_or_else(|e| e.into_inner()) = hx_core::permit::Allow::load(&cwd);
let mut app = App {
t: Transcript::default(),
ed: Editor::default(),
+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));
}
}