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 / tests / integration_tests.rs
| 1 | use aien_action_protocol::{CapabilityGrant, ContainmentState}; |
| 2 | use aien_agent_state_abi::*; |
| 3 | use aien_inference_client::MockInferenceClient; |
| 4 | use aien_inference_protocol::*; |
| 5 | use aien_protocol_types::*; |
| 6 | use aien_provenance::compute_sha256; |
| 7 | |
| 8 | #[test] |
| 9 | fn test_agent_state_roundtrip_and_lineage_verification() { |
| 10 | let agent_id = AgentId::new_v4(); |
| 11 | let session_id = SessionId::new_v4(); |
| 12 | let initial_timestamp = Timestamp::from_micros(1700000000); |
| 13 | |
| 14 | let initial_state = AgentState { |
| 15 | abi_version: ProtocolVersion::new(1, 0), |
| 16 | agent: AgentIdentity { |
| 17 | agent_id, |
| 18 | name: "Atlas-Sovereign".to_string(), |
| 19 | model_profile: "GB10-Grace-Blackwell".to_string(), |
| 20 | created_at: initial_timestamp, |
| 21 | }, |
| 22 | session: SessionState { |
| 23 | session_id, |
| 24 | title: "Autonomous Agent State ABI Verification".to_string(), |
| 25 | created_at: initial_timestamp, |
| 26 | closed_at: None, |
| 27 | }, |
| 28 | objective: Some(Objective { |
| 29 | objective_id: uuid::Uuid::new_v4(), |
| 30 | description: "Verify agent state ABI transitions".to_string(), |
| 31 | success_criteria: vec![ |
| 32 | "Deterministic state hash".to_string(), |
| 33 | "Zero unslop".to_string(), |
| 34 | ], |
| 35 | }), |
| 36 | context: ContextState { |
| 37 | conversation: Vec::new(), |
| 38 | pinned: Vec::new(), |
| 39 | summaries: Vec::new(), |
| 40 | cortex: Vec::new(), |
| 41 | inference: None, |
| 42 | }, |
| 43 | authority: AuthorityState { |
| 44 | principal: uuid::Uuid::new_v4(), |
| 45 | policy_profile: "autonomous-strict".to_string(), |
| 46 | capabilities: vec![CapabilityGrant { |
| 47 | capability: "shell.execute".to_string(), |
| 48 | scope: "sandbox:/tmp".to_string(), |
| 49 | granted_at: initial_timestamp, |
| 50 | expires_at: None, |
| 51 | }], |
| 52 | restrictions: vec!["zero_network_leak".to_string()], |
| 53 | containment: ContainmentState::default(), |
| 54 | }, |
| 55 | budget: ResourceBudget { |
| 56 | max_tokens: Some(8192), |
| 57 | max_cost_micro_usd: None, |
| 58 | max_duration_seconds: Some(120), |
| 59 | max_subagents: Some(16), |
| 60 | }, |
| 61 | execution: ExecutionState { |
| 62 | active_run: None, |
| 63 | status: AgentExecutionStatus::Idle, |
| 64 | pending_actions: Vec::new(), |
| 65 | pending_approvals: Vec::new(), |
| 66 | last_event: None, |
| 67 | }, |
| 68 | parent: None, |
| 69 | sequence: SequenceNumber(1), |
| 70 | }; |
| 71 | |
| 72 | let initial_digest = initial_state |
| 73 | .compute_digest() |
| 74 | .expect("Failed to compute initial digest"); |
| 75 | assert_ne!(initial_digest, Digest32::ZERO); |
| 76 | |
| 77 | // Transition 1: RunStarted |
| 78 | let run_id = RunId::new_v4(); |
| 79 | let event1 = AgentStateEvent { |
| 80 | abi_version: ProtocolVersion::new(1, 0), |
| 81 | event_id: EventId::new_v4(), |
| 82 | agent_id, |
| 83 | session_id, |
| 84 | run_id: Some(run_id), |
| 85 | sequence: SequenceNumber(2), |
| 86 | timestamp: Timestamp::from_micros(1700000050), |
| 87 | previous_state: initial_digest, |
| 88 | event: AgentEvent::RunStarted(run_id), |
| 89 | resulting_state: Digest32::ZERO, // updated below |
| 90 | }; |
| 91 | |
| 92 | let mut state_after_run = initial_state.clone(); |
| 93 | state_after_run.sequence = SequenceNumber(2); |
| 94 | state_after_run.execution.active_run = Some(run_id); |
| 95 | state_after_run.execution.status = AgentExecutionStatus::Running; |
| 96 | state_after_run.execution.last_event = Some(event1.event_id); |
| 97 | let digest_after_run = state_after_run.compute_digest().unwrap(); |
| 98 | assert_ne!(initial_digest, digest_after_run); |
| 99 | |
| 100 | // Verify state can be serialized to and from JSON |
| 101 | let json_str = serde_json::to_string_pretty(&state_after_run).unwrap(); |
| 102 | let deserialized: AgentState = serde_json::from_str(&json_str).unwrap(); |
| 103 | assert_eq!(state_after_run, deserialized); |
| 104 | assert_eq!(deserialized.compute_digest().unwrap(), digest_after_run); |
| 105 | } |
| 106 | |
| 107 | #[tokio::test] |
| 108 | async fn test_inference_client_cow_branching_contract() { |
| 109 | let client = MockInferenceClient::new(); |
| 110 | let parent_context = InferenceContextRef { |
| 111 | abi_version: 1, |
| 112 | context_id: ContextId::new_v4(), |
| 113 | branch_id: BranchId::new_v4(), |
| 114 | generation: Generation(1), |
| 115 | model: ModelFingerprint { |
| 116 | weights_digest: compute_sha256(b"tinyllama-1.1b-bf16"), |
| 117 | model_config_digest: compute_sha256(b"config-v1"), |
| 118 | }, |
| 119 | tokenizer: TokenizerFingerprint { |
| 120 | tokenizer_digest: compute_sha256(b"tokenizer-v1"), |
| 121 | }, |
| 122 | kv_format: KvFormatFingerprint { |
| 123 | format_version: 1, |
| 124 | dtype: "BF16".to_string(), |
| 125 | }, |
| 126 | logical_state_digest: compute_sha256(b"logical-tokens-root"), |
| 127 | token_count: 128, |
| 128 | lineage: ContextLineage { |
| 129 | parent_context: None, |
| 130 | parent_branch: None, |
| 131 | parent_digest: None, |
| 132 | fork_token_index: None, |
| 133 | }, |
| 134 | isolation: CacheIsolationKey { |
| 135 | domain_id: uuid::Uuid::new_v4(), |
| 136 | domain_digest: compute_sha256(b"tenant-sovereign"), |
| 137 | }, |
| 138 | binding: None, |
| 139 | recovery: ContextRecipeRef { |
| 140 | recipe_id: uuid::Uuid::new_v4(), |
| 141 | recipe_digest: compute_sha256(b"recipe-data"), |
| 142 | }, |
| 143 | }; |
| 144 | |
| 145 | let child_branch_id = BranchId::new_v4(); |
| 146 | let branch_req = BranchContextRequest { |
| 147 | operation_id: uuid::Uuid::new_v4(), |
| 148 | parent_context: parent_context.clone(), |
| 149 | child_branch_id, |
| 150 | isolation: parent_context.isolation, |
| 151 | }; |
| 152 | |
| 153 | let receipt = client |
| 154 | .branch_context(branch_req) |
| 155 | .await |
| 156 | .expect("Failed to branch context"); |
| 157 | assert_eq!(receipt.child_context.branch_id, child_branch_id); |
| 158 | assert_eq!(receipt.child_context.generation, Generation(2)); |
| 159 | assert_eq!( |
| 160 | receipt.child_context.lineage.parent_context, |
| 161 | Some(parent_context.context_id) |
| 162 | ); |
| 163 | assert_eq!( |
| 164 | receipt.child_context.lineage.parent_branch, |
| 165 | Some(parent_context.branch_id) |
| 166 | ); |
| 167 | assert_eq!(receipt.shared_pages, 10); |
| 168 | assert_eq!(receipt.copied_pages, 0); |
| 169 | } |