TUI: permission prompt, /diff review, /stats dashboard, lens/context/MCP side panel
This commit is contained in:
1 parent
4a8e920ab8
commit
5fd7044a2f
15 files changed
+945
-2
No files matched your search
@@ -0,0 +1,124 @@
|
||||
//! Permission gate for mutating tools (bash, write, edit) and the edits log behind `/review`.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Tools that need a yes before they run.
|
||||
pub fn gated(tool: &str) -> bool {
|
||||
matches!(tool, "bash" | "write" | "edit")
|
||||
}
|
||||
|
||||
/// What the user is asked.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Request {
|
||||
pub tool: String,
|
||||
/// One line: the command or the path.
|
||||
pub detail: String,
|
||||
/// What "allow always" stores and shows.
|
||||
pub rule: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Decision {
|
||||
Once,
|
||||
Always,
|
||||
Deny,
|
||||
}
|
||||
|
||||
/// Asks the user; set by interactive frontends only (None = everything runs, as in print mode).
|
||||
pub type Permit = std::sync::Arc<
|
||||
dyn Fn(Request) -> std::pin::Pin<Box<dyn std::future::Future<Output = Decision> + Send>>
|
||||
+ Send
|
||||
+ Sync,
|
||||
>;
|
||||
|
||||
/// `bash` rules cover a command's first word (`bash:cargo`); file tools are covered as a whole.
|
||||
// ponytail: first-word granularity; add glob/argument patterns when people want `rm target/*` only.
|
||||
pub fn request(tool: &str, args: &serde_json::Value) -> Request {
|
||||
let s = |k: &str| args[k].as_str().unwrap_or_default().to_string();
|
||||
if tool == "bash" {
|
||||
let cmd = s("command");
|
||||
let word = cmd.split_whitespace().next().unwrap_or_default();
|
||||
Request {
|
||||
tool: tool.into(),
|
||||
detail: cmd.clone(),
|
||||
rule: format!("bash:{word}"),
|
||||
}
|
||||
} else {
|
||||
Request {
|
||||
tool: tool.into(),
|
||||
detail: s("path"),
|
||||
rule: tool.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Always-allowed rules, persisted in `<project>/.hx/allow.json`.
|
||||
#[derive(Default)]
|
||||
pub struct Allow {
|
||||
rules: HashSet<String>,
|
||||
file: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl Allow {
|
||||
pub fn load(cwd: &Path) -> Self {
|
||||
let file = cwd.join(".hx/allow.json");
|
||||
let rules = std::fs::read_to_string(&file)
|
||||
.ok()
|
||||
.and_then(|s| serde_json::from_str(&s).ok())
|
||||
.unwrap_or_default();
|
||||
Self {
|
||||
rules,
|
||||
file: Some(file),
|
||||
}
|
||||
}
|
||||
pub fn has(&self, rule: &str) -> bool {
|
||||
self.rules.contains(rule)
|
||||
}
|
||||
/// Remember a rule; the file is best effort (the rule holds for this session either way).
|
||||
pub fn add(&mut self, rule: &str) {
|
||||
self.rules.insert(rule.into());
|
||||
if let Some(f) = &self.file {
|
||||
let mut v: Vec<_> = self.rules.iter().collect();
|
||||
v.sort();
|
||||
if let Some(d) = f.parent() {
|
||||
let _ = std::fs::create_dir_all(d);
|
||||
}
|
||||
let _ = std::fs::write(f, serde_json::to_string_pretty(&v).unwrap_or_default());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One file change by write/edit; `old` None = the file did not exist.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct FileEdit {
|
||||
pub path: PathBuf,
|
||||
pub old: Option<String>,
|
||||
pub new: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn rules_from_requests() {
|
||||
let r = request("bash", &json!({"command": "cargo test -q"}));
|
||||
assert_eq!(
|
||||
(r.rule.as_str(), r.detail.as_str()),
|
||||
("bash:cargo", "cargo test -q")
|
||||
);
|
||||
assert_eq!(request("edit", &json!({"path": "a.rs"})).rule, "edit");
|
||||
assert!(gated("bash") && !gated("read"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allow_persists() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let mut a = Allow::load(d.path());
|
||||
assert!(!a.has("bash:ls"));
|
||||
a.add("bash:ls");
|
||||
assert!(Allow::load(d.path()).has("bash:ls"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user