liminal-server 0.19.1

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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! Ordered merge of literal v2 base rows and schema-v1 Unit 2 extension rows.

use liminal_protocol::lifecycle::RecipientAckObligations;
#[cfg(test)]
use std::cell::Cell;
#[cfg(test)]
use std::collections::VecDeque;

use super::outbox::{ConversationOutbox, ConversationOutboxError, ConversationOutboxLimits};
use super::outbox_log::{
    OutboxLog, OutboxLogError, OutboxRestoreCursor, OutboxRow, UNIT2_OUTBOX_RESTORE_BATCH_ROWS,
};
use super::state::{ConversationAuthority, StateError};

/// Restore failure with cursor faults kept separate from semantic replay.
#[derive(Debug, thiserror::Error)]
pub(super) enum RestoreError {
    /// Extension cursor durability, stream, schema, or codec failure.
    #[error(transparent)]
    Extension(#[from] OutboxLogError),
    /// Base/extension merge, authority replay, repair, or finish failure.
    #[error(transparent)]
    Semantic(#[from] StateError),
}

/// How one replay pass holds the extension repair rows it owes.
///
/// Either way the merge never holds more than one page of
/// [`UNIT2_OUTBOX_RESTORE_BATCH_ROWS`] repair rows (DUR-008 D03, Hermes
/// 20:18): what a pass owes beyond one page is written, or not held at all.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum RepairPaging {
    /// The first pass, which has not yet audited the log: up to one page of
    /// repairs is staged and nothing is written until the whole log audits
    /// clean. Owing more than one page drops the staged rows and stops
    /// staging; the pass still applies every repair to the owner, finishes
    /// its audit, and then answers [`StagedRepairs::Overflowed`].
    StageOnePage,
    /// The second pass, run only after a first pass audited the same log
    /// clean: each page of repairs is written the moment it fills, and the
    /// last, partial page when the replay persists.
    WriteEachPage,
}

/// What [`ExtensionMerge::persist_staged_repairs`] did with the repairs the
/// pass owed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum StagedRepairs {
    /// Every repair the pass owed is durable.
    Persisted,
    /// The pass owed more than one page under [`RepairPaging::StageOnePage`]
    /// and wrote none of it: the replay must run again under
    /// [`RepairPaging::WriteEachPage`].
    Overflowed,
}

#[cfg(test)]
std::thread_local! {
    /// The most repair rows any merge on this thread held at once.
    static STAGED_REPAIR_HIGH_WATER: Cell<usize> = const { Cell::new(0) };
}

/// The most repair rows a merge on this thread has held at once since the
/// last call, which resets it.
#[cfg(test)]
pub(super) fn take_staged_repair_high_water() -> usize {
    STAGED_REPAIR_HIGH_WATER.with(|high_water| high_water.replace(0))
}

#[cfg(test)]
fn record_staged_repairs(rows: usize) {
    STAGED_REPAIR_HIGH_WATER.with(|high_water| high_water.set(high_water.get().max(rows)));
}

/// Move-only merge/reconciliation transaction for one cold first touch.
pub(super) struct ExtensionMerge<'a> {
    log: &'a OutboxLog,
    cursor: OutboxRestoreCursor<'a>,
    outbox: ConversationOutbox,
    repair_head: Option<u64>,
    /// Repair rows already applied to `outbox`, in extension order, and not
    /// yet durable: never more than one page. [`Self::persist_staged_repairs`]
    /// writes them, and under [`RepairPaging::WriteEachPage`] so does each
    /// page as it fills; nothing else does, so the replay decides WHEN a
    /// repair reaches the store.
    staged_repairs: Vec<(u64, OutboxRow)>,
    paging: RepairPaging,
    /// Set once [`RepairPaging::StageOnePage`] owed more than one page and
    /// dropped what it had staged.
    repairs_overflowed: bool,
    /// The first row [`Self::validate_rest`] found unconsumed, kept as its
    /// physical sequence and base boundary so [`Self::finish`] refuses it
    /// after the rest of the stream has been decoded past it.
    leftover: Option<(u64, Option<u64>)>,
}

impl<'a> ExtensionMerge<'a> {
    pub(super) fn new(
        log: &'a OutboxLog,
        conversation_id: u64,
        limits: ConversationOutboxLimits,
        paging: RepairPaging,
    ) -> Result<Self, StateError> {
        Ok(Self {
            log,
            cursor: log.restore_cursor(),
            outbox: ConversationOutbox::restore(conversation_id, Vec::new(), limits)?,
            repair_head: None,
            staged_repairs: Vec::new(),
            paging,
            repairs_overflowed: false,
            leftover: None,
        })
    }

    /// Decodes every extension row the merge has not consumed, through
    /// confirmed EOF, on the merge's own cursor.
    ///
    /// The stream is read once: the merge consumes it in order and this
    /// finishes it. Called before any staged repair is persisted and before any
    /// refusal is answered, it keeps the order a validate-first pass gave: an
    /// undecodable extension row anywhere is the refusal, ahead of every base
    /// or merge failure, and nothing is written over a stream that does not
    /// decode.
    pub(super) async fn validate_rest(&mut self) -> Result<(), OutboxLogError> {
        if self.leftover.is_none() {
            if let Some((physical_sequence, row)) = self.cursor.front().await? {
                self.leftover = Some((*physical_sequence, row.base_log_head()));
            }
        }
        self.cursor.drain().await
    }

    pub(super) fn recipient_ack_obligations(
        &self,
        participant_id: u64,
        acknowledged_through: u64,
    ) -> Result<(RecipientAckObligations, u64), StateError> {
        Ok(self
            .outbox
            .recipient_ack_obligations(participant_id, acknowledged_through)?)
    }

    /// Applies every physical extension row tied at `base_log_head`, then
    /// repairs the current base source only when the durable extension is an
    /// exact prefix with no later row already present.
    ///
    /// The repair row is applied to the owner here and STAGED, not written:
    /// the single replay pass reaches this point before it has audited the
    /// rest of the log, and a log refused later in that audit must leave the
    /// store exactly as it found it. Staging holds one page at most
    /// ([`Self::stage_repair`]).
    pub(super) async fn apply_boundary(
        &mut self,
        authority: &mut ConversationAuthority,
        base_log_head: u64,
        expected_projection: Option<&OutboxRow>,
    ) -> Result<(), RestoreError> {
        let mut projection_seen = false;
        loop {
            let Some((physical_sequence, row)) = self.cursor.front().await? else {
                break;
            };
            let boundary = row.base_log_head().ok_or_else(|| {
                RestoreError::Semantic(StateError::invariant(
                    "Unit 2 extension base boundary overflowed",
                ))
            })?;
            if boundary < base_log_head {
                return Err(RestoreError::Semantic(StateError::invariant(format!(
                    "Unit 2 extension boundary {boundary} is nonmonotone below {base_log_head}"
                ))));
            }
            if boundary > base_log_head {
                break;
            }
            let physical_sequence = *physical_sequence;
            let (_, row) = self.cursor.pop_front().ok_or_else(|| {
                RestoreError::Semantic(StateError::invariant(
                    "Unit 2 extension merge lost its physical head",
                ))
            })?;
            authority
                .begin_observer_progress_source()
                .map_err(RestoreError::Semantic)?;
            match &row {
                OutboxRow::Produced(_) | OutboxRow::AckAdvanced { .. } => {
                    if expected_projection != Some(&row) {
                        return Err(RestoreError::Semantic(StateError::invariant(format!(
                            "Unit 2 extension projection at physical sequence {physical_sequence} conflicts with v2 source"
                        ))));
                    }
                    projection_seen = true;
                }
                OutboxRow::MarkerAckCommitted(marker) => {
                    if marker.extension_sequence != physical_sequence {
                        return Err(RestoreError::Semantic(StateError::invariant(format!(
                            "MarkerAck extension sequence {} differs from physical {physical_sequence}",
                            marker.extension_sequence
                        ))));
                    }
                    authority
                        .replay_marker_ack_extension(marker)
                        .map_err(RestoreError::Semantic)?;
                }
            }
            self.outbox
                .apply_row(physical_sequence, row)
                .map_err(StateError::from)
                .map_err(RestoreError::Semantic)?;
            authority
                .end_observer_progress_source()
                .map_err(RestoreError::Semantic)?;
        }

        if let Some(expected) = expected_projection {
            if projection_seen {
                return Ok(());
            }
            if self.cursor.confirmed_head().is_none() {
                return Err(RestoreError::Semantic(StateError::invariant(
                    "Unit 2 extension is missing a projection before a later physical row",
                )));
            }
            let next_extension_sequence =
                *self
                    .repair_head
                    .get_or_insert(self.cursor.confirmed_head().ok_or_else(|| {
                        RestoreError::Semantic(StateError::invariant(
                            "Unit 2 extension repair attempted before confirmed EOF",
                        ))
                    })?);
            self.stage_repair(next_extension_sequence, expected.clone())
                .await?;
            authority.begin_observer_progress_source()?;
            self.outbox
                .apply_row(next_extension_sequence, expected.clone())
                .map_err(StateError::from)
                .map_err(RestoreError::Semantic)?;
            authority.end_observer_progress_source()?;
            self.repair_head = Some(next_extension_sequence.checked_add(1).ok_or_else(|| {
                RestoreError::Semantic(StateError::invariant("Unit 2 extension head overflowed"))
            })?);
        }
        Ok(())
    }

    /// Holds one owed repair row, never more than one page of them.
    ///
    /// Under [`RepairPaging::StageOnePage`] a row past the first page drops
    /// the page already staged and stops staging: a pass that owes more than
    /// one page writes nothing and is run again. Under
    /// [`RepairPaging::WriteEachPage`] a page is written the moment it fills.
    async fn stage_repair(
        &mut self,
        extension_sequence: u64,
        row: OutboxRow,
    ) -> Result<(), RestoreError> {
        match self.paging {
            RepairPaging::StageOnePage => {
                if self.repairs_overflowed {
                    return Ok(());
                }
                if self.staged_repairs.len() >= UNIT2_OUTBOX_RESTORE_BATCH_ROWS {
                    self.staged_repairs = Vec::new();
                    self.repairs_overflowed = true;
                    return Ok(());
                }
                self.staged_repairs.push((extension_sequence, row));
                #[cfg(test)]
                record_staged_repairs(self.staged_repairs.len());
            }
            RepairPaging::WriteEachPage => {
                self.staged_repairs.push((extension_sequence, row));
                #[cfg(test)]
                record_staged_repairs(self.staged_repairs.len());
                if self.staged_repairs.len() >= UNIT2_OUTBOX_RESTORE_BATCH_ROWS {
                    self.write_staged_page().await?;
                }
            }
        }
        Ok(())
    }

    /// Makes every staged repair row durable, in extension order, at the
    /// sequence it was applied under, as one page.
    ///
    /// The replay calls this once it knows the former validate-first pass
    /// would have reached the same repairs: after the whole log has audited
    /// clean. The rows, their order and their sequences are exactly the ones
    /// the in-line repair appended, so the durable result is byte-identical;
    /// only the moment moves, to after the audit. Repairs are contiguous from
    /// the extension's confirmed head, so the page of at most
    /// [`UNIT2_OUTBOX_RESTORE_BATCH_ROWS`] rows is one `append_all` and one
    /// flush (DUR-008 A3, D03), never one row and one flush at a time.
    ///
    /// A first pass that owed more than one page persists nothing and
    /// answers [`StagedRepairs::Overflowed`]; the replay then runs its second
    /// pass, which writes every page.
    pub(super) async fn persist_staged_repairs(&mut self) -> Result<StagedRepairs, RestoreError> {
        if self.repairs_overflowed {
            return Ok(StagedRepairs::Overflowed);
        }
        self.write_staged_page().await?;
        Ok(StagedRepairs::Persisted)
    }

    /// Writes the staged rows, at most one page, as one `append_all` and one
    /// flush, and empties the stage.
    async fn write_staged_page(&mut self) -> Result<(), RestoreError> {
        let staged = std::mem::take(&mut self.staged_repairs);
        let Some((first_sequence, _)) = staged.first() else {
            return Ok(());
        };
        let first_sequence = *first_sequence;
        let mut expected = first_sequence;
        let mut rows = Vec::with_capacity(staged.len());
        for (extension_sequence, row) in staged {
            if extension_sequence != expected {
                return Err(RestoreError::Semantic(StateError::invariant(format!(
                    "Unit 2 extension repair at sequence {extension_sequence} does not follow \
                     the repair at {}",
                    expected.saturating_sub(1)
                ))));
            }
            rows.push(row);
            expected = expected.checked_add(1).ok_or_else(|| {
                RestoreError::Semantic(StateError::invariant("Unit 2 extension head overflowed"))
            })?;
        }
        self.log
            .append_page(&rows, first_sequence)
            .await
            .map_err(ConversationOutboxError::from)
            .map_err(StateError::from)
            .map_err(RestoreError::Semantic)?;
        Ok(())
    }

    /// Refuses impossible future boundaries and installs the fully validated
    /// move-only owner only after the complete merge has succeeded.
    pub(super) fn finish(
        mut self,
        authority: &mut ConversationAuthority,
        final_base_log_head: u64,
    ) -> Result<(), StateError> {
        // An owner whose repairs never reached the store would answer from
        // rows a restart cannot see. Refused, by name, rather than installed.
        if let Some((extension_sequence, _)) = self.staged_repairs.first() {
            return Err(StateError::invariant(format!(
                "Unit 2 extension repair at sequence {extension_sequence} was staged but never \
                 persisted"
            )));
        }
        if self.repairs_overflowed {
            return Err(StateError::invariant(
                "Unit 2 extension repairs overflowed one staged page and were never persisted",
            ));
        }
        let leftover = self.leftover.or_else(|| {
            self.cursor
                .pop_front()
                .map(|(physical_sequence, row)| (physical_sequence, row.base_log_head()))
        });
        if let Some((physical_sequence, boundary)) = leftover {
            let boundary = boundary.ok_or_else(|| {
                StateError::invariant("Unit 2 extension base boundary overflowed")
            })?;
            return Err(StateError::invariant(format!(
                "Unit 2 extension physical sequence {physical_sequence} has impossible future boundary {boundary} above base head {final_base_log_head}"
            )));
        }
        if self.cursor.confirmed_head().is_none() {
            return Err(StateError::invariant(
                "Unit 2 extension merge finished before confirmed EOF",
            ));
        }
        authority.outbox = Some(self.outbox);
        Ok(())
    }
}

/// Frozen pre-W3 aggregate merge used only for byte/error equivalence oracles.
///
/// Its pending-history ownership and repair placement are deliberately
/// independent of [`ExtensionMerge`]; production cannot select this path.
#[cfg(test)]
pub(super) struct AggregateExtensionMerge<'a> {
    log: &'a OutboxLog,
    pending: VecDeque<(u64, OutboxRow)>,
    outbox: ConversationOutbox,
    next_extension_sequence: u64,
}

#[cfg(test)]
impl<'a> AggregateExtensionMerge<'a> {
    pub(super) fn new(
        log: &'a OutboxLog,
        rows: Vec<(u64, OutboxRow)>,
        conversation_id: u64,
        limits: ConversationOutboxLimits,
    ) -> Result<Self, StateError> {
        let next_extension_sequence = u64::try_from(rows.len())
            .map_err(|_| StateError::invariant("Unit 2 extension stream length exceeds u64"))?;
        Ok(Self {
            log,
            pending: rows.into(),
            outbox: ConversationOutbox::restore(conversation_id, Vec::new(), limits)?,
            next_extension_sequence,
        })
    }

    pub(super) fn recipient_ack_obligations(
        &self,
        participant_id: u64,
        acknowledged_through: u64,
    ) -> Result<(RecipientAckObligations, u64), StateError> {
        Ok(self
            .outbox
            .recipient_ack_obligations(participant_id, acknowledged_through)?)
    }

    pub(super) async fn apply_boundary(
        &mut self,
        authority: &mut ConversationAuthority,
        base_log_head: u64,
        expected_projection: Option<&OutboxRow>,
    ) -> Result<(), StateError> {
        let mut projection_seen = false;
        while let Some((physical_sequence, row)) = self.pending.front() {
            let boundary = row.base_log_head().ok_or_else(|| {
                StateError::invariant("Unit 2 extension base boundary overflowed")
            })?;
            if boundary < base_log_head {
                return Err(StateError::invariant(format!(
                    "Unit 2 extension boundary {boundary} is nonmonotone below {base_log_head}"
                )));
            }
            if boundary > base_log_head {
                break;
            }
            let physical_sequence = *physical_sequence;
            let (_, row) = self.pending.pop_front().ok_or_else(|| {
                StateError::invariant("Unit 2 extension merge lost its physical head")
            })?;
            authority.begin_observer_progress_source()?;
            match &row {
                OutboxRow::Produced(_) | OutboxRow::AckAdvanced { .. } => {
                    if expected_projection != Some(&row) {
                        return Err(StateError::invariant(format!(
                            "Unit 2 extension projection at physical sequence {physical_sequence} conflicts with v2 source"
                        )));
                    }
                    projection_seen = true;
                }
                OutboxRow::MarkerAckCommitted(marker) => {
                    if marker.extension_sequence != physical_sequence {
                        return Err(StateError::invariant(format!(
                            "MarkerAck extension sequence {} differs from physical {physical_sequence}",
                            marker.extension_sequence
                        )));
                    }
                    authority.replay_marker_ack_extension(marker)?;
                }
            }
            self.outbox.apply_row(physical_sequence, row)?;
            authority.end_observer_progress_source()?;
        }

        if let Some(expected) = expected_projection {
            if projection_seen {
                return Ok(());
            }
            if !self.pending.is_empty() {
                return Err(StateError::invariant(
                    "Unit 2 extension is missing a projection before a later physical row",
                ));
            }
            self.log
                .append(expected, self.next_extension_sequence)
                .await
                .map_err(ConversationOutboxError::from)?;
            authority.begin_observer_progress_source()?;
            self.outbox
                .apply_row(self.next_extension_sequence, expected.clone())?;
            authority.end_observer_progress_source()?;
            self.next_extension_sequence = self
                .next_extension_sequence
                .checked_add(1)
                .ok_or_else(|| StateError::invariant("Unit 2 extension head overflowed"))?;
        }
        Ok(())
    }

    pub(super) fn finish(
        self,
        authority: &mut ConversationAuthority,
        final_base_log_head: u64,
    ) -> Result<(), StateError> {
        if let Some((physical_sequence, row)) = self.pending.front() {
            let boundary = row.base_log_head().ok_or_else(|| {
                StateError::invariant("Unit 2 extension base boundary overflowed")
            })?;
            return Err(StateError::invariant(format!(
                "Unit 2 extension physical sequence {physical_sequence} has impossible future boundary {boundary} above base head {final_base_log_head}"
            )));
        }
        authority.outbox = Some(self.outbox);
        Ok(())
    }
}