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
//! Durable owner for one opaque participant-conversation aggregate.
//!
//! The server stores only canonical protocol events. It never serializes the
//! private lifecycle state: cold load starts from protocol genesis and consumes
//! each decoded event through protocol replay. A live transition remains inside
//! [`ConversationCommit`] until its exact bytes append successfully.
use std::sync::Arc;
use liminal::durability::DurableStore;
use liminal_protocol::lifecycle::{
ConversationDecision, ConversationEvent, ConversationEventDecodeError, ConversationGenesis,
ConversationRefusalReason, ConversationReplayError, ParticipantConversation,
};
use super::conversation_stream::{ConversationEventStream, ConversationStreamError};
/// Failure while cold-loading or initializing one participant conversation.
#[derive(Debug, thiserror::Error)]
pub(super) enum ConversationAggregateError {
/// The append-only conversation stream rejected a read or append.
#[error(transparent)]
Stream(#[from] ConversationStreamError),
/// Stored event bytes are not one canonical protocol event.
#[error("conversation event {stored_sequence} failed canonical decode: {reason:?}")]
EventDecode {
/// Durable stream position carrying the malformed bytes.
stored_sequence: u64,
/// Stable protocol codec failure.
reason: ConversationEventDecodeError,
},
/// The event's canonical ordinal differs from its durable stream position.
#[error(
"conversation event ordinal mismatch: stored at {stored_sequence}, event names {event_ordinal}"
)]
StoredEventOrdinal {
/// Durable stream position.
stored_sequence: u64,
/// Ordinal decoded from the canonical event.
event_ordinal: u64,
},
/// Protocol replay rejected the structurally valid durable event.
#[error("protocol rejected conversation event {stored_sequence}: {reason:?}")]
Replay {
/// Durable stream position of the rejected event.
stored_sequence: u64,
/// Exact protocol replay failure.
reason: ConversationReplayError,
},
/// A prepared protocol event did not own the current durable stream head.
#[error(
"prepared conversation event ordinal mismatch: stream head {stream_head}, event ordinal {event_ordinal}"
)]
PreparedEventOrdinal {
/// Exact optimistic stream head.
stream_head: u64,
/// Protocol-emitted event ordinal.
event_ordinal: u64,
},
/// An uninitialized aggregate returned a non-commit protocol decision.
#[error("protocol refused required genesis validation: {reason:?}")]
GenesisRefused {
/// Exact protocol refusal.
reason: ConversationRefusalReason,
},
/// Append failed and the mandatory immediate cold reload also failed.
#[error("genesis append failed ({append}); cold reload then failed ({reload})")]
ReloadAfterAppendFailure {
/// Original append failure.
append: ConversationStreamError,
/// Failure reconstructing durable reality after the ambiguous append.
reload: Box<Self>,
},
}
/// Result of opening one participant conversation stream.
#[derive(Debug)]
pub(super) enum ConversationAggregateOpen {
/// Cold replay and any required genesis append completed durably.
Ready(ParticipantConversationAggregate),
/// Genesis append failed; the speculative commit was aborted and durable
/// reality was cold-reloaded without retrying the transition.
AppendFailed(ConversationAggregateAppendFailure),
}
/// Failed genesis append paired with its freshly reloaded aggregate.
#[derive(Debug)]
pub(super) struct ConversationAggregateAppendFailure {
error: ConversationStreamError,
reloaded: ParticipantConversationAggregate,
}
impl ConversationAggregateAppendFailure {
/// Returns the original non-retried stream append failure.
#[must_use]
pub(super) const fn error(&self) -> &ConversationStreamError {
&self.error
}
/// Borrows the aggregate reconstructed from durable storage after failure.
#[must_use]
#[cfg(test)]
pub(super) const fn reloaded(&self) -> &ParticipantConversationAggregate {
&self.reloaded
}
/// Consumes the failure into the reconstructed durable aggregate.
#[must_use]
#[cfg(test)]
pub(super) fn into_reloaded(self) -> ParticipantConversationAggregate {
self.reloaded
}
/// Returns the original append error while destroying the pre-barrier reload.
///
/// A read after failed flush does not prove durability: it can expose buffered
/// bytes, or omit bytes which a later recovery flush persists. Production
/// therefore cannot extract that replay as executable aggregate authority.
#[must_use]
pub(super) fn into_error_discarding_reload(self) -> ConversationStreamError {
let Self { error, reloaded } = self;
drop(reloaded);
error
}
}
/// Sole live owner of one stream head and non-cloneable protocol aggregate.
#[derive(Debug)]
pub(super) struct ParticipantConversationAggregate {
stream: ConversationEventStream,
stream_head: u64,
conversation: ParticipantConversation,
}
impl ParticipantConversationAggregate {
/// Cold-loads one conversation and durably appends protocol genesis only
/// when replay proves the stream is new.
///
/// Existing streams reach the protocol's one-shot refusal and append
/// nothing. A new stream appends the exact canonical event before consuming
/// its [`ConversationCommit`](liminal_protocol::lifecycle::ConversationCommit).
/// If append fails, the commit is aborted and the stream is immediately
/// replayed once from ordinal zero; no timer, backoff, or append retry exists.
///
/// # Errors
///
/// Returns [`ConversationAggregateError`] for malformed or inconsistent
/// durable history, a failed read, or a failed post-append cold reload.
pub(super) async fn open(
store: Arc<dyn DurableStore>,
conversation_id: u64,
) -> Result<ConversationAggregateOpen, ConversationAggregateError> {
let stream = ConversationEventStream::new(store, conversation_id);
let conversation =
ParticipantConversation::from_genesis(ConversationGenesis::new(conversation_id));
let aggregate = replay_from_start(stream, conversation).await?;
aggregate.initialize_genesis().await
}
/// Returns the stable owning conversation id.
#[must_use]
pub(super) const fn conversation_id(&self) -> u64 {
self.conversation.conversation_id()
}
/// Returns the exact optimistic durable stream head.
#[must_use]
pub(super) const fn stream_head(&self) -> u64 {
self.stream_head
}
/// Returns the protocol-owned next event ordinal.
#[must_use]
pub(super) const fn next_event_ordinal(&self) -> u64 {
self.conversation.next_event_ordinal()
}
/// Reports whether protocol genesis validation is durable.
#[must_use]
pub(super) const fn genesis_validated(&self) -> bool {
self.conversation.genesis_validated()
}
/// Resumes genesis initialization from an aggregate already reconstructed
/// after an ambiguous append.
///
/// The registry invokes this only on a later explicit operation when the
/// reloaded stream was still empty. It is not an append retry inside the
/// failed operation and introduces no timer or polling loop.
pub(super) async fn resume_open(
self,
) -> Result<ConversationAggregateOpen, ConversationAggregateError> {
self.initialize_genesis().await
}
async fn initialize_genesis(
self,
) -> Result<ConversationAggregateOpen, ConversationAggregateError> {
let Self {
stream,
stream_head,
conversation,
} = self;
match conversation.decide_genesis_validation() {
ConversationDecision::Refused(refusal) => {
let reason = refusal.reason();
let conversation = refusal.into_conversation();
if reason == ConversationRefusalReason::GenesisAlreadyValidated {
Ok(ConversationAggregateOpen::Ready(Self {
stream,
stream_head,
conversation,
}))
} else {
Err(ConversationAggregateError::GenesisRefused { reason })
}
}
ConversationDecision::Commit(commit) => {
let event_ordinal = commit.event().ordinal();
if event_ordinal != stream_head {
let _unchanged = commit.abort();
return Err(ConversationAggregateError::PreparedEventOrdinal {
stream_head,
event_ordinal,
});
}
let payload = commit.event().encode_canonical();
match stream.append(stream_head, payload).await {
Ok(next_stream_head) => {
let conversation = commit.commit();
Ok(ConversationAggregateOpen::Ready(Self {
stream,
stream_head: next_stream_head,
conversation,
}))
}
Err(append) => {
let unchanged = commit.abort();
let conversation_id = unchanged.conversation_id();
let cold = ParticipantConversation::from_genesis(ConversationGenesis::new(
conversation_id,
));
match replay_from_start(stream, cold).await {
Ok(reloaded) => Ok(ConversationAggregateOpen::AppendFailed(
ConversationAggregateAppendFailure {
error: append,
reloaded,
},
)),
Err(reload) => {
Err(ConversationAggregateError::ReloadAfterAppendFailure {
append,
reload: Box::new(reload),
})
}
}
}
}
}
}
}
}
async fn replay_from_start(
stream: ConversationEventStream,
mut conversation: ParticipantConversation,
) -> Result<ParticipantConversationAggregate, ConversationAggregateError> {
let mut stream_head = 0_u64;
loop {
let page = stream.read_page(stream_head).await?;
if page.is_empty() {
return Ok(ParticipantConversationAggregate {
stream,
stream_head,
conversation,
});
}
let next_stream_head = page.next_sequence();
for entry in page.into_entries() {
let event = ConversationEvent::decode_canonical(&entry.payload).map_err(|reason| {
ConversationAggregateError::EventDecode {
stored_sequence: entry.sequence,
reason,
}
})?;
if event.ordinal() != entry.sequence {
return Err(ConversationAggregateError::StoredEventOrdinal {
stored_sequence: entry.sequence,
event_ordinal: event.ordinal(),
});
}
conversation = match conversation.replay(event) {
Ok(resulting) => resulting,
Err(failure) => {
return Err(ConversationAggregateError::Replay {
stored_sequence: entry.sequence,
reason: failure.reason(),
});
}
};
}
stream_head = next_stream_head;
}
}