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

+38 -15
View File
@@ -3,7 +3,7 @@
//! hx process that owns the TUI; that one shows the usual permission prompt.
use crate::permit::Request;
use crate::tool::{Tool, ToolCtx, ToolResult, gate};
use crate::tool::{Tool, ToolCtx, ToolResult, gate_why};
use async_trait::async_trait;
use hx_ai::ToolSpec;
use serde_json::{Value, json};
@@ -82,10 +82,10 @@ pub async fn serve(path: &Path, cx: ToolCtx) -> std::io::Result<()> {
let ok = if v["tool_name"] == "AskUserQuestion" {
// claude's own question tool: show hx's popup, hand the answers back as input
input = ask(&cx, &v["input"]).await;
input.is_some()
Ok(())
} else if let Some(q) = v.get("permit") {
let s = |k: &str| q[k].as_str().unwrap_or_default().to_string();
gate(
gate_why(
&cx,
Request {
tool: s("tool"),
@@ -96,12 +96,26 @@ pub async fn serve(path: &Path, cx: ToolCtx) -> std::io::Result<()> {
.await
} else {
match question(v["tool_name"].as_str().unwrap_or_default(), &v["input"]) {
None => true,
Some(q) => gate(&cx, q).await,
None => Ok(()),
Some(q) => gate_why(&cx, q).await,
}
};
let ok = ok.and_then(|()| {
if v["tool_name"] == "AskUserQuestion" && input.is_none() {
Err("denied by the user; do not retry, ask what to do instead".to_string())
} else {
Ok(())
}
});
let msg = ok.as_ref().err().cloned();
let _ = w
.write_all(format!("{}\n", json!({"allow": ok, "input": input})).as_bytes())
.write_all(
format!(
"{}\n",
json!({"allow": ok.is_ok(), "input": input, "message": msg})
)
.as_bytes(),
)
.await;
});
}
@@ -145,7 +159,6 @@ async fn roundtrip(path: &Path, msg: Value) -> Option<Value> {
ask.await
.ok()
.and_then(|l| serde_json::from_str::<Value>(&l).ok())
.filter(|v| v["allow"] == true)
}
/// Permission hook for tools that run in another process (`hx mcp-serve`): ask the owner of the socket.
@@ -157,7 +170,10 @@ pub fn remote_permit(path: PathBuf) -> crate::permit::Permit {
Box::pin(async move {
let m = json!({"permit": {"tool": r.tool, "detail": r.detail, "rule": r.rule}});
// the owner stores "always" itself; here a yes is a plain yes
if roundtrip(&path, m).await.is_some() {
if roundtrip(&path, m)
.await
.is_some_and(|v| v["allow"] == true)
{
Decision::Once
} else {
Decision::Deny
@@ -182,13 +198,20 @@ impl Tool for Approve {
}
async fn call(&self, args: Value, _cx: &ToolCtx) -> ToolResult {
let r = roundtrip(&self.0, args.clone()).await;
ToolResult::ok(if let Some(r) = r {
let input = if r["input"].is_null() { &args["input"] } else { &r["input"] };
json!({"behavior": "allow", "updatedInput": input})
} else {
json!({"behavior": "deny", "message": "denied by the user; do not retry, ask what to do instead"})
}
.to_string())
ToolResult::ok(
if let Some(r) = r.as_ref().filter(|r| r["allow"] == true) {
let input = if r["input"].is_null() {
&args["input"]
} else {
&r["input"]
};
json!({"behavior": "allow", "updatedInput": input})
} else {
json!({"behavior": "deny", "message": r.as_ref().and_then(|r| r["message"].as_str())
.unwrap_or("denied by the user; do not retry, ask what to do instead")})
}
.to_string(),
)
}
}