liminal-protocol 0.2.1

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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use alloc::boxed::Box;

use crate::algebra::{ResourceDimension, ResourceVector, WideResourceVector};
use crate::wire::{
    ClosureCapacityReason, ClosureCheckedEnvelope, ClosureRefusalReason, ClosureSnapshot,
    MarkerClosureCapacityExceeded, ParticipantCursorProgressEdge, RepaymentEdge,
};

use super::{ClosureState, StoredEdge};

/// Invalid durable closure-accounting state.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ClosureAccountingError {
    /// The signed churn limit must be nonzero.
    ZeroChurnLimit,
    /// Durable churn use exceeds the signed limit.
    ChurnUsedExceedsLimit {
        /// Durable cycles already used.
        used: u64,
        /// Signed episode limit.
        limit: u64,
    },
    /// Marker anchors cannot outnumber slots owning marker credits.
    MarkerAnchorsExceedCredits {
        /// Current distinct marker anchors.
        anchors: u64,
        /// Current marker-capacity credits.
        credits: u64,
    },
    /// The durable baseline exceeds the configured capacity.
    BaselineExceedsCapacity {
        /// First failing component.
        dimension: ResourceDimension,
    },
    /// A clear edge retained edge-owned claims or recovery occupancy.
    ClearStateOwnsEdgeResources,
    /// A nonzero debt component cannot fit the frozen wire snapshot width.
    DebtOutsideWireDomain {
        /// First unencodable component.
        dimension: ResourceDimension,
    },
}

/// Validated unchanged-prestate closure accounting.
///
/// This wrapper owns every common field disclosed by a closure refusal. The
/// wire snapshot is derived from it, so a server binding cannot mix current and
/// proposed values or hand-construct an operation-specific refusal.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ClosureAccounting {
    state: ClosureState,
    marker_capacity_credits: u64,
    marker_anchors: u64,
    edge_sequence_claims: u64,
    edge_order_position_claims: u64,
    edge_k_remaining: ResourceVector,
    baseline: WideResourceVector,
    configured_cap: ResourceVector,
    episode_churn_used: u64,
    episode_churn_limit: u64,
}

impl ClosureAccounting {
    /// Validates one complete durable accounting snapshot.
    ///
    /// The edge-owned claim counts remain explicit durable facts because their
    /// exact occurrence plan is deliberately not the defective fixed array
    /// excluded by `docs/design/LP-EXTRACTION-GOAL.md` Fix 2.
    ///
    /// # Errors
    ///
    /// Returns [`ClosureAccountingError`] for a zero/overused churn bound,
    /// impossible anchor count, baseline outside capacity, edge resources in a
    /// clear state, or debt that cannot use the frozen `u64` wire fields.
    #[allow(clippy::too_many_arguments)]
    pub fn try_new(
        state: ClosureState,
        marker_capacity_credits: u64,
        marker_anchors: u64,
        edge_sequence_claims: u64,
        edge_order_position_claims: u64,
        edge_k_remaining: ResourceVector,
        baseline: WideResourceVector,
        configured_cap: ResourceVector,
        episode_churn_used: u64,
        episode_churn_limit: u64,
    ) -> Result<Self, ClosureAccountingError> {
        if episode_churn_limit == 0 {
            return Err(ClosureAccountingError::ZeroChurnLimit);
        }
        if episode_churn_used > episode_churn_limit {
            return Err(ClosureAccountingError::ChurnUsedExceedsLimit {
                used: episode_churn_used,
                limit: episode_churn_limit,
            });
        }
        if marker_anchors > marker_capacity_credits {
            return Err(ClosureAccountingError::MarkerAnchorsExceedCredits {
                anchors: marker_anchors,
                credits: marker_capacity_credits,
            });
        }
        if baseline.entries > u128::from(configured_cap.entries) {
            return Err(ClosureAccountingError::BaselineExceedsCapacity {
                dimension: ResourceDimension::Entries,
            });
        }
        if baseline.bytes > u128::from(configured_cap.bytes) {
            return Err(ClosureAccountingError::BaselineExceedsCapacity {
                dimension: ResourceDimension::Bytes,
            });
        }
        match state {
            ClosureState::Clear => {
                if edge_sequence_claims != 0
                    || edge_order_position_claims != 0
                    || edge_k_remaining != ResourceVector::default()
                {
                    return Err(ClosureAccountingError::ClearStateOwnsEdgeResources);
                }
            }
            ClosureState::Owed { debt, .. } => {
                let value = debt.value();
                if u64::try_from(value.entries).is_err() {
                    return Err(ClosureAccountingError::DebtOutsideWireDomain {
                        dimension: ResourceDimension::Entries,
                    });
                }
                if u64::try_from(value.bytes).is_err() {
                    return Err(ClosureAccountingError::DebtOutsideWireDomain {
                        dimension: ResourceDimension::Bytes,
                    });
                }
            }
        }
        Ok(Self {
            state,
            marker_capacity_credits,
            marker_anchors,
            edge_sequence_claims,
            edge_order_position_claims,
            edge_k_remaining,
            baseline,
            configured_cap,
            episode_churn_used,
            episode_churn_limit,
        })
    }

    /// Returns the typed clear-or-owed closure state.
    #[must_use]
    pub const fn state(self) -> ClosureState {
        self.state
    }

    /// Returns identity slots currently owning marker-capacity credits.
    #[must_use]
    pub const fn marker_capacity_credits(self) -> u64 {
        self.marker_capacity_credits
    }

    /// Returns current planned, undelivered, or delivered marker anchors.
    #[must_use]
    pub const fn marker_anchors(self) -> u64 {
        self.marker_anchors
    }

    /// Returns sequence claims owned by the current edge.
    #[must_use]
    pub const fn edge_sequence_claims(self) -> u64 {
        self.edge_sequence_claims
    }

    /// Returns transaction-order positions owned by the current edge.
    #[must_use]
    pub const fn edge_order_position_claims(self) -> u64 {
        self.edge_order_position_claims
    }

    /// Returns the exact current recovery-capacity occupancy.
    #[must_use]
    pub const fn edge_k_remaining(self) -> ResourceVector {
        self.edge_k_remaining
    }

    /// Returns the current retained baseline `B`.
    #[must_use]
    pub const fn baseline(self) -> WideResourceVector {
        self.baseline
    }

    /// Returns the configured entry/byte capacity.
    #[must_use]
    pub const fn configured_cap(self) -> ResourceVector {
        self.configured_cap
    }

    /// Returns activated churn cycles already used.
    #[must_use]
    pub const fn episode_churn_used(self) -> u64 {
        self.episode_churn_used
    }

    /// Returns the signed churn-cycle limit.
    #[must_use]
    pub const fn episode_churn_limit(self) -> u64 {
        self.episode_churn_limit
    }

    fn snapshot(self, delta_cycles: u64) -> ClosureSnapshot {
        let (debt, repayment_edge) = closure_state_wire(self.state);
        ClosureSnapshot {
            marker_capacity_credits: self.marker_capacity_credits,
            marker_anchors: self.marker_anchors,
            entry_debt: debt.entries,
            byte_debt: debt.bytes,
            repayment_edge,
            edge_sequence_claims: self.edge_sequence_claims,
            edge_order_position_claims: self.edge_order_position_claims,
            edge_k_remaining: self.edge_k_remaining,
            k_headroom: WideResourceVector::new(
                u128::from(self.configured_cap.entries) - self.baseline.entries,
                u128::from(self.configured_cap.bytes) - self.baseline.bytes,
            ),
            episode_churn_used: self.episode_churn_used,
            delta_cycles,
            episode_churn_limit: self.episode_churn_limit,
        }
    }
}

/// Invalid required-capacity simulation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RequiredCapacityPlanError {
    /// A successor simulation supplied no reachable state.
    EmptySuccessorSet,
    /// A checked ordinary-capacity addition exceeded `u128`.
    ArithmeticOverflow {
        /// First overflowing component.
        dimension: ResourceDimension,
    },
}

/// Componentwise maximum required capacity across a finite successor plan.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RequiredCapacityPlan {
    maximum: WideResourceVector,
}

impl RequiredCapacityPlan {
    /// Takes the componentwise maximum across every reachable successor node.
    ///
    /// # Errors
    ///
    /// Returns [`RequiredCapacityPlanError::EmptySuccessorSet`] for an empty
    /// plan, which cannot prove closure coverage.
    pub fn from_successors(
        successors: &[WideResourceVector],
    ) -> Result<Self, RequiredCapacityPlanError> {
        let Some(first) = successors.first().copied() else {
            return Err(RequiredCapacityPlanError::EmptySuccessorSet);
        };
        let maximum = successors.iter().skip(1).fold(first, |maximum, state| {
            WideResourceVector::new(
                maximum.entries.max(state.entries),
                maximum.bytes.max(state.bytes),
            )
        });
        Ok(Self { maximum })
    }

    /// Derives an ordinary caller's required `B' + Q + K` vector.
    ///
    /// # Errors
    ///
    /// Returns [`RequiredCapacityPlanError::ArithmeticOverflow`] for the first
    /// component whose canonical additions cannot fit `u128`.
    pub fn ordinary(
        resulting_baseline: WideResourceVector,
        mandatory_bound: ResourceVector,
        full_recovery_claim: ResourceVector,
    ) -> Result<Self, RequiredCapacityPlanError> {
        let Some(entries_with_q) = resulting_baseline
            .entries
            .checked_add(u128::from(mandatory_bound.entries))
        else {
            return Err(RequiredCapacityPlanError::ArithmeticOverflow {
                dimension: ResourceDimension::Entries,
            });
        };
        let Some(entries) = entries_with_q.checked_add(u128::from(full_recovery_claim.entries))
        else {
            return Err(RequiredCapacityPlanError::ArithmeticOverflow {
                dimension: ResourceDimension::Entries,
            });
        };
        let Some(bytes_with_q) = resulting_baseline
            .bytes
            .checked_add(u128::from(mandatory_bound.bytes))
        else {
            return Err(RequiredCapacityPlanError::ArithmeticOverflow {
                dimension: ResourceDimension::Bytes,
            });
        };
        let Some(bytes) = bytes_with_q.checked_add(u128::from(full_recovery_claim.bytes)) else {
            return Err(RequiredCapacityPlanError::ArithmeticOverflow {
                dimension: ResourceDimension::Bytes,
            });
        };
        Ok(Self {
            maximum: WideResourceVector::new(entries, bytes),
        })
    }

    /// Returns the simulated componentwise maximum.
    #[must_use]
    pub const fn maximum(self) -> WideResourceVector {
        self.maximum
    }
}

/// Successful stage-7 proof that no recovery fence applies.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RecoveryFencePermit {
    accounting: ClosureAccounting,
}

impl RecoveryFencePermit {
    /// Returns the exact unchanged accounting checked at stage 7.
    #[must_use]
    pub const fn accounting(self) -> ClosureAccounting {
        self.accounting
    }
}

/// Stage-7 recovery-fence decision.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RecoveryFenceDecision {
    /// No detached-edge or second-quartet fence applies.
    Eligible(RecoveryFencePermit),
    /// Exact unchanged-prestate closure refusal.
    Respond(Box<MarkerClosureCapacityExceeded>),
}

/// Applies the stage-7 recovery-fence predicate before numeric admission.
#[must_use]
pub fn check_recovery_fence(
    request: &ClosureCheckedEnvelope,
    accounting: ClosureAccounting,
    recovery_fence: bool,
) -> RecoveryFenceDecision {
    if recovery_fence {
        return RecoveryFenceDecision::Respond(Box::new(closure_refusal(
            request.clone(),
            accounting,
            0,
            ClosureRefusalReason::RecoveryFence,
        )));
    }
    RecoveryFenceDecision::Eligible(RecoveryFencePermit { accounting })
}

/// Successful remaining closure gate.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RemainingClosurePermit {
    accounting: ClosureAccounting,
    required_capacity: RequiredCapacityPlan,
    delta_cycles: u64,
}

impl RemainingClosurePermit {
    /// Returns the unchanged prestate accounting.
    #[must_use]
    pub const fn accounting(self) -> ClosureAccounting {
        self.accounting
    }

    /// Returns the checked componentwise successor maximum.
    #[must_use]
    pub const fn required_capacity(self) -> RequiredCapacityPlan {
        self.required_capacity
    }

    /// Returns the exact charged plan cycles.
    #[must_use]
    pub const fn delta_cycles(self) -> u64 {
        self.delta_cycles
    }
}

/// Stage-12 closure decision after observer admission.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RemainingClosureDecision {
    /// Delivered-marker, churn, and componentwise capacity checks passed.
    Eligible(Box<RemainingClosurePermit>),
    /// Exact first remaining closure refusal.
    Respond(Box<MarkerClosureCapacityExceeded>),
}

/// Applies the remaining closure order: delivered marker, churn, then capacity.
///
/// Entries precede bytes and equality passes. A delivered-marker refusal uses
/// `delta_cycles=0`; churn and capacity serialize the exact proposed charge.
#[must_use]
pub fn check_remaining_closure(
    request: &ClosureCheckedEnvelope,
    accounting: ClosureAccounting,
    delivered_marker_awaiting_ack: bool,
    delta_cycles: u64,
    required_capacity: RequiredCapacityPlan,
) -> RemainingClosureDecision {
    if delivered_marker_awaiting_ack {
        return RemainingClosureDecision::Respond(Box::new(closure_refusal(
            request.clone(),
            accounting,
            0,
            ClosureRefusalReason::DeliveredMarkerAwaitingAck,
        )));
    }
    let resulting_cycles = u128::from(accounting.episode_churn_used) + u128::from(delta_cycles);
    if resulting_cycles > u128::from(accounting.episode_churn_limit) {
        return RemainingClosureDecision::Respond(Box::new(closure_refusal(
            request.clone(),
            accounting,
            delta_cycles,
            ClosureRefusalReason::EpisodeChurnLimit,
        )));
    }
    let maximum = required_capacity.maximum;
    if maximum.entries > u128::from(accounting.configured_cap.entries) {
        return RemainingClosureDecision::Respond(Box::new(closure_refusal(
            request.clone(),
            accounting,
            delta_cycles,
            ClosureRefusalReason::Capacity(ClosureCapacityReason {
                dimension: ResourceDimension::Entries,
                required: maximum.entries,
                limit: u128::from(accounting.configured_cap.entries),
            }),
        )));
    }
    if maximum.bytes > u128::from(accounting.configured_cap.bytes) {
        return RemainingClosureDecision::Respond(Box::new(closure_refusal(
            request.clone(),
            accounting,
            delta_cycles,
            ClosureRefusalReason::Capacity(ClosureCapacityReason {
                dimension: ResourceDimension::Bytes,
                required: maximum.bytes,
                limit: u128::from(accounting.configured_cap.bytes),
            }),
        )));
    }
    RemainingClosureDecision::Eligible(Box::new(RemainingClosurePermit {
        accounting,
        required_capacity,
        delta_cycles,
    }))
}

fn closure_refusal(
    request: ClosureCheckedEnvelope,
    accounting: ClosureAccounting,
    delta_cycles: u64,
    reason: ClosureRefusalReason,
) -> MarkerClosureCapacityExceeded {
    MarkerClosureCapacityExceeded {
        request,
        snapshot: accounting.snapshot(delta_cycles),
        reason,
    }
}

fn closure_state_wire(state: ClosureState) -> (ResourceVector, RepaymentEdge) {
    match state {
        ClosureState::Clear => (ResourceVector::default(), RepaymentEdge::None),
        ClosureState::Owed { debt, edge } => {
            let debt = debt.value();
            let wire_debt = ResourceVector::new(
                u64::try_from(debt.entries).map_or(u64::MAX, core::convert::identity),
                u64::try_from(debt.bytes).map_or(u64::MAX, core::convert::identity),
            );
            (wire_debt, stored_edge_wire(edge))
        }
    }
}

const fn stored_edge_wire(edge: StoredEdge) -> RepaymentEdge {
    match edge {
        StoredEdge::ObserverProjection(edge) => RepaymentEdge::ObserverProjection {
            through_seq: edge.through_seq(),
        },
        StoredEdge::PhysicalCompaction(edge) => RepaymentEdge::PhysicalCompaction {
            from_floor: edge.from_floor(),
            through_seq: edge.through_seq(),
        },
        StoredEdge::MarkerDelivery(edge) => RepaymentEdge::MarkerDelivery {
            participant_id: edge.participant_id(),
            binding_epoch: edge.binding_epoch(),
            marker_delivery_seq: edge.marker_delivery_seq(),
        },
        StoredEdge::ParticipantCursorProgress(edge) => {
            RepaymentEdge::ParticipantCursorProgress(ParticipantCursorProgressEdge {
                participant_id: edge.participant_id(),
                binding_epoch: edge.binding_epoch(),
                through_seq: edge.through_seq(),
                marker_delivery_seq: edge.marker_delivery_seq(),
            })
        }
        StoredEdge::DetachedCredentialRecovery(edge) => RepaymentEdge::DetachedCredentialRecovery {
            participant_id: edge.participant_id(),
            marker_delivery_seq: edge.marker_delivery_seq(),
            prior_binding_epoch: edge.prior_binding_epoch(),
        },
        StoredEdge::DetachedMarkerRelease(edge) => RepaymentEdge::DetachedMarkerRelease {
            participant_id: edge.participant_id(),
            marker_delivery_seq: edge.marker_delivery_seq(),
            last_dead_binding_epoch: edge.last_dead_binding_epoch(),
        },
        StoredEdge::DetachedCursorRelease(edge) => RepaymentEdge::DetachedCursorRelease {
            participant_id: edge.participant_id(),
            last_dead_binding_epoch: edge.last_dead_binding_epoch(),
        },
    }
}