claude_cli: offer codemode to claude (codemode = true), hx tools inside it ask via the permission bridge

This commit is contained in:
hermes committed 2026-10-16 20:42:10 +00:00
1 parent 785effef11
commit fdeaaf5cf2
4 files changed
+111 -27

No files matched your search

+65 -16
View File
@@ -77,9 +77,23 @@ pub async fn serve(path: &Path, cx: ToolCtx) -> std::io::Result<()> {
return;
}
let v: Value = serde_json::from_str(&line).unwrap_or_default();
let ok = match question(v["tool_name"].as_str().unwrap_or_default(), &v["input"]) {
None => true,
Some(q) => gate(&cx, q).await,
// hx tools run by `codemode` inside mcp-serve ask for their own permission
let ok = if let Some(q) = v.get("permit") {
let s = |k: &str| q[k].as_str().unwrap_or_default().to_string();
gate(
&cx,
Request {
tool: s("tool"),
detail: s("detail"),
rule: s("rule"),
},
)
.await
} else {
match question(v["tool_name"].as_str().unwrap_or_default(), &v["input"]) {
None => true,
Some(q) => gate(&cx, q).await,
}
};
let _ = w
.write_all(format!("{}\n", json!({"allow": ok})).as_bytes())
@@ -88,6 +102,38 @@ pub async fn serve(path: &Path, cx: ToolCtx) -> std::io::Result<()> {
}
}
async fn roundtrip(path: &Path, msg: Value) -> bool {
let ask = async {
let mut c = UnixStream::connect(path).await?;
c.write_all(format!("{msg}\n").as_bytes()).await?;
let mut l = String::new();
BufReader::new(c).read_line(&mut l).await?;
std::io::Result::Ok(l)
};
ask.await
.ok()
.and_then(|l| serde_json::from_str::<Value>(&l).ok())
.is_some_and(|v| v["allow"] == true)
}
/// Permission hook for tools that run in another process (`hx mcp-serve`): ask the owner of the socket.
/// Unreachable socket = deny.
pub fn remote_permit(path: PathBuf) -> crate::permit::Permit {
use crate::permit::Decision;
std::sync::Arc::new(move |r| {
let path = path.clone();
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 {
Decision::Once
} else {
Decision::Deny
}
})
})
}
/// MCP tool `approve` for `--permission-prompt-tool`.
pub struct Approve(pub PathBuf);
@@ -103,18 +149,7 @@ impl Tool for Approve {
}
}
async fn call(&self, args: Value, _cx: &ToolCtx) -> ToolResult {
let ask = async {
let mut c = UnixStream::connect(&self.0).await?;
c.write_all(format!("{args}\n").as_bytes()).await?;
let mut l = String::new();
BufReader::new(c).read_line(&mut l).await?;
std::io::Result::Ok(l)
};
let ok = ask
.await
.ok()
.and_then(|l| serde_json::from_str::<Value>(&l).ok())
.is_some_and(|v| v["allow"] == true);
let ok = roundtrip(&self.0, args.clone()).await;
ToolResult::ok(if ok {
json!({"behavior": "allow", "updatedInput": args["input"]})
} else {
@@ -175,6 +210,20 @@ mod tests {
};
assert!(ask("cargo test").await.contains("\"allow\""));
assert!(ask("rm -rf x").await.contains("\"deny\""));
let _ = std::fs::remove_file(path);
// tools run in another process ask through the same socket
let remote = remote_permit(path.clone());
let req = |d: &str| crate::permit::Request {
tool: "bash".into(),
detail: d.into(),
rule: String::new(),
};
assert_eq!(remote(req("cargo test")).await, Decision::Once);
assert_eq!(remote(req("rm -rf x")).await, Decision::Deny);
let _ = std::fs::remove_file(&path);
assert_eq!(
remote(req("cargo test")).await,
Decision::Deny,
"no socket = deny"
);
}
}