tilo_actions/envelope.rs
1//! The [`ActionEnvelope`]: what actually replicates.
2//!
3//! Binds an [`EncodedAction`] to the metadata a peer needs to *replay it
4//! into an identical document*: the base heads it was authored against,
5//! the deterministic actor id ([`ActionId`]), the commit timestamp, and
6//! the declared [`ExecutionLocus`]. All `Serialize`/`Deserialize` so it
7//! rides any transport (the local broker today, MLS-encrypted Iroh later).
8
9use automerge::{ActorId, ChangeHash};
10use serde::{Deserialize, Serialize};
11
12use crate::action::{ActionId, EncodedAction, ExecutionLocus};
13use crate::context::ActionContext;
14
15/// One replayable action, ready to cross a boundary.
16#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
17pub struct ActionEnvelope {
18 /// Unique id; its bytes are also the Automerge actor used to commit the
19 /// action's change, so replay reproduces the exact same change hash.
20 pub id: ActionId,
21 /// The serialized action.
22 pub action: EncodedAction,
23 /// Document heads the action was authored against — a replica at exactly
24 /// these heads replays to the identical document.
25 pub base_heads: Vec<[u8; 32]>,
26 /// Commit timestamp (unix seconds), pinned so the change is
27 /// byte-identical on origin and every replaying replica.
28 pub timestamp: i64,
29 /// The action's declared execution locus, carried so the transport can
30 /// honor it.
31 pub locus: ExecutionLocus,
32 /// The member that authored this action, when named — a person, an
33 /// agent, or another session reached through [`crate::api`].
34 ///
35 /// These bytes become the Automerge actor the change commits under, so
36 /// an edit from outside attributes in history exactly like a typed one.
37 /// `None` is the app's own dispatch (the [`ActionId`]-derived actor).
38 /// Replay stays byte-identical either way, since the actor rides on the
39 /// envelope rather than the applying replica. Optional on the wire so an
40 /// envelope written before attribution existed still deserializes.
41 #[serde(default, skip_serializing_if = "Option::is_none")]
42 pub actor: Option<Vec<u8>>,
43 /// Human provenance used by history, search, and passive activity UI.
44 /// It does not affect what the action is allowed to mutate; it is carried
45 /// into the Automerge change message so it survives the transport log.
46 #[serde(default, skip_serializing_if = "Option::is_none")]
47 pub context: Option<ActionContext>,
48}
49
50impl ActionEnvelope {
51 /// The content the change's recorded action names
52 /// ([`crate::executor::recorded_form`]), by reference: the large parts
53 /// of an action larger than a change documents whole, read from the
54 /// action as it ran. A device that applies the action keeps them in its content
55 /// store, sealed as their writer sealed them, so receivers can resolve
56 /// them to replay. Empty for an action recorded whole.
57 ///
58 /// # Errors
59 /// [`ActionError::Refused`] when the recorded action is not this one.
60 pub fn recorded_content(&self) -> Result<crate::content::ContentParts, crate::ActionError> {
61 let Some(recorded) = self
62 .context
63 .as_ref()
64 .and_then(|context| context.action.as_ref())
65 else {
66 return Ok(crate::content::ContentParts::new());
67 };
68 recorded.recorded_parts_of(&self.action).ok_or_else(|| {
69 crate::ActionError::Refused("the recorded action is not the action that ran".into())
70 })
71 }
72
73 /// The base heads as Automerge [`ChangeHash`]es.
74 pub fn base_change_hashes(&self) -> Vec<ChangeHash> {
75 self.base_heads.iter().map(|b| ChangeHash(*b)).collect()
76 }
77
78 /// The actor this action's change commits under: the authoring
79 /// member when named, else the [`ActionId`]-derived actor. Read off
80 /// the envelope so a replaying replica never substitutes its own.
81 pub fn actor(&self) -> ActorId {
82 match &self.actor {
83 Some(bytes) if !bytes.is_empty() => ActorId::from(bytes.clone()),
84 _ => ActorId::from(self.id.actor_bytes().to_vec()),
85 }
86 }
87}