interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-mcp / tests / approval.rs

326 lines · 10821 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//! Single-use approval consumption, through the public API only.
17use std::sync::atomic::{AtomicUsize, Ordering};
18use std::sync::Arc;
19
20use aien_capability::{
21 Digest32, EffectId, JNodeId, ProviderId, ToolDescriptor, ToolEffects, WorldId,
22};
23use aien_mcp::memory::MemoryWire;
24use aien_mcp::{
25 ApprovalDesk, ApprovalError, AuthorityContext, AuthorityDecision, AuthorityOutcome,
26 CallOutcome, EffectAuthority, EffectClassAuthority, EffectIntent, EffectLane, EffectScope,
27 Exposure, McpBroker, SpeculativeLane, TrustLevel,
28};
29use serde_json::json;
30
31struct Fixed(AuthorityDecision);
32impl EffectAuthority for Fixed {
33 fn authorize(&self, _: &EffectIntent, _: &AuthorityContext) -> AuthorityDecision {
34 self.0.clone()
35 }
36}
37
38struct H {
39 broker: McpBroker,
40 lane: EffectLane,
41 desk: ApprovalDesk,
42 calls: Arc<AtomicUsize>,
43 intent: EffectIntent,
44}
45
46fn scope(label: &str) -> EffectScope {
47 EffectScope {
48 world_id: WorldId(1),
49 winning_jnode: JNodeId(1),
50 idempotency_key: EffectId::from_label(label),
51 }
52}
53
54async fn harness() -> H {
55 let calls = Arc::new(AtomicUsize::new(0));
56 let c = calls.clone();
57 let tool = ToolDescriptor::new("t", ToolEffects::WORLD_MUTATION, Digest32::of(b"t"));
58 let wire = MemoryWire::new(vec![tool], move |_, _| {
59 c.fetch_add(1, Ordering::SeqCst);
60 CallOutcome::Finished(json!({"done": true}))
61 });
62 let broker = McpBroker::new();
63 let provider = ProviderId::new("p");
64 broker
65 .admit(provider.clone(), Arc::new(wire))
66 .await
67 .unwrap();
68 let live = 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.clone()).with_clock(std::sync::Arc::new(|| 1)),
81 desk: ApprovalDesk::new(broker.clone()),
82 broker,
83 calls,
84 intent,
85 }
86}
87
88fn approval(e: AuthorityOutcome) -> ApprovalError {
89 match e {
90 AuthorityOutcome::Approval(a) => a,
91 other => panic!("expected Approval, got {other:?}"),
92 }
93}
94
95#[tokio::test]
96async fn first_use_mints_and_runs_once() {
97 let h = harness().await;
98 let g = h.desk.issue(&h.intent, scope("a"), 100);
99 let r = h
100 .lane
101 .authorize_and_execute_approved(h.intent.clone(), scope("a"), &EffectClassAuthority, &g, 1)
102 .await
103 .unwrap();
104 assert_eq!(r.output, json!({"done": true}));
105 assert_eq!(h.calls.load(Ordering::SeqCst), 1);
106}
107
108#[tokio::test]
109async fn second_mint_with_the_same_grant_is_refused() {
110 let h = harness().await;
111 let g = h.desk.issue(&h.intent, scope("b"), 100);
112 let a = EffectClassAuthority;
113 // Hold the minted effect: a dropped one would release the grant (see tests/two_phase.rs).
114 let held = h
115 .lane
116 .authorize_approved(h.intent.clone(), scope("b"), &a, &g, 1)
117 .unwrap();
118 let err = h
119 .lane
120 .authorize_approved(h.intent.clone(), scope("b"), &a, &g, 1)
121 .unwrap_err();
122 assert_eq!(approval(err), ApprovalError::Reserved);
123 // A cloned grant is the same grant.
124 let err = h
125 .lane
126 .authorize_approved(h.intent.clone(), scope("b"), &a, &g.clone(), 1)
127 .unwrap_err();
128 assert_eq!(approval(err), ApprovalError::Reserved);
129 // Once the held effect runs, the grant is spent for good.
130 h.lane.execute_effect(held).await.unwrap();
131 let err = h
132 .lane
133 .authorize_approved(h.intent.clone(), scope("b"), &a, &g, 1)
134 .unwrap_err();
135 assert_eq!(approval(err), ApprovalError::Consumed);
136}
137
138#[tokio::test]
139async fn replay_after_completion_returns_the_existing_receipt() {
140 let h = harness().await;
141 let g = h.desk.issue(&h.intent, scope("r"), 100);
142 let a = EffectClassAuthority;
143 let first = h
144 .lane
145 .authorize_and_execute_approved(h.intent.clone(), scope("r"), &a, &g, 1)
146 .await
147 .unwrap();
148 // Even after the grant has expired, the same logical request gets the same outcome.
149 let again = h
150 .lane
151 .authorize_and_execute_approved(h.intent.clone(), scope("r"), &a, &g, 10_000)
152 .await
153 .unwrap();
154 assert_eq!(first, again);
155 assert_eq!(h.calls.load(Ordering::SeqCst), 1);
156}
157
158#[tokio::test]
159async fn spent_grant_cannot_run_a_different_request_under_the_same_key() {
160 let h = harness().await;
161 let g = h.desk.issue(&h.intent, scope("k"), 100);
162 let a = EffectClassAuthority;
163 h.lane
164 .authorize_and_execute_approved(h.intent.clone(), scope("k"), &a, &g, 1)
165 .await
166 .unwrap();
167 let mut other = h.intent.clone();
168 other.arguments = json!({"a": 2});
169 let err = h
170 .lane
171 .authorize_and_execute_approved(other, scope("k"), &a, &g, 1)
172 .await
173 .unwrap_err();
174 assert_eq!(approval(err), ApprovalError::Mismatch);
175 assert_eq!(h.calls.load(Ordering::SeqCst), 1);
176}
177
178#[tokio::test]
179async fn grant_for_a_different_effect_is_refused_and_not_spent() {
180 let h = harness().await;
181 let g = h.desk.issue(&h.intent, scope("x"), 100);
182 let a = EffectClassAuthority;
183 let mut other = h.intent.clone();
184 other.arguments = json!({"a": 2});
185 let e = h
186 .lane
187 .authorize_approved(other, scope("x"), &a, &g, 1)
188 .unwrap_err();
189 assert_eq!(approval(e), ApprovalError::Mismatch);
190 let e = h
191 .lane
192 .authorize_approved(h.intent.clone(), scope("y"), &a, &g, 1)
193 .unwrap_err();
194 assert_eq!(approval(e), ApprovalError::Mismatch);
195 let mut w = scope("x");
196 w.world_id = WorldId(2);
197 let e = h
198 .lane
199 .authorize_approved(h.intent.clone(), w, &a, &g, 1)
200 .unwrap_err();
201 assert_eq!(approval(e), ApprovalError::Mismatch);
202 // The mismatches burned nothing: the right request still works.
203 assert!(h
204 .lane
205 .authorize_approved(h.intent.clone(), scope("x"), &a, &g, 1)
206 .is_ok());
207 assert_eq!(h.calls.load(Ordering::SeqCst), 0);
208}
209
210#[tokio::test]
211async fn expired_grant_is_refused_and_not_spent() {
212 let h = harness().await;
213 let g = h.desk.issue(&h.intent, scope("e"), 50);
214 let a = EffectClassAuthority;
215 let e = h
216 .lane
217 .authorize_approved(h.intent.clone(), scope("e"), &a, &g, 50)
218 .unwrap_err();
219 assert_eq!(approval(e), ApprovalError::Expired);
220 assert!(h
221 .lane
222 .authorize_approved(h.intent.clone(), scope("e"), &a, &g, 49)
223 .is_ok());
224}
225
226#[tokio::test]
227async fn catalog_change_makes_the_grant_stale() {
228 let h = harness().await;
229 let g = h.desk.issue(&h.intent, scope("c"), 100);
230 let provider = h.intent.provider.clone();
231 let changed = vec![
232 ToolDescriptor::new("t", ToolEffects::WORLD_MUTATION, Digest32::of(b"t")),
233 ToolDescriptor::new("u", ToolEffects::PURE, Digest32::of(b"u")),
234 ];
235 h.broker.replace_catalog(&provider, changed).await.unwrap();
236 let e = h
237 .lane
238 .authorize_approved(h.intent.clone(), scope("c"), &EffectClassAuthority, &g, 1)
239 .unwrap_err();
240 assert!(matches!(e, AuthorityOutcome::Denied(_)));
241 assert_eq!(h.calls.load(Ordering::SeqCst), 0);
242}
243
244#[tokio::test]
245async fn deny_and_contain_stay_refused_and_keep_the_grant() {
246 let h = harness().await;
247 let g = h.desk.issue(&h.intent, scope("d"), 100);
248 let deny = Fixed(AuthorityDecision::Deny("no".into()));
249 let e = h
250 .lane
251 .authorize_and_execute_approved(h.intent.clone(), scope("d"), &deny, &g, 1)
252 .await
253 .unwrap_err();
254 assert_eq!(e, AuthorityOutcome::Denied("no".into()));
255 let contain = Fixed(AuthorityDecision::Contain("box".into()));
256 let e = h
257 .lane
258 .authorize_approved(h.intent.clone(), scope("d"), &contain, &g, 1)
259 .unwrap_err();
260 assert_eq!(e, AuthorityOutcome::Contained("box".into()));
261 assert_eq!(h.calls.load(Ordering::SeqCst), 0);
262 // Grant untouched by the refusals.
263 assert!(h
264 .lane
265 .authorize_approved(h.intent.clone(), scope("d"), &EffectClassAuthority, &g, 1)
266 .is_ok());
267}
268
269#[tokio::test]
270async fn allow_does_not_spend_the_grant() {
271 let h = harness().await;
272 let g = h.desk.issue(&h.intent, scope("l"), 100);
273 let allow = Fixed(AuthorityDecision::Allow);
274 assert!(h
275 .lane
276 .authorize_approved(h.intent.clone(), scope("l"), &allow, &g, 1)
277 .is_ok());
278 assert!(h
279 .lane
280 .authorize_approved(h.intent.clone(), scope("l"), &EffectClassAuthority, &g, 1)
281 .is_ok());
282}
283
284#[tokio::test]
285async fn grant_from_another_broker_is_unknown() {
286 let h = harness().await;
287 let other = harness().await;
288 let g = other.desk.issue(&other.intent, scope("z"), 100);
289 let e = h
290 .lane
291 .authorize_approved(h.intent.clone(), scope("z"), &EffectClassAuthority, &g, 1)
292 .unwrap_err();
293 assert_eq!(approval(e), ApprovalError::Unknown);
294}
295
296#[tokio::test]
297async fn unapproved_request_is_still_pending() {
298 let h = harness().await;
299 let e = h
300 .lane
301 .authorize(h.intent.clone(), scope("n"), &EffectClassAuthority)
302 .unwrap_err();
303 assert!(matches!(e, AuthorityOutcome::Pending { .. }));
304}
305
306#[tokio::test]
307async fn approved_grant_authorizes_effect_pending_on_untrusted_exposure() {
308 // E3: an effect held back by untrusted exposure is still released by an approval grant.
309 let h = harness().await;
310 let lane = h.lane.with_exposure(Exposure::new(
311 vec!["web-1".into()],
312 TrustLevel::ExternalUntrusted,
313 ));
314 let a = EffectClassAuthority;
315 assert!(matches!(
316 lane.authorize(h.intent.clone(), scope("ex"), &a),
317 Err(AuthorityOutcome::Pending { .. })
318 ));
319 let g = h.desk.issue(&h.intent, scope("ex"), 100);
320 let r = lane
321 .authorize_and_execute_approved(h.intent.clone(), scope("ex"), &a, &g, 1)
322 .await
323 .unwrap();
324 assert_eq!(r.output, json!({"done": true}));
325 assert_eq!(h.calls.load(Ordering::SeqCst), 1);
326}