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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! Basic tests for the subagent system
//!
//! This module provides foundational tests for the agent system including registry loading,
//! invocation parsing, basic orchestration, and error handling.

use super::*;
use crate::modes::OperatingMode;
use std::collections::HashMap;
use std::path::PathBuf;
use tempfile::TempDir;
use uuid::Uuid;

// Test fixtures and helpers
mod fixtures {
    use super::*;

    pub fn create_test_config() -> SubagentConfig {
        SubagentConfig {
            name: "test-agent".to_string(),
            description: "Test agent for unit testing".to_string(),
            mode_override: Some(OperatingMode::Review),
            intelligence: IntelligenceLevel::Medium,
            tools: {
                let mut tools = HashMap::new();
                tools.insert("ast_search".to_string(), ToolPermission::Allow);
                tools.insert("file_read".to_string(), ToolPermission::Allow);
                tools
            },
            prompt: "You are a test agent.".to_string(),
            parameters: vec![config::ParameterDefinition {
                name: "file".to_string(),
                description: "File to analyze".to_string(),
                required: false,
                default: Some("**/*.rs".to_string()),
                valid_values: None,
            }],
            template: None,
            timeout_seconds: 30,
            chainable: true,
            parallelizable: true,
            metadata: HashMap::new(),
            file_patterns: vec!["*.rs".to_string()],
            tags: vec!["test".to_string()],
        }
    }

    pub fn create_test_registry() -> (SubagentRegistry, TempDir) {
        let temp_dir = TempDir::new().unwrap();
        // Create a temporary HOME directory structure
        let home_dir = temp_dir.path().join(".agcodex");
        std::fs::create_dir_all(&home_dir).unwrap();

        // Set HOME environment variable temporarily for this test
        unsafe {
            std::env::set_var("HOME", temp_dir.path());
        }

        // Use the public constructor
        let registry = SubagentRegistry::new().unwrap();

        (registry, temp_dir)
    }
}

// Registry tests
#[cfg(test)]
mod registry_tests {
    use super::fixtures::*;
    use super::*;

    #[test]
    fn test_registry_creation() {
        let (registry, _temp_dir) = create_test_registry();
        // Just verify the registry was created and is empty
        assert_eq!(registry.get_all_agents().len(), 0);
        assert_eq!(registry.get_all_templates().len(), 0);
    }

    #[test]
    fn test_agent_loading() {
        let (registry, temp_dir) = create_test_registry();

        // Create test agent configuration
        let config = create_test_config();
        // Calculate the path based on how SubagentRegistry sets it up
        let global_agents_dir = temp_dir
            .path()
            .join(".agcodex")
            .join("agents")
            .join("global");
        std::fs::create_dir_all(&global_agents_dir).unwrap();
        let config_path = global_agents_dir.join("test-agent.toml");
        config.to_file(&config_path).unwrap();

        // Load agents
        registry.load_all().unwrap();

        // Verify agent was loaded
        let loaded_agent = registry.get_agent("test-agent").unwrap();
        assert_eq!(loaded_agent.config.name, "test-agent");
        assert_eq!(
            loaded_agent.config.description,
            "Test agent for unit testing"
        );
        assert_eq!(
            loaded_agent.config.mode_override,
            Some(OperatingMode::Review)
        );
        assert!(loaded_agent.is_global);
        assert!(loaded_agent.config.tools.contains_key("ast_search"));
    }

    #[test]
    fn test_agent_filtering() {
        let (registry, temp_dir) = create_test_registry();

        // Create multiple test agents with different patterns and tags
        let configs = vec![
            {
                let mut config = create_test_config();
                config.name = "rust-agent".to_string();
                config.file_patterns = vec!["*.rs".to_string()];
                config.tags = vec!["rust".to_string(), "backend".to_string()];
                config
            },
            {
                let mut config = create_test_config();
                config.name = "js-agent".to_string();
                config.file_patterns = vec!["*.js".to_string(), "*.ts".to_string()];
                config.tags = vec!["javascript".to_string(), "frontend".to_string()];
                config
            },
        ];

        let global_agents_dir = temp_dir
            .path()
            .join(".agcodex")
            .join("agents")
            .join("global");
        std::fs::create_dir_all(&global_agents_dir).unwrap();
        for config in configs {
            let config_path = global_agents_dir.join(format!("{}.toml", config.name));
            config.to_file(&config_path).unwrap();
        }

        registry.load_all().unwrap();

        // Test file pattern filtering
        let rust_file = PathBuf::from("src/main.rs");
        let rust_agents = registry.get_agents_for_file(&rust_file);
        let rust_names: Vec<_> = rust_agents.iter().map(|a| &a.config.name).collect();
        assert!(rust_names.contains(&&"rust-agent".to_string()));

        // Test tag filtering
        let backend_agents = registry.get_agents_with_tags(&["backend".to_string()]);
        assert_eq!(backend_agents.len(), 1);
        assert_eq!(backend_agents[0].config.name, "rust-agent");
    }

    #[test]
    fn test_agent_name_conflict_detection() {
        let (registry, temp_dir) = create_test_registry();

        // Create two agents with the same name
        let config1 = create_test_config();
        let config2 = create_test_config();

        let global_agents_dir = temp_dir
            .path()
            .join(".agcodex")
            .join("agents")
            .join("global");
        std::fs::create_dir_all(&global_agents_dir).unwrap();
        let config_path1 = global_agents_dir.join("test-agent-1.toml");
        let config_path2 = global_agents_dir.join("test-agent-2.toml");

        config1.to_file(&config_path1).unwrap();
        config2.to_file(&config_path2).unwrap();

        // Loading should fail due to name conflict
        let result = registry.load_all();
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            registry::SubagentRegistryError::NameConflict { .. }
        ));
    }
}

// Invocation parser tests
#[cfg(test)]
mod invocation_tests {
    use super::invocation::*;
    use super::*;

    #[test]
    fn test_single_agent_parsing() {
        let parser = InvocationParser::new();

        // Simple invocation
        let result = parser.parse("@code-reviewer").unwrap().unwrap();
        match result.execution_plan {
            ExecutionPlan::Single(inv) => {
                assert_eq!(inv.agent_name, "code-reviewer");
                assert_eq!(inv.raw_parameters, "");
            }
            _ => panic!("Expected single execution plan"),
        }

        // With parameters
        let result = parser
            .parse("@code-reviewer check this file")
            .unwrap()
            .unwrap();
        match result.execution_plan {
            ExecutionPlan::Single(inv) => {
                assert_eq!(inv.agent_name, "code-reviewer");
                assert_eq!(inv.raw_parameters, "check this file");
            }
            _ => panic!("Expected single execution plan"),
        }
    }

    #[test]
    fn test_sequential_chain_parsing() {
        let parser = InvocationParser::new();

        // Simple chain
        let result = parser.parse("@refactorer → @test-writer").unwrap().unwrap();
        match result.execution_plan {
            ExecutionPlan::Sequential(chain) => {
                assert_eq!(chain.agents.len(), 2);
                assert_eq!(chain.agents[0].agent_name, "refactorer");
                assert_eq!(chain.agents[1].agent_name, "test-writer");
                assert!(chain.pass_output);
            }
            _ => panic!("Expected sequential execution plan"),
        }
    }

    #[test]
    fn test_parallel_execution_parsing() {
        let parser = InvocationParser::new();

        // Simple parallel
        let result = parser.parse("@performance + @security").unwrap().unwrap();
        match result.execution_plan {
            ExecutionPlan::Parallel(agents) => {
                assert_eq!(agents.len(), 2);
                assert_eq!(agents[0].agent_name, "performance");
                assert_eq!(agents[1].agent_name, "security");
            }
            _ => panic!("Expected parallel execution plan"),
        }
    }

    #[test]
    fn test_parameter_parsing_via_parse() {
        let parser = InvocationParser::new();

        // Test parameters through the public parse API
        let result = parser
            .parse("@test-agent file=src/main.rs level=high")
            .unwrap()
            .unwrap();

        // Extract the agent invocation from the execution plan
        match result.execution_plan {
            ExecutionPlan::Single(inv) => {
                assert_eq!(inv.agent_name, "test-agent");
                assert_eq!(inv.parameters.get("file").unwrap(), "src/main.rs");
                assert_eq!(inv.parameters.get("level").unwrap(), "high");
            }
            _ => panic!("Expected single execution plan"),
        }

        // Test positional arguments through parse API
        let result = parser
            .parse("@test-agent src/main.rs high")
            .unwrap()
            .unwrap();

        match result.execution_plan {
            ExecutionPlan::Single(inv) => {
                assert_eq!(inv.agent_name, "test-agent");
                // The actual parameter parsing behavior may differ
                // Check if parameters contain the values
                let params_str = inv.raw_parameters;
                assert!(params_str.contains("src/main.rs"));
                assert!(params_str.contains("high"));
            }
            _ => panic!("Expected single execution plan"),
        }
    }

    #[test]
    fn test_context_extraction() {
        let parser = InvocationParser::new();
        let result = parser
            .parse("Please @code-reviewer this file and make sure it's secure.")
            .unwrap()
            .unwrap();

        // Context should have agent invocations removed
        assert!(result.context.contains("Please"));
        assert!(result.context.contains("secure"));
        assert!(!result.context.contains("@code-reviewer"));
    }

    #[test]
    fn test_no_agents() {
        let parser = InvocationParser::new();
        let result = parser
            .parse("This is just regular text with no agents.")
            .unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_circular_dependency_detection() {
        let parser = InvocationParser::new();
        let chain = AgentChain {
            agents: vec![
                AgentInvocation {
                    agent_name: "agent1".to_string(),
                    parameters: HashMap::new(),
                    raw_parameters: String::new(),
                    position: 0,
                    intelligence_override: None,
                    mode_override: None,
                },
                AgentInvocation {
                    agent_name: "agent1".to_string(), // Duplicate!
                    parameters: HashMap::new(),
                    raw_parameters: String::new(),
                    position: 1,
                    intelligence_override: None,
                    mode_override: None,
                },
            ],
            pass_output: true,
        };

        let plan = ExecutionPlan::Sequential(chain);
        let result = parser.validate_execution_plan(&plan);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            SubagentError::CircularDependency { .. }
        ));
    }
}

// Error handling tests
#[cfg(test)]
mod error_tests {
    use super::*;

    #[test]
    fn test_subagent_error_types() {
        // Test all error variants can be created and display properly
        let errors = vec![
            SubagentError::AgentNotFound {
                name: "missing-agent".to_string(),
            },
            SubagentError::InvalidConfig("Bad config".to_string()),
            SubagentError::ExecutionFailed("Agent crashed".to_string()),
            SubagentError::CircularDependency {
                chain: vec!["a".to_string(), "b".to_string(), "a".to_string()],
            },
            SubagentError::Timeout {
                name: "slow-agent".to_string(),
            },
            SubagentError::ToolPermissionDenied {
                tool: "write_file".to_string(),
                agent: "read-only-agent".to_string(),
            },
            SubagentError::ModeRestriction {
                mode: OperatingMode::Plan,
                operation: "file_write".to_string(),
            },
        ];

        for error in errors {
            let error_string = error.to_string();
            assert!(!error_string.is_empty());

            // Test that error implements required traits
            let _: Box<dyn std::error::Error> = Box::new(error);
        }
    }

    #[test]
    fn test_error_conversion() {
        // Test IO error conversion
        let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
        let subagent_error: SubagentError = io_error.into();
        assert!(matches!(subagent_error, SubagentError::Io(_)));
    }

    #[test]
    fn test_result_types() {
        // Test that our result types work correctly
        let success: SubagentResult<String> = Ok("success".to_string());
        assert!(success.is_ok());
        assert_eq!(success.unwrap(), "success");

        let failure: SubagentResult<String> = Err(SubagentError::AgentNotFound {
            name: "test".to_string(),
        });
        assert!(failure.is_err());

        // Test registry result types
        let reg_success: registry::RegistryResult<i32> = Ok(42);
        assert!(reg_success.is_ok());

        let reg_failure: registry::RegistryResult<i32> =
            Err(registry::SubagentRegistryError::ConfigNotFound {
                path: PathBuf::from("missing.toml"),
            });
        assert!(reg_failure.is_err());
    }
}

// Basic execution tests
#[cfg(test)]
mod execution_tests {
    use super::*;

    #[test]
    fn test_subagent_execution_lifecycle() {
        let mut execution = SubagentExecution::new("test-agent".to_string());

        assert_eq!(execution.status, SubagentStatus::Pending);
        assert_eq!(execution.agent_name, "test-agent");

        execution.start();
        assert_eq!(execution.status, SubagentStatus::Running);

        execution.complete("Success!".to_string(), vec![]);
        assert_eq!(execution.status, SubagentStatus::Completed);
        assert_eq!(execution.output.as_ref().unwrap(), "Success!");
        assert!(execution.duration().is_some());
    }

    #[test]
    fn test_subagent_execution_failure() {
        let mut execution = SubagentExecution::new("failing-agent".to_string());

        execution.start();
        execution.fail("Something went wrong".to_string());

        assert!(matches!(execution.status, SubagentStatus::Failed(_)));
        assert_eq!(execution.error.as_ref().unwrap(), "Something went wrong");
        assert!(execution.duration().is_some());
    }

    #[test]
    fn test_subagent_context_creation() {
        let context = SubagentContext {
            execution_id: Uuid::new_v4(),
            mode: OperatingMode::Build,
            available_tools: vec!["ast_search".to_string(), "file_read".to_string()],
            conversation_context: "Previous context".to_string(),
            working_directory: std::env::current_dir().unwrap(),
            parameters: {
                let mut params = HashMap::new();
                params.insert("file".to_string(), "src/main.rs".to_string());
                params
            },
            metadata: HashMap::new(),
        };

        assert_eq!(context.mode, OperatingMode::Build);
        assert_eq!(context.available_tools.len(), 2);
        assert!(context.available_tools.contains(&"ast_search".to_string()));
        assert!(context.conversation_context.contains("Previous context"));
        assert_eq!(context.parameters.get("file").unwrap(), "src/main.rs");
    }
}

// Integration tests
#[cfg(test)]
mod integration_tests {
    use super::fixtures::*;
    use super::*;

    #[test]
    fn test_end_to_end_agent_discovery() {
        // This test simulates discovering and preparing to execute an agent
        let (registry, temp_dir) = create_test_registry();

        // Create test agent
        let config = create_test_config();
        let global_agents_dir = temp_dir
            .path()
            .join(".agcodex")
            .join("agents")
            .join("global");
        std::fs::create_dir_all(&global_agents_dir).unwrap();
        let config_path = global_agents_dir.join("test-agent.toml");
        config.to_file(&config_path).unwrap();

        registry.load_all().unwrap();

        // Parse invocation
        let parser = InvocationParser::new();
        let invocation = parser
            .parse("@test-agent check src/main.rs")
            .unwrap()
            .unwrap();

        // Verify invocation
        match invocation.execution_plan {
            ExecutionPlan::Single(inv) => {
                assert_eq!(inv.agent_name, "test-agent");
                assert_eq!(inv.raw_parameters, "check src/main.rs");

                // Get agent configuration
                let agent_info = registry.get_agent(&inv.agent_name).unwrap();
                assert_eq!(agent_info.config.name, "test-agent");
                assert!(agent_info.config.tools.contains_key("ast_search"));

                // Create execution context
                let context = SubagentContext {
                    execution_id: Uuid::new_v4(),
                    mode: agent_info
                        .config
                        .mode_override
                        .unwrap_or(OperatingMode::Build),
                    available_tools: agent_info.config.tools.keys().cloned().collect(),
                    conversation_context: String::new(),
                    working_directory: std::env::current_dir().unwrap(),
                    parameters: inv.parameters,
                    metadata: HashMap::new(),
                };

                assert_eq!(context.mode, OperatingMode::Review);
                assert!(context.available_tools.contains(&"ast_search".to_string()));
            }
            _ => panic!("Expected single execution plan"),
        }
    }

    #[test]
    fn test_multi_agent_planning() {
        let (registry, temp_dir) = create_test_registry();

        // Create multiple agents
        let agents = vec!["code-reviewer", "refactorer", "test-writer"];
        let global_agents_dir = temp_dir
            .path()
            .join(".agcodex")
            .join("agents")
            .join("global");
        std::fs::create_dir_all(&global_agents_dir).unwrap();
        for agent_name in &agents {
            let mut config = create_test_config();
            config.name = (*agent_name).to_string();

            let config_path = global_agents_dir.join(format!("{}.toml", agent_name));
            config.to_file(&config_path).unwrap();
        }

        registry.load_all().unwrap();

        // Parse complex invocation
        let parser = InvocationParser::new();
        let invocation = parser
            .parse("@code-reviewer → @refactorer → @test-writer")
            .unwrap()
            .unwrap();

        match invocation.execution_plan {
            ExecutionPlan::Sequential(chain) => {
                assert_eq!(chain.agents.len(), 3);
                assert!(chain.pass_output);

                // Verify all agents exist in registry
                for agent_inv in &chain.agents {
                    let agent_info = registry.get_agent(&agent_inv.agent_name);
                    assert!(
                        agent_info.is_some(),
                        "Agent {} not found",
                        agent_inv.agent_name
                    );
                }

                // Verify execution order
                assert_eq!(chain.agents[0].agent_name, "code-reviewer");
                assert_eq!(chain.agents[1].agent_name, "refactorer");
                assert_eq!(chain.agents[2].agent_name, "test-writer");
            }
            _ => panic!("Expected sequential execution plan"),
        }
    }
}

// Performance tests
#[cfg(test)]
mod performance_tests {
    use super::fixtures::*;
    use super::*;

    #[test]
    fn test_large_registry_performance() {
        let (registry, temp_dir) = create_test_registry();

        // Create many agent configurations
        let start = std::time::Instant::now();
        let global_agents_dir = temp_dir
            .path()
            .join(".agcodex")
            .join("agents")
            .join("global");
        std::fs::create_dir_all(&global_agents_dir).unwrap();

        for i in 0..50 {
            let mut config = create_test_config();
            config.name = format!("agent-{}", i);

            let config_path = global_agents_dir.join(format!("agent-{}.toml", i));
            config.to_file(&config_path).unwrap();
        }

        // Load all agents
        let load_start = std::time::Instant::now();
        registry.load_all().unwrap();
        let load_duration = load_start.elapsed();

        // Should load 50 agents reasonably quickly (under 1 second)
        assert!(load_duration < std::time::Duration::from_secs(1));

        // Verify all agents were loaded
        assert_eq!(registry.get_all_agents().len(), 50);

        let total_duration = start.elapsed();
        println!("Total time for 50 agents: {:?}", total_duration);
        println!("Load time: {:?}", load_duration);
    }

    #[test]
    fn test_complex_invocation_parsing_performance() {
        let parser = InvocationParser::new();

        // Create a complex invocation string
        let complex_input = "@agent1 param1=value1 → @agent2 + @agent3 file=test.rs → @agent4";

        let start = std::time::Instant::now();

        // Parse the same complex input many times
        for _ in 0..100 {
            let result = parser.parse(complex_input).unwrap();
            assert!(result.is_some());
        }

        let duration = start.elapsed();

        // Should parse 100 complex invocations quickly (under 200ms)
        assert!(duration < std::time::Duration::from_millis(200));
        println!("Parsed 100 complex invocations in: {:?}", duration);
    }
}