permissions: auto mode refuses dangerous calls (rules + cheap-model judge)

This commit is contained in:
hermes committed 2026-11-12 13:44:25 +00:00
1 parent 63b286efaf
commit 02144d4176
11 files changed
+791 -30

No files matched your search

+31 -5
View File
@@ -48,9 +48,9 @@ pub fn secret(path: &str) -> bool {
}
/// One simple command of a chain: its words and its source text.
struct Seg {
words: Vec<String>,
raw: String,
pub(crate) struct Seg {
pub(crate) words: Vec<String>,
pub(crate) raw: String,
}
/// Split a command line at unquoted `|`, `&&`, `;` into simple commands. `None` for anything else
@@ -58,7 +58,7 @@ struct Seg {
/// `2>&1` and `>/dev/null` are the only redirects accepted.
// ponytail: not a shell parser (no heredocs, `{}` groups, `$'..'`); those ask every time.
#[allow(unused_assignments)]
fn split_cmd(cmd: &str) -> Option<Vec<Seg>> {
pub(crate) fn split_cmd(cmd: &str) -> Option<Vec<Seg>> {
let cs: Vec<(usize, char)> = cmd.char_indices().collect();
let mut segs: Vec<Seg> = Vec::new();
let mut words: Vec<String> = Vec::new();
@@ -171,7 +171,7 @@ fn readonly_cmd(cmd: &str) -> bool {
}
/// `cd` is free: it only matters for what follows, and that is checked on its own.
fn readonly_seg(s: &Seg) -> bool {
pub(crate) fn readonly_seg(s: &Seg) -> bool {
let w: Vec<&str> = s.words.iter().map(String::as_str).collect();
let ok = match w.as_slice() {
[] => false,
@@ -327,6 +327,9 @@ pub struct Allow {
rules: HashSet<String>,
all_edits: bool,
yolo: bool,
auto: bool,
/// Last calls auto mode refused (newest last), for `/auto`.
blocks: Vec<String>,
file: Option<PathBuf>,
}
@@ -341,6 +344,8 @@ impl Allow {
rules,
all_edits: false,
yolo: false,
auto: false,
blocks: Vec::new(),
file: Some(file),
}
}
@@ -376,6 +381,27 @@ impl Allow {
self.yolo
}
pub fn auto_on(&self) -> bool {
self.auto
}
/// Auto mode: dangerous calls are refused, safe ones run, the rest is judged or asked.
pub fn set_auto(&mut self, on: bool) {
self.auto = on;
}
/// Remember a refused call (last 20).
pub fn note_block(&mut self, s: String) {
if self.blocks.len() >= 20 {
self.blocks.remove(0);
}
self.blocks.push(s);
}
pub fn blocks(&self) -> &[String] {
&self.blocks
}
/// Allow every write/edit until the session ends.
pub fn allow_edits(&mut self) {
self.all_edits = true;