liminal-server 0.3.0

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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
//! Append-only participant-state repository for the detach/attach replay path.
//!
//! Durable entries contain transition inputs rather than snapshots of protocol
//! state. Opening a repository replays every entry through `liminal-protocol`,
//! so the server never grows a second implementation of lifecycle rules.

use std::sync::Arc;

use liminal::durability::{DurabilityError, DurableStore};
use liminal_protocol::lifecycle::{
    ActiveBinding, AllocatedParticipantSlot, AttachCommitError, AttachCommitParameters,
    AttachSecretProof, AttachVerificationError, AttachedRecordPosition, BindingState,
    CommittedBindingTerminalPosition, DetachCell, DetachCommitError, DetachLookupContext,
    DetachLookupResult, DetachTokenResolution, DetachVerificationError, EnrollmentCommitError,
    EnrollmentCommitParameters, EnrollmentFingerprint, LiveMember, ParticipantSlotAllocationError,
    ParticipantSlotAllocatorProof, PresentedIdentity, ResolvedIdentity, commit_attach,
    commit_detach, commit_enrollment, lookup_detach,
};
use liminal_protocol::wire::{
    AttachAttemptToken, AttachBound, AttachSecret, BindingEpoch, ConnectionIncarnation,
    CredentialAttachRequest, DeliverySeq, DetachAttemptToken, DetachCommitted, DetachRequest,
    DetachStaleAuthority, EnrollBound, EnrollmentRequest, EnrollmentToken, Generation,
    ParticipantId, TerminalizedDetachCell, TransactionOrder,
};
use serde::{Deserialize, Serialize};
use thiserror::Error;

const EVENT_SCHEMA_VERSION: u8 = 1;
const READ_BATCH_SIZE: usize = 64;
const STREAM_PREFIX: &str = "liminal:participant-lifecycle:";

/// Fixed server-side digest used for enrollment-token mappings and detach requests.
pub type ParticipantRequestDigest = [u8; 32];

/// Server allocations committed together with one enrollment transition.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EnrollmentAllocation {
    /// Newly minted generation-one attach secret.
    pub attach_secret: AttachSecret,
    /// New generation-one binding epoch.
    pub origin_binding_epoch: BindingEpoch,
    /// Serialized conversation order of the `Attached` record.
    pub attached_transaction_order: TransactionOrder,
    /// Delivery sequence of the `Attached` record.
    pub attached_delivery_seq: DeliverySeq,
    /// Secret-bearing receipt deadline.
    pub receipt_expires_at: u128,
    /// Non-secret provenance deadline.
    pub provenance_expires_at: u128,
    /// Permanent enrollment-token mapping digest.
    pub enrollment_fingerprint: ParticipantRequestDigest,
}

/// Server allocations committed together with one immediate detach transition.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DetachAllocation {
    /// Canonical non-secret digest of the detach request.
    pub request_verifier: ParticipantRequestDigest,
    /// Exact binding epoch carried by the receiving serialized connection context.
    pub receiving_binding_epoch: BindingEpoch,
    /// Serialized conversation order of the `Detached` terminal.
    pub terminal_transaction_order: TransactionOrder,
    /// Delivery sequence of the `Detached` terminal.
    pub terminal_delivery_seq: DeliverySeq,
}

/// Server allocations committed together with one ordinary detached attach.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OrdinaryAttachAllocation {
    /// Newly authoritative binding epoch.
    pub binding_epoch: BindingEpoch,
    /// Newly minted attach secret.
    pub attach_secret: AttachSecret,
    /// Serialized conversation order of the new `Attached` record.
    pub attached_transaction_order: TransactionOrder,
    /// Delivery sequence of the new `Attached` record.
    pub attached_delivery_seq: DeliverySeq,
    /// Secret-bearing receipt deadline.
    pub receipt_expires_at: u128,
    /// Non-secret provenance deadline.
    pub provenance_expires_at: u128,
}

/// Failure to persist, restore, or apply the supported participant transition path.
#[derive(Debug, Error)]
pub enum ParticipantDetachRepositoryError {
    /// The underlying durable store rejected an operation.
    #[error("participant lifecycle durable-store operation failed: {0}")]
    DurableStore(#[from] DurabilityError),
    /// A durable event could not be encoded or decoded.
    #[error("participant lifecycle event serialization failed: {0}")]
    Serialization(#[from] serde_json::Error),
    /// A durable event uses an unsupported schema version.
    #[error("unsupported participant lifecycle event schema version {0}")]
    EventSchemaVersion(u8),
    /// A durable event carried the forbidden zero generation.
    #[error("participant lifecycle event contains zero generation")]
    ZeroGeneration,
    /// A durable stream contained a gap, duplicate, or reordered event.
    #[error("participant lifecycle stream expected sequence {expected}, found {actual}")]
    EventSequence {
        /// Next sequence required by replay.
        expected: u64,
        /// Sequence read from durable storage.
        actual: u64,
    },
    /// The store returned a different assigned sequence after a successful append.
    #[error("participant lifecycle append expected assigned sequence {expected}, got {actual}")]
    AssignedSequence {
        /// Optimistic-concurrency sequence supplied to the store.
        expected: u64,
        /// Assigned sequence returned by the store.
        actual: u64,
    },
    /// A durable event belongs to a different conversation than its stream.
    #[error(
        "participant lifecycle stream belongs to conversation {stream_conversation}, event names {event_conversation}"
    )]
    ConversationMismatch {
        /// Conversation selected by the repository stream key.
        stream_conversation: u64,
        /// Conversation encoded in the transition event.
        event_conversation: u64,
    },
    /// An enrollment was applied to a non-empty lifecycle stream.
    #[error("enrollment transition requires an empty participant lifecycle stream")]
    EnrollmentState,
    /// A detach was applied before enrollment or without a bound participant.
    #[error("detach transition requires one enrolled, bound participant")]
    DetachState,
    /// An ordinary attach was applied before a committed detach.
    #[error("ordinary attach transition requires one committed detached participant")]
    AttachState,
    /// A lookup was attempted before the participant existed.
    #[error("detach lookup requires one replayed participant")]
    LookupState,
    /// The caller described an invalid permanent participant-slot allocation.
    #[error("participant-slot allocation failed: {0:?}")]
    ParticipantSlotAllocation(ParticipantSlotAllocationError),
    /// The protocol crate rejected enrollment transition inputs.
    #[error("protocol enrollment transition failed: {0:?}")]
    EnrollmentTransition(EnrollmentCommitError),
    /// The protocol crate rejected detach authority.
    #[error("protocol detach verification failed: {0:?}")]
    DetachVerification(DetachVerificationError),
    /// The protocol crate's ordered detach lookup did not authorize the request.
    #[error("protocol detach lookup did not authorize a fresh bound request")]
    DetachLookupRejected,
    /// The protocol crate rejected a committed detach transition.
    #[error("protocol detach transition failed: {0:?}")]
    DetachTransition(DetachCommitError),
    /// The protocol crate rejected ordinary-attach authority.
    #[error("protocol attach verification failed: {0:?}")]
    AttachVerification(AttachVerificationError),
    /// The protocol crate rejected a committed ordinary attach transition.
    #[error("protocol attach transition failed: {0:?}")]
    AttachTransition(AttachCommitError),
    /// A clear closure unexpectedly failed to authorize ordinary detached attach.
    #[error("clear closure did not authorize ordinary detached attach")]
    ClearClosureAdmission,
    /// Exact lookup did not select the terminalized detach-cell outcome.
    #[error("exact detach lookup did not resolve a terminalized detach cell")]
    NotTerminalizedDetach,
}

/// Append-only repository for one conversation's participant detach replay state.
///
/// This first binding intentionally covers the mandatory enrollment, immediate
/// committed detach, ordinary attach, and exact old-token lookup path. Each
/// mutating method appends and flushes exactly one event with an optimistic
/// stream-head precondition before publishing its outcome. No protocol state
/// is cached or serialized.
#[derive(Debug)]
pub struct ParticipantDetachRepository {
    store: Arc<dyn DurableStore>,
    conversation_id: u64,
    stream_key: String,
}

impl ParticipantDetachRepository {
    /// Creates a stateless repository handle over one durable conversation stream.
    #[must_use]
    pub fn new(store: Arc<dyn DurableStore>, conversation_id: u64) -> Self {
        Self {
            store,
            conversation_id,
            stream_key: format!("{STREAM_PREFIX}{conversation_id}"),
        }
    }

    /// Returns the namespaced append-only stream key used by this repository.
    #[must_use]
    pub fn stream_key(&self) -> &str {
        &self.stream_key
    }

    /// Commits initial enrollment and appends its complete transition inputs.
    ///
    /// The live allocator proof is consumed by the initial transition. Durable
    /// replay uses the exact allocation facts stored in that successful event.
    ///
    /// # Errors
    ///
    /// Returns [`ParticipantDetachRepositoryError`] for existing state, invalid
    /// allocation or protocol inputs, serialization failure, or store failure.
    pub async fn commit_enrollment<P>(
        &self,
        request: EnrollmentRequest,
        allocator_proof: P,
        allocation: EnrollmentAllocation,
    ) -> Result<EnrollBound, ParticipantDetachRepositoryError>
    where
        P: ParticipantSlotAllocatorProof,
    {
        let event = StoredTransition::enrollment(&request, &allocator_proof, allocation);
        self.validate_event_conversation(&event.transition)?;
        let replayed = self.replay().await?;
        let (_, outcome) =
            apply_enrollment_with_proof(&replayed.state, &event.transition, allocator_proof)?;
        self.append_transition(&event, replayed.next_sequence)
            .await?;
        Ok(outcome)
    }

    /// Commits an immediate clean detach and appends its complete transition inputs.
    ///
    /// # Errors
    ///
    /// Returns [`ParticipantDetachRepositoryError`] when replay does not produce
    /// a bound participant, protocol verification fails, or persistence fails.
    pub async fn commit_detach(
        &self,
        request: DetachRequest,
        allocation: DetachAllocation,
    ) -> Result<DetachCommitted, ParticipantDetachRepositoryError> {
        let event = StoredTransition::detach(&request, allocation);
        self.validate_event_conversation(&event.transition)?;
        let replayed = self.replay().await?;
        let (_, outcome) = apply_transition(replayed.state, &event.transition)?;
        let TransitionOutcome::Detach(outcome) = outcome else {
            return Err(ParticipantDetachRepositoryError::DetachState);
        };
        self.append_transition(&event, replayed.next_sequence)
            .await?;
        Ok(outcome)
    }

    /// Commits a clear-closure ordinary attach after a committed detach.
    ///
    /// The resulting protocol transition changes `Committed` to `Terminalized`
    /// atomically with the new binding and `Attached` record.
    ///
    /// # Errors
    ///
    /// Returns [`ParticipantDetachRepositoryError`] when replay does not produce
    /// a committed detached member, protocol verification fails, or persistence fails.
    pub async fn commit_ordinary_attach(
        &self,
        request: CredentialAttachRequest,
        secret_proof: AttachSecretProof,
        allocation: OrdinaryAttachAllocation,
    ) -> Result<AttachBound, ParticipantDetachRepositoryError> {
        let event = StoredTransition::ordinary_attach(&request, secret_proof, allocation);
        self.validate_event_conversation(&event.transition)?;
        let replayed = self.replay().await?;
        let (_, outcome) = apply_transition(replayed.state, &event.transition)?;
        let TransitionOutcome::Attach(outcome) = outcome else {
            return Err(ParticipantDetachRepositoryError::AttachState);
        };
        self.append_transition(&event, replayed.next_sequence)
            .await?;
        Ok(outcome)
    }

    /// Replays durable state and performs the crate's exact detach lookup order.
    ///
    /// The response can be returned only when replay produced the mandated
    /// terminalized cell and the crate selected its exact-token stale-authority
    /// arm. The server never constructs [`TerminalizedDetachCell`] itself.
    ///
    /// # Errors
    ///
    /// Returns [`ParticipantDetachRepositoryError::NotTerminalizedDetach`] for
    /// every other lookup classification, including an inexact token or verifier.
    pub async fn exact_terminalized_detach_lookup(
        &self,
        request: &DetachRequest,
        request_verifier: ParticipantRequestDigest,
        receiving_binding_epoch: Option<BindingEpoch>,
        observer_progress: DeliverySeq,
    ) -> Result<TerminalizedDetachCell, ParticipantDetachRepositoryError> {
        let replayed = self.replay().await?;
        let ReplayState::Live(state) = replayed.state else {
            return Err(ParticipantDetachRepositoryError::LookupState);
        };
        let LiveReplayState {
            member,
            binding_state,
            detach_cell,
        } = *state;
        let token_resolution = if replayed.exact_detach_token == Some(request.detach_attempt_token)
        {
            DetachTokenResolution::Exact(ResolvedIdentity::<
                ParticipantRequestDigest,
                ParticipantRequestDigest,
                ParticipantRequestDigest,
            >::Live(&member))
        } else {
            DetachTokenResolution::NoExactMatch
        };
        let lookup = lookup_detach(&DetachLookupContext {
            token_resolution,
            presented_identity: PresentedIdentity::<
                ParticipantRequestDigest,
                ParticipantRequestDigest,
                ParticipantRequestDigest,
            >::Live(&member),
            cell: &detach_cell,
            binding: &binding_state,
            receiving_binding_epoch,
            request,
            request_verifier,
            observer_progress,
        });
        match lookup {
            DetachLookupResult::StaleAuthority(DetachStaleAuthority::TerminalizedDetachCell(
                outcome,
            )) => Ok(outcome),
            DetachLookupResult::Retired(_)
            | DetachLookupResult::ParticipantUnknown(_)
            | DetachLookupResult::StaleAuthority(DetachStaleAuthority::Live { .. })
            | DetachLookupResult::NoBinding(_)
            | DetachLookupResult::PendingReplayRequired(_)
            | DetachLookupResult::DetachInProgress(_)
            | DetachLookupResult::DetachCommitted(_)
            | DetachLookupResult::Authorized { .. } => {
                Err(ParticipantDetachRepositoryError::NotTerminalizedDetach)
            }
        }
    }

    async fn append_transition(
        &self,
        event: &StoredTransition,
        expected_sequence: u64,
    ) -> Result<(), ParticipantDetachRepositoryError> {
        let payload = serde_json::to_vec(event)?;
        let assigned = self
            .store
            .append(&self.stream_key, payload, expected_sequence)
            .await?;
        if assigned != expected_sequence {
            return Err(ParticipantDetachRepositoryError::AssignedSequence {
                expected: expected_sequence,
                actual: assigned,
            });
        }
        self.store.flush().await?;
        Ok(())
    }

    const fn validate_event_conversation(
        &self,
        transition: &DurableTransition,
    ) -> Result<(), ParticipantDetachRepositoryError> {
        let event_conversation = transition.conversation_id();
        if event_conversation != self.conversation_id {
            return Err(ParticipantDetachRepositoryError::ConversationMismatch {
                stream_conversation: self.conversation_id,
                event_conversation,
            });
        }
        Ok(())
    }

    async fn replay(&self) -> Result<ReplayedRepository, ParticipantDetachRepositoryError> {
        let mut state = ReplayState::Empty;
        let mut next_sequence = 0_u64;
        let mut exact_detach_token = None;
        loop {
            let entries = self
                .store
                .read_from(&self.stream_key, next_sequence, READ_BATCH_SIZE)
                .await?;
            if entries.is_empty() {
                break;
            }
            let entry_count = entries.len();
            for entry in entries {
                if entry.sequence != next_sequence {
                    return Err(ParticipantDetachRepositoryError::EventSequence {
                        expected: next_sequence,
                        actual: entry.sequence,
                    });
                }
                let event: StoredTransition = serde_json::from_slice(&entry.payload)?;
                if event.schema_version != EVENT_SCHEMA_VERSION {
                    return Err(ParticipantDetachRepositoryError::EventSchemaVersion(
                        event.schema_version,
                    ));
                }
                self.validate_event_conversation(&event.transition)?;
                if let DurableTransition::Detach { request, .. } = &event.transition {
                    exact_detach_token = Some(DetachAttemptToken::new(request.token));
                }
                (state, _) = apply_transition(state, &event.transition)?;
                next_sequence = next_sequence.checked_add(1).ok_or(
                    ParticipantDetachRepositoryError::EventSequence {
                        expected: u64::MAX,
                        actual: entry.sequence,
                    },
                )?;
            }
            if entry_count < READ_BATCH_SIZE {
                break;
            }
        }
        Ok(ReplayedRepository {
            state,
            next_sequence,
            exact_detach_token,
        })
    }
}

#[derive(Debug)]
struct ReplayedRepository {
    state: ReplayState,
    next_sequence: u64,
    exact_detach_token: Option<DetachAttemptToken>,
}

#[derive(Debug)]
enum ReplayState {
    Empty,
    Live(Box<LiveReplayState>),
}

#[derive(Debug)]
struct LiveReplayState {
    member: LiveMember<ParticipantRequestDigest>,
    binding_state: BindingState,
    detach_cell: DetachCell<ParticipantRequestDigest>,
}

#[derive(Debug)]
enum TransitionOutcome {
    Enrollment,
    Detach(DetachCommitted),
    Attach(AttachBound),
}

fn apply_transition(
    state: ReplayState,
    transition: &DurableTransition,
) -> Result<(ReplayState, TransitionOutcome), ParticipantDetachRepositoryError> {
    match transition {
        DurableTransition::Enrollment { allocator, .. } => {
            let proof = ReplayedParticipantSlot {
                conversation_id: allocator.conversation_id,
                participant_id: allocator.participant_id,
                identity_limit: allocator.identity_limit,
            };
            let (next_state, _outcome) = apply_enrollment_with_proof(&state, transition, proof)?;
            Ok((next_state, TransitionOutcome::Enrollment))
        }
        DurableTransition::Detach { .. } => apply_detach_transition(state, transition),
        DurableTransition::OrdinaryAttach { .. } => apply_attach_transition(state, transition),
    }
}

fn apply_detach_transition(
    state: ReplayState,
    transition: &DurableTransition,
) -> Result<(ReplayState, TransitionOutcome), ParticipantDetachRepositoryError> {
    let DurableTransition::Detach {
        request,
        request_verifier,
        receiving_binding_epoch,
        terminal_transaction_order,
        terminal_delivery_seq,
    } = transition
    else {
        return Err(ParticipantDetachRepositoryError::DetachState);
    };
    let ReplayState::Live(state) = state else {
        return Err(ParticipantDetachRepositoryError::DetachState);
    };
    let LiveReplayState {
        member,
        binding_state,
        detach_cell,
    } = *state;
    let request = request.to_detach_request()?;
    let receiving_binding_epoch = receiving_binding_epoch.to_binding_epoch()?;
    let binding = {
        let lookup = lookup_detach(&DetachLookupContext {
            token_resolution: DetachTokenResolution::<
                ParticipantRequestDigest,
                ParticipantRequestDigest,
                ParticipantRequestDigest,
            >::NoExactMatch,
            presented_identity: PresentedIdentity::<
                ParticipantRequestDigest,
                ParticipantRequestDigest,
                ParticipantRequestDigest,
            >::Live(&member),
            cell: &detach_cell,
            binding: &binding_state,
            receiving_binding_epoch: Some(receiving_binding_epoch),
            request: &request,
            request_verifier: *request_verifier,
            observer_progress: 0,
        });
        let DetachLookupResult::Authorized { binding, .. } = lookup else {
            return Err(ParticipantDetachRepositoryError::DetachLookupRejected);
        };
        binding
    };
    let verified = binding
        .verify_detach_request(request, *request_verifier)
        .map_err(ParticipantDetachRepositoryError::DetachVerification)?;
    let committed = commit_detach(
        member,
        verified,
        detach_cell,
        CommittedBindingTerminalPosition::new(*terminal_transaction_order, *terminal_delivery_seq),
    )
    .map_err(ParticipantDetachRepositoryError::DetachTransition)?;
    let (member, _terminal, binding_state, cell, outcome) = committed.into_parts();
    Ok((
        ReplayState::Live(Box::new(LiveReplayState {
            member,
            binding_state,
            detach_cell: DetachCell::Committed(cell),
        })),
        TransitionOutcome::Detach(outcome),
    ))
}

fn apply_attach_transition(
    state: ReplayState,
    transition: &DurableTransition,
) -> Result<(ReplayState, TransitionOutcome), ParticipantDetachRepositoryError> {
    let DurableTransition::OrdinaryAttach {
        request,
        secret_proof,
        allocation,
    } = transition
    else {
        return Err(ParticipantDetachRepositoryError::AttachState);
    };
    let ReplayState::Live(state) = state else {
        return Err(ParticipantDetachRepositoryError::AttachState);
    };
    let LiveReplayState {
        member,
        binding_state,
        detach_cell,
    } = *state;
    if !matches!(detach_cell, DetachCell::Committed(_)) {
        return Err(ParticipantDetachRepositoryError::AttachState);
    }
    let closure_admission = liminal_protocol::lifecycle::ClosureState::Clear
        .ordinary_detached_attach_admission()
        .map_err(|_| ParticipantDetachRepositoryError::ClearClosureAdmission)?;
    let request = request.to_attach_request()?;
    let binding = ActiveBinding {
        participant_id: request.participant_id,
        conversation_id: request.conversation_id,
        binding_epoch: allocation.binding_epoch.to_binding_epoch()?,
    };
    let verified = member
        .verify_detached_attach(
            binding_state,
            closure_admission,
            request,
            (*secret_proof).into(),
            AttachCommitParameters {
                binding,
                attach_secret: AttachSecret::new(allocation.attach_secret),
                attached_position: AttachedRecordPosition::new(
                    allocation.attached_transaction_order,
                    allocation.attached_delivery_seq,
                ),
                receipt_expires_at: allocation.receipt_expires_at.get(),
                provenance_expires_at: allocation.provenance_expires_at.get(),
            },
        )
        .map_err(ParticipantDetachRepositoryError::AttachVerification)?;
    let committed = commit_attach(verified, detach_cell)
        .map_err(ParticipantDetachRepositoryError::AttachTransition)?;
    let outcome = committed.outcome;
    Ok((
        ReplayState::Live(Box::new(LiveReplayState {
            member: committed.member,
            binding_state: committed.binding_state,
            detach_cell: committed.detach_cell,
        })),
        TransitionOutcome::Attach(outcome),
    ))
}

fn apply_enrollment_with_proof<P>(
    state: &ReplayState,
    transition: &DurableTransition,
    allocator_proof: P,
) -> Result<(ReplayState, EnrollBound), ParticipantDetachRepositoryError>
where
    P: ParticipantSlotAllocatorProof,
{
    let ReplayState::Empty = state else {
        return Err(ParticipantDetachRepositoryError::EnrollmentState);
    };
    let DurableTransition::Enrollment {
        request,
        allocation,
        ..
    } = transition
    else {
        return Err(ParticipantDetachRepositoryError::EnrollmentState);
    };
    let allocated_slot = AllocatedParticipantSlot::from_allocator(allocator_proof)
        .map_err(ParticipantDetachRepositoryError::ParticipantSlotAllocation)?;
    let request = request.to_enrollment_request();
    let committed = commit_enrollment(
        &request,
        EnrollmentCommitParameters {
            allocated_slot,
            attach_secret: AttachSecret::new(allocation.attach_secret),
            origin_binding_epoch: allocation.origin_binding_epoch.to_binding_epoch()?,
            attached_position: AttachedRecordPosition::new(
                allocation.attached_transaction_order,
                allocation.attached_delivery_seq,
            ),
            receipt_expires_at: allocation.receipt_expires_at.get(),
            provenance_expires_at: allocation.provenance_expires_at.get(),
            enrollment_fingerprint: EnrollmentFingerprint::new(allocation.enrollment_fingerprint),
        },
    )
    .map_err(ParticipantDetachRepositoryError::EnrollmentTransition)?;
    Ok((
        ReplayState::Live(Box::new(LiveReplayState {
            member: committed.member,
            binding_state: committed.binding_state,
            detach_cell: DetachCell::default(),
        })),
        committed.outcome,
    ))
}

#[derive(Clone, Copy, Debug)]
struct ReplayedParticipantSlot {
    conversation_id: u64,
    participant_id: ParticipantId,
    identity_limit: u64,
}

impl ParticipantSlotAllocatorProof for ReplayedParticipantSlot {
    fn conversation_id(&self) -> u64 {
        self.conversation_id
    }

    fn participant_index(&self) -> u64 {
        self.participant_id
    }

    fn identity_limit(&self) -> u64 {
        self.identity_limit
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct StoredTransition {
    schema_version: u8,
    transition: DurableTransition,
}

impl StoredTransition {
    fn enrollment<P: ParticipantSlotAllocatorProof>(
        request: &EnrollmentRequest,
        allocator: &P,
        allocation: EnrollmentAllocation,
    ) -> Self {
        Self {
            schema_version: EVENT_SCHEMA_VERSION,
            transition: DurableTransition::Enrollment {
                request: StoredEnrollmentRequest::from(request),
                allocator: StoredAllocator {
                    conversation_id: allocator.conversation_id(),
                    participant_id: allocator.participant_index(),
                    identity_limit: allocator.identity_limit(),
                },
                allocation: StoredEnrollmentAllocation::from(allocation),
            },
        }
    }

    fn detach(request: &DetachRequest, allocation: DetachAllocation) -> Self {
        Self {
            schema_version: EVENT_SCHEMA_VERSION,
            transition: DurableTransition::Detach {
                request: StoredDetachRequest::from(request),
                request_verifier: allocation.request_verifier,
                receiving_binding_epoch: allocation.receiving_binding_epoch.into(),
                terminal_transaction_order: allocation.terminal_transaction_order,
                terminal_delivery_seq: allocation.terminal_delivery_seq,
            },
        }
    }

    fn ordinary_attach(
        request: &CredentialAttachRequest,
        secret_proof: AttachSecretProof,
        allocation: OrdinaryAttachAllocation,
    ) -> Self {
        Self {
            schema_version: EVENT_SCHEMA_VERSION,
            transition: DurableTransition::OrdinaryAttach {
                request: StoredAttachRequest::from(request),
                secret_proof: secret_proof.into(),
                allocation: StoredAttachAllocation::from(allocation),
            },
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case", tag = "transition")]
enum DurableTransition {
    Enrollment {
        request: StoredEnrollmentRequest,
        allocator: StoredAllocator,
        allocation: StoredEnrollmentAllocation,
    },
    Detach {
        request: StoredDetachRequest,
        request_verifier: ParticipantRequestDigest,
        receiving_binding_epoch: StoredBindingEpoch,
        terminal_transaction_order: TransactionOrder,
        terminal_delivery_seq: DeliverySeq,
    },
    OrdinaryAttach {
        request: StoredAttachRequest,
        secret_proof: StoredAttachSecretProof,
        allocation: StoredAttachAllocation,
    },
}

impl DurableTransition {
    const fn conversation_id(&self) -> u64 {
        match self {
            Self::Enrollment { request, .. } => request.conversation_id,
            Self::Detach { request, .. } => request.conversation_id,
            Self::OrdinaryAttach { request, .. } => request.conversation_id,
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct StoredEnrollmentRequest {
    conversation_id: u64,
    token: [u8; 16],
}

impl From<&EnrollmentRequest> for StoredEnrollmentRequest {
    fn from(request: &EnrollmentRequest) -> Self {
        Self {
            conversation_id: request.conversation_id,
            token: request.enrollment_token.into_bytes(),
        }
    }
}

impl StoredEnrollmentRequest {
    const fn to_enrollment_request(self) -> EnrollmentRequest {
        EnrollmentRequest {
            conversation_id: self.conversation_id,
            enrollment_token: EnrollmentToken::new(self.token),
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct StoredDetachRequest {
    conversation_id: u64,
    participant_id: ParticipantId,
    capability_generation: u64,
    token: [u8; 16],
}

impl From<&DetachRequest> for StoredDetachRequest {
    fn from(request: &DetachRequest) -> Self {
        Self {
            conversation_id: request.conversation_id,
            participant_id: request.participant_id,
            capability_generation: request.capability_generation.get(),
            token: request.detach_attempt_token.into_bytes(),
        }
    }
}

impl StoredDetachRequest {
    fn to_detach_request(self) -> Result<DetachRequest, ParticipantDetachRepositoryError> {
        Ok(DetachRequest {
            conversation_id: self.conversation_id,
            participant_id: self.participant_id,
            capability_generation: generation(self.capability_generation)?,
            detach_attempt_token: DetachAttemptToken::new(self.token),
        })
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct StoredAttachRequest {
    conversation_id: u64,
    participant_id: ParticipantId,
    capability_generation: u64,
    attach_secret: [u8; 32],
    token: [u8; 16],
    accept_marker_delivery_seq: Option<DeliverySeq>,
}

impl From<&CredentialAttachRequest> for StoredAttachRequest {
    fn from(request: &CredentialAttachRequest) -> Self {
        Self {
            conversation_id: request.conversation_id,
            participant_id: request.participant_id,
            capability_generation: request.capability_generation.get(),
            attach_secret: request.attach_secret.into_bytes(),
            token: request.attach_attempt_token.into_bytes(),
            accept_marker_delivery_seq: request.accept_marker_delivery_seq,
        }
    }
}

impl StoredAttachRequest {
    fn to_attach_request(
        self,
    ) -> Result<CredentialAttachRequest, ParticipantDetachRepositoryError> {
        Ok(CredentialAttachRequest {
            conversation_id: self.conversation_id,
            participant_id: self.participant_id,
            capability_generation: generation(self.capability_generation)?,
            attach_secret: AttachSecret::new(self.attach_secret),
            attach_attempt_token: AttachAttemptToken::new(self.token),
            accept_marker_delivery_seq: self.accept_marker_delivery_seq,
        })
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct StoredAllocator {
    conversation_id: u64,
    participant_id: ParticipantId,
    identity_limit: u64,
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct StoredBindingEpoch {
    server_incarnation: u64,
    connection_ordinal: u64,
    capability_generation: u64,
}

impl From<BindingEpoch> for StoredBindingEpoch {
    fn from(epoch: BindingEpoch) -> Self {
        Self {
            server_incarnation: epoch.connection_incarnation.server_incarnation,
            connection_ordinal: epoch.connection_incarnation.connection_ordinal,
            capability_generation: epoch.capability_generation.get(),
        }
    }
}

impl StoredBindingEpoch {
    fn to_binding_epoch(self) -> Result<BindingEpoch, ParticipantDetachRepositoryError> {
        Ok(BindingEpoch::new(
            ConnectionIncarnation::new(self.server_incarnation, self.connection_ordinal),
            generation(self.capability_generation)?,
        ))
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct StoredEnrollmentAllocation {
    attach_secret: [u8; 32],
    origin_binding_epoch: StoredBindingEpoch,
    attached_transaction_order: TransactionOrder,
    attached_delivery_seq: DeliverySeq,
    receipt_expires_at: StoredU128,
    provenance_expires_at: StoredU128,
    enrollment_fingerprint: ParticipantRequestDigest,
}

impl From<EnrollmentAllocation> for StoredEnrollmentAllocation {
    fn from(allocation: EnrollmentAllocation) -> Self {
        Self {
            attach_secret: allocation.attach_secret.into_bytes(),
            origin_binding_epoch: allocation.origin_binding_epoch.into(),
            attached_transaction_order: allocation.attached_transaction_order,
            attached_delivery_seq: allocation.attached_delivery_seq,
            receipt_expires_at: allocation.receipt_expires_at.into(),
            provenance_expires_at: allocation.provenance_expires_at.into(),
            enrollment_fingerprint: allocation.enrollment_fingerprint,
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct StoredAttachAllocation {
    binding_epoch: StoredBindingEpoch,
    attach_secret: [u8; 32],
    attached_transaction_order: TransactionOrder,
    attached_delivery_seq: DeliverySeq,
    receipt_expires_at: StoredU128,
    provenance_expires_at: StoredU128,
}

impl From<OrdinaryAttachAllocation> for StoredAttachAllocation {
    fn from(allocation: OrdinaryAttachAllocation) -> Self {
        Self {
            binding_epoch: allocation.binding_epoch.into(),
            attach_secret: allocation.attach_secret.into_bytes(),
            attached_transaction_order: allocation.attached_transaction_order,
            attached_delivery_seq: allocation.attached_delivery_seq,
            receipt_expires_at: allocation.receipt_expires_at.into(),
            provenance_expires_at: allocation.provenance_expires_at.into(),
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
enum StoredAttachSecretProof {
    Mismatch,
    Verified,
}

impl From<AttachSecretProof> for StoredAttachSecretProof {
    fn from(proof: AttachSecretProof) -> Self {
        match proof {
            AttachSecretProof::Mismatch => Self::Mismatch,
            AttachSecretProof::Verified => Self::Verified,
        }
    }
}

impl From<StoredAttachSecretProof> for AttachSecretProof {
    fn from(proof: StoredAttachSecretProof) -> Self {
        match proof {
            StoredAttachSecretProof::Mismatch => Self::Mismatch,
            StoredAttachSecretProof::Verified => Self::Verified,
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct StoredU128([u8; 16]);

impl StoredU128 {
    const fn get(self) -> u128 {
        u128::from_be_bytes(self.0)
    }
}

impl From<u128> for StoredU128 {
    fn from(value: u128) -> Self {
        Self(value.to_be_bytes())
    }
}

fn generation(value: u64) -> Result<Generation, ParticipantDetachRepositoryError> {
    Generation::new(value).ok_or(ParticipantDetachRepositoryError::ZeroGeneration)
}