kael 0.1.2

GPU-accelerated native UI framework for Rust — build desktop apps with Metal, DirectX, and Vulkan rendering
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
//! Security boundary: capability model and permission broker for GPUI.
//!
//! This module provides safe defaults and explicit capability grants for
//! dangerous actions such as opening external URLs, filesystem access, and
//! plugin execution. The model is designed to integrate with the process
//! isolation layer so that child processes receive only the capabilities they
//! need.
//!
//! ## Phase 11 additions
//!
//! * **Unified permission prompt flow** — [`PermissionKind`], [`PermissionRequest`],
//!   [`PermissionManager`].
//! * **Secure credential / keychain wrappers** — [`CredentialEntry`],
//!   [`KeychainStore`].
//! * **File-scoped access tokens** — [`AccessToken`], [`AccessTokenStore`].
//! * **Plugin permission manifests** — [`PluginPermissionManifest`].
//! * **Process capability limits** — [`ProcessLimits`], [`ProcessCapability`].
//! * **Network permission policy** — [`NetworkPolicy`].
//! * **IPC schema versioning** — [`IpcSchema`].

use std::{
    collections::{HashMap, HashSet},
    path::PathBuf,
    sync::Arc,
};

use serde::{Deserialize, Serialize};

use crate::platform::PermissionStatus;
use crate::process_model::{ProcessClass, ProcessId};

type PromptHandler = Arc<dyn Fn(ProcessId, &Capability) -> PermissionResult + Send + Sync>;

// ---------------------------------------------------------------------------
// Capability Model
// ---------------------------------------------------------------------------

/// The scope of filesystem access granted by a capability.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PathScope {
    /// Access restricted to the application's own data directory.
    AppData,
    /// Access restricted to the user's downloads directory.
    Downloads,
    /// Access restricted to paths explicitly selected by the user via a
    /// platform file dialog.
    UserSelected,
    /// Unrestricted filesystem access (use sparingly).
    Any,
}

/// A capability granted to a process or component.
///
/// Capabilities are explicit grants for dangerous actions. The default for all
/// high-risk capabilities is **deny**.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Capability {
    /// Open URLs in the system default browser.
    OpenExternalUrl,
    /// Read files within the given scope.
    FilesystemRead {
        /// The scope of allowed filesystem reads.
        scope: PathScope,
    },
    /// Write files within the given scope.
    FilesystemWrite {
        /// The scope of allowed filesystem writes.
        scope: PathScope,
    },
    /// Execute shell commands.
    ShellExecute,
    /// Read from the system clipboard.
    ClipboardRead,
    /// Write to the system clipboard.
    ClipboardWrite,
    /// Show native notifications.
    Notification,
    /// Make network requests to the given hosts.
    Network {
        /// Allowed host patterns.
        hosts: Vec<String>,
    },
    /// Access the microphone.
    Microphone,
    /// Access the camera.
    Camera,
    /// Capture screen content.
    ScreenCapture,
}

impl Capability {
    /// Returns true if this capability is considered high-risk and should
    /// require explicit user or developer opt-in.
    pub fn is_high_risk(&self) -> bool {
        matches!(
            self,
            Capability::ShellExecute
                | Capability::ClipboardRead
                | Capability::Network { .. }
                | Capability::Camera
                | Capability::ScreenCapture
                | Capability::FilesystemRead {
                    scope: PathScope::Any
                }
                | Capability::FilesystemWrite {
                    scope: PathScope::Any
                }
        )
    }
}

// ---------------------------------------------------------------------------
// Permission Result
// ---------------------------------------------------------------------------

/// The result of a permission check.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PermissionResult {
    /// The capability is granted.
    Granted,
    /// The capability is explicitly denied.
    Denied,
    /// The capability requires a user prompt to decide.
    Prompt,
}

// ---------------------------------------------------------------------------
// Permission Broker
// ---------------------------------------------------------------------------

/// A centralized broker for capability checks and permission requests.
///
/// Applications configure the broker at startup with the capabilities they
/// wish to grant to each process class or individual process. The broker can
/// also be used to implement runtime permission prompts.
#[derive(Clone, Default)]
pub struct PermissionBroker {
    grants: HashMap<ProcessId, HashSet<Capability>>,
    process_classes: HashMap<ProcessId, ProcessClass>,
    class_defaults: HashMap<ProcessClass, HashSet<Capability>>,
    prompt_handler: Option<PromptHandler>,
}

impl std::fmt::Debug for PermissionBroker {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PermissionBroker")
            .field("grant_count", &self.grants.len())
            .field("registered_processes", &self.process_classes.len())
            .field("class_default_count", &self.class_defaults.len())
            .field("has_prompt_handler", &self.prompt_handler.is_some())
            .finish()
    }
}

impl PermissionBroker {
    /// Create an empty permission broker.
    pub fn new() -> Self {
        Self {
            grants: HashMap::new(),
            process_classes: HashMap::new(),
            class_defaults: HashMap::new(),
            prompt_handler: None,
        }
    }

    /// Register a process and its process class with the broker.
    pub fn register_process(&mut self, process: ProcessId, class: ProcessClass) {
        self.process_classes.insert(process, class);
    }

    /// Remove a process from the broker, clearing grants and class metadata.
    pub fn unregister_process(&mut self, process: ProcessId) {
        self.process_classes.remove(&process);
        self.grants.remove(&process);
    }

    /// Configure default capabilities for a process class.
    pub fn set_default_capabilities<I>(&mut self, class: ProcessClass, capabilities: I)
    where
        I: IntoIterator<Item = Capability>,
    {
        self.class_defaults
            .insert(class, capabilities.into_iter().collect());
    }

    /// Configure default capabilities using the provided threat model.
    pub fn apply_threat_model(&mut self, model: &ThreatModel) {
        self.set_default_capabilities(
            ProcessClass::Ui,
            model.defaults_for(ProcessClass::Ui).iter().cloned(),
        );
        self.set_default_capabilities(
            ProcessClass::Worker,
            model.defaults_for(ProcessClass::Worker).iter().cloned(),
        );
        self.set_default_capabilities(
            ProcessClass::Media,
            model.defaults_for(ProcessClass::Media).iter().cloned(),
        );
        self.set_default_capabilities(
            ProcessClass::Extension,
            model.defaults_for(ProcessClass::Extension).iter().cloned(),
        );
    }

    /// Install a prompt handler for permissions that are not already granted.
    pub fn set_prompt_handler(
        &mut self,
        handler: impl Fn(ProcessId, &Capability) -> PermissionResult + Send + Sync + 'static,
    ) {
        self.prompt_handler = Some(Arc::new(handler));
    }

    /// Return a copy of the broker with a prompt handler installed.
    pub fn with_prompt_handler(
        mut self,
        handler: impl Fn(ProcessId, &Capability) -> PermissionResult + Send + Sync + 'static,
    ) -> Self {
        self.set_prompt_handler(handler);
        self
    }

    /// Grant a capability to a process.
    pub fn grant(&mut self, process: ProcessId, capability: Capability) {
        self.grants.entry(process).or_default().insert(capability);
    }

    /// Grant multiple capabilities to a process.
    pub fn grant_all<I>(&mut self, process: ProcessId, capabilities: I)
    where
        I: IntoIterator<Item = Capability>,
    {
        self.grants.entry(process).or_default().extend(capabilities);
    }

    /// Revoke a capability from a process.
    pub fn revoke(&mut self, process: ProcessId, capability: &Capability) {
        if let Some(set) = self.grants.get_mut(&process) {
            let _ = std::collections::HashSet::<Capability>::remove(set, capability);
        }
    }

    /// Revoke all capabilities from a process.
    pub fn revoke_all(&mut self, process: ProcessId) {
        self.grants.remove(&process);
    }

    fn granted_by_default(&self, process: ProcessId, capability: &Capability) -> bool {
        self.process_classes
            .get(&process)
            .and_then(|class| self.class_defaults.get(class))
            .is_some_and(|set| set.contains(capability))
    }

    /// Explicitly prompt for a capability.
    pub fn prompt(&self, process: ProcessId, capability: &Capability) -> PermissionResult {
        if let Some(handler) = &self.prompt_handler {
            handler(process, capability)
        } else {
            PermissionResult::Denied
        }
    }

    /// Check whether a process holds a given capability.
    pub fn check(&self, process: ProcessId, capability: &Capability) -> PermissionResult {
        if let Some(set) = self.grants.get(&process) {
            if std::collections::HashSet::<Capability>::contains(set, capability) {
                return PermissionResult::Granted;
            }
        }
        if self.granted_by_default(process, capability) {
            return PermissionResult::Granted;
        }
        if let Some(prompt_handler) = &self.prompt_handler {
            return prompt_handler(process, capability);
        }
        PermissionResult::Denied
    }

    /// Check whether a process holds any of the given capabilities.
    pub fn check_any(&self, process: ProcessId, capabilities: &[Capability]) -> PermissionResult {
        let mut prompted = false;
        for cap in capabilities {
            match self.check(process, cap) {
                PermissionResult::Granted => return PermissionResult::Granted,
                PermissionResult::Prompt => prompted = true,
                PermissionResult::Denied => {}
            }
        }
        if prompted {
            PermissionResult::Prompt
        } else {
            PermissionResult::Denied
        }
    }

    /// Return all capabilities granted to a process.
    pub fn capabilities(&self, process: ProcessId) -> Vec<Capability> {
        let mut capabilities = self
            .process_classes
            .get(&process)
            .and_then(|class| self.class_defaults.get(class))
            .cloned()
            .unwrap_or_default();
        if let Some(grants) = self.grants.get(&process) {
            capabilities.extend(grants.iter().cloned());
        }
        capabilities.into_iter().collect()
    }
}

// ---------------------------------------------------------------------------
// Threat Model Documentation Types
// ---------------------------------------------------------------------------

/// Categories of threats that the GPUI security model addresses.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ThreatCategory {
    /// Untrusted or semi-trusted content rendered by the app.
    UntrustedContent,
    /// Malicious or compromised plugins or extensions.
    MaliciousPlugin,
    /// A compromised child process attempting to escape its sandbox.
    CompromisedChildProcess,
    /// Unsafe handling of external URLs or protocol schemes.
    UnsafeExternalUrl,
    /// Leakage of local privileges (filesystem, shell, etc.).
    LocalPrivilegeLeak,
}

/// A documented threat and its mitigations.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Threat {
    /// The category of threat.
    pub category: ThreatCategory,
    /// Human-readable description.
    pub description: String,
    /// Framework surfaces that are vulnerable.
    pub surfaces: Vec<String>,
    /// Mitigations in place or planned.
    pub mitigations: Vec<String>,
}

/// A lightweight threat model for a GPUI application.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct ThreatModel {
    /// Documented threats.
    pub threats: Vec<Threat>,
    /// Default capability grants for the UI process.
    pub ui_defaults: Vec<Capability>,
    /// Default capability grants for worker processes.
    pub worker_defaults: Vec<Capability>,
    /// Default capability grants for media processes.
    pub media_defaults: Vec<Capability>,
    /// Default capability grants for extension hosts.
    pub extension_defaults: Vec<Capability>,
}

impl ThreatModel {
    /// Create a new threat model with safe defaults.
    pub fn new() -> Self {
        Self {
            threats: Vec::new(),
            ui_defaults: vec![
                Capability::OpenExternalUrl,
                Capability::ClipboardRead,
                Capability::ClipboardWrite,
                Capability::Notification,
            ],
            worker_defaults: vec![
                Capability::FilesystemRead {
                    scope: PathScope::AppData,
                },
                Capability::FilesystemWrite {
                    scope: PathScope::AppData,
                },
            ],
            media_defaults: vec![
                Capability::Microphone,
                Capability::Camera,
                Capability::ScreenCapture,
            ],
            extension_defaults: vec![],
        }
    }

    /// Add a threat to the model.
    pub fn add_threat(mut self, threat: Threat) -> Self {
        self.threats.push(threat);
        self
    }

    /// Return the default capabilities for a given process class.
    pub fn defaults_for(&self, class: ProcessClass) -> &[Capability] {
        match class {
            ProcessClass::Ui => &self.ui_defaults,
            ProcessClass::Worker => &self.worker_defaults,
            ProcessClass::Media => &self.media_defaults,
            ProcessClass::Extension => &self.extension_defaults,
        }
    }
}

// ===========================================================================
// Phase 11: Unified Permission Prompt Flow
// ===========================================================================

/// The kind of system permission that can be requested.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PermissionKind {
    /// Camera access.
    Camera,
    /// Microphone access.
    Microphone,
    /// Location services.
    Location,
    /// File system access.
    FileAccess,
    /// Network access.
    Network,
    /// Push / local notifications.
    Notifications,
    /// Accessibility APIs (e.g. screen reader control).
    Accessibility,
}

/// A request to grant a specific permission, including a human-readable reason.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PermissionRequest {
    /// The permission being requested.
    pub kind: PermissionKind,
    /// A human-readable explanation of why the permission is needed.
    pub reason: String,
}

/// Tracks and manages permission states for the application.
#[derive(Debug, Clone, Default)]
pub struct PermissionManager {
    states: HashMap<PermissionKind, PermissionStatus>,
}

impl PermissionManager {
    /// Create an empty permission manager with all permissions undetermined.
    pub fn new() -> Self {
        Self {
            states: HashMap::new(),
        }
    }

    /// Return the current status for the given permission kind.
    pub fn status(&self, kind: PermissionKind) -> PermissionStatus {
        self.states
            .get(&kind)
            .copied()
            .unwrap_or(PermissionStatus::NotDetermined)
    }

    /// Request a permission. If the permission has not been determined yet,
    /// the provided callback decides the outcome.
    pub fn request(
        &mut self,
        request: &PermissionRequest,
        decide: impl FnOnce(&PermissionRequest) -> PermissionStatus,
    ) -> PermissionStatus {
        let current = self.status(request.kind);
        if current != PermissionStatus::NotDetermined {
            return current;
        }
        let result = decide(request);
        self.states.insert(request.kind, result);
        result
    }

    /// Directly set the status of a permission (e.g. from a platform callback).
    pub fn set_status(&mut self, kind: PermissionKind, status: PermissionStatus) {
        self.states.insert(kind, status);
    }

    /// Revoke a previously granted permission, resetting it to [`PermissionStatus::Denied`].
    pub fn revoke(&mut self, kind: PermissionKind) {
        self.states.insert(kind, PermissionStatus::Denied);
    }

    /// Return all permissions that have been explicitly set.
    pub fn all_statuses(&self) -> &HashMap<PermissionKind, PermissionStatus> {
        &self.states
    }
}

// ===========================================================================
// Phase 11: Secure Credential / Keychain Wrappers
// ===========================================================================

/// A single credential entry stored in the keychain.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CredentialEntry {
    /// The service or application the credential belongs to.
    pub service: String,
    /// The account or username.
    pub account: String,
    /// The secret data (password, token, etc.).
    pub secret: Vec<u8>,
}

/// An in-memory credential store that mimics system keychain semantics.
///
/// In production, callers should back this with a platform keychain
/// (macOS Keychain, Windows Credential Manager, libsecret on Linux).
#[derive(Debug, Clone, Default)]
pub struct KeychainStore {
    entries: HashMap<(String, String), CredentialEntry>,
}

impl KeychainStore {
    /// Create an empty keychain store.
    pub fn new() -> Self {
        Self {
            entries: HashMap::new(),
        }
    }

    /// Store a credential. Overwrites any existing entry with the same
    /// service + account pair.
    pub fn store(&mut self, entry: CredentialEntry) {
        let key = (entry.service.clone(), entry.account.clone());
        self.entries.insert(key, entry);
    }

    /// Retrieve a credential by service and account.
    pub fn retrieve(&self, service: &str, account: &str) -> Option<&CredentialEntry> {
        self.entries.get(&(service.to_owned(), account.to_owned()))
    }

    /// Delete a credential by service and account. Returns `true` if removed.
    pub fn delete(&mut self, service: &str, account: &str) -> bool {
        self.entries
            .remove(&(service.to_owned(), account.to_owned()))
            .is_some()
    }

    /// List all stored credentials (without exposing secrets directly).
    pub fn list(&self) -> Vec<(&str, &str)> {
        self.entries
            .values()
            .map(|entry| (entry.service.as_str(), entry.account.as_str()))
            .collect()
    }

    /// Return the number of stored credentials.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Return true if no credentials are stored.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

// ===========================================================================
// Phase 11: File-Scoped Access Tokens
// ===========================================================================

/// A time-scoped token granting access to a specific file path.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AccessToken {
    /// The file path this token grants access to.
    pub path: PathBuf,
    /// An opaque token string.
    pub token: String,
    /// Unix timestamp (seconds) when the token was created.
    pub created_at: u64,
    /// Optional expiry as a Unix timestamp (seconds).
    pub expires_at: Option<u64>,
}

/// Manages file-scoped access tokens with optional expiry.
#[derive(Debug, Clone, Default)]
pub struct AccessTokenStore {
    tokens: HashMap<String, AccessToken>,
    next_id: u64,
}

impl AccessTokenStore {
    /// Create an empty token store.
    pub fn new() -> Self {
        Self {
            tokens: HashMap::new(),
            next_id: 0,
        }
    }

    /// Issue a new access token for the given path. Returns the token string.
    pub fn issue(&mut self, path: PathBuf, now: u64, ttl_seconds: Option<u64>) -> String {
        self.next_id += 1;
        let token_str = format!(
            "kat_{:016x}_{}",
            self.next_id,
            seahash::hash(path.to_string_lossy().as_bytes())
        );
        let access_token = AccessToken {
            path,
            token: token_str.clone(),
            created_at: now,
            expires_at: ttl_seconds.map(|ttl| now + ttl),
        };
        self.tokens.insert(token_str.clone(), access_token);
        token_str
    }

    /// Validate a token at the given timestamp. Returns the associated path
    /// if valid.
    pub fn validate(&self, token: &str, now: u64) -> Option<&PathBuf> {
        let entry = self.tokens.get(token)?;
        if let Some(expires) = entry.expires_at {
            if now >= expires {
                return None;
            }
        }
        Some(&entry.path)
    }

    /// Revoke (remove) a token. Returns `true` if the token existed.
    pub fn revoke(&mut self, token: &str) -> bool {
        self.tokens.remove(token).is_some()
    }

    /// List all currently stored tokens (including expired ones).
    pub fn list(&self) -> Vec<&AccessToken> {
        self.tokens.values().collect()
    }

    /// Remove all expired tokens as of `now`. Returns the count removed.
    pub fn purge_expired(&mut self, now: u64) -> usize {
        let before = self.tokens.len();
        self.tokens
            .retain(|_, token| token.expires_at.map_or(true, |expires| now < expires));
        before - self.tokens.len()
    }
}

// ===========================================================================
// Phase 11: Plugin Permission Manifests
// ===========================================================================

/// A manifest declaring the permissions a plugin requires and optionally requests.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PluginPermissionManifest {
    /// Unique identifier for the plugin.
    pub plugin_id: String,
    /// Permissions the plugin must have to function.
    pub required: Vec<PermissionKind>,
    /// Permissions the plugin can use but does not strictly need.
    pub optional: Vec<PermissionKind>,
}

impl PluginPermissionManifest {
    /// Create a new manifest with the given plugin id and no permissions.
    pub fn new(plugin_id: impl Into<String>) -> Self {
        Self {
            plugin_id: plugin_id.into(),
            required: Vec::new(),
            optional: Vec::new(),
        }
    }

    /// Validate that all required permissions are present in the granted set.
    pub fn validate(&self, granted: &HashSet<PermissionKind>) -> Result<(), Vec<PermissionKind>> {
        let missing: Vec<PermissionKind> = self
            .required
            .iter()
            .filter(|perm| !granted.contains(perm))
            .copied()
            .collect();
        if missing.is_empty() {
            Ok(())
        } else {
            Err(missing)
        }
    }

    /// Check whether a specific permission is declared (required or optional).
    pub fn check_permission(&self, kind: PermissionKind) -> bool {
        self.required.contains(&kind) || self.optional.contains(&kind)
    }

    /// Return true if the manifest has at least one required permission.
    pub fn has_required(&self) -> bool {
        !self.required.is_empty()
    }

    /// Return all declared permissions (required + optional), deduplicated.
    pub fn all_permissions(&self) -> Vec<PermissionKind> {
        let mut set = HashSet::new();
        for perm in &self.required {
            set.insert(*perm);
        }
        for perm in &self.optional {
            set.insert(*perm);
        }
        set.into_iter().collect()
    }
}

// ===========================================================================
// Phase 11: Process Capability Limits
// ===========================================================================

/// Resource limits that can be applied to a sandboxed process.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProcessLimits {
    /// Maximum memory in bytes the process may allocate.
    pub max_memory_bytes: Option<u64>,
    /// Maximum CPU usage as a percentage (0.0–100.0).
    pub max_cpu_percent: Option<f64>,
    /// Maximum number of open file descriptors.
    pub max_open_files: Option<u32>,
    /// Whether the process is allowed to make network connections.
    pub network_allowed: bool,
}

impl Default for ProcessLimits {
    fn default() -> Self {
        Self {
            max_memory_bytes: None,
            max_cpu_percent: None,
            max_open_files: None,
            network_allowed: true,
        }
    }
}

/// A process tracked by the capability system, including its limits and
/// any recorded violations.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProcessCapability {
    /// The OS-level process identifier.
    pub pid: u32,
    /// A human-readable name for the process.
    pub name: String,
    /// Resource limits applied to this process.
    pub limits: ProcessLimits,
    /// Recorded violation descriptions.
    pub violations: Vec<String>,
}

impl ProcessCapability {
    /// Create a new process capability tracker with the given limits.
    pub fn new(pid: u32, name: impl Into<String>, limits: ProcessLimits) -> Self {
        Self {
            pid,
            name: name.into(),
            limits,
            violations: Vec::new(),
        }
    }

    /// Record a violation against this process.
    pub fn record_violation(&mut self, description: impl Into<String>) {
        self.violations.push(description.into());
    }

    /// Return the number of violations recorded.
    pub fn violation_count(&self) -> usize {
        self.violations.len()
    }

    /// Check whether a memory usage value exceeds the configured limit.
    pub fn check_memory(&mut self, used_bytes: u64) -> bool {
        if let Some(max) = self.limits.max_memory_bytes {
            if used_bytes > max {
                self.record_violation(format!("memory limit exceeded: {used_bytes} > {max}"));
                return false;
            }
        }
        true
    }

    /// Check whether a CPU usage value exceeds the configured limit.
    pub fn check_cpu(&mut self, cpu_percent: f64) -> bool {
        if let Some(max) = self.limits.max_cpu_percent {
            if cpu_percent > max {
                self.record_violation(format!("CPU limit exceeded: {cpu_percent:.1}% > {max:.1}%"));
                return false;
            }
        }
        true
    }

    /// Check whether a network request is allowed.
    pub fn check_network(&mut self) -> bool {
        if !self.limits.network_allowed {
            self.record_violation("network access denied".to_owned());
            return false;
        }
        true
    }
}

// ===========================================================================
// Phase 11: Network Permission Policy
// ===========================================================================

/// A policy governing which network hosts a process may contact.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum NetworkPolicy {
    /// All hosts are permitted.
    AllowAll,
    /// No hosts are permitted.
    #[default]
    DenyAll,
    /// Only the listed hosts are permitted.
    AllowList(Vec<String>),
    /// All hosts except the listed ones are permitted.
    DenyList(Vec<String>),
}

impl NetworkPolicy {
    /// Check whether a given host is permitted under this policy.
    pub fn check(&self, host: &str) -> bool {
        match self {
            NetworkPolicy::AllowAll => true,
            NetworkPolicy::DenyAll => false,
            NetworkPolicy::AllowList(hosts) => hosts.iter().any(|h| h == host),
            NetworkPolicy::DenyList(hosts) => !hosts.iter().any(|h| h == host),
        }
    }
}

// ===========================================================================
// Phase 11: IPC Schema Versioning
// ===========================================================================

/// Version metadata for an IPC message schema, enabling forward/backward
/// compatibility negotiation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IpcSchema {
    /// The current schema version.
    pub version: u32,
    /// The minimum version this schema is compatible with.
    pub min_compatible: u32,
    /// The message types supported by this schema.
    pub message_types: Vec<String>,
}

impl IpcSchema {
    /// Create a new IPC schema.
    pub fn new(version: u32, min_compatible: u32, message_types: Vec<String>) -> Self {
        Self {
            version,
            min_compatible,
            message_types,
        }
    }

    /// Check whether this schema is compatible with another schema.
    ///
    /// Two schemas are compatible when each schema's version falls within the
    /// other's supported range (i.e. `>= min_compatible`).
    pub fn is_compatible(&self, other: &IpcSchema) -> bool {
        self.version >= other.min_compatible && other.version >= self.min_compatible
    }

    /// Negotiate a common schema version between `self` and `other`.
    ///
    /// Returns the lower of the two versions if compatible, or `None`.
    pub fn negotiate(&self, other: &IpcSchema) -> Option<u32> {
        if self.is_compatible(other) {
            Some(self.version.min(other.version))
        } else {
            None
        }
    }

    /// Return the intersection of message types supported by both schemas.
    pub fn common_message_types(&self, other: &IpcSchema) -> Vec<String> {
        let other_set: HashSet<&String> = other.message_types.iter().collect();
        self.message_types
            .iter()
            .filter(|mt| other_set.contains(mt))
            .cloned()
            .collect()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // === Capability & PermissionBroker (existing) ===

    #[test]
    fn test_capability_high_risk() {
        assert!(Capability::ShellExecute.is_high_risk());
        assert!(Capability::ClipboardRead.is_high_risk());
        assert!(
            Capability::Network {
                hosts: vec!["example.com".to_string()]
            }
            .is_high_risk()
        );
        assert!(!Capability::Notification.is_high_risk());
        assert!(!Capability::OpenExternalUrl.is_high_risk());
    }

    #[test]
    fn test_permission_broker_grant_and_check() {
        let mut broker = PermissionBroker::new();
        let process = ProcessId(1);

        assert_eq!(
            broker.check(process, &Capability::OpenExternalUrl),
            PermissionResult::Denied
        );

        broker.grant(process, Capability::OpenExternalUrl);
        assert_eq!(
            broker.check(process, &Capability::OpenExternalUrl),
            PermissionResult::Granted
        );
    }

    #[test]
    fn test_permission_broker_class_defaults() {
        let process = ProcessId(2);
        let mut broker = PermissionBroker::new();
        broker.register_process(process, ProcessClass::Worker);
        broker.set_default_capabilities(
            ProcessClass::Worker,
            [Capability::FilesystemRead {
                scope: PathScope::AppData,
            }],
        );

        assert_eq!(
            broker.check(
                process,
                &Capability::FilesystemRead {
                    scope: PathScope::AppData,
                },
            ),
            PermissionResult::Granted
        );
    }

    #[test]
    fn test_permission_broker_revoke() {
        let mut broker = PermissionBroker::new();
        let process = ProcessId(1);

        broker.grant(process, Capability::ClipboardRead);
        broker.revoke(process, &Capability::ClipboardRead);

        assert_eq!(
            broker.check(process, &Capability::ClipboardRead),
            PermissionResult::Denied
        );
    }

    #[test]
    fn test_permission_broker_check_any() {
        let mut broker = PermissionBroker::new();
        let process = ProcessId(1);

        broker.grant(process, Capability::Notification);
        assert_eq!(
            broker.check_any(
                process,
                &[Capability::ShellExecute, Capability::Notification]
            ),
            PermissionResult::Granted
        );
        assert_eq!(
            broker.check_any(process, &[Capability::ShellExecute, Capability::Camera]),
            PermissionResult::Denied
        );
    }

    #[test]
    fn test_threat_model_defaults() {
        let model = ThreatModel::new();
        assert!(!model.ui_defaults.is_empty());
        assert!(!model.worker_defaults.is_empty());
        assert!(!model.media_defaults.is_empty());
        assert!(model.extension_defaults.is_empty());
    }

    #[test]
    fn test_threat_model_serialization() {
        let model = ThreatModel::new().add_threat(Threat {
            category: ThreatCategory::UntrustedContent,
            description: "User-generated HTML may contain scripts".to_string(),
            surfaces: vec!["webview".to_string()],
            mitigations: vec!["sanitize input".to_string()],
        });

        let json = serde_json::to_string(&model).unwrap();
        let decoded: ThreatModel = serde_json::from_str(&json).unwrap();
        assert_eq!(model, decoded);
    }

    #[test]
    fn test_permission_broker_apply_threat_model() {
        let process = ProcessId(3);
        let mut broker = PermissionBroker::new();
        broker.register_process(process, ProcessClass::Ui);
        broker.apply_threat_model(&ThreatModel::new());

        assert_eq!(
            broker.check(process, &Capability::Notification),
            PermissionResult::Granted
        );
    }

    #[test]
    fn test_permission_broker_prompt_handler() {
        let process = ProcessId(4);
        let broker = PermissionBroker::new().with_prompt_handler(|_, capability| {
            if capability.is_high_risk() {
                PermissionResult::Prompt
            } else {
                PermissionResult::Denied
            }
        });

        assert_eq!(
            broker.check(process, &Capability::ShellExecute),
            PermissionResult::Prompt
        );
    }

    // === PermissionManager ===

    #[test]
    fn test_permission_manager_default_not_determined() {
        let mgr = PermissionManager::new();
        assert_eq!(
            mgr.status(PermissionKind::Camera),
            PermissionStatus::NotDetermined
        );
    }

    #[test]
    fn test_permission_manager_request_grants() {
        let mut mgr = PermissionManager::new();
        let req = PermissionRequest {
            kind: PermissionKind::Camera,
            reason: "Video call".to_string(),
        };
        let status = mgr.request(&req, |_| PermissionStatus::Granted);
        assert_eq!(status, PermissionStatus::Granted);
        assert_eq!(
            mgr.status(PermissionKind::Camera),
            PermissionStatus::Granted
        );
    }

    #[test]
    fn test_permission_manager_request_denied() {
        let mut mgr = PermissionManager::new();
        let req = PermissionRequest {
            kind: PermissionKind::Microphone,
            reason: "Audio recording".to_string(),
        };
        let status = mgr.request(&req, |_| PermissionStatus::Denied);
        assert_eq!(status, PermissionStatus::Denied);
    }

    #[test]
    fn test_permission_manager_does_not_re_prompt() {
        let mut mgr = PermissionManager::new();
        mgr.set_status(PermissionKind::Location, PermissionStatus::Denied);
        let req = PermissionRequest {
            kind: PermissionKind::Location,
            reason: "Map".to_string(),
        };
        let status = mgr.request(&req, |_| PermissionStatus::Granted);
        assert_eq!(status, PermissionStatus::Denied);
    }

    #[test]
    fn test_permission_manager_revoke() {
        let mut mgr = PermissionManager::new();
        mgr.set_status(PermissionKind::Network, PermissionStatus::Granted);
        mgr.revoke(PermissionKind::Network);
        assert_eq!(
            mgr.status(PermissionKind::Network),
            PermissionStatus::Denied
        );
    }

    #[test]
    fn test_permission_manager_all_statuses() {
        let mut mgr = PermissionManager::new();
        mgr.set_status(PermissionKind::Camera, PermissionStatus::Granted);
        mgr.set_status(PermissionKind::Microphone, PermissionStatus::Denied);
        assert_eq!(mgr.all_statuses().len(), 2);
    }

    #[test]
    fn test_permission_manager_restricted_status() {
        let mut mgr = PermissionManager::new();
        mgr.set_status(PermissionKind::Camera, PermissionStatus::Restricted);
        assert_eq!(
            mgr.status(PermissionKind::Camera),
            PermissionStatus::Restricted
        );
    }

    // === KeychainStore ===

    #[test]
    fn test_keychain_store_and_retrieve() {
        let mut store = KeychainStore::new();
        store.store(CredentialEntry {
            service: "github".to_string(),
            account: "user1".to_string(),
            secret: b"token123".to_vec(),
        });
        let entry = store.retrieve("github", "user1").unwrap();
        assert_eq!(entry.secret, b"token123");
    }

    #[test]
    fn test_keychain_retrieve_missing() {
        let store = KeychainStore::new();
        assert!(store.retrieve("nope", "nada").is_none());
    }

    #[test]
    fn test_keychain_delete() {
        let mut store = KeychainStore::new();
        store.store(CredentialEntry {
            service: "svc".to_string(),
            account: "acct".to_string(),
            secret: vec![1, 2, 3],
        });
        assert!(store.delete("svc", "acct"));
        assert!(!store.delete("svc", "acct"));
        assert!(store.is_empty());
    }

    #[test]
    fn test_keychain_list() {
        let mut store = KeychainStore::new();
        store.store(CredentialEntry {
            service: "a".to_string(),
            account: "b".to_string(),
            secret: vec![],
        });
        store.store(CredentialEntry {
            service: "c".to_string(),
            account: "d".to_string(),
            secret: vec![],
        });
        assert_eq!(store.len(), 2);
        assert_eq!(store.list().len(), 2);
    }

    #[test]
    fn test_keychain_overwrite() {
        let mut store = KeychainStore::new();
        store.store(CredentialEntry {
            service: "svc".to_string(),
            account: "acct".to_string(),
            secret: b"old".to_vec(),
        });
        store.store(CredentialEntry {
            service: "svc".to_string(),
            account: "acct".to_string(),
            secret: b"new".to_vec(),
        });
        assert_eq!(store.len(), 1);
        assert_eq!(store.retrieve("svc", "acct").unwrap().secret, b"new");
    }

    // === AccessTokenStore ===

    #[test]
    fn test_access_token_issue_and_validate() {
        let mut store = AccessTokenStore::new();
        let token = store.issue(PathBuf::from("/tmp/file.txt"), 1000, Some(3600));
        assert!(store.validate(&token, 1000).is_some());
        assert_eq!(
            store.validate(&token, 1000).unwrap(),
            &PathBuf::from("/tmp/file.txt")
        );
    }

    #[test]
    fn test_access_token_expired() {
        let mut store = AccessTokenStore::new();
        let token = store.issue(PathBuf::from("/f"), 1000, Some(60));
        assert!(store.validate(&token, 1059).is_some());
        assert!(store.validate(&token, 1060).is_none());
    }

    #[test]
    fn test_access_token_no_expiry() {
        let mut store = AccessTokenStore::new();
        let token = store.issue(PathBuf::from("/f"), 0, None);
        assert!(store.validate(&token, u64::MAX - 1).is_some());
    }

    #[test]
    fn test_access_token_revoke() {
        let mut store = AccessTokenStore::new();
        let token = store.issue(PathBuf::from("/f"), 0, None);
        assert!(store.revoke(&token));
        assert!(store.validate(&token, 0).is_none());
        assert!(!store.revoke(&token));
    }

    #[test]
    fn test_access_token_list() {
        let mut store = AccessTokenStore::new();
        store.issue(PathBuf::from("/a"), 0, None);
        store.issue(PathBuf::from("/b"), 0, None);
        assert_eq!(store.list().len(), 2);
    }

    #[test]
    fn test_access_token_purge_expired() {
        let mut store = AccessTokenStore::new();
        store.issue(PathBuf::from("/a"), 0, Some(10));
        store.issue(PathBuf::from("/b"), 0, Some(20));
        store.issue(PathBuf::from("/c"), 0, None);
        let purged = store.purge_expired(15);
        assert_eq!(purged, 1);
        assert_eq!(store.list().len(), 2);
    }

    // === PluginPermissionManifest ===

    #[test]
    fn test_plugin_manifest_validate_ok() {
        let manifest = PluginPermissionManifest {
            plugin_id: "my-plugin".to_string(),
            required: vec![PermissionKind::Network, PermissionKind::FileAccess],
            optional: vec![PermissionKind::Notifications],
        };
        let granted: HashSet<PermissionKind> =
            [PermissionKind::Network, PermissionKind::FileAccess]
                .into_iter()
                .collect();
        assert!(manifest.validate(&granted).is_ok());
    }

    #[test]
    fn test_plugin_manifest_validate_missing() {
        let manifest = PluginPermissionManifest {
            plugin_id: "p".to_string(),
            required: vec![PermissionKind::Camera, PermissionKind::Microphone],
            optional: vec![],
        };
        let granted: HashSet<PermissionKind> = [PermissionKind::Camera].into_iter().collect();
        let err = manifest.validate(&granted).unwrap_err();
        assert_eq!(err, vec![PermissionKind::Microphone]);
    }

    #[test]
    fn test_plugin_manifest_check_permission() {
        let manifest = PluginPermissionManifest {
            plugin_id: "p".to_string(),
            required: vec![PermissionKind::Camera],
            optional: vec![PermissionKind::Location],
        };
        assert!(manifest.check_permission(PermissionKind::Camera));
        assert!(manifest.check_permission(PermissionKind::Location));
        assert!(!manifest.check_permission(PermissionKind::Network));
    }

    #[test]
    fn test_plugin_manifest_has_required() {
        let empty = PluginPermissionManifest::new("e");
        assert!(!empty.has_required());

        let with_req = PluginPermissionManifest {
            plugin_id: "p".to_string(),
            required: vec![PermissionKind::Camera],
            optional: vec![],
        };
        assert!(with_req.has_required());
    }

    #[test]
    fn test_plugin_manifest_all_permissions() {
        let manifest = PluginPermissionManifest {
            plugin_id: "p".to_string(),
            required: vec![PermissionKind::Camera],
            optional: vec![PermissionKind::Camera, PermissionKind::Network],
        };
        let all = manifest.all_permissions();
        assert_eq!(all.len(), 2);
    }

    #[test]
    fn test_plugin_manifest_serialization() {
        let manifest = PluginPermissionManifest {
            plugin_id: "test".to_string(),
            required: vec![PermissionKind::Camera],
            optional: vec![PermissionKind::Network],
        };
        let json = serde_json::to_string(&manifest).unwrap();
        let decoded: PluginPermissionManifest = serde_json::from_str(&json).unwrap();
        assert_eq!(manifest, decoded);
    }

    // === ProcessCapability ===

    #[test]
    fn test_process_capability_memory_ok() {
        let limits = ProcessLimits {
            max_memory_bytes: Some(1024 * 1024),
            ..Default::default()
        };
        let mut cap = ProcessCapability::new(100, "worker", limits);
        assert!(cap.check_memory(512 * 1024));
        assert_eq!(cap.violation_count(), 0);
    }

    #[test]
    fn test_process_capability_memory_exceeded() {
        let limits = ProcessLimits {
            max_memory_bytes: Some(1024),
            ..Default::default()
        };
        let mut cap = ProcessCapability::new(101, "worker", limits);
        assert!(!cap.check_memory(2048));
        assert_eq!(cap.violation_count(), 1);
    }

    #[test]
    fn test_process_capability_cpu_exceeded() {
        let limits = ProcessLimits {
            max_cpu_percent: Some(50.0),
            ..Default::default()
        };
        let mut cap = ProcessCapability::new(102, "renderer", limits);
        assert!(cap.check_cpu(49.9));
        assert!(!cap.check_cpu(75.0));
        assert_eq!(cap.violation_count(), 1);
    }

    #[test]
    fn test_process_capability_network_denied() {
        let limits = ProcessLimits {
            network_allowed: false,
            ..Default::default()
        };
        let mut cap = ProcessCapability::new(103, "sandbox", limits);
        assert!(!cap.check_network());
        assert_eq!(cap.violation_count(), 1);
    }

    #[test]
    fn test_process_capability_network_allowed() {
        let limits = ProcessLimits::default();
        let mut cap = ProcessCapability::new(104, "app", limits);
        assert!(cap.check_network());
        assert_eq!(cap.violation_count(), 0);
    }

    #[test]
    fn test_process_limits_serialization() {
        let limits = ProcessLimits {
            max_memory_bytes: Some(1_000_000),
            max_cpu_percent: Some(80.0),
            max_open_files: Some(256),
            network_allowed: false,
        };
        let json = serde_json::to_string(&limits).unwrap();
        let decoded: ProcessLimits = serde_json::from_str(&json).unwrap();
        assert_eq!(limits, decoded);
    }

    // === NetworkPolicy ===

    #[test]
    fn test_network_policy_allow_all() {
        let policy = NetworkPolicy::AllowAll;
        assert!(policy.check("anything.com"));
    }

    #[test]
    fn test_network_policy_deny_all() {
        let policy = NetworkPolicy::DenyAll;
        assert!(!policy.check("anything.com"));
    }

    #[test]
    fn test_network_policy_allow_list() {
        let policy = NetworkPolicy::AllowList(vec!["api.example.com".to_string()]);
        assert!(policy.check("api.example.com"));
        assert!(!policy.check("evil.com"));
    }

    #[test]
    fn test_network_policy_deny_list() {
        let policy = NetworkPolicy::DenyList(vec!["evil.com".to_string()]);
        assert!(!policy.check("evil.com"));
        assert!(policy.check("good.com"));
    }

    #[test]
    fn test_network_policy_default_is_deny_all() {
        let policy = NetworkPolicy::default();
        assert!(!policy.check("anything.com"));
    }

    #[test]
    fn test_network_policy_serialization() {
        let policy = NetworkPolicy::AllowList(vec!["a.com".to_string(), "b.com".to_string()]);
        let json = serde_json::to_string(&policy).unwrap();
        let decoded: NetworkPolicy = serde_json::from_str(&json).unwrap();
        assert_eq!(policy, decoded);
    }

    // === IpcSchema ===

    #[test]
    fn test_ipc_schema_compatible() {
        let a = IpcSchema::new(3, 1, vec!["ping".to_string()]);
        let b = IpcSchema::new(2, 1, vec!["pong".to_string()]);
        assert!(a.is_compatible(&b));
        assert!(b.is_compatible(&a));
    }

    #[test]
    fn test_ipc_schema_incompatible() {
        let a = IpcSchema::new(3, 3, vec![]);
        let b = IpcSchema::new(1, 1, vec![]);
        assert!(!a.is_compatible(&b));
    }

    #[test]
    fn test_ipc_schema_negotiate() {
        let a = IpcSchema::new(5, 2, vec![]);
        let b = IpcSchema::new(3, 1, vec![]);
        assert_eq!(a.negotiate(&b), Some(3));
    }

    #[test]
    fn test_ipc_schema_negotiate_incompatible() {
        let a = IpcSchema::new(5, 4, vec![]);
        let b = IpcSchema::new(3, 1, vec![]);
        assert_eq!(a.negotiate(&b), None);
    }

    #[test]
    fn test_ipc_schema_common_message_types() {
        let a = IpcSchema::new(1, 1, vec!["ping".to_string(), "data".to_string()]);
        let b = IpcSchema::new(1, 1, vec!["data".to_string(), "pong".to_string()]);
        let common = a.common_message_types(&b);
        assert_eq!(common, vec!["data".to_string()]);
    }

    #[test]
    fn test_ipc_schema_serialization() {
        let schema = IpcSchema::new(2, 1, vec!["hello".to_string()]);
        let json = serde_json::to_string(&schema).unwrap();
        let decoded: IpcSchema = serde_json::from_str(&json).unwrap();
        assert_eq!(schema, decoded);
    }

    #[test]
    fn test_ipc_schema_self_compatible() {
        let schema = IpcSchema::new(1, 1, vec!["msg".to_string()]);
        assert!(schema.is_compatible(&schema));
        assert_eq!(schema.negotiate(&schema), Some(1));
    }

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

    #[test]
    fn test_permission_broker_unregister() {
        let mut broker = PermissionBroker::new();
        let pid = ProcessId(10);
        broker.register_process(pid, ProcessClass::Extension);
        broker.grant(pid, Capability::Notification);
        broker.unregister_process(pid);
        assert_eq!(
            broker.check(pid, &Capability::Notification),
            PermissionResult::Denied
        );
    }

    #[test]
    fn test_permission_broker_revoke_all() {
        let mut broker = PermissionBroker::new();
        let pid = ProcessId(11);
        broker.grant(pid, Capability::Camera);
        broker.grant(pid, Capability::Microphone);
        broker.revoke_all(pid);
        assert!(broker.capabilities(pid).is_empty());
    }

    #[test]
    fn test_credential_entry_clone() {
        let entry = CredentialEntry {
            service: "s".to_string(),
            account: "a".to_string(),
            secret: vec![42],
        };
        let cloned = entry.clone();
        assert_eq!(cloned.secret, vec![42]);
    }

    #[test]
    fn test_access_token_unique_ids() {
        let mut store = AccessTokenStore::new();
        let t1 = store.issue(PathBuf::from("/a"), 0, None);
        let t2 = store.issue(PathBuf::from("/a"), 0, None);
        assert_ne!(t1, t2);
    }

    #[test]
    fn test_process_capability_multiple_violations() {
        let limits = ProcessLimits {
            max_memory_bytes: Some(100),
            max_cpu_percent: Some(10.0),
            network_allowed: false,
            ..Default::default()
        };
        let mut cap = ProcessCapability::new(200, "test", limits);
        cap.check_memory(200);
        cap.check_cpu(50.0);
        cap.check_network();
        assert_eq!(cap.violation_count(), 3);
    }
}