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/aegis-runtime-mv/blob/main/src/orchestration/grant_gate.rs # aegis-runtime-mv Native Rust agent runtime: Axum WebSocket/SSE/REST gateway, heartbeat scheduler, dependency-aware task workflow, SQLite storage. Attachment of AienOS. Pre-alpha. 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 `aegis-runtime-mv / src / orchestration / grant_gate.rs` 208 lines ยท 7049 bytes Raw Blame History 1 //! Grant gate for effectful workflow tasks (issue #59, lane G6). 2 //! 3 //! A task that changes the world starts only under a grant from the Spark 4 //! approval desk that is bound to this request's objective id and cause id. 5 //! The grant is checked when the task starts and again immediately before 6 //! the effect, after any preparation. A missing grant asks for approval; a 7 //! stale, foreign, forged or unbound one is refused with a named reason and 8 //! no effect. 9 //! 10 //! The desk is a trait. The authority is the durable, objective-bound grant 11 //! in the sovereign-core compose ledger (docs/DELEGATED-DAG.md, proposed 12 //! decision D3); no client to it exists yet (sovereign-core#379), so tests 13 //! use a fake. The gate checks; it does not spend. Spending the grant 14 //! belongs to the effect ledger when it opens the effect intent, so there 15 //! is one authority for that fact. 16 //! 17 //! Not here yet: binding the grant to the INTERPLANE canonical request 18 //! digest (item 3, after aien-protocols-mv#33) and the real desk adapter 19 //! (item 4). 20 21 use crate::failure::{EffectCertainty, FailureClass, ToolFault}; 22 use crate::orchestration::recovery::{GrantId, GrantStatus, LookupError}; 23 use crate::orchestration::workflow::{TaskContext, TaskOutcome, TaskRunner}; 24 use async_trait::async_trait; 25 use std::fmt; 26 use std::sync::Arc; 27 28 /// What the desk holds for one grant id. 29 #[derive(Debug, Clone, PartialEq, Eq)] 30 pub struct DeskGrant { 31 pub status: GrantStatus, 32 pub objective_id: String, 33 pub cause_id: String, 34 } 35 36 /// Read access to the approval desk. 37 #[async_trait] 38 pub trait GrantDesk: Send + Sync { 39 /// `Ok(None)` means the desk never issued this id. 40 async fn grant(&self, id: &GrantId) -> Result, LookupError>; 41 } 42 43 /// The request this task belongs to, and the grant id it was handed. 44 #[derive(Debug, Clone, PartialEq, Eq)] 45 pub struct GrantBinding { 46 pub objective_id: String, 47 /// Copied from the Pi; never minted here. 48 pub cause_id: String, 49 pub grant: Option, 50 } 51 52 /// Why the gate stopped the task. 53 #[derive(Debug, Clone, PartialEq, Eq)] 54 pub enum GrantRefusal { 55 /// The binding has no objective id or no cause id, so nothing can match. 56 Unbound, 57 /// No grant id was handed over: the task waits for approval. 58 NoGrant, 59 /// The desk never issued this id. 60 Forged { 61 grant: GrantId, 62 }, 63 /// Spent, expired or revoked. 64 Stale { 65 grant: GrantId, 66 status: GrantStatus, 67 }, 68 ForeignObjective { 69 grant: GrantId, 70 }, 71 ForeignCause { 72 grant: GrantId, 73 }, 74 DeskUnavailable { 75 detail: String, 76 }, 77 } 78 79 impl GrantRefusal { 80 pub fn name(&self) -> &'static str { 81 match self { 82 Self::Unbound => "GRANT_GATE_UNBOUND", 83 Self::NoGrant => "GRANT_GATE_NO_GRANT", 84 Self::Forged { .. } => "GRANT_GATE_FORGED", 85 Self::Stale { .. } => "GRANT_GATE_STALE", 86 Self::ForeignObjective { .. } => "GRANT_GATE_FOREIGN_OBJECTIVE", 87 Self::ForeignCause { .. } => "GRANT_GATE_FOREIGN_CAUSE", 88 Self::DeskUnavailable { .. } => "GRANT_GATE_DESK_UNAVAILABLE", 89 } 90 } 91 92 /// What the workflow records. A missing grant is a wait for a human, 93 /// not a failure; every other refusal is a no-effect failure. 94 pub fn outcome(&self) -> TaskOutcome { 95 let class = match self { 96 Self::NoGrant => return TaskOutcome::ApprovalPending, 97 Self::DeskUnavailable { .. } => FailureClass::Unavailable, 98 _ => FailureClass::Rejected, 99 }; 100 TaskOutcome::Failed(ToolFault::new( 101 class, 102 EffectCertainty::NoEffect, 103 self.to_string(), 104 )) 105 } 106 } 107 108 impl fmt::Display for GrantRefusal { 109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 110 match self { 111 Self::Unbound => write!(f, "{}: binding has no objective or cause", self.name()), 112 Self::NoGrant => write!(f, "{}: no grant id", self.name()), 113 Self::Forged { grant } 114 | Self::ForeignObjective { grant } 115 | Self::ForeignCause { grant } => write!(f, "{}: {grant}", self.name()), 116 Self::Stale { grant, status } => write!(f, "{}: {grant} is {status:?}", self.name()), 117 Self::DeskUnavailable { detail } => write!(f, "{}: {detail}", self.name()), 118 } 119 } 120 } 121 122 impl std::error::Error for GrantRefusal {} 123 124 /// One check against the desk. Returns the grant id the effect may run 125 /// under. 126 pub async fn check(desk: &dyn GrantDesk, b: &GrantBinding) -> Result { 127 if b.objective_id.is_empty() || b.cause_id.is_empty() { 128 return Err(GrantRefusal::Unbound); 129 } 130 let id = b.grant.clone().ok_or(GrantRefusal::NoGrant)?; 131 let g = desk 132 .grant(&id) 133 .await 134 .map_err(|e| GrantRefusal::DeskUnavailable { detail: e.detail })? 135 .ok_or_else(|| GrantRefusal::Forged { grant: id.clone() })?; 136 if g.objective_id != b.objective_id { 137 return Err(GrantRefusal::ForeignObjective { grant: id }); 138 } 139 if g.cause_id != b.cause_id { 140 return Err(GrantRefusal::ForeignCause { grant: id }); 141 } 142 if g.status != GrantStatus::Available { 143 return Err(GrantRefusal::Stale { 144 grant: id, 145 status: g.status, 146 }); 147 } 148 Ok(id) 149 } 150 151 /// An effectful task body, split so the gate can check between the parts. 152 #[async_trait] 153 pub trait EffectBody: Send + Sync { 154 /// Work that changes nothing outside (building the request, reading). 155 /// An `Err` ends the task with that outcome and no effect. 156 async fn prepare(&self, _ctx: &TaskContext) -> Result<(), TaskOutcome> { 157 Ok(()) 158 } 159 160 /// The effect, under the grant the gate just checked. 161 async fn apply(&self, ctx: TaskContext, grant: GrantId) -> TaskOutcome; 162 163 /// Passed through to the workflow on timeout or cancel. 164 fn certainty_on_interrupt(&self) -> EffectCertainty { 165 EffectCertainty::Uncertain 166 } 167 } 168 169 /// Wraps an effectful body: check, prepare, check again, apply. 170 pub struct GrantGate { 171 desk: Arc, 172 binding: GrantBinding, 173 body: Arc, 174 } 175 176 impl GrantGate { 177 pub fn new( 178 desk: Arc, 179 binding: GrantBinding, 180 body: Arc, 181 ) -> Arc { 182 Arc::new(Self { 183 desk, 184 binding, 185 body, 186 }) 187 } 188 } 189 190 #[async_trait] 191 impl TaskRunner for GrantGate { 192 async fn run(&self, ctx: TaskContext) -> TaskOutcome { 193 if let Err(r) = check(self.desk.as_ref(), &self.binding).await { 194 return r.outcome(); 195 } 196 if let Err(out) = self.body.prepare(&ctx).await { 197 return out; 198 } 199 match check(self.desk.as_ref(), &self.binding).await { 200 Ok(grant) => self.body.apply(ctx, grant).await, 201 Err(r) => r.outcome(), 202 } 203 } 204 205 fn certainty_on_interrupt(&self) -> EffectCertainty { 206 self.body.certainty_on_interrupt() 207 } 208 }