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
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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
//! Guard engine for security and policy validation
//!
//! The Guard provides comprehensive security validation and policy enforcement
//! for all workflow actions before they are executed. It implements multiple
//! layers of security checks and provides detailed violation reporting.

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use tracing::{debug, info, warn};

use super::schemas::{
    ActionModification, ActionType, GuardResult, RiskLevel, SecurityViolation, WorkflowAction,
};
use crate::runners::ExecContext;

/// Guard configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GuardConfig {
    /// Security enforcement level
    pub enforcement_level: EnforcementLevel,

    /// Policy rules to enforce
    pub policy_rules: Vec<PolicyRule>,

    /// Allowed capabilities
    pub allowed_capabilities: HashSet<String>,

    /// Blocked patterns
    pub blocked_patterns: Vec<BlockedPattern>,

    /// Resource limits
    pub resource_limits: GuardResourceLimits,

    /// Security context requirements
    pub security_context: SecurityContext,
}

impl Default for GuardConfig {
    fn default() -> Self {
        Self {
            enforcement_level: EnforcementLevel::Strict,
            policy_rules: Self::default_policy_rules(),
            allowed_capabilities: Self::default_capabilities(),
            blocked_patterns: Self::default_blocked_patterns(),
            resource_limits: GuardResourceLimits::default(),
            security_context: SecurityContext::default(),
        }
    }
}

impl GuardConfig {
    /// Get default policy rules
    fn default_policy_rules() -> Vec<PolicyRule> {
        vec![
            PolicyRule {
                name: "filesystem_access".to_string(),
                rule_type: PolicyRuleType::PathValidation,
                conditions: vec![PolicyCondition::PathAllowList(vec![
                    "/tmp/".to_string(),
                    "/workspace/".to_string(),
                    "/var/tmp/".to_string(),
                ])],
                severity: RiskLevel::High,
                action: PolicyAction::Block,
            },
            PolicyRule {
                name: "network_access".to_string(),
                rule_type: PolicyRuleType::NetworkValidation,
                conditions: vec![PolicyCondition::UrlPattern(
                    r"^https?://[a-zA-Z0-9.-]+\.(com|org|net|edu).*".to_string(),
                )],
                severity: RiskLevel::Medium,
                action: PolicyAction::Validate,
            },
            PolicyRule {
                name: "shell_command".to_string(),
                rule_type: PolicyRuleType::CommandValidation,
                conditions: vec![PolicyCondition::CommandBlockList(vec![
                    "rm -rf".to_string(),
                    "sudo".to_string(),
                    "chmod 777".to_string(),
                    "dd if=".to_string(),
                ])],
                severity: RiskLevel::Critical,
                action: PolicyAction::Block,
            },
            PolicyRule {
                name: "resource_limits".to_string(),
                rule_type: PolicyRuleType::ResourceValidation,
                conditions: vec![
                    PolicyCondition::MaxActions(100),
                    PolicyCondition::MaxDuration(3600), // 1 hour
                ],
                severity: RiskLevel::Medium,
                action: PolicyAction::Limit,
            },
        ]
    }

    /// Get default allowed capabilities
    fn default_capabilities() -> HashSet<String> {
        vec![
            "fs.read.v1".to_string(),
            "fs.write.v1".to_string(),
            "http.fetch.v1".to_string(),
            "planner.exec.v1".to_string(),
        ]
        .into_iter()
        .collect()
    }

    /// Get default blocked patterns
    fn default_blocked_patterns() -> Vec<BlockedPattern> {
        vec![
            BlockedPattern {
                pattern: r"/etc/passwd".to_string(),
                pattern_type: PatternType::Path,
                reason: "System password file access forbidden".to_string(),
            },
            BlockedPattern {
                pattern: r"/proc/.*".to_string(),
                pattern_type: PatternType::Path,
                reason: "Process filesystem access restricted".to_string(),
            },
            BlockedPattern {
                pattern: r".*\.exe$".to_string(),
                pattern_type: PatternType::Path,
                reason: "Executable file access restricted".to_string(),
            },
            BlockedPattern {
                pattern: r"password\s*=".to_string(),
                pattern_type: PatternType::Content,
                reason: "Potential credential exposure".to_string(),
            },
        ]
    }
}

/// Security enforcement levels
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum EnforcementLevel {
    /// Block all violations
    Strict,
    /// Allow with warnings
    Permissive,
    /// Log only
    Monitor,
    /// No enforcement (testing only)
    Disabled,
}

/// Policy rule definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PolicyRule {
    /// Rule name/identifier
    pub name: String,

    /// Type of validation rule
    pub rule_type: PolicyRuleType,

    /// Conditions that trigger this rule
    pub conditions: Vec<PolicyCondition>,

    /// Severity of violations
    pub severity: RiskLevel,

    /// Action to take on violation
    pub action: PolicyAction,
}

/// Types of policy rules
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum PolicyRuleType {
    PathValidation,
    NetworkValidation,
    CommandValidation,
    ResourceValidation,
    ContentValidation,
    CapabilityValidation,
}

/// Policy rule conditions
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "value")]
pub enum PolicyCondition {
    PathAllowList(Vec<String>),
    PathBlockList(Vec<String>),
    UrlPattern(String),
    CommandBlockList(Vec<String>),
    MaxActions(u32),
    MaxDuration(u64),
    RequiredCapability(String),
    ContentPattern(String),
}

/// Actions to take when policy violations occur
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum PolicyAction {
    /// Block the action entirely
    Block,
    /// Allow but require validation
    Validate,
    /// Apply resource limits
    Limit,
    /// Transform the action
    Transform,
    /// Log and continue
    Log,
}

/// Blocked pattern definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockedPattern {
    /// Regular expression pattern
    pub pattern: String,

    /// Type of pattern
    pub pattern_type: PatternType,

    /// Reason for blocking
    pub reason: String,
}

/// Pattern types for validation
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum PatternType {
    Path,
    Url,
    Command,
    Content,
    Parameter,
}

/// Resource limits enforced by the Guard
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GuardResourceLimits {
    /// Maximum number of actions per workflow
    pub max_actions: u32,

    /// Maximum workflow duration in seconds
    pub max_duration_seconds: u64,

    /// Maximum memory usage per action in MB
    pub max_memory_mb: u64,

    /// Maximum file size for reads in MB
    pub max_file_size_mb: u64,

    /// Maximum network payload size in MB
    pub max_network_payload_mb: u64,
}

impl Default for GuardResourceLimits {
    fn default() -> Self {
        Self {
            max_actions: 100,
            max_duration_seconds: 3600, // 1 hour
            max_memory_mb: 512,
            max_file_size_mb: 100,
            max_network_payload_mb: 50,
        }
    }
}

/// Security context requirements
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityContext {
    /// Required sandbox mode
    pub required_sandbox: bool,

    /// Required isolation features
    pub required_isolation: Vec<IsolationFeature>,

    /// Allowed network access
    pub network_allowed: bool,

    /// Allowed file system access
    pub filesystem_allowed: bool,

    /// Required audit level
    pub audit_level: AuditLevel,
}

impl Default for SecurityContext {
    fn default() -> Self {
        Self {
            required_sandbox: true,
            required_isolation: vec![
                IsolationFeature::ProcessNamespace,
                IsolationFeature::NetworkNamespace,
                IsolationFeature::FileSystemRestrictions,
            ],
            network_allowed: true,
            filesystem_allowed: true,
            audit_level: AuditLevel::Full,
        }
    }
}

/// Isolation features
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum IsolationFeature {
    ProcessNamespace,
    NetworkNamespace,
    FileSystemRestrictions,
    ResourceLimits,
    SeccompFiltering,
    LandlockRestrictions,
}

/// Audit level requirements
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum AuditLevel {
    None,
    Basic,
    Standard,
    Full,
    Forensic,
}

/// Main Guard implementation
pub struct Guard {
    config: GuardConfig,
    execution_context: ExecContext,
    violation_count: u32,
    blocked_count: u32,
}

impl Guard {
    /// Create a new Guard instance
    pub fn new(exec_context: &ExecContext) -> Result<Self> {
        let config = GuardConfig::default();

        info!(
            enforcement_level = ?config.enforcement_level,
            policy_rules = config.policy_rules.len(),
            "Guard initialized"
        );

        Ok(Self {
            config,
            execution_context: exec_context.clone(),
            violation_count: 0,
            blocked_count: 0,
        })
    }

    /// Create Guard with custom configuration
    pub fn with_config(exec_context: &ExecContext, config: GuardConfig) -> Result<Self> {
        info!(
            enforcement_level = ?config.enforcement_level,
            policy_rules = config.policy_rules.len(),
            "Guard initialized with custom config"
        );

        Ok(Self {
            config,
            execution_context: exec_context.clone(),
            violation_count: 0,
            blocked_count: 0,
        })
    }

    /// Validate a workflow action
    pub async fn validate_action(&mut self, action: &WorkflowAction) -> Result<bool> {
        debug!(
            action_id = %action.id,
            action_type = ?action.action_type,
            "Validating action"
        );

        let result = self.perform_validation(action).await?;

        // Update statistics
        if !result.violations.is_empty() {
            self.violation_count += 1;
        }

        if !result.allowed {
            self.blocked_count += 1;
            warn!(
                action_id = %action.id,
                reason = %result.reason,
                violations = result.violations.len(),
                "Action blocked by guard"
            );
        } else {
            debug!(
                action_id = %action.id,
                violations = result.violations.len(),
                "Action allowed by guard"
            );
        }

        Ok(result.allowed)
    }

    /// Get detailed validation result
    pub async fn validate_action_detailed(
        &mut self,
        action: &WorkflowAction,
    ) -> Result<GuardResult> {
        debug!(
            action_id = %action.id,
            "Performing detailed validation"
        );

        let result = self.perform_validation(action).await?;

        // Update statistics
        if !result.violations.is_empty() {
            self.violation_count += 1;
        }

        if !result.allowed {
            self.blocked_count += 1;
        }

        Ok(result)
    }

    /// Perform the actual validation
    async fn perform_validation(&self, action: &WorkflowAction) -> Result<GuardResult> {
        let mut violations = Vec::new();
        let mut modifications = Vec::new();
        let mut allowed = true;
        let mut reason = "Action allowed".to_string();

        // Check if enforcement is disabled
        if self.config.enforcement_level == EnforcementLevel::Disabled {
            return Ok(GuardResult {
                allowed: true,
                reason: "Guard enforcement disabled".to_string(),
                violations,
                modifications,
            });
        }

        // 1. Capability validation
        self.validate_capability(action, &mut violations, &mut allowed, &mut reason)?;

        // 2. Policy rule validation
        self.validate_policy_rules(
            action,
            &mut violations,
            &mut modifications,
            &mut allowed,
            &mut reason,
        )
        .await?;

        // 3. Pattern validation
        self.validate_patterns(action, &mut violations, &mut allowed, &mut reason)?;

        // 4. Resource validation
        self.validate_resources(action, &mut violations, &mut allowed, &mut reason)?;

        // 5. Security context validation
        self.validate_security_context(action, &mut violations, &mut allowed, &mut reason)?;

        // Apply enforcement level
        match self.config.enforcement_level {
            EnforcementLevel::Strict => {
                // Already applied above
            }
            EnforcementLevel::Permissive => {
                // Allow with warnings
                if !allowed {
                    reason = format!(
                        "Permissive mode: {} (would be blocked in strict mode)",
                        reason
                    );
                    allowed = true;
                }
            }
            EnforcementLevel::Monitor => {
                // Log only
                allowed = true;
                if !violations.is_empty() {
                    reason = format!("Monitor mode: violations logged but action allowed");
                }
            }
            EnforcementLevel::Disabled => {
                // Already handled above
            }
        }

        Ok(GuardResult {
            allowed,
            reason,
            violations,
            modifications,
        })
    }

    /// Validate capability access
    fn validate_capability(
        &self,
        action: &WorkflowAction,
        violations: &mut Vec<SecurityViolation>,
        allowed: &mut bool,
        reason: &mut String,
    ) -> Result<()> {
        let capability = match &action.action_type {
            ActionType::FileSystem(cap) => cap,
            ActionType::Http(cap) => cap,
            ActionType::Shell(cap) => cap,
            ActionType::Research(cap) => cap,
            ActionType::Planning(cap) => cap,
            ActionType::Analysis(cap) => cap,
            ActionType::Custom(cap) => cap,
        };

        if !self.config.allowed_capabilities.contains(capability) {
            violations.push(SecurityViolation {
                violation_type: "CAPABILITY_NOT_ALLOWED".to_string(),
                severity: RiskLevel::High,
                description: format!("Capability '{}' is not in the allowed list", capability),
                remediation: "Use an allowed capability or request permission".to_string(),
            });

            *allowed = false;
            *reason = format!("Capability '{}' not allowed", capability);
        }

        Ok(())
    }

    /// Validate against policy rules
    async fn validate_policy_rules(
        &self,
        action: &WorkflowAction,
        violations: &mut Vec<SecurityViolation>,
        modifications: &mut Vec<ActionModification>,
        allowed: &mut bool,
        reason: &mut String,
    ) -> Result<()> {
        for rule in &self.config.policy_rules {
            let rule_violations = self.check_policy_rule(rule, action).await?;

            for violation in rule_violations {
                violations.push(violation.clone());

                match rule.action {
                    PolicyAction::Block => {
                        *allowed = false;
                        *reason = format!("Blocked by policy rule: {}", rule.name);
                    }
                    PolicyAction::Validate => {
                        // Additional validation required
                        debug!(rule = %rule.name, "Additional validation required");
                    }
                    PolicyAction::Limit => {
                        // Apply limits
                        modifications.push(ActionModification {
                            field: "resource_limits".to_string(),
                            suggested_value: serde_json::json!({
                                "max_memory_mb": self.config.resource_limits.max_memory_mb,
                                "max_duration_s": self.config.resource_limits.max_duration_seconds
                            }),
                            reason: format!("Applied limits due to rule: {}", rule.name),
                        });
                    }
                    PolicyAction::Transform => {
                        // Suggest transformations
                        modifications.push(ActionModification {
                            field: "parameters".to_string(),
                            suggested_value: serde_json::json!({}),
                            reason: format!("Transform required due to rule: {}", rule.name),
                        });
                    }
                    PolicyAction::Log => {
                        // Just log, don't block
                        info!(rule = %rule.name, "Policy violation logged");
                    }
                }
            }
        }

        Ok(())
    }

    /// Check a specific policy rule
    async fn check_policy_rule(
        &self,
        rule: &PolicyRule,
        action: &WorkflowAction,
    ) -> Result<Vec<SecurityViolation>> {
        let mut violations = Vec::new();

        for condition in &rule.conditions {
            match condition {
                PolicyCondition::PathAllowList(allowed_paths) => {
                    if let Some(path) = self.extract_path_parameter(action) {
                        if !allowed_paths
                            .iter()
                            .any(|allowed| path.starts_with(allowed))
                        {
                            violations.push(SecurityViolation {
                                violation_type: "PATH_NOT_ALLOWED".to_string(),
                                severity: rule.severity.clone(),
                                description: format!("Path '{}' not in allowed list", path),
                                remediation: "Use a path within the allowed directories"
                                    .to_string(),
                            });
                        }
                    }
                }

                PolicyCondition::PathBlockList(blocked_paths) => {
                    if let Some(path) = self.extract_path_parameter(action) {
                        if blocked_paths.iter().any(|blocked| path.contains(blocked)) {
                            violations.push(SecurityViolation {
                                violation_type: "PATH_BLOCKED".to_string(),
                                severity: rule.severity.clone(),
                                description: format!("Path '{}' contains blocked pattern", path),
                                remediation:
                                    "Use a different path that doesn't match blocked patterns"
                                        .to_string(),
                            });
                        }
                    }
                }

                PolicyCondition::UrlPattern(pattern) => {
                    if let Some(url) = self.extract_url_parameter(action) {
                        let regex =
                            regex::Regex::new(pattern).context("Invalid URL pattern regex")?;
                        if !regex.is_match(&url) {
                            violations.push(SecurityViolation {
                                violation_type: "URL_PATTERN_VIOLATION".to_string(),
                                severity: rule.severity.clone(),
                                description: format!(
                                    "URL '{}' doesn't match required pattern",
                                    url
                                ),
                                remediation: "Use a URL that matches the required pattern"
                                    .to_string(),
                            });
                        }
                    }
                }

                PolicyCondition::CommandBlockList(blocked_commands) => {
                    if let Some(command) = self.extract_command_parameter(action) {
                        if blocked_commands
                            .iter()
                            .any(|blocked| command.contains(blocked))
                        {
                            violations.push(SecurityViolation {
                                violation_type: "COMMAND_BLOCKED".to_string(),
                                severity: rule.severity.clone(),
                                description: format!(
                                    "Command '{}' contains blocked pattern",
                                    command
                                ),
                                remediation:
                                    "Use a different command that doesn't match blocked patterns"
                                        .to_string(),
                            });
                        }
                    }
                }

                PolicyCondition::MaxActions(max_actions) => {
                    // This would be checked at the workflow level
                    debug!(max_actions = max_actions, "Max actions condition checked");
                }

                PolicyCondition::MaxDuration(max_duration) => {
                    if let Some(timeout) = action.timeout_ms {
                        if timeout > (*max_duration * 1000) {
                            violations.push(SecurityViolation {
                                violation_type: "DURATION_EXCEEDED".to_string(),
                                severity: rule.severity.clone(),
                                description: format!(
                                    "Action timeout {}ms exceeds maximum {}s",
                                    timeout, max_duration
                                ),
                                remediation: format!(
                                    "Reduce timeout to maximum of {}s",
                                    max_duration
                                ),
                            });
                        }
                    }
                }

                PolicyCondition::RequiredCapability(required_cap) => {
                    let action_capability = match &action.action_type {
                        ActionType::FileSystem(cap) => cap,
                        ActionType::Http(cap) => cap,
                        ActionType::Shell(cap) => cap,
                        ActionType::Research(cap) => cap,
                        ActionType::Planning(cap) => cap,
                        ActionType::Analysis(cap) => cap,
                        ActionType::Custom(cap) => cap,
                    };

                    if action_capability != required_cap {
                        violations.push(SecurityViolation {
                            violation_type: "REQUIRED_CAPABILITY_MISSING".to_string(),
                            severity: rule.severity.clone(),
                            description: format!(
                                "Required capability '{}' not present",
                                required_cap
                            ),
                            remediation: format!("Use the required capability '{}'", required_cap),
                        });
                    }
                }

                PolicyCondition::ContentPattern(pattern) => {
                    let params_str = action.parameters.to_string();
                    let regex =
                        regex::Regex::new(pattern).context("Invalid content pattern regex")?;
                    if regex.is_match(&params_str) {
                        violations.push(SecurityViolation {
                            violation_type: "CONTENT_PATTERN_VIOLATION".to_string(),
                            severity: rule.severity.clone(),
                            description: "Action parameters contain prohibited content pattern"
                                .to_string(),
                            remediation: "Remove or modify prohibited content in parameters"
                                .to_string(),
                        });
                    }
                }
            }
        }

        Ok(violations)
    }

    /// Validate against blocked patterns
    fn validate_patterns(
        &self,
        action: &WorkflowAction,
        violations: &mut Vec<SecurityViolation>,
        allowed: &mut bool,
        reason: &mut String,
    ) -> Result<()> {
        let params_str = action.parameters.to_string();

        for pattern in &self.config.blocked_patterns {
            let regex =
                regex::Regex::new(&pattern.pattern).context("Invalid blocked pattern regex")?;

            let matches = match pattern.pattern_type {
                PatternType::Path => {
                    if let Some(path) = self.extract_path_parameter(action) {
                        regex.is_match(&path)
                    } else {
                        false
                    }
                }
                PatternType::Url => {
                    if let Some(url) = self.extract_url_parameter(action) {
                        regex.is_match(&url)
                    } else {
                        false
                    }
                }
                PatternType::Command => {
                    if let Some(command) = self.extract_command_parameter(action) {
                        regex.is_match(&command)
                    } else {
                        false
                    }
                }
                PatternType::Content | PatternType::Parameter => regex.is_match(&params_str),
            };

            if matches {
                violations.push(SecurityViolation {
                    violation_type: "BLOCKED_PATTERN".to_string(),
                    severity: RiskLevel::High,
                    description: format!("Content matches blocked pattern: {}", pattern.reason),
                    remediation: "Modify content to avoid blocked patterns".to_string(),
                });

                *allowed = false;
                *reason = format!("Blocked pattern detected: {}", pattern.reason);
            }
        }

        Ok(())
    }

    /// Validate resource requirements
    fn validate_resources(
        &self,
        action: &WorkflowAction,
        violations: &mut Vec<SecurityViolation>,
        allowed: &mut bool,
        reason: &mut String,
    ) -> Result<()> {
        // Check timeout limits
        if let Some(timeout_ms) = action.timeout_ms {
            let timeout_seconds = timeout_ms / 1000;
            if timeout_seconds > self.config.resource_limits.max_duration_seconds {
                violations.push(SecurityViolation {
                    violation_type: "TIMEOUT_EXCEEDED".to_string(),
                    severity: RiskLevel::Medium,
                    description: format!(
                        "Action timeout {}s exceeds maximum {}s",
                        timeout_seconds, self.config.resource_limits.max_duration_seconds
                    ),
                    remediation: format!(
                        "Reduce timeout to maximum of {}s",
                        self.config.resource_limits.max_duration_seconds
                    ),
                });

                *allowed = false;
                *reason = "Timeout exceeds resource limits".to_string();
            }
        }

        // Check file size limits (for file operations)
        if matches!(action.action_type, ActionType::FileSystem(_)) {
            if let Some(max_bytes) = action.parameters.get("max_bytes") {
                if let Some(max_bytes_val) = max_bytes.as_u64() {
                    let max_bytes_mb = max_bytes_val / (1024 * 1024);
                    if max_bytes_mb > self.config.resource_limits.max_file_size_mb {
                        violations.push(SecurityViolation {
                            violation_type: "FILE_SIZE_EXCEEDED".to_string(),
                            severity: RiskLevel::Medium,
                            description: format!(
                                "Requested file size {}MB exceeds maximum {}MB",
                                max_bytes_mb, self.config.resource_limits.max_file_size_mb
                            ),
                            remediation: format!(
                                "Reduce file size limit to maximum of {}MB",
                                self.config.resource_limits.max_file_size_mb
                            ),
                        });

                        *allowed = false;
                        *reason = "File size exceeds resource limits".to_string();
                    }
                }
            }
        }

        Ok(())
    }

    /// Validate security context
    fn validate_security_context(
        &self,
        _action: &WorkflowAction,
        violations: &mut Vec<SecurityViolation>,
        allowed: &mut bool,
        reason: &mut String,
    ) -> Result<()> {
        // Check if sandbox is required but not available
        if self.config.security_context.required_sandbox {
            // In a real implementation, we'd check if the execution context has sandbox capabilities
            // For now, we'll assume sandbox is available if execution context is present
            debug!("Sandbox requirement validated");
        }

        // Check required isolation features
        for feature in &self.config.security_context.required_isolation {
            match feature {
                IsolationFeature::ProcessNamespace => {
                    // Check if process namespace isolation is available
                    debug!("Process namespace isolation required");
                }
                IsolationFeature::NetworkNamespace => {
                    if !self.config.security_context.network_allowed {
                        violations.push(SecurityViolation {
                            violation_type: "NETWORK_NOT_ALLOWED".to_string(),
                            severity: RiskLevel::High,
                            description: "Network access not allowed in current security context"
                                .to_string(),
                            remediation: "Remove network operations or request network permissions"
                                .to_string(),
                        });

                        *allowed = false;
                        *reason = "Network access not allowed".to_string();
                    }
                }
                IsolationFeature::FileSystemRestrictions => {
                    if !self.config.security_context.filesystem_allowed {
                        violations.push(SecurityViolation {
                            violation_type: "FILESYSTEM_NOT_ALLOWED".to_string(),
                            severity: RiskLevel::High,
                            description:
                                "Filesystem access not allowed in current security context"
                                    .to_string(),
                            remediation:
                                "Remove filesystem operations or request filesystem permissions"
                                    .to_string(),
                        });

                        *allowed = false;
                        *reason = "Filesystem access not allowed".to_string();
                    }
                }
                _ => {
                    debug!(feature = ?feature, "Isolation feature check");
                }
            }
        }

        Ok(())
    }

    /// Extract path parameter from action
    fn extract_path_parameter(&self, action: &WorkflowAction) -> Option<String> {
        action
            .parameters
            .get("path")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string())
    }

    /// Extract URL parameter from action
    fn extract_url_parameter(&self, action: &WorkflowAction) -> Option<String> {
        action
            .parameters
            .get("url")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string())
    }

    /// Extract command parameter from action
    fn extract_command_parameter(&self, action: &WorkflowAction) -> Option<String> {
        action
            .parameters
            .get("command")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string())
    }

    /// Get Guard statistics
    pub fn get_statistics(&self) -> GuardStatistics {
        GuardStatistics {
            violation_count: self.violation_count,
            blocked_count: self.blocked_count,
            enforcement_level: self.config.enforcement_level.clone(),
            active_rules: self.config.policy_rules.len(),
            blocked_patterns: self.config.blocked_patterns.len(),
        }
    }

    /// Reset statistics
    pub fn reset_statistics(&mut self) {
        self.violation_count = 0;
        self.blocked_count = 0;
    }
}

/// Guard statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GuardStatistics {
    /// Total violations detected
    pub violation_count: u32,

    /// Total actions blocked
    pub blocked_count: u32,

    /// Current enforcement level
    pub enforcement_level: EnforcementLevel,

    /// Number of active policy rules
    pub active_rules: usize,

    /// Number of blocked patterns
    pub blocked_patterns: usize,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runners::{create_exec_context, Scope};
    use serde_json::json;
    use smith_protocol::ExecutionLimits;
    use tempfile::tempdir;

    fn create_test_context() -> ExecContext {
        let temp_dir = tempdir().unwrap();
        let limits = ExecutionLimits {
            cpu_ms_per_100ms: 50,
            mem_bytes: 100_000_000,
            io_bytes: 10_000_000,
            pids_max: 10,
            timeout_ms: 30_000,
        };
        let scope = Scope {
            paths: vec![temp_dir.path().to_string_lossy().to_string()],
            urls: vec![],
        };
        create_exec_context(temp_dir.path(), limits, scope, "test-trace-id".to_string())
    }

    #[tokio::test]
    async fn test_guard_creation() {
        let ctx = create_test_context();
        let guard = Guard::new(&ctx).unwrap();

        assert_eq!(guard.config.enforcement_level, EnforcementLevel::Strict);
        assert!(!guard.config.policy_rules.is_empty());
        assert!(!guard.config.allowed_capabilities.is_empty());
    }

    #[tokio::test]
    async fn test_capability_validation() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        // Test allowed capability
        let allowed_action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/tmp/test.txt"}),
            "Read test file".to_string(),
        );

        let result = guard.validate_action(&allowed_action).await.unwrap();
        assert!(result);

        // Test disallowed capability
        let disallowed_action = WorkflowAction::new(
            ActionType::Custom("dangerous.capability.v1".to_string()),
            json!({}),
            "Dangerous action".to_string(),
        );

        let result = guard.validate_action(&disallowed_action).await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_path_validation() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        // Test allowed path
        let allowed_action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/tmp/allowed.txt"}),
            "Read allowed file".to_string(),
        );

        let result = guard.validate_action(&allowed_action).await.unwrap();
        assert!(result);

        // Test blocked path
        let blocked_action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/etc/passwd"}),
            "Read password file".to_string(),
        );

        let result = guard.validate_action(&blocked_action).await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_command_validation() {
        let ctx = create_test_context();

        // Create a custom config that allows shell commands
        let mut config = GuardConfig::default();
        config
            .allowed_capabilities
            .insert("shell.exec.v1".to_string());

        let mut guard = Guard::with_config(&ctx, config).unwrap();

        // Test safe command
        let safe_action = WorkflowAction::new(
            ActionType::Shell("shell.exec.v1".to_string()),
            json!({"command": "echo hello"}),
            "Safe echo command".to_string(),
        );

        let result = guard.validate_action(&safe_action).await.unwrap();
        assert!(result);

        // Test dangerous command
        let dangerous_action = WorkflowAction::new(
            ActionType::Shell("shell.exec.v1".to_string()),
            json!({"command": "rm -rf /"}),
            "Dangerous delete command".to_string(),
        );

        let result = guard.validate_action(&dangerous_action).await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_detailed_validation() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        let action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/etc/passwd"}),
            "Read password file".to_string(),
        );

        let result = guard.validate_action_detailed(&action).await.unwrap();

        assert!(!result.allowed);
        assert!(!result.violations.is_empty());
        assert!(!result.reason.is_empty());
    }

    #[tokio::test]
    async fn test_enforcement_levels() {
        let ctx = create_test_context();

        // Test strict enforcement
        let mut strict_config = GuardConfig::default();
        strict_config.enforcement_level = EnforcementLevel::Strict;
        let mut strict_guard = Guard::with_config(&ctx, strict_config).unwrap();

        let blocked_action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/etc/passwd"}),
            "Read password file".to_string(),
        );

        assert!(!strict_guard.validate_action(&blocked_action).await.unwrap());

        // Test permissive enforcement
        let mut permissive_config = GuardConfig::default();
        permissive_config.enforcement_level = EnforcementLevel::Permissive;
        let mut permissive_guard = Guard::with_config(&ctx, permissive_config).unwrap();

        assert!(permissive_guard
            .validate_action(&blocked_action)
            .await
            .unwrap());

        // Test disabled enforcement
        let mut disabled_config = GuardConfig::default();
        disabled_config.enforcement_level = EnforcementLevel::Disabled;
        let mut disabled_guard = Guard::with_config(&ctx, disabled_config).unwrap();

        assert!(disabled_guard
            .validate_action(&blocked_action)
            .await
            .unwrap());
    }

    #[tokio::test]
    async fn test_resource_limits() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        // Test action with excessive timeout
        let mut long_action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/tmp/test.txt"}),
            "Long running read".to_string(),
        );
        long_action.timeout_ms = Some(7200000); // 2 hours

        let result = guard.validate_action(&long_action).await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_statistics() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        let stats = guard.get_statistics();
        assert_eq!(stats.violation_count, 0);
        assert_eq!(stats.blocked_count, 0);

        // Trigger some violations
        let blocked_action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/etc/passwd"}),
            "Read password file".to_string(),
        );

        guard.validate_action(&blocked_action).await.unwrap();

        let updated_stats = guard.get_statistics();
        assert!(updated_stats.violation_count > 0);
        assert!(updated_stats.blocked_count > 0);

        // Test reset
        guard.reset_statistics();
        let reset_stats = guard.get_statistics();
        assert_eq!(reset_stats.violation_count, 0);
        assert_eq!(reset_stats.blocked_count, 0);
    }

    // === Serialization tests ===

    #[test]
    fn test_enforcement_level_serialization() {
        let levels = vec![
            (EnforcementLevel::Strict, "strict"),
            (EnforcementLevel::Permissive, "permissive"),
            (EnforcementLevel::Monitor, "monitor"),
            (EnforcementLevel::Disabled, "disabled"),
        ];

        for (level, expected) in levels {
            let json = serde_json::to_string(&level).unwrap();
            assert!(json.contains(expected));
            let parsed: EnforcementLevel = serde_json::from_str(&json).unwrap();
            assert_eq!(parsed, level);
        }
    }

    #[test]
    fn test_policy_rule_type_serialization() {
        let types = vec![
            PolicyRuleType::PathValidation,
            PolicyRuleType::NetworkValidation,
            PolicyRuleType::CommandValidation,
            PolicyRuleType::ResourceValidation,
            PolicyRuleType::ContentValidation,
            PolicyRuleType::CapabilityValidation,
        ];

        for rule_type in types {
            let json = serde_json::to_string(&rule_type).unwrap();
            let parsed: PolicyRuleType = serde_json::from_str(&json).unwrap();
            assert_eq!(parsed, rule_type);
        }
    }

    #[test]
    fn test_policy_action_serialization() {
        let actions = vec![
            PolicyAction::Block,
            PolicyAction::Validate,
            PolicyAction::Limit,
            PolicyAction::Transform,
            PolicyAction::Log,
        ];

        for action in actions {
            let json = serde_json::to_string(&action).unwrap();
            let parsed: PolicyAction = serde_json::from_str(&json).unwrap();
            assert_eq!(parsed, action);
        }
    }

    #[test]
    fn test_pattern_type_serialization() {
        let types = vec![
            PatternType::Path,
            PatternType::Url,
            PatternType::Command,
            PatternType::Content,
            PatternType::Parameter,
        ];

        for pattern_type in types {
            let json = serde_json::to_string(&pattern_type).unwrap();
            let parsed: PatternType = serde_json::from_str(&json).unwrap();
            assert_eq!(parsed, pattern_type);
        }
    }

    #[test]
    fn test_isolation_feature_serialization() {
        let features = vec![
            IsolationFeature::ProcessNamespace,
            IsolationFeature::NetworkNamespace,
            IsolationFeature::FileSystemRestrictions,
            IsolationFeature::ResourceLimits,
            IsolationFeature::SeccompFiltering,
            IsolationFeature::LandlockRestrictions,
        ];

        for feature in features {
            let json = serde_json::to_string(&feature).unwrap();
            let parsed: IsolationFeature = serde_json::from_str(&json).unwrap();
            assert_eq!(parsed, feature);
        }
    }

    #[test]
    fn test_audit_level_serialization() {
        let levels = vec![
            AuditLevel::None,
            AuditLevel::Basic,
            AuditLevel::Standard,
            AuditLevel::Full,
            AuditLevel::Forensic,
        ];

        for level in levels {
            let json = serde_json::to_string(&level).unwrap();
            let parsed: AuditLevel = serde_json::from_str(&json).unwrap();
            assert_eq!(parsed, level);
        }
    }

    #[test]
    fn test_guard_config_default() {
        let config = GuardConfig::default();
        assert_eq!(config.enforcement_level, EnforcementLevel::Strict);
        assert!(!config.policy_rules.is_empty());
        assert!(!config.allowed_capabilities.is_empty());
        assert!(!config.blocked_patterns.is_empty());
    }

    #[test]
    fn test_guard_resource_limits_default() {
        let limits = GuardResourceLimits::default();
        assert_eq!(limits.max_actions, 100);
        assert_eq!(limits.max_duration_seconds, 3600);
        assert_eq!(limits.max_memory_mb, 512);
        assert_eq!(limits.max_file_size_mb, 100);
        assert_eq!(limits.max_network_payload_mb, 50);
    }

    #[test]
    fn test_security_context_default() {
        let ctx = SecurityContext::default();
        assert!(ctx.required_sandbox);
        assert!(ctx.network_allowed);
        assert!(ctx.filesystem_allowed);
        assert_eq!(ctx.audit_level, AuditLevel::Full);
        assert!(!ctx.required_isolation.is_empty());
    }

    #[test]
    fn test_policy_condition_serialization_path_allow_list() {
        let condition = PolicyCondition::PathAllowList(vec!["/tmp/".to_string()]);
        let json = serde_json::to_string(&condition).unwrap();
        assert!(json.contains("PathAllowList"));
        let parsed: PolicyCondition = serde_json::from_str(&json).unwrap();
        if let PolicyCondition::PathAllowList(paths) = parsed {
            assert_eq!(paths.len(), 1);
        } else {
            panic!("Expected PathAllowList");
        }
    }

    #[test]
    fn test_policy_condition_serialization_path_block_list() {
        let condition = PolicyCondition::PathBlockList(vec!["/etc/".to_string()]);
        let json = serde_json::to_string(&condition).unwrap();
        let parsed: PolicyCondition = serde_json::from_str(&json).unwrap();
        if let PolicyCondition::PathBlockList(paths) = parsed {
            assert_eq!(paths.len(), 1);
        } else {
            panic!("Expected PathBlockList");
        }
    }

    #[test]
    fn test_policy_condition_serialization_url_pattern() {
        let condition = PolicyCondition::UrlPattern("https://.*".to_string());
        let json = serde_json::to_string(&condition).unwrap();
        let parsed: PolicyCondition = serde_json::from_str(&json).unwrap();
        if let PolicyCondition::UrlPattern(pattern) = parsed {
            assert_eq!(pattern, "https://.*");
        } else {
            panic!("Expected UrlPattern");
        }
    }

    #[test]
    fn test_policy_condition_serialization_command_block_list() {
        let condition = PolicyCondition::CommandBlockList(vec!["rm".to_string()]);
        let json = serde_json::to_string(&condition).unwrap();
        let parsed: PolicyCondition = serde_json::from_str(&json).unwrap();
        if let PolicyCondition::CommandBlockList(commands) = parsed {
            assert_eq!(commands.len(), 1);
        } else {
            panic!("Expected CommandBlockList");
        }
    }

    #[test]
    fn test_policy_condition_serialization_max_actions() {
        let condition = PolicyCondition::MaxActions(50);
        let json = serde_json::to_string(&condition).unwrap();
        let parsed: PolicyCondition = serde_json::from_str(&json).unwrap();
        if let PolicyCondition::MaxActions(max) = parsed {
            assert_eq!(max, 50);
        } else {
            panic!("Expected MaxActions");
        }
    }

    #[test]
    fn test_policy_condition_serialization_max_duration() {
        let condition = PolicyCondition::MaxDuration(1800);
        let json = serde_json::to_string(&condition).unwrap();
        let parsed: PolicyCondition = serde_json::from_str(&json).unwrap();
        if let PolicyCondition::MaxDuration(duration) = parsed {
            assert_eq!(duration, 1800);
        } else {
            panic!("Expected MaxDuration");
        }
    }

    #[test]
    fn test_policy_condition_serialization_required_capability() {
        let condition = PolicyCondition::RequiredCapability("fs.read.v1".to_string());
        let json = serde_json::to_string(&condition).unwrap();
        let parsed: PolicyCondition = serde_json::from_str(&json).unwrap();
        if let PolicyCondition::RequiredCapability(cap) = parsed {
            assert_eq!(cap, "fs.read.v1");
        } else {
            panic!("Expected RequiredCapability");
        }
    }

    #[test]
    fn test_policy_condition_serialization_content_pattern() {
        let condition = PolicyCondition::ContentPattern("password=.*".to_string());
        let json = serde_json::to_string(&condition).unwrap();
        let parsed: PolicyCondition = serde_json::from_str(&json).unwrap();
        if let PolicyCondition::ContentPattern(pattern) = parsed {
            assert_eq!(pattern, "password=.*");
        } else {
            panic!("Expected ContentPattern");
        }
    }

    #[test]
    fn test_policy_rule_serialization() {
        let rule = PolicyRule {
            name: "test_rule".to_string(),
            rule_type: PolicyRuleType::PathValidation,
            conditions: vec![PolicyCondition::PathAllowList(vec!["/tmp/".to_string()])],
            severity: RiskLevel::Medium,
            action: PolicyAction::Block,
        };

        let json = serde_json::to_string(&rule).unwrap();
        assert!(json.contains("test_rule"));
        let parsed: PolicyRule = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.name, "test_rule");
        assert_eq!(parsed.action, PolicyAction::Block);
    }

    #[test]
    fn test_blocked_pattern_serialization() {
        let pattern = BlockedPattern {
            pattern: "/etc/.*".to_string(),
            pattern_type: PatternType::Path,
            reason: "System files blocked".to_string(),
        };

        let json = serde_json::to_string(&pattern).unwrap();
        assert!(json.contains("System files blocked"));
        let parsed: BlockedPattern = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.pattern_type, PatternType::Path);
    }

    #[test]
    fn test_guard_resource_limits_serialization() {
        let limits = GuardResourceLimits {
            max_actions: 200,
            max_duration_seconds: 7200,
            max_memory_mb: 1024,
            max_file_size_mb: 200,
            max_network_payload_mb: 100,
        };

        let json = serde_json::to_string(&limits).unwrap();
        let parsed: GuardResourceLimits = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.max_actions, 200);
        assert_eq!(parsed.max_memory_mb, 1024);
    }

    #[test]
    fn test_security_context_serialization() {
        let ctx = SecurityContext {
            required_sandbox: false,
            required_isolation: vec![IsolationFeature::ProcessNamespace],
            network_allowed: false,
            filesystem_allowed: true,
            audit_level: AuditLevel::Forensic,
        };

        let json = serde_json::to_string(&ctx).unwrap();
        let parsed: SecurityContext = serde_json::from_str(&json).unwrap();
        assert!(!parsed.required_sandbox);
        assert!(!parsed.network_allowed);
        assert_eq!(parsed.audit_level, AuditLevel::Forensic);
    }

    #[test]
    fn test_guard_statistics_serialization() {
        let stats = GuardStatistics {
            violation_count: 10,
            blocked_count: 5,
            enforcement_level: EnforcementLevel::Strict,
            active_rules: 4,
            blocked_patterns: 3,
        };

        let json = serde_json::to_string(&stats).unwrap();
        let parsed: GuardStatistics = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.violation_count, 10);
        assert_eq!(parsed.blocked_count, 5);
    }

    #[test]
    fn test_guard_config_serialization() {
        let config = GuardConfig::default();
        let json = serde_json::to_string(&config).unwrap();
        let parsed: GuardConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.enforcement_level, config.enforcement_level);
    }

    // === Additional edge case tests ===

    #[tokio::test]
    async fn test_monitor_enforcement_level() {
        let ctx = create_test_context();
        let mut monitor_config = GuardConfig::default();
        monitor_config.enforcement_level = EnforcementLevel::Monitor;
        let mut guard = Guard::with_config(&ctx, monitor_config).unwrap();

        let blocked_action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/etc/passwd"}),
            "Read password file".to_string(),
        );

        // Monitor mode should allow the action but log violations
        let result = guard.validate_action(&blocked_action).await.unwrap();
        assert!(result);
    }

    #[tokio::test]
    async fn test_http_action_validation() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        let http_action = WorkflowAction::new(
            ActionType::Http("http.fetch.v1".to_string()),
            json!({"url": "https://example.com/api"}),
            "Fetch API data".to_string(),
        );

        let result = guard.validate_action(&http_action).await.unwrap();
        assert!(result);
    }

    #[tokio::test]
    async fn test_research_action_validation() {
        let ctx = create_test_context();
        let mut config = GuardConfig::default();
        config
            .allowed_capabilities
            .insert("research.v1".to_string());
        let mut guard = Guard::with_config(&ctx, config).unwrap();

        let research_action = WorkflowAction::new(
            ActionType::Research("research.v1".to_string()),
            json!({"query": "test research"}),
            "Research task".to_string(),
        );

        let result = guard.validate_action(&research_action).await.unwrap();
        assert!(result);
    }

    #[tokio::test]
    async fn test_planning_action_validation() {
        let ctx = create_test_context();
        let mut config = GuardConfig::default();
        config
            .allowed_capabilities
            .insert("planning.v1".to_string());
        let mut guard = Guard::with_config(&ctx, config).unwrap();

        let planning_action = WorkflowAction::new(
            ActionType::Planning("planning.v1".to_string()),
            json!({"goal": "test planning"}),
            "Planning task".to_string(),
        );

        let result = guard.validate_action(&planning_action).await.unwrap();
        assert!(result);
    }

    #[tokio::test]
    async fn test_analysis_action_validation() {
        let ctx = create_test_context();
        let mut config = GuardConfig::default();
        config
            .allowed_capabilities
            .insert("analysis.v1".to_string());
        let mut guard = Guard::with_config(&ctx, config).unwrap();

        let analysis_action = WorkflowAction::new(
            ActionType::Analysis("analysis.v1".to_string()),
            json!({"data": "test analysis"}),
            "Analysis task".to_string(),
        );

        let result = guard.validate_action(&analysis_action).await.unwrap();
        assert!(result);
    }

    #[tokio::test]
    async fn test_file_size_limit_validation() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        let large_file_action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/tmp/test.txt", "max_bytes": 200 * 1024 * 1024}), // 200MB
            "Read large file".to_string(),
        );

        let result = guard.validate_action(&large_file_action).await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_proc_filesystem_blocked() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        let proc_action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/proc/1/status"}),
            "Read proc file".to_string(),
        );

        let result = guard.validate_action(&proc_action).await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_exe_file_blocked() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        let exe_action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/tmp/malware.exe"}),
            "Read exe file".to_string(),
        );

        let result = guard.validate_action(&exe_action).await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_workspace_path_allowed() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        let workspace_action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/workspace/project/file.txt"}),
            "Read workspace file".to_string(),
        );

        let result = guard.validate_action(&workspace_action).await.unwrap();
        assert!(result);
    }

    #[tokio::test]
    async fn test_var_tmp_path_allowed() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        let var_tmp_action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/var/tmp/test.txt"}),
            "Read var/tmp file".to_string(),
        );

        let result = guard.validate_action(&var_tmp_action).await.unwrap();
        assert!(result);
    }

    #[tokio::test]
    async fn test_sudo_command_blocked() {
        let ctx = create_test_context();
        let mut config = GuardConfig::default();
        config
            .allowed_capabilities
            .insert("shell.exec.v1".to_string());
        let mut guard = Guard::with_config(&ctx, config).unwrap();

        let sudo_action = WorkflowAction::new(
            ActionType::Shell("shell.exec.v1".to_string()),
            json!({"command": "sudo apt-get install something"}),
            "Sudo command".to_string(),
        );

        let result = guard.validate_action(&sudo_action).await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_chmod_777_blocked() {
        let ctx = create_test_context();
        let mut config = GuardConfig::default();
        config
            .allowed_capabilities
            .insert("shell.exec.v1".to_string());
        let mut guard = Guard::with_config(&ctx, config).unwrap();

        let chmod_action = WorkflowAction::new(
            ActionType::Shell("shell.exec.v1".to_string()),
            json!({"command": "chmod 777 /tmp/file.txt"}),
            "Chmod 777 command".to_string(),
        );

        let result = guard.validate_action(&chmod_action).await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_dd_command_blocked() {
        let ctx = create_test_context();
        let mut config = GuardConfig::default();
        config
            .allowed_capabilities
            .insert("shell.exec.v1".to_string());
        let mut guard = Guard::with_config(&ctx, config).unwrap();

        let dd_action = WorkflowAction::new(
            ActionType::Shell("shell.exec.v1".to_string()),
            json!({"command": "dd if=/dev/zero of=/tmp/file bs=1M count=100"}),
            "DD command".to_string(),
        );

        let result = guard.validate_action(&dd_action).await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_content_pattern_credential_detection() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        let cred_action = WorkflowAction::new(
            ActionType::FileSystem("fs.write.v1".to_string()),
            json!({"path": "/tmp/config.txt", "content": "password = secret123"}),
            "Write credential".to_string(),
        );

        let result = guard.validate_action(&cred_action).await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_action_with_no_path_parameter() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        let action_no_path = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"other_param": "value"}),
            "Action without path".to_string(),
        );

        // Should still be allowed (no path means no path validation applies)
        let result = guard.validate_action(&action_no_path).await.unwrap();
        assert!(result);
    }

    #[tokio::test]
    async fn test_action_without_timeout() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        let action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/tmp/test.txt"}),
            "Action without timeout".to_string(),
        );

        let result = guard.validate_action(&action).await.unwrap();
        assert!(result);
    }

    #[tokio::test]
    async fn test_multiple_violations() {
        let ctx = create_test_context();
        let mut guard = Guard::new(&ctx).unwrap();

        let multi_violation_action = WorkflowAction::new(
            ActionType::Custom("dangerous.capability.v1".to_string()),
            json!({"path": "/etc/passwd", "content": "password = test"}),
            "Multiple violations".to_string(),
        );

        let result = guard
            .validate_action_detailed(&multi_violation_action)
            .await
            .unwrap();
        assert!(!result.allowed);
        assert!(result.violations.len() > 1);
    }

    #[tokio::test]
    async fn test_custom_config_with_additional_capability() {
        let ctx = create_test_context();
        let mut config = GuardConfig::default();
        config
            .allowed_capabilities
            .insert("custom.capability.v1".to_string());

        let mut guard = Guard::with_config(&ctx, config).unwrap();

        let custom_action = WorkflowAction::new(
            ActionType::Custom("custom.capability.v1".to_string()),
            json!({}),
            "Custom action".to_string(),
        );

        let result = guard.validate_action(&custom_action).await.unwrap();
        assert!(result);
    }

    #[tokio::test]
    async fn test_security_context_network_not_allowed() {
        let ctx = create_test_context();
        let mut config = GuardConfig::default();
        config.security_context.network_allowed = false;

        let mut guard = Guard::with_config(&ctx, config).unwrap();

        // Any action should fail due to network namespace check
        let action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/tmp/test.txt"}),
            "Read file".to_string(),
        );

        let result = guard.validate_action(&action).await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_security_context_filesystem_not_allowed() {
        let ctx = create_test_context();
        let mut config = GuardConfig::default();
        config.security_context.filesystem_allowed = false;

        let mut guard = Guard::with_config(&ctx, config).unwrap();

        let action = WorkflowAction::new(
            ActionType::FileSystem("fs.read.v1".to_string()),
            json!({"path": "/tmp/test.txt"}),
            "Read file".to_string(),
        );

        let result = guard.validate_action(&action).await.unwrap();
        assert!(!result);
    }
}