evidencebook 0.0.23

Evidence book: records, epistemic types, typed links, retention, disclosure, index classes, request answers and reconcile/close over an embedded Checkpointed Local Log -- a Rust implementation of the Evidence Layer semantics.
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
//! The record header, its epistemic type, and typed links.
//!
//! I-D sections: the record header (anchor `record`), *Epistemic Type*,
//! *Typed Links*.
//!
//! The epistemic-type value set is not defined here. It is read from
//! `schemas/vendor/epistemic-types.json`, the same vendored file the Go and
//! Python implementations read, and [`EpistemicType::ALL`] is tested against
//! it so the three implementations cannot drift apart silently.

use crate::retention::RetentionState;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;

/// The vendored epistemic-type value set, byte for byte as shipped.
pub const VENDORED_EPISTEMIC_TYPES_JSON: &str =
    include_str!("../schemas/vendor/epistemic-types.json");

/// The header version this crate writes and accepts.
pub const HEADER_VERSION: u32 = 1;

/// How a record's content came to be known (I-D *Epistemic Type*). Closed
/// vocabulary, assigned once at commit and never upgraded.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EpistemicType {
    ObservedEvent,
    SystemOfRecordFact,
    ProducerClaim,
    HumanReport,
    SemanticJudgment,
    DerivedMetric,
    Adjudication,
    ObligationReference,
}

impl EpistemicType {
    /// Every value, in the I-D's table order.
    pub const ALL: [EpistemicType; 8] = [
        EpistemicType::ObservedEvent,
        EpistemicType::SystemOfRecordFact,
        EpistemicType::ProducerClaim,
        EpistemicType::HumanReport,
        EpistemicType::SemanticJudgment,
        EpistemicType::DerivedMetric,
        EpistemicType::Adjudication,
        EpistemicType::ObligationReference,
    ];

    /// The wire token (lowercase, as the record header carries it).
    pub fn as_str(self) -> &'static str {
        match self {
            EpistemicType::ObservedEvent => "observed_event",
            EpistemicType::SystemOfRecordFact => "system_of_record_fact",
            EpistemicType::ProducerClaim => "producer_claim",
            EpistemicType::HumanReport => "human_report",
            EpistemicType::SemanticJudgment => "semantic_judgment",
            EpistemicType::DerivedMetric => "derived_metric",
            EpistemicType::Adjudication => "adjudication",
            EpistemicType::ObligationReference => "obligation_reference",
        }
    }
}

impl fmt::Display for EpistemicType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for EpistemicType {
    type Err = RecordError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        EpistemicType::ALL
            .into_iter()
            .find(|t| t.as_str() == s)
            .ok_or_else(|| RecordError::UnknownEpistemicType(s.to_string()))
    }
}

/// A link type (I-D *Typed Links*). Closed vocabulary: a relationship the
/// list does not name is a new registered type, never an overloaded token.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LinkType {
    Cites,
    Adjudicates,
    Supersedes,
    Acknowledges,
    Rebuts,
    Closes,
}

impl LinkType {
    /// Every value, in the I-D's order.
    pub const ALL: [LinkType; 6] = [
        LinkType::Cites,
        LinkType::Adjudicates,
        LinkType::Supersedes,
        LinkType::Acknowledges,
        LinkType::Rebuts,
        LinkType::Closes,
    ];

    /// The wire token.
    pub fn as_str(self) -> &'static str {
        match self {
            LinkType::Cites => "cites",
            LinkType::Adjudicates => "adjudicates",
            LinkType::Supersedes => "supersedes",
            LinkType::Acknowledges => "acknowledges",
            LinkType::Rebuts => "rebuts",
            LinkType::Closes => "closes",
        }
    }
}

/// A typed, directed reference from the carrying record to a target record,
/// named by the target's record id (I-D *Typed Links*). A link is committed
/// inside the carrying record and never mutates its target.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Link {
    #[serde(rename = "type")]
    pub link_type: LinkType,
    pub target: String,
}

/// Record types the book itself writes. `record_type` stays open vocabulary
/// for callers; these are the ones this crate's semantics depend on. The
/// tokens match the Go implementation's.
pub mod record_types {
    /// A Close (I-D *Reconcile and Close*).
    pub const CLOSE: &str = "close";
    /// A disclosure record (I-D *Disclosure Records*).
    pub const DISCLOSURE: &str = "disclosure";
    /// A retention change (I-D *Retention States*).
    pub const LIFECYCLE: &str = "lifecycle";
    /// A committed authenticated-index root (I-D *Index Classes*).
    pub const INDEX_ROOT: &str = "index_root";
    /// A request this book sent.
    pub const REQUEST: &str = "evidence_request";
    /// A request this book answered.
    pub const REQUEST_ANSWERED: &str = "evidence_request_answered";
    /// A response this book received.
    pub const RESPONSE: &str = "evidence_response";
    /// The requester's own record that nothing arrived in its window.
    pub const RECORDED_ABSENCE: &str = "recorded_absence";
    /// A record carrying an `acknowledges` link to a counterparty's Close.
    pub const ACKNOWLEDGEMENT: &str = "acknowledgement";
    /// A record carrying a `rebuts` link to a counterparty's Close.
    pub const REBUTTAL: &str = "rebuttal";
}

/// The durable record header (I-D section anchor `record`). Its canonical JSON is
/// what a record commits to, so every field is fixed at commit. JSON member
/// names match the Go implementation's header.
///
/// `correlation` holds the calling profile's correlation keys by name. The
/// book never interprets a key's name; [`crate::reconcile::JoinPolicy`] says
/// which keys pair two halves.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Header {
    #[serde(rename = "v")]
    pub version: u32,
    pub book_id: String,
    pub seq: u64,
    pub record_type: String,
    pub epistemic_type: EpistemicType,
    pub committed_at: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub event_time_claim: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub payload_commitments: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub retention_state: Option<RetentionState>,
    pub links: Vec<Link>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub subject_ref: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub principal_ref: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub counterparty_ref: Option<String>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub correlation: BTreeMap<String, String>,
    /// Marks a record of a reproduced scratch book (evidencebook#232): its
    /// `committed_at` was given by the caller, not read from a clock. It is
    /// part of the digested header. Absent in a book that stamps the wall clock.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reproduction: Option<Reproduction>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub statement: Option<serde_json::Value>,
}

/// The only clock a [`Reproduction`] names: the caller gave the times.
pub const REPRODUCTION_CLOCK_CALLER: &str = "caller";

/// The header marker of a reproduced scratch book.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Reproduction {
    pub clock: String,
}

impl Header {
    /// Check the header against the I-D's rules for a committed record:
    /// required fields present, `epistemic_type` and link types registered
    /// (enforced by the types), `retention_state` present whenever payload
    /// commitments are, every digest and link target a lowercase SHA-256 hex
    /// value, and `epistemic_type` `adjudication` on any record carrying an
    /// `adjudicates` link.
    pub fn validate(&self) -> Result<(), RecordError> {
        if self.version != HEADER_VERSION {
            return Err(RecordError::Invalid(format!(
                "header version {}",
                self.version
            )));
        }
        if self.book_id.is_empty()
            || self.seq == 0
            || self.record_type.is_empty()
            || self.committed_at.is_empty()
        {
            return Err(RecordError::Invalid(
                "book_id, seq, record_type and committed_at are required".into(),
            ));
        }
        if !self.payload_commitments.is_empty() && self.retention_state.is_none() {
            return Err(RecordError::Invalid(
                "retention_state is required with payload_commitments".into(),
            ));
        }
        if let Some(digest) = self.payload_commitments.iter().find(|d| !is_digest(d)) {
            return Err(RecordError::Invalid(format!(
                "payload commitment {digest:?} is not a lowercase SHA-256 digest"
            )));
        }
        if let Some(r) = &self.reproduction {
            if r.clock != REPRODUCTION_CLOCK_CALLER {
                return Err(RecordError::Invalid(format!(
                    "reproduction clock {:?} is not {REPRODUCTION_CLOCK_CALLER:?}",
                    r.clock
                )));
            }
        }
        for link in &self.links {
            if !is_digest(&link.target) {
                return Err(RecordError::Invalid(format!(
                    "link target {:?} is not a record id",
                    link.target
                )));
            }
            if link.link_type == LinkType::Adjudicates
                && self.epistemic_type != EpistemicType::Adjudication
            {
                return Err(RecordError::Invalid(
                    "a record carrying an adjudicates link must have epistemic_type adjudication"
                        .into(),
                ));
            }
        }
        Ok(())
    }

    /// The targets of every link of type `link_type`, in header order.
    pub fn links_to(&self, link_type: LinkType) -> Vec<&str> {
        self.links
            .iter()
            .filter(|l| l.link_type == link_type)
            .map(|l| l.target.as_str())
            .collect()
    }
}

/// True when `s` is 64 lowercase hex characters.
pub fn is_digest(s: &str) -> bool {
    s.len() == 64 && s.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
}

#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum RecordError {
    #[error("epistemic_type {0:?} is not registered")]
    UnknownEpistemicType(String),
    #[error("invalid record header: {0}")]
    Invalid(String),
}

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

    fn header() -> Header {
        Header {
            version: HEADER_VERSION,
            book_id: "book-a".into(),
            seq: 1,
            record_type: "observation".into(),
            epistemic_type: EpistemicType::ObservedEvent,
            committed_at: "2026-09-27T00:00:00Z".into(),
            event_time_claim: None,
            payload_commitments: vec![],
            retention_state: None,
            links: vec![],
            subject_ref: None,
            principal_ref: None,
            counterparty_ref: None,
            correlation: BTreeMap::new(),
            reproduction: None,
            statement: None,
        }
    }

    #[test]
    fn epistemic_types_match_the_vendored_set() {
        let vendored: serde_json::Value =
            serde_json::from_str(VENDORED_EPISTEMIC_TYPES_JSON).unwrap();
        let values: Vec<String> = vendored["values"]
            .as_array()
            .expect("vendored file carries a values array")
            .iter()
            .map(|v| v.as_str().unwrap().to_ascii_lowercase())
            .collect();
        let ours: Vec<&str> = EpistemicType::ALL.iter().map(|t| t.as_str()).collect();
        assert_eq!(values, ours);
    }

    #[test]
    fn wire_tokens_round_trip_through_serde_and_from_str() {
        for t in EpistemicType::ALL {
            let json = serde_json::to_string(&t).unwrap();
            assert_eq!(json, format!("\"{}\"", t.as_str()));
            assert_eq!(t.as_str().parse::<EpistemicType>().unwrap(), t);
        }
        for t in LinkType::ALL {
            assert_eq!(
                serde_json::to_string(&t).unwrap(),
                format!("\"{}\"", t.as_str())
            );
        }
        assert!("OBSERVED_EVENT".parse::<EpistemicType>().is_err());
    }

    #[test]
    fn adjudicates_link_requires_an_adjudication_record() {
        let mut h = header();
        h.links.push(Link {
            link_type: LinkType::Adjudicates,
            target: "a".repeat(64),
        });
        assert!(h.validate().is_err());
        h.epistemic_type = EpistemicType::Adjudication;
        h.validate().unwrap();
    }

    #[test]
    fn payload_commitments_require_a_retention_state() {
        let mut h = header();
        h.payload_commitments.push("b".repeat(64));
        assert!(h.validate().is_err());
        h.retention_state = Some(RetentionState::Available);
        h.validate().unwrap();
    }

    #[test]
    fn digests_must_be_lowercase_sha256_hex() {
        let mut h = header();
        h.retention_state = Some(RetentionState::Available);
        h.payload_commitments.push("B".repeat(64));
        assert!(h.validate().is_err());
        let mut h = header();
        h.links.push(Link {
            link_type: LinkType::Cites,
            target: "not-a-digest".into(),
        });
        assert!(h.validate().is_err());
    }

    #[test]
    fn required_fields_are_required() {
        for broken in [
            Header { seq: 0, ..header() },
            Header {
                book_id: String::new(),
                ..header()
            },
            Header {
                record_type: String::new(),
                ..header()
            },
            Header {
                committed_at: String::new(),
                ..header()
            },
            Header {
                version: 2,
                ..header()
            },
        ] {
            assert!(broken.validate().is_err(), "{broken:?}");
        }
    }

    #[test]
    fn links_serialize_under_the_type_member_and_are_always_present() {
        let mut h = header();
        let empty = serde_json::to_value(&h).unwrap();
        assert_eq!(empty["links"], serde_json::json!([]));
        h.links.push(Link {
            link_type: LinkType::Supersedes,
            target: "c".repeat(64),
        });
        let v = serde_json::to_value(&h).unwrap();
        assert_eq!(v["links"][0]["type"], "supersedes");
        assert_eq!(h.links_to(LinkType::Supersedes), vec!["c".repeat(64)]);
        assert!(h.links_to(LinkType::Cites).is_empty());
    }

    #[test]
    fn a_reproduction_marker_round_trips_and_is_validated() {
        let mut h = header();
        h.reproduction = Some(Reproduction {
            clock: REPRODUCTION_CLOCK_CALLER.into(),
        });
        assert!(h.validate().is_ok());
        let v = serde_json::to_value(&h).unwrap();
        assert_eq!(v["reproduction"], serde_json::json!({"clock": "caller"}));
        assert_eq!(serde_json::from_value::<Header>(v).unwrap(), h);
        assert!(serde_json::to_value(header())
            .unwrap()
            .get("reproduction")
            .is_none());
        h.reproduction = Some(Reproduction {
            clock: "wall".into(),
        });
        assert!(h.validate().is_err());
    }
}