Ask parallel permission prompts one by one; add allow-all-edits option
This commit is contained in:
1 parent
6f951ad7f8
commit
49be5673bd
4 files changed
+42
-8
No files matched your search
@@ -59,6 +59,8 @@ pub struct State {
|
|||||||
pub permit: std::sync::OnceLock<permit::Permit>,
|
pub permit: std::sync::OnceLock<permit::Permit>,
|
||||||
/// Rules the user allowed for good.
|
/// Rules the user allowed for good.
|
||||||
pub allow: Mutex<permit::Allow>,
|
pub allow: Mutex<permit::Allow>,
|
||||||
|
/// Serializes permission prompts: parallel tool calls are asked one by one.
|
||||||
|
pub asking: tokio::sync::Mutex<()>,
|
||||||
/// Every write/edit this session, for `/review`.
|
/// Every write/edit this session, for `/review`.
|
||||||
pub edits: Mutex<Vec<permit::FileEdit>>,
|
pub edits: Mutex<Vec<permit::FileEdit>>,
|
||||||
/// Same for multiple-choice questions.
|
/// Same for multiple-choice questions.
|
||||||
|
|||||||
@@ -77,6 +77,8 @@ pub struct Request {
|
|||||||
pub enum Decision {
|
pub enum Decision {
|
||||||
Once,
|
Once,
|
||||||
Always,
|
Always,
|
||||||
|
/// All write/edit calls for the rest of this session (not persisted).
|
||||||
|
Edits,
|
||||||
Deny,
|
Deny,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,6 +163,7 @@ fn glob(pat: &str, s: &str) -> bool {
|
|||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Allow {
|
pub struct Allow {
|
||||||
rules: HashSet<String>,
|
rules: HashSet<String>,
|
||||||
|
all_edits: bool,
|
||||||
file: Option<PathBuf>,
|
file: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,12 +176,16 @@ impl Allow {
|
|||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
Self {
|
Self {
|
||||||
rules,
|
rules,
|
||||||
|
all_edits: false,
|
||||||
file: Some(file),
|
file: Some(file),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Is this request covered by a stored rule? `bash:cargo test *` also covers a bare `cargo test`.
|
/// Is this request covered by a stored rule? `bash:cargo test *` also covers a bare `cargo test`.
|
||||||
pub fn allows(&self, r: &Request) -> bool {
|
pub fn allows(&self, r: &Request) -> bool {
|
||||||
|
if self.all_edits && matches!(r.tool.as_str(), "write" | "edit") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if r.tool != "bash" {
|
if r.tool != "bash" {
|
||||||
return self.rules.contains(&r.rule);
|
return self.rules.contains(&r.rule);
|
||||||
}
|
}
|
||||||
@@ -191,6 +198,11 @@ impl Allow {
|
|||||||
.any(|p| glob(p, &r.detail) || p.strip_suffix(" *").is_some_and(|b| b == r.detail))
|
.any(|p| glob(p, &r.detail) || p.strip_suffix(" *").is_some_and(|b| b == r.detail))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Allow every write/edit until the session ends.
|
||||||
|
pub fn allow_edits(&mut self) {
|
||||||
|
self.all_edits = true;
|
||||||
|
}
|
||||||
|
|
||||||
/// Remember a rule; the file is best effort (the rule holds for this session either way).
|
/// Remember a rule; the file is best effort (the rule holds for this session either way).
|
||||||
pub fn add(&mut self, rule: &str) {
|
pub fn add(&mut self, rule: &str) {
|
||||||
if rule.is_empty() {
|
if rule.is_empty() {
|
||||||
@@ -265,5 +277,9 @@ mod tests {
|
|||||||
assert!(Allow::load(d.path()).allows(&bash("cargo test -q")));
|
assert!(Allow::load(d.path()).allows(&bash("cargo test -q")));
|
||||||
a.add("");
|
a.add("");
|
||||||
assert!(!a.allows(&bash("")));
|
assert!(!a.allows(&bash("")));
|
||||||
|
let e = request("edit", &json!({"path": "a.rs"}));
|
||||||
|
assert!(!a.allows(&e));
|
||||||
|
a.allow_edits();
|
||||||
|
assert!(a.allows(&e) && !a.allows(&bash("make")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -109,15 +109,18 @@ impl Registry {
|
|||||||
&& let Some(ask) = cx.state.permit.get()
|
&& let Some(ask) = cx.state.permit.get()
|
||||||
{
|
{
|
||||||
let req = crate::permit::request(name, &args);
|
let req = crate::permit::request(name, &args);
|
||||||
let known = cx
|
let allowed = || {
|
||||||
.state
|
cx.state
|
||||||
.allow
|
.allow
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap_or_else(|e| e.into_inner())
|
.unwrap_or_else(|e| e.into_inner())
|
||||||
.allows(&req);
|
.allows(&req)
|
||||||
if !known {
|
};
|
||||||
|
// One prompt at a time; re-check after waiting, an earlier answer may cover this call.
|
||||||
|
let _turn = cx.state.asking.lock().await;
|
||||||
|
if !allowed() {
|
||||||
let rule = req.rule.clone();
|
let rule = req.rule.clone();
|
||||||
match ask(req).await {
|
match ask(req.clone()).await {
|
||||||
crate::permit::Decision::Once => {}
|
crate::permit::Decision::Once => {}
|
||||||
crate::permit::Decision::Always => cx
|
crate::permit::Decision::Always => cx
|
||||||
.state
|
.state
|
||||||
@@ -125,6 +128,12 @@ impl Registry {
|
|||||||
.lock()
|
.lock()
|
||||||
.unwrap_or_else(|e| e.into_inner())
|
.unwrap_or_else(|e| e.into_inner())
|
||||||
.add(&rule),
|
.add(&rule),
|
||||||
|
crate::permit::Decision::Edits => cx
|
||||||
|
.state
|
||||||
|
.allow
|
||||||
|
.lock()
|
||||||
|
.unwrap_or_else(|e| e.into_inner())
|
||||||
|
.allow_edits(),
|
||||||
crate::permit::Decision::Deny => {
|
crate::permit::Decision::Deny => {
|
||||||
return ToolResult::err(
|
return ToolResult::err(
|
||||||
"denied by the user; do not retry, ask what to do instead",
|
"denied by the user; do not retry, ask what to do instead",
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ impl Prompt {
|
|||||||
.unwrap_or(&self.req.rule);
|
.unwrap_or(&self.req.rule);
|
||||||
v.push((Decision::Always, format!("[a] allow always: {r}")));
|
v.push((Decision::Always, format!("[a] allow always: {r}")));
|
||||||
}
|
}
|
||||||
|
if matches!(self.req.tool.as_str(), "write" | "edit") {
|
||||||
|
v.push((Decision::Edits, "[e] allow all edits this session".into()));
|
||||||
|
}
|
||||||
v.push((Decision::Deny, "[n] deny".into()));
|
v.push((Decision::Deny, "[n] deny".into()));
|
||||||
v
|
v
|
||||||
}
|
}
|
||||||
@@ -41,6 +44,9 @@ impl Prompt {
|
|||||||
match k.code {
|
match k.code {
|
||||||
KeyCode::Char('y' | 'Y') => Some(Decision::Once),
|
KeyCode::Char('y' | 'Y') => Some(Decision::Once),
|
||||||
KeyCode::Char('a' | 'A') if self.can_always() => Some(Decision::Always),
|
KeyCode::Char('a' | 'A') if self.can_always() => Some(Decision::Always),
|
||||||
|
KeyCode::Char('e' | 'E') if matches!(self.req.tool.as_str(), "write" | "edit") => {
|
||||||
|
Some(Decision::Edits)
|
||||||
|
}
|
||||||
KeyCode::Char('n' | 'N') | KeyCode::Esc => Some(Decision::Deny),
|
KeyCode::Char('n' | 'N') | KeyCode::Esc => Some(Decision::Deny),
|
||||||
KeyCode::Up | KeyCode::Left => {
|
KeyCode::Up | KeyCode::Left => {
|
||||||
let n = self.opts().len();
|
let n = self.opts().len();
|
||||||
@@ -60,6 +66,7 @@ impl Prompt {
|
|||||||
match d {
|
match d {
|
||||||
Decision::Once => "allowed",
|
Decision::Once => "allowed",
|
||||||
Decision::Always => "allowed always",
|
Decision::Always => "allowed always",
|
||||||
|
Decision::Edits => "allowed all edits this session:",
|
||||||
Decision::Deny => "denied",
|
Decision::Deny => "denied",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user