lenso-app-plan 0.1.4

Immutable application plans for the Lenso vNext 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
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
use std::{
    collections::{BTreeMap, BTreeSet},
    error::Error,
    fmt,
};

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{
    AppComposition, CapabilityBinding, CapabilityCardinality, CapabilityEndpointPlan,
    CapabilityRequirementPlan, ExecutionClassId, ExecutionLaneId, ExecutionLanePlan,
    ModuleCriticality, ModuleInstancePlan, PlanResolutionError, RestartPolicy,
};

fn empty_configuration() -> Value {
    Value::Object(serde_json::Map::new())
}

fn default_entrypoint() -> String {
    "default".to_owned()
}

fn default_execution_lanes() -> Vec<ExecutionLanePlan> {
    vec![ExecutionLanePlan::new("main")]
}

/// Package-owned facts for one executable Module entrypoint.
///
/// This data is generated and shipped by a Module package. App authors select
/// descriptors; they do not repeat Capability or lifecycle metadata.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ModuleDescriptor {
    package_id: String,
    package_revision: String,
    #[serde(default = "default_entrypoint")]
    entrypoint: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    configuration_schema: Option<Value>,
    provided_capabilities: Vec<CapabilityEndpointPlan>,
    required_capabilities: Vec<CapabilityRequirementPlan>,
    execution_class: ExecutionClassId,
    restart_policy: RestartPolicy,
    criticality: ModuleCriticality,
}

impl ModuleDescriptor {
    /// Starts a descriptor for one locked package revision and entrypoint.
    pub fn new(package_id: impl Into<String>, package_revision: impl Into<String>) -> Self {
        Self {
            package_id: package_id.into(),
            package_revision: package_revision.into(),
            entrypoint: default_entrypoint(),
            configuration_schema: None,
            provided_capabilities: Vec::new(),
            required_capabilities: Vec::new(),
            execution_class: ExecutionClassId::native_rust(),
            restart_policy: RestartPolicy::default(),
            criticality: ModuleCriticality::default(),
        }
    }

    #[must_use]
    pub fn with_entrypoint(mut self, entrypoint: impl Into<String>) -> Self {
        self.entrypoint = entrypoint.into();
        self
    }

    /// Selects the package-owned JSON Schema used to validate Instance configuration.
    #[must_use]
    pub fn with_configuration_schema(mut self, schema: Value) -> Self {
        self.configuration_schema = Some(schema);
        self
    }

    #[must_use]
    pub fn with_capability(mut self, capability: CapabilityEndpointPlan) -> Self {
        self.provided_capabilities.push(capability);
        self
    }

    #[must_use]
    pub fn with_requirement(mut self, requirement: CapabilityRequirementPlan) -> Self {
        self.required_capabilities.push(requirement);
        self
    }

    #[must_use]
    pub fn with_execution_class(mut self, execution_class: ExecutionClassId) -> Self {
        self.execution_class = execution_class;
        self
    }

    #[must_use]
    pub const fn with_restart_policy(mut self, restart_policy: RestartPolicy) -> Self {
        self.restart_policy = restart_policy;
        self
    }

    #[must_use]
    pub const fn with_criticality(mut self, criticality: ModuleCriticality) -> Self {
        self.criticality = criticality;
        self
    }

    pub fn package_id(&self) -> &str {
        &self.package_id
    }
    pub fn package_revision(&self) -> &str {
        &self.package_revision
    }
    pub fn entrypoint(&self) -> &str {
        &self.entrypoint
    }
    pub const fn configuration_schema(&self) -> Option<&Value> {
        self.configuration_schema.as_ref()
    }
    pub fn provided_capabilities(&self) -> &[CapabilityEndpointPlan] {
        &self.provided_capabilities
    }
    pub fn required_capabilities(&self) -> &[CapabilityRequirementPlan] {
        &self.required_capabilities
    }
    pub fn execution_class(&self) -> &ExecutionClassId {
        &self.execution_class
    }
    pub const fn restart_policy(&self) -> RestartPolicy {
        self.restart_policy
    }
    pub const fn criticality(&self) -> ModuleCriticality {
        self.criticality
    }
}

/// Descriptor set discovered from the exact package lock selected for an App.
#[derive(Clone, Debug, Default)]
pub struct ModuleCatalog {
    descriptors: BTreeMap<(String, String), ModuleDescriptor>,
}

impl ModuleCatalog {
    pub fn new(
        descriptors: impl IntoIterator<Item = ModuleDescriptor>,
    ) -> Result<Self, DefinitionResolutionError> {
        let mut catalog = Self::default();
        for descriptor in descriptors {
            catalog.insert(descriptor)?;
        }
        Ok(catalog)
    }

    pub fn insert(
        &mut self,
        descriptor: ModuleDescriptor,
    ) -> Result<(), DefinitionResolutionError> {
        let key = (descriptor.package_id.clone(), descriptor.entrypoint.clone());
        if self.descriptors.insert(key.clone(), descriptor).is_some() {
            return Err(DefinitionResolutionError::DuplicateDescriptor {
                package_id: key.0,
                entrypoint: key.1,
            });
        }
        Ok(())
    }

    pub fn get(&self, package_id: &str, entrypoint: &str) -> Option<&ModuleDescriptor> {
        self.descriptors
            .get(&(package_id.to_owned(), entrypoint.to_owned()))
    }
}

/// One App-local use of a package-owned Module descriptor.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ModuleSelection {
    key: String,
    package: String,
    #[serde(default = "default_entrypoint")]
    entrypoint: String,
    #[serde(default = "empty_configuration")]
    configuration: Value,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    execution_lane: Option<String>,
}

impl ModuleSelection {
    pub fn new(key: impl Into<String>, package: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            package: package.into(),
            entrypoint: default_entrypoint(),
            configuration: empty_configuration(),
            execution_lane: None,
        }
    }

    #[must_use]
    pub fn with_entrypoint(mut self, entrypoint: impl Into<String>) -> Self {
        self.entrypoint = entrypoint.into();
        self
    }

    #[must_use]
    pub fn with_configuration(mut self, configuration: Value) -> Self {
        self.configuration = configuration;
        self
    }

    #[must_use]
    pub fn with_execution_lane(mut self, execution_lane: impl Into<String>) -> Self {
        self.execution_lane = Some(execution_lane.into());
        self
    }

    pub fn key(&self) -> &str {
        &self.key
    }
    pub fn package(&self) -> &str {
        &self.package
    }
    pub fn entrypoint(&self) -> &str {
        &self.entrypoint
    }
    pub fn configuration(&self) -> &Value {
        &self.configuration
    }
    pub fn execution_lane(&self) -> Option<&str> {
        self.execution_lane.as_deref()
    }
}

/// An explicit answer to an otherwise ambiguous one/optional Capability slot.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct BindingDecision {
    consumer: String,
    capability_id: String,
    provider: String,
}

impl BindingDecision {
    pub fn new(
        consumer: impl Into<String>,
        capability_id: impl Into<String>,
        provider: impl Into<String>,
    ) -> Self {
        Self {
            consumer: consumer.into(),
            capability_id: capability_id.into(),
            provider: provider.into(),
        }
    }
    pub fn consumer(&self) -> &str {
        &self.consumer
    }
    pub fn capability_id(&self) -> &str {
        &self.capability_id
    }
    pub fn provider(&self) -> &str {
        &self.provider
    }
}

/// Small, human-authored input for one App variant.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct AppDefinition {
    name: String,
    #[serde(default)]
    modules: Vec<ModuleSelection>,
    #[serde(default)]
    decisions: Vec<BindingDecision>,
    #[serde(default = "default_execution_lanes")]
    execution_lanes: Vec<ExecutionLanePlan>,
}

impl AppDefinition {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            modules: Vec::new(),
            decisions: Vec::new(),
            execution_lanes: default_execution_lanes(),
        }
    }

    #[must_use]
    pub fn with_module(mut self, module: ModuleSelection) -> Self {
        self.modules.push(module);
        self
    }
    #[must_use]
    pub fn with_decision(mut self, decision: BindingDecision) -> Self {
        self.decisions.push(decision);
        self
    }
    #[must_use]
    pub fn with_execution_lanes(mut self, lanes: Vec<ExecutionLanePlan>) -> Self {
        self.execution_lanes = lanes;
        self
    }
    pub fn name(&self) -> &str {
        &self.name
    }
    pub fn modules(&self) -> &[ModuleSelection] {
        &self.modules
    }
    pub fn decisions(&self) -> &[BindingDecision] {
        &self.decisions
    }

    /// Derives the complete, explicit App Composition from selected descriptors.
    pub fn derive(
        &self,
        catalog: &ModuleCatalog,
    ) -> Result<AppComposition, DefinitionResolutionError> {
        let instances = materialize_instances(&self.modules, catalog)?;
        let bindings = derive_bindings(&instances, &self.decisions)?;
        let composition = AppComposition::new(instances, bindings)
            .with_execution_lanes(self.execution_lanes.clone());
        composition
            .resolve()
            .map_err(DefinitionResolutionError::InvalidComposition)?;
        Ok(composition)
    }
}

fn materialize_instances(
    selections: &[ModuleSelection],
    catalog: &ModuleCatalog,
) -> Result<Vec<ModuleInstancePlan>, DefinitionResolutionError> {
    let mut instances = Vec::with_capacity(selections.len());
    let mut instance_keys = BTreeSet::new();
    for selection in selections {
        if !instance_keys.insert(selection.key.clone()) {
            return Err(DefinitionResolutionError::DuplicateModuleSelection {
                instance_key: selection.key.clone(),
            });
        }
        let descriptor = catalog
            .get(selection.package(), selection.entrypoint())
            .ok_or_else(|| DefinitionResolutionError::UnknownDescriptor {
                instance_key: selection.key.clone(),
                package_id: selection.package.clone(),
                entrypoint: selection.entrypoint.clone(),
            })?;
        instances.push(materialize_instance(selection, descriptor)?);
    }
    Ok(instances)
}

fn materialize_instance(
    selection: &ModuleSelection,
    descriptor: &ModuleDescriptor,
) -> Result<ModuleInstancePlan, DefinitionResolutionError> {
    validate_configuration(
        selection.configuration(),
        descriptor.configuration_schema(),
        selection.key(),
    )?;
    let configuration = serde_json::to_string(&selection.configuration).map_err(|error| {
        DefinitionResolutionError::InvalidConfiguration {
            instance_key: selection.key.clone(),
            detail: error.to_string(),
        }
    })?;
    let mut instance = ModuleInstancePlan::new(selection.key(), descriptor.package_id())
        .with_entrypoint(descriptor.entrypoint())
        .with_package_revision(descriptor.package_revision())
        .with_configuration(configuration)
        .with_execution_class(descriptor.execution_class().clone())
        .with_restart_policy(descriptor.restart_policy())
        .with_criticality(descriptor.criticality())
        .with_execution_lane(ExecutionLaneId::new(
            selection.execution_lane().unwrap_or("main"),
        ));
    for capability in descriptor.provided_capabilities() {
        instance = instance.with_capability(capability.clone());
    }
    for requirement in descriptor.required_capabilities() {
        instance = instance.with_requirement(requirement.clone());
    }
    Ok(instance)
}

const SUPPORTED_SCHEMA_KEYWORDS: &[&str] = &[
    "additionalProperties",
    "const",
    "enum",
    "items",
    "properties",
    "required",
    "type",
    "x-lenso-sensitive",
];
const SCHEMA_METADATA_KEYWORDS: &[&str] = &[
    "$anchor",
    "$comment",
    "$id",
    "$schema",
    "default",
    "deprecated",
    "description",
    "examples",
    "readOnly",
    "title",
    "writeOnly",
];

fn validate_configuration(
    configuration: &Value,
    schema: Option<&Value>,
    instance_key: &str,
) -> Result<(), DefinitionResolutionError> {
    match schema {
        Some(schema) => validate_json_schema(configuration, schema, "$", instance_key),
        None if configuration == &empty_configuration() => Ok(()),
        None => Err(invalid_configuration(
            instance_key,
            "$",
            "non-empty configuration requires a package-owned schema",
        )),
    }
}

fn validate_json_schema(
    value: &Value,
    schema: &Value,
    path: &str,
    instance_key: &str,
) -> Result<(), DefinitionResolutionError> {
    let schema = schema.as_object().ok_or_else(|| {
        invalid_configuration(
            instance_key,
            path,
            "configuration schema must be a JSON object",
        )
    })?;
    if let Some(keyword) = schema.keys().find(|keyword| {
        !SUPPORTED_SCHEMA_KEYWORDS.contains(&keyword.as_str())
            && !SCHEMA_METADATA_KEYWORDS.contains(&keyword.as_str())
    }) {
        return Err(invalid_configuration(
            instance_key,
            path,
            format!("unsupported JSON Schema keyword `{keyword}`"),
        ));
    }
    if schema
        .get("x-lenso-sensitive")
        .and_then(Value::as_bool)
        .unwrap_or(false)
    {
        let secret_reference = value.as_object().is_some_and(|object| {
            object.len() == 1 && object.get("secret_ref").is_some_and(Value::is_string)
        });
        return secret_reference.then_some(()).ok_or_else(|| {
            invalid_configuration(instance_key, path, "sensitive value must be a secret_ref")
        });
    }
    validate_schema_type(value, schema, path, instance_key)?;
    if let Some(expected) = schema.get("const")
        && value != expected
    {
        return Err(invalid_configuration(
            instance_key,
            path,
            "value does not match schema const",
        ));
    }
    if let Some(values) = schema.get("enum") {
        let values = values.as_array().ok_or_else(|| {
            invalid_configuration(instance_key, path, "schema enum must be an array")
        })?;
        if !values.iter().any(|expected| expected == value) {
            return Err(invalid_configuration(
                instance_key,
                path,
                "value is not in schema enum",
            ));
        }
    }
    validate_required(value, schema, path, instance_key)?;
    validate_properties(value, schema, path, instance_key)?;
    validate_items(value, schema, path, instance_key)
}

fn validate_schema_type(
    value: &Value,
    schema: &serde_json::Map<String, Value>,
    path: &str,
    instance_key: &str,
) -> Result<(), DefinitionResolutionError> {
    let Some(expected) = schema.get("type") else {
        return Ok(());
    };
    let expected = expected
        .as_str()
        .ok_or_else(|| invalid_configuration(instance_key, path, "schema type must be a string"))?;
    if ![
        "array", "boolean", "integer", "null", "number", "object", "string",
    ]
    .contains(&expected)
    {
        return Err(invalid_configuration(
            instance_key,
            path,
            format!("unsupported JSON Schema type `{expected}`"),
        ));
    }
    let actual = match value {
        Value::Null => "null",
        Value::Bool(_) => "boolean",
        Value::Number(_) => "number",
        Value::String(_) => "string",
        Value::Array(_) => "array",
        Value::Object(_) => "object",
    };
    let integer = value
        .as_number()
        .is_some_and(|number| number.is_i64() || number.is_u64());
    if expected != actual && !(expected == "integer" && integer) {
        return Err(invalid_configuration(
            instance_key,
            path,
            format!("expected {expected}, found {actual}"),
        ));
    }
    Ok(())
}

fn validate_required(
    value: &Value,
    schema: &serde_json::Map<String, Value>,
    path: &str,
    instance_key: &str,
) -> Result<(), DefinitionResolutionError> {
    let Some(required) = schema.get("required") else {
        return Ok(());
    };
    let required = required.as_array().ok_or_else(|| {
        invalid_configuration(instance_key, path, "schema required must be an array")
    })?;
    let object = value.as_object().ok_or_else(|| {
        invalid_configuration(instance_key, path, "required fields need an object")
    })?;
    for name in required {
        let name = name.as_str().ok_or_else(|| {
            invalid_configuration(
                instance_key,
                path,
                "schema required entries must be strings",
            )
        })?;
        if !object.contains_key(name) {
            return Err(invalid_configuration(
                instance_key,
                &format!("{path}.{name}"),
                "required field is missing",
            ));
        }
    }
    Ok(())
}

fn validate_properties(
    value: &Value,
    schema: &serde_json::Map<String, Value>,
    path: &str,
    instance_key: &str,
) -> Result<(), DefinitionResolutionError> {
    let empty = serde_json::Map::new();
    let properties = match schema.get("properties") {
        Some(properties) => properties.as_object().ok_or_else(|| {
            invalid_configuration(instance_key, path, "schema properties must be an object")
        })?,
        None => &empty,
    };
    let Some(object) = value.as_object() else {
        return Ok(());
    };
    for (name, child_schema) in properties {
        if let Some(child) = object.get(name) {
            validate_json_schema(child, child_schema, &format!("{path}.{name}"), instance_key)?;
        }
    }
    if let Some(additional) = schema.get("additionalProperties") {
        match additional {
            Value::Bool(true) => {}
            Value::Bool(false) => {
                if let Some(name) = object.keys().find(|name| !properties.contains_key(*name)) {
                    return Err(invalid_configuration(
                        instance_key,
                        &format!("{path}.{name}"),
                        "additional property is not allowed",
                    ));
                }
            }
            additional_schema => {
                for (name, child) in object {
                    if !properties.contains_key(name) {
                        validate_json_schema(
                            child,
                            additional_schema,
                            &format!("{path}.{name}"),
                            instance_key,
                        )?;
                    }
                }
            }
        }
    }
    Ok(())
}

fn validate_items(
    value: &Value,
    schema: &serde_json::Map<String, Value>,
    path: &str,
    instance_key: &str,
) -> Result<(), DefinitionResolutionError> {
    let Some(item_schema) = schema.get("items") else {
        return Ok(());
    };
    if !item_schema.is_object() {
        return Err(invalid_configuration(
            instance_key,
            path,
            "schema items must be an object",
        ));
    }
    let Some(items) = value.as_array() else {
        return Ok(());
    };
    for (index, item) in items.iter().enumerate() {
        validate_json_schema(item, item_schema, &format!("{path}[{index}]"), instance_key)?;
    }
    Ok(())
}

fn invalid_configuration(
    instance_key: &str,
    path: &str,
    detail: impl Into<String>,
) -> DefinitionResolutionError {
    DefinitionResolutionError::InvalidConfiguration {
        instance_key: instance_key.to_owned(),
        detail: format!("{path}: {}", detail.into()),
    }
}

fn derive_bindings(
    instances: &[ModuleInstancePlan],
    authored_decisions: &[BindingDecision],
) -> Result<Vec<CapabilityBinding>, DefinitionResolutionError> {
    let decisions = index_decisions(authored_decisions)?;
    let mut consumed_decisions = BTreeSet::new();
    let mut bindings = Vec::new();
    for consumer in instances {
        for requirement in consumer.required_capabilities() {
            let key = (
                consumer.instance_key().to_owned(),
                requirement.capability_id().to_owned(),
            );
            let candidates = provider_candidates(instances, requirement);
            let selected = select_providers(
                requirement,
                &key,
                candidates,
                &decisions,
                &mut consumed_decisions,
            )?;
            bindings.extend(selected.into_iter().map(|provider| {
                CapabilityBinding::new(
                    consumer.instance_key(),
                    requirement.capability_id(),
                    requirement.descriptor_version(),
                    provider,
                )
            }));
        }
    }
    if let Some(((consumer, capability_id), provider)) = decisions
        .iter()
        .find(|(key, _)| !consumed_decisions.contains(*key))
    {
        return Err(DefinitionResolutionError::UnusedDecision {
            consumer: consumer.clone(),
            capability_id: capability_id.clone(),
            provider: provider.clone(),
        });
    }
    Ok(bindings)
}

fn provider_candidates(
    instances: &[ModuleInstancePlan],
    requirement: &CapabilityRequirementPlan,
) -> Vec<String> {
    let mut candidates = instances
        .iter()
        .filter(|provider| {
            provider.provided_capabilities().iter().any(|endpoint| {
                endpoint.capability_id() == requirement.capability_id()
                    && endpoint.descriptor_version() == requirement.descriptor_version()
            })
        })
        .map(|provider| provider.instance_key().to_owned())
        .collect::<Vec<_>>();
    candidates.sort();
    candidates
}

fn select_providers(
    requirement: &CapabilityRequirementPlan,
    key: &(String, String),
    candidates: Vec<String>,
    decisions: &BTreeMap<(String, String), String>,
    consumed_decisions: &mut BTreeSet<(String, String)>,
) -> Result<Vec<String>, DefinitionResolutionError> {
    if requirement.cardinality() == CapabilityCardinality::Many {
        return Ok(candidates);
    }
    if let Some(provider) = decisions.get(key) {
        consumed_decisions.insert(key.clone());
        if !candidates.contains(provider) {
            return Err(DefinitionResolutionError::InvalidDecision {
                consumer: key.0.clone(),
                capability_id: key.1.clone(),
                provider: provider.clone(),
                candidates,
            });
        }
        return Ok(vec![provider.clone()]);
    }
    match candidates.len() {
        0 if requirement.cardinality() == CapabilityCardinality::One => {
            Err(DefinitionResolutionError::MissingProvider {
                consumer: key.0.clone(),
                capability_id: key.1.clone(),
                descriptor_version: requirement.descriptor_version().to_owned(),
            })
        }
        0 => Ok(Vec::new()),
        1 => Ok(candidates),
        _ => Err(DefinitionResolutionError::NeedsDecision {
            consumer: key.0.clone(),
            capability_id: key.1.clone(),
            candidates,
        }),
    }
}

fn index_decisions(
    decisions: &[BindingDecision],
) -> Result<BTreeMap<(String, String), String>, DefinitionResolutionError> {
    let mut indexed = BTreeMap::new();
    for decision in decisions {
        let key = (decision.consumer.clone(), decision.capability_id.clone());
        if indexed
            .insert(key.clone(), decision.provider.clone())
            .is_some()
        {
            return Err(DefinitionResolutionError::DuplicateDecision {
                consumer: key.0,
                capability_id: key.1,
            });
        }
    }
    Ok(indexed)
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DefinitionResolutionError {
    DuplicateDescriptor {
        package_id: String,
        entrypoint: String,
    },
    DuplicateModuleSelection {
        instance_key: String,
    },
    UnknownDescriptor {
        instance_key: String,
        package_id: String,
        entrypoint: String,
    },
    InvalidConfiguration {
        instance_key: String,
        detail: String,
    },
    DuplicateDecision {
        consumer: String,
        capability_id: String,
    },
    MissingProvider {
        consumer: String,
        capability_id: String,
        descriptor_version: String,
    },
    NeedsDecision {
        consumer: String,
        capability_id: String,
        candidates: Vec<String>,
    },
    InvalidDecision {
        consumer: String,
        capability_id: String,
        provider: String,
        candidates: Vec<String>,
    },
    UnusedDecision {
        consumer: String,
        capability_id: String,
        provider: String,
    },
    InvalidComposition(PlanResolutionError),
}

impl fmt::Display for DefinitionResolutionError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::DuplicateDescriptor {
                package_id,
                entrypoint,
            } => write!(
                formatter,
                "duplicate Module Descriptor `{package_id}#{entrypoint}`"
            ),
            Self::DuplicateModuleSelection { instance_key } => {
                write!(formatter, "duplicate Module selection `{instance_key}`")
            }
            Self::UnknownDescriptor {
                instance_key,
                package_id,
                entrypoint,
            } => write!(
                formatter,
                "Module selection `{instance_key}` refers to unknown descriptor `{package_id}#{entrypoint}`"
            ),
            Self::InvalidConfiguration {
                instance_key,
                detail,
            } => write!(
                formatter,
                "Module selection `{instance_key}` has invalid configuration: {detail}"
            ),
            Self::DuplicateDecision {
                consumer,
                capability_id,
            } => write!(
                formatter,
                "duplicate binding decision for `{consumer}` Capability `{capability_id}`"
            ),
            Self::MissingProvider {
                consumer,
                capability_id,
                descriptor_version,
            } => write!(
                formatter,
                "consumer `{consumer}` has no provider for Capability `{capability_id}` version `{descriptor_version}`"
            ),
            Self::NeedsDecision {
                consumer,
                capability_id,
                candidates,
            } => write!(
                formatter,
                "consumer `{consumer}` needs a provider decision for Capability `{capability_id}`; candidates: {}",
                candidates.join(", ")
            ),
            Self::InvalidDecision {
                consumer,
                capability_id,
                provider,
                candidates,
            } => write!(
                formatter,
                "binding decision `{consumer}` -> `{provider}` for Capability `{capability_id}` is invalid; candidates: {}",
                candidates.join(", ")
            ),
            Self::UnusedDecision {
                consumer,
                capability_id,
                provider,
            } => write!(
                formatter,
                "binding decision `{consumer}` -> `{provider}` for Capability `{capability_id}` does not match a one/optional requirement"
            ),
            Self::InvalidComposition(error) => {
                write!(formatter, "derived App Composition is invalid: {error}")
            }
        }
    }
}

impl Error for DefinitionResolutionError {}