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/aien-local-stack/blob/main/tests/cow_demonstration.rs # aien-local-stack aien-dev/aien-local-stack 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 `aien-local-stack / tests / cow_demonstration.rs` 183 lines ยท 6639 bytes Raw Blame History 1 use aien_inference_protocol::*; 2 use aien_local_stack::{ContextRecipe, RecipeStore, SovereignInferenceService}; 3 use uuid::Uuid; 4 5 #[tokio::test] 6 async fn test_flagship_cow_multi_branch_hardened_lifecycle() { 7 let temp_dir = std::env::temp_dir().join(format!("aien-test-recipe-store-{}", Uuid::new_v4())); 8 let recipe_store = RecipeStore::with_storage_dir(&temp_dir) 9 .expect("Failed to initialize file-backed RecipeStore"); 10 11 let service = SovereignInferenceService::new_with_epoch(1).expect("Service creation failed"); 12 13 // Stage 1: Prefill root context with durable ContextRecipe 14 let prompt: Vec = (1..=40).map(|x| (x * 7) % 256).collect(); 15 let mut recipe = ContextRecipe::new(prompt.clone()); 16 recipe_store.insert(recipe.clone()).unwrap(); 17 18 let (_root_handle, root_context) = service 19 .create_root_context(&prompt, &recipe) 20 .expect("create root context failed"); 21 22 assert_eq!(root_context.generation, Generation(1)); 23 assert_eq!(root_context.token_count, 40); 24 assert_eq!(root_context.kv_format.dtype, "FP32"); 25 assert_eq!(service.get_cow_faults(), 0); 26 27 let root_blocks = service.get_block_table(root_context.branch_id.0).unwrap(); 28 assert_eq!(root_blocks.len(), 3); 29 for &b in &root_blocks { 30 assert_eq!(service.get_block_refcount(b), Some(1)); 31 } 32 33 // Stage 2: Fork into 3 branches 34 let mut branch_contexts = Vec::new(); 35 for _ in 0..3 { 36 let child_branch_id = BranchId::new_v4(); 37 let branch_req = BranchContextRequest { 38 operation_id: Uuid::new_v4(), 39 parent_context: root_context.clone(), 40 child_branch_id, 41 isolation: root_context.isolation, 42 }; 43 let receipt = service.branch_context(branch_req).await.unwrap(); 44 assert_eq!(receipt.shared_pages, 3); 45 assert_eq!(receipt.copied_pages, 0); 46 assert_eq!(receipt.child_context.generation, Generation(2)); 47 branch_contexts.push(receipt.child_context); 48 } 49 50 let m2 = service.get_kv_metrics().unwrap(); 51 assert_eq!(m2.used_blocks, 3, "Flat zero-copy invariant"); 52 for &b in &root_blocks { 53 assert_eq!( 54 service.get_block_refcount(b), 55 Some(4), 56 "Root + 3 branches = 4" 57 ); 58 } 59 60 // Stage 3: True Branch Divergence 61 // Branch 0 receives "gen" delta 62 let req0 = InferenceRequest { 63 request_id: Uuid::new_v4(), 64 model: "sovereign-embedded".to_string(), 65 messages: vec![InferenceMessage { 66 role: "user".to_string(), 67 content: "gen".to_string(), 68 }], 69 context: Some(branch_contexts[0].clone()), 70 max_tokens: 1, 71 temperature: 0.0, 72 stop_sequences: vec![], 73 }; 74 let res0 = service.infer(req0).await.unwrap(); 75 let ctx0 = res0.context.unwrap(); 76 assert_eq!(service.get_cow_faults(), 1); 77 assert_eq!(ctx0.generation, Generation(3)); 78 // 40 root tokens + 3 "gen" delta tokens + 1 generated token = 44 tokens 79 assert_eq!(ctx0.token_count, 44); 80 81 // Branch 1 receives "sec" delta 82 let req1 = InferenceRequest { 83 request_id: Uuid::new_v4(), 84 model: "sovereign-embedded".to_string(), 85 messages: vec![InferenceMessage { 86 role: "user".to_string(), 87 content: "sec".to_string(), 88 }], 89 context: Some(branch_contexts[1].clone()), 90 max_tokens: 1, 91 temperature: 0.0, 92 stop_sequences: vec![], 93 }; 94 let res1 = service.infer(req1).await.unwrap(); 95 let ctx1 = res1.context.unwrap(); 96 assert_eq!(service.get_cow_faults(), 2); 97 assert_eq!(ctx1.generation, Generation(3)); 98 assert_eq!(ctx1.token_count, 44); 99 100 // Verify true logical divergence 101 assert_ne!(ctx0.logical_state_digest, ctx1.logical_state_digest); 102 103 // Branch 2 remains unmodified and fully shared 104 let b2_blocks = service 105 .get_block_table(branch_contexts[2].branch_id.0) 106 .unwrap(); 107 assert_eq!(b2_blocks, root_blocks); 108 109 // Snapshot Branch 0 state (including generated tokens) into durable recipe 110 recipe = service 111 .snapshot_branch_recipe(ctx0.branch_id.0, &recipe) 112 .expect("Snapshot recipe failed"); 113 recipe_store.insert(recipe.clone()).unwrap(); 114 115 // Stage 4: Zero-copy logical merge 116 let winner_id = ctx0.branch_id.0; 117 let sibling_ids = [ 118 branch_contexts[1].branch_id.0, 119 branch_contexts[2].branch_id.0, 120 ]; 121 service.select_branch(winner_id, &sibling_ids).unwrap(); 122 123 // Stage 5: Authentic Engine Crash & RecipeStore Recovery 124 let pre_crash_digest = ctx0.logical_state_digest; 125 let pre_crash_recipe_ref = recipe.to_ref(); 126 127 // Complete destruction of engine instance and recipe store memory 128 drop(service); 129 drop(recipe_store); 130 131 let new_service = SovereignInferenceService::new_with_epoch(2).expect("Restart failed"); 132 assert_eq!(new_service.runtime_epoch(), 2); 133 134 let recovered_recipe_store = 135 RecipeStore::with_storage_dir(&temp_dir).expect("Failed to reload RecipeStore from disk"); 136 137 // Stale epoch rejected 138 let stale_req = InferenceRequest { 139 request_id: Uuid::new_v4(), 140 model: "sovereign-embedded".to_string(), 141 messages: vec![InferenceMessage { 142 role: "user".to_string(), 143 content: "continue".to_string(), 144 }], 145 context: Some(ctx0.clone()), 146 max_tokens: 1, 147 temperature: 0.0, 148 stop_sequences: vec![], 149 }; 150 match new_service.infer(stale_req).await { 151 Err(InferenceError::StaleRuntimeEpoch) => {} 152 other => panic!("Expected StaleRuntimeEpoch, got {:?}", other), 153 } 154 155 // Recipe store recovery from disk 156 let verified_recipe = recovered_recipe_store 157 .verify_and_get(&pre_crash_recipe_ref) 158 .unwrap(); 159 let (_recon_handle, recon_ctx) = new_service 160 .reconstruct_from_recipe(winner_id, &verified_recipe) 161 .unwrap(); 162 163 assert_eq!(recon_ctx.logical_state_digest, pre_crash_digest); 164 assert_eq!(recon_ctx.token_count, 44); 165 166 let _ = std::fs::remove_dir_all(&temp_dir); 167 168 // Continued inference on reconstructed branch 169 let cont_req = InferenceRequest { 170 request_id: Uuid::new_v4(), 171 model: "sovereign-embedded".to_string(), 172 messages: vec![InferenceMessage { 173 role: "user".to_string(), 174 content: "next".to_string(), 175 }], 176 context: Some(recon_ctx), 177 max_tokens: 1, 178 temperature: 0.0, 179 stop_sequences: vec![], 180 }; 181 let cont_res = new_service.infer(cont_req).await.unwrap(); 182 assert_eq!(cont_res.completion_tokens, 1); 183 }