interplane-mv
INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.
Brought by drakestapleton
interplane-mv / rust / crates / interplane-core / src / lifecycle.rs
| 1 | //! The Crossveil lifecycle state machine (CORE.md). Authority only enters through a `&Decision`. |
| 2 | use crate::types::{Decision, DecisionKind}; |
| 3 | use thiserror::Error; |
| 4 | |
| 5 | /// Lifecycle states. |
| 6 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 7 | pub enum State { |
| 8 | Proposed, |
| 9 | Rejected, |
| 10 | Mapped, |
| 11 | Denied, |
| 12 | RequiresApproval, |
| 13 | Authorized, |
| 14 | Executing, |
| 15 | Succeeded, |
| 16 | Failed, |
| 17 | TimedOut, |
| 18 | } |
| 19 | |
| 20 | impl State { |
| 21 | /// Terminal states (REQUIRES_APPROVAL is terminal only for the model, so it is not listed). |
| 22 | pub fn is_terminal(self) -> bool { |
| 23 | matches!( |
| 24 | self, |
| 25 | State::Rejected | State::Denied | State::Succeeded | State::Failed | State::TimedOut |
| 26 | ) |
| 27 | } |
| 28 | /// The CORE.md stage name. |
| 29 | pub fn name(self) -> &'static str { |
| 30 | match self { |
| 31 | State::Proposed => "PROPOSED", |
| 32 | State::Rejected => "REJECTED", |
| 33 | State::Mapped => "MAPPED", |
| 34 | State::Denied => "DENIED", |
| 35 | State::RequiresApproval => "REQUIRES_APPROVAL", |
| 36 | State::Authorized => "AUTHORIZED", |
| 37 | State::Executing => "EXECUTING", |
| 38 | State::Succeeded => "SUCCEEDED", |
| 39 | State::Failed => "FAILED", |
| 40 | State::TimedOut => "TIMED_OUT", |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// A refused transition. The state does not change. |
| 46 | #[derive(Debug, Clone, PartialEq, Eq, Error)] |
| 47 | pub enum LifecycleError { |
| 48 | #[error("illegal transition from {from:?}")] |
| 49 | Illegal { from: State }, |
| 50 | #[error("decision is not authorized")] |
| 51 | NotAuthorized, |
| 52 | #[error("decision is for a different request")] |
| 53 | WrongRequest, |
| 54 | /// The continuation did not cite the approval_id the runtime minted for this request |
| 55 | /// (absent, empty, never minted, or minted for another request). |
| 56 | #[error("approval requires a new decision citing the approval_id")] |
| 57 | MissingApproval, |
| 58 | } |
| 59 | |
| 60 | /// One request's lifecycle. The state field is private and there is no setter, so an |
| 61 | /// `Authorized` or `Executing` state can only come from [`Lifecycle::apply_decision`] / |
| 62 | /// [`Lifecycle::begin_execution`] with an authorized [`Decision`]. |
| 63 | #[derive(Debug, Clone)] |
| 64 | pub struct Lifecycle { |
| 65 | request_id: String, |
| 66 | state: State, |
| 67 | /// The non-empty approval_id the runtime minted in the decision that entered |
| 68 | /// REQUIRES_APPROVAL. A continuation must cite exactly this id. |
| 69 | approval_id: Option<String>, |
| 70 | } |
| 71 | |
| 72 | impl Lifecycle { |
| 73 | /// A new lifecycle in `Proposed`. |
| 74 | pub fn new(request_id: &str) -> Self { |
| 75 | Self { |
| 76 | request_id: request_id.to_string(), |
| 77 | state: State::Proposed, |
| 78 | approval_id: None, |
| 79 | } |
| 80 | } |
| 81 | pub fn state(&self) -> State { |
| 82 | self.state |
| 83 | } |
| 84 | pub fn request_id(&self) -> &str { |
| 85 | &self.request_id |
| 86 | } |
| 87 | |
| 88 | /// PROPOSED -> REJECTED (refused before runtime contact) or MAPPED -> REJECTED is via decision. |
| 89 | pub fn reject(&mut self) -> Result<State, LifecycleError> { |
| 90 | match self.state { |
| 91 | State::Proposed => self.set(State::Rejected), |
| 92 | from => Err(LifecycleError::Illegal { from }), |
| 93 | } |
| 94 | } |
| 95 | /// PROPOSED -> MAPPED. |
| 96 | pub fn map(&mut self) -> Result<State, LifecycleError> { |
| 97 | match self.state { |
| 98 | State::Proposed => self.set(State::Mapped), |
| 99 | from => Err(LifecycleError::Illegal { from }), |
| 100 | } |
| 101 | } |
| 102 | /// Apply a runtime decision. From MAPPED: authorized -> AUTHORIZED, denied (and any unknown |
| 103 | /// value) -> DENIED, requires_approval -> REQUIRES_APPROVAL, invalid/not_found -> REJECTED. |
| 104 | /// From REQUIRES_APPROVAL: only a NEW decision citing the non-empty `approval.approval_id` |
| 105 | /// the runtime minted when it required approval; authorized -> AUTHORIZED, every other |
| 106 | /// value -> DENIED. Any other id (absent, empty, never minted, minted for another request) |
| 107 | /// is refused with [`LifecycleError::MissingApproval`] and the state does not change. |
| 108 | pub fn apply_decision(&mut self, d: &Decision) -> Result<State, LifecycleError> { |
| 109 | if d.request_id != self.request_id { |
| 110 | return Err(LifecycleError::WrongRequest); |
| 111 | } |
| 112 | match self.state { |
| 113 | State::Mapped => { |
| 114 | let next = match &d.decision { |
| 115 | DecisionKind::Authorized => State::Authorized, |
| 116 | DecisionKind::Denied => State::Denied, |
| 117 | DecisionKind::RequiresApproval => { |
| 118 | self.approval_id = minted_id(d); |
| 119 | State::RequiresApproval |
| 120 | } |
| 121 | DecisionKind::NotFound | DecisionKind::Invalid => State::Rejected, |
| 122 | DecisionKind::Unknown(_) => State::Denied, |
| 123 | }; |
| 124 | self.set(next) |
| 125 | } |
| 126 | State::RequiresApproval => { |
| 127 | let cited = minted_id(d); |
| 128 | if cited.is_none() || cited != self.approval_id { |
| 129 | return Err(LifecycleError::MissingApproval); |
| 130 | } |
| 131 | match &d.decision { |
| 132 | DecisionKind::Authorized => self.set(State::Authorized), |
| 133 | _ => self.set(State::Denied), |
| 134 | } |
| 135 | } |
| 136 | from => Err(LifecycleError::Illegal { from }), |
| 137 | } |
| 138 | } |
| 139 | /// REQUIRES_APPROVAL -> DENIED without a decision: a host cancel, or a continuation the |
| 140 | /// pipeline refused to honour (fail closed). It can only deny, so it is the one way out of |
| 141 | /// REQUIRES_APPROVAL that is not a runtime decision and still cannot authorize anything. |
| 142 | pub fn cancel(&mut self) -> Result<State, LifecycleError> { |
| 143 | match self.state { |
| 144 | State::RequiresApproval => self.set(State::Denied), |
| 145 | from => Err(LifecycleError::Illegal { from }), |
| 146 | } |
| 147 | } |
| 148 | /// AUTHORIZED -> EXECUTING; needs the authorized decision itself. |
| 149 | pub fn begin_execution(&mut self, d: &Decision) -> Result<State, LifecycleError> { |
| 150 | if d.request_id != self.request_id { |
| 151 | return Err(LifecycleError::WrongRequest); |
| 152 | } |
| 153 | if !d.is_authorized() { |
| 154 | return Err(LifecycleError::NotAuthorized); |
| 155 | } |
| 156 | match self.state { |
| 157 | State::Authorized => self.set(State::Executing), |
| 158 | from => Err(LifecycleError::Illegal { from }), |
| 159 | } |
| 160 | } |
| 161 | /// EXECUTING -> SUCCEEDED | FAILED | TIMED_OUT. |
| 162 | pub fn finish(&mut self, outcome: State) -> Result<State, LifecycleError> { |
| 163 | match (self.state, outcome) { |
| 164 | (State::Executing, State::Succeeded | State::Failed | State::TimedOut) => { |
| 165 | self.set(outcome) |
| 166 | } |
| 167 | (from, _) => Err(LifecycleError::Illegal { from }), |
| 168 | } |
| 169 | } |
| 170 | fn set(&mut self, s: State) -> Result<State, LifecycleError> { |
| 171 | self.state = s; |
| 172 | Ok(s) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /// The decision's `approval.approval_id`, if present and non-empty. |
| 177 | fn minted_id(d: &Decision) -> Option<String> { |
| 178 | d.approval |
| 179 | .as_ref() |
| 180 | .map(|a| a.approval_id.clone()) |
| 181 | .filter(|id| !id.is_empty()) |
| 182 | } |