interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / crates / aienos-aegis / src / evaluator.rs

242 lines · 8936 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! AEGIS Policy Evaluator: Enforcing cryptographic capabilities and reversibility invariants (ยง5).
17
18use crate::capability::{path_within, CapabilityGraph, CapabilityScope};
19use crate::effect::{EffectIntent, OperatorGrant};
20use serde::{Deserialize, Serialize};
21
22/// Policy evaluation outcome emitted by AEGIS.
23#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
24pub enum AegisDecision {
25 Approved { reason: String },
26 RequiresOperatorApproval { intent_id: [u8; 16], reason: String },
27 Rejected { reason: String },
28}
29
30/// Deterministic AEGIS Evaluator.
31///
32/// Intelligence proposes; deterministic authority disposes. The model cannot authorize itself.
33pub struct AegisEvaluator {
34 operator_secret: [u8; 32],
35}
36
37impl AegisEvaluator {
38 /// Create new AEGIS Evaluator bound to the operator's sovereign root secret.
39 pub fn new(operator_secret: [u8; 32]) -> Self {
40 Self { operator_secret }
41 }
42
43 /// Evaluate an effect intent against the capability graph and reversibility policy.
44 pub fn evaluate(
45 &self,
46 intent: &EffectIntent,
47 graph: &CapabilityGraph,
48 grant: Option<&OperatorGrant>,
49 now_utc: u64,
50 ) -> AegisDecision {
51 self.evaluate_with_world_binding(intent, graph, grant, now_utc, false)
52 }
53
54 /// The broker sets `world_bound` only when it owns an active World delta
55 /// and will route the effect to that delta rather than a general handler.
56 pub(crate) fn evaluate_with_world_binding(
57 &self,
58 intent: &EffectIntent,
59 graph: &CapabilityGraph,
60 grant: Option<&OperatorGrant>,
61 now_utc: u64,
62 world_bound: bool,
63 ) -> AegisDecision {
64 // 1. Retrieve and cryptographically validate the claimed capability token
65 let token = match graph.get_token(&intent.claimed_capability_id) {
66 Ok(t) => t,
67 Err(e) => {
68 return AegisDecision::Rejected {
69 reason: e.to_string(),
70 }
71 }
72 };
73
74 if let Err(e) = graph.validate_token(&token, now_utc) {
75 return AegisDecision::Rejected {
76 reason: e.to_string(),
77 };
78 }
79
80 // 2. Validate token holder matches proposing agent
81 if token.holder != intent.agent_id {
82 return AegisDecision::Rejected {
83 reason: format!(
84 "Agent {} is not authorized holder of token {}",
85 intent.agent_id, token.holder
86 ),
87 };
88 }
89
90 // 3. Match capability scope against requested action
91 if !Self::scope_authorizes_action(&token.scope, &intent.action, &intent.parameters) {
92 return AegisDecision::Rejected {
93 reason: format!(
94 "Capability scope {:?} does not authorize action \"{}\"",
95 token.scope, intent.action
96 ),
97 };
98 }
99
100 // 4. Only a broker-owned World delta can establish local reversibility.
101 if world_bound
102 && intent.is_reversible
103 && intent.world_id.is_some()
104 && matches!(intent.action.as_str(), "fs.write" | "fs.delete")
105 {
106 return AegisDecision::Approved {
107 reason: "Effect routed to an active J-Space World delta".to_string(),
108 };
109 }
110
111 let is_standing_fetch = matches!(token.scope, CapabilityScope::StandingFetch { .. })
112 || (intent.action == "fs.read" || intent.action == "hardware.inspect");
113
114 if is_standing_fetch && !Self::is_high_impact_boundary(&intent.action) {
115 return AegisDecision::Approved {
116 reason: format!(
117 "Autonomous effect authorized via Standing Capability {:?}",
118 token.scope
119 ),
120 };
121 }
122
123 // Irreversible or high-impact effect: strictly requires valid OperatorGrant
124 if let Some(og) = grant {
125 if og.intent_id != intent.id {
126 return AegisDecision::Rejected {
127 reason: "Operator grant intent ID does not match proposed intent".to_string(),
128 };
129 }
130 if !og.verify(&self.operator_secret) {
131 return AegisDecision::Rejected {
132 reason: "Cryptographic signature on operator grant is invalid".to_string(),
133 };
134 }
135 AegisDecision::Approved {
136 reason: format!(
137 "Irreversible action approved by operator grant from {}",
138 og.operator_id
139 ),
140 }
141 } else {
142 AegisDecision::RequiresOperatorApproval {
143 intent_id: intent.id,
144 reason: format!(
145 "Irreversible action \"{}\" crosses boundary and requires explicit operator authorization",
146 intent.action
147 ),
148 }
149 }
150 }
151
152 /// Check if action is high impact boundary requiring human operator grant.
153 pub fn is_high_impact_boundary(action: &str) -> bool {
154 matches!(
155 action,
156 "disk.format"
157 | "fs.delete"
158 | "fs.write"
159 | "git.publish"
160 | "code.publish"
161 | "kernel.replace"
162 | "system.install"
163 | "credential.modify"
164 | "finance.spend"
165 )
166 }
167
168 fn scope_authorizes_action(
169 scope: &CapabilityScope,
170 action: &str,
171 params: &std::collections::BTreeMap<String, String>,
172 ) -> bool {
173 match scope {
174 CapabilityScope::SystemAdmin => true,
175 CapabilityScope::Filesystem {
176 path_prefix,
177 read_only,
178 } => {
179 if action == "fs.read" {
180 if let Some(path) = params.get("path") {
181 path_within(path, path_prefix)
182 } else {
183 false
184 }
185 } else if action == "fs.write" || action == "fs.delete" {
186 if *read_only {
187 false
188 } else if let Some(path) = params.get("path") {
189 path_within(path, path_prefix)
190 } else {
191 false
192 }
193 } else {
194 false
195 }
196 }
197 CapabilityScope::StandingFetch { host } => {
198 if action == "net.fetch" || action == "net.read" {
199 let req_host = params.get("host").map(|s| s.as_str()).unwrap_or("");
200 host == "*" || host == req_host
201 } else {
202 false
203 }
204 }
205 CapabilityScope::Network { host, port } => {
206 if action == "net.connect" || action == "net.send" {
207 let req_host = params.get("host").map(|s| s.as_str()).unwrap_or("");
208 let req_port: u16 =
209 params.get("port").and_then(|p| p.parse().ok()).unwrap_or(0);
210
211 (host == "*" || host == req_host) && (*port == 0 || *port == req_port)
212 } else {
213 false
214 }
215 }
216 CapabilityScope::Device {
217 device_name,
218 read_only,
219 } => {
220 if action.starts_with("device.") {
221 let req_dev = params.get("device").map(|s| s.as_str()).unwrap_or("");
222 let is_write = action == "device.write";
223 req_dev == device_name && (!is_write || !*read_only)
224 } else {
225 false
226 }
227 }
228 CapabilityScope::World { .. } => action.starts_with("world."),
229 CapabilityScope::Custom {
230 namespace,
231 action: act,
232 } => {
233 if let Some(suffix) = action.strip_prefix(&format!("{}.", namespace)) {
234 act == "*" || act == suffix
235 } else {
236 false
237 }
238 }
239 _ => false,
240 }
241 }
242}