liminal-protocol 0.2.0

Shared participant-lifecycle protocol types for liminal
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
#![allow(clippy::expect_used)]

use alloc::{vec, vec::Vec};

use crate::{
    algebra::{ResourceVector, WideResourceVector},
    outcome::CandidatePhase,
    wire::{
        BindingEpoch, ConnectionIncarnation, Generation, ParticipantDelivery, ParticipantRecord,
    },
};

use super::super::{
    AdmissionOrder, BindingTerminalOwner, ClaimFrontiers, ClaimFrontiersRestore, ClosureAccounting,
    ClosureDebt, ClosureState, FrontierBinding, FrontierParticipant,
    ImmutableOrderCandidateMajorRestore, ImmutableSequenceCandidate, MarkerCandidateAuthority,
    MarkerProvenance, MarkerSequenceOwner, MovableOrderClaim, MovableSequenceClaim,
    ObserverProjection, OrderClaimFrontierRestore, OrderClaims, OrderDirectOwner, OrderHigh,
    OrderLedger, PhysicalCompaction, RecoverySequenceReserve, RetainedCausalRecord,
    RetainedCausalRecordKind, SequenceClaimFrontierRestore, SequenceClaims, SequenceDirectOwner,
    SequenceLedger, SequenceProductRangesRestore, StoredEdge, TerminalProductRangeRestore,
    storage::{MarkerDeliveryRestore, StorageRestoreError},
};
use super::{
    MarkerDrainCommit, MarkerDrainError, RetainedRecordCharge,
    drain_next_marker as drain_next_marker_owned,
};

#[path = "marker_drain_acceptance_tests.rs"]
mod marker_drain_acceptance_tests;
fn drain_next_marker(
    frontiers: ClaimFrontiers,
    closure: ClosureState,
) -> Result<MarkerDrainCommit, MarkerDrainError> {
    let retained_charges = frontiers
        .retained_records()
        .iter()
        .map(|record| {
            RetainedRecordCharge::new(
                record.delivery_seq,
                record.admission_order,
                ResourceVector::new(1, 1),
            )
        })
        .collect();
    let marker_charge = frontiers
        .sequence()
        .immutable_candidates()
        .first()
        .map_or_else(
            || RetainedRecordCharge::new(0, marker_key(), ResourceVector::new(1, 1)),
            |candidate| {
                RetainedRecordCharge::new(
                    candidate.delivery_seq(),
                    candidate.admission_order(),
                    ResourceVector::new(1, 1),
                )
            },
        );
    let accounting = ClosureAccounting::try_new(
        closure,
        1,
        1,
        0,
        0,
        ResourceVector::default(),
        WideResourceVector::new(1, 1),
        ResourceVector::new(100, 100),
        0,
        2,
    )
    .expect("marker drain accounting fixture is valid");
    drain_next_marker_owned(frontiers, accounting, retained_charges, marker_charge)
}

const PARTICIPANT_ID: u64 = 0;
const CONVERSATION_ID: u64 = 91;

fn epoch() -> BindingEpoch {
    BindingEpoch::new(ConnectionIncarnation::new(91, 1), Generation::ONE)
}

fn marker_key() -> AdmissionOrder {
    AdmissionOrder::new(0, CandidatePhase::CompactionMarker, PARTICIPANT_ID)
}

fn marker_candidates(
    with_marker: bool,
    target_binding: FrontierBinding,
) -> (
    Vec<ImmutableSequenceCandidate>,
    Vec<ImmutableOrderCandidateMajorRestore>,
) {
    if with_marker {
        (
            vec![ImmutableSequenceCandidate::Marker(
                MarkerCandidateAuthority {
                    delivery_seq: 2,
                    admission_order: marker_key(),
                    target_binding,
                    provenance: MarkerProvenance::NonProductM,
                    abandoned_after: 0,
                    abandoned_through: 1,
                    physical_floor_at_decision: 1,
                    current_owner: MarkerSequenceOwner::Marker,
                },
            )],
            vec![ImmutableOrderCandidateMajorRestore {
                transaction_order: 0,
                candidate_keys: vec![marker_key()],
            }],
        )
    } else {
        (vec![], vec![])
    }
}

fn bound_claims(
    terminal: BindingTerminalOwner,
    marker_offset: u64,
) -> (
    Vec<MovableSequenceClaim>,
    SequenceProductRangesRestore,
    Vec<MovableOrderClaim>,
) {
    (
        vec![
            MovableSequenceClaim {
                delivery_seq: 2 + marker_offset,
                owner: SequenceDirectOwner::MembershipExit {
                    participant_index: PARTICIPANT_ID,
                },
            },
            MovableSequenceClaim {
                delivery_seq: 3 + marker_offset,
                owner: SequenceDirectOwner::BindingTerminal(terminal),
            },
        ],
        SequenceProductRangesRestore {
            live_times_terminal: vec![TerminalProductRangeRestore {
                start: 4 + marker_offset,
                length: 1,
                terminal,
            }],
            live_times_replacement_terminal: None,
            other_live_times_exit: vec![],
        },
        vec![
            MovableOrderClaim {
                transaction_order: 1,
                owner: OrderDirectOwner::ActiveBindingTerminal(terminal),
            },
            MovableOrderClaim {
                transaction_order: 2,
                owner: OrderDirectOwner::MembershipExit {
                    participant_index: PARTICIPANT_ID,
                },
            },
        ],
    )
}

fn detached_claims(
    marker_offset: u64,
) -> (
    Vec<MovableSequenceClaim>,
    SequenceProductRangesRestore,
    Vec<MovableOrderClaim>,
) {
    (
        vec![MovableSequenceClaim {
            delivery_seq: 2 + marker_offset,
            owner: SequenceDirectOwner::MembershipExit {
                participant_index: PARTICIPANT_ID,
            },
        }],
        SequenceProductRangesRestore::default(),
        vec![MovableOrderClaim {
            transaction_order: 1,
            owner: OrderDirectOwner::MembershipExit {
                participant_index: PARTICIPANT_ID,
            },
        }],
    )
}

fn frontiers(target_binding: FrontierBinding, with_marker: bool) -> ClaimFrontiers {
    let terminal = BindingTerminalOwner {
        participant_index: PARTICIPANT_ID,
        binding_epoch: epoch(),
    };
    let marker_count = u64::from(with_marker);
    let (terminal_count, active_terminal_count, claim_parts) = match target_binding {
        FrontierBinding::Bound(_) => (1, 1, bound_claims(terminal, marker_count)),
        FrontierBinding::Detached(_) => (0, 0, detached_claims(marker_count)),
    };
    let (sequence_movable, products, order_movable) = claim_parts;
    let sequence_ledger = SequenceLedger::try_new(
        1,
        SequenceClaims::new(
            1,
            terminal_count,
            marker_count,
            RecoverySequenceReserve::None,
        ),
    )
    .expect("test sequence reserve fits after H=1");
    let order_ledger = OrderLedger::try_new(
        OrderHigh::Allocated(0),
        OrderClaims::new(active_terminal_count, 1, false, false).expect("no recovery half-pair"),
    )
    .expect("test order reserve fits after major zero");
    let (immutable_candidates, order_candidates) = marker_candidates(with_marker, target_binding);

    ClaimFrontiers::restore(
        ClaimFrontiersRestore {
            conversation_id: CONVERSATION_ID,
            active_identities: vec![FrontierParticipant::new(PARTICIPANT_ID, 0, target_binding)],
            identity_slot_limit: 1,
            retained_floor: 1,
            retained_record_limit: 1,
            retained_records: vec![RetainedCausalRecord {
                delivery_seq: 1,
                admission_order: AdmissionOrder::new(
                    0,
                    CandidatePhase::OrdinaryRecord,
                    PARTICIPANT_ID,
                ),
                kind: RetainedCausalRecordKind::OrdinaryRecord {
                    participant_index: PARTICIPANT_ID,
                },
            }],
            active_marker_anchors: vec![],
            historical_marker_deliveries: vec![],
            historical_causal_facts: vec![],
            sequence: SequenceClaimFrontierRestore {
                movable_claims: sequence_movable,
                immutable_candidates,
                products,
                recovery: None,
            },
            order: OrderClaimFrontierRestore {
                movable_claims: order_movable,
                immutable_candidates: order_candidates,
                recovery: None,
            },
            recovery_marker_delivery_seq: None,
        },
        sequence_ledger,
        order_ledger,
    )
    .expect("complete marker-drain fixture restores")
}

fn debt() -> ClosureDebt {
    ClosureDebt::new(WideResourceVector::new(1, 1)).expect("test debt is nonzero")
}

#[test]
fn drain_consumes_next_marker_and_returns_one_atomic_authority_commit() -> Result<(), &'static str>
{
    let commit = drain_next_marker(
        frontiers(FrontierBinding::Bound(epoch()), true),
        ClosureState::Clear,
    )
    .expect("next bound marker must drain");

    let StoredEdge::MarkerDelivery(delivery) = commit.marker_successor() else {
        return Err("bound candidate did not select marker delivery");
    };
    assert_eq!(delivery.participant_id(), PARTICIPANT_ID);
    assert_eq!(delivery.binding_epoch(), epoch());
    assert_eq!(delivery.marker_delivery_seq(), 2);
    assert_eq!(
        commit.delivery_projection().delivery(),
        &ParticipantDelivery {
            conversation_id: CONVERSATION_ID,
            delivery_seq: 2,
            record: ParticipantRecord::HistoryCompacted {
                affected_participant_id: PARTICIPANT_ID,
                abandoned_after: 0,
                abandoned_through: 1,
                physical_floor_at_decision: 1,
            },
        }
    );
    assert_eq!(commit.closure(), ClosureState::Clear);
    assert_eq!(commit.frontiers().conversation_id(), CONVERSATION_ID);
    assert_eq!(commit.frontiers().retained_marker_records().len(), 1);
    assert_eq!(
        commit.frontiers().retained_marker_records()[0].delivery_seq,
        2
    );
    assert_eq!(
        commit.frontiers().retained_marker_records()[0].admission_order,
        marker_key()
    );
    assert_eq!(commit.frontiers().sequence().ledger().high_watermark(), 2);
    assert_eq!(commit.frontiers().sequence().ledger().claims().markers(), 0);
    assert_eq!(
        commit.frontiers().order().ledger().high(),
        OrderHigh::Allocated(0)
    );
    assert!(
        commit
            .frontiers()
            .sequence()
            .immutable_candidates()
            .is_empty()
    );
    assert!(commit.frontiers().order().immutable_candidates().is_empty());
    assert_eq!(commit.frontiers().retained_marker_records().len(), 1);
    Ok(())
}

#[test]
fn missing_candidate_is_an_invariant_fault_and_replay_is_deterministic() {
    assert_eq!(
        drain_next_marker(
            frontiers(FrontierBinding::Bound(epoch()), false),
            ClosureState::Clear,
        ),
        Err(MarkerDrainError::NoCandidate)
    );
    assert_eq!(
        drain_next_marker(
            frontiers(FrontierBinding::Bound(epoch()), true),
            ClosureState::Clear,
        ),
        drain_next_marker(
            frontiers(FrontierBinding::Bound(epoch()), true),
            ClosureState::Clear,
        )
    );
}

#[test]
fn detached_candidate_appends_and_selects_exact_dmr() -> Result<(), &'static str> {
    let commit = drain_next_marker(
        frontiers(FrontierBinding::Detached(epoch()), true),
        ClosureState::Clear,
    )
    .expect("detached marker candidate still appends");

    let StoredEdge::DetachedMarkerRelease(release) = commit.marker_successor() else {
        return Err("detached candidate did not select DMR");
    };
    assert_eq!(release.participant_id(), PARTICIPANT_ID);
    assert_eq!(release.last_dead_binding_epoch(), epoch());
    assert_eq!(release.marker_delivery_seq(), 2);
    assert_eq!(commit.frontiers().sequence().ledger().high_watermark(), 2);
    assert_eq!(commit.frontiers().retained_marker_records().len(), 1);
    Ok(())
}

#[test]
fn drained_detached_marker_cannot_be_reused_as_delivered_recovery_proof() {
    let commit = drain_next_marker(
        frontiers(FrontierBinding::Detached(epoch()), true),
        ClosureState::Clear,
    )
    .expect("detached marker candidate still appends");
    assert!(matches!(
        commit.marker_successor(),
        StoredEdge::DetachedMarkerRelease(_)
    ));

    let undelivered_record = commit.into_record_for_test();
    assert_eq!(
        MarkerDeliveryRestore {
            participant_id: PARTICIPANT_ID,
            binding_epoch: epoch(),
            marker_delivery_seq: 2,
        }
        .restore_detached_delivered_for_test(CONVERSATION_ID, undelivered_record),
        Err(StorageRestoreError::StoredEdgeProvenance),
        "a DMR append lacks the delivery occurrence required by DCR/fenced recovery"
    );
}

#[test]
fn op_and_pc_remain_strict_while_bound_marker_appends() {
    let closure_debt = debt();
    let op_commit = drain_next_marker(
        frontiers(FrontierBinding::Bound(epoch()), true),
        ClosureState::Owed {
            debt: closure_debt,
            edge: StoredEdge::ObserverProjection(ObserverProjection::new(1)),
        },
    )
    .expect("marker append advances exact current OP");
    assert_eq!(
        op_commit.closure(),
        ClosureState::Owed {
            debt: closure_debt,
            edge: StoredEdge::ObserverProjection(ObserverProjection::new(2)),
        }
    );

    let compaction = PhysicalCompaction::new(1, 1).expect("test PC range is valid");
    let pc_commit = drain_next_marker(
        frontiers(FrontierBinding::Bound(epoch()), true),
        ClosureState::Owed {
            debt: closure_debt,
            edge: StoredEdge::PhysicalCompaction(compaction),
        },
    )
    .expect("marker append preserves exact current PC");
    assert_eq!(
        pc_commit.closure(),
        ClosureState::Owed {
            debt: closure_debt,
            edge: StoredEdge::PhysicalCompaction(compaction),
        }
    );
}

#[test]
fn already_materialized_marker_edge_cannot_coexist_with_pending_candidate() {
    let selected = drain_next_marker(
        frontiers(FrontierBinding::Bound(epoch()), true),
        ClosureState::Clear,
    )
    .expect("fixture obtains the sealed marker successor")
    .marker_successor();

    assert_eq!(
        drain_next_marker(
            frontiers(FrontierBinding::Bound(epoch()), true),
            ClosureState::Owed {
                debt: debt(),
                edge: selected,
            },
        ),
        Err(MarkerDrainError::CurrentEdgeMismatch),
        "candidate-not-consumed and already-materialized successor is corrupt"
    );
}