|
| 1 | +//! Context Perception Integration Tests with Real LLM (Minimax) |
| 2 | +//! |
| 3 | +//! Run with: |
| 4 | +//! ```bash |
| 5 | +//! cd crates/code/core |
| 6 | +//! export MINIMAX_API_KEY="your-api-key" |
| 7 | +//! export MINIMAX_BASE_URL="https://your-endpoint/v1/" # optional |
| 8 | +//! export MINIMAX_MODEL="MiniMax-M2.7-highspeed" # optional |
| 9 | +//! cargo test --features ahp --test test_context_perception_with_llm -- --ignored --test-threads=1 --nocapture |
| 10 | +//! ``` |
| 11 | +
|
| 12 | +#![cfg(feature = "ahp")] |
| 13 | + |
| 14 | +use std::path::PathBuf; |
| 15 | +use std::sync::Arc; |
| 16 | +use std::time::Instant; |
| 17 | + |
| 18 | +use a3s_code_core::agent::{AgentConfig, AgentLoop}; |
| 19 | +use a3s_code_core::ahp::{AhpHookExecutor, AhpTransport}; |
| 20 | +use a3s_code_core::llm::OpenAiClient; |
| 21 | +use a3s_code_core::tools::ToolExecutor; |
| 22 | + |
| 23 | +/// Create a ToolContext for tests |
| 24 | +fn test_tool_context() -> a3s_code_core::tools::ToolContext { |
| 25 | + a3s_code_core::tools::ToolContext::new(PathBuf::from("/tmp")) |
| 26 | +} |
| 27 | + |
| 28 | +/// Get test config from environment variables |
| 29 | +fn get_test_config() -> (String, String, String) { |
| 30 | + let api_key = |
| 31 | + std::env::var("MINIMAX_API_KEY").expect("MINIMAX_API_KEY environment variable not set"); |
| 32 | + let base_url = std::env::var("MINIMAX_BASE_URL") |
| 33 | + .unwrap_or_else(|_| "https://api.minimax.io/v1/".to_string()); |
| 34 | + let model = |
| 35 | + std::env::var("MINIMAX_MODEL").unwrap_or_else(|_| "MiniMax-M2.7-highspeed".to_string()); |
| 36 | + (api_key, base_url, model) |
| 37 | +} |
| 38 | + |
| 39 | +/// Test: Benchmark intent detection performance |
| 40 | +/// This test doesn't need a real LLM - it just tests the intent detection logic |
| 41 | +#[test] |
| 42 | +#[ignore] |
| 43 | +fn test_intent_detection_performance() { |
| 44 | + // We need to create an AgentLoop, but we can use a dummy LLM client |
| 45 | + // For testing intent detection, we just need any LLM client |
| 46 | + let (api_key, base_url, model) = get_test_config(); |
| 47 | + let client = OpenAiClient::new(api_key.into(), model).with_base_url(base_url); |
| 48 | + let tool_executor = Arc::new(ToolExecutor::new("/tmp".to_string())); |
| 49 | + |
| 50 | + let config = AgentConfig::default(); |
| 51 | + |
| 52 | + let agent = AgentLoop::new(Arc::new(client), tool_executor, test_tool_context(), config); |
| 53 | + |
| 54 | + let prompt = "Where is the main function in auth.rs? Explain how the login works and verify the test cases."; |
| 55 | + |
| 56 | + // Warm up |
| 57 | + for _ in 0..100 { |
| 58 | + let _ = agent.detect_context_perception_intent(prompt, "test", "/workspace"); |
| 59 | + } |
| 60 | + |
| 61 | + // Benchmark |
| 62 | + let iterations = 1000; |
| 63 | + let start = Instant::now(); |
| 64 | + for _ in 0..iterations { |
| 65 | + let _ = agent.detect_context_perception_intent(prompt, "test", "/workspace"); |
| 66 | + } |
| 67 | + let elapsed = start.elapsed(); |
| 68 | + |
| 69 | + println!("\n=== Intent Detection Performance ==="); |
| 70 | + println!("Prompt: {}", prompt); |
| 71 | + println!("Iterations: {}", iterations); |
| 72 | + println!("Total time: {:?}", elapsed); |
| 73 | + println!( |
| 74 | + "Average: {:.3} µs/op", |
| 75 | + elapsed.as_micros() as f64 / iterations as f64 |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +/// Test: Full agent with context perception (requires AHP harness server) |
| 80 | +#[test] |
| 81 | +#[ignore] |
| 82 | +fn test_context_perception_with_ahp_harness() { |
| 83 | + let (api_key, base_url, model) = get_test_config(); |
| 84 | + let client = OpenAiClient::new(api_key.into(), model).with_base_url(base_url); |
| 85 | + let tool_executor = Arc::new(ToolExecutor::new("/tmp".to_string())); |
| 86 | + |
| 87 | + // Create AHP executor with stdio transport |
| 88 | + let rt = tokio::runtime::Runtime::new().unwrap(); |
| 89 | + |
| 90 | + println!("Creating AHP transport..."); |
| 91 | + let transport = AhpTransport::Stdio { |
| 92 | + program: "echo".to_string(), |
| 93 | + args: vec![], |
| 94 | + }; |
| 95 | + let transport_result: Result<AhpTransport, Box<dyn std::error::Error + Send + Sync>> = |
| 96 | + Ok(transport); |
| 97 | + |
| 98 | + let hook_engine: Option<Arc<dyn a3s_code_core::hooks::HookExecutor>> = match transport_result { |
| 99 | + Ok(transport) => { |
| 100 | + println!("Transport created, connecting to harness..."); |
| 101 | + match rt.block_on(AhpHookExecutor::new_with_config(transport, 5000)) { |
| 102 | + Ok(ahp) => { |
| 103 | + println!("Connected to harness!"); |
| 104 | + Some(Arc::new(ahp) as Arc<dyn a3s_code_core::hooks::HookExecutor>) |
| 105 | + } |
| 106 | + Err(e) => { |
| 107 | + println!("Failed to create AHP executor: {}", e); |
| 108 | + None |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + Err(e) => { |
| 113 | + println!("Failed to create transport: {}", e); |
| 114 | + None |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + let has_harness = hook_engine.is_some(); |
| 119 | + |
| 120 | + let config = AgentConfig { |
| 121 | + hook_engine, |
| 122 | + ..Default::default() |
| 123 | + }; |
| 124 | + |
| 125 | + let agent = AgentLoop::new(Arc::new(client), tool_executor, test_tool_context(), config); |
| 126 | + |
| 127 | + // Run a prompt that triggers context perception |
| 128 | + let prompt = "Where is the main function defined? Explain how the auth module works."; |
| 129 | + |
| 130 | + println!("\n=== Context Perception Test ==="); |
| 131 | + println!("Prompt: {}", prompt); |
| 132 | + |
| 133 | + // Test intent detection first |
| 134 | + let intent = agent.detect_context_perception_intent(prompt, "test-session", "/workspace"); |
| 135 | + println!("Detected intent: {:?}", intent.map(|i| i.intent)); |
| 136 | + |
| 137 | + // If we have a harness, run the full agent |
| 138 | + if has_harness { |
| 139 | + println!("Running full agent with harness..."); |
| 140 | + let start = Instant::now(); |
| 141 | + let result = rt.block_on(agent.execute_with_session(&[], prompt, Some("test"), None, None)); |
| 142 | + let elapsed = start.elapsed(); |
| 143 | + |
| 144 | + match result { |
| 145 | + Ok(r) => { |
| 146 | + let preview = if r.text.len() > 200 { |
| 147 | + format!("{}...", &r.text[..200]) |
| 148 | + } else { |
| 149 | + r.text.clone() |
| 150 | + }; |
| 151 | + println!("Success!"); |
| 152 | + println!("Response: {}", preview); |
| 153 | + println!("Tokens: {:?}", r.usage); |
| 154 | + println!("Total time: {:?}", elapsed); |
| 155 | + } |
| 156 | + Err(e) => println!("Error: {}", e), |
| 157 | + } |
| 158 | + } else { |
| 159 | + println!("No harness configured, skipping full test"); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +/// Test: Compare with and without context providers (performance) |
| 164 | +#[test] |
| 165 | +#[ignore] |
| 166 | +fn test_performance_comparison() { |
| 167 | + let (api_key, base_url, model) = get_test_config(); |
| 168 | + let client1 = |
| 169 | + OpenAiClient::new(api_key.clone().into(), model.clone()).with_base_url(base_url.clone()); |
| 170 | + let _client2 = OpenAiClient::new(api_key.into(), model).with_base_url(base_url); |
| 171 | + let tool_executor = Arc::new(ToolExecutor::new("/tmp".to_string())); |
| 172 | + |
| 173 | + let prompt = "Explain the codebase structure of this project."; |
| 174 | + |
| 175 | + // Without context providers |
| 176 | + let config_no_ctx = AgentConfig::default(); |
| 177 | + |
| 178 | + let agent_no_ctx = AgentLoop::new( |
| 179 | + Arc::new(client1), |
| 180 | + tool_executor.clone(), |
| 181 | + test_tool_context(), |
| 182 | + config_no_ctx, |
| 183 | + ); |
| 184 | + |
| 185 | + let rt = tokio::runtime::Runtime::new().unwrap(); |
| 186 | + |
| 187 | + println!("\n=== Performance Comparison ==="); |
| 188 | + println!("Prompt: {}", prompt); |
| 189 | + println!("\nRunning WITHOUT context providers..."); |
| 190 | + |
| 191 | + let start = Instant::now(); |
| 192 | + let result = |
| 193 | + rt.block_on(agent_no_ctx.execute_with_session(&[], prompt, Some("test"), None, None)); |
| 194 | + let without_time = start.elapsed(); |
| 195 | + |
| 196 | + match result { |
| 197 | + Ok(r) => { |
| 198 | + let preview = if r.text.len() > 100 { |
| 199 | + format!("{}...", &r.text[..100]) |
| 200 | + } else { |
| 201 | + r.text.clone() |
| 202 | + }; |
| 203 | + println!("Time: {:?}", without_time); |
| 204 | + println!("Tokens: {:?}", r.usage); |
| 205 | + println!("Response preview: {}", preview); |
| 206 | + } |
| 207 | + Err(e) => println!("Error: {}", e), |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +/// Test: Verify all intent types are detected correctly |
| 212 | +#[test] |
| 213 | +#[ignore] |
| 214 | +fn test_all_intent_types() { |
| 215 | + let (api_key, base_url, model) = get_test_config(); |
| 216 | + let client = OpenAiClient::new(api_key.into(), model).with_base_url(base_url); |
| 217 | + let tool_executor = Arc::new(ToolExecutor::new("/tmp".to_string())); |
| 218 | + let config = AgentConfig::default(); |
| 219 | + |
| 220 | + let agent = AgentLoop::new(Arc::new(client), tool_executor, test_tool_context(), config); |
| 221 | + |
| 222 | + let test_cases = vec![ |
| 223 | + // Locate |
| 224 | + ("Where is the main function?", "locate"), |
| 225 | + ("Find all files related to auth", "locate"), |
| 226 | + ("Locate the config file", "locate"), |
| 227 | + // Understand |
| 228 | + ("How does authentication work?", "understand"), |
| 229 | + ("What does this code do?", "understand"), |
| 230 | + ("Explain the login flow", "understand"), |
| 231 | + // Retrieve |
| 232 | + ("Remember what we discussed earlier?", "retrieve"), |
| 233 | + ("What was the previous approach?", "retrieve"), |
| 234 | + // Explore |
| 235 | + ("What files are in this project?", "explore"), |
| 236 | + ("Show me the project structure", "explore"), |
| 237 | + // Reason |
| 238 | + ("Why did the build fail?", "reason"), |
| 239 | + ("Why is this code structured this way?", "reason"), |
| 240 | + // Validate |
| 241 | + ("Verify this code is correct", "validate"), |
| 242 | + ("Check if the tests pass", "validate"), |
| 243 | + // Compare |
| 244 | + ("What's the difference between A and B?", "compare"), |
| 245 | + ("Compare these two approaches", "compare"), |
| 246 | + // Track |
| 247 | + ("Show me the status of the task", "track"), |
| 248 | + ("What's the progress?", "track"), |
| 249 | + ]; |
| 250 | + |
| 251 | + println!("\n=== Intent Detection Test ==="); |
| 252 | + let mut passed = 0; |
| 253 | + let mut failed = 0; |
| 254 | + |
| 255 | + for (prompt, expected) in test_cases { |
| 256 | + let intent = agent.detect_context_perception_intent(prompt, "test-session", "/workspace"); |
| 257 | + match intent { |
| 258 | + Some(i) if i.intent == expected => { |
| 259 | + println!("OK: '{}' -> {}", &prompt[..prompt.len().min(40)], expected); |
| 260 | + passed += 1; |
| 261 | + } |
| 262 | + Some(i) => { |
| 263 | + println!( |
| 264 | + "FAIL: '{}' -> expected '{}', got '{}'", |
| 265 | + &prompt[..prompt.len().min(40)], |
| 266 | + expected, |
| 267 | + i.intent |
| 268 | + ); |
| 269 | + failed += 1; |
| 270 | + } |
| 271 | + None => { |
| 272 | + println!( |
| 273 | + "FAIL: '{}' -> expected '{}', got None", |
| 274 | + &prompt[..prompt.len().min(40)], |
| 275 | + expected |
| 276 | + ); |
| 277 | + failed += 1; |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + println!("\nResults: {} passed, {} failed", passed, failed); |
| 283 | + assert_eq!(failed, 0, "Some intent detection tests failed"); |
| 284 | +} |
0 commit comments