Allowlist with claude_cli: fail closed until hx#182; record provider mode (#18)

This commit was merged in pull request #18.
This commit is contained in:
hermes committed 2026-11-14 10:32:32 +00:00
1 parent 374cf28aed
commit a76c0ac16f
9 files changed
+259 -14

No files matched your search

+25
View File
@@ -112,6 +112,11 @@ fn usage_total(u: &Value) -> u64 {
.sum()
}
/// The line hx' claude_cli provider writes into the text stream for every tool claude runs: `\n[claude Bash: ls]\n`.
fn is_claude_tool_line(t: &str) -> bool {
t.starts_with("\n[claude ") && t.ends_with("]\n") && t.len() > 12
}
/// hx surfaces provider rejections as `api 429: ...`; also match the usual wording.
fn is_rate_limit(reason: &str) -> bool {
let r = reason.to_lowercase();
@@ -304,6 +309,9 @@ fn run_hx(
let deadline = Instant::now() + Duration::from_secs(p.wall_secs());
let mut done = None::<Value>;
// claude_cli: claude runs the tools; hx prints a `[claude TOOL: ...]` line per tool use and (hx#182) reports
// each one as a tool_call event so the guard can decide. Fewer events than lines = something ran unseen.
let (mut claude_lines, mut calls) = (0u32, 0u32);
let mut killed = None;
loop {
let left = deadline.saturating_duration_since(Instant::now());
@@ -320,7 +328,17 @@ fn run_hx(
killed = Some("budget:turns".to_string());
}
}
Some("stream")
if pol.is_some()
&& ev["event"]["type"] == "text_delta"
&& is_claude_tool_line(
ev["event"]["text"].as_str().unwrap_or_default(),
) =>
{
claude_lines += 1;
}
Some("tool_call") if pol.is_some() => {
calls += 1;
// second line of defence: hx treats a dead or slow guard as "continue"
let name = ev["name"].as_str().unwrap_or_default();
if let Decision::Deny(why) = pol.unwrap().decide(name, &ev["args"]) {
@@ -397,6 +415,13 @@ fn run_hx(
if let Some(k) = killed {
return Ok(Some(k));
}
if pol.is_some() && calls < claude_lines {
// fail closed: never call a run guarded when claude used tools hx did not report to the guard
return Ok(Some(format!(
"policy: claude used {claude_lines} tool(s) but hx reported {calls} tool_call event(s); the allowlist \
could not be enforced for this run (hx without claude_cli tool events, see johannes/hx#182)"
)));
}
Ok(match &done {
None => Some("hx-failed: no done event".into()),
Some(d) if !d["error"].is_null() => Some(format!(