rustchain-community 1.0.0

Open-source AI agent framework with core functionality and plugin system
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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
use anyhow::{anyhow, Result};
use chrono::{DateTime, Timelike, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tracing::{debug, info, warn};
use uuid::Uuid;

/// Enhanced policy engine with rule-based access control
pub struct EnhancedPolicyEngine {
    rules: HashMap<String, PolicyRule>,
    default_effect: PolicyEffect,
}

impl EnhancedPolicyEngine {
    pub fn new() -> Self {
        Self {
            rules: HashMap::new(),
            default_effect: PolicyEffect::Deny,
        }
    }

    pub fn add_rule(&mut self, rule: PolicyRule) -> Result<String> {
        let rule_id = rule.id.clone();

        // Check for conflicts
        for existing in self.rules.values() {
            if existing.priority == rule.priority
                && Self::rules_overlap(&existing.actions, &rule.actions)
            {
                warn!("Policy rule {} may conflict with {}", rule_id, existing.id);
            }
        }

        self.rules.insert(rule_id.clone(), rule);
        debug!("Added policy rule: {}", rule_id);

        Ok(rule_id)
    }

    pub fn remove_rule(&mut self, rule_id: &str) -> Result<()> {
        self.rules
            .remove(rule_id)
            .ok_or_else(|| anyhow!("Rule not found: {}", rule_id))?;
        Ok(())
    }

    pub fn evaluate_action(&self, action: &str, context: &PolicyContext) -> PolicyDecision {
        debug!(
            "Evaluating action: {} for agent: {}",
            action, context.agent_id
        );

        // Find all matching rules
        let mut matching_rules: Vec<&PolicyRule> = self
            .rules
            .values()
            .filter(|rule| rule.matches(action, context))
            .collect();

        // Sort by priority (higher priority first)
        matching_rules.sort_by(|a, b| b.priority.cmp(&a.priority));

        // Apply first matching rule
        if let Some(rule) = matching_rules.first() {
            let allowed = matches!(rule.effect, PolicyEffect::Allow);

            info!(
                "Action {} {} by rule {}",
                action,
                if allowed { "allowed" } else { "denied" },
                rule.id
            );

            PolicyDecision {
                allowed,
                rule_id: Some(rule.id.clone()),
                reason: rule.description.clone(),
            }
        } else {
            // No matching rule, use default effect
            let allowed = matches!(self.default_effect, PolicyEffect::Allow);

            PolicyDecision {
                allowed,
                rule_id: None,
                reason: format!("No matching rule, default: {:?}", self.default_effect),
            }
        }
    }

    pub fn list_rules(&self) -> Vec<(String, PolicyRule)> {
        self.rules
            .iter()
            .map(|(id, rule)| (id.clone(), rule.clone()))
            .collect()
    }

    pub fn get_rule(&self, rule_id: &str) -> Option<&PolicyRule> {
        self.rules.get(rule_id)
    }

    pub fn set_default_effect(&mut self, effect: PolicyEffect) {
        self.default_effect = effect;
    }

    fn rules_overlap(actions1: &[String], actions2: &[String]) -> bool {
        for a1 in actions1 {
            for a2 in actions2 {
                if a1 == a2 || a1 == "*" || a2 == "*" {
                    return true;
                }
                // Check for pattern overlap (e.g., "tool:*" overlaps with "tool:create_file")
                if (a1.ends_with("*") && a2.starts_with(a1.trim_end_matches('*')))
                    || (a2.ends_with("*") && a1.starts_with(a2.trim_end_matches('*')))
                {
                    return true;
                }
            }
        }
        false
    }
}

/// Policy rule definition
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PolicyRule {
    pub id: String,
    pub name: String,
    pub description: String,
    pub effect: PolicyEffect,
    pub priority: u32,
    pub actions: Vec<String>,
    pub conditions: Vec<PolicyCondition>,
}

impl PolicyRule {
    pub fn new(name: String, effect: PolicyEffect) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            name,
            description: String::new(),
            effect,
            priority: 100,
            actions: Vec::new(),
            conditions: Vec::new(),
        }
    }

    pub fn with_description(mut self, desc: String) -> Self {
        self.description = desc;
        self
    }

    pub fn with_priority(mut self, priority: u32) -> Self {
        self.priority = priority;
        self
    }

    pub fn with_actions(mut self, actions: Vec<String>) -> Self {
        self.actions = actions;
        self
    }

    pub fn with_condition(mut self, condition: PolicyCondition) -> Self {
        self.conditions.push(condition);
        self
    }

    pub fn matches(&self, action: &str, context: &PolicyContext) -> bool {
        // Check if action matches
        let action_matches = self.actions.iter().any(|a| {
            a == "*"
                || a == action
                || (a.ends_with("*") && action.starts_with(a.trim_end_matches('*')))
        });

        if !action_matches {
            return false;
        }

        // Check all conditions
        self.conditions
            .iter()
            .all(|condition| condition.evaluate(context))
    }
}

/// Policy condition for fine-grained control
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PolicyCondition {
    pub field: String,
    pub operator: ConditionOperator,
    pub value: serde_json::Value,
}

impl PolicyCondition {
    pub fn evaluate(&self, context: &PolicyContext) -> bool {
        let field_value = match self.field.as_str() {
            "agent_id" => Some(serde_json::Value::String(context.agent_id.clone())),
            "time_of_day" => {
                let hour = Utc::now().hour();
                Some(serde_json::Value::Number(hour.into()))
            }
            "resource_type" => context.metadata.get("resource_type").cloned(),
            "environment" => context.metadata.get("environment").cloned(),
            _ => context.metadata.get(&self.field).cloned(),
        };

        match (&self.operator, field_value) {
            (ConditionOperator::Equals, Some(val)) => val == self.value,
            (ConditionOperator::NotEquals, Some(val)) => val != self.value,
            (ConditionOperator::In, Some(val)) => {
                if let Some(array) = self.value.as_array() {
                    array.contains(&val)
                } else {
                    false
                }
            }
            (ConditionOperator::NotIn, Some(val)) => {
                if let Some(array) = self.value.as_array() {
                    !array.contains(&val)
                } else {
                    true
                }
            }
            (ConditionOperator::GreaterThan, Some(val)) => {
                if let (Some(v1), Some(v2)) = (val.as_f64(), self.value.as_f64()) {
                    v1 > v2
                } else {
                    false
                }
            }
            (ConditionOperator::LessThan, Some(val)) => {
                if let (Some(v1), Some(v2)) = (val.as_f64(), self.value.as_f64()) {
                    v1 < v2
                } else {
                    false
                }
            }
            (ConditionOperator::Contains, Some(val)) => {
                if let (Some(s1), Some(s2)) = (val.as_str(), self.value.as_str()) {
                    s1.contains(s2)
                } else {
                    false
                }
            }
            _ => false,
        }
    }
}

/// Condition operators
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ConditionOperator {
    Equals,
    NotEquals,
    In,
    NotIn,
    GreaterThan,
    LessThan,
    Contains,
}

/// Policy effect (allow or deny)
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum PolicyEffect {
    Allow,
    Deny,
}

/// Policy evaluation context
#[derive(Clone, Debug, Default)]
pub struct PolicyContext {
    pub agent_id: String,
    pub timestamp: Option<DateTime<Utc>>,
    pub metadata: HashMap<String, serde_json::Value>,
}

impl PolicyContext {
    pub fn new(agent_id: String) -> Self {
        Self {
            agent_id,
            timestamp: Some(Utc::now()),
            metadata: HashMap::new(),
        }
    }

    pub fn with_metadata(mut self, key: String, value: serde_json::Value) -> Self {
        self.metadata.insert(key, value);
        self
    }
}

/// Policy decision result
#[derive(Debug)]
pub struct PolicyDecision {
    pub allowed: bool,
    pub rule_id: Option<String>,
    pub reason: String,
}

/// Create default policy rules
pub fn create_default_policies() -> Vec<PolicyRule> {
    vec![
        // Allow document loader tools (high priority)
        PolicyRule::new("allow_document_loaders".to_string(), PolicyEffect::Allow)
            .with_description("Allow document loader tools for file processing".to_string())
            .with_priority(400) // Highest priority for document tools
            .with_actions(vec![
                "tool:csv_loader".to_string(),
                "tool:json_yaml_loader".to_string(),
                "tool:html_loader".to_string(),
                "tool:pdf_loader".to_string(),
            ]),
        // Allow test tools (for testing purposes)
        PolicyRule::new("allow_test_tools".to_string(), PolicyEffect::Allow)
            .with_description("Allow test tools and mock tools for testing".to_string())
            .with_priority(300) // High priority to override other rules
            .with_actions(vec![
                "tool:test_tool".to_string(),
                "tool:failing_tool".to_string(),
                "tool:param_tool".to_string(),
                "tool:timing_tool".to_string(),
            ]),
        // Allow safe commands
        PolicyRule::new("allow_safe_commands".to_string(), PolicyEffect::Allow)
            .with_description("Allow safe system commands".to_string())
            .with_priority(150)
            .with_actions(vec!["tool:command".to_string()])
            .with_condition(PolicyCondition {
                field: "command".to_string(),
                operator: ConditionOperator::In,
                value: serde_json::json!(["echo", "ls", "dir", "pwd", "whoami", "date"]),
            }),
        // Allow all file operations for testing (no conditions)
        PolicyRule::new("allow_all_file_ops".to_string(), PolicyEffect::Allow)
            .with_description("Allow all file operations for testing".to_string())
            .with_priority(250) // Higher priority than original rule
            .with_actions(vec![
                "tool:create_file".to_string(),
                "tool:edit_file".to_string(),
            ]),
        // Allow basic file operations in safe directories (original rule)
        PolicyRule::new("safe_file_ops".to_string(), PolicyEffect::Allow)
            .with_description("Allow file operations in safe directories".to_string())
            .with_priority(100)
            .with_actions(vec![
                "tool:create_file".to_string(),
                "tool:edit_file".to_string(),
            ])
            .with_condition(PolicyCondition {
                field: "path".to_string(),
                operator: ConditionOperator::NotIn,
                value: serde_json::json!(["/etc", "/bin", "/sbin", "C:\\Windows"]),
            }),
        // Deny dangerous commands
        PolicyRule::new("deny_dangerous_commands".to_string(), PolicyEffect::Deny)
            .with_description("Block dangerous system commands".to_string())
            .with_priority(200)
            .with_actions(vec!["tool:command".to_string()])
            .with_condition(PolicyCondition {
                field: "command".to_string(),
                operator: ConditionOperator::In,
                value: serde_json::json!(["rm -rf", "sudo", "format", "mkfs"]),
            }),
        // Allow HTTP to safe domains
        PolicyRule::new("safe_http".to_string(), PolicyEffect::Allow)
            .with_description("Allow HTTP requests to safe domains".to_string())
            .with_priority(100)
            .with_actions(vec!["tool:http".to_string()])
            .with_condition(PolicyCondition {
                field: "domain".to_string(),
                operator: ConditionOperator::In,
                value: serde_json::json!(["localhost", "127.0.0.1", "api.openai.com"]),
            }),
        // Time-based restriction example
        PolicyRule::new("business_hours_only".to_string(), PolicyEffect::Allow)
            .with_description("Allow operations only during business hours".to_string())
            .with_priority(50)
            .with_actions(vec!["*".to_string()])
            .with_condition(PolicyCondition {
                field: "time_of_day".to_string(),
                operator: ConditionOperator::GreaterThan,
                value: serde_json::json!(8),
            })
            .with_condition(PolicyCondition {
                field: "time_of_day".to_string(),
                operator: ConditionOperator::LessThan,
                value: serde_json::json!(18),
            }),
    ]
}

/// Legacy PolicyEngine for backward compatibility
pub struct PolicyEngine {
    policies: Vec<String>,
}

impl PolicyEngine {
    pub fn new() -> Self {
        Self {
            policies: Vec::new(),
        }
    }

    pub fn validate(&self, action: &str) -> bool {
        // Basic policy validation - block if action matches any deny policy
        !self.policies.iter().any(|p| action.contains(p))
    }

    pub fn add_policy(&mut self, policy: String) {
        self.policies.push(policy);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_enhanced_policy_engine_creation() {
        let engine = EnhancedPolicyEngine::new();
        assert_eq!(engine.rules.len(), 0);
        assert!(matches!(engine.default_effect, PolicyEffect::Deny));
    }

    #[test]
    fn test_policy_rule_creation() {
        let rule = PolicyRule::new("test_rule".to_string(), PolicyEffect::Allow)
            .with_description("Test rule description".to_string())
            .with_priority(150)
            .with_actions(vec!["test_action".to_string()]);

        assert_eq!(rule.name, "test_rule");
        assert!(matches!(rule.effect, PolicyEffect::Allow));
        assert_eq!(rule.description, "Test rule description");
        assert_eq!(rule.priority, 150);
        assert_eq!(rule.actions, vec!["test_action"]);
    }

    #[test]
    fn test_policy_context_creation() {
        let context = PolicyContext::new("agent_123".to_string())
            .with_metadata("environment".to_string(), json!("production"))
            .with_metadata("resource_type".to_string(), json!("file"));

        assert_eq!(context.agent_id, "agent_123");
        assert!(context.timestamp.is_some());
        assert_eq!(
            context.metadata.get("environment"),
            Some(&json!("production"))
        );
        assert_eq!(context.metadata.get("resource_type"), Some(&json!("file")));
    }

    #[test]
    fn test_add_rule_to_engine() {
        let mut engine = EnhancedPolicyEngine::new();
        let rule = PolicyRule::new("test_rule".to_string(), PolicyEffect::Allow);

        let rule_id = engine.add_rule(rule).unwrap();
        assert_eq!(engine.rules.len(), 1);
        assert!(engine.rules.contains_key(&rule_id));
    }

    #[test]
    fn test_remove_rule_from_engine() {
        let mut engine = EnhancedPolicyEngine::new();
        let rule = PolicyRule::new("test_rule".to_string(), PolicyEffect::Allow);
        let rule_id = engine.add_rule(rule).unwrap();

        let result = engine.remove_rule(&rule_id);
        assert!(result.is_ok());
        assert_eq!(engine.rules.len(), 0);

        let result = engine.remove_rule("nonexistent");
        assert!(result.is_err());
    }

    #[test]
    fn test_policy_rule_action_matching() {
        let rule = PolicyRule::new("test_rule".to_string(), PolicyEffect::Allow)
            .with_actions(vec!["tool:create_file".to_string()]);

        let context = PolicyContext::new("agent_123".to_string());

        assert!(rule.matches("tool:create_file", &context));
        assert!(!rule.matches("tool:delete_file", &context));
    }

    #[test]
    fn test_policy_rule_wildcard_matching() {
        let rule = PolicyRule::new("test_rule".to_string(), PolicyEffect::Allow)
            .with_actions(vec!["tool:*".to_string()]);

        let context = PolicyContext::new("agent_123".to_string());

        assert!(rule.matches("tool:create_file", &context));
        assert!(rule.matches("tool:delete_file", &context));
        assert!(!rule.matches("llm:complete", &context));
    }

    #[test]
    fn test_policy_rule_universal_wildcard() {
        let rule = PolicyRule::new("test_rule".to_string(), PolicyEffect::Allow)
            .with_actions(vec!["*".to_string()]);

        let context = PolicyContext::new("agent_123".to_string());

        assert!(rule.matches("tool:create_file", &context));
        assert!(rule.matches("llm:complete", &context));
        assert!(rule.matches("any_action", &context));
    }

    #[test]
    fn test_policy_condition_equals() {
        let condition = PolicyCondition {
            field: "agent_id".to_string(),
            operator: ConditionOperator::Equals,
            value: json!("agent_123"),
        };

        let context = PolicyContext::new("agent_123".to_string());
        assert!(condition.evaluate(&context));

        let context = PolicyContext::new("agent_456".to_string());
        assert!(!condition.evaluate(&context));
    }

    #[test]
    fn test_policy_condition_not_equals() {
        let condition = PolicyCondition {
            field: "agent_id".to_string(),
            operator: ConditionOperator::NotEquals,
            value: json!("agent_123"),
        };

        let context = PolicyContext::new("agent_456".to_string());
        assert!(condition.evaluate(&context));

        let context = PolicyContext::new("agent_123".to_string());
        assert!(!condition.evaluate(&context));
    }

    #[test]
    fn test_policy_condition_in() {
        let condition = PolicyCondition {
            field: "environment".to_string(),
            operator: ConditionOperator::In,
            value: json!(["production", "staging"]),
        };

        let context = PolicyContext::new("agent_123".to_string())
            .with_metadata("environment".to_string(), json!("production"));
        assert!(condition.evaluate(&context));

        let context = PolicyContext::new("agent_123".to_string())
            .with_metadata("environment".to_string(), json!("development"));
        assert!(!condition.evaluate(&context));
    }

    #[test]
    fn test_policy_condition_not_in() {
        let condition = PolicyCondition {
            field: "environment".to_string(),
            operator: ConditionOperator::NotIn,
            value: json!(["production", "staging"]),
        };

        let context = PolicyContext::new("agent_123".to_string())
            .with_metadata("environment".to_string(), json!("development"));
        assert!(condition.evaluate(&context));

        let context = PolicyContext::new("agent_123".to_string())
            .with_metadata("environment".to_string(), json!("production"));
        assert!(!condition.evaluate(&context));
    }

    #[test]
    fn test_policy_condition_greater_than() {
        let condition = PolicyCondition {
            field: "score".to_string(),
            operator: ConditionOperator::GreaterThan,
            value: json!(80),
        };

        let context = PolicyContext::new("agent_123".to_string())
            .with_metadata("score".to_string(), json!(90));
        assert!(condition.evaluate(&context));

        let context = PolicyContext::new("agent_123".to_string())
            .with_metadata("score".to_string(), json!(70));
        assert!(!condition.evaluate(&context));
    }

    #[test]
    fn test_policy_condition_less_than() {
        let condition = PolicyCondition {
            field: "score".to_string(),
            operator: ConditionOperator::LessThan,
            value: json!(80),
        };

        let context = PolicyContext::new("agent_123".to_string())
            .with_metadata("score".to_string(), json!(70));
        assert!(condition.evaluate(&context));

        let context = PolicyContext::new("agent_123".to_string())
            .with_metadata("score".to_string(), json!(90));
        assert!(!condition.evaluate(&context));
    }

    #[test]
    fn test_policy_condition_contains() {
        let condition = PolicyCondition {
            field: "path".to_string(),
            operator: ConditionOperator::Contains,
            value: json!("/safe/"),
        };

        let context = PolicyContext::new("agent_123".to_string())
            .with_metadata("path".to_string(), json!("/safe/documents/file.txt"));
        assert!(condition.evaluate(&context));

        let context = PolicyContext::new("agent_123".to_string())
            .with_metadata("path".to_string(), json!("/etc/passwd"));
        assert!(!condition.evaluate(&context));
    }

    #[test]
    fn test_policy_condition_time_of_day() {
        let condition = PolicyCondition {
            field: "time_of_day".to_string(),
            operator: ConditionOperator::GreaterThan,
            value: json!(-1), // Always should be greater than -1 (since hour is 0-23)
        };

        let context = PolicyContext::new("agent_123".to_string());
        assert!(condition.evaluate(&context));
    }

    #[test]
    fn test_policy_rule_with_conditions() {
        let rule = PolicyRule::new("safe_file_ops".to_string(), PolicyEffect::Allow)
            .with_actions(vec!["tool:create_file".to_string()])
            .with_condition(PolicyCondition {
                field: "path".to_string(),
                operator: ConditionOperator::Contains,
                value: json!("/safe/"),
            });

        let safe_context = PolicyContext::new("agent_123".to_string())
            .with_metadata("path".to_string(), json!("/safe/file.txt"));
        assert!(rule.matches("tool:create_file", &safe_context));

        let unsafe_context = PolicyContext::new("agent_123".to_string())
            .with_metadata("path".to_string(), json!("/etc/passwd"));
        assert!(!rule.matches("tool:create_file", &unsafe_context));
    }

    #[test]
    fn test_policy_rule_multiple_conditions() {
        let rule = PolicyRule::new("restricted_access".to_string(), PolicyEffect::Allow)
            .with_actions(vec!["tool:command".to_string()])
            .with_condition(PolicyCondition {
                field: "agent_id".to_string(),
                operator: ConditionOperator::Equals,
                value: json!("admin_agent"),
            })
            .with_condition(PolicyCondition {
                field: "environment".to_string(),
                operator: ConditionOperator::Equals,
                value: json!("production"),
            });

        // Both conditions must match
        let valid_context = PolicyContext::new("admin_agent".to_string())
            .with_metadata("environment".to_string(), json!("production"));
        assert!(rule.matches("tool:command", &valid_context));

        // Only one condition matches
        let invalid_context = PolicyContext::new("admin_agent".to_string())
            .with_metadata("environment".to_string(), json!("development"));
        assert!(!rule.matches("tool:command", &invalid_context));
    }

    #[test]
    fn test_engine_evaluate_action_allow() {
        let mut engine = EnhancedPolicyEngine::new();
        let rule = PolicyRule::new("allow_rule".to_string(), PolicyEffect::Allow)
            .with_actions(vec!["test_action".to_string()]);
        engine.add_rule(rule).unwrap();

        let context = PolicyContext::new("agent_123".to_string());
        let decision = engine.evaluate_action("test_action", &context);

        assert!(decision.allowed);
        assert!(decision.rule_id.is_some());
    }

    #[test]
    fn test_engine_evaluate_action_deny() {
        let mut engine = EnhancedPolicyEngine::new();
        let rule = PolicyRule::new("deny_rule".to_string(), PolicyEffect::Deny)
            .with_actions(vec!["test_action".to_string()]);
        engine.add_rule(rule).unwrap();

        let context = PolicyContext::new("agent_123".to_string());
        let decision = engine.evaluate_action("test_action", &context);

        assert!(!decision.allowed);
        assert!(decision.rule_id.is_some());
    }

    #[test]
    fn test_engine_evaluate_action_no_match() {
        let engine = EnhancedPolicyEngine::new();
        let context = PolicyContext::new("agent_123".to_string());
        let decision = engine.evaluate_action("test_action", &context);

        // Default effect is Deny
        assert!(!decision.allowed);
        assert!(decision.rule_id.is_none());
        assert!(decision.reason.contains("default"));
    }

    #[test]
    fn test_engine_priority_handling() {
        let mut engine = EnhancedPolicyEngine::new();

        // Lower priority rule (should be overridden)
        let low_priority = PolicyRule::new("low_priority".to_string(), PolicyEffect::Allow)
            .with_priority(50)
            .with_actions(vec!["test_action".to_string()]);
        engine.add_rule(low_priority).unwrap();

        // Higher priority rule (should take precedence)
        let high_priority = PolicyRule::new("high_priority".to_string(), PolicyEffect::Deny)
            .with_priority(100)
            .with_actions(vec!["test_action".to_string()]);
        engine.add_rule(high_priority).unwrap();

        let context = PolicyContext::new("agent_123".to_string());
        let decision = engine.evaluate_action("test_action", &context);

        // Higher priority rule should win (Deny)
        assert!(!decision.allowed);
        assert!(decision.rule_id.is_some());
    }

    #[test]
    fn test_engine_set_default_effect() {
        let mut engine = EnhancedPolicyEngine::new();
        engine.set_default_effect(PolicyEffect::Allow);

        let context = PolicyContext::new("agent_123".to_string());
        let decision = engine.evaluate_action("unknown_action", &context);

        // Should allow due to default effect
        assert!(decision.allowed);
        assert!(decision.rule_id.is_none());
    }

    #[test]
    fn test_engine_list_rules() {
        let mut engine = EnhancedPolicyEngine::new();
        let rule1 = PolicyRule::new("rule1".to_string(), PolicyEffect::Allow);
        let rule2 = PolicyRule::new("rule2".to_string(), PolicyEffect::Deny);

        engine.add_rule(rule1).unwrap();
        engine.add_rule(rule2).unwrap();

        let rules = engine.list_rules();
        assert_eq!(rules.len(), 2);
        assert!(rules.iter().any(|(_, rule)| rule.name == "rule1"));
        assert!(rules.iter().any(|(_, rule)| rule.name == "rule2"));
    }

    #[test]
    fn test_engine_get_rule() {
        let mut engine = EnhancedPolicyEngine::new();
        let rule = PolicyRule::new("test_rule".to_string(), PolicyEffect::Allow);
        let rule_id = engine.add_rule(rule).unwrap();

        let retrieved = engine.get_rule(&rule_id);
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().name, "test_rule");

        let not_found = engine.get_rule("nonexistent");
        assert!(not_found.is_none());
    }

    #[test]
    fn test_rules_overlap_detection() {
        let actions1 = vec!["tool:create_file".to_string()];
        let actions2 = vec!["tool:create_file".to_string()];
        assert!(EnhancedPolicyEngine::rules_overlap(&actions1, &actions2));

        let actions1 = vec!["tool:*".to_string()];
        let actions2 = vec!["tool:create_file".to_string()];
        assert!(EnhancedPolicyEngine::rules_overlap(&actions1, &actions2));

        let actions1 = vec!["*".to_string()];
        let actions2 = vec!["anything".to_string()];
        assert!(EnhancedPolicyEngine::rules_overlap(&actions1, &actions2));

        let actions1 = vec!["tool:create_file".to_string()];
        let actions2 = vec!["llm:complete".to_string()];
        assert!(!EnhancedPolicyEngine::rules_overlap(&actions1, &actions2));
    }

    #[test]
    fn test_create_default_policies() {
        let policies = create_default_policies();
        assert!(!policies.is_empty());

        // Check that we have the expected default policies
        assert!(policies.iter().any(|p| p.name == "allow_document_loaders"));
        assert!(policies.iter().any(|p| p.name == "safe_file_ops"));
        assert!(policies.iter().any(|p| p.name == "deny_dangerous_commands"));
        assert!(policies.iter().any(|p| p.name == "safe_http"));
        assert!(policies.iter().any(|p| p.name == "business_hours_only"));
    }

    #[test]
    fn test_default_policy_safe_file_ops() {
        let policies = create_default_policies();
        let safe_file_rule = policies.iter().find(|p| p.name == "safe_file_ops").unwrap();

        // Test that it allows safe file operations
        let safe_context = PolicyContext::new("agent_123".to_string())
            .with_metadata("path".to_string(), json!("/home/user/file.txt"));
        assert!(safe_file_rule.matches("tool:create_file", &safe_context));

        // Test that it blocks unsafe paths (exact match required for NotIn)
        let unsafe_context = PolicyContext::new("agent_123".to_string())
            .with_metadata("path".to_string(), json!("/etc"));
        assert!(!safe_file_rule.matches("tool:create_file", &unsafe_context));
    }

    #[test]
    fn test_default_policy_deny_dangerous_commands() {
        let policies = create_default_policies();
        let dangerous_rule = policies
            .iter()
            .find(|p| p.name == "deny_dangerous_commands")
            .unwrap();

        // Test that it blocks dangerous commands
        let dangerous_context = PolicyContext::new("agent_123".to_string())
            .with_metadata("command".to_string(), json!("rm -rf"));
        assert!(dangerous_rule.matches("tool:command", &dangerous_context));
        assert!(matches!(dangerous_rule.effect, PolicyEffect::Deny));
    }

    #[test]
    fn test_legacy_policy_engine() {
        let mut engine = PolicyEngine::new();

        // Initially should allow everything
        assert!(engine.validate("test_action"));

        // Add blocking policy
        engine.add_policy("dangerous".to_string());

        // Should now block actions containing "dangerous"
        assert!(!engine.validate("dangerous_action"));
        assert!(engine.validate("safe_action"));
    }

    #[test]
    fn test_policy_decision_debug() {
        let decision = PolicyDecision {
            allowed: true,
            rule_id: Some("test_rule".to_string()),
            reason: "Test reason".to_string(),
        };

        let debug_str = format!("{:?}", decision);
        assert!(debug_str.contains("allowed: true"));
        assert!(debug_str.contains("test_rule"));
        assert!(debug_str.contains("Test reason"));
    }
}