bombay-machine-executor 0.1.0

Ordered concurrent execution for composable representable machines.
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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
//! Concurrent execution policies for pure representable machines.
//!
//! [`ExclusiveExecutor`] directly returns outputs to a caller that serializes
//! turns through exclusive access. [`SerializedExecutor`] provides
//! run-to-completion turns: it queues inputs
//! and does not advance the next transition until the preceding output handler
//! returns. [`LinearizedExecutor`] advances inputs immediately under its lock,
//! then dispatches already-ordered outputs. The latter policy is appropriate
//! only when transition linearization may precede completion of earlier work.

#![deny(missing_docs)]

#[cfg(loom)]
use loom::sync::{Arc, Condvar, Mutex};
use std::collections::VecDeque;
#[cfg(not(loom))]
use std::sync::{Arc, Condvar, Mutex};

pub use bombay_transition::Machine;

#[derive(Debug)]
enum ExclusiveSeat<M> {
    Ready(M),
    Poisoned,
}

/// Observable state of an [`ExclusiveExecutor`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExclusiveState {
    /// The successor machine is available for another turn.
    Ready,
    /// A machine transition panicked and consumed the previous machine.
    Poisoned,
}

/// Failure to recover a machine consumed by a panicking transition.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("executor was poisoned by a previous panic")]
pub struct ExclusivePoisoned;

/// Allocation-free execution of an affine machine through exclusive access.
///
/// A turn installs the poisoned state before calling [`Machine::step`]. If the
/// transition unwinds, no machine remains accessible and all future inputs are
/// rejected intact. Output consumption after a successful turn is outside this
/// poison boundary.
#[derive(Debug)]
pub struct ExclusiveExecutor<M: Machine> {
    seat: ExclusiveSeat<M>,
}

impl<M: Machine> ExclusiveExecutor<M> {
    /// Construct an executor containing `machine`.
    #[must_use]
    pub const fn new(machine: M) -> Self {
        Self {
            seat: ExclusiveSeat::Ready(machine),
        }
    }

    /// Execute one immediate turn and install its successor.
    ///
    /// # Errors
    ///
    /// Returns the supplied input without accepting it if an earlier transition
    /// poisoned the executor. An input consumed by a transition that panics is
    /// not recoverable.
    ///
    /// # Panics
    ///
    /// Propagates a panic from [`Machine::step`] after poisoning the executor.
    pub fn turn(&mut self, input: M::Input) -> Result<M::Output, PoisonedInput<M::Input>> {
        let machine = match core::mem::replace(&mut self.seat, ExclusiveSeat::Poisoned) {
            ExclusiveSeat::Ready(machine) => machine,
            ExclusiveSeat::Poisoned => return Err(PoisonedInput(input)),
        };
        let (output, successor) = machine.step(input);
        self.seat = ExclusiveSeat::Ready(successor);
        Ok(output)
    }

    /// Report whether a successor machine remains available.
    #[must_use]
    pub const fn state(&self) -> ExclusiveState {
        match &self.seat {
            ExclusiveSeat::Ready(_) => ExclusiveState::Ready,
            ExclusiveSeat::Poisoned => ExclusiveState::Poisoned,
        }
    }

    /// Borrow the current successor machine, or `None` if it was poisoned.
    #[must_use]
    pub const fn machine(&self) -> Option<&M> {
        match &self.seat {
            ExclusiveSeat::Ready(machine) => Some(machine),
            ExclusiveSeat::Poisoned => None,
        }
    }

    /// Recover the current successor machine.
    ///
    /// # Errors
    ///
    /// Returns [`ExclusivePoisoned`] if a panicking transition consumed it.
    pub fn into_inner(self) -> Result<M, ExclusivePoisoned> {
        match self.seat {
            ExclusiveSeat::Ready(machine) => Ok(machine),
            ExclusiveSeat::Poisoned => Err(ExclusivePoisoned),
        }
    }
}

/// Handles one machine output synchronously.
pub trait OutputHandler<O> {
    /// Handle the complete output of one transition.
    fn handle(&self, output: O);
}

impl<O, F> OutputHandler<O> for F
where
    F: Fn(O),
{
    fn handle(&self, output: O) {
        self(output);
    }
}

/// Extracts small copyable evidence before an output is queued for dispatch.
pub trait OutputEvidence {
    /// Evidence returned to the submitting caller.
    type Evidence;

    /// Extract evidence without consuming the output.
    fn evidence(&self) -> Self::Evidence;
}

/// Result of waiting for a serialized turn.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TurnOutcome {
    /// The transition and its complete synchronous output handling finished.
    Completed,
    /// The executor was poisoned by a transition or output-handler panic.
    Poisoned,
}

/// Completion receipt for one serialized input.
pub struct TurnReceipt(Arc<TurnCompletion>);

struct TurnCompletion {
    outcome: Mutex<Option<TurnOutcome>>,
    ready: Condvar,
}

impl TurnReceipt {
    /// Return the outcome without blocking, if the turn has finished.
    ///
    /// # Panics
    ///
    /// Panics if receipt synchronization was poisoned.
    #[must_use]
    pub fn outcome(&self) -> Option<TurnOutcome> {
        *self.0.outcome.lock().expect("turn receipt lock poisoned")
    }

    /// Block until the turn completes or its executor is poisoned.
    ///
    /// # Panics
    ///
    /// Panics if receipt synchronization was poisoned.
    #[must_use]
    pub fn wait(self) -> TurnOutcome {
        let mut outcome = self.0.outcome.lock().expect("turn receipt lock poisoned");
        loop {
            if let Some(outcome) = *outcome {
                return outcome;
            }
            outcome = self
                .0
                .ready
                .wait(outcome)
                .expect("turn receipt lock poisoned");
        }
    }
}

fn complete(completion: &TurnCompletion, outcome: TurnOutcome) {
    *completion
        .outcome
        .lock()
        .expect("turn receipt lock poisoned") = Some(outcome);
    // wait(self) consumes the receipt, so at most one waiter exists.
    completion.ready.notify_one();
}

/// Rejection of an input after a serialized executor was poisoned.
#[derive(Debug, thiserror::Error)]
#[error("executor was poisoned by a previous panic")]
pub struct PoisonedInput<I>(
    /// Input whose ownership was not accepted.
    pub I,
);

/// Serialized run-to-completion execution of one machine.
pub struct SerializedExecutor<M: Machine> {
    execution: Mutex<SerializedExecution<M>>,
}

struct SerializedExecution<M: Machine> {
    machine: Option<M>,
    inputs: VecDeque<(M::Input, Arc<TurnCompletion>)>,
    turn: TurnState,
}

/// Ownership phase of the serialized turn drain.
enum TurnState {
    /// No caller is draining turns.
    Idle,
    /// One caller owns the drain loop.
    Running,
    /// A transition or handler panic poisoned the executor.
    Poisoned,
}

impl<M: Machine> SerializedExecutor<M> {
    /// Construct a serialized executor with an empty input queue.
    #[must_use]
    pub fn new(machine: M) -> Self {
        Self {
            execution: Mutex::new(SerializedExecution {
                machine: Some(machine),
                inputs: VecDeque::new(),
                turn: TurnState::Idle,
            }),
        }
    }

    /// Queue one input and, when this caller acquires ownership, drain turns.
    ///
    /// Reentrant and concurrent calls enqueue their input and return a receipt;
    /// they never advance a transition while an earlier output is being handled.
    /// Only the drain owner's `handler` processes outputs: a caller that loses
    /// ownership has its input handled by the owner's handler, while its
    /// receipt still reports completion of its own turn. Waiting on a receipt
    /// from inside `handler` would deadlock and must be deferred until the
    /// outer turn returns.
    ///
    /// # Errors
    ///
    /// Returns input ownership when a previous transition or handler panicked.
    ///
    /// # Panics
    ///
    /// Propagates a machine transition or output-handler panic after poisoning
    /// this executor and resolving every outstanding receipt.
    pub fn submit<H>(
        &self,
        input: M::Input,
        handler: &H,
    ) -> Result<TurnReceipt, PoisonedInput<M::Input>>
    where
        H: OutputHandler<M::Output>,
    {
        let completion = Arc::new(TurnCompletion {
            outcome: Mutex::new(None),
            ready: Condvar::new(),
        });
        let owns = {
            let mut execution = self.execution.lock().expect("executor lock poisoned");
            match execution.turn {
                TurnState::Poisoned => return Err(PoisonedInput(input)),
                TurnState::Running => {
                    execution.inputs.push_back((input, Arc::clone(&completion)));
                    false
                }
                TurnState::Idle => {
                    execution.inputs.push_back((input, Arc::clone(&completion)));
                    execution.turn = TurnState::Running;
                    true
                }
            }
        };
        if owns {
            self.drain(handler);
        }
        Ok(TurnReceipt(completion))
    }

    fn drain<H>(&self, handler: &H)
    where
        H: OutputHandler<M::Output>,
    {
        let mut ownership = SerializedOwnership::new(&self.execution);
        loop {
            let Some((machine, input, completion)) = ownership.take_turn() else {
                return;
            };
            let (output, successor) = machine.step(input);
            ownership.install(successor, &completion);
            handler.handle(output);
            complete(&completion, TurnOutcome::Completed);
            ownership.turn_completed();
        }
    }
}

struct SerializedOwnership<'a, M: Machine> {
    execution: Option<&'a Mutex<SerializedExecution<M>>>,
    active: Option<Arc<TurnCompletion>>,
}

impl<'a, M: Machine> SerializedOwnership<'a, M> {
    fn new(execution: &'a Mutex<SerializedExecution<M>>) -> Self {
        Self {
            execution: Some(execution),
            active: None,
        }
    }

    fn take_turn(&mut self) -> Option<(M, M::Input, Arc<TurnCompletion>)> {
        let execution = self.execution?;
        let mut state = execution.lock().expect("executor lock poisoned");
        let Some((input, completion)) = state.inputs.pop_front() else {
            state.turn = TurnState::Idle;
            // Normal exhaustion disarms the guard: dropping it must not poison.
            self.execution = None;
            return None;
        };
        let machine = state.machine.take().expect("executor machine missing");
        self.active = Some(Arc::clone(&completion));
        Some((machine, input, completion))
    }

    fn install(&self, machine: M, completion: &Arc<TurnCompletion>) {
        self.execution
            .expect("ownership armed")
            .lock()
            .expect("executor lock poisoned")
            .machine = Some(machine);
        debug_assert!(Arc::ptr_eq(
            self.active.as_ref().expect("active turn"),
            completion
        ));
    }

    fn turn_completed(&mut self) {
        self.active = None;
    }
}

impl<M: Machine> Drop for SerializedOwnership<'_, M> {
    fn drop(&mut self) {
        let Some(execution) = self.execution.take() else {
            return;
        };
        let mut state = execution
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        state.turn = TurnState::Poisoned;
        if let Some(active) = self.active.take() {
            complete(&active, TurnOutcome::Poisoned);
        }
        state
            .inputs
            .drain(..)
            .for_each(|(_, receipt)| complete(&receipt, TurnOutcome::Poisoned));
    }
}

/// Transition-linearized execution with separately ordered output dispatch.
pub struct LinearizedExecutor<M>
where
    M: Machine,
    M::Output: OutputEvidence,
    <M::Output as OutputEvidence>::Evidence: Clone,
{
    execution: Mutex<LinearizedExecution<M, M::Output, <M::Output as OutputEvidence>::Evidence>>,
}

struct LinearizedExecution<M, O, E> {
    machine: LinearizedMachine<M>,
    outputs: VecDeque<O>,
    evidence: Option<E>,
    dispatch: DispatchState,
}

enum LinearizedMachine<M> {
    Ready(M),
    Poisoned,
}

/// Ownership phase of output dispatch.
enum DispatchState {
    /// No caller is dispatching queued outputs.
    Idle,
    /// One caller owns output dispatch.
    Dispatching,
}

impl<M> LinearizedExecutor<M>
where
    M: Machine,
    M::Output: OutputEvidence,
    <M::Output as OutputEvidence>::Evidence: Clone,
{
    /// Construct an executor with an empty output queue.
    #[must_use]
    pub fn new(machine: M) -> Self {
        Self {
            execution: Mutex::new(LinearizedExecution {
                machine: LinearizedMachine::Ready(machine),
                outputs: VecDeque::new(),
                evidence: None,
                dispatch: DispatchState::Idle,
            }),
        }
    }

    /// Advance and enqueue one output at the same linearization point.
    ///
    /// # Panics
    ///
    /// Panics after synchronization poison or a transition panic that consumed
    /// the affine machine state.
    pub fn submit(&self, input: M::Input) -> <M::Output as OutputEvidence>::Evidence {
        let mut execution = self.execution.lock().expect("executor lock poisoned");
        let LinearizedMachine::Ready(machine) =
            core::mem::replace(&mut execution.machine, LinearizedMachine::Poisoned)
        else {
            panic!("executor machine poisoned");
        };
        let (output, successor) = machine.step(input);
        let evidence = output.evidence();
        execution.evidence = Some(evidence.clone());
        execution.machine = LinearizedMachine::Ready(successor);
        execution.outputs.push_back(output);
        evidence
    }

    /// Clone the evidence installed by the latest linearized transition.
    ///
    /// # Panics
    ///
    /// Panics if executor synchronization was poisoned.
    #[must_use]
    pub fn evidence(&self) -> Option<<M::Output as OutputEvidence>::Evidence> {
        self.execution
            .lock()
            .expect("executor lock poisoned")
            .evidence
            .clone()
    }

    /// Dispatch queued outputs until empty, or contribute them to another owner.
    ///
    /// [`DispatchOutcome::OwnedElsewhere`] means another caller owns dispatch
    /// and this call is fire-and-forget; it does not mean the caller's output
    /// completed. If a handler panics, its owned output is dropped exactly once
    /// and a later call resumes with the remaining queue.
    ///
    /// # Panics
    ///
    /// Panics if executor synchronization was poisoned by a transition panic
    /// in [`LinearizedExecutor::submit`].
    pub fn dispatch_pending<H>(&self, handler: &H) -> DispatchOutcome
    where
        H: OutputHandler<M::Output>,
    {
        let Some(mut ownership) = DispatchOwnership::acquire(&self.execution) else {
            return DispatchOutcome::OwnedElsewhere;
        };
        while let Some(output) = ownership.next() {
            handler.handle(output);
        }
        DispatchOutcome::Drained
    }
}

/// Ownership result of one [`LinearizedExecutor::dispatch_pending`] call.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DispatchOutcome {
    /// This call owned dispatch and drained the output queue.
    Drained,
    /// Another caller owns dispatch; queued outputs will be handled there.
    OwnedElsewhere,
}

struct DispatchOwnership<'a, M, O, E> {
    execution: Option<&'a Mutex<LinearizedExecution<M, O, E>>>,
}

impl<'a, M, O, E> DispatchOwnership<'a, M, O, E> {
    fn acquire(execution: &'a Mutex<LinearizedExecution<M, O, E>>) -> Option<Self> {
        let mut state = execution.lock().expect("executor lock poisoned");
        match state.dispatch {
            DispatchState::Dispatching => None,
            DispatchState::Idle => {
                state.dispatch = DispatchState::Dispatching;
                Some(Self {
                    execution: Some(execution),
                })
            }
        }
    }

    fn next(&mut self) -> Option<O> {
        let execution = self.execution?;
        let mut state = execution.lock().expect("executor lock poisoned");
        let output = state.outputs.pop_front();
        if output.is_none() {
            state.dispatch = DispatchState::Idle;
            // An exhausted queue releases ownership; dropping must not repeat it.
            self.execution = None;
        }
        output
    }
}

impl<M, O, E> Drop for DispatchOwnership<'_, M, O, E> {
    fn drop(&mut self) {
        if let Some(execution) = self.execution.take() {
            // This drop can run while its own `next` unwinds after another
            // thread poisoned the executor; a second panic here would abort
            // the process. Recovering the guard preserves the documented
            // panic-only contract.
            execution
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .dispatch = DispatchState::Idle;
        }
    }
}

#[cfg(all(test, not(loom)))]
mod tests {
    use std::panic::{AssertUnwindSafe, catch_unwind};
    use std::sync::{Arc, Mutex, Weak};

    use bombay_transition::{Base, Topology, Vertex, VertexId};

    use super::{
        ExclusiveExecutor, ExclusiveState, LinearizedExecutor, Machine, OutputEvidence,
        OutputHandler, SerializedExecutor, TurnOutcome, TurnReceipt,
    };

    const VERTICES: &[Vertex] = &[Vertex {
        id: VertexId(0),
        label: "ready",
    }];
    const TOPOLOGY: Topology = Topology {
        name: "test",
        initial: VertexId(0),
        vertices: VERTICES,
        transitions: &[],
    };

    #[derive(Debug)]
    struct Output(u8);

    impl OutputEvidence for Output {
        type Evidence = u8;
        fn evidence(&self) -> Self::Evidence {
            self.0
        }
    }

    fn machine() -> Base<u8, impl FnMut(u8, u8) -> (Output, u8), u8, Output> {
        Base::new(0, TOPOLOGY.validated().unwrap(), |state, input| {
            (Output(input), state + input)
        })
    }

    #[derive(Debug)]
    struct ExclusiveTestMachine {
        state: usize,
        steps: Arc<std::sync::atomic::AtomicUsize>,
        panic: bool,
    }

    #[derive(Debug, PartialEq, Eq)]
    struct OwnedOutput(Box<str>);

    impl Machine for ExclusiveTestMachine {
        type Input = usize;
        type Output = OwnedOutput;

        fn step(self, input: Self::Input) -> (Self::Output, Self) {
            self.steps.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            assert!(!self.panic, "transition failure");
            let successor = Self {
                state: self.state + input,
                steps: self.steps,
                panic: false,
            };
            (OwnedOutput(format!("output-{input}").into()), successor)
        }

        fn describe<V: bombay_transition::Structure>(&self, visitor: &mut V) -> V::Output {
            visitor.base(TOPOLOGY)
        }
    }

    #[test]
    fn exclusive_turn_returns_output_and_installs_successor() {
        let steps = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let mut executor = ExclusiveExecutor::new(ExclusiveTestMachine {
            state: 1,
            steps: Arc::clone(&steps),
            panic: false,
        });

        assert_eq!(executor.state(), ExclusiveState::Ready);
        assert_eq!(executor.machine().unwrap().state, 1);
        assert_eq!(executor.turn(2).unwrap(), OwnedOutput("output-2".into()));
        assert_eq!(executor.machine().unwrap().state, 3);
        assert_eq!(executor.turn(4).unwrap(), OwnedOutput("output-4".into()));
        assert_eq!(executor.into_inner().unwrap().state, 7);
        assert_eq!(steps.load(std::sync::atomic::Ordering::SeqCst), 2);
    }

    #[test]
    fn exclusive_transition_panic_permanently_refuses_later_input() {
        let steps = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let mut executor = ExclusiveExecutor::new(ExclusiveTestMachine {
            state: 0,
            steps: Arc::clone(&steps),
            panic: true,
        });

        assert!(
            catch_unwind(AssertUnwindSafe(|| {
                let _ = executor.turn(1);
            }))
            .is_err()
        );
        assert_eq!(executor.state(), ExclusiveState::Poisoned);
        assert!(executor.machine().is_none());
        let Err(rejected) = executor.turn(9) else {
            panic!("poisoned executor accepted input")
        };
        assert_eq!(rejected.0, 9);
        assert_eq!(steps.load(std::sync::atomic::Ordering::SeqCst), 1);
        assert!(matches!(
            executor.into_inner(),
            Err(super::ExclusivePoisoned)
        ));
    }

    #[test]
    fn exclusive_executor_inherits_machine_auto_traits() {
        const fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<ExclusiveExecutor<ExclusiveTestMachine>>();
    }

    #[derive(Debug)]
    struct DropSentinel(Arc<std::sync::atomic::AtomicUsize>);

    impl Drop for DropSentinel {
        fn drop(&mut self) {
            self.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }
    }

    #[derive(Debug)]
    struct TrackedInput {
        id: usize,
        _drop: DropSentinel,
    }

    #[derive(Debug)]
    struct TrackedOutput {
        id: usize,
        _drop: DropSentinel,
    }

    #[derive(Debug)]
    struct OwnershipMachine {
        panic: bool,
        steps: Arc<std::sync::atomic::AtomicUsize>,
        _machine_drop: DropSentinel,
        successor_drops: Arc<std::sync::atomic::AtomicUsize>,
        output_drops: Arc<std::sync::atomic::AtomicUsize>,
    }

    impl Machine for OwnershipMachine {
        type Input = TrackedInput;
        type Output = TrackedOutput;

        fn step(self, input: Self::Input) -> (Self::Output, Self) {
            self.steps.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            assert!(!self.panic, "transition failure");
            let output = TrackedOutput {
                id: input.id,
                _drop: DropSentinel(Arc::clone(&self.output_drops)),
            };
            drop(input);
            let successor = Self {
                panic: false,
                steps: Arc::clone(&self.steps),
                _machine_drop: DropSentinel(Arc::clone(&self.successor_drops)),
                successor_drops: self.successor_drops,
                output_drops: self.output_drops,
            };
            (output, successor)
        }

        fn describe<V: bombay_transition::Structure>(&self, visitor: &mut V) -> V::Output {
            visitor.base(TOPOLOGY)
        }
    }

    #[test]
    fn exclusive_success_moves_each_owned_payload_exactly_once() {
        let original_machine_drops = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let successor_drops = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let input_drops = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let output_drops = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let steps = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let mut executor = ExclusiveExecutor::new(OwnershipMachine {
            panic: false,
            steps: Arc::clone(&steps),
            _machine_drop: DropSentinel(Arc::clone(&original_machine_drops)),
            successor_drops: Arc::clone(&successor_drops),
            output_drops: Arc::clone(&output_drops),
        });

        let output = executor
            .turn(TrackedInput {
                id: 41,
                _drop: DropSentinel(Arc::clone(&input_drops)),
            })
            .unwrap();
        assert_eq!(output.id, 41);
        assert_eq!(steps.load(std::sync::atomic::Ordering::SeqCst), 1);
        assert_eq!(
            original_machine_drops.load(std::sync::atomic::Ordering::SeqCst),
            1
        );
        assert_eq!(input_drops.load(std::sync::atomic::Ordering::SeqCst), 1);
        assert_eq!(successor_drops.load(std::sync::atomic::Ordering::SeqCst), 0);
        assert_eq!(output_drops.load(std::sync::atomic::Ordering::SeqCst), 0);

        drop(output);
        drop(executor.into_inner().unwrap());
        assert_eq!(output_drops.load(std::sync::atomic::Ordering::SeqCst), 1);
        assert_eq!(successor_drops.load(std::sync::atomic::Ordering::SeqCst), 1);
    }

    #[test]
    fn exclusive_panic_consumes_active_values_but_returns_later_input() {
        let machine_drops = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let accepted_input_drops = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let rejected_input_drops = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let steps = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let mut executor = ExclusiveExecutor::new(OwnershipMachine {
            panic: true,
            steps: Arc::clone(&steps),
            _machine_drop: DropSentinel(Arc::clone(&machine_drops)),
            successor_drops: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
            output_drops: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        });

        assert!(
            catch_unwind(AssertUnwindSafe(|| {
                let _ = executor.turn(TrackedInput {
                    id: 1,
                    _drop: DropSentinel(Arc::clone(&accepted_input_drops)),
                });
            }))
            .is_err()
        );
        assert_eq!(machine_drops.load(std::sync::atomic::Ordering::SeqCst), 1);
        assert_eq!(
            accepted_input_drops.load(std::sync::atomic::Ordering::SeqCst),
            1
        );

        let Err(rejected) = executor.turn(TrackedInput {
            id: 73,
            _drop: DropSentinel(Arc::clone(&rejected_input_drops)),
        }) else {
            panic!("poisoned executor accepted input")
        };
        assert_eq!(rejected.0.id, 73);
        assert_eq!(steps.load(std::sync::atomic::Ordering::SeqCst), 1);
        assert_eq!(
            rejected_input_drops.load(std::sync::atomic::Ordering::SeqCst),
            0
        );
        drop(rejected);
        assert_eq!(
            rejected_input_drops.load(std::sync::atomic::Ordering::SeqCst),
            1
        );
        drop(executor);
        assert_eq!(machine_drops.load(std::sync::atomic::Ordering::SeqCst), 1);
    }

    #[test]
    fn poisoned_input_reports_the_rejection() {
        assert_eq!(
            super::PoisonedInput(7_u8).to_string(),
            "executor was poisoned by a previous panic"
        );
    }

    #[test]
    fn serialized_turns_finish_effects_before_the_next_transition() {
        let executor = SerializedExecutor::new(machine());
        let trace = Mutex::new(Vec::new());
        let receipt = executor
            .submit(1, &|output: Output| trace.lock().unwrap().push(output.0))
            .unwrap();
        assert_eq!(receipt.wait(), TurnOutcome::Completed);
        assert_eq!(*trace.lock().unwrap(), [1]);
    }

    type TestMachine = Base<u8, fn(u8, u8) -> (Output, u8), u8, Output>;

    struct ReentrantHandler {
        executor: Weak<SerializedExecutor<TestMachine>>,
        trace: Arc<Mutex<Vec<u8>>>,
    }

    impl OutputHandler<Output> for ReentrantHandler {
        fn handle(&self, output: Output) {
            self.trace.lock().unwrap().push(output.0);
            if output.0 == 1 {
                let executor = self.executor.upgrade().unwrap();
                let receipt = executor.submit(2, self).unwrap();
                assert_eq!(receipt.outcome(), None);
            }
        }
    }

    #[test]
    fn reentrant_serialized_submission_waits_for_current_handler() {
        fn transition(state: u8, input: u8) -> (Output, u8) {
            (Output(input), state + input)
        }
        let executor = Arc::new(SerializedExecutor::new(Base::new(
            0,
            TOPOLOGY.validated().unwrap(),
            transition as fn(u8, u8) -> (Output, u8),
        )));
        let trace = Arc::new(Mutex::new(Vec::new()));
        let handler = ReentrantHandler {
            executor: Arc::downgrade(&executor),
            trace: Arc::clone(&trace),
        };
        assert_eq!(
            executor.submit(1, &handler).unwrap().wait(),
            TurnOutcome::Completed
        );
        assert_eq!(*trace.lock().unwrap(), [1, 2]);
    }

    #[test]
    fn linearized_dispatch_resumes_after_handler_panic() {
        let executor = LinearizedExecutor::new(machine());
        assert_eq!(executor.submit(1), 1);
        assert_eq!(executor.submit(2), 2);
        assert!(
            catch_unwind(AssertUnwindSafe(|| {
                executor.dispatch_pending(&|_: Output| panic!("handler"));
            }))
            .is_err()
        );
        let seen = Mutex::new(Vec::new());
        assert_eq!(
            executor.dispatch_pending(&|output: Output| seen.lock().unwrap().push(output.0)),
            super::DispatchOutcome::Drained
        );
        assert_eq!(*seen.lock().unwrap(), [2]);
    }

    #[test]
    fn serialized_handler_panic_poisons_future_submissions() {
        let executor = SerializedExecutor::new(machine());
        assert!(
            catch_unwind(AssertUnwindSafe(|| {
                let _ = executor.submit(1, &|_: Output| panic!("handler"));
            }))
            .is_err()
        );
        let Err(rejected) = executor.submit(2, &|_: Output| {}) else {
            panic!("poisoned executor accepted input")
        };
        assert_eq!(rejected.0, 2);
    }

    struct ReentrantPoisonHandler {
        executor: Weak<SerializedExecutor<TestMachine>>,
        queued: Mutex<Option<TurnReceipt>>,
    }

    impl OutputHandler<Output> for ReentrantPoisonHandler {
        fn handle(&self, output: Output) {
            if output.0 == 1 {
                let receipt = self.executor.upgrade().unwrap().submit(2, self).unwrap();
                *self.queued.lock().unwrap() = Some(receipt);
                panic!("handler");
            }
        }
    }

    #[test]
    fn serialized_handler_panic_resolves_queued_receipt_as_poisoned() {
        fn transition(state: u8, input: u8) -> (Output, u8) {
            (Output(input), state + input)
        }
        let executor = Arc::new(SerializedExecutor::new(Base::new(
            0,
            TOPOLOGY.validated().unwrap(),
            transition as fn(u8, u8) -> (Output, u8),
        )));
        let handler = ReentrantPoisonHandler {
            executor: Arc::downgrade(&executor),
            queued: Mutex::new(None),
        };

        assert!(catch_unwind(AssertUnwindSafe(|| executor.submit(1, &handler))).is_err());
        let queued = handler.queued.lock().unwrap().take().unwrap();
        assert_eq!(queued.outcome(), Some(TurnOutcome::Poisoned));
        assert_eq!(queued.wait(), TurnOutcome::Poisoned);
        assert!(matches!(
            executor.submit(3, &handler),
            Err(super::PoisonedInput(3))
        ));
    }

    impl super::OutputEvidence for usize {
        type Evidence = usize;

        fn evidence(&self) -> Self::Evidence {
            *self
        }
    }

    #[test]
    fn dispatch_guard_drop_recovers_during_poison_unwind() {
        use std::sync::Condvar;
        use std::thread;

        let machine = Base::new(0_usize, TOPOLOGY.validated().unwrap(), |state, input| {
            assert_ne!(input, 9, "transition failure");
            (input, state + input)
        });
        let executor = Arc::new(LinearizedExecutor::new(machine));
        assert_eq!(executor.submit(1), 1);
        let gate = Arc::new((Mutex::new((false, false)), Condvar::new()));

        let dispatcher = {
            let executor = Arc::clone(&executor);
            let gate = Arc::clone(&gate);
            thread::spawn(move || {
                catch_unwind(AssertUnwindSafe(|| {
                    executor.dispatch_pending(&|_output| {
                        let (lock, ready) = &*gate;
                        let mut phase = lock.lock().unwrap();
                        phase.0 = true;
                        ready.notify_one();
                        // Hold dispatch ownership until the transition panic
                        // has landed; returning loops into next(), which then
                        // observes the poisoned executor.
                        while !phase.1 {
                            phase = ready.wait(phase).unwrap();
                        }
                    });
                }))
            })
        };
        {
            let (lock, ready) = &*gate;
            let mut phase = lock.lock().unwrap();
            while !phase.0 {
                phase = ready.wait(phase).unwrap();
            }
        }
        // The dispatcher holds dispatch ownership inside the handler; this
        // transition panic poisons the executor underneath it.
        let _ = catch_unwind(AssertUnwindSafe(|| {
            executor.submit(9);
        }));
        {
            let (lock, ready) = &*gate;
            *lock.lock().unwrap() = (true, true);
            ready.notify_one();
        }
        let outcome = dispatcher.join().expect("dispatcher thread aborted");
        assert!(outcome.is_err(), "next() must observe the poisoned lock");
        assert!(
            catch_unwind(AssertUnwindSafe(|| {
                executor.submit(2);
            }))
            .is_err()
        );
    }
}

#[cfg(all(test, loom))]
mod loom_tests {
    use loom::sync::atomic::{AtomicUsize, Ordering};
    use loom::sync::{Arc, Mutex};
    use loom::thread;

    use bombay_transition::{Base, Topology, Vertex, VertexId};

    use super::{LinearizedExecutor, OutputEvidence, SerializedExecutor, TurnOutcome};

    const VERTICES: &[Vertex] = &[Vertex {
        id: VertexId(0),
        label: "ready",
    }];
    const TOPOLOGY: Topology = Topology {
        name: "loom",
        initial: VertexId(0),
        vertices: VERTICES,
        transitions: &[],
    };

    struct Output(usize, Arc<AtomicUsize>);

    impl Drop for Output {
        fn drop(&mut self) {
            self.1.fetch_add(1, Ordering::SeqCst);
        }
    }

    impl OutputEvidence for Output {
        type Evidence = usize;

        fn evidence(&self) -> Self::Evidence {
            self.0
        }
    }

    #[test]
    fn real_linearized_executor_handles_submit_dispatch_boundary() {
        loom::model(|| {
            let drops = Arc::new(AtomicUsize::new(0));
            let machine_drops = Arc::clone(&drops);
            let machine = Base::new(0, TOPOLOGY.validated().unwrap(), move |state, input| {
                (Output(input, Arc::clone(&machine_drops)), state + input)
            });
            let executor = Arc::new(LinearizedExecutor::new(machine));
            let seen = Arc::new(Mutex::new(Vec::new()));

            let submitter = {
                let executor = Arc::clone(&executor);
                thread::spawn(move || {
                    executor.submit(1);
                    executor.submit(2);
                })
            };
            let dispatcher = {
                let executor = Arc::clone(&executor);
                let seen = Arc::clone(&seen);
                thread::spawn(move || {
                    executor.dispatch_pending(&|output: Output| {
                        seen.lock().unwrap().push(output.0);
                    });
                })
            };
            submitter.join().unwrap();
            dispatcher.join().unwrap();
            executor.dispatch_pending(&|output: Output| {
                seen.lock().unwrap().push(output.0);
            });
            assert_eq!(*seen.lock().unwrap(), [1, 2]);
            assert_eq!(drops.load(Ordering::SeqCst), 2);
        });
    }

    #[test]
    fn real_serialized_executor_keeps_each_turn_contiguous() {
        loom::model(|| {
            let trace = Arc::new(Mutex::new(Vec::new()));
            let machine_trace = Arc::clone(&trace);
            let machine = Base::new((), TOPOLOGY.validated().unwrap(), move |(), input| {
                machine_trace.lock().unwrap().push(input * 10);
                (input, ())
            });
            let executor = Arc::new(SerializedExecutor::new(machine));
            let mut threads = Vec::new();
            for input in [1, 2] {
                let executor = Arc::clone(&executor);
                let trace = Arc::clone(&trace);
                threads.push(thread::spawn(move || {
                    let receipt = executor
                        .submit(input, &|output| {
                            trace.lock().unwrap().push(output * 10 + 1);
                        })
                        .unwrap();
                    assert_eq!(receipt.wait(), TurnOutcome::Completed);
                }));
            }
            threads
                .into_iter()
                .for_each(|thread| thread.join().unwrap());
            let trace = trace.lock().unwrap();
            assert!(matches!(
                trace.as_slice(),
                [10, 11, 20, 21] | [20, 21, 10, 11]
            ));
        });
    }
}