Skip to main content

policy_approval/
policy_approval.rs

1use serde_json::json;
2use yoagent_state::{
3    ActorRef, MemoryEventStore, NodeId, Policy, PolicyAction, PolicyId, StateError, YoAgentRuntime,
4    YoAgentState,
5};
6
7#[tokio::main]
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let state = YoAgentState::load(MemoryEventStore::new()).await?;
10    let mut runtime = YoAgentRuntime::new(state.clone());
11    runtime.register_policy(Policy::require_approval(
12        PolicyId::new("policy_create_node_review"),
13        "Creating graph nodes requires review",
14        PolicyAction::CreateNode,
15    ));
16
17    let result = runtime
18        .create_typed_node(
19            ActorRef::agent("demo"),
20            NodeId::new("sensitive_node"),
21            "memory",
22            json!({ "title": "Potentially sensitive memory" }),
23        )
24        .await;
25
26    match result {
27        Err(StateError::PolicyDenied(message)) => println!("blocked: {message}"),
28        other => println!("unexpected: {other:?}"),
29    }
30
31    let approvals = state
32        .graph()
33        .await
34        .nodes
35        .values()
36        .filter(|node| node.kind == "approval_request")
37        .count();
38    println!("approval_requests={approvals}");
39    Ok(())
40}