khive-capability 0.15.0

Sealed grants, durable authenticated envelopes, and attenuating capability delegation.
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
use super::{
    bound, collection, nonempty, sorted_unique, EnvelopeError, EnvelopeResult, UnsupportedRight,
    MAX_RECORD_BYTES,
};
use khive_types::Timestamp;
use lion_core::{Right, Rights};
use std::collections::BTreeMap;
use uuid::Uuid;

#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Principal {
    pub kind: String,
    pub id: String,
    pub leg: Option<String>,
}
impl Principal {
    pub fn validate(&self) -> EnvelopeResult<()> {
        nonempty(&self.kind)?;
        nonempty(&self.id)?;
        if let Some(leg) = &self.leg {
            let bytes = leg.as_bytes();
            if bytes.is_empty()
                || bytes.len() > 64
                || !bytes[0].is_ascii_alphanumeric()
                || !bytes[bytes.len() - 1].is_ascii_alphanumeric()
                || bytes
                    .iter()
                    .any(|b| !b.is_ascii_lowercase() && !b.is_ascii_digit() && *b != b'-')
            {
                return Err(EnvelopeError::InvalidInput("invalid structural leg label"));
            }
        }
        Ok(())
    }
    pub fn parent(&self) -> Self {
        Self {
            kind: self.kind.clone(),
            id: self.id.clone(),
            leg: None,
        }
    }
}

#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ContractDigest(pub [u8; 32]);
impl ContractDigest {
    pub fn blake3(bytes: &[u8]) -> Self {
        Self(*blake3::hash(bytes).as_bytes())
    }
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ContractRef {
    pub name: String,
    pub version: u32,
    pub digest: ContractDigest,
}
impl ContractRef {
    fn validate(&self) -> EnvelopeResult<()> {
        nonempty(&self.name)
    }
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ExactBytes(pub Vec<u8>);
impl ExactBytes {
    fn validate(&self) -> EnvelopeResult<()> {
        bound("reference bytes", self.0.len(), MAX_RECORD_BYTES)?;
        if self.0.is_empty() {
            return Err(EnvelopeError::InvalidInput("empty exact reference"));
        }
        Ok(())
    }
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct RepositoryIdentity {
    pub forge: String,
    pub immutable_id: ExactBytes,
}
impl RepositoryIdentity {
    fn validate(&self) -> EnvelopeResult<()> {
        nonempty(&self.forge)?;
        self.immutable_id.validate()
    }
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Validity {
    pub not_before: Timestamp,
    pub expires_at: Option<Timestamp>,
}
impl Validity {
    pub fn validate(&self) -> EnvelopeResult<()> {
        if self.expires_at.is_some_and(|end| end <= self.not_before) {
            return Err(EnvelopeError::InvalidInput("expiry must follow not_before"));
        }
        Ok(())
    }
    pub fn contains(&self, now: Timestamp) -> bool {
        now >= self.not_before && self.expires_at.is_none_or(|end| now < end)
    }
    pub fn covers(&self, child: &Self) -> bool {
        self.not_before <= child.not_before
            && match (self.expires_at, child.expires_at) {
                (None, _) => true,
                (Some(_), None) => false,
                (Some(a), Some(b)) => b <= a,
            }
    }
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct RunRef(pub Uuid);
impl RunRef {
    pub fn parse(value: &str) -> EnvelopeResult<Self> {
        let id =
            Uuid::parse_str(value).map_err(|_| EnvelopeError::InvalidInput("invalid run UUID"))?;
        if id.to_string() != value {
            return Err(EnvelopeError::InvalidInput("noncanonical run UUID"));
        }
        Ok(Self(id))
    }
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct PhaseVisit {
    pub phase_id: String,
    pub round: i64,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ActionOccurrence {
    pub visit: PhaseVisit,
    pub action_name: String,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum ArgumentValue {
    String(String),
    Integer(i64),
    Duration(i64),
    Bool(bool),
    Word(String),
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum ArgumentType {
    String,
    Integer,
    Duration,
    Bool,
    Word,
}
impl ArgumentValue {
    fn kind(&self) -> ArgumentType {
        match self {
            Self::String(_) => ArgumentType::String,
            Self::Integer(_) => ArgumentType::Integer,
            Self::Duration(_) => ArgumentType::Duration,
            Self::Bool(_) => ArgumentType::Bool,
            Self::Word(_) => ArgumentType::Word,
        }
    }
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ArgumentBinding {
    pub name: String,
    pub value: ArgumentValue,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ExactArguments(pub Vec<ArgumentBinding>);
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ArgumentSpec {
    pub name: String,
    pub kind: ArgumentType,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum ProducerClass {
    Actor,
    Instrument,
    Platform,
    System,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum SubmissionChannel {
    Observe,
    Sample,
    Internal,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum SubjectSelector {
    Exact(Vec<ExactBytes>),
    Repositories(Vec<RepositoryIdentity>),
    AllSubjectsOfKind,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct SubjectScope {
    pub kind: ContractRef,
    pub selector: SubjectSelector,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct SourceBinding {
    pub contract: ContractRef,
    pub identities: Vec<ExactBytes>,
    pub arguments: Vec<ExactArguments>,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum ReportingBinding {
    Role(String),
    Initiator(Principal),
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum SubmissionBinding {
    ActorReport {
        run: RunRef,
        definition: ContractDigest,
        role: String,
    },
    ActionResult {
        run: RunRef,
        definition: ContractDigest,
        occurrence: ActionOccurrence,
        reporter: ReportingBinding,
        arguments: ExactArguments,
    },
    Instrument(SourceBinding),
    PlatformRelay {
        source: SourceBinding,
        response_contract: ContractRef,
    },
    ClaimTimePlatformBundle,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ObservationPermission {
    pub policy_domain: String,
    pub entry: ContractRef,
    pub producer_class: ProducerClass,
    pub channel: SubmissionChannel,
    pub subject: SubjectScope,
    pub binding: SubmissionBinding,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ObservationScope {
    pub permissions: Vec<ObservationPermission>,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum AdminTarget {
    Read,
    Create,
    Write,
    Delete,
    Send,
    RecordObservation(ObservationScope),
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct AdminScope {
    pub namespace: String,
    pub target: AdminTarget,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum RightSpec {
    Read,
    Create,
    Write,
    Delete,
    Send,
    RecordObservation(ObservationScope),
    GrantAdmin(AdminScope),
}

#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ReferenceFormat {
    Opaque,
    CanonicalUuid,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug)]
pub struct SubjectContract {
    pub identity: ContractRef,
    pub reference_format: ReferenceFormat,
    pub repository_independent: bool,
    pub repositories: Vec<RepositoryIdentity>,
    pub membership: Vec<(ExactBytes, RepositoryIdentity)>,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug)]
pub struct SourceContract {
    pub identity: ContractRef,
    pub reference_format: ReferenceFormat,
    pub identities: Vec<ExactBytes>,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug)]
pub struct EntryContract {
    pub identity: ContractRef,
    pub class: ProducerClass,
    pub subject_kind: ContractRef,
    pub source_contract: Option<ContractRef>,
    pub response_contract: Option<ContractRef>,
    pub authority_arguments: Vec<ArgumentSpec>,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug)]
pub struct DeclaredAction {
    pub entry: ContractRef,
    pub phase_id: String,
    pub action_name: String,
    pub by: Option<String>,
    pub phase_owner: Option<String>,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug)]
pub struct DefinitionContract {
    pub digest: ContractDigest,
    pub reporting_roles: Vec<(ContractRef, String)>,
    pub actions: Vec<DeclaredAction>,
}
#[cfg_attr(feature = "authority-store", derive(serde::Serialize))]
#[derive(Clone, Debug)]
pub struct RunContract {
    pub policy_domain: String,
    pub id: RunRef,
    pub definition: ContractDigest,
    pub bindings: Vec<(String, Principal)>,
    pub initiating_principal: Principal,
    pub subject_kind: ContractRef,
    pub subject: ExactBytes,
}

/// Provisioned contract snapshots, never parsed from a grant request.
/// Run facts must come from trusted immutable run bindings; a later authority
/// service still discharges current run/subject obligations in its transaction.
#[derive(Clone, Debug)]
pub struct ClosedCatalog {
    subjects: BTreeMap<ContractRef, SubjectContract>,
    sources: BTreeMap<ContractRef, SourceContract>,
    entries: BTreeMap<ContractRef, EntryContract>,
    definitions: BTreeMap<ContractDigest, DefinitionContract>,
    runs: BTreeMap<RunRef, RunContract>,
}
impl ClosedCatalog {
    pub fn provision(
        subjects: Vec<SubjectContract>,
        sources: Vec<SourceContract>,
        entries: Vec<EntryContract>,
        definitions: Vec<DefinitionContract>,
        runs: Vec<RunContract>,
    ) -> EnvelopeResult<Self> {
        collection(&subjects, false)?;
        collection(&sources, false)?;
        collection(&entries, true)?;
        collection(&definitions, false)?;
        collection(&runs, false)?;
        let mut catalog = Self {
            subjects: BTreeMap::new(),
            sources: BTreeMap::new(),
            entries: BTreeMap::new(),
            definitions: BTreeMap::new(),
            runs: BTreeMap::new(),
        };
        for subject in subjects {
            subject.identity.validate()?;
            sorted_unique(&subject.repositories, false)?;
            collection(&subject.membership, false)?;
            if subject.repository_independent
                && (!subject.repositories.is_empty() || !subject.membership.is_empty())
            {
                return Err(EnvelopeError::InvalidInput(
                    "repository-independent contract has repository bindings",
                ));
            }
            for repository in &subject.repositories {
                repository.validate()?;
            }
            let mut members = BTreeMap::new();
            for (reference, repository) in &subject.membership {
                validate_reference(reference, subject.reference_format)?;
                if !subject.repositories.contains(repository)
                    || members.insert(reference, repository).is_some()
                {
                    return Err(EnvelopeError::InvalidInput(
                        "ambiguous or unknown repository membership",
                    ));
                }
            }
            if catalog
                .subjects
                .insert(subject.identity.clone(), subject)
                .is_some()
            {
                return Err(EnvelopeError::InvalidInput("duplicate subject contract"));
            }
        }
        for source in sources {
            source.identity.validate()?;
            sorted_unique(&source.identities, true)?;
            for reference in &source.identities {
                validate_reference(reference, source.reference_format)?;
            }
            if catalog
                .sources
                .insert(source.identity.clone(), source)
                .is_some()
            {
                return Err(EnvelopeError::InvalidInput("duplicate source contract"));
            }
        }
        for entry in entries {
            entry.identity.validate()?;
            sorted_unique(&entry.authority_arguments, false)?;
            if entry
                .authority_arguments
                .windows(2)
                .any(|p| p[0].name == p[1].name)
            {
                return Err(EnvelopeError::InvalidInput("duplicate argument name"));
            }
            for arg in &entry.authority_arguments {
                nonempty(&arg.name)?;
            }
            if !catalog.subjects.contains_key(&entry.subject_kind) {
                return Err(EnvelopeError::InvalidInput(
                    "unknown entry subject contract",
                ));
            }
            if let Some(source) = &entry.source_contract {
                if !catalog.sources.contains_key(source) {
                    return Err(EnvelopeError::InvalidInput("unknown entry source contract"));
                }
            }
            if entry.class == ProducerClass::Platform {
                entry
                    .response_contract
                    .as_ref()
                    .ok_or(EnvelopeError::InvalidInput(
                        "platform entry lacks response contract",
                    ))?
                    .validate()?;
            } else if entry.response_contract.is_some() {
                return Err(EnvelopeError::InvalidInput(
                    "non-platform response contract",
                ));
            }
            if catalog
                .entries
                .insert(entry.identity.clone(), entry)
                .is_some()
            {
                return Err(EnvelopeError::InvalidInput("duplicate entry contract"));
            }
        }
        for definition in definitions {
            collection(&definition.reporting_roles, false)?;
            collection(&definition.actions, false)?;
            for (entry, role) in &definition.reporting_roles {
                if !catalog.entries.contains_key(entry) {
                    return Err(EnvelopeError::InvalidInput(
                        "unknown reporting entry contract",
                    ));
                }
                nonempty(role)?;
            }
            for action in &definition.actions {
                if !catalog.entries.contains_key(&action.entry) {
                    return Err(EnvelopeError::InvalidInput("unknown action entry"));
                }
                nonempty(&action.phase_id)?;
                nonempty(&action.action_name)?;
                if let Some(role) = &action.by {
                    nonempty(role)?;
                }
                if let Some(role) = &action.phase_owner {
                    nonempty(role)?;
                }
            }
            if catalog
                .definitions
                .insert(definition.digest, definition)
                .is_some()
            {
                return Err(EnvelopeError::InvalidInput("duplicate definition contract"));
            }
        }
        for run in runs {
            nonempty(&run.policy_domain)?;
            collection(&run.bindings, false)?;
            run.initiating_principal.validate()?;
            if !catalog.definitions.contains_key(&run.definition) {
                return Err(EnvelopeError::InvalidInput("unknown run definition"));
            }
            let subject = catalog
                .subjects
                .get(&run.subject_kind)
                .ok_or(EnvelopeError::InvalidInput("unknown run subject kind"))?;
            validate_reference(&run.subject, subject.reference_format)?;
            let mut roles = BTreeMap::new();
            for (role, principal) in &run.bindings {
                nonempty(role)?;
                principal.validate()?;
                if roles.insert(role, principal).is_some() {
                    return Err(EnvelopeError::InvalidInput("duplicate run role"));
                }
            }
            if catalog.runs.insert(run.id, run).is_some() {
                return Err(EnvelopeError::InvalidInput("duplicate run contract"));
            }
        }
        Ok(catalog)
    }

    pub fn validate_rights(
        &self,
        rights: &[RightSpec],
        namespace: &str,
        grantee: &Principal,
    ) -> EnvelopeResult<Rights> {
        self.validate_rights_inner(rights, namespace, grantee, true)
    }
    // A finite bootstrap source is a bound on future grants, not registration of
    // its owner as the producer. Emitted grants still use validate_rights above.
    pub(crate) fn validate_source_rights(
        &self,
        rights: &[RightSpec],
        namespace: &str,
        owner: &Principal,
    ) -> EnvelopeResult<Rights> {
        self.validate_rights_inner(rights, namespace, owner, false)
    }
    fn validate_rights_inner(
        &self,
        rights: &[RightSpec],
        namespace: &str,
        grantee: &Principal,
        bind_producer: bool,
    ) -> EnvelopeResult<Rights> {
        nonempty(namespace)?;
        grantee.validate()?;
        sorted_unique(rights, true)?;
        let mut carriers = Rights::empty();
        for right in rights {
            let scope = match right {
                RightSpec::RecordObservation(scope) => {
                    carriers = carriers.with(Right::Execute);
                    scope
                }
                RightSpec::GrantAdmin(admin) => {
                    if grantee.leg.is_some() {
                        return Err(EnvelopeError::InvalidInput(
                            "subactor cannot hold administration",
                        ));
                    }
                    if admin.namespace != namespace {
                        return Err(EnvelopeError::InvalidInput(
                            "admin namespace differs from grant",
                        ));
                    }
                    let AdminTarget::RecordObservation(scope) = &admin.target else {
                        return Err(EnvelopeError::RightNotSupported(
                            UnsupportedRight::OrdinaryNamespace,
                        ));
                    };
                    carriers = carriers.with(Right::Delegate);
                    scope
                }
                _ => {
                    return Err(EnvelopeError::RightNotSupported(
                        UnsupportedRight::OrdinaryNamespace,
                    ))
                }
            };
            collection(&scope.permissions, true)?;
            for (index, permission) in scope.permissions.iter().enumerate() {
                if scope.permissions[..index].contains(permission) {
                    return Err(EnvelopeError::InvalidInput(
                        "duplicate observation permission",
                    ));
                }
                self.validate_permission(
                    permission,
                    if bind_producer && matches!(right, RightSpec::RecordObservation(_)) {
                        Some(grantee)
                    } else {
                        None
                    },
                )?;
            }
        }
        Ok(carriers)
    }

    #[cfg(feature = "authority-store")]
    pub(crate) fn covers_observation_permission(
        &self,
        held: &ObservationPermission,
        wanted: &ObservationPermission,
    ) -> bool {
        held.policy_domain == wanted.policy_domain
            && held.entry == wanted.entry
            && held.producer_class == wanted.producer_class
            && held.channel == wanted.channel
            && self.covers_subject(&held.subject, &wanted.subject)
            && binding_covers(&held.binding, &wanted.binding)
    }

    pub fn covers_rights(&self, parent: &[RightSpec], requested: &[RightSpec]) -> bool {
        requested.iter().all(|child| {
            parent.iter().any(|held| match (held, child) {
                (RightSpec::RecordObservation(a), RightSpec::RecordObservation(b)) => {
                    self.covers_scope(a, b)
                }
                (RightSpec::GrantAdmin(a), RightSpec::GrantAdmin(b))
                    if a.namespace == b.namespace =>
                {
                    match (&a.target, &b.target) {
                        (AdminTarget::RecordObservation(a), AdminTarget::RecordObservation(b)) => {
                            self.covers_scope(a, b)
                        }
                        _ => false,
                    }
                }
                _ => false,
            })
        })
    }
    fn covers_scope(&self, parent: &ObservationScope, child: &ObservationScope) -> bool {
        child.permissions.iter().all(|wanted| {
            parent.permissions.iter().any(|held| {
                held.policy_domain == wanted.policy_domain
                    && held.entry == wanted.entry
                    && held.producer_class == wanted.producer_class
                    && held.channel == wanted.channel
                    && self.covers_subject(&held.subject, &wanted.subject)
                    && binding_covers(&held.binding, &wanted.binding)
            })
        })
    }
    fn validate_permission(
        &self,
        p: &ObservationPermission,
        grantee: Option<&Principal>,
    ) -> EnvelopeResult<()> {
        nonempty(&p.policy_domain)?;
        if matches!(p.binding, SubmissionBinding::ClaimTimePlatformBundle) {
            return Err(EnvelopeError::RightNotSupported(
                UnsupportedRight::ClaimTimePlatformBundleInM1,
            ));
        }
        if p.channel != SubmissionChannel::Observe || p.producer_class == ProducerClass::System {
            return Err(EnvelopeError::RightNotSupported(
                UnsupportedRight::InternalProducer,
            ));
        }
        let entry = self
            .entries
            .get(&p.entry)
            .ok_or(EnvelopeError::InvalidInput("unknown entry contract"))?;
        if p.producer_class != entry.class || p.subject.kind != entry.subject_kind {
            return Err(EnvelopeError::InvalidInput(
                "entry class or subject contract mismatch",
            ));
        }
        self.validate_subject(&p.subject)?;
        match &p.binding {
            SubmissionBinding::Instrument(source)
            | SubmissionBinding::PlatformRelay { source, .. } => {
                if let SubmissionBinding::PlatformRelay {
                    response_contract, ..
                } = &p.binding
                {
                    if entry.response_contract.as_ref() != Some(response_contract) {
                        return Err(EnvelopeError::InvalidInput(
                            "platform response contract mismatch",
                        ));
                    }
                }
                let class = if matches!(p.binding, SubmissionBinding::Instrument(_)) {
                    ProducerClass::Instrument
                } else {
                    ProducerClass::Platform
                };
                if p.producer_class != class
                    || entry.source_contract.as_ref() != Some(&source.contract)
                {
                    return Err(EnvelopeError::InvalidInput(
                        "source binding contract mismatch",
                    ));
                }
                let known = self
                    .sources
                    .get(&source.contract)
                    .ok_or(EnvelopeError::InvalidInput("unknown source contract"))?;
                sorted_unique(&source.identities, true)?;
                sorted_unique(&source.arguments, true)?;
                for identity in &source.identities {
                    validate_reference(identity, known.reference_format)?;
                    if !known.identities.contains(identity) {
                        return Err(EnvelopeError::InvalidInput("unknown source identity"));
                    }
                }
                for args in &source.arguments {
                    validate_arguments(args, &entry.authority_arguments)?;
                }
            }
            SubmissionBinding::ActorReport {
                run,
                definition,
                role,
            } => {
                let (run, def) = self.actor_context(p, *run, *definition)?;
                let roles: Vec<_> = def
                    .reporting_roles
                    .iter()
                    .filter(|(identity, _)| identity == &p.entry)
                    .collect();
                if roles.len() != 1
                    || roles[0].1 != *role
                    || grantee.is_some_and(|p| resolve_role(run, role).ok() != Some(p))
                {
                    return Err(EnvelopeError::InvalidInput(
                        "reporting role does not resolve to grantee",
                    ));
                }
                resolve_role(run, role)?;
                if !entry.authority_arguments.is_empty() {
                    return Err(EnvelopeError::InvalidInput(
                        "actor report lacks required argument binding",
                    ));
                }
            }
            SubmissionBinding::ActionResult {
                run,
                definition,
                occurrence,
                reporter,
                arguments,
            } => {
                let (run, def) = self.actor_context(p, *run, *definition)?;
                nonempty(&occurrence.visit.phase_id)?;
                nonempty(&occurrence.action_name)?;
                if occurrence.visit.round < 1 {
                    return Err(EnvelopeError::InvalidInput(
                        "round zero is not a phase visit",
                    ));
                }
                let actions: Vec<_> = def
                    .actions
                    .iter()
                    .filter(|a| {
                        a.phase_id == occurrence.visit.phase_id
                            && a.action_name == occurrence.action_name
                    })
                    .collect();
                if actions.len() != 1 {
                    return Err(EnvelopeError::InvalidInput(
                        "ambiguous or unknown action occurrence",
                    ));
                }
                let action = actions[0];
                if action.entry != p.entry {
                    return Err(EnvelopeError::InvalidInput(
                        "action entry differs from permission",
                    ));
                }
                let expected = match action.by.as_ref().or(action.phase_owner.as_ref()) {
                    Some(role) => {
                        if grantee.is_some_and(|p| resolve_role(run, role).ok() != Some(p)) {
                            return Err(EnvelopeError::InvalidInput(
                                "action reporter does not resolve to grantee",
                            ));
                        }
                        {
                            resolve_role(run, role)?;
                            ReportingBinding::Role(role.clone())
                        }
                    }
                    None => ReportingBinding::Initiator(run.initiating_principal.clone()),
                };
                if &expected != reporter
                    || (matches!(expected, ReportingBinding::Initiator(_))
                        && grantee.is_some_and(|p| &run.initiating_principal != p))
                {
                    return Err(EnvelopeError::InvalidInput(
                        "action reporting binding mismatch",
                    ));
                }
                validate_arguments(arguments, &entry.authority_arguments)?;
            }
            SubmissionBinding::ClaimTimePlatformBundle => unreachable!(),
        }
        Ok(())
    }
    fn actor_context(
        &self,
        p: &ObservationPermission,
        id: RunRef,
        digest: ContractDigest,
    ) -> EnvelopeResult<(&RunContract, &DefinitionContract)> {
        if p.producer_class != ProducerClass::Actor {
            return Err(EnvelopeError::InvalidInput(
                "actor binding has non-actor class",
            ));
        }
        let run = self
            .runs
            .get(&id)
            .ok_or(EnvelopeError::InvalidInput("unknown named run"))?;
        let definition = self
            .definitions
            .get(&digest)
            .ok_or(EnvelopeError::InvalidInput("unknown pinned definition"))?;
        if run.policy_domain != p.policy_domain
            || run.definition != digest
            || run.subject_kind != p.subject.kind
            || !self.subject_contains(&p.subject, &run.subject)
        {
            return Err(EnvelopeError::InvalidInput(
                "run subject or definition mismatch",
            ));
        }
        Ok((run, definition))
    }
    fn validate_subject(&self, scope: &SubjectScope) -> EnvelopeResult<()> {
        let contract = self
            .subjects
            .get(&scope.kind)
            .ok_or(EnvelopeError::InvalidInput("unknown subject kind"))?;
        match &scope.selector {
            SubjectSelector::Exact(refs) => {
                sorted_unique(refs, true)?;
                for reference in refs {
                    validate_reference(reference, contract.reference_format)?;
                }
            }
            SubjectSelector::Repositories(repositories) => {
                sorted_unique(repositories, true)?;
                if contract.repository_independent
                    || repositories
                        .iter()
                        .any(|r| !contract.repositories.contains(r))
                {
                    return Err(EnvelopeError::InvalidInput(
                        "unknown or unsupported repository selector",
                    ));
                }
            }
            SubjectSelector::AllSubjectsOfKind if !contract.repository_independent => {
                return Err(EnvelopeError::InvalidInput(
                    "repository-bearing subject cannot use all-subjects",
                ))
            }
            SubjectSelector::AllSubjectsOfKind => {}
        }
        Ok(())
    }
    fn subject_contains(&self, scope: &SubjectScope, reference: &ExactBytes) -> bool {
        let Some(contract) = self.subjects.get(&scope.kind) else {
            return false;
        };
        match &scope.selector {
            SubjectSelector::Exact(refs) => refs.contains(reference),
            SubjectSelector::AllSubjectsOfKind => contract.repository_independent,
            SubjectSelector::Repositories(repos) => contract
                .membership
                .iter()
                .any(|(r, repo)| r == reference && repos.contains(repo)),
        }
    }
    fn covers_subject(&self, parent: &SubjectScope, child: &SubjectScope) -> bool {
        if parent.kind != child.kind {
            return false;
        }
        match (&parent.selector, &child.selector) {
            (_, SubjectSelector::Exact(refs)) => {
                refs.iter().all(|r| self.subject_contains(parent, r))
            }
            (SubjectSelector::Repositories(a), SubjectSelector::Repositories(b)) => {
                b.iter().all(|r| a.contains(r))
            }
            (SubjectSelector::AllSubjectsOfKind, SubjectSelector::AllSubjectsOfKind) => self
                .subjects
                .get(&parent.kind)
                .is_some_and(|s| s.repository_independent),
            _ => false,
        }
    }
}
fn resolve_role<'a>(run: &'a RunContract, role: &str) -> EnvelopeResult<&'a Principal> {
    run.bindings
        .iter()
        .find(|(name, _)| name == role)
        .map(|(_, p)| p)
        .ok_or(EnvelopeError::InvalidInput(
            "explicit reporting role is unbound",
        ))
}
fn validate_reference(reference: &ExactBytes, format: ReferenceFormat) -> EnvelopeResult<()> {
    reference.validate()?;
    if format == ReferenceFormat::CanonicalUuid {
        RunRef::parse(
            std::str::from_utf8(&reference.0)
                .map_err(|_| EnvelopeError::InvalidInput("non-UTF8 UUID reference"))?,
        )?;
    }
    Ok(())
}
fn validate_arguments(arguments: &ExactArguments, schema: &[ArgumentSpec]) -> EnvelopeResult<()> {
    collection(&arguments.0, false)?;
    if arguments.0.len() != schema.len() {
        return Err(EnvelopeError::InvalidInput("incomplete argument tuple"));
    }
    for (arg, expected) in arguments.0.iter().zip(schema) {
        if arg.name != expected.name || arg.value.kind() != expected.kind {
            return Err(EnvelopeError::InvalidInput(
                "argument name or type mismatch",
            ));
        }
        if let ArgumentValue::String(text) | ArgumentValue::Word(text) = &arg.value {
            bound("argument text", text.len(), MAX_RECORD_BYTES)?;
        }
    }
    Ok(())
}
fn binding_covers(parent: &SubmissionBinding, child: &SubmissionBinding) -> bool {
    match (parent, child) {
        (SubmissionBinding::Instrument(a), SubmissionBinding::Instrument(b)) => source_covers(a, b),
        (
            SubmissionBinding::PlatformRelay {
                source: a,
                response_contract: ar,
            },
            SubmissionBinding::PlatformRelay {
                source: b,
                response_contract: br,
            },
        ) => ar == br && source_covers(a, b),
        _ => parent == child,
    }
}

fn source_covers(a: &SourceBinding, b: &SourceBinding) -> bool {
    a.contract == b.contract
        && b.identities.iter().all(|i| a.identities.contains(i))
        && b.arguments.iter().all(|args| a.arguments.contains(args))
}