vela-protocol 0.940.5

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
//! Exact predecessor boundary for a current-only Frontier repository.

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

pub const REPOSITORY_EPOCH_V1_SCHEMA: &str = "vela.repository-epoch.v1";
pub const REPOSITORY_GENESIS_V1_SCHEMA: &str = "vela.repository-genesis.v1";

/// Native origin for a repository created directly in the current object
/// model. Unlike [`RepositoryEpochV1`], this object has no predecessor,
/// archive, equivalence report, or compatibility roots.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RepositoryGenesisV1 {
    pub schema: String,
    pub epoch_id: String,
    pub frontier_id: String,
    pub epoch: u64,
    pub profile_root: String,
    pub initial_object_set_root: String,
    pub initial_event_log_root: String,
    pub initial_actor_registry_root: String,
    pub reason: String,
}

impl RepositoryGenesisV1 {
    pub fn build(
        frontier_id: String,
        profile_root: String,
        initial_object_set_root: String,
        reason: String,
    ) -> Result<Self, String> {
        let mut value = Self {
            schema: REPOSITORY_GENESIS_V1_SCHEMA.to_string(),
            epoch_id: String::new(),
            frontier_id,
            epoch: 1,
            profile_root,
            initial_object_set_root,
            initial_event_log_root: format!("sha256:{}", crate::events::event_log_hash(&[])),
            initial_actor_registry_root: format!("sha256:{}", hex::encode(Sha256::digest([]))),
            reason,
        };
        value.validate_semantics()?;
        value.epoch_id = value.derive_id()?;
        value.verify()?;
        Ok(value)
    }

    pub fn parse(bytes: &[u8]) -> Result<Self, String> {
        if bytes.len() > 64 * 1024 {
            return Err("Repository genesis exceeds the 64 KiB encoded limit".into());
        }
        let value: Self = serde_json::from_slice(bytes)
            .map_err(|error| format!("parse repository genesis v1: {error}"))?;
        value.verify()?;
        if value.canonical_bytes()? != bytes {
            return Err("repository genesis bytes are not canonical JSON".into());
        }
        Ok(value)
    }

    pub fn verify(&self) -> Result<(), String> {
        self.validate_semantics()?;
        let expected = self.derive_id()?;
        if self.epoch_id != expected {
            return Err(format!(
                "repository genesis id mismatch: declared {}, rebuilt {expected}",
                self.epoch_id
            ));
        }
        Ok(())
    }

    pub fn canonical_bytes(&self) -> Result<Vec<u8>, String> {
        self.verify()?;
        crate::canonical::to_canonical_bytes(self)
    }

    pub fn canonical_root(&self) -> Result<String, String> {
        Ok(format!(
            "sha256:{}",
            hex::encode(Sha256::digest(self.canonical_bytes()?))
        ))
    }

    fn derive_id(&self) -> Result<String, String> {
        let mut body = self.clone();
        body.epoch_id.clear();
        let bytes = crate::canonical::to_canonical_bytes(&body)?;
        Ok(format!("vre_{}", &hex::encode(Sha256::digest(bytes))[..16]))
    }

    fn validate_semantics(&self) -> Result<(), String> {
        if self.schema != REPOSITORY_GENESIS_V1_SCHEMA {
            return Err(format!(
                "repository genesis schema must be `{REPOSITORY_GENESIS_V1_SCHEMA}`"
            ));
        }
        require_prefixed("frontier_id", &self.frontier_id, "vfr_")?;
        if self.epoch != 1 {
            return Err("repository genesis epoch must be exactly 1".into());
        }
        require_sha256("profile_root", &self.profile_root)?;
        require_sha256("initial_object_set_root", &self.initial_object_set_root)?;
        require_sha256("initial_event_log_root", &self.initial_event_log_root)?;
        require_sha256(
            "initial_actor_registry_root",
            &self.initial_actor_registry_root,
        )?;
        let expected_event_root = format!("sha256:{}", crate::events::event_log_hash(&[]));
        let expected_actor_root = format!("sha256:{}", hex::encode(Sha256::digest([])));
        if self.initial_event_log_root != expected_event_root
            || self.initial_actor_registry_root != expected_actor_root
        {
            return Err(
                "repository genesis must bind the exact empty event and actor-registry roots"
                    .into(),
            );
        }
        require_text("reason", &self.reason)?;
        Ok(())
    }
}

/// Schema-dispatched current repository origin. Existing upgraded
/// repositories retain their exact predecessor epoch bytes; newly created
/// repositories use the smaller native genesis object.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RepositoryBoundaryV1 {
    Predecessor(RepositoryEpochV1),
    Genesis(RepositoryGenesisV1),
}

impl RepositoryBoundaryV1 {
    pub fn parse(bytes: &[u8]) -> Result<Self, String> {
        let schema = serde_json::from_slice::<serde_json::Value>(bytes)
            .map_err(|error| format!("parse repository boundary JSON: {error}"))?
            .get("schema")
            .and_then(serde_json::Value::as_str)
            .ok_or_else(|| "repository boundary has no schema".to_string())?
            .to_string();
        match schema.as_str() {
            REPOSITORY_EPOCH_V1_SCHEMA => {
                let epoch = RepositoryEpochV1::parse(bytes)?;
                if epoch.canonical_bytes()? != bytes {
                    return Err("repository epoch bytes are not canonical JSON".into());
                }
                Ok(Self::Predecessor(epoch))
            }
            REPOSITORY_GENESIS_V1_SCHEMA => Ok(Self::Genesis(RepositoryGenesisV1::parse(bytes)?)),
            _ => Err(format!("unsupported repository boundary schema `{schema}`")),
        }
    }

    pub fn frontier_id(&self) -> &str {
        match self {
            Self::Predecessor(value) => &value.frontier_id,
            Self::Genesis(value) => &value.frontier_id,
        }
    }

    pub fn epoch_id(&self) -> &str {
        match self {
            Self::Predecessor(value) => &value.epoch_id,
            Self::Genesis(value) => &value.epoch_id,
        }
    }

    pub fn reason(&self) -> &str {
        match self {
            Self::Predecessor(value) => &value.reason,
            Self::Genesis(value) => &value.reason,
        }
    }

    pub fn canonical_root(&self) -> Result<String, String> {
        match self {
            Self::Predecessor(value) => value.canonical_root(),
            Self::Genesis(value) => value.canonical_root(),
        }
    }

    pub fn canonical_bytes(&self) -> Result<Vec<u8>, String> {
        match self {
            Self::Predecessor(value) => value.canonical_bytes(),
            Self::Genesis(value) => value.canonical_bytes(),
        }
    }

    pub fn predecessor_roots(&self) -> Option<&PredecessorRoots> {
        match self {
            Self::Predecessor(value) => Some(&value.predecessor_roots),
            Self::Genesis(_) => None,
        }
    }

    pub fn genesis(&self) -> Option<&RepositoryGenesisV1> {
        match self {
            Self::Genesis(value) => Some(value),
            Self::Predecessor(_) => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PredecessorRoots {
    pub event_log: String,
    pub scientific_state: String,
    pub compatibility_snapshot: String,
    pub proposal_state: String,
    pub actor_registry: String,
    pub artifact_registry: String,
    pub authority_head: String,
    pub authority_event_log: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RepositoryEpochV1 {
    pub schema: String,
    pub epoch_id: String,
    pub frontier_id: String,
    pub epoch: u64,
    pub predecessor_remote: String,
    pub predecessor_tag: String,
    pub predecessor_commit: String,
    pub predecessor_tree: String,
    pub predecessor_profile_schema: String,
    pub predecessor_roots: PredecessorRoots,
    pub predecessor_git_object_manifest_root: String,
    pub archive_bundle_sha256: String,
    pub imported_claim_set_root: String,
    pub retained_current_object_set_root: String,
    pub archived_object_index_root: String,
    pub equivalence_report_root: String,
    pub reason: String,
}

impl RepositoryEpochV1 {
    #[allow(clippy::too_many_arguments)]
    pub fn build(
        frontier_id: String,
        epoch: u64,
        predecessor_remote: String,
        predecessor_tag: String,
        predecessor_commit: String,
        predecessor_tree: String,
        predecessor_profile_schema: String,
        predecessor_roots: PredecessorRoots,
        predecessor_git_object_manifest_root: String,
        archive_bundle_sha256: String,
        imported_claim_set_root: String,
        retained_current_object_set_root: String,
        archived_object_index_root: String,
        equivalence_report_root: String,
        reason: String,
    ) -> Result<Self, String> {
        let mut value = Self {
            schema: REPOSITORY_EPOCH_V1_SCHEMA.to_string(),
            epoch_id: String::new(),
            frontier_id,
            epoch,
            predecessor_remote,
            predecessor_tag,
            predecessor_commit,
            predecessor_tree,
            predecessor_profile_schema,
            predecessor_roots,
            predecessor_git_object_manifest_root,
            archive_bundle_sha256,
            imported_claim_set_root,
            retained_current_object_set_root,
            archived_object_index_root,
            equivalence_report_root,
            reason,
        };
        value.validate_semantics()?;
        value.epoch_id = value.derive_id()?;
        value.verify()?;
        Ok(value)
    }

    pub fn parse(bytes: &[u8]) -> Result<Self, String> {
        if bytes.len() > 2 * 1024 * 1024 {
            return Err("Repository epoch exceeds the 2 MiB encoded limit".into());
        }
        let value: Self = serde_json::from_slice(bytes)
            .map_err(|error| format!("parse repository epoch v1: {error}"))?;
        value.verify()?;
        Ok(value)
    }

    pub fn verify(&self) -> Result<(), String> {
        self.validate_semantics()?;
        let expected = self.derive_id()?;
        if self.epoch_id != expected {
            return Err(format!(
                "repository epoch id mismatch: declared {}, rebuilt {expected}",
                self.epoch_id
            ));
        }
        Ok(())
    }

    pub fn canonical_bytes(&self) -> Result<Vec<u8>, String> {
        crate::canonical::to_canonical_bytes(self)
    }

    pub fn canonical_root(&self) -> Result<String, String> {
        Ok(format!(
            "sha256:{}",
            hex::encode(Sha256::digest(self.canonical_bytes()?))
        ))
    }

    fn derive_id(&self) -> Result<String, String> {
        let mut body = self.clone();
        body.epoch_id.clear();
        let bytes = crate::canonical::to_canonical_bytes(&body)?;
        Ok(format!("vre_{}", &hex::encode(Sha256::digest(bytes))[..16]))
    }

    fn validate_semantics(&self) -> Result<(), String> {
        if self.schema != REPOSITORY_EPOCH_V1_SCHEMA {
            return Err(format!(
                "repository epoch schema must be `{REPOSITORY_EPOCH_V1_SCHEMA}`"
            ));
        }
        require_prefixed("frontier_id", &self.frontier_id, "vfr_")?;
        if self.epoch == 0 {
            return Err("repository epoch must be positive".into());
        }
        require_text("predecessor_remote", &self.predecessor_remote)?;
        if !self.predecessor_remote.starts_with("https://github.com/")
            || !self.predecessor_remote.ends_with(".git")
        {
            return Err(
                "repository epoch predecessor_remote must be canonical GitHub HTTPS".into(),
            );
        }
        require_text("predecessor_tag", &self.predecessor_tag)?;
        if !self.predecessor_tag.starts_with("pre-current-epoch/") {
            return Err("repository epoch predecessor_tag must use `pre-current-epoch/`".into());
        }
        require_git_oid("predecessor_commit", &self.predecessor_commit)?;
        require_git_oid("predecessor_tree", &self.predecessor_tree)?;
        require_text(
            "predecessor_profile_schema",
            &self.predecessor_profile_schema,
        )?;
        for (field, root) in [
            (
                "predecessor_roots.event_log",
                &self.predecessor_roots.event_log,
            ),
            (
                "predecessor_roots.scientific_state",
                &self.predecessor_roots.scientific_state,
            ),
            (
                "predecessor_roots.compatibility_snapshot",
                &self.predecessor_roots.compatibility_snapshot,
            ),
            (
                "predecessor_roots.proposal_state",
                &self.predecessor_roots.proposal_state,
            ),
            (
                "predecessor_roots.actor_registry",
                &self.predecessor_roots.actor_registry,
            ),
            (
                "predecessor_roots.artifact_registry",
                &self.predecessor_roots.artifact_registry,
            ),
            (
                "predecessor_roots.authority_head",
                &self.predecessor_roots.authority_head,
            ),
            (
                "predecessor_roots.authority_event_log",
                &self.predecessor_roots.authority_event_log,
            ),
        ] {
            require_sha256(field, root)?;
        }
        for (field, root) in [
            (
                "predecessor_git_object_manifest_root",
                &self.predecessor_git_object_manifest_root,
            ),
            ("archive_bundle_sha256", &self.archive_bundle_sha256),
            ("imported_claim_set_root", &self.imported_claim_set_root),
            (
                "retained_current_object_set_root",
                &self.retained_current_object_set_root,
            ),
            (
                "archived_object_index_root",
                &self.archived_object_index_root,
            ),
            ("equivalence_report_root", &self.equivalence_report_root),
        ] {
            require_sha256(field, root)?;
        }
        require_text("reason", &self.reason)?;
        Ok(())
    }
}

fn require_text(field: &str, value: &str) -> Result<(), String> {
    if value.trim().is_empty() || value != value.trim() || value.chars().any(char::is_control) {
        return Err(format!(
            "repository epoch {field} must be non-empty, trimmed text"
        ));
    }
    Ok(())
}

fn require_prefixed(field: &str, value: &str, prefix: &str) -> Result<(), String> {
    require_text(field, value)?;
    if !value.starts_with(prefix) {
        return Err(format!("repository epoch {field} must start with {prefix}"));
    }
    Ok(())
}

fn require_sha256(field: &str, value: &str) -> Result<(), String> {
    let digest = value
        .strip_prefix("sha256:")
        .ok_or_else(|| format!("repository epoch {field} must be a full sha256: digest"))?;
    if digest.len() != 64
        || !digest
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    {
        return Err(format!(
            "repository epoch {field} must be a full sha256: digest"
        ));
    }
    Ok(())
}

fn require_git_oid(field: &str, value: &str) -> Result<(), String> {
    if !matches!(value.len(), 40 | 64)
        || !value
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    {
        return Err(format!(
            "repository epoch {field} must be a full Git object id"
        ));
    }
    Ok(())
}

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

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

    fn fixture() -> RepositoryEpochV1 {
        RepositoryEpochV1::build(
            "vfr_fixture".into(),
            1,
            "https://github.com/vela-science/fixture-frontier.git".into(),
            "pre-current-epoch/2026-07-27".into(),
            "a".repeat(40),
            "b".repeat(40),
            "vela.frontier-profile.v1".into(),
            PredecessorRoots {
                event_log: root('a'),
                scientific_state: root('b'),
                compatibility_snapshot: root('c'),
                proposal_state: root('d'),
                actor_registry: root('e'),
                artifact_registry: root('f'),
                authority_head: root('1'),
                authority_event_log: root('2'),
            },
            root('3'),
            root('4'),
            root('5'),
            root('6'),
            root('7'),
            root('8'),
            "Adopt the current repository contract.".into(),
        )
        .unwrap()
    }

    #[test]
    fn repository_epoch_is_content_addressed() {
        let value = fixture();
        assert!(value.epoch_id.starts_with("vre_"));
        RepositoryEpochV1::parse(&value.canonical_bytes().unwrap()).unwrap();
    }

    #[test]
    fn repository_epoch_rejects_predecessor_substitution() {
        let mut value = fixture();
        value.predecessor_commit = "9".repeat(40);
        assert!(value.verify().is_err());
    }

    #[test]
    fn native_genesis_has_no_predecessor_contract() {
        let value = RepositoryGenesisV1::build(
            "vfr_0123456789abcdef".into(),
            root('a'),
            root('b'),
            "Establish repository authority.".into(),
        )
        .unwrap();
        let bytes = value.canonical_bytes().unwrap();
        let parsed = RepositoryBoundaryV1::parse(&bytes).unwrap();
        assert_eq!(parsed.frontier_id(), "vfr_0123456789abcdef");
        assert_eq!(parsed.epoch_id(), value.epoch_id);
        assert!(parsed.predecessor_roots().is_none());
        let json = String::from_utf8(bytes).unwrap();
        assert!(!json.contains("predecessor"));
        assert!(!json.contains("archive"));
        assert!(!json.contains("equivalence"));
    }

    #[test]
    fn native_genesis_rejects_nonempty_history_roots() {
        let mut value = RepositoryGenesisV1::build(
            "vfr_0123456789abcdef".into(),
            root('a'),
            root('b'),
            "Establish repository authority.".into(),
        )
        .unwrap();
        value.initial_event_log_root = root('c');
        assert!(value.verify().is_err());
    }
}