aegis-runtime-mv
Native Rust agent runtime: Axum WebSocket/SSE/REST gateway, heartbeat scheduler, dependency-aware task workflow, SQLite storage. Attachment of AienOS. Pre-alpha.
Brought by drakestapleton
aegis-runtime-mv / src / policy_guard.rs
| 1 | //! Sovereign sub-millisecond probe policy and tool call gating for AEGIS. |
| 2 | //! |
| 3 | //! Uses the native non-generative `aien-probe` engine to evaluate candidate actions |
| 4 | //! against security and containment probes before dispatching to the host. |
| 5 | |
| 6 | use crate::security::SecurityError; |
| 7 | use aien_probe::{ |
| 8 | Choice, ChoiceOption, DeterministicReferenceBackend, HttpProbeBackend, Noul, Probe, |
| 9 | ProbeBackend, ProbeEngine, ProbeSet, |
| 10 | }; |
| 11 | use serde_json::json; |
| 12 | |
| 13 | /// Evaluates candidate agent actions using sovereign non-generative probes. |
| 14 | pub struct ProbePolicyGuard<B: ProbeBackend + 'static> { |
| 15 | engine: ProbeEngine<B>, |
| 16 | min_safety_probability: f64, |
| 17 | } |
| 18 | |
| 19 | impl ProbePolicyGuard<DeterministicReferenceBackend> { |
| 20 | /// Constructs a policy guard using the in-process reference oracle. |
| 21 | pub fn new_reference(min_safety_probability: f64) -> Self { |
| 22 | Self { |
| 23 | engine: ProbeEngine::new(DeterministicReferenceBackend::new()), |
| 24 | min_safety_probability, |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | impl ProbePolicyGuard<HttpProbeBackend> { |
| 30 | /// Constructs a policy guard targeting a local GB10 inference endpoint. |
| 31 | pub fn new_http(endpoint: impl Into<String>, min_safety_probability: f64) -> Self { |
| 32 | Self { |
| 33 | engine: ProbeEngine::new(HttpProbeBackend::new(endpoint)), |
| 34 | min_safety_probability, |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl<B: ProbeBackend + 'static> ProbePolicyGuard<B> { |
| 40 | /// Constructs a policy guard over any custom probe engine backend. |
| 41 | pub fn new(engine: ProbeEngine<B>, min_safety_probability: f64) -> Self { |
| 42 | Self { |
| 43 | engine, |
| 44 | min_safety_probability, |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// Evaluates a candidate tool action or shell command against safety and containment probes. |
| 49 | pub async fn check_action( |
| 50 | &self, |
| 51 | action_name: &str, |
| 52 | payload: &str, |
| 53 | ) -> Result<bool, SecurityError> { |
| 54 | let state = json!({ |
| 55 | "action": action_name, |
| 56 | "payload": payload, |
| 57 | }); |
| 58 | |
| 59 | let probes = ProbeSet::new() |
| 60 | .with( |
| 61 | "safety", |
| 62 | Probe::Noul(Noul::new( |
| 63 | "Is this agent action safe, non-destructive, and strictly within security bounds?", |
| 64 | )), |
| 65 | ) |
| 66 | .with( |
| 67 | "boundary", |
| 68 | Probe::Choice(Choice::new( |
| 69 | "Classify permission scope of the proposed action.", |
| 70 | [ |
| 71 | ChoiceOption::new("contained").with_description("Operates inside permitted workspace"), |
| 72 | ChoiceOption::new("unauthorized_escalation").with_description("Escalates outside workspace"), |
| 73 | ], |
| 74 | )), |
| 75 | ); |
| 76 | |
| 77 | let response = |
| 78 | self.engine.evaluate(&state, &probes).await.map_err(|e| { |
| 79 | SecurityError::AccessDenied(format!("Probe evaluation failed: {e}")) |
| 80 | })?; |
| 81 | |
| 82 | let safety_prob = response.noul("safety").map(|a| a.noul).unwrap_or(0.0); |
| 83 | let boundary = response |
| 84 | .choice("boundary") |
| 85 | .map(|a| a.choice.clone()) |
| 86 | .unwrap_or_else(|| "unauthorized_escalation".to_string()); |
| 87 | |
| 88 | if safety_prob < self.min_safety_probability || boundary == "unauthorized_escalation" { |
| 89 | return Err(SecurityError::AccessDenied(format!( |
| 90 | "Action '{}' blocked by sovereign probe policy: safety {:.4} < {:.4}, scope '{}'", |
| 91 | action_name, safety_prob, self.min_safety_probability, boundary |
| 92 | ))); |
| 93 | } |
| 94 | |
| 95 | Ok(true) |
| 96 | } |
| 97 | |
| 98 | /// Pre-dispatch gate for a named skill plus JSON arguments. |
| 99 | /// Runs the deterministic membrane first, then the probe opinion. |
| 100 | pub async fn gate_skill( |
| 101 | &self, |
| 102 | skill_name: &str, |
| 103 | args: &serde_json::Value, |
| 104 | ) -> Result<(), SecurityError> { |
| 105 | if let Err(reason) = crate::enforcement::pre_dispatch_check(skill_name, args) { |
| 106 | return Err(SecurityError::AccessDenied(reason)); |
| 107 | } |
| 108 | let payload = args.to_string(); |
| 109 | self.check_action(skill_name, &payload).await.map(|_| ()) |
| 110 | } |
| 111 | } |