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 / effect.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::Arc; |
| 17 | |
| 18 | use crate::approval::Reservation; |
| 19 | use aien_capability::{Digest32, EffectId, JNodeId, ProviderId, WorldId}; |
| 20 | use serde_json::Value; |
| 21 | |
| 22 | /// Irreversible work J-Space may record. Staging does not call the provider. |
| 23 | #[derive(Clone, Debug, PartialEq)] |
| 24 | pub struct EffectIntent { |
| 25 | pub provider: ProviderId, |
| 26 | pub tool_name: String, |
| 27 | pub arguments: Value, |
| 28 | pub capability_digest: Digest32, |
| 29 | } |
| 30 | |
| 31 | /// Result of one speculative-safe `tools/call`. |
| 32 | #[derive(Clone, Debug, PartialEq)] |
| 33 | pub struct ToolResult { |
| 34 | pub output: Value, |
| 35 | } |
| 36 | |
| 37 | /// Proof that a committed effect finished. The output is the provider result, |
| 38 | /// not a credential. |
| 39 | #[derive(Clone, Debug, PartialEq)] |
| 40 | pub struct EffectReceipt { |
| 41 | pub effect_id: EffectId, |
| 42 | pub world_id: WorldId, |
| 43 | pub winning_jnode: JNodeId, |
| 44 | pub policy_digest: Digest32, |
| 45 | pub provider: ProviderId, |
| 46 | pub tool_name: String, |
| 47 | pub capability_digest: Digest32, |
| 48 | pub output: Value, |
| 49 | } |
| 50 | |
| 51 | /// An effect the policy boundary has allowed. |
| 52 | /// |
| 53 | /// The only production mint is crate-private and reachable solely through |
| 54 | /// [`crate::EffectLane::authorize`], which asks an [`crate::EffectAuthority`]. Fields stay |
| 55 | /// private, so another crate cannot build the value with a struct literal: |
| 56 | /// |
| 57 | /// ```compile_fail |
| 58 | /// use aien_capability::{Digest32, EffectId, JNodeId, WorldId}; |
| 59 | /// use aien_mcp::AuthorizedEffect; |
| 60 | /// let _ = AuthorizedEffect { |
| 61 | /// intent: (), |
| 62 | /// world_id: WorldId(1), |
| 63 | /// winning_jnode: JNodeId(1), |
| 64 | /// policy_digest: Digest32::of(b"x"), |
| 65 | /// capability_digest: Digest32::of(b"x"), |
| 66 | /// idempotency_key: EffectId::from_label("x"), |
| 67 | /// }; |
| 68 | /// ``` |
| 69 | /// |
| 70 | /// A bare intent cannot be executed: |
| 71 | /// |
| 72 | /// ```compile_fail |
| 73 | /// use aien_mcp::{EffectIntent, EffectLane, McpBroker}; |
| 74 | /// async fn f(lane: EffectLane, intent: EffectIntent) { |
| 75 | /// let _ = lane.execute_effect(intent).await; |
| 76 | /// } |
| 77 | /// ``` |
| 78 | #[derive(Clone, Debug)] |
| 79 | pub struct AuthorizedEffect<T> { |
| 80 | intent: T, |
| 81 | world_id: WorldId, |
| 82 | winning_jnode: JNodeId, |
| 83 | policy_digest: Digest32, |
| 84 | capability_digest: Digest32, |
| 85 | idempotency_key: EffectId, |
| 86 | reservation: Option<Arc<Reservation>>, |
| 87 | } |
| 88 | |
| 89 | impl<T> AuthorizedEffect<T> { |
| 90 | /// Id of the approval grant this effect holds, if it was minted by `authorize_approved`. |
| 91 | pub fn approval_id(&self) -> Option<Digest32> { |
| 92 | self.reservation.as_ref().map(|r| r.grant_id()) |
| 93 | } |
| 94 | |
| 95 | /// Give up this effect without running it. If it holds an approval grant, the grant is |
| 96 | /// released (usable again once re-checked at the next mint) and `reason` is recorded |
| 97 | /// ([`crate::ApprovalDesk::release_reason`]). Dropping the effect has the same release with a |
| 98 | /// fixed reason. Clones share the hold: after a cancel, a clone cannot be executed with the |
| 99 | /// grant (`Error::ApprovalNotReserved`). |
| 100 | pub fn cancel(self, reason: &str) { |
| 101 | if let Some(r) = &self.reservation { |
| 102 | r.release(reason); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | pub(crate) fn reservation(&self) -> Option<&Reservation> { |
| 107 | self.reservation.as_deref() |
| 108 | } |
| 109 | |
| 110 | pub fn intent(&self) -> &T { |
| 111 | &self.intent |
| 112 | } |
| 113 | |
| 114 | pub fn world_id(&self) -> WorldId { |
| 115 | self.world_id |
| 116 | } |
| 117 | |
| 118 | pub fn winning_jnode(&self) -> JNodeId { |
| 119 | self.winning_jnode |
| 120 | } |
| 121 | |
| 122 | pub fn policy_digest(&self) -> Digest32 { |
| 123 | self.policy_digest |
| 124 | } |
| 125 | |
| 126 | pub fn capability_digest(&self) -> Digest32 { |
| 127 | self.capability_digest |
| 128 | } |
| 129 | |
| 130 | pub fn idempotency_key(&self) -> EffectId { |
| 131 | self.idempotency_key |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// Test-only mint. Absent from production builds. |
| 136 | #[cfg(test)] |
| 137 | pub(crate) fn authorize_for_test<T>( |
| 138 | intent: T, |
| 139 | world_id: WorldId, |
| 140 | winning_jnode: JNodeId, |
| 141 | policy_digest: Digest32, |
| 142 | capability_digest: Digest32, |
| 143 | idempotency_key: EffectId, |
| 144 | ) -> AuthorizedEffect<T> { |
| 145 | AuthorizedEffect { |
| 146 | intent, |
| 147 | world_id, |
| 148 | winning_jnode, |
| 149 | policy_digest, |
| 150 | capability_digest, |
| 151 | idempotency_key, |
| 152 | reservation: None, |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /// Production mint. Crate-private: reached only from `EffectLane::authorize` after an |
| 157 | /// `EffectAuthority` returned `Allow` or an empty `AllowRestricted`. |
| 158 | pub(crate) fn mint<T>( |
| 159 | intent: T, |
| 160 | world_id: WorldId, |
| 161 | winning_jnode: JNodeId, |
| 162 | policy_digest: Digest32, |
| 163 | capability_digest: Digest32, |
| 164 | idempotency_key: EffectId, |
| 165 | ) -> AuthorizedEffect<T> { |
| 166 | AuthorizedEffect { |
| 167 | intent, |
| 168 | world_id, |
| 169 | winning_jnode, |
| 170 | policy_digest, |
| 171 | capability_digest, |
| 172 | idempotency_key, |
| 173 | reservation: None, |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /// Production mint for an effect that holds a reserved approval grant. |
| 178 | pub(crate) fn mint_reserved<T>( |
| 179 | intent: T, |
| 180 | world_id: WorldId, |
| 181 | winning_jnode: JNodeId, |
| 182 | policy_digest: Digest32, |
| 183 | capability_digest: Digest32, |
| 184 | idempotency_key: EffectId, |
| 185 | reservation: Reservation, |
| 186 | ) -> AuthorizedEffect<T> { |
| 187 | let mut effect = mint( |
| 188 | intent, |
| 189 | world_id, |
| 190 | winning_jnode, |
| 191 | policy_digest, |
| 192 | capability_digest, |
| 193 | idempotency_key, |
| 194 | ); |
| 195 | effect.reservation = Some(Arc::new(reservation)); |
| 196 | effect |
| 197 | } |