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
//! The [`ActionEnvelope`]: what actually replicates.
//!
//! Binds an [`EncodedAction`] to the metadata a peer needs to *replay it
//! into an identical document*: the base heads it was authored against,
//! the deterministic actor id ([`ActionId`]), the commit timestamp, and
//! the declared [`ExecutionLocus`]. All `Serialize`/`Deserialize` so it
//! rides any transport (the local broker today, MLS-encrypted Iroh later).
use automerge::{ActorId, ChangeHash};
use serde::{Deserialize, Serialize};
use crate::action::{ActionId, EncodedAction, ExecutionLocus};
use crate::context::ActionContext;
/// One replayable action, ready to cross a boundary.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActionEnvelope {
/// Unique id; its bytes are also the Automerge actor used to commit the
/// action's change, so replay reproduces the exact same change hash.
pub id: ActionId,
/// The serialized action.
pub action: EncodedAction,
/// Document heads the action was authored against — a replica at exactly
/// these heads replays to the identical document.
pub base_heads: Vec<[u8; 32]>,
/// Commit timestamp (unix seconds), pinned so the change is
/// byte-identical on origin and every replaying replica.
pub timestamp: i64,
/// The action's declared execution locus, carried so the transport can
/// honor it.
pub locus: ExecutionLocus,
/// The member that authored this action, when named — a person, an
/// agent, or another session reached through [`crate::api`].
///
/// These bytes become the Automerge actor the change commits under, so
/// an edit from outside attributes in history exactly like a typed one.
/// `None` is the app's own dispatch (the [`ActionId`]-derived actor).
/// Replay stays byte-identical either way, since the actor rides on the
/// envelope rather than the applying replica. Optional on the wire so an
/// envelope written before attribution existed still deserializes.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub actor: Option<Vec<u8>>,
/// Human provenance used by history, search, and passive activity UI.
/// It does not affect what the action is allowed to mutate; it is carried
/// into the Automerge change message so it survives the transport log.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub context: Option<ActionContext>,
}
impl ActionEnvelope {
/// The content the change's recorded action names
/// ([`crate::executor::recorded_form`]), by reference: the large parts
/// of an action larger than a change documents whole, read from the
/// action as it ran. A device that applies the action keeps them in its content
/// store, sealed as their writer sealed them, so receivers can resolve
/// them to replay. Empty for an action recorded whole.
///
/// # Errors
/// [`ActionError::Refused`] when the recorded action is not this one.
pub fn recorded_content(&self) -> Result<crate::content::ContentParts, crate::ActionError> {
let Some(recorded) = self
.context
.as_ref()
.and_then(|context| context.action.as_ref())
else {
return Ok(crate::content::ContentParts::new());
};
recorded.recorded_parts_of(&self.action).ok_or_else(|| {
crate::ActionError::Refused("the recorded action is not the action that ran".into())
})
}
/// The base heads as Automerge [`ChangeHash`]es.
pub fn base_change_hashes(&self) -> Vec<ChangeHash> {
self.base_heads.iter().map(|b| ChangeHash(*b)).collect()
}
/// The actor this action's change commits under: the authoring
/// member when named, else the [`ActionId`]-derived actor. Read off
/// the envelope so a replaying replica never substitutes its own.
pub fn actor(&self) -> ActorId {
match &self.actor {
Some(bytes) if !bytes.is_empty() => ActorId::from(bytes.clone()),
_ => ActorId::from(self.id.actor_bytes().to_vec()),
}
}
}