agcodex-core 0.1.0

Core business logic with AST-RAG engine and tree-sitter integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Example usage of the AgentOrchestrator
//!
//! This module demonstrates how to use the orchestrator for various
//! agent execution patterns.

#[cfg(test)]
mod examples {
    use crate::modes::OperatingMode;
    use crate::subagents::{
        AgentChain, AgentInvocation, AgentOrchestrator, ExecutionPlan, ExecutionStep,
        InvocationRequest, OrchestratorConfig, SharedContext, SubagentConfig, SubagentRegistry,
        SubagentStatus,
    };
    use std::collections::HashMap;
    use std::sync::Arc;
    use std::time::Duration;
    use uuid::Uuid;
    
    /// Example: Single agent execution
    #[tokio::test]
    async fn example_single_agent() {
        // Create registry and register agents
        let registry = Arc::new(SubagentRegistry::new());
        registry.register(create_test_agent("code-reviewer")).await.unwrap();
        
        // Create orchestrator
        let orchestrator = AgentOrchestrator::new(
            registry,
            OrchestratorConfig::default(),
            OperatingMode::Review,
        );
        
        // Create invocation request
        let request = InvocationRequest {
            id: Uuid::new_v4(),
            original_input: "@code-reviewer check src/main.rs".to_string(),
            execution_plan: ExecutionPlan::Single(AgentInvocation {
                agent_name: "code-reviewer".to_string(),
                parameters: HashMap::from([
                    ("file".to_string(), "src/main.rs".to_string()),
                ]),
                raw_parameters: "check src/main.rs".to_string(),
                position: 0,
            }),
            context: "Check the main file for issues".to_string(),
        };
        
        // Execute
        let result = orchestrator.execute_plan(request).await.unwrap();
        
        assert!(result.success);
        assert_eq!(result.executions.len(), 1);
        println!("Single agent completed in {:?}", result.total_duration);
    }
    
    /// Example: Sequential chain execution with output passing
    #[tokio::test]
    async fn example_sequential_chain() {
        let registry = Arc::new(SubagentRegistry::new());
        registry.register(create_test_agent("refactorer")).await.unwrap();
        registry.register(create_test_agent("test-writer")).await.unwrap();
        registry.register(create_test_agent("docs")).await.unwrap();
        
        let orchestrator = AgentOrchestrator::new(
            registry,
            OrchestratorConfig::default(),
            OperatingMode::Build,
        );
        
        let request = InvocationRequest {
            id: Uuid::new_v4(),
            original_input: "@refactorer → @test-writer → @docs".to_string(),
            execution_plan: ExecutionPlan::Sequential(AgentChain {
                agents: vec![
                    create_invocation("refactorer", "improve code structure"),
                    create_invocation("test-writer", "add missing tests"),
                    create_invocation("docs", "update documentation"),
                ],
                pass_output: true, // Pass output from one agent to the next
            }),
            context: "Improve the codebase".to_string(),
        };
        
        let result = orchestrator.execute_plan(request).await.unwrap();
        
        assert_eq!(result.executions.len(), 3);
        
        // Check that outputs were passed through the chain
        let outputs = result.context.all_outputs().await;
        assert_eq!(outputs.len(), 3);
        
        println!("Sequential chain completed with {} agents", result.executions.len());
    }
    
    /// Example: Parallel execution for independent tasks
    #[tokio::test]
    async fn example_parallel_execution() {
        let registry = Arc::new(SubagentRegistry::new());
        registry.register(create_test_agent("performance")).await.unwrap();
        registry.register(create_test_agent("security")).await.unwrap();
        registry.register(create_test_agent("code-reviewer")).await.unwrap();
        
        let orchestrator = AgentOrchestrator::new(
            registry,
            OrchestratorConfig {
                max_concurrency: 3, // Allow 3 agents to run simultaneously
                ..Default::default()
            },
            OperatingMode::Review,
        );
        
        let request = InvocationRequest {
            id: Uuid::new_v4(),
            original_input: "@performance + @security + @code-reviewer".to_string(),
            execution_plan: ExecutionPlan::Parallel(vec![
                create_invocation("performance", "analyze performance"),
                create_invocation("security", "scan for vulnerabilities"),
                create_invocation("code-reviewer", "review code quality"),
            ]),
            context: "Comprehensive code analysis".to_string(),
        };
        
        let start = std::time::Instant::now();
        let result = orchestrator.execute_plan(request).await.unwrap();
        let elapsed = start.elapsed();
        
        assert_eq!(result.executions.len(), 3);
        println!(
            "Parallel execution of {} agents completed in {:?}",
            result.executions.len(),
            elapsed
        );
        
        // Parallel execution should be faster than sequential
        // (in real scenarios with actual work)
    }
    
    /// Example: Mixed execution pattern (combine sequential and parallel)
    #[tokio::test]
    async fn example_mixed_execution() {
        let registry = Arc::new(SubagentRegistry::new());
        registry.register(create_test_agent("analyzer")).await.unwrap();
        registry.register(create_test_agent("refactorer")).await.unwrap();
        registry.register(create_test_agent("optimizer")).await.unwrap();
        registry.register(create_test_agent("test-writer")).await.unwrap();
        registry.register(create_test_agent("docs")).await.unwrap();
        
        let orchestrator = AgentOrchestrator::new(
            registry,
            OrchestratorConfig::default(),
            OperatingMode::Build,
        );
        
        // Pattern: analyzer → (refactorer + optimizer) → (test-writer + docs)
        let request = InvocationRequest {
            id: Uuid::new_v4(),
            original_input: "@analyzer → @refactorer + @optimizer → @test-writer + @docs".to_string(),
            execution_plan: ExecutionPlan::Mixed(vec![
                // Step 1: Analyze code
                ExecutionStep::Single(create_invocation("analyzer", "analyze codebase")),
                ExecutionStep::Barrier, // Wait for analysis to complete
                
                // Step 2: Refactor and optimize in parallel
                ExecutionStep::Parallel(vec![
                    create_invocation("refactorer", "refactor based on analysis"),
                    create_invocation("optimizer", "optimize performance"),
                ]),
                ExecutionStep::Barrier, // Wait for both to complete
                
                // Step 3: Update tests and docs in parallel
                ExecutionStep::Parallel(vec![
                    create_invocation("test-writer", "update tests"),
                    create_invocation("docs", "update documentation"),
                ]),
            ]),
            context: "Complex workflow with dependencies".to_string(),
        };
        
        let result = orchestrator.execute_plan(request).await.unwrap();
        
        assert_eq!(result.executions.len(), 5);
        println!("Mixed execution pattern completed with {} agents", result.executions.len());
    }
    
    /// Example: Using shared context for data passing
    #[tokio::test]
    async fn example_shared_context() {
        let registry = Arc::new(SubagentRegistry::new());
        registry.register(create_test_agent("scanner")).await.unwrap();
        registry.register(create_test_agent("fixer")).await.unwrap();
        
        let orchestrator = AgentOrchestrator::new(
            registry,
            OrchestratorConfig::default(),
            OperatingMode::Build,
        );
        
        // Create shared context with initial data
        let context = SharedContext::new();
        context.set("target_files".to_string(), serde_json::json!([
            "src/main.rs",
            "src/lib.rs"
        ])).await;
        
        // First agent: scan for issues
        let scan_result = orchestrator.execute_single(
            create_invocation("scanner", "scan for issues"),
            &context,
        ).await.unwrap();
        
        // Store scan results in context
        context.set("issues_found".to_string(), serde_json::json!({
            "count": 5,
            "severity": "medium"
        })).await;
        
        // Second agent: fix issues based on scan results
        let fix_result = orchestrator.execute_single(
            create_invocation("fixer", "fix identified issues"),
            &context,
        ).await.unwrap();
        
        // Check shared data
        let issues = context.get("issues_found").await.unwrap();
        println!("Shared context: {:?}", issues);
        
        assert_eq!(scan_result.status, SubagentStatus::Completed);
        assert_eq!(fix_result.status, SubagentStatus::Completed);
    }
    
    /// Example: Error handling and retries
    #[tokio::test]
    async fn example_error_handling() {
        let registry = Arc::new(SubagentRegistry::new());
        registry.register(create_test_agent("unreliable-agent")).await.unwrap();
        
        let orchestrator = AgentOrchestrator::new(
            registry,
            OrchestratorConfig {
                enable_retries: true,
                max_retries: 3,
                retry_backoff: Duration::from_millis(100),
                enable_circuit_breaker: true,
                circuit_breaker_threshold: 5,
                ..Default::default()
            },
            OperatingMode::Build,
        );
        
        let request = InvocationRequest {
            id: Uuid::new_v4(),
            original_input: "@unreliable-agent process data".to_string(),
            execution_plan: ExecutionPlan::Single(
                create_invocation("unreliable-agent", "process data")
            ),
            context: "Test error handling".to_string(),
        };
        
        // Execute with automatic retries
        let result = orchestrator.execute_plan(request).await;
        
        match result {
            Ok(res) => {
                println!("Execution succeeded after retries");
                assert!(res.success || res.partial_success);
            }
            Err(e) => {
                println!("Execution failed after retries: {}", e);
                // Circuit breaker should be open after repeated failures
            }
        }
    }
    
    /// Example: Progress tracking
    #[tokio::test]
    async fn example_progress_tracking() {
        let registry = Arc::new(SubagentRegistry::new());
        registry.register(create_test_agent("long-running")).await.unwrap();
        
        let orchestrator = AgentOrchestrator::new(
            registry,
            OrchestratorConfig::default(),
            OperatingMode::Build,
        );
        
        // Spawn a task to track progress
        let progress_task = tokio::spawn(async move {
            let mut rx = orchestrator.progress_receiver().await;
            
            while let Some(update) = rx.recv().await {
                println!(
                    "[{}] {}: {:?} - {:?}",
                    update.agent_name,
                    update.progress_percentage.unwrap_or(0),
                    update.status,
                    update.message
                );
            }
        });
        
        let request = InvocationRequest {
            id: Uuid::new_v4(),
            original_input: "@long-running heavy computation".to_string(),
            execution_plan: ExecutionPlan::Single(
                create_invocation("long-running", "heavy computation")
            ),
            context: "Track progress".to_string(),
        };
        
        let result = orchestrator.execute_plan(request).await.unwrap();
        assert!(result.success);
        
        // Clean up progress task
        progress_task.abort();
    }
    
    /// Example: Cancellation
    #[tokio::test]
    async fn example_cancellation() {
        let registry = Arc::new(SubagentRegistry::new());
        registry.register(create_test_agent("slow-agent")).await.unwrap();
        
        let orchestrator = Arc::new(AgentOrchestrator::new(
            registry,
            OrchestratorConfig {
                agent_timeout: Duration::from_secs(10),
                ..Default::default()
            },
            OperatingMode::Build,
        ));
        
        let orchestrator_clone = orchestrator.clone();
        
        // Start execution in background
        let execution_task = tokio::spawn(async move {
            let request = InvocationRequest {
                id: Uuid::new_v4(),
                original_input: "@slow-agent process large dataset".to_string(),
                execution_plan: ExecutionPlan::Single(
                    create_invocation("slow-agent", "process large dataset")
                ),
                context: "Test cancellation".to_string(),
            };
            
            orchestrator_clone.execute_plan(request).await
        });
        
        // Cancel after a short delay
        tokio::time::sleep(Duration::from_millis(50)).await;
        orchestrator.cancel();
        
        // Execution should fail due to cancellation
        let result = execution_task.await.unwrap();
        assert!(result.is_err());
        
        // Reset for future use
        orchestrator.reset_cancellation();
    }
    
    /// Example: Conditional execution
    #[tokio::test]
    async fn example_conditional_execution() {
        let registry = Arc::new(SubagentRegistry::new());
        registry.register(create_test_agent("validator")).await.unwrap();
        registry.register(create_test_agent("processor")).await.unwrap();
        
        let orchestrator = AgentOrchestrator::new(
            registry,
            OrchestratorConfig::default(),
            OperatingMode::Build,
        );
        
        let context = SharedContext::new();
        
        // First, validate
        let validation = orchestrator.execute_single(
            create_invocation("validator", "validate input"),
            &context,
        ).await.unwrap();
        
        // Store validation result
        context.set("is_valid".to_string(), serde_json::json!(true)).await;
        
        // Conditionally execute processor
        let processor_result = orchestrator.execute_conditional(
            create_invocation("processor", "process if valid"),
            &context,
            |ctx| Box::pin(async move {
                ctx.get("is_valid").await
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false)
            }),
        ).await.unwrap();
        
        assert!(processor_result.is_some());
        println!("Conditional execution: processor ran because validation passed");
    }
    
    // Helper functions
    
    fn create_test_agent(name: &str) -> SubagentConfig {
        SubagentConfig {
            name: name.to_string(),
            description: format!("Test agent: {}", name),
            mode_override: None,
            tools: vec![],
            intelligence: crate::subagents::config::IntelligenceLevel::Medium,
            prompt: format!("You are the {} agent", name),
            timeout_seconds: Some(30),
            max_retries: Some(2),
        }
    }
    
    fn create_invocation(name: &str, params: &str) -> AgentInvocation {
        AgentInvocation {
            agent_name: name.to_string(),
            parameters: HashMap::from([
                ("task".to_string(), params.to_string()),
            ]),
            raw_parameters: params.to_string(),
            position: 0,
        }
    }
}