trustformers 0.1.1

TrustformeRS - Rust port of Hugging Face Transformers
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
use crate::error::{Result, TrustformersError};
use crate::pipeline::{Pipeline, PipelineOutput};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[cfg(feature = "async")]
use crate::pipeline::AsyncPipeline;

/// Error handling strategy for pipeline composition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ErrorHandling {
    /// Stop execution on first error
    StopOnError,
    /// Continue with default values on error
    ContinueWithDefault,
    /// Skip failed steps and continue
    SkipOnError,
}

/// Strategy for pipeline composition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CompositionStrategy {
    /// Sequential execution
    Sequential,
    /// Parallel execution (where possible)
    Parallel,
    /// Conditional execution based on outputs
    Conditional,
}

/// Configuration for pipeline composition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompositionConfig {
    /// Error handling strategy
    pub error_handling: ErrorHandling,
    /// Composition strategy
    pub strategy: CompositionStrategy,
    /// Maximum execution time (in seconds)
    pub timeout: Option<f64>,
}

impl Default for CompositionConfig {
    fn default() -> Self {
        Self {
            error_handling: ErrorHandling::StopOnError,
            strategy: CompositionStrategy::Sequential,
            timeout: None,
        }
    }
}

/// Alias for backward compatibility
pub type PipelineComposition = PipelineComposer;

/// Trait for converting between pipeline outputs and inputs
pub trait OutputConverter<T>: Send + Sync {
    fn convert(&self, output: PipelineOutput) -> Result<T>;
}

/// Default converter that attempts to extract text from pipeline outputs
pub struct TextConverter;

impl OutputConverter<String> for TextConverter {
    fn convert(&self, output: PipelineOutput) -> Result<String> {
        match output {
            PipelineOutput::Generation(gen) => Ok(gen.generated_text),
            PipelineOutput::Summarization(text) => Ok(text),
            PipelineOutput::Translation(text) => Ok(text),
            PipelineOutput::Classification(results) => {
                if let Some(first) = results.first() {
                    Ok(first.label.clone())
                } else {
                    Err(TrustformersError::invalid_input_simple(
                        "No classification results to convert".to_string(),
                    ))
                }
            },
            PipelineOutput::QuestionAnswering(qa) => Ok(qa.answer),
            PipelineOutput::FillMask(results) => {
                if let Some(first) = results.first() {
                    Ok(first.sequence.clone())
                } else {
                    Err(TrustformersError::invalid_input_simple(
                        "No fill mask results to convert".to_string(),
                    ))
                }
            },
            PipelineOutput::TokenClassification(tokens) => {
                // Concatenate all token words
                let text = tokens.iter().map(|t| &t.word).cloned().collect::<Vec<_>>().join(" ");
                Ok(text)
            },
            #[cfg(feature = "vision")]
            PipelineOutput::ImageToText(result) => Ok(result.generated_text),
            #[cfg(feature = "vision")]
            PipelineOutput::VisualQuestionAnswering(result) => Ok(result.answer),
            #[cfg(feature = "audio")]
            PipelineOutput::SpeechToText(result) => Ok(result.text),
            #[cfg(feature = "audio")]
            PipelineOutput::TextToSpeech(_result) => Err(TrustformersError::invalid_input_simple(
                "Cannot convert TextToSpeech output to text".to_string(),
            )),
            PipelineOutput::DocumentUnderstanding(result) => Ok(result.text.unwrap_or_default()),
            PipelineOutput::MultiModal(result) => Ok(result.text.unwrap_or_default()),
            #[cfg(feature = "async")]
            PipelineOutput::Conversational(result) => Ok(result.response),
            PipelineOutput::AdvancedRAG(result) => Ok(result.final_answer),
            PipelineOutput::MixtureOfDepths(result) => Ok(format!(
                "Processed with efficiency: {}",
                result.efficiency_score
            )),
            PipelineOutput::SpeculativeDecoding(result) => Ok(result.generated_text),
            PipelineOutput::Mamba2(result) => Ok(result.text),
            PipelineOutput::Text(text) => Ok(text),
        }
    }
}

/// A pipeline that composes two pipelines sequentially
pub struct ComposedPipeline<P1, P2> {
    first: Arc<P1>,
    second: Arc<P2>,
    converter: Arc<TextConverter>,
}

impl<P1, P2> ComposedPipeline<P1, P2>
where
    P1: Pipeline<Input = String, Output = PipelineOutput>,
    P2: Pipeline<Input = String, Output = PipelineOutput>,
{
    pub fn new(first: P1, second: P2) -> Self {
        Self {
            first: Arc::new(first),
            second: Arc::new(second),
            converter: Arc::new(TextConverter),
        }
    }

    /// Chain another pipeline to this composed pipeline
    pub fn chain<P3>(self, third: P3) -> ComposedPipeline<Self, P3>
    where
        P3: Pipeline<Input = String, Output = PipelineOutput>,
    {
        ComposedPipeline::new(self, third)
    }
}

impl<P1, P2> Pipeline for ComposedPipeline<P1, P2>
where
    P1: Pipeline<Input = String, Output = PipelineOutput>,
    P2: Pipeline<Input = String, Output = PipelineOutput>,
{
    type Input = String;
    type Output = PipelineOutput;

    fn __call__(&self, input: Self::Input) -> Result<Self::Output> {
        // Process with first pipeline
        let first_output = self.first.__call__(input)?;

        // Convert output to input for second pipeline
        let second_input = self.converter.convert(first_output)?;

        // Process with second pipeline
        self.second.__call__(second_input)
    }

    fn batch(&self, inputs: Vec<Self::Input>) -> Result<Vec<Self::Output>> {
        // Process all inputs through first pipeline
        let first_outputs = self.first.batch(inputs)?;

        // Convert all outputs to inputs for second pipeline
        let second_inputs: Result<Vec<_>> =
            first_outputs.into_iter().map(|output| self.converter.convert(output)).collect();
        let second_inputs = second_inputs?;

        // Process through second pipeline
        self.second.batch(second_inputs)
    }
}

#[cfg(feature = "async")]
#[async_trait::async_trait]
impl<P1, P2> AsyncPipeline for ComposedPipeline<P1, P2>
where
    P1: AsyncPipeline<Input = String, Output = PipelineOutput> + Sync,
    P2: AsyncPipeline<Input = String, Output = PipelineOutput> + Sync,
{
    type Input = String;
    type Output = PipelineOutput;

    async fn __call_async__(&self, input: Self::Input) -> Result<Self::Output> {
        // Process with first pipeline
        let first_output = self.first.__call_async__(input).await?;

        // Convert output to input for second pipeline
        let second_input = self.converter.convert(first_output)?;

        // Process with second pipeline
        self.second.__call_async__(second_input).await
    }

    async fn batch_async(&self, inputs: Vec<Self::Input>) -> Result<Vec<Self::Output>> {
        // Process all inputs through first pipeline
        let first_outputs = self.first.batch_async(inputs).await?;

        // Convert all outputs to inputs for second pipeline
        let second_inputs: Result<Vec<_>> =
            first_outputs.into_iter().map(|output| self.converter.convert(output)).collect();
        let second_inputs = second_inputs?;

        // Process through second pipeline
        self.second.batch_async(second_inputs).await
    }
}

/// A flexible pipeline chain that can handle multiple pipelines
pub struct PipelineChain {
    stages: Vec<Box<dyn Pipeline<Input = String, Output = PipelineOutput>>>,
}

impl Default for PipelineChain {
    fn default() -> Self {
        Self::new()
    }
}

impl PipelineChain {
    pub fn new() -> Self {
        Self { stages: Vec::new() }
    }

    /// Add a pipeline stage to the chain
    pub fn add_stage<P>(mut self, pipeline: P) -> Self
    where
        P: Pipeline<Input = String, Output = PipelineOutput> + 'static,
    {
        self.stages.push(Box::new(pipeline));
        self
    }

    /// Create a chain from a vector of pipelines
    pub fn from_pipelines(
        pipelines: Vec<Box<dyn Pipeline<Input = String, Output = PipelineOutput>>>,
    ) -> Self {
        Self { stages: pipelines }
    }
}

impl Pipeline for PipelineChain {
    type Input = String;
    type Output = PipelineOutput;

    fn __call__(&self, input: Self::Input) -> Result<Self::Output> {
        if self.stages.is_empty() {
            return Err(TrustformersError::invalid_input_simple(
                "Pipeline chain is empty".to_string(),
            ));
        }

        let mut current_input = input;
        let mut current_output = None;

        for (i, stage) in self.stages.iter().enumerate() {
            let output = stage.__call__(current_input.clone())?;

            if i == self.stages.len() - 1 {
                // Last stage, return the output
                current_output = Some(output);
            } else {
                // Convert output to string for next stage
                let converter = TextConverter;
                current_input = converter.convert(output)?;
            }
        }

        current_output.ok_or_else(|| {
            TrustformersError::invalid_input_simple("Pipeline chain produced no output".to_string())
        })
    }

    fn batch(&self, inputs: Vec<Self::Input>) -> Result<Vec<Self::Output>> {
        inputs.into_iter().map(|input| self.__call__(input)).collect()
    }
}

/// Builder for creating pipeline compositions
pub struct PipelineComposer {
    stages: Vec<Box<dyn Pipeline<Input = String, Output = PipelineOutput>>>,
}

impl PipelineComposer {
    pub fn new() -> Self {
        Self { stages: Vec::new() }
    }

    /// Start the composition with a pipeline
    pub fn start<P>(mut self, pipeline: P) -> Self
    where
        P: Pipeline<Input = String, Output = PipelineOutput> + 'static,
    {
        self.stages.push(Box::new(pipeline));
        self
    }

    /// Add another pipeline to the composition
    pub fn then<P>(mut self, pipeline: P) -> Self
    where
        P: Pipeline<Input = String, Output = PipelineOutput> + 'static,
    {
        self.stages.push(Box::new(pipeline));
        self
    }

    /// Build the final composed pipeline; returns Err if no stages were added
    pub fn build(self) -> Result<Box<dyn Pipeline<Input = String, Output = PipelineOutput>>> {
        if self.stages.is_empty() {
            return Err(TrustformersError::invalid_input_simple(
                "No pipelines added to composer".to_string(),
            ));
        }
        Ok(Box::new(PipelineChain::from_pipelines(self.stages)))
    }
}

impl Default for PipelineComposer {
    fn default() -> Self {
        Self::new()
    }
}

/// Convenience function to create a simple two-pipeline composition
pub fn compose_pipelines<P1, P2>(first: P1, second: P2) -> ComposedPipeline<P1, P2>
where
    P1: Pipeline<Input = String, Output = PipelineOutput>,
    P2: Pipeline<Input = String, Output = PipelineOutput>,
{
    ComposedPipeline::new(first, second)
}

/// Macro for easy pipeline chaining
#[macro_export]
macro_rules! chain_pipelines {
    ($first:expr) => {
        $crate::pipeline::composition::PipelineComposer::new().start($first).build()
    };
    ($first:expr, $($rest:expr),+ $(,)?) => {
        {
            let mut composer = $crate::pipeline::composition::PipelineComposer::new().start($first);
            $(
                composer = composer.then($rest);
            )+
            composer.build()
        }
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pipeline::{GenerationOutput, PipelineOutput};

    // ── Shared mock helpers ───────────────────────────────────────────────────

    struct MockPipeline {
        name: String,
    }

    impl MockPipeline {
        fn new(name: &str) -> Self {
            Self {
                name: name.to_string(),
            }
        }
    }

    impl Pipeline for MockPipeline {
        type Input = String;
        type Output = PipelineOutput;

        fn __call__(&self, input: Self::Input) -> Result<Self::Output> {
            Ok(PipelineOutput::Generation(GenerationOutput {
                generated_text: format!("{}({})", self.name, input),
                sequences: None,
                scores: None,
            }))
        }
    }

    struct FailingPipeline;

    impl Pipeline for FailingPipeline {
        type Input = String;
        type Output = PipelineOutput;

        fn __call__(&self, _input: Self::Input) -> Result<Self::Output> {
            Err(TrustformersError::invalid_input_simple(
                "simulated failure".to_string(),
            ))
        }
    }

    // ── ComposedPipeline (A → B) ──────────────────────────────────────────────

    #[test]
    fn test_composed_pipeline() {
        let composed =
            ComposedPipeline::new(MockPipeline::new("first"), MockPipeline::new("second"));
        let result = composed
            .__call__("input".to_string())
            .expect("composed pipeline should succeed");
        if let PipelineOutput::Generation(gen) = result {
            assert_eq!(gen.generated_text, "second(first(input))");
        } else {
            panic!("Expected generation output");
        }
    }

    #[test]
    fn test_composed_pipeline_error_propagates() {
        let composed = ComposedPipeline::new(FailingPipeline, MockPipeline::new("second"));
        let result = composed.__call__("input".to_string());
        assert!(result.is_err(), "error in first stage should propagate");
    }

    #[test]
    fn test_composed_pipeline_second_stage_error() {
        let composed = ComposedPipeline::new(MockPipeline::new("first"), FailingPipeline);
        let result = composed.__call__("input".to_string());
        assert!(result.is_err(), "error in second stage should propagate");
    }

    // ── Pipeline chaining (A → B → C) ────────────────────────────────────────

    #[test]
    fn test_composed_pipeline_chain_three() {
        let ab = ComposedPipeline::new(MockPipeline::new("A"), MockPipeline::new("B"));
        let abc = ab.chain(MockPipeline::new("C"));
        let result = abc.__call__("x".to_string()).expect("3-stage chain should succeed");
        if let PipelineOutput::Generation(gen) = result {
            assert!(
                gen.generated_text.contains("C"),
                "final stage name should appear in output"
            );
            assert!(
                gen.generated_text.contains("x"),
                "original input should appear in output"
            );
        } else {
            panic!("Expected generation output");
        }
    }

    // ── PipelineChain (vec of stages) ─────────────────────────────────────────

    #[test]
    fn test_pipeline_chain() {
        let chain = PipelineChain::new()
            .add_stage(MockPipeline::new("stage1"))
            .add_stage(MockPipeline::new("stage2"))
            .add_stage(MockPipeline::new("stage3"));
        let result = chain.__call__("input".to_string()).expect("operation failed in test");
        if let PipelineOutput::Generation(gen) = result {
            assert_eq!(gen.generated_text, "stage3(stage2(stage1(input)))");
        } else {
            panic!("Expected generation output");
        }
    }

    #[test]
    fn test_pipeline_chain_empty_errors() {
        let chain = PipelineChain::new();
        let result = chain.__call__("input".to_string());
        assert!(result.is_err(), "empty chain should return an error");
    }

    #[test]
    fn test_pipeline_chain_single_stage() {
        let chain = PipelineChain::new().add_stage(MockPipeline::new("only"));
        let result = chain.__call__("val".to_string()).expect("single-stage chain should succeed");
        if let PipelineOutput::Generation(gen) = result {
            assert_eq!(gen.generated_text, "only(val)");
        } else {
            panic!("Expected generation output");
        }
    }

    #[test]
    fn test_pipeline_chain_error_propagates() {
        let chain = PipelineChain::new()
            .add_stage(MockPipeline::new("s1"))
            .add_stage(FailingPipeline);
        let result = chain.__call__("input".to_string());
        assert!(result.is_err(), "error in chain stage should propagate");
    }

    #[test]
    fn test_pipeline_chain_from_pipelines() {
        let stages: Vec<Box<dyn Pipeline<Input = String, Output = PipelineOutput>>> = vec![
            Box::new(MockPipeline::new("a")),
            Box::new(MockPipeline::new("b")),
        ];
        let chain = PipelineChain::from_pipelines(stages);
        let result = chain.__call__("x".to_string()).expect("from_pipelines chain should succeed");
        if let PipelineOutput::Generation(gen) = result {
            assert!(gen.generated_text.contains("b"));
        } else {
            panic!("Expected generation output");
        }
    }

    // ── PipelineChain batch ───────────────────────────────────────────────────

    #[test]
    fn test_pipeline_chain_batch() {
        let chain = PipelineChain::new()
            .add_stage(MockPipeline::new("p1"))
            .add_stage(MockPipeline::new("p2"));
        let results = chain
            .batch(vec!["a".to_string(), "b".to_string()])
            .expect("batch should succeed");
        assert_eq!(results.len(), 2);
    }

    // ── PipelineComposer ──────────────────────────────────────────────────────

    #[test]
    fn test_pipeline_composer() {
        let composed = PipelineComposer::new()
            .start(MockPipeline::new("first"))
            .then(MockPipeline::new("second"))
            .then(MockPipeline::new("third"))
            .build()
            .expect("operation failed in test");
        let result = composed.__call__("input".to_string()).expect("operation failed in test");
        if let PipelineOutput::Generation(gen) = result {
            // All three stages must participate: third(second(first(input)))
            assert_eq!(gen.generated_text, "third(second(first(input)))");
        } else {
            panic!("Expected generation output");
        }
    }

    #[test]
    fn test_pipeline_composer_three_stages() {
        let composed = PipelineComposer::new()
            .start(MockPipeline::new("A"))
            .then(MockPipeline::new("B"))
            .then(MockPipeline::new("C"))
            .build()
            .expect("three-stage composer should build");
        let result = composed.__call__("x".to_string()).expect("three-stage call should succeed");
        if let PipelineOutput::Generation(gen) = result {
            assert_eq!(
                gen.generated_text, "C(B(A(x)))",
                "all three stages must run in order"
            );
        } else {
            panic!("Expected generation output");
        }
    }

    #[test]
    fn test_pipeline_composer_empty_build() {
        let result = PipelineComposer::new().build();
        assert!(result.is_err(), "build() with zero stages must return Err");
    }

    #[test]
    fn test_pipeline_composer_empty_build_errors() {
        let result = PipelineComposer::new().build();
        assert!(result.is_err(), "building an empty composer should fail");
    }

    #[test]
    fn test_pipeline_composer_single_pipeline() {
        let composed = PipelineComposer::new()
            .start(MockPipeline::new("solo"))
            .build()
            .expect("single-pipeline composer should succeed");
        let result = composed.__call__("in".to_string()).expect("solo pipeline should succeed");
        if let PipelineOutput::Generation(gen) = result {
            assert!(gen.generated_text.contains("solo"));
        } else {
            panic!("Expected generation output");
        }
    }

    // ── TextConverter ────────────────────────────────────────────────────────

    #[test]
    fn test_text_converter_generation() {
        let converter = TextConverter;
        let output = PipelineOutput::Generation(GenerationOutput {
            generated_text: "hello world".to_string(),
            sequences: None,
            scores: None,
        });
        let text = converter.convert(output).expect("TextConverter should convert generation");
        assert_eq!(text, "hello world");
    }

    #[test]
    fn test_text_converter_summarization() {
        let converter = TextConverter;
        let output = PipelineOutput::Summarization("summary text".to_string());
        let text = converter.convert(output).expect("TextConverter should convert summarization");
        assert_eq!(text, "summary text");
    }

    #[test]
    fn test_text_converter_text_variant() {
        let converter = TextConverter;
        let output = PipelineOutput::Text("raw text".to_string());
        let text = converter.convert(output).expect("TextConverter should convert Text");
        assert_eq!(text, "raw text");
    }

    // ── Pipeline composition function ─────────────────────────────────────────

    #[test]
    fn test_compose_pipelines_function() {
        let composed = compose_pipelines(MockPipeline::new("first"), MockPipeline::new("second"));
        let result =
            composed.__call__("test".to_string()).expect("compose_pipelines should succeed");
        if let PipelineOutput::Generation(gen) = result {
            assert_eq!(gen.generated_text, "second(first(test))");
        } else {
            panic!("Expected generation output");
        }
    }

    // ── Intermediate output capture in chain ─────────────────────────────────

    #[test]
    fn test_intermediate_output_propagates_through_chain() {
        // A → B → C: verify B transforms A's output before C sees it
        let chain = PipelineChain::new()
            .add_stage(MockPipeline::new("A"))
            .add_stage(MockPipeline::new("B"))
            .add_stage(MockPipeline::new("C"));
        let result = chain.__call__("x".to_string()).expect("chain should succeed");
        if let PipelineOutput::Generation(gen) = result {
            // C sees B(A(x)) as its input
            assert_eq!(gen.generated_text, "C(B(A(x)))");
        } else {
            panic!("Expected generation output");
        }
    }

    // ── Macro test ────────────────────────────────────────────────────────────

    #[test]
    fn test_chain_pipelines_macro() {
        let result = chain_pipelines!(
            MockPipeline::new("p1"),
            MockPipeline::new("p2"),
            MockPipeline::new("p3")
        )
        .expect("operation failed in test");
        let output = result.__call__("test".to_string()).expect("operation failed in test");
        if let PipelineOutput::Generation(gen) = output {
            assert!(gen.generated_text.contains("test"));
        } else {
            panic!("Expected generation output");
        }
    }

    // ── Parallel fork/join conceptual test ───────────────────────────────────

    #[test]
    fn test_two_pipelines_independent_execution() {
        // Verify A and B can each be run independently (simulates parallel fork)
        let a = MockPipeline::new("fork_a");
        let b = MockPipeline::new("fork_b");
        let res_a = a.__call__("x".to_string()).expect("fork_a should succeed");
        let res_b = b.__call__("x".to_string()).expect("fork_b should succeed");
        if let (PipelineOutput::Generation(g_a), PipelineOutput::Generation(g_b)) = (res_a, res_b) {
            assert_ne!(
                g_a.generated_text, g_b.generated_text,
                "forked pipelines should produce different outputs"
            );
        } else {
            panic!("Expected generation outputs from both forks");
        }
    }

    // ── Pipeline metadata accumulation ────────────────────────────────────────

    #[test]
    fn test_pipeline_chain_accumulates_all_stage_names() {
        // The chain output should reflect all stage transformations
        let chain = PipelineChain::new()
            .add_stage(MockPipeline::new("alpha"))
            .add_stage(MockPipeline::new("beta"))
            .add_stage(MockPipeline::new("gamma"));
        let result = chain.__call__("seed".to_string()).expect("chain should succeed");
        if let PipelineOutput::Generation(gen) = result {
            assert!(
                gen.generated_text.contains("gamma"),
                "last stage must appear in output"
            );
            assert!(
                gen.generated_text.contains("seed"),
                "original input must appear in output"
            );
        }
    }
}