agentd 0.1.2

Agent daemon for secure capability execution with pluggable isolation backends
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
//! Comprehensive error handling and edge case tests for the executor
//! 
//! This module contains tests for error scenarios, edge cases, and
//! recovery mechanisms throughout the executor system.

use anyhow::Result;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::{Write, Error as IoError, ErrorKind};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
use tempfile::{tempdir, TempDir};
use tracing::{debug, warn};
use uuid::Uuid;

/// Error handling test environment
pub struct ErrorHandlingTestEnvironment {
    pub workdir: TempDir,
    pub invalid_policy_path: PathBuf,
    pub corrupted_file_path: PathBuf,
    pub permission_denied_path: PathBuf,
    pub non_existent_path: PathBuf,
    pub circular_symlink_path: PathBuf,
    pub error_counters: Arc<Mutex<HashMap<String, u32>>>,
}

impl ErrorHandlingTestEnvironment {
    /// Create a comprehensive test environment for error handling testing
    pub fn new() -> Result<Self> {
        let workdir = tempdir()?;
        
        // Create invalid policy file
        let invalid_policy_path = workdir.path().join("invalid_policy.json");
        File::create(&invalid_policy_path)?.write_all(b"invalid json content {")?;
        
        // Create corrupted file
        let corrupted_file_path = workdir.path().join("corrupted.bin");
        let mut corrupted_file = File::create(&corrupted_file_path)?;
        corrupted_file.write_all(&[0xFF, 0xFE, 0xFD])?; // Invalid UTF-8
        
        // Create file with restricted permissions (if possible)
        let permission_denied_path = workdir.path().join("restricted.txt");
        File::create(&permission_denied_path)?.write_all(b"restricted content")?;
        
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = fs::metadata(&permission_denied_path)?.permissions();
            perms.set_mode(0o000); // No permissions
            fs::set_permissions(&permission_denied_path, perms)?;
        }
        
        // Non-existent path
        let non_existent_path = workdir.path().join("does_not_exist.txt");
        
        // Create circular symlink (if supported)
        let circular_symlink_path = workdir.path().join("circular_link");
        let _ = std::os::unix::fs::symlink(&circular_symlink_path, &circular_symlink_path);
        
        let error_counters = Arc::new(Mutex::new(HashMap::new()));
        
        Ok(Self {
            workdir,
            invalid_policy_path,
            corrupted_file_path,
            permission_denied_path,
            non_existent_path,
            circular_symlink_path,
            error_counters,
        })
    }
    
    /// Increment error counter for tracking
    pub fn increment_error(&self, error_type: &str) {
        let mut counters = self.error_counters.lock().unwrap();
        *counters.entry(error_type.to_string()).or_insert(0) += 1;
    }
    
    /// Get error count for a specific error type
    pub fn get_error_count(&self, error_type: &str) -> u32 {
        let counters = self.error_counters.lock().unwrap();
        counters.get(error_type).copied().unwrap_or(0)
    }
}

/// Error recovery strategy enumeration
#[derive(Debug, Clone, PartialEq)]
pub enum ErrorRecoveryStrategy {
    Retry,
    Fallback,
    Fail,
    Skip,
    Quarantine,
}

/// Error severity levels
#[derive(Debug, Clone, PartialEq, Ord, PartialOrd, Eq)]
pub enum ErrorSeverity {
    Low,
    Medium,
    High,
    Critical,
}

/// Comprehensive error classification system
#[derive(Debug, Clone)]
pub struct ExecutorError {
    pub error_type: String,
    pub severity: ErrorSeverity,
    pub recoverable: bool,
    pub recovery_strategy: ErrorRecoveryStrategy,
    pub message: String,
    pub context: HashMap<String, String>,
}

impl ExecutorError {
    pub fn new(error_type: &str, severity: ErrorSeverity, message: &str) -> Self {
        let recoverable = matches!(severity, ErrorSeverity::Low | ErrorSeverity::Medium);
        let recovery_strategy = match severity {
            ErrorSeverity::Low => ErrorRecoveryStrategy::Retry,
            ErrorSeverity::Medium => ErrorRecoveryStrategy::Fallback,
            ErrorSeverity::High => ErrorRecoveryStrategy::Quarantine,
            ErrorSeverity::Critical => ErrorRecoveryStrategy::Fail,
        };
        
        Self {
            error_type: error_type.to_string(),
            severity,
            recoverable,
            recovery_strategy,
            message: message.to_string(),
            context: HashMap::new(),
        }
    }
    
    pub fn with_context(mut self, key: &str, value: &str) -> Self {
        self.context.insert(key.to_string(), value.to_string());
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    
    #[test]
    fn test_error_environment_setup() {
        let env = ErrorHandlingTestEnvironment::new().unwrap();
        
        // Verify error environment is properly set up
        assert!(env.workdir.path().exists());
        assert!(env.invalid_policy_path.exists());
        assert!(env.corrupted_file_path.exists());
        assert!(env.permission_denied_path.exists());
        assert!(!env.non_existent_path.exists());
    }
    
    #[test]
    fn test_file_system_error_handling() {
        let env = ErrorHandlingTestEnvironment::new().unwrap();
        
        // Test non-existent file error
        let result = fs::read_to_string(&env.non_existent_path);
        assert!(result.is_err());
        match result.unwrap_err().kind() {
            ErrorKind::NotFound => {
                env.increment_error("file_not_found");
            }
            _ => panic!("Expected NotFound error"),
        }
        
        // Test permission denied error (may not work in all test environments)
        let result = fs::read_to_string(&env.permission_denied_path);
        if result.is_err() {
            match result.unwrap_err().kind() {
                ErrorKind::PermissionDenied => {
                    env.increment_error("permission_denied");
                }
                _ => {
                    debug!("Permission test skipped - may not work in this environment");
                }
            }
        }
        
        // Test corrupted file handling
        let result = fs::read_to_string(&env.corrupted_file_path);
        match result {
            Ok(content) => {
                // File read successfully, but content may be invalid UTF-8
                debug!("Corrupted file content: {:?}", content);
            }
            Err(e) => {
                debug!("Expected error reading corrupted file: {}", e);
                env.increment_error("corrupted_file");
            }
        }
        
        // Verify error counting works
        assert!(env.get_error_count("file_not_found") > 0);
    }
    
    #[test]
    fn test_json_parsing_error_handling() {
        let env = ErrorHandlingTestEnvironment::new().unwrap();
        
        // Test invalid JSON parsing
        let invalid_json_cases = vec![
            "invalid json content {",
            "{\"key\": }",
            "{\"key\": \"value\",}",
            "null null null",
            "\"unclosed string",
            "{\"nested\": {\"deeply\": {\"malformed\": }}}",
        ];
        
        for (i, invalid_json) in invalid_json_cases.iter().enumerate() {
            let result: Result<Value, _> = serde_json::from_str(invalid_json);
            assert!(result.is_err(), "Case {}: Should fail to parse: {}", i, invalid_json);
            env.increment_error("json_parse_error");
        }
        
        // Test JSON with unexpected structure
        let unexpected_structures = vec![
            json!(123), // Number instead of object
            json!("string"), // String instead of object
            json!([1, 2, 3]), // Array instead of object
            json!(null), // Null instead of object
        ];
        
        for (i, unexpected) in unexpected_structures.iter().enumerate() {
            let result = validate_json_structure(unexpected);
            assert!(result.is_err(), "Case {}: Should fail structure validation", i);
            env.increment_error("json_structure_error");
        }
        
        assert!(env.get_error_count("json_parse_error") > 0);
        assert!(env.get_error_count("json_structure_error") > 0);
    }
    
    #[test]
    fn test_intent_validation_error_handling() {
        let env = ErrorHandlingTestEnvironment::new().unwrap();
        
        // Test malformed intents
        let malformed_intents = vec![
            json!({}), // Empty object
            json!({"id": "invalid"}), // Missing capability
            json!({"capability": "fs.read.v1"}), // Missing ID
            json!({"id": 123, "capability": "fs.read.v1"}), // Wrong ID type
            json!({"id": Uuid::new_v4().to_string(), "capability": 123}), // Wrong capability type
            json!({"id": Uuid::new_v4().to_string(), "capability": "unknown.capability.v1"}), // Unknown capability
            json!({"id": Uuid::new_v4().to_string(), "capability": "fs.read.v1", "params": "not_object"}), // Wrong params type
        ];
        
        for (i, malformed_intent) in malformed_intents.iter().enumerate() {
            let result = validate_intent_structure(malformed_intent);
            assert!(result.is_err(), "Case {}: Should fail intent validation: {:?}", i, malformed_intent);
            env.increment_error("intent_validation_error");
        }
        
        // Test intents with invalid parameters
        let invalid_param_intents = vec![
            json!({
                "id": Uuid::new_v4().to_string(),
                "capability": "fs.read.v1",
                "params": {"path": "", "max_size": 1024} // Empty path
            }),
            json!({
                "id": Uuid::new_v4().to_string(),
                "capability": "fs.read.v1",
                "params": {"path": "/../../../etc/passwd", "max_size": 1024} // Path traversal
            }),
            json!({
                "id": Uuid::new_v4().to_string(),
                "capability": "http.fetch.v1",
                "params": {"url": "not-a-url", "method": "GET"} // Invalid URL
            }),
            json!({
                "id": Uuid::new_v4().to_string(),
                "capability": "http.fetch.v1",
                "params": {"url": "https://example.com", "method": "INVALID"} // Invalid method
            }),
        ];
        
        for (i, invalid_intent) in invalid_param_intents.iter().enumerate() {
            let result = validate_intent_parameters(invalid_intent);
            assert!(result.is_err(), "Case {}: Should fail parameter validation: {:?}", i, invalid_intent);
            env.increment_error("parameter_validation_error");
        }
        
        assert!(env.get_error_count("intent_validation_error") > 0);
        assert!(env.get_error_count("parameter_validation_error") > 0);
    }
    
    #[test]
    fn test_resource_exhaustion_handling() {
        let env = ErrorHandlingTestEnvironment::new().unwrap();
        
        // Test memory exhaustion simulation
        let large_requests = vec![
            json!({
                "id": Uuid::new_v4().to_string(),
                "capability": "fs.read.v1",
                "params": {"path": "/tmp/test.txt", "max_size": 1_000_000_000} // 1GB
            }),
            json!({
                "id": Uuid::new_v4().to_string(),
                "capability": "http.fetch.v1",
                "params": {
                    "url": "https://example.com",
                    "body": "x".repeat(10_000_000) // 10MB body
                }
            }),
        ];
        
        for (i, large_request) in large_requests.iter().enumerate() {
            let result = validate_resource_limits(large_request);
            assert!(result.is_err(), "Case {}: Should fail resource limit check: {:?}", i, large_request);
            env.increment_error("resource_limit_exceeded");
        }
        
        // Test concurrent request handling
        let mut handles = vec![];
        
        for i in 0..10 {
            let env = env.error_counters.clone();
            let handle = thread::spawn(move || {
                let intent = json!({
                    "id": Uuid::new_v4().to_string(),
                    "capability": "fs.read.v1",
                    "params": {"path": format!("/tmp/test_{}.txt", i)}
                });
                
                if validate_intent_structure(&intent).is_ok() {
                    // Simulate processing
                    thread::sleep(Duration::from_millis(10));
                    "success"
                } else {
                    let mut counters = env.lock().unwrap();
                    *counters.entry("concurrent_validation_error".to_string()).or_insert(0) += 1;
                    "error"
                }
            });
            handles.push(handle);
        }
        
        let mut success_count = 0;
        for handle in handles {
            match handle.join().unwrap() {
                "success" => success_count += 1,
                "error" => {}
                _ => {}
            }
        }
        
        assert!(success_count > 0, "At least some concurrent requests should succeed");
        assert!(env.get_error_count("resource_limit_exceeded") > 0);
    }
    
    #[test]
    fn test_timeout_handling() {
        let env = ErrorHandlingTestEnvironment::new().unwrap();
        
        // Test timeout scenarios
        let timeout_operations = vec![
            ("quick_operation", Duration::from_millis(10), Duration::from_millis(100), true),
            ("slow_operation", Duration::from_millis(200), Duration::from_millis(50), false),
            ("very_slow_operation", Duration::from_millis(1000), Duration::from_millis(100), false),
        ];
        
        for (name, operation_time, timeout, should_succeed) in timeout_operations {
            let start = Instant::now();
            let result = simulate_operation_with_timeout(operation_time, timeout);
            let elapsed = start.elapsed();
            
            if should_succeed {
                assert!(result.is_ok(), "Operation {} should succeed", name);
                assert!(elapsed >= operation_time, "Should take at least operation time");
            } else {
                assert!(result.is_err(), "Operation {} should timeout", name);
                assert!(elapsed <= timeout + Duration::from_millis(50), "Should timeout quickly");
                env.increment_error("timeout_error");
            }
        }
        
        assert!(env.get_error_count("timeout_error") > 0);
    }
    
    #[test]
    fn test_error_recovery_strategies() {
        let env = ErrorHandlingTestEnvironment::new().unwrap();
        
        // Test different error types and their recovery strategies
        let error_scenarios = vec![
            ("network_timeout", ErrorSeverity::Medium, ErrorRecoveryStrategy::Retry),
            ("file_not_found", ErrorSeverity::High, ErrorRecoveryStrategy::Quarantine),
            ("memory_exhausted", ErrorSeverity::Critical, ErrorRecoveryStrategy::Fail),
            ("invalid_parameter", ErrorSeverity::Low, ErrorRecoveryStrategy::Retry),
            ("security_violation", ErrorSeverity::Critical, ErrorRecoveryStrategy::Fail),
        ];
        
        for (error_type, severity, expected_strategy) in error_scenarios {
            let error = ExecutorError::new(error_type, severity, "Test error")
                .with_context("test_id", "test_123")
                .with_context("capability", "fs.read.v1");
            
            assert_eq!(error.recovery_strategy, expected_strategy);
            assert_eq!(error.error_type, error_type);
            assert!(error.context.contains_key("test_id"));
            assert!(error.context.contains_key("capability"));
            
            env.increment_error(error_type);
        }
        
        // Verify all error types were tracked
        assert!(env.get_error_count("network_timeout") > 0);
        assert!(env.get_error_count("memory_exhausted") > 0);
        assert!(env.get_error_count("security_violation") > 0);
    }
    
    #[test]
    fn test_circular_dependency_detection() {
        let env = ErrorHandlingTestEnvironment::new().unwrap();
        
        // Test circular symlink detection
        if env.circular_symlink_path.exists() {
            let result = detect_circular_reference(&env.circular_symlink_path, 10);
            assert!(result.is_err(), "Should detect circular symlink");
            env.increment_error("circular_reference");
        }
        
        // Test circular intent dependencies (simulated)
        let circular_intents = json!({
            "intent_a": {"depends_on": ["intent_b"]},
            "intent_b": {"depends_on": ["intent_c"]},
            "intent_c": {"depends_on": ["intent_a"]} // Circular dependency
        });
        
        let result = detect_intent_circular_dependencies(&circular_intents);
        assert!(result.is_err(), "Should detect circular intent dependencies");
        env.increment_error("circular_dependency");
        
        // Test valid dependency chain
        let valid_intents = json!({
            "intent_a": {"depends_on": ["intent_b"]},
            "intent_b": {"depends_on": ["intent_c"]},
            "intent_c": {"depends_on": []}
        });
        
        let result = detect_intent_circular_dependencies(&valid_intents);
        assert!(result.is_ok(), "Should allow valid dependency chain");
    }
    
    #[test]
    fn test_error_aggregation_and_reporting() {
        let env = ErrorHandlingTestEnvironment::new().unwrap();
        
        // Generate various errors
        for i in 0..5 {
            env.increment_error("validation_error");
            if i % 2 == 0 {
                env.increment_error("timeout_error");
            }
            if i % 3 == 0 {
                env.increment_error("permission_error");
            }
        }
        
        // Test error aggregation
        let error_summary = get_error_summary(&env);
        assert!(error_summary.contains_key("validation_error"));
        assert!(error_summary.contains_key("timeout_error"));
        assert!(error_summary.contains_key("permission_error"));
        
        assert_eq!(error_summary["validation_error"], 5);
        assert_eq!(error_summary["timeout_error"], 3); // 0, 2, 4
        assert_eq!(error_summary["permission_error"], 2); // 0, 3
        
        // Test error report generation
        let report = generate_error_report(&env);
        assert!(report.contains("validation_error"));
        assert!(report.contains("timeout_error"));
        assert!(report.contains("permission_error"));
        assert!(report.contains("Total errors"));
    }
}

/// Helper functions for testing
#[cfg(test)]
mod test_helpers {
    use super::*;
    
    pub fn validate_json_structure(value: &Value) -> Result<()> {
        // Expect object with specific structure
        let obj = value.as_object()
            .ok_or_else(|| anyhow::anyhow!("Expected JSON object"))?;
        
        if obj.is_empty() {
            return Err(anyhow::anyhow!("Object cannot be empty"));
        }
        
        Ok(())
    }
    
    pub fn validate_intent_structure(intent: &Value) -> Result<()> {
        let obj = intent.as_object()
            .ok_or_else(|| anyhow::anyhow!("Intent must be an object"))?;
        
        // Check required fields
        let id = obj.get("id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing or invalid 'id' field"))?;
        
        // Validate UUID format
        Uuid::parse_str(id)
            .map_err(|_| anyhow::anyhow!("Invalid UUID format in 'id' field"))?;
        
        let capability = obj.get("capability")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing or invalid 'capability' field"))?;
        
        // Validate capability format
        if !capability.contains('.') || !capability.ends_with(".v1") {
            return Err(anyhow::anyhow!("Invalid capability format"));
        }
        
        // Known capabilities
        let known_capabilities = vec![
            "fs.read.v1", "fs.write.v1", "http.fetch.v1", 
            "sqlite.query.v1", "archive.read.v1"
        ];
        
        if !known_capabilities.contains(&capability) {
            return Err(anyhow::anyhow!("Unknown capability: {}", capability));
        }
        
        Ok(())
    }
    
    pub fn validate_intent_parameters(intent: &Value) -> Result<()> {
        let capability = intent["capability"].as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing capability"))?;
        let params = intent.get("params")
            .ok_or_else(|| anyhow::anyhow!("Missing params"))?;
        
        match capability {
            "fs.read.v1" => validate_fs_read_params(params),
            "http.fetch.v1" => validate_http_fetch_params(params),
            "sqlite.query.v1" => validate_sqlite_params(params),
            _ => Ok(()), // Other capabilities not validated in this test
        }
    }
    
    fn validate_fs_read_params(params: &Value) -> Result<()> {
        let path = params["path"].as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing path parameter"))?;
        
        if path.is_empty() {
            return Err(anyhow::anyhow!("Path cannot be empty"));
        }
        
        if path.contains("..") {
            return Err(anyhow::anyhow!("Path traversal detected"));
        }
        
        if let Some(max_size) = params.get("max_size") {
            let size = max_size.as_u64()
                .ok_or_else(|| anyhow::anyhow!("Invalid max_size type"))?;
            
            if size > 100_000_000 { // 100MB limit
                return Err(anyhow::anyhow!("max_size too large"));
            }
        }
        
        Ok(())
    }
    
    fn validate_http_fetch_params(params: &Value) -> Result<()> {
        let url_str = params["url"].as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing URL parameter"))?;
        
        let url = url::Url::parse(url_str)
            .map_err(|_| anyhow::anyhow!("Invalid URL format"))?;
        
        if url.scheme() != "https" && url.scheme() != "http" {
            return Err(anyhow::anyhow!("Only HTTP(S) URLs allowed"));
        }
        
        if let Some(method) = params.get("method") {
            let method_str = method.as_str()
                .ok_or_else(|| anyhow::anyhow!("Invalid method type"))?;
            
            let allowed_methods = vec!["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS"];
            if !allowed_methods.contains(&method_str) {
                return Err(anyhow::anyhow!("Invalid HTTP method"));
            }
        }
        
        Ok(())
    }
    
    fn validate_sqlite_params(params: &Value) -> Result<()> {
        let query = params["query"].as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing query parameter"))?;
        
        let query_upper = query.to_uppercase();
        let dangerous_keywords = vec!["DROP", "DELETE", "ALTER", "CREATE"];
        
        for keyword in dangerous_keywords {
            if query_upper.contains(keyword) {
                return Err(anyhow::anyhow!("Dangerous SQL operation detected"));
            }
        }
        
        Ok(())
    }
    
    pub fn validate_resource_limits(intent: &Value) -> Result<()> {
        let capability = intent["capability"].as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing capability"))?;
        let params = intent["params"].as_object()
            .ok_or_else(|| anyhow::anyhow!("Missing params"))?;
        
        match capability {
            "fs.read.v1" => {
                if let Some(max_size) = params.get("max_size") {
                    let size = max_size.as_u64()
                        .ok_or_else(|| anyhow::anyhow!("Invalid max_size type"))?;
                    
                    if size > 10_000_000 { // 10MB limit
                        return Err(anyhow::anyhow!("File size limit exceeded"));
                    }
                }
            }
            "http.fetch.v1" => {
                if let Some(body) = params.get("body") {
                    let body_str = serde_json::to_string(body)?;
                    if body_str.len() > 1_000_000 { // 1MB limit
                        return Err(anyhow::anyhow!("Request body size limit exceeded"));
                    }
                }
            }
            _ => {}
        }
        
        Ok(())
    }
    
    pub fn simulate_operation_with_timeout(operation_time: Duration, timeout: Duration) -> Result<()> {
        use std::sync::mpsc;
        
        let (tx, rx) = mpsc::channel();
        
        thread::spawn(move || {
            thread::sleep(operation_time);
            let _ = tx.send(());
        });
        
        match rx.recv_timeout(timeout) {
            Ok(()) => Ok(()),
            Err(mpsc::RecvTimeoutError::Timeout) => Err(anyhow::anyhow!("Operation timed out")),
            Err(mpsc::RecvTimeoutError::Disconnected) => Err(anyhow::anyhow!("Operation failed")),
        }
    }
    
    pub fn detect_circular_reference(path: &Path, max_depth: usize) -> Result<()> {
        let mut visited = std::collections::HashSet::new();
        check_circular_reference_recursive(path, &mut visited, 0, max_depth)
    }
    
    fn check_circular_reference_recursive(
        path: &Path, 
        visited: &mut std::collections::HashSet<PathBuf>, 
        depth: usize, 
        max_depth: usize
    ) -> Result<()> {
        if depth > max_depth {
            return Err(anyhow::anyhow!("Maximum recursion depth exceeded"));
        }
        
        let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
        
        if visited.contains(&canonical) {
            return Err(anyhow::anyhow!("Circular reference detected"));
        }
        
        visited.insert(canonical.clone());
        
        if canonical.is_symlink() {
            if let Ok(target) = fs::read_link(&canonical) {
                check_circular_reference_recursive(&target, visited, depth + 1, max_depth)?;
            }
        }
        
        visited.remove(&canonical);
        Ok(())
    }
    
    pub fn detect_intent_circular_dependencies(intents: &Value) -> Result<()> {
        let obj = intents.as_object()
            .ok_or_else(|| anyhow::anyhow!("Intents must be an object"))?;
        
        for (intent_id, intent_data) in obj {
            let mut visited = std::collections::HashSet::new();
            check_intent_dependencies(intent_id, intent_data, obj, &mut visited)?;
        }
        
        Ok(())
    }
    
    fn check_intent_dependencies(
        intent_id: &str,
        intent_data: &Value,
        all_intents: &serde_json::Map<String, Value>,
        visited: &mut std::collections::HashSet<String>
    ) -> Result<()> {
        if visited.contains(intent_id) {
            return Err(anyhow::anyhow!("Circular dependency detected at: {}", intent_id));
        }
        
        visited.insert(intent_id.to_string());
        
        if let Some(dependencies) = intent_data.get("depends_on") {
            if let Some(deps_array) = dependencies.as_array() {
                for dep in deps_array {
                    if let Some(dep_id) = dep.as_str() {
                        if let Some(dep_data) = all_intents.get(dep_id) {
                            check_intent_dependencies(dep_id, dep_data, all_intents, visited)?;
                        }
                    }
                }
            }
        }
        
        visited.remove(intent_id);
        Ok(())
    }
    
    pub fn get_error_summary(env: &ErrorHandlingTestEnvironment) -> HashMap<String, u32> {
        env.error_counters.lock().unwrap().clone()
    }
    
    pub fn generate_error_report(env: &ErrorHandlingTestEnvironment) -> String {
        let counters = env.error_counters.lock().unwrap();
        let mut report = String::new();
        
        report.push_str("=== ERROR HANDLING TEST REPORT ===\n\n");
        
        let total_errors: u32 = counters.values().sum();
        report.push_str(&format!("Total errors encountered: {}\n\n", total_errors));
        
        let mut sorted_errors: Vec<_> = counters.iter().collect();
        sorted_errors.sort_by(|a, b| b.1.cmp(a.1)); // Sort by count, descending
        
        report.push_str("Error breakdown:\n");
        for (error_type, count) in sorted_errors {
            report.push_str(&format!("  {}: {} occurrences\n", error_type, count));
        }
        
        report.push_str("\n=== END REPORT ===\n");
        report
    }
}