liminal-protocol 0.3.2

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
439
440
441
442
443
444
445
446
//! Move-coupled closure-debt authority and synchronous delivery seam.

use crate::wire::{AckCommitted, BindingEpoch, DeliverySeq, ParticipantId};

use super::{
    ClosureState, CursorEpisodeBuildError, FrontierBinding, LiveFrontierOwner, LiveMember,
    NonzeroDebtCursorEpisode, NonzeroParticipantAckCommit, NonzeroParticipantAckCommitError,
};

/// Failure to couple a validated live frontier to its complete debt episode.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ObligationDebtOwnerError {
    /// Validated frontier/observer inputs could not form one exact episode.
    Episode(CursorEpisodeBuildError),
    /// A nonzero commit did not apply to the exact coupled member/episode pair.
    NonzeroAck(NonzeroParticipantAckCommitError),
    /// A nonzero commit was attempted without one coherent Owed owner.
    NonzeroAckState,
}

/// Complete protocol-owned authority while closure debt is nonzero.
#[derive(Debug, PartialEq, Eq)]
pub struct CoupledObligationDebtOwner {
    frontier: LiveFrontierOwner,
    episode: NonzeroDebtCursorEpisode,
}

/// Sole protocol-owned live frontier and obligation-debt dispatch state.
#[derive(Debug, PartialEq, Eq)]
pub enum ObligationDebtDispatchState {
    /// Closure accounting is clear and no cursor episode exists.
    Clear(LiveFrontierOwner),
    /// Closure accounting is owed and one complete episode is move-coupled to it.
    Owed(CoupledObligationDebtOwner),
}

/// Move-only authority that prevents production from reinstalling a bare
/// frontier after beginning a coupled transition.
#[derive(Debug)]
pub struct ObligationDebtDispatchTransition {
    prior_episode: Option<NonzeroDebtCursorEpisode>,
}

impl ObligationDebtDispatchState {
    /// Couples one validated frontier according to its resulting closure state.
    ///
    /// # Errors
    ///
    /// Returns [`ObligationDebtOwnerError`] if an Owed frontier cannot form a
    /// complete episode from the same validated frontier and observer state.
    pub fn from_frontier(
        frontier: LiveFrontierOwner,
        observer_progress: DeliverySeq,
    ) -> Result<Self, ObligationDebtOwnerError> {
        match frontier.closure_accounting().state() {
            ClosureState::Clear => Ok(Self::Clear(frontier)),
            ClosureState::Owed { debt, .. } => {
                let episode = NonzeroDebtCursorEpisode::from_claim_frontiers(
                    frontier.frontiers(),
                    debt,
                    observer_progress,
                )
                .map_err(ObligationDebtOwnerError::Episode)?;
                Ok(Self::Owed(CoupledObligationDebtOwner { frontier, episode }))
            }
        }
    }

    /// Borrows the one live frontier owner.
    #[must_use]
    pub const fn frontier(&self) -> &LiveFrontierOwner {
        match self {
            Self::Clear(frontier) => frontier,
            Self::Owed(coupled) => &coupled.frontier,
        }
    }

    /// Borrows the episode only on the Owed branch.
    #[must_use]
    pub const fn episode(&self) -> Option<&NonzeroDebtCursorEpisode> {
        match self {
            Self::Clear(_) => None,
            Self::Owed(coupled) => Some(&coupled.episode),
        }
    }

    /// Returns one participant's binding-tagged protocol cursor from the live
    /// frontier on both owner variants.
    #[must_use]
    pub fn frontier_participant(
        &self,
        participant_id: ParticipantId,
    ) -> Option<(FrontierBinding, DeliverySeq)> {
        self.frontier()
            .frontiers()
            .active_identities()
            .participants()
            .iter()
            .find(|participant| participant.participant_index() == participant_id)
            .map(|participant| (participant.binding(), participant.cursor()))
    }

    /// Returns one participant's binding-tagged episode cursor on Owed.
    #[must_use]
    pub fn participant(
        &self,
        participant_id: ParticipantId,
    ) -> Option<(FrontierBinding, DeliverySeq)> {
        self.episode()?.participant_binding(participant_id)
    }

    /// Begins one consuming frontier transition while retaining the prior
    /// episode in an unforgeable completion token.
    #[must_use]
    pub fn begin_transition(self) -> (LiveFrontierOwner, ObligationDebtDispatchTransition) {
        match self {
            Self::Clear(frontier) => (
                frontier,
                ObligationDebtDispatchTransition {
                    prior_episode: None,
                },
            ),
            Self::Owed(coupled) => (
                coupled.frontier,
                ObligationDebtDispatchTransition {
                    prior_episode: Some(coupled.episode),
                },
            ),
        }
    }
}

impl ObligationDebtDispatchTransition {
    /// Completes a coupled transition from the operation's exact resulting
    /// frontier. Clear consumes any old episode; Owed reconciles all tagged
    /// participants and surviving facts into one complete episode.
    ///
    /// # Errors
    ///
    /// Returns [`ObligationDebtOwnerError`] if resulting frontier and observer
    /// state cannot form one coherent owner.
    pub fn complete(
        self,
        frontier: LiveFrontierOwner,
        observer_progress: DeliverySeq,
    ) -> Result<ObligationDebtDispatchState, ObligationDebtOwnerError> {
        match frontier.closure_accounting().state() {
            ClosureState::Clear => Ok(ObligationDebtDispatchState::Clear(frontier)),
            ClosureState::Owed { debt, .. } => {
                let episode = self
                    .prior_episode
                    .map_or_else(
                        || {
                            NonzeroDebtCursorEpisode::from_claim_frontiers(
                                frontier.frontiers(),
                                debt,
                                observer_progress,
                            )
                        },
                        |episode| {
                            episode.reconcile_claim_frontiers(
                                frontier.frontiers(),
                                debt,
                                observer_progress,
                            )
                        },
                    )
                    .map_err(ObligationDebtOwnerError::Episode)?;
                Ok(ObligationDebtDispatchState::Owed(
                    CoupledObligationDebtOwner { frontier, episode },
                ))
            }
        }
    }

    /// Completes one nonzero acknowledgement with the exact episode produced by
    /// its sealed commit instead of regenerating cursor facts from frontier
    /// scalars. The member, episode, and frontier therefore cross one protocol
    /// barrier as the same commit.
    ///
    /// # Errors
    ///
    /// Returns [`ObligationDebtOwnerError`] if the transition did not begin from
    /// Owed, the aggregate commit rejects the member/episode pair, or the exact
    /// resulting episode disagrees with the resulting frontier and closure debt.
    pub fn complete_nonzero_ack<F>(
        mut self,
        frontier: LiveFrontierOwner,
        commit: NonzeroParticipantAckCommit,
        member: &mut LiveMember<F>,
        observer_progress: DeliverySeq,
    ) -> Result<(ObligationDebtDispatchState, AckCommitted), ObligationDebtOwnerError> {
        let ClosureState::Owed { debt, .. } = frontier.closure_accounting().state() else {
            return Err(ObligationDebtOwnerError::NonzeroAckState);
        };
        let mut episode = self
            .prior_episode
            .take()
            .ok_or(ObligationDebtOwnerError::NonzeroAckState)?;
        let outcome = commit
            .apply_to(member, &mut episode)
            .map_err(ObligationDebtOwnerError::NonzeroAck)?;
        if episode.debt() != debt {
            return Err(ObligationDebtOwnerError::NonzeroAckState);
        }
        let reconciled = episode
            .clone()
            .reconcile_claim_frontiers(frontier.frontiers(), debt, observer_progress)
            .map_err(ObligationDebtOwnerError::Episode)?;
        if reconciled != episode {
            return Err(ObligationDebtOwnerError::NonzeroAckState);
        }
        Ok((
            ObligationDebtDispatchState::Owed(CoupledObligationDebtOwner { frontier, episode }),
            outcome,
        ))
    }
}

/// Internal scheduling deferral selected by the completed Leg 2 decision body.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DebtDispatchDeferral {
    /// No durable recipient obligation follows the reconciled cursor.
    NoObligation,
    /// The participant has no exact current binding.
    NoCurrentBinding,
    /// The least testified endpoint is beyond the active debt high watermark.
    BeyondDebtHighWatermark,
}

/// Typed disagreement that must fail dispatch rather than fabricate a wire value.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DebtDispatchInvariant {
    /// Coupled closure and episode authority disagree.
    CoupledState,
    /// Binding or cursor authority differs across the locked snapshot.
    ParticipantAuthority,
    /// Outbox selection did not advance beyond the reconciled cursor.
    OutboxSelection,
    /// The two nonzero acknowledgement selectors diverged.
    ScalarDivergence,
}

/// Total synchronous result at the participant delivery-decision seam.
#[derive(Debug, PartialEq, Eq)]
pub enum ObligationDebtDispatchDecision<T> {
    /// The selected durable obligation may proceed to publication construction.
    Permit(T),
    /// Current debt authority does not permit work yet.
    Defer(DebtDispatchDeferral),
    /// Locked authority is internally inconsistent.
    Invariant(DebtDispatchInvariant),
}

/// Executes the protocol-owned dispatch decision under the conversation owner.
///
/// The caller supplies the already-reconciled cursor and a one-shot read of the
/// least durable recipient obligation strictly after it. Clear authority permits
/// any advancing selection. Owed authority additionally requires exact agreement
/// between frontier and episode participant state, then treats endpoint testimony
/// and the episode high watermark as the complete eligibility domain: the
/// physical retention floor is deliberately not consulted.
pub fn decide_obligation_debt_dispatch<T>(
    state: &ObligationDebtDispatchState,
    participant_id: ParticipantId,
    binding_epoch: BindingEpoch,
    dispatch_after: DeliverySeq,
    select_next: impl FnOnce(ParticipantId, BindingEpoch, DeliverySeq) -> Option<(DeliverySeq, T)>,
) -> ObligationDebtDispatchDecision<T> {
    let frontier_participant = state
        .frontier()
        .frontiers()
        .active_identities()
        .participants()
        .iter()
        .find(|participant| participant.participant_index() == participant_id)
        .copied();
    let Some(frontier_participant) = frontier_participant else {
        return ObligationDebtDispatchDecision::Invariant(
            DebtDispatchInvariant::ParticipantAuthority,
        );
    };
    let FrontierBinding::Bound(frontier_epoch) = frontier_participant.binding() else {
        return ObligationDebtDispatchDecision::Defer(DebtDispatchDeferral::NoCurrentBinding);
    };
    if frontier_epoch != binding_epoch || frontier_participant.cursor() > dispatch_after {
        return ObligationDebtDispatchDecision::Invariant(
            DebtDispatchInvariant::ParticipantAuthority,
        );
    }

    let owed_high_watermark = match state {
        ObligationDebtDispatchState::Clear(frontier) => {
            if !matches!(frontier.closure_accounting().state(), ClosureState::Clear) {
                return ObligationDebtDispatchDecision::Invariant(
                    DebtDispatchInvariant::CoupledState,
                );
            }
            None
        }
        ObligationDebtDispatchState::Owed(coupled) => {
            let ClosureState::Owed { debt, .. } = coupled.frontier.closure_accounting().state()
            else {
                return ObligationDebtDispatchDecision::Invariant(
                    DebtDispatchInvariant::CoupledState,
                );
            };
            if coupled.episode.debt() != debt {
                return ObligationDebtDispatchDecision::Invariant(
                    DebtDispatchInvariant::CoupledState,
                );
            }
            if coupled.episode.participant_binding(participant_id)
                != Some((
                    FrontierBinding::Bound(binding_epoch),
                    frontier_participant.cursor(),
                ))
            {
                return ObligationDebtDispatchDecision::Invariant(
                    DebtDispatchInvariant::ParticipantAuthority,
                );
            }
            Some(coupled.episode.candidate_high_watermark())
        }
    };

    classify_selected_obligation(
        dispatch_after,
        owed_high_watermark,
        select_next(participant_id, binding_epoch, dispatch_after),
    )
}

fn classify_selected_obligation<T>(
    dispatch_after: DeliverySeq,
    owed_high_watermark: Option<DeliverySeq>,
    selected: Option<(DeliverySeq, T)>,
) -> ObligationDebtDispatchDecision<T> {
    let Some((delivery_seq, selected)) = selected else {
        return ObligationDebtDispatchDecision::Defer(DebtDispatchDeferral::NoObligation);
    };
    if delivery_seq <= dispatch_after {
        return ObligationDebtDispatchDecision::Invariant(DebtDispatchInvariant::OutboxSelection);
    }
    if owed_high_watermark.is_some_and(|high_watermark| delivery_seq > high_watermark) {
        return ObligationDebtDispatchDecision::Defer(
            DebtDispatchDeferral::BeyondDebtHighWatermark,
        );
    }
    ObligationDebtDispatchDecision::Permit(selected)
}

#[cfg(test)]
mod tests {
    use alloc::vec;

    use crate::{
        algebra::WideResourceVector,
        wire::{BindingEpoch, ConnectionIncarnation, Generation},
    };

    use super::{
        super::{BoundParticipantCursor, ClosureDebt, NonzeroDebtCursorEpisode},
        DebtDispatchDeferral, DebtDispatchInvariant, ObligationDebtDispatchDecision,
        classify_selected_obligation,
    };

    fn binding_epoch() -> BindingEpoch {
        BindingEpoch::new(
            ConnectionIncarnation::new(1, 1),
            Generation::new(1).unwrap_or(Generation::ONE),
        )
    }

    #[test]
    fn nonzero_debt_permits_testified_below_floor_and_defers_above_high_watermark() {
        let cursor = 0;
        let high_watermark = 100;
        let physical_floor = 25;
        let capacity_floor = 25;
        let below_floor_endpoint = 10;
        let above_high_watermark = high_watermark + 1;
        let episode = NonzeroDebtCursorEpisode::new(
            1,
            ClosureDebt::new(WideResourceVector::new(1, 1))
                .unwrap_or_else(|| unreachable!("fixture debt is nonzero")),
            high_watermark,
            high_watermark,
            physical_floor,
            capacity_floor,
            vec![BoundParticipantCursor::new(0, binding_epoch(), cursor)],
        )
        .unwrap_or_else(|error| unreachable!("fixture episode must be valid: {error:?}"));

        assert_eq!(episode.candidate_high_watermark(), high_watermark);
        assert_eq!(episode.observer_progress(), high_watermark);
        assert_eq!(episode.cap_floor(), capacity_floor);
        assert_eq!(episode.retained_suffix_start(), Some(25));
        assert!(!episode.retains(below_floor_endpoint));
        assert_eq!(
            classify_selected_obligation(
                cursor,
                Some(episode.candidate_high_watermark()),
                Some((below_floor_endpoint, below_floor_endpoint)),
            ),
            ObligationDebtDispatchDecision::Permit(below_floor_endpoint)
        );
        assert_eq!(
            classify_selected_obligation(
                cursor,
                Some(episode.candidate_high_watermark()),
                Some((above_high_watermark, above_high_watermark)),
            ),
            ObligationDebtDispatchDecision::Defer(DebtDispatchDeferral::BeyondDebtHighWatermark)
        );
        assert_eq!(
            classify_selected_obligation::<u64>(
                cursor,
                Some(episode.candidate_high_watermark()),
                None,
            ),
            ObligationDebtDispatchDecision::Defer(DebtDispatchDeferral::NoObligation)
        );
    }

    #[test]
    fn debt_dispatch_invariant_never_falls_back_or_fabricates_wire_refusal() {
        let cursor = 7;
        let selected_payload = "durable publication";
        let decision =
            classify_selected_obligation(cursor, Some(100), Some((cursor, selected_payload)));

        assert_eq!(
            decision,
            ObligationDebtDispatchDecision::Invariant(DebtDispatchInvariant::OutboxSelection)
        );
        assert!(!matches!(
            decision,
            ObligationDebtDispatchDecision::Permit(_)
        ));
        assert!(!matches!(
            decision,
            ObligationDebtDispatchDecision::Defer(_)
        ));
    }
}