lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
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
//! Security and sandboxing features for I/O operations.
//!
//! This module provides:
//! - File access control and permission management
//! - Resource limits (file descriptors, bandwidth, disk space)
//! - Chroot/jail support for process isolation
//! - I/O operation auditing and logging
//! - Secure file handling with validation

use crate::diagnostics::{Error as DiagnosticError, Result};
use crate::eval::value::{
    Value, PrimitiveProcedure, PrimitiveImpl, ThreadSafeEnvironment
};
use crate::effects::Effect;
use std::sync::{Arc, Mutex, RwLock};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use std::sync::LazyLock;

#[cfg(all(unix, feature = "advanced-io"))]
use nix::unistd::{chroot, chdir};
#[cfg(unix)]
/// Security policy for I/O operations
#[derive(Debug, Clone)]
pub struct SecurityPolicy {
    pub allowed_paths: HashSet<PathBuf>,
    pub forbidden_paths: HashSet<PathBuf>,
    pub max_file_size: Option<u64>,
    pub max_bandwidth: Option<u64>, // bytes per second
    pub max_open_files: Option<usize>,
    pub audit_enabled: bool,
    pub strict_mode: bool,
}

impl Default for SecurityPolicy {
    fn default() -> Self {
        SecurityPolicy {
            allowed_paths: HashSet::new(),
            forbidden_paths: HashSet::new(),
            max_file_size: Some(100 * 1024 * 1024), // 100MB default
            max_bandwidth: Some(10 * 1024 * 1024), // 10MB/s default
            max_open_files: Some(1024),
            audit_enabled: true,
            strict_mode: false,
        }
    }
}

/// Resource usage tracking
#[derive(Debug, Clone)]
pub struct ResourceUsage {
    pub open_files: usize,
    pub bytes_read: u64,
    pub bytes_written: u64,
    pub operations_count: u64,
    pub last_reset: Instant,
    pub bandwidth_window: Duration,
    pub recent_transfers: Vec<(Instant, u64)>, // (timestamp, bytes)
}

impl Default for ResourceUsage {
    fn default() -> Self {
        ResourceUsage {
            open_files: 0,
            bytes_read: 0,
            bytes_written: 0,
            operations_count: 0,
            last_reset: Instant::now(),
            bandwidth_window: Duration::from_secs(1),
            recent_transfers: Vec::new(),
        }
    }
}

/// Audit log entry
#[derive(Debug, Clone)]
pub struct AuditEntry {
    pub timestamp: Instant,
    pub operation: String,
    pub path: Option<PathBuf>,
    pub user_data: Option<String>,
    pub success: bool,
    pub error_message: Option<String>,
}

/// Security manager for I/O operations
#[derive(Debug, Default)]
pub struct SecurityManager {
    pub policy: Arc<RwLock<SecurityPolicy>>,
    pub usage: Arc<Mutex<ResourceUsage>>,
    pub audit_log: Arc<Mutex<Vec<AuditEntry>>>,
    pub sandbox_active: bool,
    pub chroot_path: Option<PathBuf>,
}

impl SecurityManager {
    pub fn new() -> Self {
        SecurityManager {
            policy: Arc::new(RwLock::new(SecurityPolicy::default())),
            usage: Arc::new(Mutex::new(ResourceUsage::default())),
            audit_log: Arc::new(Mutex::new(Vec::new())),
            sandbox_active: false,
            chroot_path: None,
        }
    }
    
    pub fn with_policy(self, policy: SecurityPolicy) -> Self {
        *self.policy.write().unwrap() = policy;
        self
    }
    
    pub fn check_path_access(&self, path: &Path, operation: &str) -> Result<()> {
        let policy = self.policy.read().unwrap();
        
        // Normalize path
        let canonical_path = match path.canonicalize() {
            Ok(p) => p,
            Err(_) => {
                if policy.strict_mode {
                    return Err(Box::new(DiagnosticError::runtime_error(
                        format!("Cannot access non-existent path: {}", path.display()),
                        None,
                    )));
                } else {
                    path.to_path_buf()
                }
            }
        };
        
        // Check forbidden paths first
        for forbidden in &policy.forbidden_paths {
            if canonical_path.starts_with(forbidden) {
                self.log_audit_entry(AuditEntry {
                    timestamp: Instant::now(),
                    operation: operation.to_string(),
                    path: Some(canonical_path.clone()),
                    user_data: None,
                    success: false,
                    error_message: Some("Path is forbidden".to_string()),
                });
                
                return Err(Box::new(DiagnosticError::runtime_error(
                    format!("Access denied to forbidden path: {}", canonical_path.display()),
                    None,
                )));
            }
        }
        
        // Check allowed paths
        if !policy.allowed_paths.is_empty() {
            let mut allowed = false;
            for allowed_path in &policy.allowed_paths {
                if canonical_path.starts_with(allowed_path) {
                    allowed = true;
                    break;
                }
            }
            
            if !allowed {
                self.log_audit_entry(AuditEntry {
                    timestamp: Instant::now(),
                    operation: operation.to_string(),
                    path: Some(canonical_path.clone()),
                    user_data: None,
                    success: false,
                    error_message: Some("Path not in allowed list".to_string()),
                });
                
                return Err(Box::new(DiagnosticError::runtime_error(
                    format!("Access denied to path not in allowed list: {}", canonical_path.display()),
                    None,
                )));
            }
        }
        
        Ok(())
    }
    
    pub fn check_file_size_limit(&self, size: u64) -> Result<()> {
        let policy = self.policy.read().unwrap();
        
        if let Some(max_size) = policy.max_file_size {
            if size > max_size {
                return Err(Box::new(DiagnosticError::runtime_error(
                    format!("File size {size} exceeds limit {max_size}"),
                    None,
                )));
            }
        }
        
        Ok(())
    }
    
    pub fn check_bandwidth_limit(&self, bytes: u64) -> Result<()> {
        let policy = self.policy.read().unwrap();
        
        if let Some(max_bandwidth) = policy.max_bandwidth {
            let mut usage = self.usage.lock().unwrap();
            let now = Instant::now();
            
            // Clean up old entries
            let bandwidth_window = usage.bandwidth_window;
            usage.recent_transfers.retain(|(timestamp, _)| {
                now.duration_since(*timestamp) <= bandwidth_window
            });
            
            // Calculate current bandwidth usage
            let current_usage: u64 = usage.recent_transfers.iter()
                .map(|(_, bytes)| *bytes)
                .sum();
            
            if current_usage + bytes > max_bandwidth {
                return Err(Box::new(DiagnosticError::runtime_error(
                    format!("Bandwidth limit exceeded: {current_usage} + {bytes} > {max_bandwidth}"),
                    None,
                )));
            }
            
            // Record this transfer
            usage.recent_transfers.push((now, bytes));
        }
        
        Ok(())
    }
    
    pub fn check_open_file_limit(&self) -> Result<()> {
        let policy = self.policy.read().unwrap();
        let usage = self.usage.lock().unwrap();
        
        if let Some(max_files) = policy.max_open_files {
            if usage.open_files >= max_files {
                return Err(Box::new(DiagnosticError::runtime_error(
                    format!("Open file limit exceeded: {open_files} >= {max_files}", open_files = usage.open_files),
                    None,
                )));
            }
        }
        
        Ok(())
    }
    
    pub fn track_file_opened(&self) {
        let mut usage = self.usage.lock().unwrap();
        usage.open_files += 1;
        usage.operations_count += 1;
    }
    
    pub fn track_file_closed(&self) {
        let mut usage = self.usage.lock().unwrap();
        if usage.open_files > 0 {
            usage.open_files -= 1;
        }
    }
    
    pub fn track_bytes_read(&self, bytes: u64) {
        let mut usage = self.usage.lock().unwrap();
        usage.bytes_read += bytes;
        usage.operations_count += 1;
    }
    
    pub fn track_bytes_written(&self, bytes: u64) {
        let mut usage = self.usage.lock().unwrap();
        usage.bytes_written += bytes;
        usage.operations_count += 1;
    }
    
    fn log_audit_entry(&self, entry: AuditEntry) {
        let policy = self.policy.read().unwrap();
        if policy.audit_enabled {
            let mut audit_log = self.audit_log.lock().unwrap();
            audit_log.push(entry);
            
            // Limit audit log size
            if audit_log.len() > 10000 {
                audit_log.drain(0..1000);
            }
        }
    }
    
    pub fn enable_sandbox(&mut self, chroot_path: Option<PathBuf>) -> Result<()> {
        #[cfg(all(unix, feature = "advanced-io"))]
        {
            if let Some(ref path) = chroot_path {
                // Change to chroot directory first
                chdir(path).map_err(|e| {
                    DiagnosticError::runtime_error(
                        format!("Cannot change to chroot directory '{}': {e}", path.display()),
                        None,
                    )
                })?;
                
                // Apply chroot
                chroot(path).map_err(|e| {
                    DiagnosticError::runtime_error(
                        format!("Cannot apply chroot to '{}': {e}", path.display()),
                        None,
                    )
                })?;
                
                self.chroot_path = Some(path.clone());
            }
            
            self.sandbox_active = true;
            Ok(())
        }
        
        // Fallback for Unix systems without advanced-io feature - sandbox is not available
        #[cfg(all(unix, not(feature = "advanced-io")))]
        {
            if chroot_path.is_some() {
                return Err(Box::new(DiagnosticError::runtime_error(
                    "Sandboxing with chroot requires 'advanced-io' feature".to_string(),
                    None,
                )));
            }
            self.sandbox_active = false;
            Ok(())
        }
        
        #[cfg(not(unix))]
        {
            if chroot_path.is_some() {
                return Err(DiagnosticError::runtime_error(
                    "Chroot sandboxing not supported on this platform".to_string(),
                    None,
                ));
            }
            
            // Enable basic sandboxing without chroot
            self.sandbox_active = true;
            Ok(())
        }
    }
}

/// Global security manager instance
static SECURITY_MANAGER: LazyLock<Mutex<SecurityManager>> = LazyLock::new(|| Mutex::new(SecurityManager::new()));

pub fn get_security_manager() -> &'static Mutex<SecurityManager> {
    &SECURITY_MANAGER
}

/// Creates security and sandboxing operation bindings.
pub fn create_security_io_bindings(env: &Arc<ThreadSafeEnvironment>) {
    // Security policy management
    bind_security_policy_operations(env);
    
    // Resource management
    bind_resource_management_operations(env);
    
    // Sandboxing operations
    bind_sandbox_operations(env);
    
    // Auditing operations
    bind_audit_operations(env);
    
    // Secure file operations
    bind_secure_file_operations(env);
}

// ============= SECURITY POLICY OPERATIONS =============

fn bind_security_policy_operations(env: &Arc<ThreadSafeEnvironment>) {
    // set-security-policy
    env.define("set-security-policy".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "set-security-policy".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_set_security_policy),
        effects: vec![Effect::IO],
    })));
    
    // get-security-policy
    env.define("get-security-policy".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "get-security-policy".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_get_security_policy),
        effects: vec![Effect::IO],
    })));
    
    // add-allowed-path
    env.define("add-allowed-path".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "add-allowed-path".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_add_allowed_path),
        effects: vec![Effect::IO],
    })));
    
    // add-forbidden-path
    env.define("add-forbidden-path".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "add-forbidden-path".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_add_forbidden_path),
        effects: vec![Effect::IO],
    })));
    
    // check-path-access
    env.define("check-path-access".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "check-path-access".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_check_path_access),
        effects: vec![Effect::IO],
    })));
}

fn bind_resource_management_operations(env: &Arc<ThreadSafeEnvironment>) {
    // set-resource-limits
    env.define("set-resource-limits".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "set-resource-limits".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_set_resource_limits),
        effects: vec![Effect::IO],
    })));
    
    // get-resource-usage
    env.define("get-resource-usage".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "get-resource-usage".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_get_resource_usage),
        effects: vec![Effect::IO],
    })));
    
    // reset-resource-counters
    env.define("reset-resource-counters".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "reset-resource-counters".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_reset_resource_counters),
        effects: vec![Effect::IO],
    })));
}

fn bind_sandbox_operations(env: &Arc<ThreadSafeEnvironment>) {
    // enable-sandbox
    env.define("enable-sandbox".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "enable-sandbox".to_string(),
        arity_min: 0,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_enable_sandbox),
        effects: vec![Effect::IO],
    })));
    
    // sandbox-active?
    env.define("sandbox-active?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "sandbox-active?".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_sandbox_active_p),
        effects: vec![Effect::IO],
    })));
    
    // create-secure-environment
    env.define("create-secure-environment".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "create-secure-environment".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_create_secure_environment),
        effects: vec![Effect::IO],
    })));
}

fn bind_audit_operations(env: &Arc<ThreadSafeEnvironment>) {
    // enable-audit-logging
    env.define("enable-audit-logging".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "enable-audit-logging".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_enable_audit_logging),
        effects: vec![Effect::IO],
    })));
    
    // get-audit-log
    env.define("get-audit-log".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "get-audit-log".to_string(),
        arity_min: 0,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_get_audit_log),
        effects: vec![Effect::IO],
    })));
    
    // clear-audit-log
    env.define("clear-audit-log".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "clear-audit-log".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_clear_audit_log),
        effects: vec![Effect::IO],
    })));
}

fn bind_secure_file_operations(env: &Arc<ThreadSafeEnvironment>) {
    // secure-file-read
    env.define("secure-file-read".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "secure-file-read".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_secure_file_read),
        effects: vec![Effect::IO],
    })));
    
    // secure-file-write
    env.define("secure-file-write".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "secure-file-write".to_string(),
        arity_min: 2,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_secure_file_write),
        effects: vec![Effect::IO],
    })));
    
    // validate-file-path
    env.define("validate-file-path".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "validate-file-path".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_validate_file_path),
        effects: vec![Effect::Pure],
    })));
}

// ============= IMPLEMENTATION FUNCTIONS =============

// === Security Policy Operations ===

pub fn primitive_set_security_policy(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("set-security-policy expects 1 argument, got {args_len}", args_len = args.len()),
            None,
        )));
    }
    
    // Extract policy from hashtable
    match &args[0] {
        Value::Hashtable(policy_table) => {
            let table = policy_table.read().unwrap();
            let mut policy = SecurityPolicy::default();
            
            // Parse policy settings
            if let Some(Value::Literal(crate::ast::Literal::Boolean(strict))) = 
                table.get(&Value::Symbol(crate::utils::intern_symbol("strict-mode"))) {
                policy.strict_mode = *strict;
            }
            
            if let Some(Value::Literal(crate::ast::Literal::Boolean(audit))) = 
                table.get(&Value::Symbol(crate::utils::intern_symbol("audit-enabled"))) {
                policy.audit_enabled = *audit;
            }
            
            if let Some(max_size_val) = table.get(&Value::Symbol(crate::utils::intern_symbol("max-file-size"))) {
                if let Some(size) = extract_optional_integer(max_size_val) {
                    policy.max_file_size = Some(size as u64);
                }
            }
            
            if let Some(max_bandwidth_val) = table.get(&Value::Symbol(crate::utils::intern_symbol("max-bandwidth"))) {
                if let Some(bandwidth) = extract_optional_integer(max_bandwidth_val) {
                    policy.max_bandwidth = Some(bandwidth as u64);
                }
            }
            
            if let Some(max_files_val) = table.get(&Value::Symbol(crate::utils::intern_symbol("max-open-files"))) {
                if let Some(files) = extract_optional_integer(max_files_val) {
                    policy.max_open_files = Some(files as usize);
                }
            }
            
            // Update security manager
            let security_manager = get_security_manager();
            let manager = security_manager.lock().unwrap();
            *manager.policy.write().unwrap() = policy;
            
            Ok(Value::Unspecified)
        }
        _ => Err(Box::new(DiagnosticError::runtime_error(
            "set-security-policy requires hashtable argument".to_string(),
            None,
        ))),
    }
}

pub fn primitive_get_security_policy(_args: &[Value]) -> Result<Value> {
    let security_manager = get_security_manager();
    let manager = security_manager.lock().unwrap();
    let policy = manager.policy.read().unwrap();
    
    #[allow(clippy::mutable_key_type)]
    let mut result = HashMap::new();
    
    result.insert(
        Value::Symbol(crate::utils::intern_symbol("strict-mode")),
        Value::boolean(policy.strict_mode)
    );
    
    result.insert(
        Value::Symbol(crate::utils::intern_symbol("audit-enabled")),
        Value::boolean(policy.audit_enabled)
    );
    
    if let Some(max_size) = policy.max_file_size {
        result.insert(
            Value::Symbol(crate::utils::intern_symbol("max-file-size")),
            Value::integer(max_size as i64)
        );
    }
    
    if let Some(max_bandwidth) = policy.max_bandwidth {
        result.insert(
            Value::Symbol(crate::utils::intern_symbol("max-bandwidth")),
            Value::integer(max_bandwidth as i64)
        );
    }
    
    if let Some(max_files) = policy.max_open_files {
        result.insert(
            Value::Symbol(crate::utils::intern_symbol("max-open-files")),
            Value::integer(max_files as i64)
        );
    }
    
    result.insert(
        Value::Symbol(crate::utils::intern_symbol("allowed-paths")),
        {
            let paths: Vec<Value> = policy.allowed_paths.iter()
                .map(|p| Value::string(p.to_string_lossy().to_string()))
                .collect();
            list_to_value(paths)
        }
    );
    
    result.insert(
        Value::Symbol(crate::utils::intern_symbol("forbidden-paths")),
        {
            let paths: Vec<Value> = policy.forbidden_paths.iter()
                .map(|p| Value::string(p.to_string_lossy().to_string()))
                .collect();
            list_to_value(paths)
        }
    );
    
    Ok(Value::Hashtable(Arc::new(std::sync::RwLock::new(result))))
}

pub fn primitive_add_allowed_path(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("add-allowed-path expects 1 argument, got {args_len}", args_len = args.len()),
            None,
        )));
    }
    
    let path = extract_string(&args[0], "add-allowed-path")?;
    let path_buf = PathBuf::from(path);
    
    let security_manager = get_security_manager();
    let manager = security_manager.lock().unwrap();
    let mut policy = manager.policy.write().unwrap();
    policy.allowed_paths.insert(path_buf);
    
    Ok(Value::Unspecified)
}

pub fn primitive_add_forbidden_path(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("add-forbidden-path expects 1 argument, got {args_len}", args_len = args.len()),
            None,
        )));
    }
    
    let path = extract_string(&args[0], "add-forbidden-path")?;
    let path_buf = PathBuf::from(path);
    
    let security_manager = get_security_manager();
    let manager = security_manager.lock().unwrap();
    let mut policy = manager.policy.write().unwrap();
    policy.forbidden_paths.insert(path_buf);
    
    Ok(Value::Unspecified)
}

pub fn primitive_check_path_access(args: &[Value]) -> Result<Value> {
    if args.len() != 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("check-path-access expects 2 arguments, got {args_len}", args_len = args.len()),
            None,
        )));
    }
    
    let path = extract_string(&args[0], "check-path-access")?;
    let operation = extract_string(&args[1], "check-path-access")?;
    
    let security_manager = get_security_manager();
    let manager = security_manager.lock().unwrap();
    
    match manager.check_path_access(Path::new(&path), &operation) {
        Ok(()) => Ok(Value::boolean(true)),
        Err(_) => Ok(Value::boolean(false)),
    }
}

// === Resource Management Operations ===

pub fn primitive_set_resource_limits(_args: &[Value]) -> Result<Value> {
    // TODO: Implement resource limit setting
    Err(Box::new(DiagnosticError::runtime_error(
        "set-resource-limits not yet implemented".to_string(),
        None,
    )))
}

pub fn primitive_get_resource_usage(_args: &[Value]) -> Result<Value> {
    let security_manager = get_security_manager();
    let manager = security_manager.lock().unwrap();
    let usage = manager.usage.lock().unwrap();
    
    #[allow(clippy::mutable_key_type)]
    let mut result = HashMap::new();
    
    result.insert(
        Value::Symbol(crate::utils::intern_symbol("open-files")),
        Value::integer(usage.open_files as i64)
    );
    
    result.insert(
        Value::Symbol(crate::utils::intern_symbol("bytes-read")),
        Value::integer(usage.bytes_read as i64)
    );
    
    result.insert(
        Value::Symbol(crate::utils::intern_symbol("bytes-written")),
        Value::integer(usage.bytes_written as i64)
    );
    
    result.insert(
        Value::Symbol(crate::utils::intern_symbol("operations-count")),
        Value::integer(usage.operations_count as i64)
    );
    
    Ok(Value::Hashtable(Arc::new(std::sync::RwLock::new(result))))
}

pub fn primitive_reset_resource_counters(_args: &[Value]) -> Result<Value> {
    let security_manager = get_security_manager();
    let manager = security_manager.lock().unwrap();
    let mut usage = manager.usage.lock().unwrap();
    
    usage.bytes_read = 0;
    usage.bytes_written = 0;
    usage.operations_count = 0;
    usage.last_reset = Instant::now();
    usage.recent_transfers.clear();
    
    Ok(Value::Unspecified)
}

// === Sandbox Operations ===

pub fn primitive_enable_sandbox(args: &[Value]) -> Result<Value> {
    if args.len() > 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("enable-sandbox expects 0 or 1 arguments, got {args_len}", args_len = args.len()),
            None,
        )));
    }
    
    let chroot_path = if args.len() == 1 {
        Some(PathBuf::from(extract_string(&args[0], "enable-sandbox")?))
    } else {
        None
    };
    
    let security_manager = get_security_manager();
    let mut manager = security_manager.lock().unwrap();
    
    match manager.enable_sandbox(chroot_path) {
        Ok(()) => Ok(Value::Unspecified),
        Err(e) => Err(e),
    }
}

pub fn primitive_sandbox_active_p(_args: &[Value]) -> Result<Value> {
    let security_manager = get_security_manager();
    let manager = security_manager.lock().unwrap();
    Ok(Value::boolean(manager.sandbox_active))
}

pub fn primitive_create_secure_environment(_args: &[Value]) -> Result<Value> {
    // TODO: Implement secure environment creation
    Err(Box::new(DiagnosticError::runtime_error(
        "create-secure-environment not yet implemented".to_string(),
        None,
    )))
}

// === Audit Operations ===

pub fn primitive_enable_audit_logging(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("enable-audit-logging expects 1 argument, got {args_len}", args_len = args.len()),
            None,
        )));
    }
    
    let enabled = extract_boolean(&args[0], "enable-audit-logging")?;
    
    let security_manager = get_security_manager();
    let manager = security_manager.lock().unwrap();
    let mut policy = manager.policy.write().unwrap();
    policy.audit_enabled = enabled;
    
    Ok(Value::Unspecified)
}

pub fn primitive_get_audit_log(args: &[Value]) -> Result<Value> {
    if args.len() > 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("get-audit-log expects 0 to 2 arguments, got {args_len}", args_len = args.len()),
            None,
        )));
    }
    
    let limit = if !args.is_empty() {
        Some(extract_integer(&args[0], "get-audit-log")? as usize)
    } else {
        None
    };
    
    let _filter = if args.len() > 1 {
        Some(extract_string(&args[1], "get-audit-log")?)
    } else {
        None
    };
    
    let security_manager = get_security_manager();
    let manager = security_manager.lock().unwrap();
    let audit_log = manager.audit_log.lock().unwrap();
    
    let entries_to_return = if let Some(limit) = limit {
        audit_log.iter().rev().take(limit).collect::<Vec<_>>()
    } else {
        audit_log.iter().collect::<Vec<_>>()
    };
    
    let audit_entries: Vec<Value> = entries_to_return.into_iter().rev().map(|entry| {
        #[allow(clippy::mutable_key_type)]
        let mut entry_map = HashMap::new();
        
        entry_map.insert(
            Value::Symbol(crate::utils::intern_symbol("operation")),
            Value::string(entry.operation.clone())
        );
        
        if let Some(ref path) = entry.path {
            entry_map.insert(
                Value::Symbol(crate::utils::intern_symbol("path")),
                Value::string(path.to_string_lossy().to_string())
            );
        }
        
        entry_map.insert(
            Value::Symbol(crate::utils::intern_symbol("success")),
            Value::boolean(entry.success)
        );
        
        if let Some(ref error) = entry.error_message {
            entry_map.insert(
                Value::Symbol(crate::utils::intern_symbol("error")),
                Value::string(error.clone())
            );
        }
        
        Value::Hashtable(Arc::new(std::sync::RwLock::new(entry_map)))
    }).collect();
    
    Ok(list_to_value(audit_entries))
}

pub fn primitive_clear_audit_log(_args: &[Value]) -> Result<Value> {
    let security_manager = get_security_manager();
    let manager = security_manager.lock().unwrap();
    let mut audit_log = manager.audit_log.lock().unwrap();
    audit_log.clear();
    
    Ok(Value::Unspecified)
}

// === Secure File Operations ===

pub fn primitive_secure_file_read(args: &[Value]) -> Result<Value> {
    if args.is_empty() || args.len() > 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("secure-file-read expects 1 or 2 arguments, got {args_len}", args_len = args.len()),
            None,
        )));
    }
    
    let path = extract_string(&args[0], "secure-file-read")?;
    let as_binary = if args.len() > 1 {
        extract_boolean(&args[1], "secure-file-read")?
    } else {
        false
    };
    
    let security_manager = get_security_manager();
    let manager = security_manager.lock().unwrap();
    
    // Check path access
    manager.check_path_access(Path::new(&path), "read")?;
    
    // Check file size limit
    match std::fs::metadata(&path) {
        Ok(metadata) => {
            manager.check_file_size_limit(metadata.len())?;
            manager.check_bandwidth_limit(metadata.len())?;
        }
        Err(e) => {
            return Err(Box::new(DiagnosticError::runtime_error(
                format!("Cannot access file '{path}': {e}"),
                None,
            )));
        }
    }
    
    // Read file
    let result = if as_binary {
        match std::fs::read(&path) {
            Ok(data) => {
                manager.track_bytes_read(data.len() as u64);
                Ok(Value::bytevector(data))
            }
            Err(e) => Err(Box::new(DiagnosticError::runtime_error(
                format!("Cannot read file '{path}': {e}"),
                None,
            ))),
        }
    } else {
        match std::fs::read_to_string(&path) {
            Ok(content) => {
                manager.track_bytes_read(content.len() as u64);
                Ok(Value::string(content))
            }
            Err(e) => Err(Box::new(DiagnosticError::runtime_error(
                format!("Cannot read file '{path}': {e}"),
                None,
            ))),
        }
    };
    
    // Log audit entry
    manager.log_audit_entry(AuditEntry {
        timestamp: Instant::now(),
        operation: "read".to_string(),
        path: Some(PathBuf::from(&path)),
        user_data: None,
        success: result.is_ok(),
        error_message: result.as_ref().err().map(|e| e.to_string()),
    });
    
    result
}

pub fn primitive_secure_file_write(args: &[Value]) -> Result<Value> {
    if args.len() < 2 || args.len() > 3 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("secure-file-write expects 2 or 3 arguments, got {args_len}", args_len = args.len()),
            None,
        )));
    }
    
    let path = extract_string(&args[0], "secure-file-write")?;
    let append = if args.len() > 2 {
        extract_boolean(&args[2], "secure-file-write")?
    } else {
        false
    };
    
    let (data, data_len) = match &args[1] {
        Value::Literal(crate::ast::Literal::String(s)) => (s.as_bytes().to_vec(), s.len()),
        Value::Literal(crate::ast::Literal::Bytevector(bv)) => (bv.clone(), bv.len()),
        _ => {
            return Err(Box::new(DiagnosticError::runtime_error(
                "secure-file-write requires string or bytevector data".to_string(),
                None,
            )));
        }
    };
    
    let security_manager = get_security_manager();
    let manager = security_manager.lock().unwrap();
    
    // Check path access
    manager.check_path_access(Path::new(&path), "write")?;
    
    // Check file size and bandwidth limits
    manager.check_file_size_limit(data_len as u64)?;
    manager.check_bandwidth_limit(data_len as u64)?;
    
    // Write file
    let write_result = if append {
        std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&path)
            .and_then(|mut file| {
                use std::io::Write;
                file.write_all(&data)
            })
    } else {
        std::fs::write(&path, &data)
    };
    
    let result = match write_result {
        Ok(()) => {
            manager.track_bytes_written(data_len as u64);
            Ok(Value::Unspecified)
        }
        Err(e) => Err(Box::new(DiagnosticError::runtime_error(
            format!("Cannot write to file '{path}': {e}"),
            None,
        ))),
    };
    
    // Log audit entry
    manager.log_audit_entry(AuditEntry {
        timestamp: Instant::now(),
        operation: if append { "append" } else { "write" }.to_string(),
        path: Some(PathBuf::from(&path)),
        user_data: Some(format!("{data_len} bytes")),
        success: result.is_ok(),
        error_message: result.as_ref().err().map(|e| e.to_string()),
    });
    
    result
}

pub fn primitive_validate_file_path(args: &[Value]) -> Result<Value> {
    if args.is_empty() || args.len() > 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("validate-file-path expects 1 or 2 arguments, got {args_len}", args_len = args.len()),
            None,
        )));
    }
    
    let path = extract_string(&args[0], "validate-file-path")?;
    let strict = if args.len() > 1 {
        extract_boolean(&args[1], "validate-file-path")?
    } else {
        true
    };
    
    // Basic path validation
    let path_obj = Path::new(&path);
    
    // Check for dangerous patterns
    let path_str = path_obj.to_string_lossy();
    
    // Check for path traversal attempts
    if path_str.contains("..") {
        return Ok(Value::boolean(false));
    }
    
    // Check for absolute paths in strict mode
    if strict && path_obj.is_absolute() {
        return Ok(Value::boolean(false));
    }
    
    // Check for special characters
    if path_str.contains('\0') {
        return Ok(Value::boolean(false));
    }
    
    // Additional platform-specific checks
    #[cfg(windows)]
    {
        // Check for Windows reserved names
        let filename = path_obj.file_name()
            .and_then(|name| name.to_str())
            .unwrap_or("");
        
        let reserved_names = ["CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", 
                             "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", 
                             "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"];
        
        if reserved_names.iter().any(|&name| filename.eq_ignore_ascii_case(name)) {
            return Ok(Value::boolean(false));
        }
    }
    
    Ok(Value::boolean(true))
}

// ============= HELPER FUNCTIONS =============

/// Extracts a string from a Value.
fn extract_string(value: &Value, operation: &str) -> Result<String> {
    match value {
        Value::Literal(crate::ast::Literal::String(s)) => Ok(s.clone()),
        _ => Err(Box::new(DiagnosticError::runtime_error(
            format!("{operation} requires string arguments"),
            None,
        ))),
    }
}

/// Extracts a boolean from a Value.
fn extract_boolean(value: &Value, operation: &str) -> Result<bool> {
    match value {
        Value::Literal(crate::ast::Literal::Boolean(b)) => Ok(*b),
        _ => Err(Box::new(DiagnosticError::runtime_error(
            format!("{operation} requires boolean arguments"),
            None,
        ))),
    }
}

/// Extracts an integer from a Value.
fn extract_integer(value: &Value, operation: &str) -> Result<i64> {
    match value {
        Value::Literal(lit) => {
            if let Some(i) = lit.to_i64() {
                Ok(i)
            } else {
                Err(Box::new(DiagnosticError::runtime_error(
                    format!("{operation} requires integer arguments"),
                    None,
                )))
            }
        }
        _ => Err(Box::new(DiagnosticError::runtime_error(
            format!("{operation} requires integer arguments"),
            None,
        ))),
    }
}

/// Extracts an optional integer from a Value.
fn extract_optional_integer(value: &Value) -> Option<i64> {
    match value {
        Value::Literal(lit) => lit.to_i64(),
        _ => None,
    }
}

/// Converts a vector of values to a Scheme list.
fn list_to_value(values: Vec<Value>) -> Value {
    values.into_iter().rev().fold(Value::Nil, |acc, val| {
        Value::Pair(Arc::new(val), Arc::new(acc))
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    
    #[test]
    fn test_security_policy() {
        let mut policy_map = HashMap::new();
        policy_map.insert(
            Value::Symbol(crate::utils::intern_symbol("strict-mode")),
            Value::boolean(true)
        );
        policy_map.insert(
            Value::Symbol(crate::utils::intern_symbol("audit-enabled")),
            Value::boolean(true)
        );
        policy_map.insert(
            Value::Symbol(crate::utils::intern_symbol("max-file-size")),
            Value::integer(1024 * 1024) // 1MB
        );
        
        let args = vec![Value::Hashtable(Arc::new(std::sync::RwLock::new(policy_map)))];
        let result = primitive_set_security_policy(&args);
        assert!(result.is_ok());
        
        // Test getting the policy
        let get_result = primitive_get_security_policy(&[]);
        assert!(get_result.is_ok());
    }
    
    #[test]
    fn test_path_validation() {
        // Valid path
        let args = vec![Value::string("valid/path.txt".to_string())];
        let result = primitive_validate_file_path(&args);
        assert!(result.is_ok());
        if let Ok(Value::Literal(crate::ast::Literal::Boolean(valid))) = result {
            assert!(valid);
        }
        
        // Invalid path with traversal
        let args = vec![Value::string("../../../etc/passwd".to_string())];
        let result = primitive_validate_file_path(&args);
        assert!(result.is_ok());
        if let Ok(Value::Literal(crate::ast::Literal::Boolean(valid))) = result {
            assert!(!valid);
        }
    }
    
    #[test]
    fn test_resource_tracking() {
        let security_manager = get_security_manager();
        let manager = security_manager.lock().unwrap();
        
        // Test tracking
        manager.track_file_opened();
        manager.track_bytes_read(1024);
        manager.track_bytes_written(512);
        
        let usage = manager.usage.lock().unwrap();
        assert_eq!(usage.open_files, 1);
        assert_eq!(usage.bytes_read, 1024);
        assert_eq!(usage.bytes_written, 512);
        assert_eq!(usage.operations_count, 3);
    }
    
    #[test]
    fn test_secure_file_operations() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("secure_test.txt");
        let file_path = test_file.to_string_lossy().to_string();
        
        // Add allowed path
        let args = vec![Value::string(temp_dir.path().to_string_lossy().to_string())];
        let result = primitive_add_allowed_path(&args);
        assert!(result.is_ok());
        
        // Test secure write
        let write_args = vec![
            Value::string(file_path.clone()),
            Value::string("Hello, secure world!".to_string()),
        ];
        let result = primitive_secure_file_write(&write_args);
        assert!(result.is_ok());
        
        // Test secure read
        let read_args = vec![Value::string(file_path)];
        let result = primitive_secure_file_read(&read_args);
        assert!(result.is_ok());
        
        if let Ok(Value::Literal(crate::ast::Literal::String(content))) = result {
            assert_eq!(content, "Hello, secure world!");
        } else {
            panic!("Expected string result");
        }
    }
}