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
aien-sovereign-core-mv / crates / aien-mcp / src / lane_tests.rs
| 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 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 17 | use std::sync::Arc; |
| 18 | |
| 19 | use aien_capability::{ |
| 20 | Digest32, EffectId, JNodeId, ProviderId, ToolDescriptor, ToolEffects, WorldId, |
| 21 | }; |
| 22 | use serde_json::json; |
| 23 | |
| 24 | use crate::memory::MemoryWire; |
| 25 | use crate::{ |
| 26 | authorize_for_test, CallOutcome, EffectLane, Error, McpBroker, SpeculativeLane, |
| 27 | SpeculativeToolCall, |
| 28 | }; |
| 29 | |
| 30 | fn tool(name: &str, effects: ToolEffects) -> ToolDescriptor { |
| 31 | ToolDescriptor::new(name, effects, Digest32::of(name.as_bytes())) |
| 32 | } |
| 33 | |
| 34 | struct Harness { |
| 35 | broker: McpBroker, |
| 36 | calls: Arc<AtomicUsize>, |
| 37 | provider: ProviderId, |
| 38 | } |
| 39 | |
| 40 | async fn harness(tools: Vec<ToolDescriptor>, outcome: CallOutcome) -> Harness { |
| 41 | let calls = Arc::new(AtomicUsize::new(0)); |
| 42 | let counted = calls.clone(); |
| 43 | let wire = MemoryWire::new(tools, move |_name, _args| { |
| 44 | counted.fetch_add(1, Ordering::SeqCst); |
| 45 | outcome.clone() |
| 46 | }); |
| 47 | let broker = McpBroker::new(); |
| 48 | let provider = ProviderId::new("mail"); |
| 49 | broker |
| 50 | .admit(provider.clone(), Arc::new(wire)) |
| 51 | .await |
| 52 | .unwrap(); |
| 53 | Harness { |
| 54 | broker, |
| 55 | calls, |
| 56 | provider, |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[tokio::test] |
| 61 | async fn unadmitted_provider_cannot_be_discovered() { |
| 62 | let broker = McpBroker::new(); |
| 63 | let lane = SpeculativeLane::new(broker); |
| 64 | let err = lane |
| 65 | .discovery_snapshot(&ProviderId::new("unknown")) |
| 66 | .await |
| 67 | .unwrap_err(); |
| 68 | assert!(matches!(err, Error::NotAdmitted(_))); |
| 69 | } |
| 70 | |
| 71 | #[tokio::test] |
| 72 | async fn discovery_returns_a_snapshot_and_does_not_call_a_tool() { |
| 73 | let h = harness( |
| 74 | vec![tool("mail.search", ToolEffects::READ_NETWORK)], |
| 75 | CallOutcome::Finished(json!({})), |
| 76 | ) |
| 77 | .await; |
| 78 | let lane = SpeculativeLane::new(h.broker.clone()); |
| 79 | let snapshot = lane.discovery_snapshot(&h.provider).await.unwrap(); |
| 80 | assert_eq!(snapshot.provider, h.provider); |
| 81 | assert_eq!(snapshot.tools.len(), 1); |
| 82 | assert_eq!(snapshot.tools[0].name(), "mail.search"); |
| 83 | assert_ne!(snapshot.catalog_digest, Digest32::of(&[])); |
| 84 | assert!(snapshot.expires_at > snapshot.generated_at); |
| 85 | assert_eq!(h.calls.load(Ordering::SeqCst), 0); |
| 86 | } |
| 87 | |
| 88 | #[tokio::test] |
| 89 | async fn read_only_tool_call_runs_on_the_speculative_lane() { |
| 90 | let h = harness( |
| 91 | vec![tool( |
| 92 | "mail.search", |
| 93 | ToolEffects::READ_NETWORK | ToolEffects::SECRET_BEARING, |
| 94 | )], |
| 95 | CallOutcome::Finished(json!({"hits": 1})), |
| 96 | ) |
| 97 | .await; |
| 98 | let lane = SpeculativeLane::new(h.broker); |
| 99 | let result = lane |
| 100 | .invoke_speculative(SpeculativeToolCall { |
| 101 | provider: h.provider, |
| 102 | tool_name: "mail.search".into(), |
| 103 | arguments: json!({}), |
| 104 | sandboxed: false, |
| 105 | }) |
| 106 | .await |
| 107 | .unwrap(); |
| 108 | assert_eq!(result.output, json!({"hits": 1})); |
| 109 | assert_eq!(h.calls.load(Ordering::SeqCst), 1); |
| 110 | } |
| 111 | |
| 112 | #[tokio::test] |
| 113 | async fn irreversible_tool_call_is_rejected_before_the_session_is_used() { |
| 114 | let h = harness( |
| 115 | vec![tool("mail.send", ToolEffects::EXTERNAL_IRREVERSIBLE)], |
| 116 | CallOutcome::Finished(json!({"sent": true})), |
| 117 | ) |
| 118 | .await; |
| 119 | let lane = SpeculativeLane::new(h.broker); |
| 120 | let err = lane |
| 121 | .invoke_speculative(SpeculativeToolCall { |
| 122 | provider: h.provider, |
| 123 | tool_name: "mail.send".into(), |
| 124 | arguments: json!({"to": "a@b.c"}), |
| 125 | sandboxed: true, |
| 126 | }) |
| 127 | .await |
| 128 | .unwrap_err(); |
| 129 | assert!(matches!(err, Error::EffectRequiresCommit)); |
| 130 | assert_eq!(h.calls.load(Ordering::SeqCst), 0); |
| 131 | } |
| 132 | |
| 133 | #[tokio::test] |
| 134 | async fn local_ephemeral_requires_a_sandbox() { |
| 135 | let h = harness( |
| 136 | vec![tool( |
| 137 | "build.compile", |
| 138 | ToolEffects::SPAWN_PROCESS | ToolEffects::LOCAL_EPHEMERAL, |
| 139 | )], |
| 140 | CallOutcome::Finished(json!({"ok": true})), |
| 141 | ) |
| 142 | .await; |
| 143 | let lane = SpeculativeLane::new(h.broker.clone()); |
| 144 | let err = lane |
| 145 | .invoke_speculative(SpeculativeToolCall { |
| 146 | provider: h.provider.clone(), |
| 147 | tool_name: "build.compile".into(), |
| 148 | arguments: json!({}), |
| 149 | sandboxed: false, |
| 150 | }) |
| 151 | .await |
| 152 | .unwrap_err(); |
| 153 | assert!(matches!(err, Error::SandboxRequired)); |
| 154 | assert_eq!(h.calls.load(Ordering::SeqCst), 0); |
| 155 | |
| 156 | lane.invoke_speculative(SpeculativeToolCall { |
| 157 | provider: h.provider, |
| 158 | tool_name: "build.compile".into(), |
| 159 | arguments: json!({}), |
| 160 | sandboxed: true, |
| 161 | }) |
| 162 | .await |
| 163 | .unwrap(); |
| 164 | assert_eq!(h.calls.load(Ordering::SeqCst), 1); |
| 165 | } |
| 166 | |
| 167 | #[tokio::test] |
| 168 | async fn irreversible_work_is_staged_and_the_session_is_not_called() { |
| 169 | let h = harness( |
| 170 | vec![tool( |
| 171 | "mail.send", |
| 172 | ToolEffects::EXTERNAL_IRREVERSIBLE | ToolEffects::SECRET_BEARING, |
| 173 | )], |
| 174 | CallOutcome::Finished(json!({})), |
| 175 | ) |
| 176 | .await; |
| 177 | let lane = SpeculativeLane::new(h.broker); |
| 178 | let intent = lane |
| 179 | .stage_effect_intent(&h.provider, "mail.send", json!({"to": "a@b.c"})) |
| 180 | .await |
| 181 | .unwrap(); |
| 182 | assert_eq!(intent.tool_name, "mail.send"); |
| 183 | assert_eq!(h.calls.load(Ordering::SeqCst), 0); |
| 184 | } |
| 185 | |
| 186 | #[tokio::test] |
| 187 | async fn speculation_safe_tool_is_not_staged_as_an_external_effect() { |
| 188 | let h = harness( |
| 189 | vec![tool("mail.search", ToolEffects::READ_NETWORK)], |
| 190 | CallOutcome::Finished(json!({})), |
| 191 | ) |
| 192 | .await; |
| 193 | let lane = SpeculativeLane::new(h.broker); |
| 194 | let err = lane |
| 195 | .stage_effect_intent(&h.provider, "mail.search", json!({})) |
| 196 | .await |
| 197 | .unwrap_err(); |
| 198 | assert!(matches!(err, Error::SpeculationSafe)); |
| 199 | } |
| 200 | |
| 201 | #[tokio::test] |
| 202 | async fn effect_lane_calls_the_warm_session_when_the_catalog_digest_matches() { |
| 203 | let h = harness( |
| 204 | vec![tool("mail.send", ToolEffects::EXTERNAL_IRREVERSIBLE)], |
| 205 | CallOutcome::Finished(json!({"id": "m1"})), |
| 206 | ) |
| 207 | .await; |
| 208 | let speculative = SpeculativeLane::new(h.broker.clone()); |
| 209 | let snapshot = speculative.discovery_snapshot(&h.provider).await.unwrap(); |
| 210 | let epoch = snapshot.session_epoch; |
| 211 | let intent = speculative |
| 212 | .stage_effect_intent(&h.provider, "mail.send", json!({"to": "a@b.c"})) |
| 213 | .await |
| 214 | .unwrap(); |
| 215 | let authorized = authorize_for_test( |
| 216 | intent, |
| 217 | WorldId(7), |
| 218 | JNodeId(3), |
| 219 | Digest32::of(b"policy"), |
| 220 | snapshot.catalog_digest, |
| 221 | EffectId::from_label("send-1"), |
| 222 | ); |
| 223 | let receipt = EffectLane::new(h.broker.clone()) |
| 224 | .execute_effect(authorized) |
| 225 | .await |
| 226 | .unwrap(); |
| 227 | assert_eq!(receipt.output, json!({"id": "m1"})); |
| 228 | assert_eq!(h.calls.load(Ordering::SeqCst), 1); |
| 229 | |
| 230 | let after = speculative.discovery_snapshot(&h.provider).await.unwrap(); |
| 231 | assert_eq!(after.session_epoch, epoch); |
| 232 | } |
| 233 | |
| 234 | #[tokio::test] |
| 235 | async fn stale_catalog_digest_does_not_call_the_tool() { |
| 236 | let tools = vec![tool("mail.send", ToolEffects::EXTERNAL_IRREVERSIBLE)]; |
| 237 | let calls = Arc::new(AtomicUsize::new(0)); |
| 238 | let counted = calls.clone(); |
| 239 | let wire = MemoryWire::new(tools.clone(), move |_name, _args| { |
| 240 | counted.fetch_add(1, Ordering::SeqCst); |
| 241 | CallOutcome::Finished(json!({"id": "m1"})) |
| 242 | }); |
| 243 | let broker = McpBroker::new(); |
| 244 | let provider = ProviderId::new("mail"); |
| 245 | broker |
| 246 | .admit(provider.clone(), Arc::new(wire)) |
| 247 | .await |
| 248 | .unwrap(); |
| 249 | let speculative = SpeculativeLane::new(broker.clone()); |
| 250 | let snapshot = speculative.discovery_snapshot(&provider).await.unwrap(); |
| 251 | let intent = speculative |
| 252 | .stage_effect_intent(&provider, "mail.send", json!({})) |
| 253 | .await |
| 254 | .unwrap(); |
| 255 | let authorized = authorize_for_test( |
| 256 | intent, |
| 257 | WorldId(1), |
| 258 | JNodeId(1), |
| 259 | Digest32::of(b"policy"), |
| 260 | snapshot.catalog_digest, |
| 261 | EffectId::from_label("send-stale"), |
| 262 | ); |
| 263 | |
| 264 | let changed = vec![ |
| 265 | tool("mail.send", ToolEffects::EXTERNAL_IRREVERSIBLE), |
| 266 | tool("mail.search", ToolEffects::READ_NETWORK), |
| 267 | ]; |
| 268 | broker.replace_catalog(&provider, changed).await.unwrap(); |
| 269 | |
| 270 | let err = EffectLane::new(broker) |
| 271 | .execute_effect(authorized) |
| 272 | .await |
| 273 | .unwrap_err(); |
| 274 | assert!(matches!(err, Error::StaleCapability)); |
| 275 | assert_eq!(calls.load(Ordering::SeqCst), 0); |
| 276 | } |
| 277 | |
| 278 | #[tokio::test] |
| 279 | async fn a_completed_effect_is_not_sent_twice() { |
| 280 | let h = harness( |
| 281 | vec![tool("mail.send", ToolEffects::EXTERNAL_IRREVERSIBLE)], |
| 282 | CallOutcome::Finished(json!({"id": "m1"})), |
| 283 | ) |
| 284 | .await; |
| 285 | let speculative = SpeculativeLane::new(h.broker.clone()); |
| 286 | let snapshot = speculative.discovery_snapshot(&h.provider).await.unwrap(); |
| 287 | let intent = speculative |
| 288 | .stage_effect_intent(&h.provider, "mail.send", json!({})) |
| 289 | .await |
| 290 | .unwrap(); |
| 291 | let key = EffectId::from_label("send-once"); |
| 292 | let first = authorize_for_test( |
| 293 | intent.clone(), |
| 294 | WorldId(1), |
| 295 | JNodeId(1), |
| 296 | Digest32::of(b"policy"), |
| 297 | snapshot.catalog_digest, |
| 298 | key, |
| 299 | ); |
| 300 | let lane = EffectLane::new(h.broker); |
| 301 | let receipt = lane.execute_effect(first).await.unwrap(); |
| 302 | let second = authorize_for_test( |
| 303 | intent, |
| 304 | WorldId(1), |
| 305 | JNodeId(1), |
| 306 | Digest32::of(b"policy"), |
| 307 | snapshot.catalog_digest, |
| 308 | key, |
| 309 | ); |
| 310 | let replay = lane.execute_effect(second).await.unwrap(); |
| 311 | assert_eq!(receipt, replay); |
| 312 | assert_eq!(h.calls.load(Ordering::SeqCst), 1); |
| 313 | } |
| 314 | |
| 315 | #[tokio::test] |
| 316 | async fn an_uncertain_call_is_reconciled_instead_of_retried() { |
| 317 | let h = harness( |
| 318 | vec![tool("mail.send", ToolEffects::EXTERNAL_IRREVERSIBLE)], |
| 319 | CallOutcome::Uncertain, |
| 320 | ) |
| 321 | .await; |
| 322 | let speculative = SpeculativeLane::new(h.broker.clone()); |
| 323 | let snapshot = speculative.discovery_snapshot(&h.provider).await.unwrap(); |
| 324 | let intent = speculative |
| 325 | .stage_effect_intent(&h.provider, "mail.send", json!({})) |
| 326 | .await |
| 327 | .unwrap(); |
| 328 | let key = EffectId::from_label("send-uncertain"); |
| 329 | let lane = EffectLane::new(h.broker); |
| 330 | let first = lane |
| 331 | .execute_effect(authorize_for_test( |
| 332 | intent.clone(), |
| 333 | WorldId(1), |
| 334 | JNodeId(1), |
| 335 | Digest32::of(b"policy"), |
| 336 | snapshot.catalog_digest, |
| 337 | key, |
| 338 | )) |
| 339 | .await |
| 340 | .unwrap_err(); |
| 341 | assert!(matches!(first, Error::ReconciliationRequired)); |
| 342 | let second = lane |
| 343 | .execute_effect(authorize_for_test( |
| 344 | intent, |
| 345 | WorldId(1), |
| 346 | JNodeId(1), |
| 347 | Digest32::of(b"policy"), |
| 348 | snapshot.catalog_digest, |
| 349 | key, |
| 350 | )) |
| 351 | .await |
| 352 | .unwrap_err(); |
| 353 | assert!(matches!(second, Error::ReconciliationRequired)); |
| 354 | assert_eq!(h.calls.load(Ordering::SeqCst), 1); |
| 355 | } |