heddle-object-model 0.24.0

Heddle's content-addressed object model and stable codecs.
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
//! Explicit present-tense acceptance of immutable originals. This never claims
//! that an expired or revoked original credential authorized a past action.
#[path = "original_boundary_preflight.rs"]
mod preflight;

use std::collections::BTreeSet;

use serde::{Deserialize, Serialize};
use uuid::Uuid;

use super::{
    ContentHash, StateId,
    thread_authority_admission::OriginalAuthorityBinding,
    thread_replication::{
        GenesisOwner, SourceAuthor, ThreadGenesis, ThreadOperation, metadata::AUTHORITY_FORMAT,
        ownership_claim::ThreadOwnershipClaim, ownership_resolution::ThreadOwnershipResolution,
    },
};
use crate::error::{HeddleError, Result};

pub const FORMAT: &str = "heddle-original-boundary-acceptance-v1";
pub const MANIFEST_FORMAT: &str = "heddle-original-publication-manifest-v1";
pub const INTENT_FORMAT: &str = "heddle-original-publication-intent-v1";
pub const MAX_RECORDS: usize = 10_384;
pub const MAX_MANIFEST_BYTES: usize = 16 * 1024 * 1024;
pub const MAX_ACCEPTANCE_BYTES: usize = 96 * 1024;

/// The manifest classifies all original records, including ineligible metadata
/// and unclaimed local sources; their inclusion grants no acceptance authority.
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
pub enum ManifestSubject {
    Genesis(ContentHash),
    Source(ContentHash),
    OtherOperation(ContentHash),
    OwnershipClaim(ContentHash),
    OwnershipResolution(ContentHash),
}
impl ManifestSubject {
    pub fn id(&self) -> ContentHash {
        match self {
            Self::Genesis(id)
            | Self::Source(id)
            | Self::OtherOperation(id)
            | Self::OwnershipClaim(id)
            | Self::OwnershipResolution(id) => *id,
        }
    }
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OriginalManifestEntry {
    pub subject: ManifestSubject,
    pub thread: ContentHash,
    pub publisher: [u8; 32],
    pub authority: Option<OriginalAuthorityBinding>,
}
impl OriginalManifestEntry {
    /// Caller verifies the original signature before building this descriptor.
    pub fn from_operation(operation: &ThreadOperation) -> Result<Self> {
        let id = operation.id()?;
        Ok(Self {
            subject: if operation.source_result()?.is_some() {
                ManifestSubject::Source(id)
            } else {
                ManifestSubject::OtherOperation(id)
            },
            thread: operation.thread,
            publisher: operation.publisher,
            authority: OriginalAuthorityBinding::from_operation(operation)?,
        })
    }
    /// Caller verifies the creator signature. Genesis does not directly sign an
    /// agent field: None here means unspecified, not human attribution. The exact
    /// envelope digest binds the identity the host subsequently inspects.
    pub fn from_genesis(genesis: &ThreadGenesis, creator_authority: &[u8]) -> Result<Self> {
        let authority = match genesis.owner {
            GenesisOwner::Account(account) => {
                if creator_authority.is_empty() || creator_authority.len() > 64 * 1024 {
                    return Err(invalid(
                        "account genesis requires bounded creator authority",
                    ));
                }
                Some(OriginalAuthorityBinding {
                    spool: Uuid::parse_str(&genesis.spool)
                        .map_err(|_| invalid("invalid genesis Spool"))?,
                    actor: super::CollaborationActor {
                        principal_id: account,
                        agent_id: None,
                    },
                    authority_digest: ContentHash::compute_typed(
                        AUTHORITY_FORMAT,
                        creator_authority,
                    ),
                })
            }
            GenesisOwner::LocalKey(_) => {
                if !creator_authority.is_empty() {
                    return Err(invalid("local genesis has no account authority"));
                }
                None
            }
        };
        let id = genesis.id()?;
        Ok(Self {
            subject: ManifestSubject::Genesis(id),
            thread: id,
            publisher: genesis.creator,
            authority,
        })
    }
    /// Caller verifies both original claim signatures and its immutable genesis.
    pub fn from_claim(claim: &ThreadOwnershipClaim) -> Result<Self> {
        claim.encode()?;
        let SourceAuthor::Account {
            spool,
            actor,
            authority_digest,
            ..
        } = &claim.acceptance
        else {
            return Err(invalid("claim requires explicit account authority"));
        };
        Ok(Self {
            subject: ManifestSubject::OwnershipClaim(claim.id()?),
            thread: claim.thread,
            publisher: claim.accepting_publisher,
            authority: Some(OriginalAuthorityBinding {
                spool: *spool,
                actor: actor.clone(),
                authority_digest: *authority_digest,
            }),
        })
    }
    pub fn from_resolution(resolution: &ThreadOwnershipResolution) -> Result<Self> {
        resolution.encode()?;
        let SourceAuthor::Account {
            spool,
            actor,
            authority_digest,
            ..
        } = &resolution.acceptance
        else {
            return Err(invalid("resolution requires explicit account acceptance"));
        };
        Ok(Self {
            subject: ManifestSubject::OwnershipResolution(resolution.id()?),
            thread: resolution.thread,
            publisher: resolution.accepting_publisher,
            authority: Some(OriginalAuthorityBinding {
                spool: *spool,
                actor: actor.clone(),
                authority_digest: *authority_digest,
            }),
        })
    }
    fn validate(&self) -> Result<()> {
        if self.publisher == [0; 32]
            || self.subject.id().as_bytes() == &[0; 32]
            || self.thread.as_bytes() == &[0; 32]
        {
            return Err(invalid("invalid original manifest identity"));
        }
        if matches!(self.subject, ManifestSubject::Genesis(_))
            && (self.subject.id() != self.thread
                || self
                    .authority
                    .as_ref()
                    .is_some_and(|binding| binding.actor.agent_id.is_some()))
        {
            return Err(invalid("genesis manifest cannot assert an unsigned agent"));
        }
        if let Some(binding) = &self.authority
            && (binding.spool.is_nil()
                || binding.actor.principal_id.is_nil()
                || binding.actor.agent_id.as_ref().is_some_and(|id| {
                    id.is_empty() || id.len() > 256 || id.chars().any(char::is_control)
                }))
        {
            return Err(invalid("invalid original manifest authority"));
        }
        Ok(())
    }
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OriginalPublicationManifest {
    pub version: u16,
    pub entries: Vec<OriginalManifestEntry>,
}
impl OriginalPublicationManifest {
    pub fn new(mut entries: Vec<OriginalManifestEntry>) -> Result<Self> {
        if entries.len() > MAX_RECORDS {
            return Err(invalid("original manifest record bound exceeded"));
        }
        entries.sort_by(|a, b| a.subject.cmp(&b.subject));
        let value = Self {
            version: 1,
            entries,
        };
        value.encode()?;
        Ok(value)
    }
    pub fn encode(&self) -> Result<Vec<u8>> {
        if self.version != 1 || self.entries.is_empty() || self.entries.len() > MAX_RECORDS {
            return Err(invalid("original manifest record bound exceeded"));
        }
        let mut ids = BTreeSet::new();
        for entry in &self.entries {
            entry.validate()?;
            // Source and OtherOperation share the original operation ID domain.
            let domain = match entry.subject {
                ManifestSubject::Genesis(_) => 0,
                ManifestSubject::Source(_) | ManifestSubject::OtherOperation(_) => 1,
                ManifestSubject::OwnershipClaim(_) => 2,
                ManifestSubject::OwnershipResolution(_) => 3,
            };
            if !ids.insert((domain, entry.subject.id())) {
                return Err(invalid("duplicate original manifest identity"));
            }
        }
        if self
            .entries
            .windows(2)
            .any(|pair| pair[0].subject >= pair[1].subject)
        {
            return Err(invalid("original manifest is not sorted"));
        }
        let bytes = rmp_serde::to_vec_named(self)?;
        if bytes.len() > MAX_MANIFEST_BYTES {
            return Err(invalid("original manifest byte bound exceeded"));
        }
        Ok(bytes)
    }
    pub fn decode(bytes: &[u8]) -> Result<Self> {
        if bytes.is_empty() || bytes.len() > MAX_MANIFEST_BYTES {
            return Err(invalid("original manifest byte bound exceeded"));
        }
        let value: Self =
            preflight::decode(bytes, true, |bytes| Ok(rmp_serde::from_slice(bytes)?))?;
        if value.encode()? != bytes {
            return Err(invalid("noncanonical original manifest"));
        }
        Ok(value)
    }
    pub fn id(&self) -> Result<ContentHash> {
        Ok(ContentHash::compute_typed(MANIFEST_FORMAT, &self.encode()?))
    }
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
pub enum BoundaryOriginalKind {
    Source,
    AccountGenesis,
    OwnershipClaim,
    OwnershipResolution,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PublicationIntent {
    pub spool: Uuid,
    pub spool_genesis: ContentHash,
    pub thread: ContentHash,
    pub revision: StateId,
    /// Digest of the complete ordered pack/index inventory, verified by intake.
    pub inventory: ContentHash,
    pub sharing_policy: Option<ContentHash>,
    pub source: [u8; 32],
    pub destination: [u8; 32],
    pub client_operation_id: Uuid,
}
impl PublicationIntent {
    pub fn id(&self) -> Result<ContentHash> {
        if self.spool.is_nil()
            || self.client_operation_id.is_nil()
            || self.source == [0; 32]
            || self.destination == [0; 32]
        {
            return Err(invalid("invalid boundary publication intent"));
        }
        Ok(ContentHash::compute_typed(
            INTENT_FORMAT,
            &rmp_serde::to_vec_named(self)?,
        ))
    }
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OriginalBoundaryAcceptance {
    pub version: u16,
    pub publication_intent: ContentHash,
    pub originals_manifest: ContentHash,
    pub original_account: Uuid,
    pub kinds: BTreeSet<BoundaryOriginalKind>,
    pub accepting_publisher: [u8; 32],
    /// Current explicit acceptance, independent of each immutable original author.
    pub accepting_author: SourceAuthor,
}
impl OriginalBoundaryAcceptance {
    pub fn encode(&self) -> Result<Vec<u8>> {
        self.accepting_author.validate()?;
        let SourceAuthor::Account { actor, .. } = &self.accepting_author else {
            return Err(invalid(
                "boundary acceptance requires explicit account authority",
            ));
        };
        if self.version != 1
            || self.kinds.is_empty()
            || self.original_account.is_nil()
            || actor.principal_id != self.original_account
            || self.accepting_publisher == [0; 32]
        {
            return Err(invalid("invalid boundary acceptance identity"));
        }
        let bytes = rmp_serde::to_vec_named(self)?;
        if bytes.len() > MAX_ACCEPTANCE_BYTES {
            return Err(invalid("boundary acceptance byte bound exceeded"));
        }
        Ok(bytes)
    }
    pub fn decode(bytes: &[u8]) -> Result<Self> {
        if bytes.is_empty() || bytes.len() > MAX_ACCEPTANCE_BYTES {
            return Err(invalid("boundary acceptance byte bound exceeded"));
        }
        let value: Self =
            preflight::decode(bytes, false, |bytes| Ok(rmp_serde::from_slice(bytes)?))?;
        if value.encode()? != bytes {
            return Err(invalid("noncanonical boundary acceptance"));
        }
        Ok(value)
    }
    pub fn id(&self) -> Result<ContentHash> {
        Ok(ContentHash::compute_typed(FORMAT, &self.encode()?))
    }
    /// Only verifies exact signed intent/selection. This is NOT current authority
    /// or proof that the original credentials ever authorized an action.
    pub fn selected<'a>(
        &self,
        intent: &PublicationIntent,
        manifest: &'a OriginalPublicationManifest,
    ) -> Result<Vec<&'a OriginalManifestEntry>> {
        self.encode()?;
        let SourceAuthor::Account { spool, .. } = &self.accepting_author else {
            return Err(invalid("account acceptance required"));
        };
        if *spool != intent.spool
            || self.publication_intent != intent.id()?
            || self.originals_manifest != manifest.id()?
        {
            return Err(invalid(
                "boundary acceptance differs from exact publication",
            ));
        }
        let selected: Vec<_> = manifest
            .entries
            .iter()
            .filter(|entry| {
                let Some(authority) = &entry.authority else {
                    return false;
                };
                if authority.spool != intent.spool
                    || authority.actor.principal_id != self.original_account
                {
                    return false;
                }
                let kind = match entry.subject {
                    ManifestSubject::Genesis(_) => BoundaryOriginalKind::AccountGenesis,
                    ManifestSubject::Source(_) => BoundaryOriginalKind::Source,
                    ManifestSubject::OwnershipClaim(_) => BoundaryOriginalKind::OwnershipClaim,
                    ManifestSubject::OwnershipResolution(_) => {
                        BoundaryOriginalKind::OwnershipResolution
                    }
                    ManifestSubject::OtherOperation(_) => return false,
                };
                self.kinds.contains(&kind)
            })
            .collect();
        if selected.is_empty() {
            return Err(invalid("boundary acceptance selects no original"));
        }
        Ok(selected)
    }
}
/// Explicit canonical receipt basis; historical original-author testimony is
/// never reinterpreted as a fresh boundary acceptance.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum AdmissionBasis {
    OriginalAuthority,
    BoundaryAcceptance { acceptance: ContentHash },
}
impl AdmissionBasis {
    /// Structural evidence binding only. The crypto layer verifies its signature;
    /// an independently pinned per-original executor receipt attests membership.
    pub fn authorize_evidence(
        &self,
        evidence: Option<&OriginalBoundaryAcceptance>,
        spool: Uuid,
        account: Uuid,
        kind: Option<BoundaryOriginalKind>,
    ) -> Result<()> {
        match (self, evidence) {
            (Self::OriginalAuthority, None) => Ok(()),
            (Self::BoundaryAcceptance { acceptance }, Some(value)) => {
                let SourceAuthor::Account {
                    spool: accepting_spool,
                    ..
                } = &value.accepting_author
                else {
                    return Err(invalid("boundary receipt requires account acceptance"));
                };
                if value.id()? != *acceptance
                    || *accepting_spool != spool
                    || value.original_account != account
                    || !kind.is_some_and(|kind| value.kinds.contains(&kind))
                {
                    return Err(invalid(
                        "boundary receipt evidence differs from original authority scope",
                    ));
                }
                Ok(())
            }
            _ => Err(invalid(
                "receipt requires exactly its matched admission basis evidence",
            )),
        }
    }
}
fn invalid(message: &str) -> HeddleError {
    HeddleError::InvalidObject(message.into())
}

#[cfg(test)]
#[path = "original_boundary_acceptance_tests.rs"]
mod tests;