Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/aien-mcp/tests/authority.rs # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / aien-mcp / tests / authority.rs` 341 lines ยท 11644 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 //! Negative controls for the host-owned authority seam. This file is outside the crate, so it 17 //! can touch only the public API: no `AuthorizedEffect` can be built here except through 18 //! `EffectLane::authorize`. 19 use std::sync::atomic::{AtomicUsize, Ordering}; 20 use std::sync::Arc; 21 22 use aien_capability::{ 23 Digest32, EffectId, JNodeId, ProviderId, ToolDescriptor, ToolEffects, WorldId, 24 }; 25 use aien_mcp::memory::MemoryWire; 26 use aien_mcp::{ 27 AuthorityContext, AuthorityDecision, AuthorityOutcome, CallOutcome, EffectAuthority, 28 EffectClassAuthority, EffectIntent, EffectLane, EffectScope, Exposure, McpBroker, TrustLevel, 29 }; 30 use serde_json::json; 31 32 struct Fixed(AuthorityDecision); 33 impl EffectAuthority for Fixed { 34 fn authorize(&self, _: &EffectIntent, _: &AuthorityContext) -> AuthorityDecision { 35 self.0.clone() 36 } 37 } 38 39 struct H { 40 lane: EffectLane, 41 calls: Arc, 42 intent: EffectIntent, 43 } 44 45 fn scope(label: &str) -> EffectScope { 46 EffectScope { 47 world_id: WorldId(1), 48 winning_jnode: JNodeId(1), 49 idempotency_key: EffectId::from_label(label), 50 } 51 } 52 53 async fn harness(effects: ToolEffects) -> H { 54 let calls = Arc::new(AtomicUsize::new(0)); 55 let c = calls.clone(); 56 let tool = ToolDescriptor::new("t", effects, Digest32::of(b"t")); 57 let wire = MemoryWire::new(vec![tool], move |_, _| { 58 c.fetch_add(1, Ordering::SeqCst); 59 CallOutcome::Finished(json!({"done": true})) 60 }); 61 let broker = McpBroker::new(); 62 let provider = ProviderId::new("p"); 63 broker 64 .admit(provider.clone(), Arc::new(wire)) 65 .await 66 .unwrap(); 67 // An intent is plain data (public fields). Possessing one grants nothing. 68 let live = aien_mcp::SpeculativeLane::new(broker.clone()) 69 .discovery_snapshot(&provider) 70 .await 71 .unwrap() 72 .catalog_digest; 73 let intent = EffectIntent { 74 provider, 75 tool_name: "t".into(), 76 arguments: json!({"a": 1}), 77 capability_digest: live, 78 }; 79 H { 80 lane: EffectLane::new(broker), 81 calls, 82 intent, 83 } 84 } 85 86 #[tokio::test] 87 async fn deny_mints_nothing_and_never_executes() { 88 let h = harness(ToolEffects::PURE).await; 89 let a = Fixed(AuthorityDecision::Deny("no".into())); 90 assert!(h.lane.authorize(h.intent.clone(), scope("d"), &a).is_err()); 91 let r = h.lane.authorize_and_execute(h.intent, scope("d"), &a).await; 92 assert_eq!(r.unwrap_err(), AuthorityOutcome::Denied("no".into())); 93 assert_eq!(h.calls.load(Ordering::SeqCst), 0); 94 } 95 96 #[tokio::test] 97 async fn contain_mints_nothing_and_never_executes() { 98 let h = harness(ToolEffects::PURE).await; 99 let a = Fixed(AuthorityDecision::Contain("box".into())); 100 let r = h.lane.authorize_and_execute(h.intent, scope("c"), &a).await; 101 assert_eq!(r.unwrap_err(), AuthorityOutcome::Contained("box".into())); 102 assert_eq!(h.calls.load(Ordering::SeqCst), 0); 103 } 104 105 #[tokio::test] 106 async fn require_approval_is_pending_and_never_executes() { 107 let h = harness(ToolEffects::WORLD_MUTATION).await; 108 let want = aien_mcp::intent_digest(&h.intent); 109 let r = h 110 .lane 111 .authorize_and_execute(h.intent, scope("p"), &EffectClassAuthority) 112 .await; 113 match r.unwrap_err() { 114 AuthorityOutcome::Pending { intent_digest, .. } => assert_eq!(intent_digest, want), 115 other => panic!("expected Pending, got {other:?}"), 116 } 117 assert_eq!(h.calls.load(Ordering::SeqCst), 0); 118 } 119 120 #[tokio::test] 121 async fn allow_executes_and_returns_receipt() { 122 let h = harness(ToolEffects::LOCAL_EPHEMERAL).await; 123 let r = h 124 .lane 125 .authorize_and_execute(h.intent, scope("a"), &EffectClassAuthority) 126 .await 127 .unwrap(); 128 assert_eq!(r.output, json!({"done": true})); 129 assert_eq!(r.tool_name, "t"); 130 assert_eq!(h.calls.load(Ordering::SeqCst), 1); 131 } 132 133 #[tokio::test] 134 async fn allow_restricted_with_restrictions_is_denied_and_nothing_runs() { 135 // The lane cannot enforce a restriction list, so it must not mint as if it were `Allow`. 136 let h = harness(ToolEffects::PURE).await; 137 let a = Fixed(AuthorityDecision::AllowRestricted(vec!["x".into()])); 138 let r = h.lane.authorize_and_execute(h.intent, scope("r"), &a).await; 139 assert!(matches!(r.unwrap_err(), AuthorityOutcome::Denied(_))); 140 assert_eq!(h.calls.load(Ordering::SeqCst), 0); 141 } 142 143 #[tokio::test] 144 async fn allow_restricted_without_restrictions_mints() { 145 let h = harness(ToolEffects::PURE).await; 146 let a = Fixed(AuthorityDecision::AllowRestricted(Vec::new())); 147 assert!(h 148 .lane 149 .authorize_and_execute(h.intent, scope("r"), &a) 150 .await 151 .is_ok()); 152 assert_eq!(h.calls.load(Ordering::SeqCst), 1); 153 } 154 155 #[tokio::test] 156 async fn unknown_descriptor_is_denied() { 157 let h = harness(ToolEffects::PURE).await; 158 let mut intent = h.intent.clone(); 159 intent.tool_name = "ghost".into(); 160 let r = h 161 .lane 162 .authorize_and_execute(intent, scope("u"), &EffectClassAuthority) 163 .await; 164 assert!(matches!(r.unwrap_err(), AuthorityOutcome::Denied(_))); 165 assert_eq!(h.calls.load(Ordering::SeqCst), 0); 166 } 167 168 #[tokio::test] 169 async fn stale_intent_is_denied_before_the_authority_is_asked() { 170 let h = harness(ToolEffects::PURE).await; 171 let mut intent = h.intent.clone(); 172 intent.capability_digest = Digest32::of(b"old"); 173 let a = Fixed(AuthorityDecision::Allow); 174 let r = h.lane.authorize_and_execute(intent, scope("s"), &a).await; 175 assert!(matches!(r.unwrap_err(), AuthorityOutcome::Denied(_))); 176 assert_eq!(h.calls.load(Ordering::SeqCst), 0); 177 } 178 179 #[tokio::test] 180 async fn effect_class_table() { 181 use AuthorityDecision as D; 182 let cases = [ 183 (ToolEffects::PURE, "allow"), 184 (ToolEffects::READ_FILESYSTEM, "allow"), 185 (ToolEffects::READ_NETWORK, "allow"), 186 (ToolEffects::LOCAL_EPHEMERAL, "allow"), 187 (ToolEffects::EXTERNAL_WRITE, "approval"), 188 (ToolEffects::WORLD_MUTATION, "approval"), 189 (ToolEffects::SECRET_BEARING, "approval"), 190 (ToolEffects::SPAWN_PROCESS, "approval"), 191 (ToolEffects::EXTERNAL_IRREVERSIBLE, "deny"), 192 ( 193 ToolEffects::EXTERNAL_IRREVERSIBLE | ToolEffects::READ_FILESYSTEM, 194 "deny", 195 ), 196 ]; 197 for (effects, want) in cases { 198 let h = harness(effects).await; 199 let r = h 200 .lane 201 .authorize(h.intent.clone(), scope("t"), &EffectClassAuthority); 202 let got = match &r { 203 Ok(_) => "allow", 204 Err(AuthorityOutcome::Pending { .. }) => "approval", 205 Err(AuthorityOutcome::Denied(_)) => "deny", 206 Err(_) => "other", 207 }; 208 assert_eq!(got, want, "{effects:?}"); 209 } 210 let _ = D::Allow; 211 } 212 213 // ---- E3: exposure input (INTERPLANE 0.3 cut E3) ---- 214 215 fn exposure(floor: TrustLevel) -> Exposure { 216 Exposure::new(vec!["in-1".into()], floor) 217 } 218 219 const EFFECT_BITS: [ToolEffects; 4] = [ 220 ToolEffects::EXTERNAL_WRITE, 221 ToolEffects::WORLD_MUTATION, 222 ToolEffects::SECRET_BEARING, 223 ToolEffects::SPAWN_PROCESS, 224 ]; 225 226 #[test] 227 fn trust_order_and_unknown_wire_value() { 228 assert!(TrustLevel::TrustedRuntime > TrustLevel::UserSupplied); 229 assert!(TrustLevel::UserSupplied > TrustLevel::WorkspaceUntrusted); 230 assert!(TrustLevel::WorkspaceUntrusted > TrustLevel::ExternalUntrusted); 231 assert_eq!( 232 TrustLevel::from_wire("user_supplied"), 233 TrustLevel::UserSupplied 234 ); 235 assert_eq!( 236 TrustLevel::from_wire("unknown"), 237 TrustLevel::ExternalUntrusted 238 ); 239 assert_eq!( 240 TrustLevel::from_wire("garbage"), 241 TrustLevel::ExternalUntrusted 242 ); 243 } 244 245 #[tokio::test] 246 async fn untrusted_exposure_makes_effects_pending_never_allow() { 247 for floor in [ 248 TrustLevel::WorkspaceUntrusted, 249 TrustLevel::ExternalUntrusted, 250 ] { 251 for fx in EFFECT_BITS { 252 let h = harness(fx).await; 253 let r = h 254 .lane 255 .with_exposure(exposure(floor)) 256 .authorize_and_execute(h.intent, scope("x"), &EffectClassAuthority) 257 .await; 258 match r { 259 Err(AuthorityOutcome::Pending { reason, .. }) => { 260 assert!(reason.contains("exposure"), "{reason}") 261 } 262 other => panic!("expected Pending, got {other:?}"), 263 } 264 assert_eq!(h.calls.load(Ordering::SeqCst), 0); 265 } 266 } 267 } 268 269 #[tokio::test] 270 async fn read_class_is_unaffected_by_exposure() { 271 for fx in [ 272 ToolEffects::PURE, 273 ToolEffects::READ_FILESYSTEM, 274 ToolEffects::READ_NETWORK, 275 ] { 276 let h = harness(fx).await; 277 let r = h 278 .lane 279 .with_exposure(exposure(TrustLevel::ExternalUntrusted)) 280 .authorize_and_execute(h.intent, scope("r"), &EffectClassAuthority) 281 .await; 282 assert!(r.is_ok()); 283 assert_eq!(h.calls.load(Ordering::SeqCst), 1); 284 } 285 } 286 287 #[tokio::test] 288 async fn absent_exposure_fails_closed_for_effects_only() { 289 let h = harness(ToolEffects::EXTERNAL_WRITE).await; 290 let r = h 291 .lane 292 .authorize(h.intent.clone(), scope("n"), &EffectClassAuthority); 293 match r { 294 Err(AuthorityOutcome::Pending { reason, .. }) => assert!(reason.contains("exposure")), 295 other => panic!("expected Pending, got {other:?}"), 296 } 297 let h = harness(ToolEffects::PURE).await; 298 assert!(h 299 .lane 300 .authorize(h.intent.clone(), scope("n2"), &EffectClassAuthority) 301 .is_ok()); 302 } 303 304 #[tokio::test] 305 async fn context_exposes_only_what_the_host_set() { 306 struct Probe(std::sync::Mutex>>); 307 impl EffectAuthority for Probe { 308 fn authorize(&self, _: &EffectIntent, c: &AuthorityContext) -> AuthorityDecision { 309 *self.0.lock().unwrap() = Some(c.exposure().cloned()); 310 AuthorityDecision::Allow 311 } 312 } 313 let h = harness(ToolEffects::PURE).await; 314 let p = Probe(Default::default()); 315 h.lane.authorize(h.intent.clone(), scope("p1"), &p).unwrap(); 316 assert_eq!(p.0.lock().unwrap().take(), Some(None)); 317 let e = exposure(TrustLevel::UserSupplied); 318 h.lane 319 .with_exposure(e.clone()) 320 .authorize(h.intent.clone(), scope("p2"), &p) 321 .unwrap(); 322 assert_eq!(p.0.lock().unwrap().take(), Some(Some(e))); 323 } 324 325 #[tokio::test] 326 async fn user_supplied_or_better_keeps_the_existing_effect_table() { 327 for floor in [TrustLevel::UserSupplied, TrustLevel::TrustedRuntime] { 328 let h = harness(ToolEffects::EXTERNAL_WRITE).await; 329 let r = h.lane.with_exposure(exposure(floor)).authorize( 330 h.intent.clone(), 331 scope("t"), 332 &EffectClassAuthority, 333 ); 334 match r { 335 Err(AuthorityOutcome::Pending { reason, .. }) => { 336 assert!(!reason.contains("exposure"), "{reason}") 337 } 338 other => panic!("expected Pending, got {other:?}"), 339 } 340 } 341 }