exochain-governance 0.2.0-beta

EXOCHAIN constitutional trust fabric — legislative legitimacy: quorum, clearance, crosscheck, challenge, delegation, deliberation
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
// Copyright 2026 Exochain Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

//! Clearance with independence enforcement.

use std::collections::BTreeMap;

use exo_core::{Did, Timestamp, hash::hash_structured};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::{
    audit::{self, AuditLog},
    errors::GovernanceError,
    quorum::QuorumPolicy,
};

const CLEARANCE_ASSIGNMENT_EVIDENCE_DOMAIN: &str = "exo.governance.clearance_assignment.v1";
const CLEARANCE_ASSIGNMENT_EVIDENCE_SCHEMA_VERSION: u16 = 1;

/// Hierarchical clearance level from None (lowest) to Governor (highest).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum ClearanceLevel {
    None,
    ReadOnly,
    Contributor,
    Reviewer,
    Steward,
    Governor,
}

impl std::fmt::Display for ClearanceLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::None => write!(f, "None"),
            Self::ReadOnly => write!(f, "ReadOnly"),
            Self::Contributor => write!(f, "Contributor"),
            Self::Reviewer => write!(f, "Reviewer"),
            Self::Steward => write!(f, "Steward"),
            Self::Governor => write!(f, "Governor"),
        }
    }
}

/// Per-action policy: required clearance level, optional quorum, and
/// whether independent approvers are mandatory for multi-party actions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionPolicy {
    pub required_level: ClearanceLevel,
    pub quorum_policy: Option<QuorumPolicy>,
    /// When true, the action requires a quorum policy configured with
    /// `min_independent > 0`.  `check_clearance` returns
    /// `InsufficientIndependence` if this constraint is violated.
    #[serde(default)]
    pub independence_required: bool,
}

/// Governance clearance policy: maps action names to their requirements.
///
/// `policy_hash` is the canonical CBOR-SHA-256 digest of this policy at
/// the time it was disclosed; embed it in every `Granted` decision so
/// auditors can verify which policy governed the check.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ClearancePolicy {
    pub actions: BTreeMap<String, ActionPolicy>,
    /// 32-byte disclosed policy hash (all-zeros when not yet set).
    #[serde(default)]
    pub policy_hash: [u8; 32],
}

/// Caller-supplied metadata for a clearance assignment.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClearanceAssignment {
    pub entry_id: Uuid,
    pub timestamp: Timestamp,
    pub assigner: Did,
    pub subject: Did,
    pub level: ClearanceLevel,
}

/// Deterministic receipt proving the registry mutation and its audit evidence.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClearanceAssignmentReceipt {
    pub audit_entry_id: Uuid,
    pub timestamp: Timestamp,
    pub assigner: Did,
    pub subject: Did,
    pub previous_level: ClearanceLevel,
    pub assigned_level: ClearanceLevel,
    pub evidence_hash: [u8; 32],
}

#[derive(Debug, Clone, Serialize)]
struct ClearanceAssignmentEvidencePayload {
    domain: &'static str,
    schema_version: u16,
    audit_entry_id: Uuid,
    timestamp: Timestamp,
    assigner: Did,
    assigner_level: ClearanceLevel,
    subject: Did,
    previous_level: ClearanceLevel,
    assigned_level: ClearanceLevel,
}

/// Maps DIDs to their assigned clearance levels.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ClearanceRegistry {
    entries: BTreeMap<Did, ClearanceLevel>,
}

impl ClearanceRegistry {
    /// Build a registry from a previously verified state snapshot.
    #[must_use]
    pub fn from_verified_snapshot(entries: BTreeMap<Did, ClearanceLevel>) -> Self {
        Self { entries }
    }

    /// Return the immutable clearance state snapshot.
    #[must_use]
    pub fn entries(&self) -> &BTreeMap<Did, ClearanceLevel> {
        &self.entries
    }

    /// Return the clearance level for the given actor, defaulting to `None`.
    #[must_use]
    pub fn get_level(&self, actor: &Did) -> ClearanceLevel {
        self.entries
            .get(actor)
            .copied()
            .unwrap_or(ClearanceLevel::None)
    }

    /// Assign or update clearance through an audited authorization boundary.
    ///
    /// # Errors
    ///
    /// Returns [`GovernanceError::ConstitutionalViolation`] when the assignment
    /// is self-directed, the assigner is not a Governor, or the assignment would
    /// set a level equal to or above the assigner's own clearance. Returns audit
    /// and serialization errors from the underlying append-only audit log.
    pub fn assign_level(
        &mut self,
        audit_log: &mut AuditLog,
        assignment: ClearanceAssignment,
    ) -> Result<ClearanceAssignmentReceipt, GovernanceError> {
        if assignment.assigner == assignment.subject {
            return Err(GovernanceError::ConstitutionalViolation {
                constraint_id: "NoSelfGrant".into(),
                reason: format!(
                    "actor {} cannot assign its own clearance",
                    assignment.assigner
                ),
            });
        }

        let assigner_level = self.get_level(&assignment.assigner);
        if assigner_level != ClearanceLevel::Governor {
            return Err(GovernanceError::ConstitutionalViolation {
                constraint_id: "ClearanceAuthority".into(),
                reason: format!(
                    "assigner {} has clearance {assigner_level}; Governor required",
                    assignment.assigner
                ),
            });
        }
        if assignment.level >= assigner_level {
            return Err(GovernanceError::ConstitutionalViolation {
                constraint_id: "ClearanceCeiling".into(),
                reason: format!(
                    "assigner {} with clearance {assigner_level} cannot assign {}",
                    assignment.assigner, assignment.level
                ),
            });
        }

        let previous_level = self.get_level(&assignment.subject);
        let evidence_hash =
            clearance_assignment_evidence_hash(&assignment, assigner_level, previous_level)?;
        let audit_entry = audit::create_entry(
            audit_log,
            assignment.entry_id,
            assignment.timestamp,
            assignment.assigner.clone(),
            "clearance.assign".into(),
            format!(
                "subject={} previous={previous_level} assigned={}",
                assignment.subject, assignment.level
            ),
            evidence_hash,
        )?;
        audit::append(audit_log, audit_entry)?;

        self.entries
            .insert(assignment.subject.clone(), assignment.level);
        Ok(ClearanceAssignmentReceipt {
            audit_entry_id: assignment.entry_id,
            timestamp: assignment.timestamp,
            assigner: assignment.assigner,
            subject: assignment.subject,
            previous_level,
            assigned_level: assignment.level,
            evidence_hash,
        })
    }
}

fn clearance_assignment_evidence_hash(
    assignment: &ClearanceAssignment,
    assigner_level: ClearanceLevel,
    previous_level: ClearanceLevel,
) -> Result<[u8; 32], GovernanceError> {
    let payload = ClearanceAssignmentEvidencePayload {
        domain: CLEARANCE_ASSIGNMENT_EVIDENCE_DOMAIN,
        schema_version: CLEARANCE_ASSIGNMENT_EVIDENCE_SCHEMA_VERSION,
        audit_entry_id: assignment.entry_id,
        timestamp: assignment.timestamp,
        assigner: assignment.assigner.clone(),
        assigner_level,
        subject: assignment.subject.clone(),
        previous_level,
        assigned_level: assignment.level,
    };
    hash_structured(&payload)
        .map(|hash| *hash.as_bytes())
        .map_err(|e| {
            GovernanceError::Serialization(format!(
                "clearance assignment canonical CBOR hash failed: {e}"
            ))
        })
}

/// Result of a clearance check: granted (with policy hash), denied, or insufficient independence.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ClearanceDecision {
    /// Actor is cleared; `policy_hash` binds this decision to the disclosed policy.
    Granted {
        policy_hash: [u8; 32],
    },
    Denied {
        missing_level: ClearanceLevel,
    },
    InsufficientIndependence {
        details: String,
    },
}

/// Check whether `actor` may perform `action` under `policy`.
///
/// When `independence_required` is set on the action, this function also
/// validates that the action's quorum policy is configured with
/// `min_independent > 0`.  If not, it returns `InsufficientIndependence`
/// rather than a spurious `Granted`, ensuring the policy can never silently
/// bypass independence enforcement.
#[must_use]
pub fn check_clearance(
    actor: &Did,
    action: &str,
    policy: &ClearancePolicy,
    registry: &ClearanceRegistry,
) -> ClearanceDecision {
    let ap = match policy.actions.get(action) {
        Some(ap) => ap,
        None => {
            return ClearanceDecision::Denied {
                missing_level: ClearanceLevel::Governor,
            };
        }
    };
    if registry.get_level(actor) < ap.required_level {
        return ClearanceDecision::Denied {
            missing_level: ap.required_level,
        };
    }
    if ap.independence_required {
        let min_independent = ap
            .quorum_policy
            .as_ref()
            .map(|qp| qp.min_independent)
            .unwrap_or(0);
        if min_independent == 0 {
            return ClearanceDecision::InsufficientIndependence {
                details: format!(
                    "action '{action}' requires independence but quorum policy has \
                     min_independent=0 (or no quorum policy configured)"
                ),
            };
        }
    }
    ClearanceDecision::Granted {
        policy_hash: policy.policy_hash,
    }
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;
    fn did(name: &str) -> Did {
        Did::new(&format!("did:exo:{name}")).expect("ok")
    }

    fn make_action_policy(level: ClearanceLevel) -> ActionPolicy {
        ActionPolicy {
            required_level: level,
            quorum_policy: None,
            independence_required: false,
        }
    }

    fn timestamp(ms: u64) -> exo_core::Timestamp {
        exo_core::Timestamp::new(ms, 0)
    }

    fn audit_id(value: u128) -> uuid::Uuid {
        uuid::Uuid::from_u128(value)
    }

    fn assignment(
        assigner: Did,
        subject: Did,
        level: ClearanceLevel,
        entry_id: uuid::Uuid,
    ) -> ClearanceAssignment {
        ClearanceAssignment {
            entry_id,
            timestamp: timestamp(10_000),
            assigner,
            subject,
            level,
        }
    }

    fn snapshot_registry(entries: Vec<(Did, ClearanceLevel)>) -> ClearanceRegistry {
        let mut levels = BTreeMap::new();
        for (did, level) in entries {
            assert!(
                levels.insert(did, level).is_none(),
                "test registry snapshots must not contain duplicates"
            );
        }
        ClearanceRegistry::from_verified_snapshot(levels)
    }

    fn production_source() -> &'static str {
        include_str!("clearance.rs")
            .split("#[cfg(test)]")
            .next()
            .expect("production source")
    }

    fn setup() -> (ClearancePolicy, ClearanceRegistry) {
        let mut p = ClearancePolicy::default();
        p.actions
            .insert("read".into(), make_action_policy(ClearanceLevel::ReadOnly));
        p.actions.insert(
            "write".into(),
            make_action_policy(ClearanceLevel::Contributor),
        );
        p.actions.insert(
            "review".into(),
            make_action_policy(ClearanceLevel::Reviewer),
        );
        p.actions.insert(
            "govern".into(),
            make_action_policy(ClearanceLevel::Governor),
        );
        let r = snapshot_registry(vec![
            (did("alice"), ClearanceLevel::Governor),
            (did("bob"), ClearanceLevel::Contributor),
            (did("carol"), ClearanceLevel::ReadOnly),
        ]);
        (p, r)
    }

    fn granted() -> ClearanceDecision {
        ClearanceDecision::Granted {
            policy_hash: [0u8; 32],
        }
    }

    #[test]
    fn governor_can_do_everything() {
        let (p, r) = setup();
        let a = did("alice");
        assert_eq!(check_clearance(&a, "read", &p, &r), granted());
        assert_eq!(check_clearance(&a, "write", &p, &r), granted());
        assert_eq!(check_clearance(&a, "review", &p, &r), granted());
        assert_eq!(check_clearance(&a, "govern", &p, &r), granted());
    }
    #[test]
    fn contributor_cannot_review() {
        let (p, r) = setup();
        let b = did("bob");
        assert_eq!(check_clearance(&b, "read", &p, &r), granted());
        assert_eq!(check_clearance(&b, "write", &p, &r), granted());
        assert_eq!(
            check_clearance(&b, "review", &p, &r),
            ClearanceDecision::Denied {
                missing_level: ClearanceLevel::Reviewer
            }
        );
        assert_eq!(
            check_clearance(&b, "govern", &p, &r),
            ClearanceDecision::Denied {
                missing_level: ClearanceLevel::Governor
            }
        );
    }
    #[test]
    fn readonly_can_only_read() {
        let (p, r) = setup();
        let c = did("carol");
        assert_eq!(check_clearance(&c, "read", &p, &r), granted());
        assert_eq!(
            check_clearance(&c, "write", &p, &r),
            ClearanceDecision::Denied {
                missing_level: ClearanceLevel::Contributor
            }
        );
    }
    #[test]
    fn unknown_actor_denied() {
        let (p, r) = setup();
        assert_eq!(
            check_clearance(&did("unknown"), "read", &p, &r),
            ClearanceDecision::Denied {
                missing_level: ClearanceLevel::ReadOnly
            }
        );
    }
    #[test]
    fn unknown_action_denied() {
        let (p, r) = setup();
        assert_eq!(
            check_clearance(&did("alice"), "nonexistent", &p, &r),
            ClearanceDecision::Denied {
                missing_level: ClearanceLevel::Governor
            }
        );
    }
    #[test]
    fn level_ordering() {
        assert!(ClearanceLevel::None < ClearanceLevel::ReadOnly);
        assert!(ClearanceLevel::ReadOnly < ClearanceLevel::Contributor);
        assert!(ClearanceLevel::Contributor < ClearanceLevel::Reviewer);
        assert!(ClearanceLevel::Reviewer < ClearanceLevel::Steward);
        assert!(ClearanceLevel::Steward < ClearanceLevel::Governor);
    }
    #[test]
    fn level_display() {
        assert_eq!(ClearanceLevel::None.to_string(), "None");
        assert_eq!(ClearanceLevel::Governor.to_string(), "Governor");
    }
    #[test]
    fn registry_defaults_to_none() {
        assert_eq!(
            ClearanceRegistry::default().get_level(&did("nobody")),
            ClearanceLevel::None
        );
    }
    #[test]
    fn registry_set_get() {
        let d = did("test");
        let r = snapshot_registry(vec![(d.clone(), ClearanceLevel::Steward)]);
        assert_eq!(r.get_level(&d), ClearanceLevel::Steward);
    }

    #[test]
    fn registry_has_no_public_unchecked_mutation_surface() {
        let source = production_source();
        assert!(
            !source.contains("pub entries:"),
            "clearance entries must not be publicly mutable"
        );
        assert!(
            !source.contains("pub fn set_level("),
            "clearance assignment must pass through the audited authorization boundary"
        );
    }

    #[test]
    fn governor_assignment_updates_registry_and_appends_audit_entry() {
        let governor = did("governor");
        let subject = did("delegate");
        let mut registry = snapshot_registry(vec![(governor.clone(), ClearanceLevel::Governor)]);
        let mut audit_log = crate::audit::AuditLog::new();

        let receipt = registry
            .assign_level(
                &mut audit_log,
                assignment(
                    governor.clone(),
                    subject.clone(),
                    ClearanceLevel::Steward,
                    audit_id(0xC1EA),
                ),
            )
            .expect("governor may assign lower clearance");

        assert_eq!(registry.get_level(&subject), ClearanceLevel::Steward);
        assert_eq!(receipt.subject, subject);
        assert_eq!(receipt.previous_level, ClearanceLevel::None);
        assert_eq!(receipt.assigned_level, ClearanceLevel::Steward);
        assert_eq!(audit_log.len(), 1);
        assert_eq!(audit_log.entries[0].actor, governor);
        assert_eq!(audit_log.entries[0].action, "clearance.assign");
        assert_eq!(audit_log.entries[0].evidence_hash, receipt.evidence_hash);
        crate::audit::verify_chain(&audit_log).expect("assignment audit entry must chain");
    }

    #[test]
    fn clearance_assignment_rejects_self_grant_without_mutation_or_audit() {
        let actor = did("actor");
        let mut registry = snapshot_registry(vec![(actor.clone(), ClearanceLevel::Governor)]);
        let mut audit_log = crate::audit::AuditLog::new();

        let err = registry
            .assign_level(
                &mut audit_log,
                assignment(
                    actor.clone(),
                    actor.clone(),
                    ClearanceLevel::Steward,
                    audit_id(0xC1EB),
                ),
            )
            .expect_err("actors must not assign their own clearance");

        assert!(matches!(
            err,
            crate::GovernanceError::ConstitutionalViolation { .. }
        ));
        assert_eq!(registry.get_level(&actor), ClearanceLevel::Governor);
        assert!(audit_log.is_empty());
    }

    #[test]
    fn clearance_assignment_enforces_superior_clearance_ceiling() {
        let steward = did("steward");
        let subject = did("subject");
        let mut registry = snapshot_registry(vec![(steward.clone(), ClearanceLevel::Steward)]);
        let mut audit_log = crate::audit::AuditLog::new();

        let err = registry
            .assign_level(
                &mut audit_log,
                assignment(
                    steward,
                    subject.clone(),
                    ClearanceLevel::Steward,
                    audit_id(0xC1EC),
                ),
            )
            .expect_err("assigner must not assign its own level or higher");

        assert!(matches!(
            err,
            crate::GovernanceError::ConstitutionalViolation { .. }
        ));
        assert_eq!(registry.get_level(&subject), ClearanceLevel::None);
        assert!(audit_log.is_empty());
    }

    #[test]
    fn clearance_assignment_rejects_invalid_audit_metadata_before_mutation() {
        let governor = did("governor");
        let subject = did("subject");
        let mut registry = snapshot_registry(vec![(governor.clone(), ClearanceLevel::Governor)]);
        let mut audit_log = crate::audit::AuditLog::new();

        let err = registry
            .assign_level(
                &mut audit_log,
                assignment(
                    governor,
                    subject.clone(),
                    ClearanceLevel::Reviewer,
                    uuid::Uuid::nil(),
                ),
            )
            .expect_err("assignment requires caller-supplied audit metadata");

        assert!(matches!(
            err,
            crate::GovernanceError::InvalidGovernanceMetadata { .. }
        ));
        assert_eq!(registry.get_level(&subject), ClearanceLevel::None);
        assert!(audit_log.is_empty());
    }
    #[test]
    fn insufficient_independence_variant() {
        let d = ClearanceDecision::InsufficientIndependence {
            details: "test".into(),
        };
        assert!(matches!(
            d,
            ClearanceDecision::InsufficientIndependence { .. }
        ));
    }

    // ── WO-004: independence_required enforcement ─────────────────────────────

    #[test]
    fn independence_required_without_quorum_policy_returns_insufficient() {
        let mut p = ClearancePolicy::default();
        p.actions.insert(
            "critical".into(),
            ActionPolicy {
                required_level: ClearanceLevel::Reviewer,
                quorum_policy: None,
                independence_required: true,
            },
        );
        let r = snapshot_registry(vec![(did("alice"), ClearanceLevel::Governor)]);
        assert!(matches!(
            check_clearance(&did("alice"), "critical", &p, &r),
            ClearanceDecision::InsufficientIndependence { .. }
        ));
    }

    #[test]
    fn independence_required_with_zero_min_independent_returns_insufficient() {
        use exo_core::Timestamp;

        use crate::quorum::{QuorumPolicy, Role};
        let mut p = ClearancePolicy::default();
        p.actions.insert(
            "critical".into(),
            ActionPolicy {
                required_level: ClearanceLevel::Reviewer,
                quorum_policy: Some(QuorumPolicy {
                    min_approvals: 3,
                    min_independent: 0, // misconfigured
                    required_roles: vec![Role::Steward],
                    timeout: Timestamp::new(999_999, 0),
                }),
                independence_required: true,
            },
        );
        let r = snapshot_registry(vec![(did("alice"), ClearanceLevel::Governor)]);
        assert!(matches!(
            check_clearance(&did("alice"), "critical", &p, &r),
            ClearanceDecision::InsufficientIndependence { .. }
        ));
    }

    #[test]
    fn independence_required_with_valid_quorum_policy_grants() {
        use exo_core::Timestamp;

        use crate::quorum::{QuorumPolicy, Role};
        let hash = [1u8; 32];
        let mut p = ClearancePolicy {
            actions: BTreeMap::new(),
            policy_hash: hash,
        };
        p.actions.insert(
            "critical".into(),
            ActionPolicy {
                required_level: ClearanceLevel::Reviewer,
                quorum_policy: Some(QuorumPolicy {
                    min_approvals: 3,
                    min_independent: 2,
                    required_roles: vec![Role::Steward],
                    timeout: Timestamp::new(999_999, 0),
                }),
                independence_required: true,
            },
        );
        let r = snapshot_registry(vec![(did("alice"), ClearanceLevel::Governor)]);
        assert_eq!(
            check_clearance(&did("alice"), "critical", &p, &r),
            ClearanceDecision::Granted { policy_hash: hash }
        );
    }

    #[test]
    fn policy_hash_embedded_in_granted() {
        let hash = [42u8; 32];
        let mut p = ClearancePolicy {
            actions: BTreeMap::new(),
            policy_hash: hash,
        };
        p.actions
            .insert("read".into(), make_action_policy(ClearanceLevel::ReadOnly));
        let r = snapshot_registry(vec![(did("alice"), ClearanceLevel::Governor)]);
        assert_eq!(
            check_clearance(&did("alice"), "read", &p, &r),
            ClearanceDecision::Granted { policy_hash: hash }
        );
    }
}