vela-protocol 0.940.1

Core library for the Vela scientific knowledge protocol: replayable frontier state, signed canonical events, and proof packets.
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
//! Pure protocol values for Frontier repository identity and boundary events.
//!
//! This module deliberately does not inspect Git, resolve dependency locators,
//! enumerate files, or decide actor authority. It validates the closed wire
//! values, recomputes the roots available from those values, verifies ordinary
//! event shape/signatures, and checks continuity against a supplied previous
//! boundary. A caller must still verify the anchored Git objects, retained
//! bytes, and actor-registry state before treating a boundary as valid.

use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use unicode_normalization::UnicodeNormalization;

use crate::actor_registration::{require_lower_hex, require_sha256_root};
use crate::events::{
    EVENT_KIND_FRONTIER_REPOSITORY_BOUND, EVENT_SCHEMA, FRONTIER_CREATED_SCHEMA_V1, NULL_HASH,
    StateActor, StateEvent, StateTarget, compute_event_id, event_content_preimage_bytes,
};

pub const FRONTIER_IDENTITY_SCHEMA: &str = "vela.frontier-identity.v1";
pub const LEGACY_FRONTIER_ORIGIN_SCHEMA: &str = "vela.legacy-frontier-origin.v1";
pub const FRONTIER_REPOSITORY_BOUNDARY_SCHEMA: &str = "vela.frontier-repository-boundary.v1";
pub const RETAINED_OBJECT_MANIFEST_SCHEMA: &str = "vela.retained-object-manifest.v1";

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum FrontierIdentityOrigin {
    Genesis,
    LegacyBoundary,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum GitObjectFormat {
    Sha1,
    Sha256,
}

impl GitObjectFormat {
    fn digest_len(self) -> usize {
        match self {
            Self::Sha1 => 40,
            Self::Sha256 => 64,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct FrontierIdentityV1 {
    pub schema: String,
    pub frontier_id: String,
    pub origin: FrontierIdentityOrigin,
    pub origin_commitment: String,
    pub legacy_identity_preimage_root: Option<String>,
}

impl FrontierIdentityV1 {
    /// Derive, rather than accept, the identity of a new Frontier from its
    /// exact `frontier.created` event preimage.
    pub fn from_genesis_event(event: &StateEvent) -> Result<Self, String> {
        validate_profile_v1_genesis_event(event)?;
        let digest = hex::encode(Sha256::digest(event_content_preimage_bytes(event)));
        Ok(Self {
            schema: FRONTIER_IDENTITY_SCHEMA.to_string(),
            frontier_id: format!("vfr_{}", &digest[..16]),
            origin: FrontierIdentityOrigin::Genesis,
            origin_commitment: format!("sha256:{digest}"),
            legacy_identity_preimage_root: None,
        })
    }

    /// Validate the closed record shape only.
    ///
    /// For `origin: genesis`, this does not prove that `origin_commitment`
    /// came from a real `frontier.created` event. Callers establishing
    /// repository identity must derive the record with
    /// [`Self::from_genesis_event`] or validate a boundary chain.
    pub fn validate(&self) -> Result<(), String> {
        if self.schema != FRONTIER_IDENTITY_SCHEMA {
            return Err(format!(
                "identity.schema must be {FRONTIER_IDENTITY_SCHEMA}"
            ));
        }
        require_frontier_id("identity.frontier_id", &self.frontier_id)?;
        require_sha256_root("identity.origin_commitment", &self.origin_commitment)?;
        match (self.origin, self.legacy_identity_preimage_root.as_deref()) {
            (FrontierIdentityOrigin::Genesis, None) => Ok(()),
            (FrontierIdentityOrigin::Genesis, Some(_)) => {
                Err("genesis identity must use a null legacy_identity_preimage_root".to_string())
            }
            (FrontierIdentityOrigin::LegacyBoundary, Some(root)) => {
                require_sha256_root("identity.legacy_identity_preimage_root", root)
            }
            (FrontierIdentityOrigin::LegacyBoundary, None) => {
                Err("legacy_boundary identity requires legacy_identity_preimage_root".to_string())
            }
        }
    }

    pub fn root(&self) -> Result<String, String> {
        self.validate()?;
        canonical_root(self)
    }

    pub fn verify_root(&self, expected: &str) -> Result<(), String> {
        require_sha256_root("identity_root", expected)?;
        if self.root()? != expected {
            return Err("identity_root does not match the closed identity preimage".to_string());
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct LegacyFrontierOriginV1 {
    pub schema: String,
    pub frontier_id: String,
    pub legacy_identity_preimage_root: String,
    pub git_object_format: GitObjectFormat,
    pub anchor_git_commit: String,
    pub anchor_git_tree: String,
    pub anchor_event_log_root: String,
    pub anchor_event_count: u64,
}

impl LegacyFrontierOriginV1 {
    pub fn validate(&self) -> Result<(), String> {
        if self.schema != LEGACY_FRONTIER_ORIGIN_SCHEMA {
            return Err(format!(
                "legacy origin schema must be {LEGACY_FRONTIER_ORIGIN_SCHEMA}"
            ));
        }
        require_frontier_id("legacy_origin.frontier_id", &self.frontier_id)?;
        require_sha256_root(
            "legacy_origin.legacy_identity_preimage_root",
            &self.legacy_identity_preimage_root,
        )?;
        require_git_object(
            "legacy_origin.anchor_git_commit",
            &self.anchor_git_commit,
            self.git_object_format,
        )?;
        require_git_object(
            "legacy_origin.anchor_git_tree",
            &self.anchor_git_tree,
            self.git_object_format,
        )?;
        require_sha256_root(
            "legacy_origin.anchor_event_log_root",
            &self.anchor_event_log_root,
        )?;
        if self.anchor_event_count == 0 {
            return Err("legacy origin requires a non-empty anchored event log".to_string());
        }
        Ok(())
    }

    pub fn origin_commitment(&self) -> Result<String, String> {
        self.validate()?;
        canonical_root(self)
    }

    pub fn identity(&self) -> Result<FrontierIdentityV1, String> {
        Ok(FrontierIdentityV1 {
            schema: FRONTIER_IDENTITY_SCHEMA.to_string(),
            frontier_id: self.frontier_id.clone(),
            origin: FrontierIdentityOrigin::LegacyBoundary,
            origin_commitment: self.origin_commitment()?,
            legacy_identity_preimage_root: Some(self.legacy_identity_preimage_root.clone()),
        })
    }

    pub fn identity_root(&self) -> Result<String, String> {
        self.identity()?.root()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ExactFrontierDependencyV1 {
    pub frontier_id: String,
    pub identity_root: String,
    pub scientific_state_root: String,
    pub git_object_format: GitObjectFormat,
    pub git_commit: String,
    pub git_tree: String,
}

impl ExactFrontierDependencyV1 {
    pub fn validate(&self) -> Result<(), String> {
        require_frontier_id("dependency.frontier_id", &self.frontier_id)?;
        require_sha256_root("dependency.identity_root", &self.identity_root)?;
        require_sha256_root(
            "dependency.scientific_state_root",
            &self.scientific_state_root,
        )?;
        require_git_object(
            "dependency.git_commit",
            &self.git_commit,
            self.git_object_format,
        )?;
        require_git_object(
            "dependency.git_tree",
            &self.git_tree,
            self.git_object_format,
        )
    }

    fn canonical_key(&self) -> (&str, &str) {
        (&self.frontier_id, &self.identity_root)
    }
}

pub fn validate_exact_dependencies(
    dependencies: &[ExactFrontierDependencyV1],
) -> Result<(), String> {
    let mut previous: Option<(&str, &str)> = None;
    for dependency in dependencies {
        dependency.validate()?;
        let key = dependency.canonical_key();
        if let Some(prior) = previous {
            if key == prior {
                return Err(format!(
                    "duplicate dependency key ({}, {})",
                    dependency.frontier_id, dependency.identity_root
                ));
            }
            if key < prior {
                return Err(
                    "dependencies must be sorted by (frontier_id, identity_root)".to_string(),
                );
            }
        }
        previous = Some(key);
    }
    Ok(())
}

pub fn exact_dependency_root(dependencies: &[ExactFrontierDependencyV1]) -> Result<String, String> {
    validate_exact_dependencies(dependencies)?;
    canonical_root(dependencies)
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct RetainedObjectEntryV1 {
    pub path: String,
    pub git_mode: String,
    pub size: u64,
    /// Lowercase, bare SHA-256 digest of the exact retained bytes.
    pub sha256: String,
}

impl RetainedObjectEntryV1 {
    pub fn validate(&self) -> Result<(), String> {
        validate_repository_path(&self.path)?;
        if !matches!(self.git_mode.as_str(), "100644" | "100755") {
            return Err(
                "retained object git_mode must be tracked regular-file mode 100644 or 100755"
                    .to_string(),
            );
        }
        require_lower_hex("retained object sha256", &self.sha256, 64)
    }
}

/// `vela.retained-object-manifest.v1` is represented on the wire as the exact
/// canonical JSON list, rather than an object wrapper. The schema constant is
/// the protocol label for that list and for conformance documentation.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(transparent)]
pub struct RetainedObjectManifestV1(pub Vec<RetainedObjectEntryV1>);

impl RetainedObjectManifestV1 {
    pub fn validate(&self) -> Result<(), String> {
        let mut previous_path: Option<&str> = None;
        let mut portable_collision_keys = BTreeSet::new();
        for entry in &self.0 {
            entry.validate()?;
            if let Some(previous) = previous_path {
                if entry.path == previous {
                    return Err(format!("duplicate retained object path {:?}", entry.path));
                }
                if entry.path.as_str() < previous {
                    return Err("retained object entries must be sorted by path".to_string());
                }
            }
            previous_path = Some(&entry.path);

            // This is a deliberately conservative portable key, not an
            // emulation of any particular operating system: NFC, then Unicode
            // lowercase. It prevents a manifest from relying on case-sensitive
            // checkout behavior that common consumer platforms do not share.
            let collision_key = entry
                .path
                .nfc()
                .flat_map(char::to_lowercase)
                .collect::<String>();
            if !portable_collision_keys.insert(collision_key) {
                return Err(format!(
                    "retained object path {:?} has a portable case-fold collision",
                    entry.path
                ));
            }
        }
        Ok(())
    }

    pub fn root(&self) -> Result<String, String> {
        self.validate()?;
        canonical_root(&self.0)
    }

    pub fn verify_root(&self, expected: &str) -> Result<(), String> {
        require_sha256_root("retained_object_manifest_root", expected)?;
        if self.root()? != expected {
            return Err(
                "retained-object manifest root does not match its canonical entries".to_string(),
            );
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum FrontierRepositoryBoundaryMode {
    TemporalizeExisting,
    UpdateDependencies,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum FrontierRepositoryTrustMode {
    Tofu,
    Genesis,
    PreviousBoundary,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct FrontierRepositoryBoundaryPayloadV1 {
    pub schema: String,
    pub mode: FrontierRepositoryBoundaryMode,
    pub frontier_id: String,
    pub identity_root: String,
    pub observed_profile_root: String,
    pub dependency_root: String,
    pub dependencies: Vec<ExactFrontierDependencyV1>,
    /// Full content root of either the `frontier.created` genesis event (for
    /// the first dependency update of a new Frontier) or the preceding valid
    /// repository-boundary event.
    pub previous_identity_event_root: Option<String>,
    pub legacy_identity_preimage_root: Option<String>,
    pub administrator_actor_id: String,
    pub administrator_public_key: String,
    pub administrator_algorithm: String,
    pub trust_mode: FrontierRepositoryTrustMode,
    pub git_object_format: GitObjectFormat,
    pub anchor_git_commit: String,
    pub anchor_git_tree: String,
    pub anchor_event_log_root: String,
    pub anchor_event_count: u64,
    pub anchor_snapshot_root: String,
    pub anchor_snapshot_schema: String,
    pub anchor_proposal_root: String,
    pub anchor_actor_registry_root: String,
    pub anchor_artifact_registry_root: String,
    pub anchor_canonical_store_root: String,
}

impl FrontierRepositoryBoundaryPayloadV1 {
    /// Validate only properties derivable from this closed payload.
    ///
    /// `update_dependencies` identity continuity is necessarily checked by
    /// [`Self::validate_chain`], because the current payload intentionally does
    /// not repeat the original identity preimage.
    pub fn validate(&self) -> Result<(), String> {
        if self.schema != FRONTIER_REPOSITORY_BOUNDARY_SCHEMA {
            return Err(format!(
                "payload.schema must be {FRONTIER_REPOSITORY_BOUNDARY_SCHEMA}"
            ));
        }
        require_frontier_id("payload.frontier_id", &self.frontier_id)?;
        for (field, root) in [
            ("payload.identity_root", self.identity_root.as_str()),
            (
                "payload.observed_profile_root",
                self.observed_profile_root.as_str(),
            ),
            ("payload.dependency_root", self.dependency_root.as_str()),
            (
                "payload.anchor_event_log_root",
                self.anchor_event_log_root.as_str(),
            ),
            (
                "payload.anchor_snapshot_root",
                self.anchor_snapshot_root.as_str(),
            ),
            (
                "payload.anchor_proposal_root",
                self.anchor_proposal_root.as_str(),
            ),
            (
                "payload.anchor_actor_registry_root",
                self.anchor_actor_registry_root.as_str(),
            ),
            (
                "payload.anchor_artifact_registry_root",
                self.anchor_artifact_registry_root.as_str(),
            ),
            (
                "payload.anchor_canonical_store_root",
                self.anchor_canonical_store_root.as_str(),
            ),
        ] {
            require_sha256_root(field, root)?;
        }
        if let Some(root) = self.previous_identity_event_root.as_deref() {
            require_sha256_root("payload.previous_identity_event_root", root)?;
        }
        if let Some(root) = self.legacy_identity_preimage_root.as_deref() {
            require_sha256_root("payload.legacy_identity_preimage_root", root)?;
        }
        require_git_object(
            "payload.anchor_git_commit",
            &self.anchor_git_commit,
            self.git_object_format,
        )?;
        require_git_object(
            "payload.anchor_git_tree",
            &self.anchor_git_tree,
            self.git_object_format,
        )?;
        if self.anchor_event_count == 0 {
            return Err("repository boundary requires a non-empty anchored event log".to_string());
        }
        require_text(
            "payload.anchor_snapshot_schema",
            &self.anchor_snapshot_schema,
        )?;
        if !(self.administrator_actor_id.starts_with("reviewer:")
            || self.administrator_actor_id.starts_with("steward:"))
        {
            return Err(
                "payload.administrator_actor_id must identify a reviewer: or steward: human actor"
                    .to_string(),
            );
        }
        require_lower_hex(
            "payload.administrator_public_key",
            &self.administrator_public_key,
            64,
        )?;
        if self.administrator_algorithm != "ed25519" {
            return Err("payload.administrator_algorithm must be ed25519".to_string());
        }

        let computed_dependency_root = exact_dependency_root(&self.dependencies)?;
        if computed_dependency_root != self.dependency_root {
            return Err(
                "payload.dependency_root does not match the canonical dependency list".to_string(),
            );
        }

        match self.mode {
            FrontierRepositoryBoundaryMode::TemporalizeExisting => {
                if self.trust_mode != FrontierRepositoryTrustMode::Tofu {
                    return Err("temporalize_existing requires trust_mode tofu".to_string());
                }
                if self.previous_identity_event_root.is_some() {
                    return Err(
                        "temporalize_existing requires a null previous_identity_event_root"
                            .to_string(),
                    );
                }
                let legacy_root =
                    self.legacy_identity_preimage_root
                        .as_deref()
                        .ok_or_else(|| {
                            "temporalize_existing requires legacy_identity_preimage_root"
                                .to_string()
                        })?;
                let legacy_origin = LegacyFrontierOriginV1 {
                    schema: LEGACY_FRONTIER_ORIGIN_SCHEMA.to_string(),
                    frontier_id: self.frontier_id.clone(),
                    legacy_identity_preimage_root: legacy_root.to_string(),
                    git_object_format: self.git_object_format,
                    anchor_git_commit: self.anchor_git_commit.clone(),
                    anchor_git_tree: self.anchor_git_tree.clone(),
                    anchor_event_log_root: self.anchor_event_log_root.clone(),
                    anchor_event_count: self.anchor_event_count,
                };
                if legacy_origin.identity_root()? != self.identity_root {
                    return Err(
                        "payload.identity_root does not match the temporalized legacy identity"
                            .to_string(),
                    );
                }
            }
            FrontierRepositoryBoundaryMode::UpdateDependencies => {
                if !matches!(
                    self.trust_mode,
                    FrontierRepositoryTrustMode::Genesis
                        | FrontierRepositoryTrustMode::PreviousBoundary
                ) {
                    return Err(
                        "update_dependencies requires trust_mode genesis or previous_boundary"
                            .to_string(),
                    );
                }
                if self.previous_identity_event_root.is_none() {
                    return Err(
                        "update_dependencies requires previous_identity_event_root".to_string()
                    );
                }
                if self.trust_mode == FrontierRepositoryTrustMode::Genesis
                    && self.legacy_identity_preimage_root.is_some()
                {
                    return Err(
                        "a genesis-chained dependency update cannot carry a legacy identity root"
                            .to_string(),
                    );
                }
            }
        }
        Ok(())
    }

    /// Check identity continuity against either the exact genesis event or the
    /// exact preceding repository-boundary event.
    ///
    /// This does not verify actor-registry authority or anchored Git history.
    pub fn validate_chain(&self, previous_event: &StateEvent) -> Result<(), String> {
        self.validate()?;
        if self.mode != FrontierRepositoryBoundaryMode::UpdateDependencies {
            return Err("only update_dependencies has a previous identity chain".to_string());
        }
        let previous_root = repository_identity_event_content_root(previous_event)?;
        if self.previous_identity_event_root.as_deref() != Some(previous_root.as_str()) {
            return Err(
                "previous_identity_event_root does not match the preceding event preimage"
                    .to_string(),
            );
        }
        if previous_event.kind.as_str() == "frontier.created" {
            if self.trust_mode != FrontierRepositoryTrustMode::Genesis {
                return Err("a frontier.created parent requires trust_mode genesis".to_string());
            }
            let identity = FrontierIdentityV1::from_genesis_event(previous_event)?;
            if self.frontier_id != identity.frontier_id
                || self.identity_root != identity.root()?
                || self.legacy_identity_preimage_root.is_some()
            {
                return Err(
                    "genesis-chained boundary does not preserve the derived Frontier identity"
                        .to_string(),
                );
            }
        } else {
            if self.trust_mode != FrontierRepositoryTrustMode::PreviousBoundary {
                return Err(
                    "a repository-boundary parent requires trust_mode previous_boundary"
                        .to_string(),
                );
            }
            let previous = repository_boundary_payload_from_event_shape(previous_event)?;
            if self.anchor_event_count <= previous.anchor_event_count {
                return Err(
                    "repository boundary anchor_event_count must advance beyond its parent"
                        .to_string(),
                );
            }
            if self.frontier_id != previous.frontier_id
                || self.identity_root != previous.identity_root
                || self.legacy_identity_preimage_root != previous.legacy_identity_preimage_root
                || self.administrator_actor_id != previous.administrator_actor_id
                || self.administrator_public_key != previous.administrator_public_key
                || self.administrator_algorithm != previous.administrator_algorithm
            {
                return Err(
                    "repository boundary chain changed immutable identity or administrator fields"
                        .to_string(),
                );
            }
        }
        Ok(())
    }
}

pub fn new_repository_boundary_event(
    payload: FrontierRepositoryBoundaryPayloadV1,
    reason: &str,
    timestamp: &str,
) -> Result<StateEvent, String> {
    payload.validate()?;
    require_text("event.reason", reason)?;
    chrono::DateTime::parse_from_rfc3339(timestamp)
        .map_err(|error| format!("event.timestamp must be RFC3339: {error}"))?;
    let mut event = StateEvent {
        schema: EVENT_SCHEMA.to_string(),
        id: String::new(),
        kind: EVENT_KIND_FRONTIER_REPOSITORY_BOUND.into(),
        target: StateTarget {
            r#type: "frontier".to_string(),
            id: payload.frontier_id.clone(),
        },
        actor: StateActor {
            r#type: "human".to_string(),
            id: payload.administrator_actor_id.clone(),
        },
        timestamp: timestamp.to_string(),
        reason: reason.to_string(),
        before_hash: NULL_HASH.to_string(),
        after_hash: NULL_HASH.to_string(),
        payload: serde_json::to_value(payload)
            .map_err(|error| format!("serialize repository-boundary payload: {error}"))?,
        caveats: vec![],
        signature: None,
    };
    event.id = compute_event_id(&event);
    Ok(event)
}

/// Parse and validate the event's closed payload and fixed core shape.
///
/// This checks that a signature is present but does **not** verify it and does
/// not establish Git-anchor or actor-registry authority.
pub fn repository_boundary_payload_from_event_shape(
    event: &StateEvent,
) -> Result<FrontierRepositoryBoundaryPayloadV1, String> {
    if event.schema != EVENT_SCHEMA {
        return Err(format!(
            "repository boundary event schema must be {EVENT_SCHEMA}"
        ));
    }
    if event.kind.as_str() != EVENT_KIND_FRONTIER_REPOSITORY_BOUND {
        return Err(format!(
            "expected {EVENT_KIND_FRONTIER_REPOSITORY_BOUND}, got {}",
            event.kind
        ));
    }
    if event.id != compute_event_id(event) {
        return Err(
            "repository boundary event id does not match its canonical content".to_string(),
        );
    }
    if event.target.r#type != "frontier" {
        return Err("repository boundary target.type must be frontier".to_string());
    }
    if event.actor.r#type != "human" {
        return Err("repository boundary actor.type must be human".to_string());
    }
    if event.before_hash != NULL_HASH || event.after_hash != NULL_HASH {
        return Err("repository boundary must use null before_hash and after_hash".to_string());
    }
    require_text("event.reason", &event.reason)?;
    chrono::DateTime::parse_from_rfc3339(&event.timestamp)
        .map_err(|error| format!("event.timestamp must be RFC3339: {error}"))?;
    if event.signature.is_none() {
        return Err("repository boundary must carry an ordinary event signature".to_string());
    }
    let payload: FrontierRepositoryBoundaryPayloadV1 =
        serde_json::from_value(event.payload.clone())
            .map_err(|error| format!("invalid repository-boundary payload: {error}"))?;
    payload.validate()?;
    if event.target.id != payload.frontier_id {
        return Err("repository boundary target.id must equal payload.frontier_id".to_string());
    }
    if event.actor.id != payload.administrator_actor_id {
        return Err(
            "repository boundary actor.id must equal payload.administrator_actor_id".to_string(),
        );
    }
    Ok(payload)
}

/// Verify only cryptographic proof of possession for the expected key.
///
/// The function name is intentionally explicit: success does not establish
/// that the key was the active administrator at the anchored history, that the
/// Git anchor exists, or that retained bytes match the payload. Those checks
/// belong to the future anchor-context verifier.
pub fn verify_repository_boundary_signature_only(
    event: &StateEvent,
    expected_public_key: &str,
) -> Result<FrontierRepositoryBoundaryPayloadV1, String> {
    require_lower_hex("expected administrator public key", expected_public_key, 64)?;
    let payload = repository_boundary_payload_from_event_shape(event)?;
    if payload.administrator_public_key != expected_public_key {
        return Err(
            "repository boundary payload key does not match the expected administrator key"
                .to_string(),
        );
    }
    if !crate::sign::verify_event_signature(event, expected_public_key)? {
        return Err("repository boundary event signature does not verify".to_string());
    }
    Ok(payload)
}

pub fn repository_boundary_event_content_root(event: &StateEvent) -> Result<String, String> {
    repository_boundary_payload_from_event_shape(event)?;
    Ok(event_content_root(event))
}

/// Full content root of either repository identity event kind.
pub fn repository_identity_event_content_root(event: &StateEvent) -> Result<String, String> {
    match event.kind.as_str() {
        "frontier.created" => {
            validate_profile_v1_genesis_event(event)?;
            Ok(event_content_root(event))
        }
        EVENT_KIND_FRONTIER_REPOSITORY_BOUND => repository_boundary_event_content_root(event),
        other => Err(format!(
            "repository identity parent must be frontier.created or {EVENT_KIND_FRONTIER_REPOSITORY_BOUND}, got {other}"
        )),
    }
}

/// Validate the complete repository-boundary graph present in one event set.
///
/// This is intentionally independent of event timestamps. It proves event
/// shape, proof of key possession, one linear identity chain, and
/// payload-local continuity. Git-anchor membership and actor-registry
/// authority require repository context and are checked by the higher layer.
pub fn validate_repository_boundary_event_set(events: &[StateEvent]) -> Vec<String> {
    struct Boundary<'a> {
        event: &'a StateEvent,
        payload: FrontierRepositoryBoundaryPayloadV1,
    }

    let mut boundaries = BTreeMap::<String, Boundary<'_>>::new();
    let mut errors = Vec::new();

    for event in events
        .iter()
        .filter(|event| event.kind.as_str() == EVENT_KIND_FRONTIER_REPOSITORY_BOUND)
    {
        let payload = match repository_boundary_payload_from_event_shape(event) {
            Ok(payload) => payload,
            Err(error) => {
                errors.push(format!("repository boundary {} invalid: {error}", event.id));
                continue;
            }
        };
        if let Err(error) =
            verify_repository_boundary_signature_only(event, &payload.administrator_public_key)
        {
            errors.push(format!(
                "repository boundary {} signature invalid: {error}",
                event.id
            ));
            continue;
        }
        let root = event_content_root(event);
        if boundaries
            .insert(root.clone(), Boundary { event, payload })
            .is_some()
        {
            errors.push(format!("duplicate repository boundary content root {root}"));
        }
    }

    if boundaries.is_empty() {
        return errors;
    }

    let mut genesis = BTreeMap::new();
    for event in events.iter().filter(|event| {
        event.kind.as_str() == "frontier.created"
            && event
                .payload
                .get("schema")
                .and_then(serde_json::Value::as_str)
                == Some(FRONTIER_CREATED_SCHEMA_V1)
    }) {
        match repository_identity_event_content_root(event) {
            Ok(root) => {
                if genesis.insert(root.clone(), event).is_some() {
                    errors.push(format!("duplicate frontier.created content root {root}"));
                }
            }
            Err(error) => errors.push(format!("frontier.created {} invalid: {error}", event.id)),
        }
    }
    if genesis.len() > 1 {
        errors.push(format!(
            "repository boundary graph cannot contain {} distinct frontier.created roots",
            genesis.len()
        ));
    }

    let mut roots = 0usize;
    let mut children = BTreeMap::<String, Vec<String>>::new();
    for (root, boundary) in &boundaries {
        match boundary.payload.mode {
            FrontierRepositoryBoundaryMode::TemporalizeExisting => {
                roots += 1;
                if !genesis.is_empty() {
                    errors.push(format!(
                        "legacy temporal boundary {root} cannot coexist with frontier.created"
                    ));
                }
            }
            FrontierRepositoryBoundaryMode::UpdateDependencies => {
                let Some(parent_root) = boundary.payload.previous_identity_event_root.as_deref()
                else {
                    // Intrinsic validation already reports this, but retain a
                    // defensive branch for a future parser change.
                    errors.push(format!("repository boundary {root} has no identity parent"));
                    continue;
                };
                children
                    .entry(parent_root.to_string())
                    .or_default()
                    .push(root.clone());

                let parent = boundaries
                    .get(parent_root)
                    .map(|parent| parent.event)
                    .or_else(|| genesis.get(parent_root).copied());
                let Some(parent) = parent else {
                    errors.push(format!(
                        "repository boundary {root} references missing identity parent {parent_root}"
                    ));
                    continue;
                };
                if parent.kind.as_str() == "frontier.created" {
                    roots += 1;
                }
                if let Err(error) = boundary.payload.validate_chain(parent) {
                    errors.push(format!("repository boundary {root} chain invalid: {error}"));
                }
            }
        }
    }

    for (parent, child_roots) in &children {
        if child_roots.len() > 1 {
            errors.push(format!(
                "repository identity event {parent} has conflicting children {}",
                child_roots.join(", ")
            ));
        }
    }
    if roots != 1 {
        errors.push(format!(
            "repository boundary graph must have exactly one identity-chain root, found {roots}"
        ));
    }

    for start in boundaries.keys() {
        let mut current = start.as_str();
        let mut visited = BTreeSet::new();
        loop {
            if !visited.insert(current.to_string()) {
                errors.push(format!(
                    "repository boundary graph contains a cycle through {current}"
                ));
                break;
            }
            let Some(boundary) = boundaries.get(current) else {
                break;
            };
            if boundary.payload.mode == FrontierRepositoryBoundaryMode::TemporalizeExisting {
                break;
            }
            let Some(parent) = boundary.payload.previous_identity_event_root.as_deref() else {
                break;
            };
            if genesis.contains_key(parent) {
                break;
            }
            current = parent;
        }
    }

    errors.sort();
    errors.dedup();
    errors
}

fn validate_genesis_event(event: &StateEvent) -> Result<(), String> {
    if event.schema != EVENT_SCHEMA {
        return Err(format!("genesis event schema must be {EVENT_SCHEMA}"));
    }
    if event.kind.as_str() != "frontier.created" {
        return Err(format!("expected frontier.created, got {}", event.kind));
    }
    if event.id != compute_event_id(event) {
        return Err("frontier.created id does not match its canonical content".to_string());
    }
    if event.target.r#type != "frontier" || event.actor.r#type != "frontier" {
        return Err("frontier.created target and actor types must be frontier".to_string());
    }
    if event.before_hash != NULL_HASH || event.after_hash != NULL_HASH {
        return Err("frontier.created must use null before_hash and after_hash".to_string());
    }
    crate::events::validate_event_payload("frontier.created", &event.payload)?;
    let payload = event
        .payload
        .as_object()
        .ok_or_else(|| "frontier.created payload must be an object".to_string())?;
    if payload.get("schema").and_then(serde_json::Value::as_str) == Some(FRONTIER_CREATED_SCHEMA_V1)
    {
        if event.signature.is_some() || !event.caveats.is_empty() {
            return Err(
                "Profile v1 frontier.created must be unsigned and carry no caveats".to_string(),
            );
        }
        if payload
            .get("name_at_creation")
            .and_then(serde_json::Value::as_str)
            != Some(event.target.id.as_str())
            || payload.get("creator").and_then(serde_json::Value::as_str)
                != Some(event.actor.id.as_str())
            || payload
                .get("created_at")
                .and_then(serde_json::Value::as_str)
                != Some(event.timestamp.as_str())
        {
            return Err(
                "Profile v1 frontier.created payload identity disagrees with its event core"
                    .to_string(),
            );
        }
        let empty_dependency_root = exact_dependency_root(&[])?;
        if payload
            .get("dependency_root")
            .and_then(serde_json::Value::as_str)
            != Some(empty_dependency_root.as_str())
        {
            return Err(
                "Profile v1 frontier.created must bind the canonical empty dependency root"
                    .to_string(),
            );
        }
    } else {
        if payload.get("name").and_then(serde_json::Value::as_str) != Some(event.target.id.as_str())
            || payload.get("creator").and_then(serde_json::Value::as_str)
                != Some(event.actor.id.as_str())
        {
            return Err(
                "historical frontier.created payload identity disagrees with its event core"
                    .to_string(),
            );
        }
        if let Some(compiled_at) = payload
            .get("compiled_at")
            .and_then(serde_json::Value::as_str)
            && compiled_at != event.timestamp
        {
            return Err(
                "historical frontier.created compiled_at disagrees with event timestamp"
                    .to_string(),
            );
        }
    }
    Ok(())
}

/// A Profile v1 repository identity may only originate in the closed v1
/// structural genesis. Historical `frontier.created` events remain valid
/// replay input, but they cannot be relabeled as the stronger v1 identity
/// source or used to bypass a protected legacy-boundary migration.
fn validate_profile_v1_genesis_event(event: &StateEvent) -> Result<(), String> {
    validate_genesis_event(event)?;
    if event
        .payload
        .get("schema")
        .and_then(serde_json::Value::as_str)
        != Some(FRONTIER_CREATED_SCHEMA_V1)
    {
        return Err(format!(
            "repository identity genesis must use payload.schema {FRONTIER_CREATED_SCHEMA_V1}"
        ));
    }
    Ok(())
}

fn event_content_root(event: &StateEvent) -> String {
    format!(
        "sha256:{}",
        hex::encode(Sha256::digest(event_content_preimage_bytes(event)))
    )
}

fn canonical_root<T: Serialize + ?Sized>(value: &T) -> Result<String, String> {
    Ok(format!(
        "sha256:{}",
        crate::canonical::sha256_canonical(value)?
    ))
}

fn require_frontier_id(field: &str, value: &str) -> Result<(), String> {
    let Some(hex) = value.strip_prefix("vfr_") else {
        return Err(format!("{field} must use the vfr_<16 lowercase hex> form"));
    };
    require_lower_hex(field, hex, 16)
}

fn require_git_object(
    field: &str,
    value: &str,
    object_format: GitObjectFormat,
) -> Result<(), String> {
    require_lower_hex(field, value, object_format.digest_len())
}

fn require_text(field: &str, value: &str) -> Result<(), String> {
    if value.trim().is_empty() {
        return Err(format!("{field} must be non-empty"));
    }
    if value.nfc().collect::<String>() != value {
        return Err(format!("{field} must already be Unicode NFC"));
    }
    if value.chars().any(char::is_control) {
        return Err(format!("{field} contains a forbidden control character"));
    }
    Ok(())
}

fn validate_repository_path(path: &str) -> Result<(), String> {
    require_text("retained object path", path)?;
    if path.starts_with('/') || path.ends_with('/') || path.contains('\\') {
        return Err(
            "retained object path must be a normalized relative repository path".to_string(),
        );
    }
    if path
        .split('/')
        .any(|component| component.is_empty() || matches!(component, "." | ".."))
    {
        return Err(
            "retained object path contains an empty, dot, or traversal segment".to_string(),
        );
    }
    Ok(())
}

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

    fn root(character: char) -> String {
        format!("sha256:{}", character.to_string().repeat(64))
    }

    #[test]
    fn frontier_repository_bound_legacy_identity_root_is_deterministic() {
        let origin = LegacyFrontierOriginV1 {
            schema: LEGACY_FRONTIER_ORIGIN_SCHEMA.to_string(),
            frontier_id: "vfr_1234567890abcdef".to_string(),
            legacy_identity_preimage_root: root('1'),
            git_object_format: GitObjectFormat::Sha1,
            anchor_git_commit: "2".repeat(40),
            anchor_git_tree: "3".repeat(40),
            anchor_event_log_root: root('4'),
            anchor_event_count: 1,
        };
        let identity = origin.identity().unwrap();
        let expected = canonical_root(&identity).unwrap();
        assert_eq!(origin.identity_root().unwrap(), expected);
        identity.verify_root(&expected).unwrap();

        let mut changed = origin;
        changed.anchor_event_count = 2;
        assert_ne!(changed.identity_root().unwrap(), expected);
    }

    #[test]
    fn frontier_identity_record_shape_rejects_a_genesis_legacy_root() {
        let valid = FrontierIdentityV1 {
            schema: FRONTIER_IDENTITY_SCHEMA.to_string(),
            frontier_id: "vfr_1234567890abcdef".to_string(),
            origin: FrontierIdentityOrigin::Genesis,
            origin_commitment: root('1'),
            legacy_identity_preimage_root: None,
        };
        assert!(valid.root().is_ok());

        let mut invalid = valid;
        invalid.legacy_identity_preimage_root = Some(root('2'));
        assert!(invalid.validate().is_err());
    }

    #[test]
    fn frontier_repository_bound_manifest_wire_value_is_the_exact_list() {
        let manifest = RetainedObjectManifestV1(vec![RetainedObjectEntryV1 {
            path: "records/receipt.json".to_string(),
            git_mode: "100644".to_string(),
            size: 7,
            sha256: "a".repeat(64),
        }]);
        let value = serde_json::to_value(&manifest).unwrap();
        assert!(value.is_array());
        assert_eq!(
            manifest.root().unwrap(),
            canonical_root(&manifest.0).unwrap()
        );
    }
}