aien-protocols-mv
Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem
Brought by drakestapleton
aien-protocols-mv / crates / aien-agent-state-abi / src / lib.rs
| 1 | use aien_action_protocol::{CapabilityGrant, ContainmentState}; |
| 2 | use aien_inference_protocol::InferenceContextRef; |
| 3 | use aien_protocol_types::{ |
| 4 | ActionId, AgentId, ApprovalId, ArtifactRef, Digest32, EventId, ProtocolVersion, RunId, |
| 5 | SequenceNumber, SessionId, Timestamp, |
| 6 | }; |
| 7 | use aien_provenance::compute_canonical_json_digest; |
| 8 | use serde::{Deserialize, Serialize}; |
| 9 | |
| 10 | #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 11 | pub struct AgentIdentity { |
| 12 | pub agent_id: AgentId, |
| 13 | pub name: String, |
| 14 | pub model_profile: String, |
| 15 | pub created_at: Timestamp, |
| 16 | } |
| 17 | |
| 18 | #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 19 | pub struct SessionState { |
| 20 | pub session_id: SessionId, |
| 21 | pub title: String, |
| 22 | pub created_at: Timestamp, |
| 23 | pub closed_at: Option<Timestamp>, |
| 24 | } |
| 25 | |
| 26 | #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 27 | pub struct Objective { |
| 28 | pub objective_id: uuid::Uuid, |
| 29 | pub description: String, |
| 30 | pub success_criteria: Vec<String>, |
| 31 | } |
| 32 | |
| 33 | #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 34 | pub struct MessageRef { |
| 35 | pub message_id: uuid::Uuid, |
| 36 | pub role: String, |
| 37 | pub digest: Digest32, |
| 38 | pub token_count: u32, |
| 39 | } |
| 40 | |
| 41 | #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 42 | pub struct MemoryRef { |
| 43 | pub memory_id: uuid::Uuid, |
| 44 | pub space: String, |
| 45 | pub canonical_name: String, |
| 46 | pub digest: Digest32, |
| 47 | } |
| 48 | |
| 49 | #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 50 | pub struct ContextState { |
| 51 | pub conversation: Vec<MessageRef>, |
| 52 | pub pinned: Vec<ArtifactRef>, |
| 53 | pub summaries: Vec<ArtifactRef>, |
| 54 | pub cortex: Vec<MemoryRef>, |
| 55 | pub inference: Option<InferenceContextRef>, |
| 56 | } |
| 57 | |
| 58 | #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 59 | pub struct AuthorityState { |
| 60 | pub principal: uuid::Uuid, |
| 61 | pub policy_profile: String, |
| 62 | pub capabilities: Vec<CapabilityGrant>, |
| 63 | pub restrictions: Vec<String>, |
| 64 | pub containment: ContainmentState, |
| 65 | } |
| 66 | |
| 67 | #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 68 | pub struct ResourceBudget { |
| 69 | pub max_tokens: Option<u64>, |
| 70 | pub max_cost_micro_usd: Option<u64>, |
| 71 | pub max_duration_seconds: Option<u64>, |
| 72 | pub max_subagents: Option<u32>, |
| 73 | } |
| 74 | |
| 75 | #[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 76 | pub enum AgentExecutionStatus { |
| 77 | Idle, |
| 78 | Running, |
| 79 | Suspended, |
| 80 | AwaitingApproval, |
| 81 | Terminated, |
| 82 | } |
| 83 | |
| 84 | #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 85 | pub struct ExecutionState { |
| 86 | pub active_run: Option<RunId>, |
| 87 | pub status: AgentExecutionStatus, |
| 88 | pub pending_actions: Vec<ActionId>, |
| 89 | pub pending_approvals: Vec<ApprovalId>, |
| 90 | pub last_event: Option<EventId>, |
| 91 | } |
| 92 | |
| 93 | #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 94 | pub struct StateRef { |
| 95 | pub state_id: uuid::Uuid, |
| 96 | pub digest: Digest32, |
| 97 | } |
| 98 | |
| 99 | #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 100 | pub struct AgentState { |
| 101 | pub abi_version: ProtocolVersion, |
| 102 | pub agent: AgentIdentity, |
| 103 | pub session: SessionState, |
| 104 | pub objective: Option<Objective>, |
| 105 | pub context: ContextState, |
| 106 | pub authority: AuthorityState, |
| 107 | pub budget: ResourceBudget, |
| 108 | pub execution: ExecutionState, |
| 109 | pub parent: Option<StateRef>, |
| 110 | pub sequence: SequenceNumber, |
| 111 | } |
| 112 | |
| 113 | impl AgentState { |
| 114 | pub fn compute_digest(&self) -> Result<Digest32, serde_json::Error> { |
| 115 | compute_canonical_json_digest(self) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 120 | pub enum AgentEvent { |
| 121 | SessionCreated, |
| 122 | MessageAdded(MessageRef), |
| 123 | RunStarted(RunId), |
| 124 | InferenceRequested(uuid::Uuid), |
| 125 | InferenceCompleted(uuid::Uuid), |
| 126 | ActionRequested(ActionId), |
| 127 | ActionAuthorized(ActionId), |
| 128 | ActionDenied(ActionId), |
| 129 | ActionCompleted(ActionId), |
| 130 | ApprovalRequested(ApprovalId), |
| 131 | ApprovalResolved(ApprovalId), |
| 132 | ContextBranched(uuid::Uuid), |
| 133 | BudgetUpdated(ResourceBudget), |
| 134 | ContainmentChanged(ContainmentState), |
| 135 | RunSuspended, |
| 136 | RunResumed, |
| 137 | RunCompleted(String), |
| 138 | SessionClosed, |
| 139 | } |
| 140 | |
| 141 | #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 142 | pub struct AgentStateEvent { |
| 143 | pub abi_version: ProtocolVersion, |
| 144 | pub event_id: EventId, |
| 145 | pub agent_id: AgentId, |
| 146 | pub session_id: SessionId, |
| 147 | pub run_id: Option<RunId>, |
| 148 | pub sequence: SequenceNumber, |
| 149 | pub timestamp: Timestamp, |
| 150 | pub previous_state: Digest32, |
| 151 | pub event: AgentEvent, |
| 152 | pub resulting_state: Digest32, |
| 153 | } |
| 154 | |
| 155 | #[cfg(test)] |
| 156 | mod tests { |
| 157 | use super::*; |
| 158 | |
| 159 | #[test] |
| 160 | fn test_agent_state_digest_deterministic() { |
| 161 | let state = AgentState { |
| 162 | abi_version: ProtocolVersion::new(1, 0), |
| 163 | agent: AgentIdentity { |
| 164 | agent_id: AgentId::new_v4(), |
| 165 | name: "Atlas".to_string(), |
| 166 | model_profile: "GB10-MAX-Qwen".to_string(), |
| 167 | created_at: Timestamp::from_micros(1000), |
| 168 | }, |
| 169 | session: SessionState { |
| 170 | session_id: SessionId::new_v4(), |
| 171 | title: "Test Session".to_string(), |
| 172 | created_at: Timestamp::from_micros(1000), |
| 173 | closed_at: None, |
| 174 | }, |
| 175 | objective: None, |
| 176 | context: ContextState { |
| 177 | conversation: Vec::new(), |
| 178 | pinned: Vec::new(), |
| 179 | summaries: Vec::new(), |
| 180 | cortex: Vec::new(), |
| 181 | inference: None, |
| 182 | }, |
| 183 | authority: AuthorityState { |
| 184 | principal: uuid::Uuid::new_v4(), |
| 185 | policy_profile: "autonomous-safe".to_string(), |
| 186 | capabilities: Vec::new(), |
| 187 | restrictions: Vec::new(), |
| 188 | containment: ContainmentState::default(), |
| 189 | }, |
| 190 | budget: ResourceBudget { |
| 191 | max_tokens: Some(4096), |
| 192 | max_cost_micro_usd: None, |
| 193 | max_duration_seconds: Some(60), |
| 194 | max_subagents: Some(4), |
| 195 | }, |
| 196 | execution: ExecutionState { |
| 197 | active_run: None, |
| 198 | status: AgentExecutionStatus::Idle, |
| 199 | pending_actions: Vec::new(), |
| 200 | pending_approvals: Vec::new(), |
| 201 | last_event: None, |
| 202 | }, |
| 203 | parent: None, |
| 204 | sequence: SequenceNumber(1), |
| 205 | }; |
| 206 | |
| 207 | let digest1 = state.compute_digest().unwrap(); |
| 208 | let digest2 = state.compute_digest().unwrap(); |
| 209 | assert_eq!(digest1, digest2); |
| 210 | assert_ne!(digest1, Digest32::ZERO); |
| 211 | } |
| 212 | } |