vela-protocol 0.940.2

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
//! Current scientific assertion object: `vela.claim-record.v1`.
//!
//! Claim Records contain the assertion and its exact support identity. Standing
//! is derived from the current repository and its covered Decisions; it is
//! never stored in this object.

use std::collections::BTreeMap;

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

pub const CLAIM_RECORD_V1_SCHEMA: &str = "vela.claim-record.v1";
pub const LEGACY_FINDING_EXTENSION: &str = "vela.legacy-finding.v1";

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ClaimAssertion {
    pub text: String,
    pub kind: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ClaimEvidenceRef {
    pub relation: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub artifact_id: Option<String>,
    pub artifact_root: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub artifact_path: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ClaimSource {
    pub kind: String,
    pub title: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub locator: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub authors: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub year: Option<i32>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ClaimRelation {
    pub kind: String,
    pub target_claim_id: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ImportedClaimSource {
    pub era: String,
    pub object_id: String,
    pub object_root: String,
    pub predecessor_commit: String,
}

/// A versioned Claim body. The full canonical root is security identity; the
/// readable `vcl_` prefix is routing only.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ClaimRecordV1 {
    pub schema: String,
    pub claim_id: String,
    pub revision: u32,
    pub assertion: ClaimAssertion,
    pub conditions: Vec<String>,
    pub evidence: Vec<ClaimEvidenceRef>,
    pub provenance: Vec<ClaimSource>,
    pub relations: Vec<ClaimRelation>,
    pub created_at: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub imported_from: Option<ImportedClaimSource>,
    /// Closed protocol fields remain small. Domain or migration detail is
    /// namespaced and canonical but cannot carry Standing or authority.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub extensions: BTreeMap<String, Value>,
}

impl ClaimRecordV1 {
    #[allow(clippy::too_many_arguments)]
    pub fn build(
        revision: u32,
        assertion: ClaimAssertion,
        conditions: Vec<String>,
        evidence: Vec<ClaimEvidenceRef>,
        provenance: Vec<ClaimSource>,
        relations: Vec<ClaimRelation>,
        created_at: String,
        imported_from: Option<ImportedClaimSource>,
        extensions: BTreeMap<String, Value>,
    ) -> Result<Self, String> {
        let mut value = Self {
            schema: CLAIM_RECORD_V1_SCHEMA.to_string(),
            claim_id: String::new(),
            revision,
            assertion,
            conditions,
            evidence,
            provenance,
            relations,
            created_at,
            imported_from,
            extensions,
        };
        value.validate_semantics()?;
        value.claim_id = value.derive_id()?;
        value.verify()?;
        Ok(value)
    }

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

    pub fn verify(&self) -> Result<(), String> {
        self.validate_semantics()?;
        let expected = self.derive_id()?;
        if self.claim_id != expected {
            return Err(format!(
                "Claim Record id mismatch: declared {}, rebuilt {expected}",
                self.claim_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> {
        #[derive(Serialize)]
        struct ClaimIdentity<'a> {
            schema: &'static str,
            revision: u32,
            assertion: &'a ClaimAssertion,
            conditions: &'a [String],
            evidence: &'a [ClaimEvidenceRef],
            provenance: &'a [ClaimSource],
        }
        let bytes = crate::canonical::to_canonical_bytes(&ClaimIdentity {
            schema: "vela.claim-identity.v1",
            revision: self.revision,
            assertion: &self.assertion,
            conditions: &self.conditions,
            evidence: &self.evidence,
            provenance: &self.provenance,
        })?;
        Ok(format!("vcl_{}", hex::encode(Sha256::digest(bytes))))
    }

    fn validate_semantics(&self) -> Result<(), String> {
        if self.schema != CLAIM_RECORD_V1_SCHEMA {
            return Err(format!(
                "Claim Record schema must be `{CLAIM_RECORD_V1_SCHEMA}`"
            ));
        }
        if self.revision == 0 {
            return Err("Claim Record revision must be positive".into());
        }
        require_scientific_text("assertion.text", &self.assertion.text)?;
        require_text("assertion.kind", &self.assertion.kind)?;
        for condition in &self.conditions {
            require_scientific_text("conditions", condition)?;
        }
        for evidence in &self.evidence {
            require_text("evidence.relation", &evidence.relation)?;
            if let Some(artifact_id) = &evidence.artifact_id {
                require_prefixed("evidence.artifact_id", artifact_id, "va_")?;
            }
            require_sha256("evidence.artifact_root", &evidence.artifact_root)?;
            if let Some(path) = &evidence.artifact_path {
                require_relative_path("evidence.artifact_path", path)?;
            }
        }
        for source in &self.provenance {
            require_text("provenance.kind", &source.kind)?;
            require_text("provenance.title", &source.title)?;
            if let Some(locator) = &source.locator {
                require_text("provenance.locator", locator)?;
            }
            for author in &source.authors {
                require_text("provenance.authors", author)?;
            }
        }
        for relation in &self.relations {
            require_text("relations.kind", &relation.kind)?;
            require_full_claim_id("relations.target_claim_id", &relation.target_claim_id)?;
        }
        chrono::DateTime::parse_from_rfc3339(&self.created_at)
            .map_err(|_| "Claim Record created_at must be RFC 3339".to_string())?;
        if let Some(source) = &self.imported_from {
            require_text("imported_from.era", &source.era)?;
            require_text("imported_from.object_id", &source.object_id)?;
            require_sha256("imported_from.object_root", &source.object_root)?;
            require_git_oid(
                "imported_from.predecessor_commit",
                &source.predecessor_commit,
            )?;
        }
        for (namespace, value) in &self.extensions {
            if !namespace.contains('.') || namespace.starts_with('.') || namespace.ends_with('.') {
                return Err(format!(
                    "Claim Record extension `{namespace}` must use a dotted namespace"
                ));
            }
            if !value.is_object() {
                return Err(format!(
                    "Claim Record extension `{namespace}` must be a JSON object"
                ));
            }
            reject_authority_extension(namespace, value)?;
        }
        Ok(())
    }
}

fn reject_authority_extension(namespace: &str, value: &Value) -> Result<(), String> {
    const FORBIDDEN: &[&str] = &[
        "standing",
        "accepted",
        "accepted_state",
        "decision",
        "authority",
        "signature",
    ];
    if let Some(object) = value.as_object() {
        for key in object.keys() {
            if FORBIDDEN.contains(&key.as_str()) {
                return Err(format!(
                    "Claim Record extension `{namespace}` cannot carry authority field `{key}`"
                ));
            }
        }
    }
    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!(
            "Claim Record {field} must be non-empty, trimmed text"
        ));
    }
    Ok(())
}

fn require_scientific_text(field: &str, value: &str) -> Result<(), String> {
    if value.trim().is_empty()
        || value != value.trim()
        || value
            .chars()
            .any(|character| character.is_control() && character != '\n' && character != '\t')
    {
        return Err(format!(
            "Claim Record {field} must be non-empty, trimmed scientific 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!("Claim Record {field} must start with `{prefix}`"));
    }
    Ok(())
}

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

fn require_sha256(field: &str, value: &str) -> Result<(), String> {
    let digest = value
        .strip_prefix("sha256:")
        .ok_or_else(|| format!("Claim Record {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!(
            "Claim Record {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!("Claim Record {field} must be a full Git object id"));
    }
    Ok(())
}

fn require_relative_path(field: &str, value: &str) -> Result<(), String> {
    require_text(field, value)?;
    if value.starts_with('/') || value.split('/').any(|segment| segment == "..") {
        return Err(format!("Claim Record {field} must be a safe relative path"));
    }
    Ok(())
}

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

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

    #[test]
    fn claim_record_is_content_addressed_and_carries_no_standing() {
        let record = ClaimRecordV1::build(
            1,
            ClaimAssertion {
                text: "A bounded computation returned no witness.".into(),
                kind: "computational".into(),
            },
            vec!["Range 1..100 inclusive.".into()],
            vec![ClaimEvidenceRef {
                relation: "supports".into(),
                artifact_id: Some("va_fixture".into()),
                artifact_root: root('a'),
                artifact_path: Some(format!("records/artifacts/sha256/{}", "a".repeat(64))),
            }],
            vec![ClaimSource {
                kind: "repository_import".into(),
                title: "Historical Finding vf_fixture".into(),
                locator: None,
                authors: vec![],
                year: None,
            }],
            vec![],
            "2026-07-27T00:00:00Z".into(),
            Some(ImportedClaimSource {
                era: "finding_v0_10".into(),
                object_id: "vf_fixture".into(),
                object_root: root('b'),
                predecessor_commit: "c".repeat(40),
            }),
            BTreeMap::new(),
        )
        .unwrap();
        assert!(record.claim_id.starts_with("vcl_"));
        ClaimRecordV1::parse(&record.canonical_bytes().unwrap()).unwrap();

        let mut raw = serde_json::to_value(&record).unwrap();
        raw["claim_id"] = Value::String("vcl_tampered".into());
        assert!(ClaimRecordV1::parse(&serde_json::to_vec(&raw).unwrap()).is_err());
    }

    #[test]
    fn extension_cannot_smuggle_standing_or_authority() {
        let mut extensions = BTreeMap::new();
        extensions.insert(
            LEGACY_FINDING_EXTENSION.into(),
            serde_json::json!({"standing": "accepted"}),
        );
        let error = ClaimRecordV1::build(
            1,
            ClaimAssertion {
                text: "Claim".into(),
                kind: "theoretical".into(),
            },
            vec![],
            vec![],
            vec![],
            vec![],
            "2026-07-27T00:00:00Z".into(),
            None,
            extensions,
        )
        .unwrap_err();
        assert!(error.contains("cannot carry authority field"));
    }

    #[test]
    fn scientific_assertions_preserve_bounded_multiline_text() {
        let record = ClaimRecordV1::build(
            1,
            ClaimAssertion {
                text: "Let n be positive.\n\nThen the bounded conclusion follows.".into(),
                kind: "theoretical".into(),
            },
            vec!["Assume the stated hypotheses.\nUse the exact pinned source.".into()],
            vec![],
            vec![],
            vec![],
            "2026-07-27T00:00:00Z".into(),
            None,
            BTreeMap::new(),
        )
        .unwrap();
        assert!(record.assertion.text.contains("\n\n"));
        ClaimRecordV1::parse(&record.canonical_bytes().unwrap()).unwrap();
    }
}