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
aien-sovereign-core-mv / crates / aien-runtime / tests / runtime_spine_integration.rs
| 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 | //! Comprehensive integration tests for the canonical AienRuntimeSpine. |
| 17 | |
| 18 | use aien_inference_abi::MockInferenceBackend; |
| 19 | use aien_kv_cache::{create_shared_kv_manager, KvDType, KvPoolConfig}; |
| 20 | use aien_runtime::control::{ControlCommand, ControlEnvelope, ControlResponse, LaunchSwarmReq}; |
| 21 | use aien_runtime::sequence::SequenceArena; |
| 22 | use aien_runtime::spine::AienRuntimeSpine; |
| 23 | use aien_runtime::world::{EffectIntent, WorldDraft, WorldStore}; |
| 24 | use aien_scheduler::SchedulerConfig; |
| 25 | |
| 26 | #[test] |
| 27 | fn test_sequence_arena_generational_invalidation() { |
| 28 | let mut arena = SequenceArena::new(16); |
| 29 | let id1 = arena.allocate(1, 1, 10, 100).unwrap(); |
| 30 | assert_eq!(id1.slot, 0); |
| 31 | assert_eq!(id1.generation, 1); |
| 32 | assert!(arena.validate(id1)); |
| 33 | |
| 34 | // Free the sequence -> increments slot generation |
| 35 | assert!(arena.free(id1)); |
| 36 | assert!(!arena.validate(id1), "Old SequenceId must be invalid"); |
| 37 | |
| 38 | // Re-allocate the slot -> receives generation 2 |
| 39 | let id2 = arena.allocate(1, 1, 10, 200).unwrap(); |
| 40 | assert_eq!(id2.slot, id1.slot); |
| 41 | assert_eq!(id2.generation, 2); |
| 42 | |
| 43 | // Old SequenceId still rejected |
| 44 | assert!(!arena.validate(id1)); |
| 45 | assert!(arena.validate(id2)); |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn test_context_composition_and_missing_prefill() { |
| 50 | let mut composer = aien_runtime::context::ContextComposer::new(); |
| 51 | let sys_tokens = vec![1, 2, 3, 4]; |
| 52 | let user_tokens = vec![10, 20, 30, 40, 50]; |
| 53 | let root_rev = composer.create_root_revision(sys_tokens, user_tokens); |
| 54 | |
| 55 | assert_eq!(composer.get_revision(root_rev).unwrap().total_tokens, 9); |
| 56 | |
| 57 | // Sequence with prefill_cursor = 4 needs 5 tokens |
| 58 | let span = composer.compute_missing_prefill(root_rev, 4).unwrap(); |
| 59 | assert_eq!(span.start, 4); |
| 60 | assert_eq!(span.len, 5); |
| 61 | assert_eq!(span.tokens, vec![10, 20, 30, 40, 50]); |
| 62 | |
| 63 | // Append memory |
| 64 | let mem_tokens = vec![100, 101, 102]; |
| 65 | let rev2 = composer.append_memory(root_rev, 42, mem_tokens).unwrap(); |
| 66 | assert_eq!(composer.get_revision(rev2).unwrap().total_tokens, 12); |
| 67 | |
| 68 | let span2 = composer.compute_missing_prefill(rev2, 9).unwrap(); |
| 69 | assert_eq!(span2.start, 9); |
| 70 | assert_eq!(span2.len, 3); |
| 71 | assert_eq!(span2.tokens, vec![100, 101, 102]); |
| 72 | } |
| 73 | |
| 74 | #[test] |
| 75 | fn test_world_forking_and_effect_staging() { |
| 76 | let mut store = WorldStore::new(); |
| 77 | let root_world = store.create_root_world(1, 1, 10, 100); |
| 78 | |
| 79 | // Fork child world: constant-time root sharing |
| 80 | let child_world = store.fork_world(root_world, 200).unwrap(); |
| 81 | let child_man = store.get_world(child_world).unwrap(); |
| 82 | assert_eq!(child_man.parent, Some(root_world)); |
| 83 | assert_eq!(child_man.effect_log.len(), 0); |
| 84 | |
| 85 | // Stage mutations in draft |
| 86 | let mut draft = WorldDraft::new(child_world); |
| 87 | draft.stage_object("report.md".to_string(), b"# Security Audit".to_vec()); |
| 88 | draft.record_effect(EffectIntent::WriteFile { |
| 89 | path: "report.md".to_string(), |
| 90 | content_hash: [1u8; 32], |
| 91 | }); |
| 92 | |
| 93 | let committed_world = store.commit_draft(draft, 300).unwrap(); |
| 94 | let committed_man = store.get_world(committed_world).unwrap(); |
| 95 | assert_eq!(committed_man.parent, Some(child_world)); |
| 96 | assert_eq!(committed_man.effect_log.len(), 1); |
| 97 | |
| 98 | // Parent world remains unmodified |
| 99 | let root_man = store.get_world(root_world).unwrap(); |
| 100 | assert_eq!(root_man.effect_log.len(), 0); |
| 101 | } |
| 102 | |
| 103 | #[tokio::test] |
| 104 | async fn test_swarm_launch_and_step_execution() { |
| 105 | let pool_cfg = KvPoolConfig::for_tinyllama(256, 16, KvDType::Bf16); |
| 106 | let kv_manager = create_shared_kv_manager(256, 16); |
| 107 | kv_manager.write().attach_tensor_pool(pool_cfg).unwrap(); |
| 108 | |
| 109 | let sched_cfg = SchedulerConfig { |
| 110 | max_batch_size: 64, |
| 111 | max_batch_tokens: 2048, |
| 112 | max_prefill_tokens: 1024, |
| 113 | prefill_chunk_size: 128, |
| 114 | chunk_prefill: true, |
| 115 | watermark_blocks: 4, |
| 116 | }; |
| 117 | |
| 118 | let mut spine = AienRuntimeSpine::new(128, sched_cfg, kv_manager.clone()); |
| 119 | let mut backend = MockInferenceBackend::new(1); |
| 120 | |
| 121 | let prompt: Vec<u32> = (0..32).collect(); |
| 122 | |
| 123 | let root_world = spine.world_store.create_root_world(1, 1, 10, 100); |
| 124 | |
| 125 | // Launch swarm of 16 branches via ControlEnvelope |
| 126 | let launch_req = LaunchSwarmReq { |
| 127 | model_handle: 1, |
| 128 | branch_count: 16, |
| 129 | max_active_sequences: 32, |
| 130 | max_tokens_per_branch: 4, |
| 131 | root_world_id: root_world, |
| 132 | priority: 5, |
| 133 | prompt_tokens: prompt.clone(), |
| 134 | }; |
| 135 | |
| 136 | // Unique per run so persistent idempotency from earlier runs cannot |
| 137 | // pollute this test. The replay check below reuses the same ID in run. |
| 138 | let operation_id: u128 = std::time::SystemTime::now() |
| 139 | .duration_since(std::time::UNIX_EPOCH) |
| 140 | .unwrap_or_default() |
| 141 | .as_nanos(); |
| 142 | let env = ControlEnvelope { |
| 143 | protocol_version: 1, |
| 144 | request_id: 1, |
| 145 | operation_id, |
| 146 | operator_session: 1, |
| 147 | command: ControlCommand::LaunchSwarm(launch_req), |
| 148 | }; |
| 149 | |
| 150 | let resp = spine.handle_control_command(env.clone()); |
| 151 | match resp { |
| 152 | ControlResponse::SwarmAccepted { |
| 153 | swarm_id, |
| 154 | operation_id: accepted_id, |
| 155 | } => { |
| 156 | assert_eq!(swarm_id, 1); |
| 157 | assert_eq!(accepted_id, operation_id); |
| 158 | } |
| 159 | _ => panic!("Expected SwarmAccepted response"), |
| 160 | } |
| 161 | |
| 162 | // Replay of same operation_id must be rejected (idempotency) |
| 163 | let replay_resp = spine.handle_control_command(env); |
| 164 | match replay_resp { |
| 165 | ControlResponse::Error(msg) => { |
| 166 | assert!(msg.contains("already processed")); |
| 167 | } |
| 168 | _ => panic!("Expected Error response on operation replay"), |
| 169 | } |
| 170 | |
| 171 | // Verify 16 child branches + 1 root sequence allocated in arena |
| 172 | assert_eq!(spine.arena.active_count(), 17); |
| 173 | |
| 174 | // Advance engine steps until all scheduled branch sequences finish |
| 175 | let mut steps = 0; |
| 176 | while (spine.scheduler.running_count() > 0 || spine.scheduler.waiting_count() > 0) && steps < 50 |
| 177 | { |
| 178 | steps += 1; |
| 179 | let _ = spine.step(&mut backend).await.unwrap(); |
| 180 | } |
| 181 | |
| 182 | // All 16 branches completed; natural completion reclaims the root anchor too |
| 183 | assert_eq!( |
| 184 | spine.arena.active_count(), |
| 185 | 0, |
| 186 | "Swarm completion reclaims the root anchor; zero sequences remain" |
| 187 | ); |
| 188 | let status = spine.status_report(); |
| 189 | assert_eq!(status.active_sequences, 0); |
| 190 | } |