liminal-server 0.3.0

Standalone server for the liminal messaging bus
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
//! Observer-recovery arms of the production handler (split from
//! [`super::handler`] under the 500-code-line lens).
//!
//! The A4 atomic transaction, idempotent Track registration, and the
//! crash-window repair pre-pass all operate on the handler's server-wide
//! observer aggregate and its durable row log.

use std::collections::BTreeMap;
use std::sync::Arc;

use liminal::durability::bridge::block_on;
use liminal_protocol::lifecycle::{
    ObserverProgressAdvanceDecision, ObserverProgressProjection, ObserverProgressTrackDecision,
    ObserverRecoveryArm, ObserverRecoveryTransactionDecision,
};
use liminal_protocol::wire::{
    ConversationId, ObserverRecoveryHandshake, ObserverRecoveryResponse, ServerValue,
};

use crate::server::participant::{
    ObserverPublication, ObserverPublicationTarget, ParticipantConnectionContext,
    ParticipantConnectionConversations, ParticipantSemanticError,
};

use super::handler::{
    ObserverArmTarget, ObserverOwner, ProductionParticipantHandler, bridge_error, log_error,
    state_error,
};
use super::log::OperationLog;
use super::observer::{ObserverLog, ObserverRow};
use super::state::StateError;

impl ProductionParticipantHandler {
    /// Applies one observer-recovery batch through the A4 atomic transaction.
    ///
    /// The aggregate is owned by the transaction between the progress read
    /// and the arm installation, and the whole arm plan is durably appended
    /// before installation — a crash leaves the complete plan or none.
    pub(super) fn apply_observer_recovery(
        &self,
        context: ParticipantConnectionContext,
        conversations: &mut ParticipantConnectionConversations,
        request: &ObserverRecoveryHandshake,
        target: Option<&ObserverPublicationTarget>,
    ) -> Result<ServerValue, ParticipantSemanticError> {
        // Crash-window repair pre-pass: every named conversation's observer
        // registration is made derivable from its own durable conversation
        // log before the batch classifies, so an enrollment whose Track row
        // was lost between the two appends is re-registered idempotently
        // instead of being refused forever.
        for refusal in &request.observer_refusals {
            self.ensure_tracking_from_log(refusal.conversation_id)?;
        }
        // Connection-conversation occupancy is the SAME connection-local
        // dispatch map the semantic stage-6 gate consumes, so the batch
        // preflight and every semantic operation count against one signed
        // bound.
        let tracked = conversations.tracked_conversations();
        let observer_log = ObserverLog::new(Arc::clone(&self.store));
        let mut owner = self
            .observer
            .lock()
            .map_err(|_| ParticipantSemanticError::Internal {
                message: "observer recovery aggregate lock is poisoned".to_owned(),
            })?;
        if owner.is_none() {
            let restored = block_on(observer_log.restore())
                .map_err(|error| bridge_error(&error))?
                .map_err(|error| log_error(&error))?;
            *owner = Some(ObserverOwner {
                aggregate: restored.aggregate,
                head: restored.next_sequence,
                arm_targets: BTreeMap::new(),
            });
        }
        let ObserverOwner {
            aggregate,
            head,
            mut arm_targets,
        } = owner
            .take()
            .ok_or_else(|| ParticipantSemanticError::Internal {
                message: "observer recovery aggregate is absent".to_owned(),
            })?;
        let result = match aggregate.decide_recovery(
            request,
            self.config.observer_recovery_max_entries,
            self.config.max_semantic_conversations_per_connection,
            &tracked,
        ) {
            ObserverRecoveryTransactionDecision::Respond {
                aggregate,
                response,
            } => {
                *owner = Some(ObserverOwner {
                    aggregate,
                    head,
                    arm_targets,
                });
                Ok(response.into_server_value())
            }
            ObserverRecoveryTransactionDecision::Commit(transaction) => {
                let arms = transaction
                    .arms()
                    .iter()
                    .map(|arm| (arm.conversation_id(), arm.refused_epoch()))
                    .collect::<Vec<_>>();
                match block_on(observer_log.append(&ObserverRow::Arms { arms: arms.clone() }, head))
                    .map_err(|error| bridge_error(&error))?
                {
                    Ok(()) => {
                        let (aggregate, outcome) = transaction.commit();
                        let next_head = head.checked_add(1).ok_or_else(|| {
                            ParticipantSemanticError::Internal {
                                message: "observer durable row sequence exhausted".to_owned(),
                            }
                        })?;
                        // Every armed refusal-only recipient occupies one
                        // connection-conversation slot (the batch preflight
                        // already admitted them against the signed bound).
                        for (conversation_id, refused_epoch) in &arms {
                            conversations.track(*conversation_id);
                            if let Some(target) = target.as_ref() {
                                arm_targets.insert(
                                    *conversation_id,
                                    ObserverArmTarget {
                                        refused_epoch: *refused_epoch,
                                        connection_incarnation: context.connection_incarnation(),
                                        target: (*target).clone(),
                                    },
                                );
                            } else {
                                arm_targets.remove(conversation_id);
                            }
                        }
                        *owner = Some(ObserverOwner {
                            aggregate,
                            head: next_head,
                            arm_targets,
                        });
                        Ok(ObserverRecoveryResponse::accepted(outcome).into_server_value())
                    }
                    Err(error) => {
                        // Nothing installed; the aggregate is durably restored
                        // on the next observer operation.
                        Err(state_error(&StateError::Log(error)))
                    }
                }
            }
        };
        drop(owner);
        result
    }

    /// Registers a conversation's observer progress row on first touch.
    ///
    /// Idempotent: an already-tracked conversation returns unchanged.
    pub(super) fn ensure_observer_tracked(
        &self,
        conversation_id: ConversationId,
    ) -> Result<(), ParticipantSemanticError> {
        let observer_log = ObserverLog::new(Arc::clone(&self.store));
        let mut owner = self
            .observer
            .lock()
            .map_err(|_| ParticipantSemanticError::Internal {
                message: "observer recovery aggregate lock is poisoned".to_owned(),
            })?;
        if owner.is_none() {
            let restored = block_on(observer_log.restore())
                .map_err(|error| bridge_error(&error))?
                .map_err(|error| log_error(&error))?;
            *owner = Some(ObserverOwner {
                aggregate: restored.aggregate,
                head: restored.next_sequence,
                arm_targets: BTreeMap::new(),
            });
        }
        let ObserverOwner {
            aggregate,
            head,
            arm_targets,
        } = owner
            .take()
            .ok_or_else(|| ParticipantSemanticError::Internal {
                message: "observer recovery aggregate is absent".to_owned(),
            })?;
        let result = match aggregate.decide_track(conversation_id, 0) {
            ObserverProgressTrackDecision::Refuse { aggregate, .. } => {
                // Already tracked — the registration is durable.
                *owner = Some(ObserverOwner {
                    aggregate,
                    head,
                    arm_targets,
                });
                Ok(())
            }
            ObserverProgressTrackDecision::Commit(transaction) => {
                match block_on(observer_log.append(
                    &ObserverRow::Track {
                        conversation_id,
                        observer_progress: 0,
                    },
                    head,
                ))
                .map_err(|error| bridge_error(&error))?
                {
                    Ok(()) => {
                        *owner = Some(ObserverOwner {
                            aggregate: transaction.commit(),
                            head: head.saturating_add(1),
                            arm_targets,
                        });
                        Ok(())
                    }
                    Err(error) => Err(state_error(&StateError::Log(error))),
                }
            }
        };
        drop(owner);
        result
    }

    /// Reconciles protocol-produced source projections into durable observer
    /// advances while the observer aggregate remains exclusively serialized.
    ///
    /// The projections arrive in participant replay order after their source
    /// append/flush barriers. An equal-or-greater durable value proves an
    /// earlier live append already closed the crash window; a lower value is
    /// advanced through the aggregate's consuming transaction and only
    /// committed after the `Advance` row flushes.
    pub(super) fn reconcile_observer_progress(
        &self,
        conversation_id: ConversationId,
        projections: Vec<ObserverProgressProjection>,
    ) -> Result<(), ParticipantSemanticError> {
        if projections.is_empty() {
            return Ok(());
        }
        let observer_log = ObserverLog::new(Arc::clone(&self.store));
        let mut owner = self
            .observer
            .lock()
            .map_err(|_| ParticipantSemanticError::Internal {
                message: "observer recovery aggregate lock is poisoned".to_owned(),
            })?;
        if owner.is_none() {
            let restored = block_on(observer_log.restore())
                .map_err(|error| bridge_error(&error))?
                .map_err(|error| log_error(&error))?;
            *owner = Some(ObserverOwner {
                aggregate: restored.aggregate,
                head: restored.next_sequence,
                arm_targets: BTreeMap::new(),
            });
        }
        for projection in projections {
            if projection.conversation_id() != conversation_id {
                return Err(ParticipantSemanticError::Internal {
                    message: format!(
                        "observer projection conversation {} disagrees with source {conversation_id}",
                        projection.conversation_id()
                    ),
                });
            }
            let ObserverOwner {
                aggregate,
                head,
                mut arm_targets,
            } = owner
                .take()
                .ok_or_else(|| ParticipantSemanticError::Internal {
                    message: "observer recovery aggregate is absent".to_owned(),
                })?;
            let presented = projection.new_observer_progress();
            let current = aggregate
                .observer_progress(conversation_id)
                .ok_or_else(|| ParticipantSemanticError::Internal {
                    message: format!(
                        "observer projection names untracked conversation {conversation_id}"
                    ),
                })?;
            if current >= presented {
                *owner = Some(ObserverOwner {
                    aggregate,
                    head,
                    arm_targets,
                });
                continue;
            }
            let ObserverProgressAdvanceDecision::Commit(transaction) =
                aggregate.decide_progress_advance(conversation_id, presented)
            else {
                return Err(ParticipantSemanticError::Internal {
                    message: format!(
                        "observer aggregate refused advancing source for conversation {conversation_id}"
                    ),
                });
            };
            let append = block_on(observer_log.append(
                &ObserverRow::Advance {
                    conversation_id,
                    observer_progress: presented,
                },
                head,
            ))
            .map_err(|error| bridge_error(&error))?;
            match append {
                Ok(()) => {
                    let next_head =
                        head.checked_add(1)
                            .ok_or_else(|| ParticipantSemanticError::Internal {
                                message: "observer durable row sequence exhausted".to_owned(),
                            })?;
                    let (aggregate, fired) = transaction.commit();
                    let publication_result =
                        publish_fired_observer(&mut arm_targets, fired, presented, conversation_id);
                    *owner = Some(ObserverOwner {
                        aggregate,
                        head: next_head,
                        arm_targets,
                    });
                    publication_result?;
                }
                Err(error) => {
                    *owner = Some(ObserverOwner {
                        aggregate: transaction.abort(),
                        head,
                        arm_targets,
                    });
                    return Err(state_error(&StateError::Log(error)));
                }
            }
        }
        drop(owner);
        Ok(())
    }

    /// Ensures a conversation's observer registration is derivable from its
    /// own durable log (idempotent crash-window repair).
    ///
    /// A never-committed conversation id leaves no residue: its probe cell is
    /// evicted. An already-live enrolled owner re-registers idempotently,
    /// covering an earlier failed Track append inside this process lifetime.
    pub(super) fn ensure_tracking_from_log(
        &self,
        conversation_id: ConversationId,
    ) -> Result<(), ParticipantSemanticError> {
        let cell = self.cell(conversation_id)?;
        let mut owner = cell
            .lock()
            .map_err(|_| ParticipantSemanticError::Internal {
                message: format!(
                    "participant conversation {conversation_id} owner lock is poisoned"
                ),
            })?;
        if owner.is_none() {
            let log = OperationLog::new(Arc::clone(&self.store), conversation_id);
            let replayed = self.replay_and_repair(conversation_id, &log)?;
            if replayed.next_log_sequence == 0 {
                drop(owner);
                return self.evict_uncommitted(conversation_id, &cell);
            }
            *owner = Some(replayed);
            drop(owner);
            return Ok(());
        }
        let enrolled = owner
            .as_ref()
            .is_some_and(|authority| !authority.tokens.is_empty());
        drop(owner);
        if enrolled {
            self.ensure_observer_tracked(conversation_id)?;
        }
        Ok(())
    }
}

fn publish_fired_observer(
    arm_targets: &mut BTreeMap<ConversationId, ObserverArmTarget>,
    fired: Option<ObserverRecoveryArm>,
    observer_progress: u64,
    conversation_id: ConversationId,
) -> Result<(), ParticipantSemanticError> {
    fired.map_or(Ok(()), |fired| {
        match arm_targets.remove(&fired.conversation_id()) {
            Some(association) if association.refused_epoch == fired.refused_epoch() => association
                .target
                .publish(ObserverPublication {
                    conversation_id: fired.conversation_id(),
                    refused_epoch: fired.refused_epoch(),
                    observer_progress,
                })
                .map(|_| ())
                .map_err(|error| ParticipantSemanticError::Internal {
                    message: format!("observer progressed publication failed: {error}"),
                }),
            Some(association) => Err(ParticipantSemanticError::Internal {
                message: format!(
                    "observer arm target epoch {} on incarnation {:?} disagrees with fired epoch {} for conversation {conversation_id}",
                    association.refused_epoch,
                    association.connection_incarnation,
                    fired.refused_epoch()
                ),
            }),
            None => Ok(()),
        }
    })
}