Skip to main content

tool_gate/
tool_gate.rs

1//! Minimal tool-call gate demo (safety MVP story).
2//!
3//! A *simulated* model proposes tool names. `ToolGateExecutor` is the last line
4//! of defense: allow-list + deny-list, no real side effects, allow/deny written
5//! to `audit.jsonl`.
6//!
7//! This is **not** a full LLM proxy. It proves the gate + audit shape that a
8//! later API adapter can sit behind.
9//!
10//! ```bash
11//! cargo run --example tool_gate
12//! ```
13
14use hashbrown::HashMap;
15use omega_cognitive_kernel::types::Action;
16use omega_cognitive_kernel::{AuditLog, AuditSummary, Executor, Result, ToolGateExecutor};
17use std::env::temp_dir;
18use std::path::PathBuf;
19
20fn tool(name: &str) -> Action {
21    Action {
22        name: name.into(),
23        params: HashMap::new(),
24        constraints_checked: true,
25    }
26}
27
28fn main() -> Result<()> {
29    let root: PathBuf = temp_dir().join(format!(
30        "omega-tool-gate-{}",
31        omega_cognitive_kernel::new_uuid()
32    ));
33    std::fs::create_dir_all(&root).ok();
34    let state_marker = root.join("state.json"); // audit lives beside this path
35    let audit = AuditLog::beside_state(&state_marker);
36
37    println!("╔══════════════════════════════════════════════════╗");
38    println!("║  Omega tool-gate — block high-risk tool calls    ║");
39    println!("╚══════════════════════════════════════════════════╝");
40    println!("audit: {}", audit.path.display());
41    println!();
42
43    // Safe tools an agent may call; dangerous names explicitly denied.
44    let mut gate =
45        ToolGateExecutor::new(["search_docs".into(), "http_get".into(), "summarize".into()])
46            .deny([
47                "delete_database".into(),
48                "shell_exec".into(),
49                "send_wire_transfer".into(),
50            ])
51            .with_audit(AuditLog {
52                path: audit.path.clone(),
53            })
54            .echo(true)
55            .fail_closed(false); // continue batch after deny so demo shows mixed outcomes
56
57    // Simulated model proposals (not from DecisionMaker — host/agent layer).
58    let proposals = [
59        tool("search_docs"),
60        tool("http_get"),
61        tool("delete_database"), // should DENY
62        tool("summarize"),
63        tool("shell_exec"),   // should DENY
64        tool("unknown_tool"), // not on allow-list → DENY
65    ];
66
67    println!("── model proposed tools ──");
68    let results = gate.execute(&proposals)?;
69    for r in &results {
70        println!("  → {r}");
71    }
72
73    let sum: AuditSummary = AuditLog {
74        path: audit.path.clone(),
75    }
76    .summarize()?;
77    println!("\n── audit summary ──");
78    println!(
79        "events={}  tools_allowed={}  tools_denied={}",
80        sum.total_events, sum.tools_allowed, sum.tools_denied
81    );
82    for (name, n) in &sum.action_counts {
83        println!("  {name:<24} {n}");
84    }
85
86    println!("\n── takeaway ──");
87    println!("1. Proposals come from outside (fake LLM / agent).");
88    println!("2. ToolGateExecutor allow + deny is the enforcement point.");
89    println!("3. Denies are auditable (ToolDenied) — no side effects executed.");
90    println!("4. Next engineering step: wire real HTTP tools only on ALLOW path.");
91
92    let _ = std::fs::remove_dir_all(&root);
93    Ok(())
94}