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 / src / authority.rs

246 lines · 9676 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//! Host-owned authority seam: the only production path to an [`AuthorizedEffect`].
17//!
18//! An [`EffectIntent`] carries no authority. It becomes an `AuthorizedEffect` only when
19//! [`crate::EffectLane::authorize`] (or `authorize_and_execute`) asks an [`EffectAuthority`] and
20//! the answer is `Allow` (or `AllowRestricted` with an empty list; a non-empty restriction list
21//! is denied because the lane cannot enforce it). The mint is crate-private, so a caller cannot
22//! build the value, and the lane, not the caller, supplies the live tool descriptor the
23//! authority decides from.
24//!
25//! [`AuthorityDecision`] mirrors aegis-runtime `DoctrineDecision` one-to-one
26//! (`Allow, AllowRestricted, RequireApproval, Deny, Contain`). It is not a new policy language.
27//! aien-mcp cannot depend on aegis-runtime, so the shape is repeated here. Consolidating
28//! `SafetyEngine`, `ProbePolicyGuard` and `DoctrineDecision` is a separate AIEN milestone.
29//!
30//! [`EffectClassAuthority`] is the one production implementation. It decides only from the
31//! `aien_capability::ToolEffects` bits the catalog already declares (ADR 0005, ADR 0007:
32//! irreversible effects are intents only).
33
34use aien_capability::{Digest32, EffectId, JNodeId, ToolDescriptor, ToolEffects, WorldId};
35
36use crate::approval::ApprovalError;
37use crate::{EffectIntent, Error};
38
39/// Verdict of an [`EffectAuthority`]. Same five variants as aegis-runtime `DoctrineDecision`.
40#[derive(Clone, Debug, PartialEq, Eq)]
41pub enum AuthorityDecision {
42 Allow,
43 /// Allowed under the listed restrictions. The lane cannot enforce restrictions, so it denies a
44 /// non-empty list (fail closed) and treats an empty list as `Allow`.
45 AllowRestricted(Vec<String>),
46 /// A human or approval authority must decide. Nothing runs and nothing is consumed until an approval is spent.
47 RequireApproval(String),
48 Deny(String),
49 Contain(String),
50}
51
52/// World facts the committer supplies. Identifiers only: they carry no authority.
53#[derive(Clone, Copy, Debug, PartialEq, Eq)]
54pub struct EffectScope {
55 pub world_id: WorldId,
56 pub winning_jnode: JNodeId,
57 pub idempotency_key: EffectId,
58}
59
60/// How far the runtime trusts a piece of context. Mirrors INTERPLANE Crossveil `TrustLevel`.
61/// The derived order is least to most trusted:
62/// `ExternalUntrusted < WorkspaceUntrusted < UserSupplied < TrustedRuntime`.
63#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
64pub enum TrustLevel {
65 ExternalUntrusted,
66 WorkspaceUntrusted,
67 UserSupplied,
68 TrustedRuntime,
69}
70
71impl TrustLevel {
72 /// Parse the wire name. `unknown` and any unrecognized value count as `ExternalUntrusted`:
73 /// inability to determine trust is never converted into trust.
74 pub fn from_wire(s: &str) -> Self {
75 match s {
76 "trusted_runtime" => Self::TrustedRuntime,
77 "user_supplied" => Self::UserSupplied,
78 "workspace_untrusted" => Self::WorkspaceUntrusted,
79 _ => Self::ExternalUntrusted,
80 }
81 }
82}
83
84/// What the model could see when it produced the turn: `{inputs: [input_id...], floor}`, where
85/// `floor` is the least trusted level among `inputs`. Host/runtime input only: the lane takes it
86/// from [`crate::EffectLane::with_exposure`], never from an intent, so nothing the model writes can set it.
87#[derive(Clone, Debug, PartialEq, Eq)]
88pub struct Exposure {
89 pub inputs: Vec<String>,
90 pub floor: TrustLevel,
91}
92
93impl Exposure {
94 pub fn new(inputs: Vec<String>, floor: TrustLevel) -> Self {
95 Self { inputs, floor }
96 }
97}
98
99/// What an authority sees. Built by the lane from the live session, never by the caller.
100#[derive(Clone, Debug)]
101pub struct AuthorityContext {
102 scope: EffectScope,
103 descriptor: Option<ToolDescriptor>,
104 live_catalog_digest: Digest32,
105 exposure: Option<Exposure>,
106}
107
108impl AuthorityContext {
109 pub(crate) fn new(
110 scope: EffectScope,
111 descriptor: Option<ToolDescriptor>,
112 live_catalog_digest: Digest32,
113 exposure: Option<Exposure>,
114 ) -> Self {
115 Self {
116 scope,
117 descriptor,
118 live_catalog_digest,
119 exposure,
120 }
121 }
122
123 pub fn scope(&self) -> EffectScope {
124 self.scope
125 }
126
127 /// The descriptor in the live catalog for the intent's tool, or `None` if unknown.
128 pub fn descriptor(&self) -> Option<&ToolDescriptor> {
129 self.descriptor.as_ref()
130 }
131
132 pub fn live_catalog_digest(&self) -> Digest32 {
133 self.live_catalog_digest
134 }
135
136 /// Exposure the host attached to the lane, or `None` if it attached none.
137 /// `None` means unknown and is never read as trusted.
138 pub fn exposure(&self) -> Option<&Exposure> {
139 self.exposure.as_ref()
140 }
141}
142
143/// Decides whether an intent may become an `AuthorizedEffect`.
144pub trait EffectAuthority {
145 fn authorize(&self, intent: &EffectIntent, ctx: &AuthorityContext) -> AuthorityDecision;
146}
147
148/// Why no `AuthorizedEffect` was produced (or why execution failed afterwards).
149#[derive(Debug, PartialEq, Eq)]
150pub enum AuthorityOutcome {
151 /// Approval is required. Nothing executed. `intent_digest` names the intent to approve.
152 /// Issue one with [`crate::ApprovalDesk`] and spend it with [`crate::EffectLane::authorize_approved`].
153 Pending {
154 intent_digest: Digest32,
155 reason: String,
156 },
157 /// Approval was presented but could not be spent. See [`ApprovalError`].
158 Approval(ApprovalError),
159 Denied(String),
160 Contained(String),
161 /// Authorization succeeded but the effect lane failed (stale catalog, provider error, ...).
162 Execution(Error),
163}
164
165/// Digest that names one intent: tool, arguments and the catalog it was staged against.
166pub fn intent_digest(intent: &EffectIntent) -> Digest32 {
167 let mut bytes = Vec::new();
168 bytes.extend_from_slice(intent.provider.as_str().as_bytes());
169 bytes.push(0);
170 bytes.extend_from_slice(intent.tool_name.as_bytes());
171 bytes.push(0);
172 bytes.extend_from_slice(intent.arguments.to_string().as_bytes());
173 bytes.push(0);
174 bytes.extend_from_slice(&intent.capability_digest.0);
175 Digest32::of(&bytes)
176}
177
178/// AIEN's production authority. Decides from the declared `ToolEffects` of the live descriptor.
179///
180/// | effects | decision |
181/// |---|---|
182/// | `EXTERNAL_IRREVERSIBLE` | `Deny` (irreversible effects stay intents, ADR 0005/0007) |
183/// | `EXTERNAL_WRITE`, `WORLD_MUTATION`, `SECRET_BEARING`, `SPAWN_PROCESS` | `RequireApproval` |
184/// | `PURE`, `READ_FILESYSTEM`, `READ_NETWORK`, `LOCAL_EPHEMERAL` | `Allow` |
185/// | effect-class bits (the four `RequireApproval` rows) while exposure floor < `UserSupplied`, or exposure absent | `RequireApproval` (never `Allow`; reason names exposure) |
186/// | unknown tool, unknown effect bits | `Deny` |
187#[derive(Clone, Copy, Debug, Default)]
188pub struct EffectClassAuthority;
189
190const KNOWN_BITS: u32 = 0xFF;
191
192impl EffectAuthority for EffectClassAuthority {
193 fn authorize(&self, intent: &EffectIntent, ctx: &AuthorityContext) -> AuthorityDecision {
194 let Some(desc) = ctx.descriptor() else {
195 return AuthorityDecision::Deny(format!(
196 "tool `{}` is not in the live catalog",
197 intent.tool_name
198 ));
199 };
200 let e = desc.effects();
201 if e.bits() & !KNOWN_BITS != 0 {
202 return AuthorityDecision::Deny("tool declares unknown effect bits".into());
203 }
204 if e.intersects(ToolEffects::EXTERNAL_IRREVERSIBLE) {
205 return AuthorityDecision::Deny(
206 "irreversible external effects are intents only".into(),
207 );
208 }
209 let effectful = e.intersects(
210 ToolEffects::EXTERNAL_WRITE
211 | ToolEffects::WORLD_MUTATION
212 | ToolEffects::SECRET_BEARING
213 | ToolEffects::SPAWN_PROCESS,
214 );
215 if effectful {
216 // Absent exposure is treated as external_untrusted: fail closed.
217 let floor = ctx
218 .exposure()
219 .map_or(TrustLevel::ExternalUntrusted, |x| x.floor);
220 if floor < TrustLevel::UserSupplied {
221 return AuthorityDecision::RequireApproval(format!(
222 "tool `{}` is an effect under untrusted exposure ({})",
223 intent.tool_name,
224 if ctx.exposure().is_some() {
225 "floor below user_supplied"
226 } else {
227 "exposure absent"
228 }
229 ));
230 }
231 }
232 if e.intersects(
233 ToolEffects::EXTERNAL_WRITE
234 | ToolEffects::WORLD_MUTATION
235 | ToolEffects::SECRET_BEARING
236 | ToolEffects::SPAWN_PROCESS,
237 ) {
238 return AuthorityDecision::RequireApproval(format!(
239 "tool `{}` needs approval (effect bits {:#x})",
240 intent.tool_name,
241 e.bits()
242 ));
243 }
244 AuthorityDecision::Allow
245 }
246}