phoxal 0.44.0

Phoxal - production-oriented autonomous robot framework: the runtime engine and model (the api contract tree lives in phoxal-api, the typed bus in phoxal-bus).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
//! Unversioned canonical robot model.

mod motion;

pub use motion::{KinematicConfig, MotionLimits};

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use crate::model::component::capability::{Capability, Encoder, Motor, StructuralTarget};
use crate::model::component::{CapabilityRef, Component};
use crate::model::simulation::Simulation;
use crate::model::source;
use crate::model::structure::Structure;
use anyhow::{Context, Result, anyhow, bail};

const COMPONENTS_DIR: &str = "components";
const SIMULATION_FILE: &str = "simulation.yaml";

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BehaviorConfig {
    pub root: String,
    pub autostart: bool,
}

impl From<source::robot::v0::BehaviorConfig> for BehaviorConfig {
    fn from(value: source::robot::v0::BehaviorConfig) -> Self {
        Self {
            root: value.root,
            autostart: value.autostart,
        }
    }
}

/// One resolved component instance in the canonical robot.
#[derive(Debug, Clone)]
pub struct ComponentInstance {
    pub component_type: String,
    pub mount_link: String,
    direction_signs: BTreeMap<String, i8>,
}

/// Fully loaded runtime-facing model.
///
/// This type contains normalized robot facts, resolved component documents,
/// and the validated structure. It deliberately does not retain a public
/// source-version wrapper. Construction is intentionally fail-fast across the
/// complete declared bundle: every present `simulation.yaml` is part of the
/// fully loaded model and is validated even when the current participant runs
/// against physical hardware.
#[derive(Debug, Clone)]
pub struct Robot {
    id: String,
    namespace: String,
    behavior: Option<BehaviorConfig>,
    kinematic: KinematicConfig,
    motion_limits: MotionLimits,
    component_instances: BTreeMap<String, ComponentInstance>,
    component_types: BTreeMap<String, Component>,
    simulation_types: BTreeMap<String, Simulation>,
    structure: Structure,
}

struct ResolvedCapability<'a> {
    capability: &'a Capability,
    direction_sign: i8,
}

#[derive(Debug)]
struct NormalizedRobotSpec {
    id: String,
    namespace: String,
    behavior: Option<BehaviorConfig>,
    kinematic: KinematicConfig,
    motion_limits: MotionLimits,
    components: BTreeMap<String, NormalizedComponentInstance>,
}

#[derive(Debug)]
struct NormalizedComponentInstance {
    component_type: String,
    mount_link: String,
    parameters: BTreeMap<String, NormalizedParameter>,
}

#[derive(Debug)]
struct NormalizedParameter {
    capability_kind: &'static str,
    direction_sign: i8,
}

/// Exact documents and structure accepted by [`Robot::try_from_sources`].
///
/// The source versions are named independently in the tuple; this is a
/// version-sensitive tooling seam, not a version of the canonical graph.
pub type SourceInputs = (
    source::robot::v0::Manifest,
    BTreeMap<String, source::component::v0::Manifest>,
    BTreeMap<String, source::simulation::v0::Manifest>,
    Structure,
);

impl From<source::robot::v0::Manifest> for NormalizedRobotSpec {
    fn from(value: source::robot::v0::Manifest) -> Self {
        let source::robot::v0::Manifest {
            robot, behavior, ..
        } = value;
        Self {
            id: robot.id,
            namespace: robot.namespace,
            behavior: behavior.map(Into::into),
            kinematic: robot.kinematic.into(),
            motion_limits: robot.motion_limits.into(),
            components: robot
                .components
                .into_iter()
                .map(|(component_id, component)| {
                    let parameters = component
                        .parameters
                        .into_iter()
                        .map(|(capability_id, parameter)| {
                            use source::robot::v0::capability::Parameters;
                            let capability_kind = parameter.kind_name();
                            let direction_sign = match parameter {
                                Parameters::Motor(parameter) => parameter.direction_sign,
                                Parameters::Encoder(parameter) => parameter.direction_sign,
                                _ => 1,
                            };
                            (
                                capability_id,
                                NormalizedParameter {
                                    capability_kind,
                                    direction_sign,
                                },
                            )
                        })
                        .collect();
                    (
                        component_id,
                        NormalizedComponentInstance {
                            component_type: component.component,
                            mount_link: component.mount_link,
                            parameters,
                        },
                    )
                })
                .collect(),
        }
    }
}

impl Robot {
    pub fn read_from_dir(path: impl AsRef<Path>) -> Result<Self> {
        let (robot_source, component_sources, simulation_sources, structure) =
            Self::read_sources_from_dir(path)?;
        Self::try_from_sources(
            robot_source,
            component_sources,
            simulation_sources,
            structure,
        )
    }

    /// Load the exact source documents and structure used to build a robot.
    ///
    /// This version-sensitive seam is intended for fixtures and migration
    /// tooling that must adjust exact documents before normalizing them
    /// explicitly through [`Self::try_from_sources`]. Ordinary runtime
    /// consumers should call [`Self::read_from_dir`].
    pub fn read_sources_from_dir(path: impl AsRef<Path>) -> Result<SourceInputs> {
        let root = path.as_ref();
        let robot_source = source::robot::read_from_dir(root)?;
        let component_sources = Self::read_used_component_sources(root, &robot_source)?;
        let simulation_sources = Self::read_used_simulation_sources(root, &robot_source)?;
        let structure_path = root.join(&robot_source.robot.structure);
        let structure = Structure::read_from_file(&structure_path).with_context(|| {
            format!(
                "failed to read structure declared by robot/v0 document {} at {}",
                robot_source.robot.structure.display(),
                structure_path.display()
            )
        })?;

        Ok((
            robot_source,
            component_sources,
            simulation_sources,
            structure,
        ))
    }

    /// Normalize exact source documents and validate their cross-file invariants.
    ///
    /// Source readers already perform file-local validation. This method
    /// deliberately repeats it because callers may construct or modify exact
    /// DTOs directly before crossing the canonicalization boundary.
    pub fn try_from_sources(
        robot_source: source::robot::v0::Manifest,
        component_sources: BTreeMap<String, source::component::v0::Manifest>,
        simulation_sources: BTreeMap<String, source::simulation::v0::Manifest>,
        structure: Structure,
    ) -> Result<Self> {
        robot_source.validate().map_err(|errors| {
            source::robot::validation_error("robot.yaml passed to Robot::try_from_sources", errors)
        })?;
        structure
            .validate()
            .context("canonical robot structure validation failed")?;

        let component_types = component_sources
            .into_iter()
            .map(|(component_type, document)| {
                document.validate_for_component(&component_type).with_context(|| {
                    format!(
                        "invalid component/v0 document components/{component_type}/component.yaml"
                    )
                })?;
                Ok((component_type, Component::from(document)))
            })
            .collect::<Result<BTreeMap<_, _>>>()?;
        let simulation_types = simulation_sources
            .into_iter()
            .map(|(component_type, document)| {
                document.validate().with_context(|| {
                    format!(
                        "invalid simulation/v0 document \
                         components/{component_type}/simulation.yaml"
                    )
                })?;
                Ok((component_type, Simulation::from(document)))
            })
            .collect::<Result<BTreeMap<_, _>>>()?;

        Self::try_from_normalized(
            robot_source.into(),
            component_types,
            simulation_types,
            structure,
        )
    }

    fn try_from_normalized(
        robot: NormalizedRobotSpec,
        component_types: BTreeMap<String, Component>,
        simulation_types: BTreeMap<String, Simulation>,
        structure: Structure,
    ) -> Result<Self> {
        let component_instances = robot
            .components
            .into_iter()
            .map(|(component_id, source)| {
                let definition = component_types.get(&source.component_type).ok_or_else(|| {
                    anyhow!(
                        "robot.yaml components.{component_id} references component type '{}' \
                         without components/{}/component.yaml",
                        source.component_type,
                        source.component_type
                    )
                })?;
                let mut direction_signs = BTreeMap::new();
                for (capability_id, parameters) in source.parameters {
                    let capability = definition.capability(&capability_id).ok_or_else(|| {
                        anyhow!(
                            "robot.yaml components.{component_id}.parameters.{capability_id} \
                             references a capability missing from \
                             components/{}/component.yaml",
                            source.component_type
                        )
                    })?;
                    if capability.kind_name() != parameters.capability_kind {
                        bail!(
                            "robot.yaml components.{component_id}.parameters.{capability_id} \
                             has kind '{}', but components/{}/component.yaml defines '{}'",
                            parameters.capability_kind,
                            source.component_type,
                            capability.kind_name()
                        );
                    }
                    direction_signs.insert(capability_id, parameters.direction_sign);
                }
                Ok((
                    component_id,
                    ComponentInstance {
                        component_type: source.component_type,
                        mount_link: source.mount_link,
                        direction_signs,
                    },
                ))
            })
            .collect::<Result<BTreeMap<_, _>>>()?;

        let canonical = Self {
            id: robot.id,
            namespace: robot.namespace,
            behavior: robot.behavior,
            kinematic: robot.kinematic,
            motion_limits: robot.motion_limits,
            component_instances,
            component_types,
            simulation_types,
            structure,
        };
        canonical.validate_cross_document_invariants()?;
        Ok(canonical)
    }

    fn read_used_component_sources(
        root: &Path,
        robot: &source::robot::v0::Manifest,
    ) -> Result<BTreeMap<String, source::component::v0::Manifest>> {
        robot
            .used_component_types()
            .into_iter()
            .map(|component_type| {
                let component_root = component_config_path(root, component_type);
                let document = source::component::read_from_dir(&component_root).with_context(|| {
                    format!(
                        "failed to load component type '{component_type}' referenced by robot.yaml"
                    )
                })?;
                Ok((component_type.to_string(), document))
            })
            .collect()
    }

    fn read_used_simulation_sources(
        root: &Path,
        robot: &source::robot::v0::Manifest,
    ) -> Result<BTreeMap<String, source::simulation::v0::Manifest>> {
        robot
            .used_component_types()
            .into_iter()
            .filter_map(|component_type| {
                let component_root = component_config_path(root, component_type);
                component_root
                    .join(SIMULATION_FILE)
                    .is_file()
                    .then_some((component_type, component_root))
            })
            .map(|(component_type, component_root)| {
                let document =
                    source::simulation::read_from_dir(&component_root).with_context(|| {
                        format!(
                            "failed to load simulation/v0 document for component type \
                             '{component_type}' referenced by robot.yaml"
                        )
                    })?;
                Ok((component_type.to_string(), document))
            })
            .collect()
    }

    fn validate_cross_document_invariants(&self) -> Result<()> {
        for (component_id, instance) in &self.component_instances {
            if self.structure.link(&instance.mount_link).is_none() {
                bail!(
                    "robot.yaml components.{component_id}.mount_link '{}' is missing from the \
                     canonical structure",
                    instance.mount_link
                );
            }
        }

        for (component_type, simulation) in &self.simulation_types {
            let component = self.component_types.get(component_type).ok_or_else(|| {
                anyhow!(
                    "components/{component_type}/simulation.yaml has no matching \
                     component.yaml loaded by robot.yaml"
                )
            })?;
            for (capability_id, simulation_capability) in &simulation.capabilities {
                let capability = component.capability(capability_id).ok_or_else(|| {
                    anyhow!(
                        "components/{component_type}/simulation.yaml \
                         capabilities.{capability_id} has no matching capability in \
                         components/{component_type}/component.yaml"
                    )
                })?;
                if simulation_capability.kind_name() != capability.kind_name() {
                    bail!(
                        "components/{component_type}/simulation.yaml \
                         capabilities.{capability_id} has kind '{}', but \
                         components/{component_type}/component.yaml defines '{}'",
                        simulation_capability.kind_name(),
                        capability.kind_name()
                    );
                }
            }
        }

        match &self.kinematic {
            KinematicConfig::Differential {
                left_actuators,
                right_actuators,
                left_encoders,
                right_encoders,
                ..
            } => {
                for reference in left_actuators.iter().chain(right_actuators) {
                    self.require_motor(reference).with_context(|| {
                        format!("robot.yaml kinematic actuator '{reference}' is invalid")
                    })?;
                }
                for reference in left_encoders.iter().chain(right_encoders) {
                    self.require_encoder(reference).with_context(|| {
                        format!("robot.yaml kinematic encoder '{reference}' is invalid")
                    })?;
                }
            }
            KinematicConfig::Mecanum {
                front_left_actuator,
                front_right_actuator,
                rear_left_actuator,
                rear_right_actuator,
                ..
            } => {
                for reference in [
                    front_left_actuator,
                    front_right_actuator,
                    rear_left_actuator,
                    rear_right_actuator,
                ] {
                    self.require_motor(reference).with_context(|| {
                        format!("robot.yaml kinematic actuator '{reference}' is invalid")
                    })?;
                }
            }
            KinematicConfig::Ackermann {
                steering_actuator,
                drive_actuator,
                steering_encoder,
                drive_encoder,
                ..
            } => {
                for reference in [steering_actuator, drive_actuator] {
                    self.require_motor(reference).with_context(|| {
                        format!("robot.yaml kinematic actuator '{reference}' is invalid")
                    })?;
                }
                for reference in steering_encoder.iter().chain(drive_encoder) {
                    self.require_encoder(reference).with_context(|| {
                        format!("robot.yaml kinematic encoder '{reference}' is invalid")
                    })?;
                }
            }
            KinematicConfig::Omnidirectional {
                actuators,
                encoders,
            } => {
                for reference in actuators {
                    self.require_motor(reference).with_context(|| {
                        format!("robot.yaml kinematic actuator '{reference}' is invalid")
                    })?;
                }
                for reference in encoders {
                    self.require_encoder(reference).with_context(|| {
                        format!("robot.yaml kinematic encoder '{reference}' is invalid")
                    })?;
                }
            }
        }
        Ok(())
    }

    #[must_use]
    pub fn robot_id(&self) -> &str {
        &self.id
    }

    #[must_use]
    pub fn namespace(&self) -> &str {
        &self.namespace
    }

    #[must_use]
    pub fn behavior(&self) -> Option<&BehaviorConfig> {
        self.behavior.as_ref()
    }

    #[must_use]
    pub fn kinematic(&self) -> &KinematicConfig {
        &self.kinematic
    }

    #[must_use]
    pub const fn motion_limits(&self) -> MotionLimits {
        self.motion_limits
    }

    #[must_use]
    pub fn components(&self) -> &BTreeMap<String, ComponentInstance> {
        &self.component_instances
    }

    pub fn component_instance(&self, component_id: &str) -> Result<&ComponentInstance> {
        self.component_instances.get(component_id).ok_or_else(|| {
            anyhow!("component instance '{component_id}' is not defined in robot.yaml")
        })
    }

    pub fn component_for_instance(&self, component_id: &str) -> Result<&Component> {
        let instance = self.component_instance(component_id)?;
        self.component_types
            .get(&instance.component_type)
            .ok_or_else(|| {
                anyhow!(
                    "component type '{}' for instance '{component_id}' is not loaded",
                    instance.component_type
                )
            })
    }

    #[must_use]
    pub fn simulation_for_component_type(&self, component_type: &str) -> Option<&Simulation> {
        self.simulation_types.get(component_type)
    }

    pub fn simulation_for_instance(&self, component_id: &str) -> Result<Option<&Simulation>> {
        let instance = self.component_instance(component_id)?;
        Ok(self.simulation_for_component_type(&instance.component_type))
    }

    #[must_use]
    pub fn structure(&self) -> &Structure {
        &self.structure
    }

    pub fn capability(&self, reference: &CapabilityRef) -> Result<&Capability> {
        self.component_for_instance(&reference.component_id)?
            .capability(&reference.capability_id)
            .ok_or_else(|| {
                anyhow!("capability '{reference}' is not defined in its component.yaml document")
            })
    }

    pub fn camera_capabilities(&self) -> Result<Vec<CapabilityRef>> {
        let mut capabilities = Vec::new();
        for component_id in self.component_instances.keys() {
            let component = self.component_for_instance(component_id)?;
            capabilities.extend(
                component
                    .capabilities
                    .iter()
                    .filter(|(_, capability)| matches!(capability, Capability::Camera(_)))
                    .map(|(capability_id, _)| CapabilityRef::new(component_id, capability_id)),
            );
        }
        capabilities.sort();
        Ok(capabilities)
    }

    fn resolved_capability(&self, reference: &CapabilityRef) -> Result<ResolvedCapability<'_>> {
        Ok(ResolvedCapability {
            capability: self.capability(reference)?,
            direction_sign: self
                .component_instance(&reference.component_id)?
                .direction_signs
                .get(&reference.capability_id)
                .copied()
                .unwrap_or(1),
        })
    }

    pub fn require_motor(&self, reference: &CapabilityRef) -> Result<(&Motor, i8)> {
        let resolved = self.resolved_capability(reference)?;
        let Capability::Motor(motor) = resolved.capability else {
            bail!(
                "capability '{reference}' must reference a motor, found {}",
                resolved.capability.kind_name()
            );
        };
        Ok((motor, resolved.direction_sign))
    }

    pub fn require_encoder(&self, reference: &CapabilityRef) -> Result<(&Encoder, i8)> {
        let resolved = self.resolved_capability(reference)?;
        let Capability::Encoder(encoder) = resolved.capability else {
            bail!(
                "capability '{reference}' must reference an encoder, found {}",
                resolved.capability.kind_name()
            );
        };
        Ok((encoder, resolved.direction_sign))
    }

    pub fn require_link_target(&self, reference: &CapabilityRef) -> Result<String> {
        let target = self
            .capability(reference)?
            .target()
            .namespaced(&reference.component_id);
        let StructuralTarget::Link { id } = target else {
            bail!("capability '{reference}' must target a link");
        };
        if self.structure.link(&id).is_some() {
            Ok(id)
        } else {
            bail!("link target '{id}' for capability '{reference}' not found in structure")
        }
    }

    pub fn component_mount_link(&self, component_id: &str) -> Result<String> {
        Ok(self.component_instance(component_id)?.mount_link.clone())
    }
}

fn component_config_path(bundle_root: &Path, component_type: &str) -> PathBuf {
    bundle_root.join(COMPONENTS_DIR).join(component_type)
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use anyhow::Context as _;

    use crate::model::component::Component;
    use crate::model::structure::Structure;

    use super::{
        KinematicConfig, MotionLimits, NormalizedComponentInstance, NormalizedRobotSpec, Robot,
        SourceInputs,
    };

    fn fixture_root() -> &'static str {
        concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../fixture/robot/rgbd-imu-diff-drive"
        )
    }

    fn fixture_inputs() -> anyhow::Result<SourceInputs> {
        Robot::read_sources_from_dir(fixture_root())
    }

    fn canonicalization_error(inputs: SourceInputs) -> String {
        let (robot, components, simulations, structure) = inputs;
        format!(
            "{:#}",
            Robot::try_from_sources(robot, components, simulations, structure)
                .expect_err("mutated sources must fail")
        )
    }

    #[test]
    fn v0_sources_build_the_canonical_robot() -> anyhow::Result<()> {
        let robot = Robot::read_from_dir(fixture_root())?;
        assert_eq!(robot.robot_id(), "rgbd-imu-diff-drive");
        assert!(!robot.components().is_empty());
        assert!(
            robot
                .simulation_for_component_type("drive_motor")
                .is_some_and(|simulation| simulation.capabilities.contains_key("encoder"))
        );
        Ok(())
    }

    #[test]
    fn every_repository_robot_document_builds_the_canonical_model() -> anyhow::Result<()> {
        let manifest_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
        let roots = [
            "../fixture/robot/rgbd-diff-drive",
            "../fixture/robot/rgbd-imu-diff-drive",
            "../fixture/robot/rgbd-imu-gnss-outdoor",
            "../fixture/robot/rgbd-imu-orb-lowres",
            "../examples/hello-rover",
        ];

        for root in roots {
            let root = manifest_dir.join(root);
            Robot::read_from_dir(&root)
                .with_context(|| format!("failed to load repository robot {}", root.display()))?;
        }
        Ok(())
    }

    #[test]
    fn cross_file_errors_retain_authored_context() -> anyhow::Result<()> {
        let temp = tempfile::tempdir()?;
        std::fs::write(
            temp.path().join("robot.yaml"),
            r#"
schema: robot/v0
robot:
  id: broken
  namespace: test
  kinematic: { kind: omnidirectional, actuators: [drive.motor], encoders: [] }
  motion_limits: { max_linear_speed_mps: 1.0, max_angular_speed_radps: 1.0 }
  components:
    drive:
      component: missing
      mount_link: base_link
"#,
        )?;
        let error = Robot::read_from_dir(temp.path()).expect_err("missing component must fail");
        let message = format!("{error:#}");
        assert!(message.contains("robot.yaml"));
        assert!(message.contains("components/missing/component.yaml"));
        Ok(())
    }

    #[test]
    fn parameter_capability_must_exist_in_component_source() -> anyhow::Result<()> {
        let (mut robot, components, simulations, structure) = fixture_inputs()?;
        let drive = robot
            .robot
            .components
            .get_mut("front_left_drive")
            .expect("fixture drive instance");
        let parameter = drive.parameters["encoder"].clone();
        drive.parameters.insert("missing".to_string(), parameter);
        let error = canonicalization_error((robot, components, simulations, structure));
        assert!(error.contains("robot.yaml components.front_left_drive.parameters.missing"));
        assert!(error.contains("components/drive_motor/component.yaml"));
        Ok(())
    }

    #[test]
    fn parameter_kind_must_match_component_source() -> anyhow::Result<()> {
        let (mut robot, components, simulations, structure) = fixture_inputs()?;
        let drive = robot
            .robot
            .components
            .get_mut("front_left_drive")
            .expect("fixture drive instance");
        drive
            .parameters
            .insert("motor".to_string(), drive.parameters["encoder"].clone());
        let error = canonicalization_error((robot, components, simulations, structure));
        assert!(error.contains("robot.yaml components.front_left_drive.parameters.motor"));
        assert!(error.contains("components/drive_motor/component.yaml defines 'motor'"));
        Ok(())
    }

    #[test]
    fn component_mount_link_must_exist_in_structure() -> anyhow::Result<()> {
        let (mut robot, components, simulations, structure) = fixture_inputs()?;
        robot
            .robot
            .components
            .get_mut("front_left_drive")
            .expect("fixture drive instance")
            .mount_link = "missing_link".to_string();
        let error = canonicalization_error((robot, components, simulations, structure));
        assert!(error.contains("robot.yaml components.front_left_drive.mount_link"));
        assert!(error.contains("missing_link"));
        Ok(())
    }

    #[test]
    fn simulation_source_requires_a_matching_component_source() -> anyhow::Result<()> {
        let (robot, components, mut simulations, structure) = fixture_inputs()?;
        simulations.insert(
            "orphan".to_string(),
            crate::model::source::simulation::read_from_string(
                "schema: simulation/v0\ncapabilities: {}\n",
            )?,
        );
        let error = canonicalization_error((robot, components, simulations, structure));
        assert!(error.contains("components/orphan/simulation.yaml"));
        assert!(error.contains("component.yaml"));
        Ok(())
    }

    #[test]
    fn simulation_capability_requires_a_matching_component_capability() -> anyhow::Result<()> {
        let (robot, components, mut simulations, structure) = fixture_inputs()?;
        let simulation = simulations
            .get_mut("drive_motor")
            .expect("fixture simulation");
        simulation.capabilities.insert(
            "missing".to_string(),
            simulation.capabilities["encoder"].clone(),
        );
        let error = canonicalization_error((robot, components, simulations, structure));
        assert!(error.contains("components/drive_motor/simulation.yaml capabilities.missing"));
        assert!(error.contains("components/drive_motor/component.yaml"));
        Ok(())
    }

    #[test]
    fn simulation_capability_kind_must_match_component_source() -> anyhow::Result<()> {
        let (robot, components, mut simulations, structure) = fixture_inputs()?;
        let simulation = simulations
            .get_mut("drive_motor")
            .expect("fixture simulation");
        simulation.capabilities.insert(
            "motor".to_string(),
            simulation.capabilities["encoder"].clone(),
        );
        let error = canonicalization_error((robot, components, simulations, structure));
        assert!(error.contains("components/drive_motor/simulation.yaml capabilities.motor"));
        assert!(error.contains("components/drive_motor/component.yaml defines 'motor'"));
        Ok(())
    }

    #[test]
    fn malformed_simulation_source_rejects_the_fully_loaded_canonical_robot() -> anyhow::Result<()>
    {
        let root = tempfile::tempdir()?;
        std::fs::create_dir_all(root.path().join("components/sensor"))?;
        std::fs::write(
            root.path().join("robot.yaml"),
            r#"
schema: robot/v0
robot:
  id: simulation-validation
  namespace: test
  structure: structure.urdf
  kinematic: { kind: omnidirectional, actuators: [sensor.motor], encoders: [] }
  motion_limits: { max_linear_speed_mps: 1.0, max_angular_speed_radps: 1.0 }
  components:
    sensor:
      component: sensor
      mount_link: base_link
"#,
        )?;
        std::fs::write(
            root.path().join("structure.urdf"),
            r#"<robot name="test">
  <link name="base_footprint" />
  <link name="base_link" />
  <joint name="base" type="fixed">
    <parent link="base_footprint" />
    <child link="base_link" />
  </joint>
</robot>"#,
        )?;
        std::fs::write(
            root.path().join("components/sensor/component.yaml"),
            r#"schema: component/v0
capabilities:
  motor:
    kind: motor
    command: velocity
    target: { kind: joint, id: base }
"#,
        )?;
        std::fs::write(
            root.path().join("components/sensor/simulation.yaml"),
            "schema: simulation/v0\ncapabilities:\n  Bad Id: { kind: range }\n",
        )?;

        let error =
            Robot::read_from_dir(root.path()).expect_err("invalid simulation source must fail");
        let message = format!("{error:#}");
        assert!(message.contains("simulation/v0"));
        assert!(message.contains("components/sensor/simulation.yaml"));
        Ok(())
    }

    #[test]
    fn camera_capabilities_include_color_and_exclude_depth() -> anyhow::Result<()> {
        let robot = Robot::read_from_dir(fixture_root())?;
        let cameras = robot
            .camera_capabilities()?
            .into_iter()
            .map(|reference| reference.to_string())
            .collect::<Vec<_>>();
        assert!(cameras.contains(&"front_camera.rgb".to_string()));
        assert!(!cameras.contains(&"front_camera.depth".to_string()));
        Ok(())
    }

    #[test]
    fn future_robot_version_can_normalize_with_component_v0() -> anyhow::Result<()> {
        struct FutureRobotV1 {
            id: String,
        }

        impl From<FutureRobotV1> for NormalizedRobotSpec {
            fn from(value: FutureRobotV1) -> Self {
                Self {
                    id: value.id,
                    namespace: "future".to_string(),
                    behavior: None,
                    kinematic: KinematicConfig::Omnidirectional {
                        actuators: Vec::new(),
                        encoders: Vec::new(),
                    },
                    motion_limits: MotionLimits {
                        max_linear_speed_mps: 1.0,
                        max_angular_speed_radps: 1.0,
                    },
                    components: BTreeMap::from([(
                        "sensor".to_string(),
                        NormalizedComponentInstance {
                            component_type: "sensor_v0".to_string(),
                            mount_link: "base_link".to_string(),
                            parameters: BTreeMap::new(),
                        },
                    )]),
                }
            }
        }

        let component_v0 = crate::model::source::component::read_from_string(
            "schema: component/v0\ncapabilities: {}\n",
        )?;
        let structure = Structure::from_urdf_str(
            r#"<robot name="future">
  <link name="base_footprint" />
  <link name="base_link" />
  <joint name="base" type="fixed">
    <parent link="base_footprint" />
    <child link="base_link" />
  </joint>
</robot>"#,
        )?;

        let robot = Robot::try_from_normalized(
            FutureRobotV1 {
                id: "future-v1".to_string(),
            }
            .into(),
            BTreeMap::from([("sensor_v0".to_string(), Component::from(component_v0))]),
            BTreeMap::new(),
            structure,
        )?;
        assert_eq!(robot.robot_id(), "future-v1");
        assert_eq!(
            robot.component_instance("sensor")?.component_type,
            "sensor_v0"
        );
        Ok(())
    }
}