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
use alloc::vec::Vec;

use super::{
    ClientBindingState, ClientParticipantAggregate, DetachReplayStatus, DetachReplayTerminal,
    ExpectedOperationState, LostAuthorityKind, LostAuthorityTestimony, ReconnectAggregate,
    RestoredExpectedOperationAbandonment, RestoredExpectedOperationAbandonmentReason,
    SdkDetachReplayAggregate, reconnect::ReconnectMachineState, replay::DetachReplayState,
};
use super::{resume_decode::decode_facts, resume_encode::encode_aggregate};
use crate::wire::{ClientRequest, CodecError};

pub(super) const MAGIC: [u8; 4] = *b"LPCR";
pub(super) const VERSION: u16 = 1;
pub(super) const HEADER_LEN: usize = 14;

/// Section whose tag or nested canonical frame was invalid.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ClientResumeRecordSection {
    /// Client binding state.
    Binding,
    /// Outstanding expected operation.
    ExpectedOperation,
    /// Detach replay state.
    DetachReplay,
    /// Reconnect permit or attempt state.
    Reconnect,
    /// Pending tokenless abandonment.
    Abandonment,
}

/// Failure while creating a canonical record from live client state.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ClientResumeRecordEncodeError {
    /// A nested request or terminal value cannot use the canonical wire codec.
    NestedCodec {
        /// Failing section.
        section: ClientResumeRecordSection,
        /// Exact wire codec error.
        source: CodecError,
    },
    /// The own-codec payload cannot fit its u64 length field.
    LengthOverflow,
}

/// Typed canonical client-record decode failure.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ClientResumeRecordDecodeError {
    /// Input ended before the requested number of bytes.
    Truncated {
        /// Bytes required at the failure point.
        needed: usize,
        /// Bytes remaining at the failure point.
        remaining: usize,
    },
    /// The four-byte client-record magic was not `LPCR`.
    InvalidMagic {
        /// Presented magic bytes.
        presented: [u8; 4],
    },
    /// The client-record envelope version is unsupported.
    UnsupportedVersion {
        /// Presented version.
        presented: u16,
    },
    /// Declared payload length differs from the exact remaining bytes.
    LengthMismatch {
        /// Declared payload bytes.
        declared: u64,
        /// Actual payload bytes.
        actual: usize,
    },
    /// A closed section tag was unknown.
    InvalidTag {
        /// Section containing the tag.
        section: ClientResumeRecordSection,
        /// Unknown tag.
        tag: u8,
    },
    /// A nested canonical participant frame was invalid or had the wrong direction.
    NestedCodec {
        /// Failing section.
        section: ClientResumeRecordSection,
        /// Exact wire codec error when structural decode failed.
        source: Option<CodecError>,
    },
    /// A serialized tokenless-after-crash abandonment carried an operation class
    /// that has a wire attempt token and therefore cannot use that resolution.
    InvalidAbandonmentRequest {
        /// Rejected token-bearing request class.
        request: crate::wire::ClientDiscriminant,
    },
    /// Extra bytes followed the four exact sections.
    TrailingBytes {
        /// Number of unexpected bytes.
        remaining: usize,
    },
}

/// Validated cold-restore invariant failure.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ClientResumeRestoreError {
    /// A bound credential generation differs from its binding epoch.
    BindingGenerationMismatch,
    /// A continuous acknowledgement illegally occupied the write-ahead slot.
    ContinuousAckOutstanding,
    /// Replay terminal payload does not match its retained exact detach request.
    ReplayTerminalMismatch,
    /// Expected operation authorization is zero or exceeds its durable counter.
    InvalidOperationAuthorization,
    /// Expected operation is illegal for the restored binding state or identity.
    ExpectedBindingMismatch,
    /// Active detach replay is not coupled to its matching expected detach.
    ActiveReplayExpectedDetachMismatch,
    /// An expected detach has no active matching replay lifecycle.
    ExpectedDetachActiveReplayMismatch,
    /// Reconnect authorization is zero or exceeds its durable counter.
    InvalidReconnectAuthorization,
    /// A serialized lost-authority testimony does not match the destroyed
    /// authority its slot and state imply (r2, 2026-07-18).
    LostAuthorityTestimonyMismatch,
    /// A pending tokenless abandonment coexists with a tokenless expected
    /// operation, which the crate's admission gate never produces
    /// (r2, 2026-07-18).
    PendingAbandonmentConflict,
    /// Private canonical bytes no longer decode; this is unreachable through public construction.
    CorruptRecord(ClientResumeRecordDecodeError),
}

/// Private-field, inert, canonical client persistence record.
///
/// This type proves only that bytes are a canonical committed-record envelope;
/// the storage owner remains responsible for admitting bytes from exactly one
/// cold process epoch. Preventing two processes from restoring the same bytes
/// is intentionally outside this `no_std` protocol crate, matching the server
/// precedent in `lifecycle/storage.rs`, whose joint validation likewise does
/// not prevent storage-owner double restore. Issuance flags are nevertheless
/// preserved for token-bearing operations, so a record that testifies an
/// authority was already issued never silently re-mints it. Tokenless
/// `ObserverRecovery` is the deliberate exception and resolves to typed abandonment
/// on every restore because replay cannot be made
/// at-most-once without an outbound attempt token.
#[derive(Debug, PartialEq, Eq)]
pub struct ClientResumeRecord {
    canonical: Vec<u8>,
}

impl ClientResumeRecord {
    /// Encodes this already-validated record in canonical v1 form.
    #[must_use]
    pub fn encode_canonical(&self) -> Vec<u8> {
        self.canonical.clone()
    }

    /// Decodes one exact canonical v1 client record without minting authority.
    ///
    /// # Errors
    ///
    /// Returns typed truncation, magic, version, tag, length, nested-codec, or
    /// trailing-byte errors.
    pub fn decode_canonical(input: &[u8]) -> Result<Self, ClientResumeRecordDecodeError> {
        let _ = decode_facts(input)?;
        Ok(Self {
            canonical: input.to_vec(),
        })
    }

    /// Validates cross-fact invariants and cold-restores executable client state.
    ///
    /// # Errors
    ///
    /// Returns a typed invariant error before any aggregate or reconnect
    /// authority escapes.
    pub fn restore(self) -> Result<ClientParticipantAggregate, ClientResumeRestoreError> {
        let facts =
            decode_facts(&self.canonical).map_err(ClientResumeRestoreError::CorruptRecord)?;
        validate_facts(&facts)?;
        let mut expected = facts.expected;
        let tokenless = expected
            .as_ref()
            .is_some_and(|expected| matches!(expected.request, ClientRequest::ObserverRecovery(_)));
        let restored_abandonment = if tokenless {
            expected
                .take()
                .map(|expected| RestoredExpectedOperationAbandonment {
                    request: expected.request,
                    reason: RestoredExpectedOperationAbandonmentReason::TokenlessAfterCrash,
                    was_issued: expected.issued,
                })
        } else {
            facts.abandonment
        };
        if let Some(expected) = expected.as_mut()
            && expected.issued
            && expected.lost.is_none()
        {
            let kind = if matches!(expected.request, ClientRequest::Detach(_)) {
                LostAuthorityKind::DetachTransportAttempt
            } else {
                LostAuthorityKind::IssuedOperationCorrelation
            };
            expected.lost = Some(LostAuthorityTestimony::mint(kind));
        }
        let mut reconnect_lost = facts.reconnect_lost;
        if reconnect_lost.is_none() {
            reconnect_lost = match facts.reconnect_state {
                ReconnectMachineState::Permit { issued: true, .. } => Some(
                    LostAuthorityTestimony::mint(LostAuthorityKind::ReconnectPermit),
                ),
                ReconnectMachineState::Attempt { .. } => Some(LostAuthorityTestimony::mint(
                    LostAuthorityKind::ReconnectAttempt,
                )),
                ReconnectMachineState::Parked
                | ReconnectMachineState::Permit { issued: false, .. }
                | ReconnectMachineState::Online => None,
            };
        }
        Ok(ClientParticipantAggregate {
            binding: facts.binding,
            expected,
            next_operation_authorization: facts.next_operation_authorization,
            detach_replay: SdkDetachReplayAggregate {
                state: facts.replay,
            },
            reconnect: ReconnectAggregate {
                state: facts.reconnect_state,
                next_authorization: facts.next_authorization,
                lost: reconnect_lost,
            },
            restored_abandonment,
        })
    }
}

impl ClientParticipantAggregate {
    /// Captures every durable client fact in an inert resume record.
    ///
    /// # Errors
    ///
    /// Returns a typed nested-codec or length error if a live typed value cannot
    /// be represented canonically.
    pub fn resume_record(&self) -> Result<ClientResumeRecord, ClientResumeRecordEncodeError> {
        Ok(ClientResumeRecord {
            canonical: encode_aggregate(self)?,
        })
    }
}

impl super::ClientOperationCommit {
    /// Captures the committed successor as a publicly cold-restorable record.
    ///
    /// The caller persists these `LPCR` bytes before releasing the aggregate and
    /// operation with [`Self::into_parts`]. No pending record or speculative
    /// promotion format exists.
    ///
    /// # Errors
    ///
    /// Returns a typed nested-codec or length error before authority release.
    pub fn resume_record(&self) -> Result<ClientResumeRecord, ClientResumeRecordEncodeError> {
        Ok(ClientResumeRecord {
            canonical: encode_aggregate(&self.aggregate)?,
        })
    }
}

pub(super) struct DecodedFacts {
    pub(super) binding: ClientBindingState,
    pub(super) next_operation_authorization: u64,
    pub(super) expected: Option<ExpectedOperationState>,
    pub(super) replay: DetachReplayState,
    pub(super) reconnect_state: ReconnectMachineState,
    pub(super) next_authorization: u64,
    pub(super) reconnect_lost: Option<LostAuthorityTestimony>,
    pub(super) abandonment: Option<RestoredExpectedOperationAbandonment>,
}

fn validate_facts(facts: &DecodedFacts) -> Result<(), ClientResumeRestoreError> {
    if let ClientBindingState::Bound {
        generation,
        binding_epoch,
        ..
    } = facts.binding
        && generation != binding_epoch.capability_generation
    {
        return Err(ClientResumeRestoreError::BindingGenerationMismatch);
    }
    if matches!(
        facts.expected,
        Some(ExpectedOperationState {
            request: ClientRequest::ParticipantAck(_),
            ..
        })
    ) {
        return Err(ClientResumeRestoreError::ContinuousAckOutstanding);
    }
    if facts.expected.as_ref().is_some_and(|expected| {
        expected.authorization == 0 || expected.authorization > facts.next_operation_authorization
    }) {
        return Err(ClientResumeRestoreError::InvalidOperationAuthorization);
    }
    if facts
        .expected
        .as_ref()
        .is_some_and(|expected| !facts.binding.accepts_request(&expected.request))
    {
        return Err(ClientResumeRestoreError::ExpectedBindingMismatch);
    }
    let active_replay = match &facts.replay {
        DetachReplayState::Recorded { request, status }
            if matches!(
                status,
                DetachReplayStatus::Parked | DetachReplayStatus::InFlight
            ) =>
        {
            Some((request, status))
        }
        DetachReplayState::Empty | DetachReplayState::Recorded { .. } => None,
    };
    let expected_detach = facts.expected.as_ref().and_then(|expected| {
        let ClientRequest::Detach(value) = &expected.request else {
            return None;
        };
        Some((value, expected.issued))
    });
    match (active_replay, expected_detach) {
        (Some((request, status)), Some((value, issued)))
            if value.conversation_id == request.conversation_id
                && value.participant_id == request.participant_id
                && value.capability_generation == request.capability_generation
                && value.detach_attempt_token == request.detach_attempt_token
                && ((matches!(status, DetachReplayStatus::Parked) && !issued)
                    || (matches!(status, DetachReplayStatus::InFlight) && issued)) => {}
        (Some(_), _) => {
            return Err(ClientResumeRestoreError::ActiveReplayExpectedDetachMismatch);
        }
        (None, Some(_)) => {
            return Err(ClientResumeRestoreError::ExpectedDetachActiveReplayMismatch);
        }
        (None, None) => {}
    }
    if let DetachReplayState::Recorded {
        request,
        status: DetachReplayStatus::Terminal(terminal),
    } = &facts.replay
        && !terminal_matches(request, terminal)
    {
        return Err(ClientResumeRestoreError::ReplayTerminalMismatch);
    }
    let authorization = match facts.reconnect_state {
        ReconnectMachineState::Permit { authorization, .. }
        | ReconnectMachineState::Attempt { authorization, .. } => Some(authorization),
        ReconnectMachineState::Parked | ReconnectMachineState::Online => None,
    };
    if authorization.is_some_and(|value| value == 0 || value > facts.next_authorization) {
        return Err(ClientResumeRestoreError::InvalidReconnectAuthorization);
    }
    validate_testimony_coupling(facts)?;
    Ok(())
}

/// Enforces both coupling directions for the serialized loss atoms: an atom
/// whose slot or state does not imply the recorded destruction is refused, and
/// a pending abandonment can never coexist with the tokenless expected
/// operation that would mint a second one (r2, 2026-07-18).
fn validate_testimony_coupling(facts: &DecodedFacts) -> Result<(), ClientResumeRestoreError> {
    if let Some(expected) = facts.expected.as_ref()
        && let Some(testimony) = expected.lost.as_ref()
    {
        let tokenless = matches!(expected.request, ClientRequest::ObserverRecovery(_));
        let expected_kind = if matches!(expected.request, ClientRequest::Detach(_)) {
            LostAuthorityKind::DetachTransportAttempt
        } else {
            LostAuthorityKind::IssuedOperationCorrelation
        };
        if !expected.issued || tokenless || testimony.kind() != expected_kind {
            return Err(ClientResumeRestoreError::LostAuthorityTestimonyMismatch);
        }
    }
    if let Some(testimony) = facts.reconnect_lost.as_ref() {
        let state_kind = match facts.reconnect_state {
            ReconnectMachineState::Permit { issued: true, .. } => {
                Some(LostAuthorityKind::ReconnectPermit)
            }
            ReconnectMachineState::Attempt { .. } => Some(LostAuthorityKind::ReconnectAttempt),
            ReconnectMachineState::Parked
            | ReconnectMachineState::Permit { issued: false, .. }
            | ReconnectMachineState::Online => None,
        };
        if state_kind != Some(testimony.kind()) {
            return Err(ClientResumeRestoreError::LostAuthorityTestimonyMismatch);
        }
    }
    if facts.abandonment.is_some()
        && facts
            .expected
            .as_ref()
            .is_some_and(|expected| matches!(expected.request, ClientRequest::ObserverRecovery(_)))
    {
        return Err(ClientResumeRestoreError::PendingAbandonmentConflict);
    }
    Ok(())
}

fn terminal_matches(
    request: &crate::wire::DetachEnvelope,
    terminal: &DetachReplayTerminal,
) -> bool {
    match terminal {
        DetachReplayTerminal::DetachCommitted(value) => {
            value.conversation_id() == request.conversation_id
                && value.participant_id() == request.participant_id
                && value.capability_generation() == request.capability_generation
                && value.detach_attempt_token() == request.detach_attempt_token
        }
        DetachReplayTerminal::DetachInProgress(value) => {
            let expected_generation = request.capability_generation;
            let presented_generation = value.presented_generation;
            let expected_token = request.detach_attempt_token;
            let presented_token = value.presented_token;
            value.conversation_id == request.conversation_id
                && value.participant_id == request.participant_id
                && presented_generation == expected_generation
                && presented_token == expected_token
        }
        DetachReplayTerminal::TerminalizedDetachCell(value) => {
            value.conversation_id() == request.conversation_id
                && value.participant_id() == request.participant_id
                && value.capability_generation() == request.capability_generation
                && value.detach_attempt_token() == request.detach_attempt_token
        }
    }
}