nv-perception 0.1.0

Stage model, detection/track types, perception artifacts, and derived signals for the NextVision runtime.
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
//! Stage pipeline composition — ordered collections with optional validation.
//!
//! [`StagePipeline`] provides a builder for composing stages into an
//! ordered pipeline. The resulting pipeline can be passed directly to
//! `FeedConfigBuilder::pipeline` or destructured into a `Vec<Box<dyn Stage>>`.
//!
//! # Example
//!
//! ```rust,no_run
//! use nv_perception::StagePipeline;
//! # use nv_perception::{Stage, StageContext, StageOutput};
//! # use nv_core::{StageId, StageError};
//! # struct MyDetector;
//! # impl Stage for MyDetector {
//! #     fn id(&self) -> StageId { StageId("det") }
//! #     fn process(&mut self, _: &StageContext<'_>) -> Result<StageOutput, StageError> {
//! #         Ok(StageOutput::empty())
//! #     }
//! # }
//! # struct MyTracker;
//! # impl Stage for MyTracker {
//! #     fn id(&self) -> StageId { StageId("trk") }
//! #     fn process(&mut self, _: &StageContext<'_>) -> Result<StageOutput, StageError> {
//! #         Ok(StageOutput::empty())
//! #     }
//! # }
//!
//! let pipeline = StagePipeline::builder()
//!     .add(MyDetector)
//!     .add(MyTracker)
//!     .build();
//!
//! assert_eq!(pipeline.len(), 2);
//! let stages = pipeline.into_stages();
//! ```

use crate::stage::{Stage, StageCapabilities, StageCategory};
use nv_core::id::StageId;

/// Controls whether [`StagePipeline::validate`] / [`validate_stages`]
/// warnings are ignored, logged, or promoted to hard errors.
///
/// Used by `FeedConfigBuilder` to wire validation into
/// the normal build path without requiring callers to invoke
/// `validate()` manually.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ValidationMode {
    /// Validation is skipped entirely (default).
    #[default]
    Off,
    /// Validation runs; warnings are returned but do not prevent
    /// pipeline construction.
    Warn,
    /// Validation runs; any warning is promoted to a hard error.
    Error,
}

/// An ordered, validated collection of perception stages.
///
/// Built via [`StagePipeline::builder()`]. The pipeline can be inspected
/// for stage IDs and categories before being consumed as a `Vec<Box<dyn Stage>>`.
pub struct StagePipeline {
    stages: Vec<Box<dyn Stage>>,
}

impl StagePipeline {
    /// Create a new builder.
    #[must_use]
    pub fn builder() -> StagePipelineBuilder {
        StagePipelineBuilder { stages: Vec::new() }
    }

    /// Number of stages in the pipeline.
    #[must_use]
    pub fn len(&self) -> usize {
        self.stages.len()
    }

    /// Whether the pipeline is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.stages.is_empty()
    }

    /// Get the stage IDs in execution order.
    #[must_use]
    pub fn stage_ids(&self) -> Vec<StageId> {
        self.stages.iter().map(|s| s.id()).collect()
    }

    /// Get `(StageId, StageCategory)` pairs in execution order.
    #[must_use]
    pub fn categories(&self) -> Vec<(StageId, StageCategory)> {
        self.stages.iter().map(|s| (s.id(), s.category())).collect()
    }

    /// Consume the pipeline and return the ordered stage list.
    ///
    /// Suitable for passing to `FeedConfigBuilder::stages` or
    /// `FeedConfigBuilder::pipeline`.
    #[must_use]
    pub fn into_stages(self) -> Vec<Box<dyn Stage>> {
        self.stages
    }

    /// Validate stage ordering based on declared [`StageCapabilities`].
    ///
    /// Returns a (possibly empty) list of warnings. Stages that return
    /// `None` from [`Stage::capabilities()`] are silently skipped.
    ///
    /// Warnings are advisory — the pipeline will still execute regardless.
    /// This allows pipeline builders to catch common ordering mistakes
    /// (e.g., placing a tracker before a detector) at construction time.
    #[must_use]
    pub fn validate(&self) -> Vec<ValidationWarning> {
        validate_stages(&self.stages)
    }
}

/// Advisory warning from [`StagePipeline::validate()`].
///
/// These do **not** prevent pipeline execution. They flag likely
/// composition mistakes that the builder may want to address.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationWarning {
    /// A stage declares that it consumes an artifact type that no
    /// earlier stage produces.
    UnsatisfiedDependency {
        /// The stage with the unsatisfied dependency.
        stage_id: StageId,
        /// Human-readable name of the missing artifact type.
        missing: &'static str,
    },
    /// Two or more stages share the same [`StageId`].
    DuplicateStageId {
        /// The duplicated stage ID.
        stage_id: StageId,
    },
}

/// Validate an ordered stage slice and return advisory warnings.
///
/// This is the same logic as [`StagePipeline::validate`] but operates
/// on a borrowed slice, making it usable by `FeedConfigBuilder`
/// without requiring a `StagePipeline`.
#[must_use]
pub fn validate_stages(stages: &[Box<dyn Stage>]) -> Vec<ValidationWarning> {
    let mut warnings = Vec::new();
    let mut detections_available = false;
    let mut tracks_available = false;

    for stage in stages {
        validate_one_stage(
            &**stage,
            &mut detections_available,
            &mut tracks_available,
            &mut warnings,
        );
    }

    // Check for duplicate stage IDs.
    let mut seen = std::collections::HashSet::new();
    for stage in stages {
        let id = stage.id();
        if !seen.insert(id) {
            warnings.push(ValidationWarning::DuplicateStageId { stage_id: id });
        }
    }

    warnings
}

/// Validate a batch-aware pipeline: pre-batch stages → optional batch
/// processor → post-batch stages.
///
/// Availability state (which artifact types have been produced) flows
/// through all three phases in order.  The batch processor's
/// `consumes_*` requirements are validated against pre-batch
/// availability, and its `produces_*` fields update availability for
/// the post-batch stages.  Duplicate stage IDs across all phases
/// (including the batch processor) are also reported.
///
/// This is the recommended entry-point for validating a pipeline that
/// includes a cross-feed batch processor.  For simple linear pipelines
/// without a batch processor, [`validate_stages`] is sufficient.
#[must_use]
pub fn validate_pipeline_phased(
    pre_batch: &[Box<dyn Stage>],
    batch_caps: Option<&StageCapabilities>,
    batch_id: Option<StageId>,
    post_batch: &[Box<dyn Stage>],
) -> Vec<ValidationWarning> {
    let mut warnings = Vec::new();
    let mut detections_available = false;
    let mut tracks_available = false;

    // Phase 1: pre-batch stages.
    for stage in pre_batch {
        validate_one_stage(
            &**stage,
            &mut detections_available,
            &mut tracks_available,
            &mut warnings,
        );
    }

    // Phase 2: batch processor capabilities (if declared).
    if let Some(caps) = batch_caps {
        if let Some(id) = batch_id {
            if caps.consumes_detections && !detections_available {
                warnings.push(ValidationWarning::UnsatisfiedDependency {
                    stage_id: id,
                    missing: "detections",
                });
            }
            if caps.consumes_tracks && !tracks_available {
                warnings.push(ValidationWarning::UnsatisfiedDependency {
                    stage_id: id,
                    missing: "tracks",
                });
            }
        }

        if caps.produces_detections {
            detections_available = true;
        }
        if caps.produces_tracks {
            tracks_available = true;
        }
    }

    // Phase 3: post-batch stages.
    for stage in post_batch {
        validate_one_stage(
            &**stage,
            &mut detections_available,
            &mut tracks_available,
            &mut warnings,
        );
    }

    // Duplicate ID check across all per-feed stages + batch processor.
    let mut seen = std::collections::HashSet::new();
    if let Some(id) = batch_id {
        seen.insert(id);
    }
    for stage in pre_batch.iter().chain(post_batch.iter()) {
        let id = stage.id();
        if !seen.insert(id) {
            warnings.push(ValidationWarning::DuplicateStageId { stage_id: id });
        }
    }

    warnings
}

/// Check a single stage's capabilities against current availability and
/// update availability from its outputs.
pub(crate) fn validate_one_stage(
    stage: &dyn Stage,
    detections_available: &mut bool,
    tracks_available: &mut bool,
    warnings: &mut Vec<ValidationWarning>,
) {
    let caps = match stage.capabilities() {
        Some(c) => c,
        None => return,
    };
    let id = stage.id();

    if caps.consumes_detections && !*detections_available {
        warnings.push(ValidationWarning::UnsatisfiedDependency {
            stage_id: id,
            missing: "detections",
        });
    }
    if caps.consumes_tracks && !*tracks_available {
        warnings.push(ValidationWarning::UnsatisfiedDependency {
            stage_id: id,
            missing: "tracks",
        });
    }
    if caps.produces_detections {
        *detections_available = true;
    }
    if caps.produces_tracks {
        *tracks_available = true;
    }
}

/// Builder for [`StagePipeline`].
pub struct StagePipelineBuilder {
    stages: Vec<Box<dyn Stage>>,
}

impl StagePipelineBuilder {
    /// Append a stage to the pipeline.
    #[must_use]
    #[allow(clippy::should_implement_trait)]
    pub fn add(mut self, stage: impl Stage) -> Self {
        self.stages.push(Box::new(stage));
        self
    }

    /// Append a boxed stage to the pipeline.
    #[must_use]
    pub fn add_boxed(mut self, stage: Box<dyn Stage>) -> Self {
        self.stages.push(stage);
        self
    }

    /// Build the pipeline.
    #[must_use]
    pub fn build(self) -> StagePipeline {
        StagePipeline {
            stages: self.stages,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::stage::StageCapabilities;
    use crate::{StageContext, StageOutput};
    use nv_core::error::StageError;

    struct TestStage {
        name: &'static str,
        cat: StageCategory,
    }

    impl Stage for TestStage {
        fn id(&self) -> StageId {
            StageId(self.name)
        }
        fn process(&mut self, _ctx: &StageContext<'_>) -> Result<StageOutput, StageError> {
            Ok(StageOutput::empty())
        }
        fn category(&self) -> StageCategory {
            self.cat
        }
    }

    #[test]
    fn builder_preserves_order() {
        let pipeline = StagePipeline::builder()
            .add(TestStage {
                name: "det",
                cat: StageCategory::FrameAnalysis,
            })
            .add(TestStage {
                name: "trk",
                cat: StageCategory::Association,
            })
            .add(TestStage {
                name: "temporal",
                cat: StageCategory::TemporalAnalysis,
            })
            .add(TestStage {
                name: "sink",
                cat: StageCategory::Sink,
            })
            .build();

        let ids: Vec<&str> = pipeline.stage_ids().iter().map(|s| s.as_str()).collect();
        assert_eq!(ids, vec!["det", "trk", "temporal", "sink"]);
    }

    #[test]
    fn categories_reported_correctly() {
        let pipeline = StagePipeline::builder()
            .add(TestStage {
                name: "det",
                cat: StageCategory::FrameAnalysis,
            })
            .add(TestStage {
                name: "trk",
                cat: StageCategory::Association,
            })
            .build();

        let cats = pipeline.categories();
        assert_eq!(cats[0].1, StageCategory::FrameAnalysis);
        assert_eq!(cats[1].1, StageCategory::Association);
    }

    #[test]
    fn into_stages_returns_owned_vec() {
        let pipeline = StagePipeline::builder()
            .add(TestStage {
                name: "a",
                cat: StageCategory::Custom,
            })
            .add(TestStage {
                name: "b",
                cat: StageCategory::Custom,
            })
            .build();

        let stages = pipeline.into_stages();
        assert_eq!(stages.len(), 2);
        assert_eq!(stages[0].id(), StageId("a"));
        assert_eq!(stages[1].id(), StageId("b"));
    }

    #[test]
    fn empty_pipeline() {
        let pipeline = StagePipeline::builder().build();
        assert!(pipeline.is_empty());
        assert_eq!(pipeline.len(), 0);
    }

    /// A test stage with configurable capabilities.
    struct CapStage {
        name: &'static str,
        caps: Option<StageCapabilities>,
    }

    impl Stage for CapStage {
        fn id(&self) -> StageId {
            StageId(self.name)
        }
        fn process(&mut self, _ctx: &StageContext<'_>) -> Result<StageOutput, StageError> {
            Ok(StageOutput::empty())
        }
        fn capabilities(&self) -> Option<StageCapabilities> {
            self.caps
        }
    }

    #[test]
    fn validate_happy_path() {
        let pipeline = StagePipeline::builder()
            .add(CapStage {
                name: "det",
                caps: Some(StageCapabilities::new().produces_detections()),
            })
            .add(CapStage {
                name: "trk",
                caps: Some(
                    StageCapabilities::new()
                        .consumes_detections()
                        .produces_tracks(),
                ),
            })
            .build();

        let warnings = pipeline.validate();
        assert!(warnings.is_empty());
    }

    #[test]
    fn validate_unsatisfied_detections() {
        let pipeline = StagePipeline::builder()
            .add(CapStage {
                name: "trk",
                caps: Some(StageCapabilities::new().consumes_detections()),
            })
            .add(CapStage {
                name: "det",
                caps: Some(StageCapabilities::new().produces_detections()),
            })
            .build();

        let warnings = pipeline.validate();
        assert_eq!(warnings.len(), 1);
        assert_eq!(
            warnings[0],
            ValidationWarning::UnsatisfiedDependency {
                stage_id: StageId("trk"),
                missing: "detections",
            }
        );
    }

    #[test]
    fn validate_unsatisfied_tracks() {
        let pipeline = StagePipeline::builder()
            .add(CapStage {
                name: "temporal",
                caps: Some(StageCapabilities::new().consumes_tracks()),
            })
            .build();

        let warnings = pipeline.validate();
        assert_eq!(warnings.len(), 1);
        assert!(matches!(
            &warnings[0],
            ValidationWarning::UnsatisfiedDependency {
                missing: "tracks",
                ..
            }
        ));
    }

    #[test]
    fn validate_skips_stages_without_capabilities() {
        let pipeline = StagePipeline::builder()
            .add(CapStage {
                name: "noop",
                caps: None,
            })
            .add(CapStage {
                name: "det",
                caps: Some(StageCapabilities::new().produces_detections()),
            })
            .build();

        let warnings = pipeline.validate();
        assert!(warnings.is_empty());
    }

    #[test]
    fn validate_duplicate_stage_ids() {
        let pipeline = StagePipeline::builder()
            .add(CapStage {
                name: "det",
                caps: None,
            })
            .add(CapStage {
                name: "det",
                caps: None,
            })
            .build();

        let warnings = pipeline.validate();
        assert_eq!(warnings.len(), 1);
        assert!(matches!(
            &warnings[0],
            ValidationWarning::DuplicateStageId { stage_id } if *stage_id == StageId("det")
        ));
    }

    #[test]
    fn validate_stages_fn_matches_pipeline_validate() {
        let stages: Vec<Box<dyn Stage>> = vec![
            Box::new(CapStage {
                name: "trk",
                caps: Some(StageCapabilities::new().consumes_detections()),
            }),
            Box::new(CapStage {
                name: "det",
                caps: Some(StageCapabilities::new().produces_detections()),
            }),
        ];

        let warnings = validate_stages(&stages);
        assert_eq!(warnings.len(), 1);
        assert_eq!(
            warnings[0],
            ValidationWarning::UnsatisfiedDependency {
                stage_id: StageId("trk"),
                missing: "detections",
            }
        );
    }

    #[test]
    fn validate_stages_fn_happy_path() {
        let stages: Vec<Box<dyn Stage>> = vec![
            Box::new(CapStage {
                name: "det",
                caps: Some(StageCapabilities::new().produces_detections()),
            }),
            Box::new(CapStage {
                name: "trk",
                caps: Some(
                    StageCapabilities::new()
                        .consumes_detections()
                        .produces_tracks(),
                ),
            }),
        ];

        let warnings = validate_stages(&stages);
        assert!(warnings.is_empty());
    }

    // --- validate_pipeline_phased tests ---

    #[test]
    fn phased_happy_path() {
        let pre: Vec<Box<dyn Stage>> = vec![Box::new(CapStage {
            name: "det",
            caps: Some(StageCapabilities::new().produces_detections()),
        })];
        let batch_caps = StageCapabilities::new()
            .consumes_detections()
            .produces_tracks();
        let post: Vec<Box<dyn Stage>> = vec![Box::new(CapStage {
            name: "temporal",
            caps: Some(StageCapabilities::new().consumes_tracks()),
        })];

        let warnings =
            validate_pipeline_phased(&pre, Some(&batch_caps), Some(StageId("batch")), &post);
        assert!(warnings.is_empty());
    }

    #[test]
    fn phased_batch_unsatisfied_dependency() {
        let pre: Vec<Box<dyn Stage>> = vec![];
        let batch_caps = StageCapabilities::new().consumes_detections();

        let warnings =
            validate_pipeline_phased(&pre, Some(&batch_caps), Some(StageId("batch")), &[]);
        assert_eq!(warnings.len(), 1);
        assert_eq!(
            warnings[0],
            ValidationWarning::UnsatisfiedDependency {
                stage_id: StageId("batch"),
                missing: "detections",
            }
        );
    }

    #[test]
    fn phased_post_batch_sees_batch_outputs() {
        let pre: Vec<Box<dyn Stage>> = vec![];
        let batch_caps = StageCapabilities::new().produces_detections();
        let post: Vec<Box<dyn Stage>> = vec![Box::new(CapStage {
            name: "trk",
            caps: Some(StageCapabilities::new().consumes_detections()),
        })];

        let warnings =
            validate_pipeline_phased(&pre, Some(&batch_caps), Some(StageId("batch")), &post);
        assert!(warnings.is_empty());
    }

    #[test]
    fn phased_duplicate_across_phases() {
        let pre: Vec<Box<dyn Stage>> = vec![Box::new(CapStage {
            name: "dup",
            caps: None,
        })];
        let post: Vec<Box<dyn Stage>> = vec![Box::new(CapStage {
            name: "dup",
            caps: None,
        })];

        let warnings = validate_pipeline_phased(&pre, None, None, &post);
        assert_eq!(warnings.len(), 1);
        assert!(matches!(
            &warnings[0],
            ValidationWarning::DuplicateStageId { stage_id } if *stage_id == StageId("dup")
        ));
    }

    #[test]
    fn phased_duplicate_with_batch_id() {
        let pre: Vec<Box<dyn Stage>> = vec![Box::new(CapStage {
            name: "batch",
            caps: None,
        })];

        let warnings = validate_pipeline_phased(&pre, None, Some(StageId("batch")), &[]);
        assert_eq!(warnings.len(), 1);
        assert!(matches!(
            &warnings[0],
            ValidationWarning::DuplicateStageId { stage_id } if *stage_id == StageId("batch")
        ));
    }

    #[test]
    fn phased_no_batch_processor() {
        let pre: Vec<Box<dyn Stage>> = vec![Box::new(CapStage {
            name: "det",
            caps: Some(StageCapabilities::new().produces_detections()),
        })];
        let post: Vec<Box<dyn Stage>> = vec![Box::new(CapStage {
            name: "trk",
            caps: Some(StageCapabilities::new().consumes_detections()),
        })];

        let warnings = validate_pipeline_phased(&pre, None, None, &post);
        assert!(warnings.is_empty());
    }
}