logit-loom 0.2.1

Composable Rust token-stream transforms, observers, and receipts
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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Ordered, transactional logit-transform pipelines.

use std::any::Any;
use std::collections::HashSet;
use std::panic::{AssertUnwindSafe, catch_unwind};

use logit_loom_core::{
    CallbackFailure, CallbackPhase, CandidateMode, MAX_PIPELINE_STAGES, PipelineReceipt,
    PipelineSpec, StageReceipt, TokenId, TransformSpec,
};

use crate::{PipelineError, TransformError};

/// Borrowed state supplied to one Rust logit-transform invocation.
pub struct TransformContext<'a> {
    step: u32,
    causal_tokens: &'a [TokenId],
    candidate_tokens: &'a [TokenId],
    logits: &'a mut [f32],
}

impl<'a> TransformContext<'a> {
    /// Creates a validated borrowed candidate view.
    ///
    /// # Errors
    ///
    /// Returns an error when token and logit lengths disagree.
    pub fn new(
        step: u32,
        causal_tokens: &'a [TokenId],
        candidate_tokens: &'a [TokenId],
        logits: &'a mut [f32],
    ) -> Result<Self, PipelineError> {
        if candidate_tokens.len() != logits.len() || candidate_tokens.is_empty() {
            return Err(PipelineError::InvalidCandidates(
                "token and logit arrays must be non-empty and equal in length".to_owned(),
            ));
        }
        Ok(Self {
            step,
            causal_tokens,
            candidate_tokens,
            logits,
        })
    }

    /// Zero-based sampling step.
    pub const fn step(&self) -> u32 {
        self.step
    }

    /// Exact prompt and previously admitted generation tokens.
    pub const fn causal_tokens(&self) -> &[TokenId] {
        self.causal_tokens
    }

    /// Immutable candidate token identifiers.
    pub const fn candidate_tokens(&self) -> &[TokenId] {
        self.candidate_tokens
    }

    /// Mutable candidate logits corresponding one-for-one with token IDs.
    pub fn logits_mut(&mut self) -> &mut [f32] {
        self.logits
    }

    /// Iterates token/logit pairs without permitting token-ID mutation.
    pub fn candidates_mut(&mut self) -> impl Iterator<Item = (TokenId, &mut f32)> {
        self.candidate_tokens
            .iter()
            .copied()
            .zip(self.logits.iter_mut())
    }
}

/// Safe, synchronous implementation of one per-step logit transformation.
pub trait LogitTransform {
    /// Resets implementation-local state for a fresh generation call.
    ///
    /// # Errors
    ///
    /// Returns an implementation-defined reset failure.
    fn reset(&mut self, _causal_tokens: &[TokenId]) -> Result<(), TransformError> {
        Ok(())
    }

    /// Mutates one candidate-logit view.
    ///
    /// # Errors
    ///
    /// Returns an implementation-defined transformation failure.
    fn apply(&mut self, context: TransformContext<'_>) -> Result<(), TransformError>;

    /// Observes a token after the backend has admitted it to causal state.
    ///
    /// # Errors
    ///
    /// Returns an implementation-defined token-admission failure.
    fn accept(&mut self, _token: TokenId) -> Result<(), TransformError> {
        Ok(())
    }
}

/// One identified implementation in an ordered pipeline.
pub struct Stage {
    specification: TransformSpec,
    implementation: Box<dyn LogitTransform>,
}

impl Stage {
    /// Wraps a safe Rust transform with its mechanical contract.
    ///
    /// # Errors
    ///
    /// Returns an error when the specification is invalid.
    pub fn new(
        specification: TransformSpec,
        implementation: impl LogitTransform + 'static,
    ) -> Result<Self, PipelineError> {
        specification.validate()?;
        Ok(Self {
            specification,
            implementation: Box::new(implementation),
        })
    }

    /// Returns the stage contract.
    pub const fn specification(&self) -> &TransformSpec {
        &self.specification
    }
}

/// Ordered, call-scoped transform pipeline.
///
/// Candidate changes are committed to the backend view only after every stage
/// succeeds. A returned error therefore cannot expose a partially transformed
/// vocabulary, although implementation-local state in stages that already ran
/// may have changed. Begin a fresh call before retrying.
pub struct Pipeline {
    specification: PipelineSpec,
    stages: Vec<Stage>,
    receipt: PipelineReceipt,
    begun: bool,
    failed: bool,
}

impl Pipeline {
    /// Creates an ordered pipeline of one to 32 compatible stages.
    ///
    /// # Errors
    ///
    /// Returns an error for an invalid stage count or incompatible modes.
    pub fn new(stages: Vec<Stage>) -> Result<Self, PipelineError> {
        let specification = PipelineSpec {
            stages: stages
                .iter()
                .take(MAX_PIPELINE_STAGES + 1)
                .map(|stage| stage.specification.clone())
                .collect(),
        };
        specification.validate()?;
        let receipt = empty_receipt(&specification)?;
        Ok(Self {
            specification,
            stages,
            receipt,
            begun: false,
            failed: false,
        })
    }

    /// Returns the immutable ordered specification.
    pub const fn specification(&self) -> &PipelineSpec {
        &self.specification
    }

    /// Returns current mechanical accounting.
    pub fn receipt(&self) -> PipelineReceipt {
        self.receipt.clone()
    }

    /// Returns whether a callback has failed since the last begin.
    pub const fn is_failed(&self) -> bool {
        self.failed
    }

    /// Resets every stage for a new generation call.
    ///
    /// # Errors
    ///
    /// Returns a contained callback failure. Calling `begin` again constructs
    /// fresh accounting and asks every stage to reset again.
    pub fn begin(&mut self, causal_tokens: &[TokenId]) -> Result<(), PipelineError> {
        self.receipt = empty_receipt(&self.specification)?;
        self.receipt.begins = 1;
        self.begun = true;
        self.failed = false;
        for index in 0..self.stages.len() {
            self.receipt.stages[index].resets = 1;
            let result = catch_unwind(AssertUnwindSafe(|| {
                self.stages[index].implementation.reset(causal_tokens)
            }));
            if let Some(failure) = callback_result(CallbackPhase::Reset, result) {
                return Err(self.record_failure(index, failure));
            }
        }
        Ok(())
    }

    /// Applies the pipeline to a complete vocabulary-logit slice.
    ///
    /// Sparse pipelines select the highest finite raw logits, breaking ties by
    /// ascending token ID. Full pipelines expose tokenizer order. Candidate
    /// output containing NaN is rejected before write-back; infinity remains a
    /// valid explicit suppression or promotion value. Except for a terminal
    /// selection, the prior successful step must be matched by [`Self::accept`]
    /// before the next step begins.
    ///
    /// # Errors
    ///
    /// Returns an error when the call has not begun, bounds are exceeded, the
    /// vocabulary is invalid, lifecycle order is violated, or a callback
    /// fails.
    pub fn apply_to_vocabulary(
        &mut self,
        step: u32,
        causal_tokens: &[TokenId],
        vocabulary_logits: &mut [f32],
    ) -> Result<(), PipelineError> {
        self.require_ready()?;
        let maximum_vocabulary = usize::try_from(i32::MAX).unwrap_or(usize::MAX);
        if vocabulary_logits.is_empty() || vocabulary_logits.len() > maximum_vocabulary {
            return Err(PipelineError::InvalidCandidates(
                "vocabulary must fit non-negative i32 token identifiers".to_owned(),
            ));
        }
        let mode = self.specification.mode()?;
        let indices = selected_indices(mode, vocabulary_logits);
        if indices.is_empty() {
            return Err(PipelineError::InvalidCandidates(
                "candidate exposure selected no finite logits".to_owned(),
            ));
        }
        let candidate_tokens = indices
            .iter()
            .map(|index| {
                TokenId::new(i32::try_from(*index).map_err(|_| {
                    PipelineError::InvalidCandidates("token identifier overflowed".to_owned())
                })?)
                .map_err(PipelineError::from)
            })
            .collect::<Result<Vec<_>, _>>()?;
        let mut candidate_logits = indices
            .iter()
            .map(|index| vocabulary_logits[*index])
            .collect::<Vec<_>>();
        self.apply_working_candidates(
            step,
            causal_tokens,
            &candidate_tokens,
            &mut candidate_logits,
        )?;

        for (index, value) in indices.into_iter().zip(candidate_logits) {
            vocabulary_logits[index] = value;
        }
        Ok(())
    }

    /// Applies the pipeline to a candidate view selected by a backend adapter.
    ///
    /// This entry point is for adapters whose native sampler has already copied
    /// token IDs and logits into an owned scratch view. The adapter remains
    /// responsible for proving that a full-mode view covers the vocabulary and
    /// that sparse candidates were selected according to its declared native
    /// boundary. Logit Loom enforces shape, sparse bounds, stage limits, panic
    /// containment, NaN rejection, and transactional write-back to this slice.
    ///
    /// # Errors
    ///
    /// Returns an error when the call has not begun, candidate arrays are empty
    /// or mismatched, token identifiers repeat, a sparse view exceeds its
    /// limit, lifecycle order is violated, or a callback fails.
    pub fn apply_to_candidates(
        &mut self,
        step: u32,
        causal_tokens: &[TokenId],
        candidate_tokens: &[TokenId],
        logits: &mut [f32],
    ) -> Result<(), PipelineError> {
        self.require_ready()?;
        self.validate_candidate_view(candidate_tokens, logits)?;
        let mut working_logits = logits.to_vec();
        self.apply_working_candidates(step, causal_tokens, candidate_tokens, &mut working_logits)?;
        logits.copy_from_slice(&working_logits);
        Ok(())
    }

    fn apply_working_candidates(
        &mut self,
        step: u32,
        causal_tokens: &[TokenId],
        candidate_tokens: &[TokenId],
        candidate_logits: &mut [f32],
    ) -> Result<(), PipelineError> {
        self.require_ready()?;
        if self
            .receipt
            .stages
            .iter()
            .any(|stage| stage.accepted_tokens < self.receipt.invocations)
        {
            return Err(PipelineError::AdmissionPending);
        }
        if step != self.receipt.invocations {
            return Err(PipelineError::InvalidStep {
                expected: self.receipt.invocations,
                actual: step,
            });
        }
        let candidate_count = u64::try_from(candidate_logits.len())
            .map_err(|_| PipelineError::AccountingOverflow("candidate count"))?;
        self.receipt.candidates_copied = self
            .receipt
            .candidates_copied
            .checked_add(candidate_count)
            .ok_or(PipelineError::AccountingOverflow("candidates copied"))?;

        for index in 0..self.stages.len() {
            if step >= self.stages[index].specification.max_steps
                || self.receipt.stages[index].invocations
                    >= self.stages[index].specification.max_steps
            {
                let failure = CallbackFailure::new(
                    CallbackPhase::Apply,
                    false,
                    format!(
                        "step {step} exceeds stage bound {}",
                        self.stages[index].specification.max_steps
                    ),
                );
                return Err(self.record_failure(index, failure));
            }
            let before = candidate_logits
                .iter()
                .map(|value| value.to_bits())
                .collect::<Vec<_>>();
            let context =
                TransformContext::new(step, causal_tokens, candidate_tokens, candidate_logits)?;
            self.receipt.stages[index].invocations = self.receipt.stages[index]
                .invocations
                .checked_add(1)
                .ok_or(PipelineError::AccountingOverflow("stage invocations"))?;
            self.receipt.stages[index].candidates_seen = self.receipt.stages[index]
                .candidates_seen
                .checked_add(candidate_count)
                .ok_or(PipelineError::AccountingOverflow("stage candidates seen"))?;
            let result = catch_unwind(AssertUnwindSafe(|| {
                self.stages[index].implementation.apply(context)
            }));
            if let Some(failure) = callback_result(CallbackPhase::Apply, result) {
                return Err(self.record_failure(index, failure));
            }
            if candidate_logits.iter().any(|value| value.is_nan()) {
                let failure = CallbackFailure::new(
                    CallbackPhase::Apply,
                    false,
                    "transform produced a NaN logit",
                );
                return Err(self.record_failure(index, failure));
            }
            let changed = before
                .iter()
                .zip(candidate_logits.iter())
                .filter(|(before, after)| **before != after.to_bits())
                .count();
            let changed = u64::try_from(changed)
                .map_err(|_| PipelineError::AccountingOverflow("changed logits"))?;
            self.receipt.stages[index].logits_changed = self.receipt.stages[index]
                .logits_changed
                .checked_add(changed)
                .ok_or(PipelineError::AccountingOverflow("stage logits changed"))?;
        }

        self.receipt.invocations = self
            .receipt
            .invocations
            .checked_add(1)
            .ok_or(PipelineError::AccountingOverflow("pipeline invocations"))?;
        self.receipt.candidates_committed = self
            .receipt
            .candidates_committed
            .checked_add(candidate_count)
            .ok_or(PipelineError::AccountingOverflow("candidates committed"))?;
        Ok(())
    }

    fn validate_candidate_view(
        &self,
        candidate_tokens: &[TokenId],
        logits: &[f32],
    ) -> Result<(), PipelineError> {
        if candidate_tokens.is_empty() || candidate_tokens.len() != logits.len() {
            return Err(PipelineError::InvalidCandidates(
                "token and logit arrays must be non-empty and equal in length".to_owned(),
            ));
        }
        let maximum_candidates = usize::try_from(i32::MAX).unwrap_or(usize::MAX);
        if candidate_tokens.len() > maximum_candidates {
            return Err(PipelineError::InvalidCandidates(
                "candidate count exceeds non-negative i32 token space".to_owned(),
            ));
        }
        let unique_tokens = candidate_tokens.iter().copied().collect::<HashSet<_>>();
        if unique_tokens.len() != candidate_tokens.len() {
            return Err(PipelineError::InvalidCandidates(
                "candidate token identifiers must be unique".to_owned(),
            ));
        }
        if let CandidateMode::Sparse { limit } = self.specification.mode()?
            && candidate_tokens.len() > usize::try_from(limit).unwrap_or(usize::MAX)
        {
            return Err(PipelineError::InvalidCandidates(format!(
                "candidate view exceeds sparse limit {limit}"
            )));
        }
        Ok(())
    }

    /// Reports one token after causal admission.
    ///
    /// Each admission must correspond to an unmatched successful transform
    /// invocation from the current call.
    ///
    /// # Errors
    ///
    /// Returns a contained callback failure or a lifecycle error.
    pub fn accept(&mut self, token: TokenId) -> Result<(), PipelineError> {
        self.require_ready()?;
        if self
            .receipt
            .stages
            .iter()
            .any(|stage| stage.accepted_tokens >= self.receipt.invocations)
        {
            return Err(PipelineError::UnexpectedAdmission);
        }
        for index in 0..self.stages.len() {
            self.receipt.stages[index].accepted_tokens = self.receipt.stages[index]
                .accepted_tokens
                .checked_add(1)
                .ok_or(PipelineError::AccountingOverflow("accepted tokens"))?;
            let result = catch_unwind(AssertUnwindSafe(|| {
                self.stages[index].implementation.accept(token)
            }));
            if let Some(failure) = callback_result(CallbackPhase::Accept, result) {
                return Err(self.record_failure(index, failure));
            }
        }
        Ok(())
    }

    fn require_ready(&self) -> Result<(), PipelineError> {
        if !self.begun {
            return Err(PipelineError::NotBegun);
        }
        if self.failed {
            return Err(PipelineError::Failed);
        }
        Ok(())
    }

    fn record_failure(&mut self, index: usize, failure: CallbackFailure) -> PipelineError {
        self.failed = true;
        self.receipt.failed_stage = Some(u32::try_from(index).unwrap_or(u32::MAX));
        self.receipt.stages[index].failure = Some(failure.clone());
        PipelineError::Callback {
            stage: index,
            failure,
        }
    }
}

fn empty_receipt(specification: &PipelineSpec) -> Result<PipelineReceipt, PipelineError> {
    Ok(PipelineReceipt {
        specification: specification.clone(),
        pipeline: specification.digest()?,
        begins: 0,
        invocations: 0,
        candidates_copied: 0,
        candidates_committed: 0,
        failed_stage: None,
        stages: specification
            .stages
            .iter()
            .cloned()
            .map(|specification| StageReceipt {
                specification,
                resets: 0,
                invocations: 0,
                candidates_seen: 0,
                logits_changed: 0,
                accepted_tokens: 0,
                failure: None,
            })
            .collect(),
    })
}

fn selected_indices(mode: CandidateMode, logits: &[f32]) -> Vec<usize> {
    match mode {
        CandidateMode::FullVocabulary => (0..logits.len()).collect(),
        CandidateMode::Sparse { limit } => {
            let mut indices = logits
                .iter()
                .enumerate()
                .filter(|(_, value)| value.is_finite())
                .map(|(index, _)| index)
                .collect::<Vec<_>>();
            indices.sort_unstable_by(|left, right| {
                logits[*right]
                    .total_cmp(&logits[*left])
                    .then_with(|| left.cmp(right))
            });
            indices.truncate(usize::try_from(limit).unwrap_or(usize::MAX));
            indices
        }
    }
}

fn callback_result(
    phase: CallbackPhase,
    result: Result<Result<(), TransformError>, Box<dyn Any + Send>>,
) -> Option<CallbackFailure> {
    match result {
        Ok(Ok(())) => None,
        Ok(Err(error)) => Some(CallbackFailure::new(phase, false, error.to_string())),
        Err(payload) => Some(CallbackFailure::new(phase, true, panic_message(&*payload))),
    }
}

fn panic_message(payload: &(dyn Any + Send)) -> String {
    payload.downcast_ref::<&str>().map_or_else(
        || {
            payload
                .downcast_ref::<String>()
                .cloned()
                .unwrap_or_else(|| "non-string panic payload".to_owned())
        },
        |message| (*message).to_owned(),
    )
}

#[cfg(test)]
mod tests {
    use logit_loom_core::Digest;

    use super::*;

    struct Add(f32);

    impl LogitTransform for Add {
        fn apply(&mut self, mut context: TransformContext<'_>) -> Result<(), TransformError> {
            for value in context.logits_mut() {
                *value += self.0;
            }
            Ok(())
        }
    }

    struct Panic;

    impl LogitTransform for Panic {
        fn apply(&mut self, _context: TransformContext<'_>) -> Result<(), TransformError> {
            panic!("contained")
        }
    }

    struct MakeNan;

    impl LogitTransform for MakeNan {
        fn apply(&mut self, mut context: TransformContext<'_>) -> Result<(), TransformError> {
            context.logits_mut()[0] = f32::NAN;
            Ok(())
        }
    }

    struct ResetPanics;

    impl LogitTransform for ResetPanics {
        fn reset(&mut self, _causal_tokens: &[TokenId]) -> Result<(), TransformError> {
            panic!("reset panic is contained")
        }

        fn apply(&mut self, _context: TransformContext<'_>) -> Result<(), TransformError> {
            Ok(())
        }
    }

    struct AcceptFails;

    impl LogitTransform for AcceptFails {
        fn apply(&mut self, _context: TransformContext<'_>) -> Result<(), TransformError> {
            Ok(())
        }

        fn accept(&mut self, _token: TokenId) -> Result<(), TransformError> {
            Err(TransformError::new("accept failure"))
        }
    }

    fn spec(name: &str, mode: CandidateMode) -> TransformSpec {
        TransformSpec::new(Digest::of_bytes("test", name.as_bytes()), mode, 4).unwrap()
    }

    #[test]
    fn ordered_pipeline_commits_only_after_every_stage() {
        let mut pipeline = Pipeline::new(vec![
            Stage::new(spec("one", CandidateMode::FullVocabulary), Add(1.0)).unwrap(),
            Stage::new(spec("two", CandidateMode::FullVocabulary), Add(2.0)).unwrap(),
        ])
        .unwrap();
        pipeline.begin(&[]).unwrap();
        let mut logits = [0.0, 1.0];
        pipeline.apply_to_vocabulary(0, &[], &mut logits).unwrap();
        assert_eq!(
            logits.map(f32::to_bits),
            [3.0_f32.to_bits(), 4.0_f32.to_bits()]
        );
        assert_eq!(pipeline.receipt().stages[0].logits_changed, 2);
        assert_eq!(pipeline.receipt().stages[1].logits_changed, 2);
    }

    #[test]
    fn callback_panic_does_not_commit_partial_logits() {
        let mut pipeline = Pipeline::new(vec![
            Stage::new(spec("one", CandidateMode::FullVocabulary), Add(1.0)).unwrap(),
            Stage::new(spec("panic", CandidateMode::FullVocabulary), Panic).unwrap(),
        ])
        .unwrap();
        pipeline.begin(&[]).unwrap();
        let mut logits = [0.0, 1.0];
        assert!(pipeline.apply_to_vocabulary(0, &[], &mut logits).is_err());
        assert_eq!(
            logits.map(f32::to_bits),
            [0.0_f32.to_bits(), 1.0_f32.to_bits()]
        );
        assert!(pipeline.is_failed());
        assert!(
            pipeline.receipt().stages[1]
                .failure
                .as_ref()
                .unwrap()
                .panicked
        );
    }

    #[test]
    fn sparse_mode_exposes_ranked_candidates_and_writes_back_by_token() {
        let mut pipeline = Pipeline::new(vec![
            Stage::new(spec("sparse", CandidateMode::Sparse { limit: 2 }), Add(1.0)).unwrap(),
        ])
        .unwrap();
        pipeline.begin(&[]).unwrap();
        let mut logits = [0.0, 3.0, 2.0, 1.0];
        pipeline.apply_to_vocabulary(0, &[], &mut logits).unwrap();
        assert_eq!(
            logits.map(f32::to_bits),
            [
                0.0_f32.to_bits(),
                4.0_f32.to_bits(),
                3.0_f32.to_bits(),
                1.0_f32.to_bits(),
            ]
        );
    }

    #[test]
    fn sparse_ties_break_by_ascending_token_identifier() {
        let mut pipeline = Pipeline::new(vec![
            Stage::new(spec("ties", CandidateMode::Sparse { limit: 2 }), Add(1.0)).unwrap(),
        ])
        .unwrap();
        pipeline.begin(&[]).unwrap();
        let mut logits = [0.0, 2.0, 2.0, 2.0];
        pipeline.apply_to_vocabulary(0, &[], &mut logits).unwrap();
        assert_eq!(
            logits.map(f32::to_bits),
            [
                0.0_f32.to_bits(),
                3.0_f32.to_bits(),
                3.0_f32.to_bits(),
                2.0_f32.to_bits(),
            ]
        );
    }

    #[test]
    fn backend_selected_candidate_view_is_bounded_and_transactional() {
        let mut pipeline = Pipeline::new(vec![
            Stage::new(
                spec("selected", CandidateMode::Sparse { limit: 2 }),
                Add(1.0),
            )
            .unwrap(),
        ])
        .unwrap();
        let causal = [TokenId::new(7).unwrap()];
        let candidates = [TokenId::new(41).unwrap(), TokenId::new(3).unwrap()];
        pipeline.begin(&causal).unwrap();
        let mut logits = [2.0, 1.0];
        pipeline
            .apply_to_candidates(0, &causal, &candidates, &mut logits)
            .unwrap();
        assert_eq!(
            logits.map(f32::to_bits),
            [3.0_f32.to_bits(), 2.0_f32.to_bits()]
        );
        let receipt = pipeline.receipt();
        assert_eq!(receipt.candidates_copied, 2);
        assert_eq!(receipt.candidates_committed, 2);

        let mut oversized = [0.0, 1.0, 2.0];
        let oversized_tokens = [
            TokenId::new(1).unwrap(),
            TokenId::new(2).unwrap(),
            TokenId::new(3).unwrap(),
        ];
        assert!(
            pipeline
                .apply_to_candidates(1, &causal, &oversized_tokens, &mut oversized)
                .is_err()
        );
        assert_eq!(
            oversized.map(f32::to_bits),
            [0.0_f32.to_bits(), 1.0_f32.to_bits(), 2.0_f32.to_bits()]
        );
    }

    #[test]
    fn nan_output_fails_without_writeback() {
        let mut pipeline = Pipeline::new(vec![
            Stage::new(spec("nan", CandidateMode::FullVocabulary), MakeNan).unwrap(),
        ])
        .unwrap();
        pipeline.begin(&[]).unwrap();
        let mut logits = [1.0, 2.0];
        assert!(pipeline.apply_to_vocabulary(0, &[], &mut logits).is_err());
        assert_eq!(
            logits.map(f32::to_bits),
            [1.0_f32.to_bits(), 2.0_f32.to_bits()]
        );
        assert!(pipeline.is_failed());
    }

    #[test]
    fn sampling_steps_are_zero_based_and_sequential() {
        let mut pipeline = Pipeline::new(vec![
            Stage::new(spec("sequence", CandidateMode::FullVocabulary), Add(1.0)).unwrap(),
        ])
        .unwrap();
        pipeline.begin(&[]).unwrap();
        let mut logits = [0.0];
        assert!(matches!(
            pipeline.apply_to_vocabulary(1, &[], &mut logits),
            Err(PipelineError::InvalidStep {
                expected: 0,
                actual: 1
            })
        ));
        pipeline.apply_to_vocabulary(0, &[], &mut logits).unwrap();
        pipeline.accept(TokenId::new(1).unwrap()).unwrap();
        assert!(matches!(
            pipeline.apply_to_vocabulary(0, &[], &mut logits),
            Err(PipelineError::InvalidStep {
                expected: 1,
                actual: 0
            })
        ));
    }

    #[test]
    fn candidate_views_reject_duplicate_token_identifiers() {
        let mut pipeline = Pipeline::new(vec![
            Stage::new(spec("duplicates", CandidateMode::FullVocabulary), Add(1.0)).unwrap(),
        ])
        .unwrap();
        pipeline.begin(&[]).unwrap();
        let token = TokenId::new(3).unwrap();
        let mut logits = [1.0, 2.0];
        assert!(
            pipeline
                .apply_to_candidates(0, &[], &[token, token], &mut logits)
                .is_err()
        );
        assert_eq!(
            logits.map(f32::to_bits),
            [1.0_f32.to_bits(), 2.0_f32.to_bits()]
        );
    }

    #[test]
    fn invocation_bound_cannot_be_bypassed_with_repeated_steps() {
        let specification = TransformSpec::new(
            Digest::of_bytes("test", b"one-step"),
            CandidateMode::FullVocabulary,
            1,
        )
        .unwrap();
        let mut pipeline =
            Pipeline::new(vec![Stage::new(specification, Add(1.0)).unwrap()]).unwrap();
        pipeline.begin(&[]).unwrap();
        let mut logits = [0.0];
        pipeline.apply_to_vocabulary(0, &[], &mut logits).unwrap();
        assert!(pipeline.apply_to_vocabulary(0, &[], &mut logits).is_err());
        assert_eq!(pipeline.receipt().invocations, 1);
    }

    #[test]
    fn admission_requires_an_unmatched_successful_step() {
        let mut pipeline = Pipeline::new(vec![
            Stage::new(spec("admission", CandidateMode::FullVocabulary), Add(1.0)).unwrap(),
        ])
        .unwrap();
        pipeline.begin(&[]).unwrap();
        let token = TokenId::new(1).unwrap();
        assert!(matches!(
            pipeline.accept(token),
            Err(PipelineError::UnexpectedAdmission)
        ));

        let mut logits = [0.0];
        pipeline.apply_to_vocabulary(0, &[], &mut logits).unwrap();
        assert!(matches!(
            pipeline.apply_to_vocabulary(1, &[], &mut logits),
            Err(PipelineError::AdmissionPending)
        ));
        pipeline.accept(token).unwrap();
        assert!(matches!(
            pipeline.accept(token),
            Err(PipelineError::UnexpectedAdmission)
        ));
    }

    #[test]
    fn reset_and_admission_failures_are_contained_and_accounted() {
        let mut reset = Pipeline::new(vec![
            Stage::new(
                spec("reset-panic", CandidateMode::FullVocabulary),
                ResetPanics,
            )
            .unwrap(),
        ])
        .unwrap();
        assert!(reset.begin(&[]).is_err());
        let failure = reset.receipt().stages[0].failure.clone().unwrap();
        assert!(failure.panicked);
        assert_eq!(failure.phase, CallbackPhase::Reset);

        let mut accept = Pipeline::new(vec![
            Stage::new(
                spec("accept-error", CandidateMode::FullVocabulary),
                AcceptFails,
            )
            .unwrap(),
        ])
        .unwrap();
        accept.begin(&[]).unwrap();
        let mut logits = [0.0];
        accept.apply_to_vocabulary(0, &[], &mut logits).unwrap();
        assert!(accept.accept(TokenId::new(0).unwrap()).is_err());
        let failure = accept.receipt().stages[0].failure.clone().unwrap();
        assert!(!failure.panicked);
        assert_eq!(failure.phase, CallbackPhase::Accept);
    }
}